Skip to content

Commit 9579a99

Browse files
committed
[scramjet/core] fix charset encoding errors in worker rewriter
1 parent a288ac9 commit 9579a99

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

packages/chrome/src/proxy/scramjet.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ export function createFetchHandler(controller: Controller) {
260260
return [
261261
script(controller.prefix.href + virtualWasmPath),
262262
script(controller.prefix.href + virtualInjectPath),
263-
script("data:application/javascript;base64," + base64Encode(injected)),
263+
script(
264+
"data:text/javascript;charset=utf-8;base64," + base64Encode(injected)
265+
),
264266
];
265267
};
266268

@@ -284,7 +286,7 @@ export function createFetchHandler(controller: Controller) {
284286
str += script(controller.prefix.href + virtualWasmPath);
285287
str += script(controller.prefix.href + virtualInjectPath);
286288
str += script(
287-
`data:application/javascript;base64,${base64Encode(injectLoad)}`
289+
`data:text/javascript;charset=utf-8;base64,${base64Encode(injectLoad)}`
288290
);
289291

290292
return str;

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { flagEnabled, ScramjetContext } from "@/shared";
22
import { rewriteJs } from "@rewriters/js";
33
import { URLMeta } from "@rewriters/url";
44

5+
function base64Encode(text: string) {
6+
return btoa(
7+
new TextEncoder()
8+
.encode(text)
9+
.reduce(
10+
(data, byte) => (data.push(String.fromCharCode(byte)), data),
11+
[] as any
12+
)
13+
.join("")
14+
);
15+
}
16+
517
export function rewriteWorkers(
618
context: ScramjetContext,
719
js: string | Uint8Array,
@@ -16,7 +28,10 @@ export function rewriteWorkers(
1628
}
1729
return `importScripts("${script}");\n`;
1830
};
31+
const b64 = (script: string) =>
32+
`data:text/javascript;charset=utf-8;base64,${base64Encode(script)}`;
1933
let str = context.interface.getWorkerInjectScripts(meta, type, script);
34+
2035
let rewritten = rewriteJs(js, url, context, meta, module);
2136
if (rewritten instanceof Uint8Array) {
2237
rewritten = new TextDecoder().decode(rewritten);
@@ -25,10 +40,9 @@ export function rewriteWorkers(
2540
if (flagEnabled("encapsulateWorkers", context, meta.origin)) {
2641
// TODO: check if there's already a sourceURL/sourcemap before appending another?
2742
rewritten += `//# sourceURL=${url}`;
28-
str += script(`data:text/javascript;base64,${btoa(rewritten)}`);
43+
str += script(b64(rewritten));
2944
} else {
3045
str += rewritten;
3146
}
32-
3347
return str;
3448
}

0 commit comments

Comments
 (0)