Skip to content

Commit 1c6760c

Browse files
committed
[core] fix ScramjetServiceWorker for non isolation use
1 parent ba7f9a0 commit 1c6760c

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed
File renamed without changes.

packages/scramjet/src/worker/fetch.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ export async function handleFetch(
9292
duplex: "half",
9393
} as RequestInit;
9494

95-
const req = new ScramjetRequestEvent(context, parsed.url, parsed, init);
95+
const req = new ScramjetRequestEvent(
96+
context,
97+
parsed.url,
98+
parsed,
99+
init,
100+
client
101+
);
96102
this.dispatchEvent(req);
97103

98104
// if the event listener overwrote response with a promise, use that. otherwise fetch normally
@@ -819,7 +825,8 @@ export class ScramjetRequestEvent extends Event {
819825
public context: ScramjetFetchContext,
820826
public url: URL,
821827
public parsed: ScramjetFetchParsed,
822-
public init: RequestInit
828+
public init: RequestInit,
829+
public client: BareClient
823830
) {
824831
super("request");
825832
}

packages/scramjet/src/worker/index.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
*/
44

55
import { FakeServiceWorker } from "@/worker/fakesw";
6-
import { handleFetch } from "@/worker/fetch";
6+
import { handleFetch, ScramjetFetchContext } from "@/worker/fetch";
77
import { BareClient } from "../bare-mux-custom";
88
import { ScramjetConfig, ScramjetDB } from "@/types";
99
import { asyncSetWasm } from "@rewriters/wasm";
1010
import { CookieStore } from "@/shared/cookie";
11-
import { setConfig, unrewriteUrl } from "@/shared";
11+
import { ScramjetHeaders, setConfig, unrewriteUrl } from "@/shared";
1212
import { openDB } from "idb";
1313
import { ScramjetDownload } from "@client/events";
1414
import { renderError } from "./error";
@@ -128,6 +128,7 @@ export class ScramjetServiceWorker extends EventTarget {
128128

129129
if (this.config) {
130130
setConfig(this.config);
131+
this.client = new BareClient();
131132
await asyncSetWasm();
132133
}
133134
}
@@ -197,7 +198,35 @@ export class ScramjetServiceWorker extends EventTarget {
197198
}
198199

199200
try {
200-
return handleFetch.call(this, request, this.client);
201+
const headers = new ScramjetHeaders();
202+
for (const [key, value] of request.headers.entries()) {
203+
headers.set(key, value);
204+
}
205+
const context: ScramjetFetchContext = {
206+
rawUrl: new URL(request.url),
207+
destination: request.destination,
208+
mode: request.mode,
209+
referrer: request.referrer,
210+
method: request.method,
211+
body: request.body,
212+
cache: request.cache,
213+
forceCrossOriginIsolated: crossOriginIsolated,
214+
initialHeaders: headers,
215+
cookieStore: this.cookieStore,
216+
};
217+
const resp = await handleFetch.call(
218+
this,
219+
context,
220+
this.config,
221+
this.client,
222+
new URL(location.protocol + location.host + this.config.prefix)
223+
);
224+
225+
return new Response(resp.body, {
226+
status: resp.status,
227+
statusText: resp.statusText,
228+
headers: resp.headers,
229+
});
201230
} catch (err) {
202231
const errorDetails = {
203232
message: err.message,

0 commit comments

Comments
 (0)