misc fixes

This commit is contained in:
velzie 2024-08-03 11:20:15 -04:00
parent 7e8e9990f4
commit a58f9bcb63
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
6 changed files with 26 additions and 8 deletions

View file

@ -65,6 +65,7 @@ export default defineConfig({
new rspack.ProvidePlugin({ new rspack.ProvidePlugin({
dbg: [join(__dirname, "src/log.ts"), "default"], dbg: [join(__dirname, "src/log.ts"), "default"],
Function: [join(__dirname, "src/snapshot.ts"), "Function"], Function: [join(__dirname, "src/snapshot.ts"), "Function"],
Request: [join(__dirname, "src/snapshot.ts"), "Request"],
}), }),
process.env.OBFUSCATE === "true" && { process.env.OBFUSCATE === "true" && {
apply(compiler) { apply(compiler) {

View file

@ -8,6 +8,8 @@ export const isworker = "WorkerGlobalScope" in self;
export const issw = "ServiceWorkerGlobalScope" in self; export const issw = "ServiceWorkerGlobalScope" in self;
export const isdedicated = "DedicatedWorkerGlobalScope" in self; export const isdedicated = "DedicatedWorkerGlobalScope" in self;
export const isshared = "SharedWorkerGlobalScope" in self; export const isshared = "SharedWorkerGlobalScope" in self;
export const isemulatedsw =
new URL(self.location.href).searchParams.get("dest") === "serviceworker";
dbg.log("scrammin"); dbg.log("scrammin");
// if it already exists, that means the handlers have probably already been setup by the parent document // 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); const client = new ScramjetClient(self);
client.hook(); client.hook();
if ( if (isemulatedsw) {
new URL(self.location.href).searchParams.get("dest") === "serviceworker"
) {
const runtime = new ScramjetServiceWorkerRuntime(client); const runtime = new ScramjetServiceWorkerRuntime(client);
runtime.hook(); runtime.hook();
} }

View file

@ -1,5 +1,6 @@
// ts throws an error if you dont do window.fetch // ts throws an error if you dont do window.fetch
import { isemulatedsw } from "../..";
import { ScramjetClient } from "../../client"; import { ScramjetClient } from "../../client";
import { encodeUrl, rewriteHeaders } from "../../shared"; import { encodeUrl, rewriteHeaders } from "../../shared";
@ -7,8 +8,9 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("fetch", { client.Proxy("fetch", {
apply(ctx) { apply(ctx) {
if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) { if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) {
ctx.args[0].toString();
ctx.args[0] = encodeUrl(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", { client.Proxy("Request", {
construct(ctx) { 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";
}
}, },
}); });

View file

@ -1,5 +1,6 @@
import { Request } from "../snapshot";
import { ScramjetClient } from "./client"; import { ScramjetClient } from "./client";
import { encodeUrl } from "./shared"; import { decodeUrl, encodeUrl } from "./shared";
export class ScramjetServiceWorkerRuntime { export class ScramjetServiceWorkerRuntime {
recvport: MessagePort; recvport: MessagePort;
@ -40,6 +41,7 @@ export class ScramjetServiceWorkerRuntime {
removeEventListener: () => {}, removeEventListener: () => {},
dispatchEvent: (_e: Event) => {}, dispatchEvent: (_e: Event) => {},
}, },
showNotification: async () => {},
unregister: async () => true, unregister: async () => true,
update: async () => {}, update: async () => {},
installing: null, installing: null,
@ -63,8 +65,10 @@ function handleMessage(
if (!fetchhandlers) return; if (!fetchhandlers) return;
for (const handler of fetchhandlers) { for (const handler of fetchhandlers) {
if (handler.event !== "fetch") continue;
const request = data.scramjet$request; const request = data.scramjet$request;
const fakeRequest = new Request(request.url, { const fakeRequest = new Request(decodeUrl(request.url), {
body: request.body, body: request.body,
headers: new Headers(request.headers), headers: new Headers(request.headers),
method: request.method, method: request.method,
@ -99,8 +103,10 @@ function handleMessage(
})(); })();
}; };
dbg.log("to fn", fakeFetchEvent);
handler.proxiedCallback(trustEvent(fakeFetchEvent)); handler.proxiedCallback(trustEvent(fakeFetchEvent));
if (!responded) { if (!responded) {
console.log("sw", "no response");
port.postMessage({ port.postMessage({
scramjet$type: "fetch", scramjet$type: "fetch",
scramjet$token: token, scramjet$token: token,

View file

@ -3,4 +3,5 @@
export const Function = self.Function; export const Function = self.Function;
export const URL = self.URL; export const URL = self.URL;
export const fetch = self.fetch; export const fetch = self.fetch;
export const Request = self.Request;
export const XMLHttpRequest = self.XMLHttpRequest; export const XMLHttpRequest = self.XMLHttpRequest;

View file

@ -51,7 +51,11 @@ export async function swfetch(
(w) => w.origin === url.origin (w) => w.origin === url.origin
); );
if (activeWorker && activeWorker.connected) { if (
activeWorker &&
activeWorker.connected &&
urlParam.get("from") !== "swruntime"
) {
// TODO: check scope // TODO: check scope
const r = await activeWorker.fetch(request); const r = await activeWorker.fetch(request);
if (r) return r; if (r) return r;