fix CORS
This commit is contained in:
parent
08175d3a7c
commit
39e6cccc3f
|
|
@ -120,32 +120,29 @@ export class TldrawDurableObject {
|
||||||
|
|
||||||
// what happens when someone tries to connect to this room?
|
// what happens when someone tries to connect to this room?
|
||||||
async handleConnect(request: IRequest): Promise<Response> {
|
async handleConnect(request: IRequest): Promise<Response> {
|
||||||
// extract query params from request
|
|
||||||
const sessionId = request.query.sessionId as string
|
const sessionId = request.query.sessionId as string
|
||||||
if (!sessionId) return error(400, 'Missing sessionId')
|
if (!sessionId) return error(400, 'Missing sessionId')
|
||||||
|
|
||||||
// Create the websocket pair for the client
|
|
||||||
const { 0: clientWebSocket, 1: serverWebSocket } = new WebSocketPair()
|
const { 0: clientWebSocket, 1: serverWebSocket } = new WebSocketPair()
|
||||||
// @ts-ignore
|
|
||||||
serverWebSocket.accept()
|
serverWebSocket.accept()
|
||||||
|
|
||||||
// load the room, or retrieve it if it's already loaded
|
|
||||||
const room = await this.getRoom()
|
const room = await this.getRoom()
|
||||||
|
|
||||||
// connect the client to the room
|
|
||||||
room.handleSocketConnect({ sessionId, socket: serverWebSocket })
|
room.handleSocketConnect({ sessionId, socket: serverWebSocket })
|
||||||
|
|
||||||
// return the websocket connection to the client
|
const origin = request.headers.get('Origin') || '*'
|
||||||
|
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 101,
|
status: 101,
|
||||||
webSocket: clientWebSocket,
|
webSocket: clientWebSocket,
|
||||||
headers: {
|
headers: {
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': origin,
|
||||||
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, UPGRADE',
|
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, UPGRADE',
|
||||||
'Access-Control-Allow-Headers': '*',
|
'Access-Control-Allow-Headers': '*',
|
||||||
'Access-Control-Allow-Credentials': 'true'
|
'Access-Control-Allow-Credentials': 'true',
|
||||||
|
'Upgrade': 'websocket',
|
||||||
|
'Connection': 'Upgrade'
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
getRoom() {
|
getRoom() {
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,11 @@ const router = AutoRouter<IRequest, [env: Environment, ctx: ExecutionContext]>({
|
||||||
.get('/connect/:roomId', (request, env) => {
|
.get('/connect/:roomId', (request, env) => {
|
||||||
const id = env.TLDRAW_DURABLE_OBJECT.idFromName(request.params.roomId)
|
const id = env.TLDRAW_DURABLE_OBJECT.idFromName(request.params.roomId)
|
||||||
const room = env.TLDRAW_DURABLE_OBJECT.get(id)
|
const room = env.TLDRAW_DURABLE_OBJECT.get(id)
|
||||||
return room.fetch(request.url, { headers: request.headers, body: request.body })
|
return room.fetch(request.url, {
|
||||||
|
headers: request.headers,
|
||||||
|
body: request.body,
|
||||||
|
method: request.method
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// assets can be uploaded to the bucket under /uploads:
|
// assets can be uploaded to the bucket under /uploads:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue