mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20:02 -04:00
misc fixes
This commit is contained in:
parent
7e8e9990f4
commit
a58f9bcb63
6 changed files with 26 additions and 8 deletions
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue