Skip to content

Commit b617908

Browse files
committed
[scramjet/core] clean up worker inject scripts
1 parent 6c62837 commit b617908

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

packages/chrome/src/proxy/scramjet.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,10 @@ export function createFetchHandler(controller: Controller) {
244244

245245
const getWorkerInjectScripts: ScramjetInterface["getWorkerInjectScripts"] = (
246246
meta,
247-
js,
248-
type
247+
type,
248+
script
249249
) => {
250-
const module = type === "module";
251250
let str = "";
252-
const script = (script: string) => {
253-
if (module) {
254-
str += `import "${script}"\n`;
255-
} else {
256-
str += `importScripts("${script}");\n`;
257-
}
258-
};
259251

260252
const injectLoad = `
261253
$injectLoad({
@@ -267,9 +259,11 @@ export function createFetchHandler(controller: Controller) {
267259
prefix: "${controller.prefix.href}",
268260
});
269261
`;
270-
script(controller.prefix.href + virtualWasmPath);
271-
script(controller.prefix.href + virtualInjectPath);
272-
script(`data:application/javascript;base64,${base64Encode(injectLoad)}`);
262+
str += script(controller.prefix.href + virtualWasmPath);
263+
str += script(controller.prefix.href + virtualInjectPath);
264+
str += script(
265+
`data:application/javascript;base64,${base64Encode(injectLoad)}`
266+
);
273267

274268
return str;
275269
};

packages/scramjet/packages/controller/src/index.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,11 @@ class Frame {
305305
{ ...$scramjet.defaultConfig, ...cfg },
306306
new URL(this.prefix, location.href)
307307
),
308-
getWorkerInjectScripts: (meta, js, type, url) => {
309-
const module = type === "module";
308+
getWorkerInjectScripts: (meta, type, script) => {
310309
let str = "";
311-
const script = (script: string) => {
312-
if (module) {
313-
str += `import "${script}"\n`;
314-
} else {
315-
str += `importScripts("${script}");\n`;
316-
}
317-
};
318310

319-
script(config.scramjetPath);
320-
script(this.prefix + config.virtualWasmPath);
311+
str += script(config.scramjetPath);
312+
str += script(this.prefix + config.virtualWasmPath);
321313
str += `
322314
(()=>{
323315
const { ScramjetClient, CookieJar, setWasm } = $scramjet;

packages/scramjet/packages/core/src/shared/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ export type ScramjetInterface = {
3636
): Element[];
3737
getWorkerInjectScripts?(
3838
meta: URLMeta,
39-
js: string | Uint8Array,
40-
type: string,
41-
url: string
39+
type: "module" | undefined,
40+
script: (src: string) => string
4241
): string;
4342
};
4443

packages/scramjet/packages/core/src/shared/rewriters/worker.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ import { URLMeta } from "@rewriters/url";
55
export function rewriteWorkers(
66
context: ScramjetContext,
77
js: string | Uint8Array,
8-
type: string,
8+
type: "module" | undefined,
99
url: string,
1010
meta: URLMeta
1111
) {
1212
const module = type === "module";
13-
let str = context.interface.getWorkerInjectScripts(meta, js, type, url);
13+
const script = (script: string) => {
14+
if (module) {
15+
return `import "${script}"\n`;
16+
}
17+
return `importScripts("${script}");\n`;
18+
};
19+
let str = context.interface.getWorkerInjectScripts(meta, type, script);
1420
let rewritten = rewriteJs(js, url, context, meta, module);
1521
if (rewritten instanceof Uint8Array) {
1622
rewritten = new TextDecoder().decode(rewritten);

0 commit comments

Comments
 (0)