|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import { FakeServiceWorker } from "@/worker/fakesw"; |
6 | | -import { handleFetch } from "@/worker/fetch"; |
| 6 | +import { handleFetch, ScramjetFetchContext } from "@/worker/fetch"; |
7 | 7 | import { BareClient } from "../bare-mux-custom"; |
8 | 8 | import { ScramjetConfig, ScramjetDB } from "@/types"; |
9 | 9 | import { asyncSetWasm } from "@rewriters/wasm"; |
10 | 10 | import { CookieStore } from "@/shared/cookie"; |
11 | | -import { setConfig, unrewriteUrl } from "@/shared"; |
| 11 | +import { ScramjetHeaders, setConfig, unrewriteUrl } from "@/shared"; |
12 | 12 | import { openDB } from "idb"; |
13 | 13 | import { ScramjetDownload } from "@client/events"; |
14 | 14 | import { renderError } from "./error"; |
@@ -128,6 +128,7 @@ export class ScramjetServiceWorker extends EventTarget { |
128 | 128 |
|
129 | 129 | if (this.config) { |
130 | 130 | setConfig(this.config); |
| 131 | + this.client = new BareClient(); |
131 | 132 | await asyncSetWasm(); |
132 | 133 | } |
133 | 134 | } |
@@ -197,7 +198,35 @@ export class ScramjetServiceWorker extends EventTarget { |
197 | 198 | } |
198 | 199 |
|
199 | 200 | 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 | + }); |
201 | 230 | } catch (err) { |
202 | 231 | const errorDetails = { |
203 | 232 | message: err.message, |
|
0 commit comments