
Adds an assets server to the demo worker, and reworks the existing asset server to use the same code. There are a few simplifications to the code due to some DX improvements working with R2 and caches. I also removed the `HEAD` request from the assets server: i took a look at our logs and it's not actually used at all. This also fixes an issue where users could overwrite the contents of the asset uploads bucket. ### Change type - [x] `other`
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
/// <reference no-default-lib="true"/>
|
|
/// <reference types="@cloudflare/workers-types" />
|
|
|
|
import {
|
|
createSentry,
|
|
handleUserAssetGet,
|
|
handleUserAssetUpload,
|
|
notFound,
|
|
} from '@tldraw/worker-shared'
|
|
import { WorkerEntrypoint } from 'cloudflare:workers'
|
|
import { createCors } from 'itty-cors'
|
|
import { Router } from 'itty-router'
|
|
import { Environment } from './types'
|
|
|
|
const { preflight, corsify } = createCors({ origins: ['*'] })
|
|
|
|
export default class Worker extends WorkerEntrypoint<Environment> {
|
|
readonly router = Router()
|
|
.all('*', preflight)
|
|
.get('/uploads/:objectName', async (request) => {
|
|
return handleUserAssetGet({
|
|
request,
|
|
bucket: this.env.UPLOADS,
|
|
objectName: request.params.objectName,
|
|
context: this.ctx,
|
|
})
|
|
})
|
|
.post('/uploads/:objectName', async (request) => {
|
|
return handleUserAssetUpload({
|
|
request,
|
|
bucket: this.env.UPLOADS,
|
|
objectName: request.params.objectName,
|
|
context: this.ctx,
|
|
})
|
|
})
|
|
.all('*', notFound)
|
|
|
|
override async fetch(request: Request) {
|
|
try {
|
|
return await this.router.handle(request, this.env, this.ctx).then(corsify)
|
|
} catch (error) {
|
|
const sentry = createSentry(this.ctx, this.env, request)
|
|
console.error(error)
|
|
// eslint-disable-next-line deprecation/deprecation
|
|
sentry?.captureException(error)
|
|
return new Response('Something went wrong', {
|
|
status: 500,
|
|
statusText: 'Internal Server Error',
|
|
})
|
|
}
|
|
}
|
|
}
|