Skip to content

Commit fa3a199

Browse files
committed
[scramjet] make getInjectScripts be provided by consumer
1 parent cc400bf commit fa3a199

File tree

4 files changed

+39
-44
lines changed

4 files changed

+39
-44
lines changed

packages/scramjet/src/client/entry.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
// entrypoint for scramjet.client.js
22

3-
import { loadCodecs, setConfig } from "@/shared/index";
3+
import {
4+
loadCodecs,
5+
ScramjetInterface,
6+
setBareTransport,
7+
setConfig,
8+
setInterface,
9+
} from "@/shared/index";
410
import { SCRAMJETCLIENT } from "@/symbols";
511
import { ScramjetClient } from "@client/index";
612
import { ScramjetContextEvent, UrlChangeEvent } from "@client/events";
713
import { ScramjetConfig } from "@/types";
14+
import { BareTransport } from "@mercuryworkshop/bare-mux-custom";
815

916
export const iswindow = "window" in globalThis && window instanceof Window;
1017
export const isworker = "WorkerGlobalScope" in globalThis;
@@ -19,8 +26,16 @@ function createFrameId() {
1926
.join("")}`;
2027
}
2128

22-
export function loadAndHook(config: ScramjetConfig) {
23-
setConfig(config);
29+
export type ScramjetClientEntryInit = {
30+
config: ScramjetConfig;
31+
interface: ScramjetInterface;
32+
transport: BareTransport;
33+
};
34+
export function loadAndHook(init: ScramjetClientEntryInit) {
35+
setConfig(init.config);
36+
setInterface(init.interface);
37+
setBareTransport(init.transport);
38+
2439
dbg.log("initializing scramjet client");
2540
// if it already exists, that means the handlers have probably already been setup by the parent document
2641
if (!(SCRAMJETCLIENT in <Partial<typeof self>>globalThis)) {

packages/scramjet/src/shared/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { BareTransport } from "@mercuryworkshop/bare-mux-custom";
22
import { ScramjetConfig, ScramjetFlags } from "@/types";
3+
import DomHandler, { Element } from "domhandler";
4+
import { URLMeta } from "@rewriters/url";
5+
import { CookieJar } from "./cookie";
36

47
export * from "./cookie";
58
export * from "./headers";
@@ -64,22 +67,29 @@ export type Serverbound = {
6467
};
6568

6669
export type ScramjetInterface = {
67-
sendServerbound<K extends keyof Serverbound>(
70+
sendServerbound?<K extends keyof Serverbound>(
6871
type: K,
6972
msg: Serverbound[K][0]
7073
): Promise<Serverbound[K][1]>;
71-
onClientbound<K extends keyof Clientbound>(
74+
onClientbound?<K extends keyof Clientbound>(
7275
type: K,
7376
listener: (msg: Clientbound[K][0]) => Promise<Clientbound[K][1]>
7477
): () => void;
75-
sendClientbound<K extends keyof Clientbound>(
78+
sendClientbound?<K extends keyof Clientbound>(
7679
type: K,
7780
msg: Clientbound[K][0]
7881
): Promise<Clientbound[K][1]>;
79-
onServerbound<K extends keyof Serverbound>(
82+
onServerbound?<K extends keyof Serverbound>(
8083
type: K,
8184
listener: (msg: Serverbound[K][0]) => Promise<Serverbound[K][1]>
8285
): () => void;
86+
getInjectScripts(
87+
meta: URLMeta,
88+
handler: DomHandler,
89+
config: ScramjetConfig,
90+
cookieJar: CookieJar,
91+
script: (src: string) => Element
92+
): Element[];
8393
};
8494

8595
export let iface: ScramjetInterface = null!;

packages/scramjet/src/shared/rewriters/html.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,13 @@ import { URLMeta, rewriteUrl } from "@rewriters/url";
55
import { rewriteCss } from "@rewriters/css";
66
import { rewriteJs } from "@rewriters/js";
77
import { CookieJar } from "@/shared/cookie";
8-
import { config } from "@/shared";
8+
import { config, iface } from "@/shared";
99
import { htmlRules } from "@/shared/htmlRules";
1010

11-
export function getInjectScripts<T>(
12-
cookieStore: CookieJar,
13-
script: (src: string) => T
14-
): T[] {
15-
const dump = JSON.stringify(cookieStore.dump());
16-
const injected = `
17-
self.COOKIE = ${dump};
18-
$scramjetLoadClient().loadAndHook(${JSON.stringify(config)});
19-
if ("document" in self && document?.currentScript) {
20-
document.currentScript.remove();
21-
}
22-
`;
23-
24-
// for compatibility purpose
25-
const base64Injected = bytesToBase64(encoder.encode(injected));
26-
27-
return [
28-
script(config.files.wasm),
29-
script(config.files.all),
30-
script("data:application/javascript;base64," + base64Injected),
31-
];
32-
}
33-
3411
const encoder = new TextEncoder();
3512
function rewriteHtmlInner(
3613
html: string,
37-
cookieStore: CookieJar,
14+
cookieJar: CookieJar,
3815
meta: URLMeta,
3916
fromTop: boolean = false,
4017
preRewrite?: (handler: DomHandler) => void,
@@ -46,7 +23,7 @@ function rewriteHtmlInner(
4623
parser.write(html);
4724
parser.end();
4825
if (preRewrite) preRewrite(handler);
49-
traverseParsedHtml(handler.root, cookieStore, meta);
26+
traverseParsedHtml(handler.root, cookieJar, meta);
5027

5128
function findhead(node) {
5229
if (node.type === ElementType.Tag && node.name === "head") {
@@ -69,7 +46,9 @@ function rewriteHtmlInner(
6946
}
7047

7148
const script = (src: string) => new Element("script", { src });
72-
head.children.unshift(...getInjectScripts(cookieStore, script));
49+
head.children.unshift(
50+
...iface.getInjectScripts(meta, handler, config, cookieJar, script)
51+
);
7352
}
7453

7554
if (postRewrite) postRewrite(handler);

packages/scramjet/src/shared/rewriters/wasm.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// i am a cat. i like to be petted. i like to be fed. i like to be
22
import { initSync, Rewriter } from "../../../rewriter/wasm/out/wasm.js";
33
import type { JsRewriterOutput } from "../../../rewriter/wasm/out/wasm.js";
4-
import { codecDecode, codecEncode, config, flagEnabled } from "@/shared";
4+
import { codecDecode, codecEncode, config, flagEnabled, iface } from "@/shared";
55

66
export type { JsRewriterOutput, Rewriter };
77

88
import { rewriteUrl, URLMeta } from "@rewriters/url";
99
import { htmlRules } from "@/shared/htmlRules";
1010
import { rewriteCss } from "@rewriters/css";
1111
import { rewriteJs } from "@rewriters/js";
12-
import { getInjectScripts } from "@rewriters/html";
1312
import { CookieJar } from "@/shared/cookie";
1413

1514
let wasm_u8: Uint8Array<ArrayBuffer>;
@@ -64,14 +63,6 @@ export function getRewriter(meta: URLMeta): [Rewriter, () => void] {
6463
rewriteUrl,
6564
rewriteCss,
6665
rewriteJs,
67-
getHtmlInjectCode(cookieStore: CookieJar, foundHead: boolean) {
68-
let inject = getInjectScripts(
69-
cookieStore,
70-
(src) => `<script src="${src}"></script>`
71-
).join("");
72-
73-
return foundHead ? `<head>${inject}</head>` : inject;
74-
},
7566
},
7667
},
7768
flagEnabled,

0 commit comments

Comments
 (0)