Skip to content

Commit

Permalink
feat(server): r2 cdn endpoints (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan authored Sep 25, 2023
1 parent 01a3bdf commit 984ed3a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 71 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"devDependencies": {
"@antfu/eslint-config": "latest",
"@cloudflare/workers-types": "^4.20230914.0",
"@libsql/client": "^0.3.4",
"@nuxt/devtools": "latest",
"@paralleldrive/cuid2": "^2.2.2",
Expand Down
122 changes: 54 additions & 68 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import type { R2Bucket } from '@cloudflare/workers-types'

export default defineCachedEventHandler(async (event) => {
export default defineProtectedEventHandler(async (event) => {
const bucket: R2Bucket = event.context.cloudflare.env.R2_SSTAA
const file = await bucket.get(event.context.params!.path)

if (!file) {
throw createError({
statusCode: 404,
statusMessage: 'Not found',
})
}

return sendStream(event, file.body as any)
const headers = new Headers()
file.writeHttpMetadata(headers as any)
headers.set('etag', file.httpEtag)
setResponseHeaders(event, Object.fromEntries(headers.entries()))

return file.body
}, {
maxAge: 60,
cache: {
maxAge: 60 * 60,
},
})

0 comments on commit 984ed3a

Please # to comment.