diff --git a/rspack.config.js b/rspack.config.js index 48da9ad..ad5639b 100644 --- a/rspack.config.js +++ b/rspack.config.js @@ -65,6 +65,7 @@ export default defineConfig({ new rspack.ProvidePlugin({ dbg: [join(__dirname, "src/log.ts"), "default"], Function: [join(__dirname, "src/snapshot.ts"), "Function"], + Request: [join(__dirname, "src/snapshot.ts"), "Request"], }), process.env.OBFUSCATE === "true" && { apply(compiler) { diff --git a/src/client/index.ts b/src/client/index.ts index 38bd4bb..f3f7620 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -8,6 +8,8 @@ export const isworker = "WorkerGlobalScope" in self; export const issw = "ServiceWorkerGlobalScope" in self; export const isdedicated = "DedicatedWorkerGlobalScope" in self; export const isshared = "SharedWorkerGlobalScope" in self; +export const isemulatedsw = + new URL(self.location.href).searchParams.get("dest") === "serviceworker"; dbg.log("scrammin"); // if it already exists, that means the handlers have probably already been setup by the parent document @@ -15,9 +17,7 @@ if (!(ScramjetClient.SCRAMJET in self)) { const client = new ScramjetClient(self); client.hook(); - if ( - new URL(self.location.href).searchParams.get("dest") === "serviceworker" - ) { + if (isemulatedsw) { const runtime = new ScramjetServiceWorkerRuntime(client); runtime.hook(); } diff --git a/src/client/shared/requests/fetch.ts b/src/client/shared/requests/fetch.ts index a8c5b90..3a0893e 100644 --- a/src/client/shared/requests/fetch.ts +++ b/src/client/shared/requests/fetch.ts @@ -1,5 +1,6 @@ // ts throws an error if you dont do window.fetch +import { isemulatedsw } from "../.."; import { ScramjetClient } from "../../client"; import { encodeUrl, rewriteHeaders } from "../../shared"; @@ -7,8 +8,9 @@ export default function (client: ScramjetClient, self: typeof globalThis) { client.Proxy("fetch", { apply(ctx) { if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) { - ctx.args[0].toString(); ctx.args[0] = encodeUrl(ctx.args[0].toString()); + + if (isemulatedsw) ctx.args[0] += "?from=swruntime"; } }, }); @@ -21,7 +23,11 @@ export default function (client: ScramjetClient, self: typeof globalThis) { client.Proxy("Request", { construct(ctx) { - if (typeof ctx.args[0] === "string") ctx.args[0] = encodeUrl(ctx.args[0]); + if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) { + ctx.args[0] = encodeUrl(ctx.args[0].toString()); + + if (isemulatedsw) ctx.args[0] += "?from=swruntime"; + } }, }); diff --git a/src/client/swruntime.ts b/src/client/swruntime.ts index b983ecb..06d5764 100644 --- a/src/client/swruntime.ts +++ b/src/client/swruntime.ts @@ -1,5 +1,6 @@ +import { Request } from "../snapshot"; import { ScramjetClient } from "./client"; -import { encodeUrl } from "./shared"; +import { decodeUrl, encodeUrl } from "./shared"; export class ScramjetServiceWorkerRuntime { recvport: MessagePort; @@ -40,6 +41,7 @@ export class ScramjetServiceWorkerRuntime { removeEventListener: () => {}, dispatchEvent: (_e: Event) => {}, }, + showNotification: async () => {}, unregister: async () => true, update: async () => {}, installing: null, @@ -63,8 +65,10 @@ function handleMessage( if (!fetchhandlers) return; for (const handler of fetchhandlers) { + if (handler.event !== "fetch") continue; + const request = data.scramjet$request; - const fakeRequest = new Request(request.url, { + const fakeRequest = new Request(decodeUrl(request.url), { body: request.body, headers: new Headers(request.headers), method: request.method, @@ -99,8 +103,10 @@ function handleMessage( })(); }; + dbg.log("to fn", fakeFetchEvent); handler.proxiedCallback(trustEvent(fakeFetchEvent)); if (!responded) { + console.log("sw", "no response"); port.postMessage({ scramjet$type: "fetch", scramjet$token: token, diff --git a/src/snapshot.ts b/src/snapshot.ts index a6500c4..36cb34e 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -3,4 +3,5 @@ export const Function = self.Function; export const URL = self.URL; export const fetch = self.fetch; +export const Request = self.Request; export const XMLHttpRequest = self.XMLHttpRequest; diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index 14d0e78..0af5510 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -51,7 +51,11 @@ export async function swfetch( (w) => w.origin === url.origin ); - if (activeWorker && activeWorker.connected) { + if ( + activeWorker && + activeWorker.connected && + urlParam.get("from") !== "swruntime" + ) { // TODO: check scope const r = await activeWorker.fetch(request); if (r) return r;