@mercuryworkshop/scramjet-controller 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/controller.api.js +18 -17
- package/dist/controller.api.js.map +1 -1
- package/dist/controller.inject.js +1 -1
- package/dist/controller.inject.js.map +1 -1
- package/dist/controller.sw.js +1 -1
- package/dist/controller.sw.js.map +1 -1
- package/dist/types/cache.d.ts +39 -0
- package/dist/types/index.d.ts +42 -11
- package/dist/types/inject.d.ts +9 -7
- package/dist/types/symbols.d.ts +1 -0
- package/dist/types/typesEntry.d.ts +5 -0
- package/package.json +6 -4
- package/src/cache.ts +473 -0
- package/src/index.ts +491 -120
- package/src/inject.ts +192 -68
- package/src/sw.ts +99 -27
- package/src/symbols.ts +1 -0
- package/src/types.d.ts +53 -9
- package/src/typesEntry.ts +6 -0
- package/tsconfig.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.sw.js","sources":["webpack://$scramjetController/./packages/rpc/index.ts","webpack://$scramjetController/webpack/runtime/define_property_getters","webpack://$scramjetController/webpack/runtime/has_own_property","webpack://$scramjetController/webpack/runtime/make_namespace_object","webpack://$scramjetController/./packages/controller/src/sw.ts"],"sourcesContent":["type Serverbound = {\n\tmethod1: [{ paramA: string; paramB: number }, boolean];\n\tmethod2: [string, number];\n};\n\ntype Clientbound = {\n\tmethod1: [number];\n\tmethod2: [boolean, string];\n};\n\nexport type RpcDescription = {\n\t[method: string]: [args: any, returnType: any] | [args: any] | [];\n};\n\nexport type MethodsDefinition<Description extends RpcDescription> = {\n\t[Method in keyof Description]: (\n\t\t...args: Description[Method] extends [infer A, ...any[]] ? [A] : []\n\t) => Description[Method] extends [any, infer R]\n\t\t? Promise<[R, Transferable[]]>\n\t\t: Promise<void>;\n};\n\nexport class RpcHelper<\n\tLocal extends RpcDescription,\n\tRemote extends RpcDescription,\n> {\n\tcounter: number = 0;\n\tpromiseCallbacks: Map<\n\t\tnumber,\n\t\t{ resolve: (value: any) => void; reject: (reason?: any) => void }\n\t> = new Map();\n\tconstructor(\n\t\tprivate methods: MethodsDefinition<Local>,\n\t\tprivate id: string,\n\t\tprivate sendRaw: (data: any, transfer: Transferable[]) => void\n\t) {}\n\n\trecieve(data: any) {\n\t\tif (data === undefined || data === null || typeof data !== \"object\") return;\n\t\tconst dt = data[this.id];\n\t\tif (dt === undefined || dt === null || typeof dt !== \"object\") return;\n\n\t\tconst type = dt.$type;\n\n\t\tif (type === \"response\") {\n\t\t\tconst token = dt.$token;\n\t\t\tconst data = dt.$data;\n\t\t\tconst error = dt.$error;\n\t\t\tconst cb = this.promiseCallbacks.get(token);\n\t\t\tif (!cb) return;\n\t\t\tthis.promiseCallbacks.delete(token);\n\t\t\tif (error !== undefined) {\n\t\t\t\tcb.reject(new Error(error));\n\t\t\t} else {\n\t\t\t\tcb.resolve(data);\n\t\t\t}\n\t\t} else if (type === \"request\") {\n\t\t\tconst method = dt.$method as keyof Local;\n\t\t\tconst args = dt.$args as Local[typeof method][0];\n\t\t\t(this.methods[method] as any)(args)\n\t\t\t\t.then((r: any) => {\n\t\t\t\t\tthis.sendRaw(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t\t\t$type: \"response\",\n\t\t\t\t\t\t\t\t$token: dt.$token,\n\t\t\t\t\t\t\t\t$data: r?.[0],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tr?.[1]\n\t\t\t\t\t);\n\t\t\t\t})\n\t\t\t\t.catch((err: any) => {\n\t\t\t\t\tconsole.error(err);\n\t\t\t\t\tthis.sendRaw(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t\t\t$type: \"response\",\n\t\t\t\t\t\t\t\t$token: dt.$token,\n\t\t\t\t\t\t\t\t$error: err?.toString() || \"Unknown error\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[]\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t}\n\t}\n\n\tcall<Method extends keyof Remote>(\n\t\tmethod: Method,\n\t\targs: Remote[Method][0],\n\t\ttransfer: Transferable[] = []\n\t): Promise<Remote[Method][1]> {\n\t\tlet token = this.counter++;\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tthis.promiseCallbacks.set(token, { resolve, reject });\n\t\t\tthis.sendRaw(\n\t\t\t\t{\n\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t$type: \"request\",\n\t\t\t\t\t\t$method: method,\n\t\t\t\t\t\t$args: args,\n\t\t\t\t\t\t$token: token,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\ttransfer\n\t\t\t);\n\t\t});\n\t}\n}\n","__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","declare var clients: Clients;\nimport { RpcHelper } from \"@mercuryworkshop/rpc\";\nimport type { Controllerbound, SWbound } from \"./types\";\nimport type { RawHeaders } from \"@mercuryworkshop/proxy-transports\";\nimport { ScramjetHeaders } from \"@mercuryworkshop/scramjet\";\n\nfunction makeId(): string {\n\treturn Math.random().toString(36).substring(2, 10);\n}\n\nlet cookieResolvers: Record<string, (value: void) => void> = {};\naddEventListener(\"message\", (e) => {\n\tif (!e.data) return;\n\tif (typeof e.data != \"object\") return;\n\tif (e.data.$sw$setCookieDone && typeof e.data.$sw$setCookieDone == \"object\") {\n\t\tconst done = e.data.$sw$setCookieDone;\n\n\t\tconst resolver = cookieResolvers[done.id];\n\t\tif (resolver) {\n\t\t\tresolver();\n\t\t\tdelete cookieResolvers[done.id];\n\t\t}\n\t}\n\n\tif (\n\t\te.data.$sw$initRemoteTransport &&\n\t\ttypeof e.data.$sw$initRemoteTransport == \"object\"\n\t) {\n\t\tconst { port, prefix } = e.data.$sw$initRemoteTransport;\n\n\t\tconst relevantcontroller = tabs.find((tab) =>\n\t\t\tnew URL(prefix).pathname.startsWith(tab.prefix)\n\t\t);\n\t\tif (!relevantcontroller) {\n\t\t\tconsole.error(\"No relevant controller found for transport init\");\n\t\t\treturn;\n\t\t}\n\t\trelevantcontroller.rpc.call(\"initRemoteTransport\", port, [port]);\n\t}\n});\n\nclass ControllerReference {\n\trpc: RpcHelper<SWbound, Controllerbound>;\n\n\tconstructor(\n\t\tpublic prefix: string,\n\t\tpublic id: string,\n\t\tport: MessagePort\n\t) {\n\t\tthis.rpc = new RpcHelper(\n\t\t\t{\n\t\t\t\tsendSetCookie: async ({ url, cookie }) => {\n\t\t\t\t\tlet clients = await self.clients.matchAll();\n\t\t\t\t\tlet promises = [];\n\n\t\t\t\t\tfor (const client of clients) {\n\t\t\t\t\t\tlet id = makeId();\n\t\t\t\t\t\tclient.postMessage({\n\t\t\t\t\t\t\t$controller$setCookie: {\n\t\t\t\t\t\t\t\turl,\n\t\t\t\t\t\t\t\tcookie,\n\t\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t\tpromises.push(\n\t\t\t\t\t\t\tnew Promise<void>((resolve) => {\n\t\t\t\t\t\t\t\tcookieResolvers[id] = resolve;\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tawait Promise.race([\n\t\t\t\t\t\tnew Promise<void>((resolve) =>\n\t\t\t\t\t\t\tsetTimeout(() => {\n\t\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t\t\"timed out waiting for set cookie response (deadlock?)\"\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tresolve();\n\t\t\t\t\t\t\t}, 1000)\n\t\t\t\t\t\t),\n\t\t\t\t\t\tpromises,\n\t\t\t\t\t]);\n\t\t\t\t},\n\t\t\t},\n\t\t\t\"tabchannel-\" + id,\n\t\t\t(data, transfer) => {\n\t\t\t\tport.postMessage(data, transfer);\n\t\t\t}\n\t\t);\n\t\tport.addEventListener(\"message\", (e) => {\n\t\t\tthis.rpc.recieve(e.data);\n\t\t});\n\t\tport.onmessageerror = console.error;\n\n\t\tthis.rpc.call(\"ready\", null);\n\t}\n}\n\nconst tabs: ControllerReference[] = [];\n\naddEventListener(\"message\", (e) => {\n\tif (!e.data) return;\n\tif (typeof e.data != \"object\") return;\n\tif (!e.data.$controller$init) return;\n\tif (typeof e.data.$controller$init != \"object\") return;\n\tconst init = e.data.$controller$init;\n\n\ttabs.push(new ControllerReference(init.prefix, init.id, e.ports[0]));\n});\n\nexport function shouldRoute(event: FetchEvent): boolean {\n\tconst url = new URL(event.request.url);\n\tconst tab = tabs.find((tab) => url.pathname.startsWith(tab.prefix));\n\treturn tab !== undefined;\n}\n\nexport async function route(event: FetchEvent): Promise<Response> {\n\ttry {\n\t\tconst url = new URL(event.request.url);\n\t\tconst tab = tabs.find((tab) => url.pathname.startsWith(tab.prefix))!;\n\t\tconst client = await clients.get(event.clientId);\n\n\t\tconst rawheaders: RawHeaders = [...event.request.headers];\n\n\t\tconst response = await tab.rpc.call(\n\t\t\t\"request\",\n\t\t\t{\n\t\t\t\trawUrl: event.request.url,\n\t\t\t\tdestination: event.request.destination,\n\t\t\t\tmode: event.request.mode,\n\t\t\t\treferrer: event.request.referrer,\n\t\t\t\tmethod: event.request.method,\n\t\t\t\tbody: event.request.body,\n\t\t\t\tcache: event.request.cache,\n\t\t\t\tforceCrossOriginIsolated: false,\n\t\t\t\tinitialHeaders: rawheaders,\n\t\t\t\trawClientUrl: client ? client.url : undefined,\n\t\t\t},\n\t\t\tevent.request.body instanceof ReadableStream ||\n\t\t\t\t// @ts-expect-error the types for fetchevent are messed up\n\t\t\t\tevent.request.body instanceof ArrayBuffer\n\t\t\t\t? [event.request.body]\n\t\t\t\t: undefined\n\t\t);\n\n\t\treturn new Response(response.body, {\n\t\t\tstatus: response.status,\n\t\t\tstatusText: response.statusText,\n\t\t\theaders: response.headers,\n\t\t});\n\t} catch (e) {\n\t\tconsole.error(\"Service Worker error:\", e);\n\t\treturn new Response(\n\t\t\t\"Internal Service Worker Error: \" + (e as Error).message,\n\t\t\t{\n\t\t\t\tstatus: 500,\n\t\t\t}\n\t\t);\n\t}\n}\n"],"names":["RpcHelper","Map","methods","id","sendRaw","data","dt","type","token","error","cb","undefined","Error","method","args","r","err","console","transfer","Promise","resolve","reject","e","Object","Symbol","cookieResolvers","addEventListener","done","resolver","port","prefix","relevantcontroller","tabs","tab","URL","ControllerReference","url","cookie","clients1","self","promises","client","Math","setTimeout","shouldRoute","event","route","clients","rawheaders","response","ReadableStream","ArrayBuffer","Response","init"],"mappings":"gEAsBO,OAAMA,E,kBAIZ,SAAkB,CAAE,AACpB,kBAGI,IAAIC,GAAM,AACd,aACSC,CAAiC,CACjCC,CAAU,CACVC,CAAsD,CAC7D,C,KAHOF,OAAO,CAAPA,E,KACAC,EAAE,CAAFA,E,KACAC,OAAO,CAAPA,CACN,CAEH,QAAQC,CAAS,CAAE,CAClB,GAAIA,MAAAA,GAAuC,AAAgB,UAAhB,OAAOA,EAAmB,OACrE,IAAMC,EAAKD,CAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CACxB,GAAIC,MAAAA,GAAmC,AAAc,UAAd,OAAOA,EAAiB,OAE/D,IAAMC,EAAOD,EAAG,KAAK,CAErB,GAAIC,AAAS,aAATA,EAAqB,CACxB,IAAMC,EAAQF,EAAG,MAAM,CACjBD,EAAOC,EAAG,KAAK,CACfG,EAAQH,EAAG,MAAM,CACjBI,EAAK,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAACF,GACrC,G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,IAAI,CAAC,gBAAgB,CAHrB,OAIb,IAAM+B,EAAO/B,EAAE,IAAI,CAAC,gBAAgB,CAEpCU,EAAK,IAAI,CAAC,IAAIG,EAAoBkB,EAAK,MAAM,CAAEA,EAAK,EAAE,CAAE/B,EAAE,KAAK,CAAC,EAAE,EACnE,E"}
|
|
1
|
+
{"version":3,"file":"controller.sw.js","sources":["webpack://$scramjetController/./packages/rpc/index.ts","webpack://$scramjetController/webpack/runtime/define_property_getters","webpack://$scramjetController/webpack/runtime/has_own_property","webpack://$scramjetController/webpack/runtime/make_namespace_object","webpack://$scramjetController/./packages/controller/src/sw.ts"],"sourcesContent":["type Serverbound = {\n\tmethod1: [{ paramA: string; paramB: number }, boolean];\n\tmethod2: [string, number];\n};\n\ntype Clientbound = {\n\tmethod1: [number];\n\tmethod2: [boolean, string];\n};\n\nexport type RpcDescription = {\n\t[method: string]: [args: any, returnType: any] | [args: any] | [];\n};\n\nexport type MethodsDefinition<Description extends RpcDescription> = {\n\t[Method in keyof Description]: (\n\t\t...args: Description[Method] extends [infer A, ...any[]] ? [A] : []\n\t) => Description[Method] extends [any, infer R]\n\t\t? Promise<[R, Transferable[]]>\n\t\t: Promise<void>;\n};\n\nexport class RpcHelper<\n\tLocal extends RpcDescription,\n\tRemote extends RpcDescription,\n> {\n\tcounter: number = 0;\n\tpromiseCallbacks: Map<\n\t\tnumber,\n\t\t{ resolve: (value: any) => void; reject: (reason?: any) => void }\n\t> = new Map();\n\tconstructor(\n\t\tprivate methods: MethodsDefinition<Local>,\n\t\tprivate id: string,\n\t\tprivate sendRaw: (data: any, transfer: Transferable[]) => void\n\t) {}\n\n\trecieve(data: any) {\n\t\tif (data === undefined || data === null || typeof data !== \"object\") return;\n\t\tconst dt = data[this.id];\n\t\tif (dt === undefined || dt === null || typeof dt !== \"object\") return;\n\n\t\tconst type = dt.$type;\n\n\t\tif (type === \"response\") {\n\t\t\tconst token = dt.$token;\n\t\t\tconst data = dt.$data;\n\t\t\tconst error = dt.$error;\n\t\t\tconst cb = this.promiseCallbacks.get(token);\n\t\t\tif (!cb) return;\n\t\t\tthis.promiseCallbacks.delete(token);\n\t\t\tif (error !== undefined) {\n\t\t\t\tcb.reject(new Error(error));\n\t\t\t} else {\n\t\t\t\tcb.resolve(data);\n\t\t\t}\n\t\t} else if (type === \"request\") {\n\t\t\tconst method = dt.$method as keyof Local;\n\t\t\tconst args = dt.$args as Local[typeof method][0];\n\t\t\t(this.methods[method] as any)(args)\n\t\t\t\t.then((r: any) => {\n\t\t\t\t\tthis.sendRaw(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t\t\t$type: \"response\",\n\t\t\t\t\t\t\t\t$token: dt.$token,\n\t\t\t\t\t\t\t\t$data: r?.[0],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tr?.[1]\n\t\t\t\t\t);\n\t\t\t\t})\n\t\t\t\t.catch((err: any) => {\n\t\t\t\t\tconsole.error(err);\n\t\t\t\t\tthis.sendRaw(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t\t\t$type: \"response\",\n\t\t\t\t\t\t\t\t$token: dt.$token,\n\t\t\t\t\t\t\t\t$error: err?.toString() || \"Unknown error\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[]\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t}\n\t}\n\n\tcall<Method extends keyof Remote>(\n\t\tmethod: Method,\n\t\targs: Remote[Method][0],\n\t\ttransfer: Transferable[] = []\n\t): Promise<Remote[Method][1]> {\n\t\tconst token = this.counter++;\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tthis.promiseCallbacks.set(token, { resolve, reject });\n\t\t\tthis.sendRaw(\n\t\t\t\t{\n\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t$type: \"request\",\n\t\t\t\t\t\t$method: method,\n\t\t\t\t\t\t$args: args,\n\t\t\t\t\t\t$token: token,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\ttransfer\n\t\t\t);\n\t\t});\n\t}\n}\n","__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","/// <reference lib=\"WebWorker\" />\n/// <reference types=\"@types/serviceworker\" />\nimport { RpcHelper } from \"@mercuryworkshop/rpc\";\nimport type { Controllerbound, SWbound } from \"./types\";\nimport type { RawHeaders } from \"@mercuryworkshop/proxy-transports\";\n\nfunction makeId(): string {\n\treturn Math.random().toString(36).substring(2, 10);\n}\n\nconst cookieResolvers: Record<string, (value: void) => void> = {};\naddEventListener(\"message\", (e) => {\n\tif (!e.data) return;\n\tif (typeof e.data != \"object\") return;\n\tif (e.data.$sw$setCookieDone && typeof e.data.$sw$setCookieDone == \"object\") {\n\t\tconst done = e.data.$sw$setCookieDone;\n\n\t\tconst resolver = cookieResolvers[done.id];\n\t\tif (resolver) {\n\t\t\tresolver();\n\t\t\tdelete cookieResolvers[done.id];\n\t\t}\n\t}\n\n\tif (\n\t\te.data.$sw$initRemoteTransport &&\n\t\ttypeof e.data.$sw$initRemoteTransport == \"object\"\n\t) {\n\t\tconst { port, prefix } = e.data.$sw$initRemoteTransport;\n\n\t\tconst relevantcontroller = tabs.find((tab) =>\n\t\t\tnew URL(prefix).pathname.startsWith(tab.prefix)\n\t\t);\n\t\tif (!relevantcontroller) {\n\t\t\tconsole.error(\"No relevant controller found for transport init\");\n\t\t\treturn;\n\t\t}\n\t\trelevantcontroller.rpc.call(\"initRemoteTransport\", port, [port]);\n\t}\n});\n\nclass ControllerReference {\n\trpc: RpcHelper<SWbound, Controllerbound>;\n\n\tconstructor(\n\t\tpublic prefix: string,\n\t\tpublic id: string,\n\t\tport: MessagePort\n\t) {\n\t\tthis.rpc = new RpcHelper(\n\t\t\t{\n\t\t\t\tsendSetCookie: async ({ cookies, options }) => {\n\t\t\t\t\tconst clients = await self.clients.matchAll();\n\t\t\t\t\tconst ids: string[] = [];\n\t\t\t\t\tconst promises: Promise<string>[] = [];\n\n\t\t\t\t\t// Navigation fetches (document/iframe) deliver cookies via the inject\n\t\t\t\t\t// script's embedded cookieJar dump — the destination page doesn't have\n\t\t\t\t\t// inject.ts loaded yet to ack, so awaiting would deadlock. Broadcast\n\t\t\t\t\t// so any already-loaded clients can update their jars, but don't wait.\n\t\t\t\t\tconst isNavigation =\n\t\t\t\t\t\toptions?.destination === \"document\" ||\n\t\t\t\t\t\toptions?.destination === \"iframe\";\n\n\t\t\t\t\tfor (const client of clients) {\n\t\t\t\t\t\tconst id = makeId();\n\t\t\t\t\t\tids.push(id);\n\t\t\t\t\t\tclient.postMessage({\n\t\t\t\t\t\t\t$controller$setCookie: {\n\t\t\t\t\t\t\t\tcookies,\n\t\t\t\t\t\t\t\toptions,\n\t\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t\tif (!isNavigation) {\n\t\t\t\t\t\t\tpromises.push(\n\t\t\t\t\t\t\t\tnew Promise<string>((resolve) => {\n\t\t\t\t\t\t\t\t\t// Resolve with the id so we know which client replied.\n\t\t\t\t\t\t\t\t\tcookieResolvers[id] = () => resolve(id);\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t// Wait for the first client to acknowledge the cookie sync.\n\t\t\t\t\t// Using Promise.any (not Promise.all) so that extra SW clients created by\n\t\t\t\t\t// window.open (e.g. test popup windows) don't cause timeouts — only the\n\t\t\t\t\t// main controller client needs to respond.\n\t\t\t\t\tif (promises.length > 0) {\n\t\t\t\t\t\tlet timeoutId: ReturnType<typeof setTimeout> | undefined;\n\t\t\t\t\t\tlet responded = false;\n\t\t\t\t\t\tconst timeoutPromise = new Promise<void>((resolve) => {\n\t\t\t\t\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\t\t\t\t\tif (!responded) {\n\t\t\t\t\t\t\t\t\tconst pending = ids.filter(\n\t\t\t\t\t\t\t\t\t\t(id) => cookieResolvers[id] !== undefined\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t\t\t\"timed out waiting for set cookie response (deadlock?): \" +\n\t\t\t\t\t\t\t\t\t\t\t`cookies=${cookies.length} clients=${clients.length} ` +\n\t\t\t\t\t\t\t\t\t\t\t`pending=${pending.length}/${ids.length} ` +\n\t\t\t\t\t\t\t\t\t\t\t`clientUrls=${clients.map((c) => c.url).join(\",\")}`\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tresolve();\n\t\t\t\t\t\t\t}, 1000);\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tawait Promise.race([\n\t\t\t\t\t\t\t\ttimeoutPromise,\n\t\t\t\t\t\t\t\tPromise.any(promises)\n\t\t\t\t\t\t\t\t\t.then(() => {\n\t\t\t\t\t\t\t\t\t\tresponded = true;\n\t\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t\t.catch(() => {}),\n\t\t\t\t\t\t\t]);\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\t// Clear the timeout so it doesn't fire spuriously after the\n\t\t\t\t\t\t\t// race has already been won by Promise.any.\n\t\t\t\t\t\t\tif (timeoutId !== undefined) clearTimeout(timeoutId);\n\t\t\t\t\t\t\t// Clean up any pending resolvers so clients that never\n\t\t\t\t\t\t\t// responded don't leak entries in cookieResolvers.\n\t\t\t\t\t\t\tfor (const id of ids) {\n\t\t\t\t\t\t\t\tdelete cookieResolvers[id];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t},\n\t\t\t\"tabchannel-\" + id,\n\t\t\t(data, transfer) => {\n\t\t\t\tport.postMessage(data, transfer);\n\t\t\t}\n\t\t);\n\t\tport.onmessage = (e: MessageEvent) => {\n\t\t\tthis.rpc.recieve(e.data);\n\t\t};\n\t\tport.onmessageerror = console.error;\n\n\t\tthis.rpc.call(\"ready\", undefined);\n\t}\n}\n\nconst tabs: ControllerReference[] = [];\n\naddEventListener(\"message\", (e) => {\n\tif (!e.data) return;\n\tif (typeof e.data != \"object\") return;\n\tif (!e.data.$controller$init) return;\n\tif (typeof e.data.$controller$init != \"object\") return;\n\tconst init = e.data.$controller$init;\n\n\tconst existing = tabs.findIndex((t) => t.id === init.id);\n\tif (existing !== -1) {\n\t\ttabs.splice(existing, 1);\n\t}\n\ttabs.push(new ControllerReference(init.prefix, init.id, e.ports[0]));\n});\n\nexport function shouldRoute(event: FetchEvent): boolean {\n\tconst url = new URL(event.request.url);\n\tconst tab = tabs.find((tab) => url.pathname.startsWith(tab.prefix));\n\treturn tab !== undefined;\n}\n\nexport async function route(event: FetchEvent): Promise<Response> {\n\ttry {\n\t\tconst url = new URL(event.request.url);\n\t\tconst tab = tabs.find((tab) => url.pathname.startsWith(tab.prefix))!;\n\t\tconst client = await clients.get(event.clientId);\n\n\t\tconst rawheaders: RawHeaders = [...event.request.headers];\n\n\t\tconst response = await tab.rpc.call(\n\t\t\t\"request\",\n\t\t\t{\n\t\t\t\trawUrl: event.request.url,\n\t\t\t\trawReferrer: event.request.referrer,\n\t\t\t\tdestination: event.request.destination,\n\t\t\t\tmode: event.request.mode,\n\t\t\t\treferrer: event.request.referrer,\n\t\t\t\tmethod: event.request.method,\n\t\t\t\tbody: event.request.body,\n\t\t\t\tcache: event.request.cache,\n\t\t\t\tforceCrossOriginIsolated: false,\n\t\t\t\tinitialHeaders: rawheaders,\n\t\t\t\trawClientUrl: client ? client.url : undefined,\n\t\t\t\tclientId: event.clientId || event.resultingClientId,\n\t\t\t},\n\t\t\tevent.request.body instanceof ReadableStream ||\n\t\t\t\t// @ts-expect-error the types for fetchevent are messed up\n\t\t\t\tevent.request.body instanceof ArrayBuffer\n\t\t\t\t? [event.request.body]\n\t\t\t\t: undefined\n\t\t);\n\n\t\treturn new Response(response.body, {\n\t\t\tstatus: response.status,\n\t\t\tstatusText: response.statusText,\n\t\t\theaders: response.headers,\n\t\t});\n\t} catch (e) {\n\t\tconsole.error(\"Service Worker error:\", e);\n\t\treturn new Response(\n\t\t\t\"Internal Service Worker Error: \" + (e as Error).message,\n\t\t\t{\n\t\t\t\tstatus: 500,\n\t\t\t}\n\t\t);\n\t}\n}\n\naddEventListener(\"install\", () => {\n\tself.skipWaiting();\n});\n\naddEventListener(\"activate\", (event: ExtendableEvent) => {\n\tevent.waitUntil(clients.claim());\n});\n\n// the only way to know if a service worker has suddenly died is if this code runs again\n// notify all clients to send over their messageports again\nsetTimeout(async () => {\n\tconsole.log(\"service worker activated, notifying clients to revive\");\n\tfor (const client of await clients.matchAll()) {\n\t\tclient.postMessage({\n\t\t\t$controller$swrevive: {},\n\t\t});\n\t}\n\t// short delay is apparently needed\n}, 100);\n"],"names":["RpcHelper","Map","methods","id","sendRaw","data","dt","type","token","error","cb","undefined","Error","method","args","r","err","console","transfer","Promise","resolve","reject","e","Object","Symbol","cookieResolvers","addEventListener","done","resolver","port","prefix","relevantcontroller","tabs","tab","URL","ControllerReference","cookies","options","clients1","self","ids","promises","isNavigation","client","Math","timeoutId","responded","timeoutPromise","setTimeout","pending","c","clearTimeout","shouldRoute","event","url","route","clients","rawheaders","response","ReadableStream","ArrayBuffer","Response","init","existing","t"],"mappings":"gEAsBO,OAAMA,E,kBAIZ,SAAkB,CAAE,AACpB,kBAGI,IAAIC,GAAM,AACd,aACSC,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,AAACJ,IAC5B,GAAI,CAACA,EAAE,IAAI,EACP,AAAiB,UAAjB,OAAOA,EAAE,IAAI,EACb,CAACA,EAAE,IAAI,CAAC,gBAAgB,EACxB,AAAkC,UAAlC,OAAOA,EAAE,IAAI,CAAC,gBAAgB,CAHrB,OAIb,IAAMwC,EAAOxC,EAAE,IAAI,CAAC,gBAAgB,CAE9ByC,EAAW/B,EAAK,SAAS,CAAC,AAACgC,GAAMA,EAAE,EAAE,GAAKF,EAAK,EAAE,CACnDC,AAAa,MAAbA,GACH/B,EAAK,MAAM,CAAC+B,EAAU,GAEvB/B,EAAK,IAAI,CAAC,IAAIG,EAAoB2B,EAAK,MAAM,CAAEA,EAAK,EAAE,CAAExC,EAAE,KAAK,CAAC,EAAE,EACnE,GAuDAI,iBAAiB,UAAW,KAC3Ba,KAAK,WAAW,EACjB,GAEAb,iBAAiB,WAAY,AAAC2B,IAC7BA,EAAM,SAAS,CAACG,QAAQ,KAAK,GAC9B,GAIAR,WAAW,UAEV,IAAK,IAAML,KADX1B,QAAQ,GAAG,CAAC,yDACS,MAAMuC,QAAQ,QAAQ,IAC1Cb,EAAO,WAAW,CAAC,CAClB,qBAAsB,CAAC,CACxB,EAGF,EAAG,I"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type * as ScramjetGlobal from "@mercuryworkshop/scramjet";
|
|
2
|
+
declare const $scramjet: typeof ScramjetGlobal;
|
|
3
|
+
export declare const CACHE_NAME = "scramjet-http-cache-v2";
|
|
4
|
+
export interface HttpCachePluginOptions {
|
|
5
|
+
/** Name of the underlying Cache API entry. Defaults to CACHE_NAME. */
|
|
6
|
+
cacheName?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* RFC-9111-ish HTTP cache for ScramjetFetchHandler. Subclasses
|
|
10
|
+
* `$scramjet.Plugin` so it composes with the same hook plumbing every other
|
|
11
|
+
* scramjet plugin uses; `install(target)` wires it onto a Frame (or any
|
|
12
|
+
* object exposing a `fetchHandler`), and `bust()` drops the underlying
|
|
13
|
+
* `caches` entry.
|
|
14
|
+
*
|
|
15
|
+
* One instance can be installed onto multiple Frames -- the WeakMap of
|
|
16
|
+
* "did this request come from cache?" book-keeping is per-instance, not
|
|
17
|
+
* per-Frame, so nothing leaks across installs.
|
|
18
|
+
*/
|
|
19
|
+
export declare class HttpCachePlugin extends $scramjet.Plugin {
|
|
20
|
+
readonly cacheName: string;
|
|
21
|
+
private cachePromise;
|
|
22
|
+
private cameFromCache;
|
|
23
|
+
constructor(options?: HttpCachePluginOptions);
|
|
24
|
+
/** Lazy-open the underlying Cache. Memoized for the plugin's lifetime. */
|
|
25
|
+
private openCache;
|
|
26
|
+
/**
|
|
27
|
+
* Wire the cache up to a Frame (or anything exposing `fetchHandler`).
|
|
28
|
+
* Safe to call multiple times across different Frames.
|
|
29
|
+
*/
|
|
30
|
+
install(target: {
|
|
31
|
+
fetchHandler: ScramjetGlobal.ScramjetFetchHandler;
|
|
32
|
+
}): void;
|
|
33
|
+
/**
|
|
34
|
+
* Drop every entry in the HTTP cache. Returns whether the underlying
|
|
35
|
+
* Cache existed and was deleted.
|
|
36
|
+
*/
|
|
37
|
+
bust(): Promise<boolean>;
|
|
38
|
+
}
|
|
39
|
+
export {};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,43 +1,74 @@
|
|
|
1
1
|
import { RpcHelper } from "@mercuryworkshop/rpc";
|
|
2
|
-
import type * as ScramjetGlobal from "@mercuryworkshop/scramjet";
|
|
3
|
-
import { type Controllerbound, type SWbound } from "./types";
|
|
4
2
|
import { type ProxyTransport } from "@mercuryworkshop/proxy-transports";
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
import type * as ScramjetGlobal from "@mercuryworkshop/scramjet";
|
|
4
|
+
import type { FrameInitHooks, SerializedCookieSyncEntry, Controllerbound, SWbound, FrameErrorHooks } from "./types";
|
|
5
|
+
export { HttpCachePlugin, type HttpCachePluginOptions } from "./cache";
|
|
6
|
+
export type Config = {
|
|
7
|
+
prefix: string;
|
|
8
8
|
scramjetPath: string;
|
|
9
|
+
injectPath: string;
|
|
10
|
+
wasmPath: string;
|
|
9
11
|
virtualWasmPath: string;
|
|
10
|
-
|
|
12
|
+
codec: Record<"encode" | "decode", (input: string) => string>;
|
|
11
13
|
};
|
|
12
14
|
export declare const config: Config;
|
|
13
15
|
type ControllerInit = {
|
|
14
16
|
serviceworker: ServiceWorker;
|
|
15
17
|
transport: ProxyTransport;
|
|
18
|
+
config?: Partial<Config>;
|
|
19
|
+
scramjetConfig?: Partial<ScramjetGlobal.ScramjetConfig>;
|
|
16
20
|
};
|
|
17
21
|
export declare class Controller {
|
|
18
22
|
init: ControllerInit;
|
|
19
23
|
id: string;
|
|
24
|
+
config: Config;
|
|
25
|
+
scramjetConfig: ScramjetGlobal.ScramjetConfig;
|
|
20
26
|
prefix: string;
|
|
21
|
-
frames: Frame[];
|
|
22
27
|
cookieJar: ScramjetGlobal.CookieJar;
|
|
23
|
-
|
|
28
|
+
frames: Frame[];
|
|
29
|
+
serviceWorkerController: ServiceWorker;
|
|
30
|
+
guardServiceWorkerRevive: boolean;
|
|
24
31
|
private ready;
|
|
25
32
|
private readyResolve;
|
|
26
33
|
isReady: boolean;
|
|
34
|
+
rpc: RpcHelper<Controllerbound, SWbound>;
|
|
35
|
+
private port;
|
|
27
36
|
transport: ProxyTransport;
|
|
37
|
+
private cookieUpdatedAt;
|
|
38
|
+
private cookieSyncPromise;
|
|
39
|
+
private cookieSyncDirty;
|
|
40
|
+
private cookieSyncChannel;
|
|
41
|
+
private wasmAlreadyFetched;
|
|
42
|
+
private wasmPayload;
|
|
43
|
+
private onTabChannelMessage;
|
|
44
|
+
private onCookieSyncMessage;
|
|
45
|
+
private loadScramjetWasm;
|
|
28
46
|
private methods;
|
|
29
47
|
constructor(init: ControllerInit);
|
|
48
|
+
private setupMessagePort;
|
|
49
|
+
private applyCookieSyncEntries;
|
|
50
|
+
propagateCookieSync(cookies: SerializedCookieSyncEntry[], options?: ScramjetGlobal.CookieSyncOptions): Promise<void>;
|
|
51
|
+
private loadSavedCookies;
|
|
52
|
+
persistCookies(): Promise<void>;
|
|
53
|
+
setTransport(transport: ProxyTransport): void;
|
|
30
54
|
createFrame(element?: HTMLIFrameElement): Frame;
|
|
31
55
|
wait(): Promise<void>;
|
|
32
56
|
}
|
|
33
|
-
declare class Frame {
|
|
57
|
+
export declare class Frame {
|
|
34
58
|
controller: Controller;
|
|
35
59
|
element: HTMLIFrameElement;
|
|
36
|
-
fetchHandler: ScramjetGlobal.ScramjetFetchHandler;
|
|
37
60
|
id: string;
|
|
38
61
|
prefix: string;
|
|
62
|
+
fetchHandler: ScramjetGlobal.ScramjetFetchHandler;
|
|
63
|
+
hooks: {
|
|
64
|
+
fetch: ScramjetGlobal.FetchHooks;
|
|
65
|
+
init: FrameInitHooks;
|
|
66
|
+
error: FrameErrorHooks;
|
|
67
|
+
};
|
|
39
68
|
get context(): ScramjetGlobal.ScramjetContext;
|
|
40
69
|
constructor(controller: Controller, element: HTMLIFrameElement);
|
|
70
|
+
back(): void;
|
|
71
|
+
forward(): void;
|
|
72
|
+
reload(): void;
|
|
41
73
|
go(url: string): void;
|
|
42
74
|
}
|
|
43
|
-
export {};
|
package/dist/types/inject.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
type Config
|
|
1
|
+
import type * as ScramjetGlobal from "@mercuryworkshop/scramjet";
|
|
2
|
+
import type { RawHeaders } from "@mercuryworkshop/proxy-transports";
|
|
3
|
+
import type { Config } from ".";
|
|
4
4
|
type Init = {
|
|
5
5
|
config: Config;
|
|
6
|
-
sjconfig: ScramjetConfig;
|
|
7
|
-
cookies: string;
|
|
6
|
+
sjconfig: ScramjetGlobal.ScramjetConfig;
|
|
8
7
|
prefix: URL;
|
|
9
|
-
|
|
8
|
+
cookies: string;
|
|
9
|
+
yieldGetInjectScripts: (config: Config, sjconfig: ScramjetGlobal.ScramjetConfig, prefix: URL, cookieJar: ScramjetGlobal.CookieJar, codecEncode: (input: string) => string, codecDecode: (input: string) => string) => any;
|
|
10
10
|
codecEncode: (input: string) => string;
|
|
11
11
|
codecDecode: (input: string) => string;
|
|
12
|
+
initHeaders: RawHeaders;
|
|
13
|
+
history: ScramjetGlobal.TrackedHistoryState[];
|
|
12
14
|
};
|
|
13
|
-
export declare function load(
|
|
15
|
+
export declare function load(init: Init): void;
|
|
14
16
|
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const CONTROLLERFRAME: unique symbol;
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mercuryworkshop/scramjet-controller",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"packageManager": "pnpm@10.12.1",
|
|
6
6
|
"main": "index.ts",
|
|
7
|
-
"types": "./dist/types/
|
|
7
|
+
"types": "./dist/types/typesEntry.d.ts",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@
|
|
10
|
-
"@mercuryworkshop/proxy-transports": "catalog:"
|
|
9
|
+
"@fastify/deepmerge": "^3.2.1",
|
|
10
|
+
"@mercuryworkshop/proxy-transports": "catalog:",
|
|
11
|
+
"@mercuryworkshop/scramjet": "workspace:*"
|
|
11
12
|
},
|
|
12
13
|
"devDependencies": {
|
|
14
|
+
"@types/serviceworker": "^0.0.197",
|
|
13
15
|
"@mercuryworkshop/rpc": "workspace:*"
|
|
14
16
|
}
|
|
15
17
|
}
|