11// @ts -check
2+ /// <reference lib="deno.worker" />
3+ import { handleRequest } from '../bootstrap.mjs'
24
35/**
4- * @typedef {import('./types.js ').Message } Message
5- * @typedef {import('./types.js ').RunResponseStartMessage } RunResponseStartMessage
6- * @typedef {import('./types.js ').RunResponseChunkMessage } RunResponseChunkMessage
7- * @typedef {import('./types.js ').RunResponseEndMessage } RunResponseEndMessage
6+ * @typedef {import('./types.ts ').Message } Message
7+ * @typedef {import('./types.ts ').RunResponseStartMessage } RunResponseStartMessage
8+ * @typedef {import('./types.ts ').RunResponseChunkMessage } RunResponseChunkMessage
9+ * @typedef {import('./types.ts ').RunResponseEndMessage } RunResponseEndMessage
810 */
911
1012const consoleLog = globalThis . console . log
1113/** @type {Map<string, string> } */
1214const fetchRewrites = new Map ( )
1315
14- self . onmessage = async ( e ) => {
16+ /** @type {DedicatedWorkerGlobalScope } */
17+ // @ts -ignore We are inside a worker, so the global scope is `DedicatedWorkerGlobalScope`.
18+ const worker = globalThis
19+
20+ worker . addEventListener ( 'message' , async ( e ) => {
1521 const message = /** @type {Message } */ ( e . data )
1622
1723 if ( message . type === 'request' ) {
18- const { handleRequest } = await import ( message . data . bootstrapURL )
1924 const body = message . data . method === 'GET' || message . data . method === 'HEAD' ? undefined : message . data . body
2025 const req = new Request ( message . data . url , {
2126 body,
@@ -35,12 +40,14 @@ self.onmessage = async (e) => {
3540 await Promise . allSettled ( imports )
3641
3742 const res = await handleRequest ( req , functions , {
43+ // @ts -ignore TODO: Figure out why `fetchRewrites` is not being picked up
44+ // as part of the type.
3845 fetchRewrites,
3946 rawLogger : consoleLog ,
4047 requestTimeout : message . data . timeout ,
4148 } )
4249
43- self . postMessage (
50+ worker . postMessage (
4451 /** @type {RunResponseStartMessage } */ ( {
4552 type : 'responseStart' ,
4653 data : {
@@ -58,7 +65,8 @@ self.onmessage = async (e) => {
5865 break
5966 }
6067
61- self . postMessage (
68+ // @ts -expect-error TODO: Figure out type mismatch.
69+ worker . postMessage (
6270 /** @type {RunResponseChunkMessage } */ ( {
6371 type : 'responseChunk' ,
6472 data : { chunk : value } ,
@@ -68,10 +76,10 @@ self.onmessage = async (e) => {
6876 }
6977 }
7078
71- self . postMessage ( /** @type {RunResponseEndMessage } */ ( { type : 'responseEnd' } ) )
79+ worker . postMessage ( /** @type {RunResponseEndMessage } */ ( { type : 'responseEnd' } ) )
7280
7381 return
7482 }
7583
7684 throw new Error ( 'Unsupported message' )
77- }
85+ } )
0 commit comments