@mercuryworkshop/scramjet-controller 0.0.7 → 0.0.8

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.
@@ -1 +1 @@
1
- {"version":3,"file":"controller.sw.js","sources":["webpack://$scramjetControllerInit/./packages/scramjet/packages/rpc/index.ts","webpack://$scramjetControllerInit/webpack/runtime/define_property_getters","webpack://$scramjetControllerInit/webpack/runtime/has_own_property","webpack://$scramjetControllerInit/webpack/runtime/make_namespace_object","webpack://$scramjetControllerInit/./packages/scramjet/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\tconsole.error(prefix, tabs);\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","undefined","dt","type","token","error","cb","Error","method","args","r","err","console","transfer","Promise","resolve","reject","makeId","Math","cookieResolvers","addEventListener","e","done","resolver","port","prefix","tabs","relevantcontroller","tab","URL","ControllerReference","url","cookie","clients1","self","promises","client","setTimeout","init","shouldRoute","event","route","clients","rawheaders","response","ReadableStream","ArrayBuffer","Response"],"mappings":";;;;;;;;AAsBO,MAAMA;;;;IAIZ,UAAkB,EAAE;IACpB,mBAGI,IAAIC,MAAM;IACd,YACSC,OAAiC,EACjCC,EAAU,EACVC,OAAsD,CAC7D;aAHOF,UAAAA;aACAC,KAAAA;aACAC,UAAAA;IACN;IAEH,QAAQC,IAAS,EAAE;QAClB,IAAIA,SAASC,aAAaD,SAAS,QAAQ,OAAOA,SAAS,UAAU;QACrE,MAAME,KAAKF,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,IAAIE,OAAOD,aAAaC,OAAO,QAAQ,OAAOA,OAAO,UAAU;QAE/D,MAAMC,OAAOD,GAAG,KAAK;QAErB,IAAIC,SAAS,YAAY;YACxB,MAAMC,QAAQF,GAAG,MAAM;YACvB,MAAMF,OAAOE,GAAG,KAAK;YACrB,MAAMG,QAAQH,GAAG,MAAM;YACvB,MAAMI,KAAK,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAACF;YACrC,IAAI,CAACE,IAAI;YACT,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAACF;YAC7B,IAAIC,UAAUJ,WAAW;gBACxBK,GAAG,MAAM,CAAC,IAAIC,MAAMF;YACrB,OAAO;gBACNC,GAAG,OAAO,CAACN;YACZ;QACD,OAAO,IAAIG,SAAS,WAAW;YAC9B,MAAMK,SAASN,GAAG,OAAO;YACzB,MAAMO,OAAOP,GAAG,KAAK;YACpB,IAAI,CAAC,OAAO,CAACM,OAAO,CAASC,MAC5B,IAAI,CAAC,CAACC;gBACN,IAAI,CAAC,OAAO,CACX;oBACC,CAAC,IAAI,CAAC,EAAE,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AMG,aAAyB;eAAIH,MAAM,OAAO,CAAC,OAAO;SAAC;QAEzD,MAAMI,WAAW,MAAMhB,IAAI,GAAG,CAAC,IAAI,CAClC,WACA;YACC,QAAQY,MAAM,OAAO,CAAC,GAAG;YACzB,aAAaA,MAAM,OAAO,CAAC,WAAW;YACtC,MAAMA,MAAM,OAAO,CAAC,IAAI;YACxB,UAAUA,MAAM,OAAO,CAAC,QAAQ;YAChC,QAAQA,MAAM,OAAO,CAAC,MAAM;YAC5B,MAAMA,MAAM,OAAO,CAAC,IAAI;YACxB,OAAOA,MAAM,OAAO,CAAC,KAAK;YAC1B,0BAA0B;YAC1B,gBAAgBG;YAChB,cAAcP,SAASA,OAAO,GAAG,GAAGnC;QACrC,GACAuC,MAAM,OAAO,CAAC,IAAI,YAAYK,kBAC7B,0DAA0D;QAC1DL,MAAM,OAAO,CAAC,IAAI,YAAYM,cAC5B;YAACN,MAAM,OAAO,CAAC,IAAI;SAAC,GACpBvC;QAGJ,OAAO,IAAI8C,SAASH,SAAS,IAAI,EAAE;YAClC,QAAQA,SAAS,MAAM;YACvB,YAAYA,SAAS,UAAU;YAC/B,SAASA,SAAS,OAAO;QAC1B;IACD,EAAE,OAAOvB,GAAG;QACXT,QAAQ,KAAK,CAAC,yBAAyBS;QACvC,OAAO,IAAI0B,SACV,oCAAqC1B,EAAY,OAAO,EACxD;YACC,QAAQ;QACT;IAEF;AACD"}
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,C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OADAL,QAAQ,KAAK,CAAC,wBAAyBK,GAChC,IAAI8B,SACV,kCAAqC9B,EAAY,OAAO,CACxD,CACC,OAAQ,GACT,EAEF,CACD,CA3DAI,iBAAiB,UAAW,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,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"}
@@ -0,0 +1,43 @@
1
+ import { RpcHelper } from "@mercuryworkshop/rpc";
2
+ import type * as ScramjetGlobal from "@mercuryworkshop/scramjet";
3
+ import { type Controllerbound, type SWbound } from "./types";
4
+ import { type ProxyTransport } from "@mercuryworkshop/proxy-transports";
5
+ type Config = {
6
+ wasmPath: string;
7
+ injectPath: string;
8
+ scramjetPath: string;
9
+ virtualWasmPath: string;
10
+ prefix: string;
11
+ };
12
+ export declare const config: Config;
13
+ type ControllerInit = {
14
+ serviceworker: ServiceWorker;
15
+ transport: ProxyTransport;
16
+ };
17
+ export declare class Controller {
18
+ init: ControllerInit;
19
+ id: string;
20
+ prefix: string;
21
+ frames: Frame[];
22
+ cookieJar: ScramjetGlobal.CookieJar;
23
+ rpc: RpcHelper<Controllerbound, SWbound>;
24
+ private ready;
25
+ private readyResolve;
26
+ isReady: boolean;
27
+ transport: ProxyTransport;
28
+ private methods;
29
+ constructor(init: ControllerInit);
30
+ createFrame(element?: HTMLIFrameElement): Frame;
31
+ wait(): Promise<void>;
32
+ }
33
+ declare class Frame {
34
+ controller: Controller;
35
+ element: HTMLIFrameElement;
36
+ fetchHandler: ScramjetGlobal.ScramjetFetchHandler;
37
+ id: string;
38
+ prefix: string;
39
+ get context(): ScramjetGlobal.ScramjetContext;
40
+ constructor(controller: Controller, element: HTMLIFrameElement);
41
+ go(url: string): void;
42
+ }
43
+ export {};
@@ -0,0 +1,14 @@
1
+ import type { CookieJar, ScramjetConfig } from "@mercuryworkshop/scramjet";
2
+ declare const CookieJar: typeof CookieJar;
3
+ type Config = any;
4
+ type Init = {
5
+ config: Config;
6
+ sjconfig: ScramjetConfig;
7
+ cookies: string;
8
+ prefix: URL;
9
+ yieldGetInjectScripts: (cookieJar: CookieJar, config: Config, sjconfig: ScramjetConfig, prefix: URL) => any;
10
+ codecEncode: (input: string) => string;
11
+ codecDecode: (input: string) => string;
12
+ };
13
+ export declare function load({ config, sjconfig, cookies, prefix, yieldGetInjectScripts, codecEncode, codecDecode, }: Init): void;
14
+ export {};
@@ -0,0 +1,2 @@
1
+ export declare function shouldRoute(event: FetchEvent): boolean;
2
+ export declare function route(event: FetchEvent): Promise<Response>;
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
- "name": "@mercuryworkshop/scramjet-controller",
3
- "version": "0.0.7",
4
- "type": "module",
5
- "main": "index.ts",
6
- "dependencies": {
7
- "@mercuryworkshop/proxy-transports": "1.0.2",
8
- "@mercuryworkshop/scramjet": "2.0.1-alpha",
9
- "@mercuryworkshop/rpc": "0.0.1"
10
- }
11
- }
2
+ "name": "@mercuryworkshop/scramjet-controller",
3
+ "version": "0.0.8",
4
+ "type": "module",
5
+ "packageManager": "pnpm@10.12.1",
6
+ "main": "index.ts",
7
+ "types": "./dist/types/index.d.ts",
8
+ "dependencies": {
9
+ "@mercuryworkshop/scramjet": "workspace:*",
10
+ "@mercuryworkshop/proxy-transports": "catalog:"
11
+ },
12
+ "devDependencies": {
13
+ "@mercuryworkshop/rpc": "workspace:*"
14
+ }
15
+ }
package/src/index.ts CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  } from "./types";
13
13
  import {
14
14
  BareCompatibleClient,
15
- type BareResponse,
15
+ BareResponse,
16
16
  type ProxyTransport,
17
17
  } from "@mercuryworkshop/proxy-transports";
18
18
 
@@ -26,23 +26,13 @@ type Config = {
26
26
  prefix: string;
27
27
  };
28
28
 
29
- const defaultConfig: Config = {
29
+ export const config: Config = {
30
30
  prefix: "/~/sj/",
31
31
  virtualWasmPath: "scramjet.wasm.js",
32
32
  injectPath: "/controller/controller.inject.js",
33
33
  scramjetPath: "/scramjet/scramjet.js",
34
- wasmPath: "/scramjet/scramjet.wasm.wasm",
34
+ wasmPath: "/scramjet/scramjet.wasm",
35
35
  };
36
- let config: Config;
37
-
38
- export default async function init(cfg: Partial<Config> = {}): Promise<{ Controller: typeof Controller }> {
39
- config = { ...defaultConfig, ...cfg };
40
- let resp = await fetch(config.wasmPath)
41
-
42
- $scramjet.setWasm(await resp.arrayBuffer());
43
-
44
- return { Controller };
45
- }
46
36
 
47
37
  const cfg = {
48
38
  flags: {
@@ -77,15 +67,28 @@ type ControllerInit = {
77
67
  transport: ProxyTransport;
78
68
  };
79
69
 
80
- class Controller {
70
+ let wasmAlreadyFetched = false;
71
+
72
+ async function loadScramjetWasm() {
73
+ if (wasmAlreadyFetched) {
74
+ return;
75
+ }
76
+
77
+ let resp = await fetch(config.wasmPath);
78
+ $scramjet.setWasm(await resp.arrayBuffer());
79
+ wasmAlreadyFetched = true;
80
+ }
81
+
82
+ export class Controller {
81
83
  id: string;
82
84
  prefix: string;
83
85
  frames: Frame[] = [];
84
86
  cookieJar = new $scramjet.CookieJar();
85
87
 
86
88
  rpc: RpcHelper<Controllerbound, SWbound>;
87
- private ready: Promise<void>;
89
+ private ready: Promise<[void, void]>;
88
90
  private readyResolve!: () => void;
91
+ public isReady: boolean = false;
89
92
 
90
93
  transport: ProxyTransport;
91
94
 
@@ -124,9 +127,7 @@ class Controller {
124
127
  body: wasmPayload,
125
128
  status: 200,
126
129
  statusText: "OK",
127
- headers: {
128
- "Content-Type": ["application/javascript"],
129
- },
130
+ headers: [["Content-Type", "application/javascript"]],
130
131
  },
131
132
  [],
132
133
  ];
@@ -189,10 +190,11 @@ class Controller {
189
190
  new URL(url),
190
191
  protocols,
191
192
  requestHeaders,
192
- (protocol) => {
193
+ (protocol, extensions) => {
193
194
  resolve({
194
195
  result: "success",
195
196
  protocol: protocol,
197
+ extensions: extensions,
196
198
  });
197
199
  },
198
200
  (data) => {
@@ -257,9 +259,12 @@ class Controller {
257
259
  this.id = makeId();
258
260
  this.prefix = config.prefix + this.id + "/";
259
261
 
260
- this.ready = new Promise<void>((resolve) => {
261
- this.readyResolve = resolve;
262
- });
262
+ this.ready = Promise.all([
263
+ new Promise<void>((resolve) => {
264
+ this.readyResolve = resolve;
265
+ }),
266
+ loadScramjetWasm(),
267
+ ]);
263
268
 
264
269
  let channel = new MessageChannel();
265
270
  this.rpc = new RpcHelper<Controllerbound, SWbound>(
@@ -286,6 +291,11 @@ class Controller {
286
291
  }
287
292
 
288
293
  createFrame(element?: HTMLIFrameElement): Frame {
294
+ if (!this.ready) {
295
+ throw new Error(
296
+ "Controller is not ready! Try awaiting controller.wait()"
297
+ );
298
+ }
289
299
  element ??= document.createElement("iframe");
290
300
  const frame = new Frame(this, element);
291
301
  this.frames.push(frame);
@@ -303,7 +313,11 @@ function yieldGetInjectScripts(
303
313
  sjconfig: ScramjetGlobal.ScramjetConfig,
304
314
  prefix: URL
305
315
  ) {
306
- return function getInjectScripts(meta, handler, script) {
316
+ let getInjectScripts: ScramjetGlobal.ScramjetInterface["getInjectScripts"] = (
317
+ meta,
318
+ handler,
319
+ script
320
+ ) => {
307
321
  return [
308
322
  script(config.scramjetPath),
309
323
  script(config.injectPath),
@@ -325,6 +339,7 @@ function yieldGetInjectScripts(
325
339
  ),
326
340
  ];
327
341
  };
342
+ return getInjectScripts;
328
343
  }
329
344
 
330
345
  class Frame {
@@ -332,7 +347,7 @@ class Frame {
332
347
  id: string;
333
348
  prefix: string;
334
349
 
335
- get context() {
350
+ get context(): ScramjetGlobal.ScramjetContext {
336
351
  let sjcfg = {
337
352
  ...$scramjet.defaultConfig,
338
353
  ...cfg,
@@ -405,10 +420,10 @@ class Frame {
405
420
  transport: controller.transport,
406
421
  async sendSetCookie(url, cookie) {},
407
422
  async fetchBlobUrl(url) {
408
- return (await fetch(url)) as BareResponseFetch;
423
+ return BareResponse.fromNativeResponse(await fetch(url));
409
424
  },
410
425
  async fetchDataUrl(url) {
411
- return (await fetch(url)) as BareResponseFetch;
426
+ return BareResponse.fromNativeResponse(await fetch(url));
412
427
  },
413
428
  });
414
429
  }
package/src/sw.ts CHANGED
@@ -28,7 +28,6 @@ addEventListener("message", (e) => {
28
28
  ) {
29
29
  const { port, prefix } = e.data.$sw$initRemoteTransport;
30
30
 
31
- console.error(prefix, tabs);
32
31
  const relevantcontroller = tabs.find((tab) =>
33
32
  new URL(prefix).pathname.startsWith(tab.prefix)
34
33
  );
package/tsconfig.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "types": [],
8
8
  "skipLibCheck": true,
9
9
  /* Bundler mode */
10
- "moduleResolution": "node",
10
+ "moduleResolution": "bundler",
11
11
  "allowImportingTsExtensions": true,
12
12
  "verbatimModuleSyntax": true,
13
13
  "moduleDetection": "force",
@@ -18,7 +18,7 @@
18
18
  "noUnusedParameters": false,
19
19
  "erasableSyntaxOnly": false,
20
20
  "noFallthroughCasesInSwitch": true,
21
- "noUncheckedSideEffectImports": true,
21
+ "noUncheckedSideEffectImports": true
22
22
  },
23
23
  "include": ["src"]
24
24
  }
@@ -0,0 +1,16 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "emitDeclarationOnly": true,
6
+ "declarationDir": "./dist/types",
7
+ "removeComments": false,
8
+ "skipLibCheck": true,
9
+ "noEmitOnError": false,
10
+ "noEmit": false,
11
+ "strict": false,
12
+ "noUnusedLocals": false,
13
+ "noUnusedParameters": false
14
+ },
15
+ "include": ["src"]
16
+ }