-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathworker.ts
More file actions
28 lines (22 loc) · 1018 Bytes
/
Copy pathworker.ts
File metadata and controls
28 lines (22 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import nitro from '#nitro-output'
import { SitePresence } from './worker/site-presence'
export { SitePresence }
const PRESENCE_PATH = '/api/presence'
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url)
if (url.pathname !== PRESENCE_PATH) return nitro.fetch(request, env, ctx)
if (request.method !== 'GET') {
return Response.json({ error: 'Method not allowed' }, { status: 405 })
}
if (request.headers.get('Upgrade')?.toLowerCase() !== 'websocket') {
return Response.json({ error: 'WebSocket upgrade required' }, { status: 426 })
}
const visitorId = url.searchParams.get('visitorId')
if (!visitorId || !UUID_PATTERN.test(visitorId)) {
return Response.json({ error: 'Invalid visitor ID' }, { status: 400 })
}
return env.SITE_PRESENCE.getByName('site').fetch(request)
},
}