Skip to content

Commit b2c24c2

Browse files
committed
[scramjet/controller] implement hookSubcontext
1 parent 11265a0 commit b2c24c2

File tree

1 file changed

+61
-45
lines changed
  • packages/scramjet/packages/controller/src

1 file changed

+61
-45
lines changed

packages/scramjet/packages/controller/src/inject.ts

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -166,69 +166,81 @@ type Init = {
166166
codecDecode: (input: string) => string;
167167
};
168168

169-
export function load({
170-
config,
171-
sjconfig,
172-
cookies,
173-
prefix,
174-
yieldGetInjectScripts,
175-
codecEncode,
176-
codecDecode,
177-
}: Init) {
178-
let client;
169+
export function load(init: Init) {
179170
if (SCRAMJETCLIENT in globalThis) {
180-
client = globalThis[SCRAMJETCLIENT];
181-
} else {
182-
setWasm(Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)));
183-
delete self.WASM;
171+
return;
172+
}
173+
if (!("WASM" in self)) {
174+
throw new Error("WASM not found in global scope!");
175+
}
176+
const wasm = Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0));
177+
delete (self as any).WASM;
178+
setWasm(wasm);
179+
180+
let context = new ExecutionContextWrapper(globalThis, init);
181+
}
182+
183+
function createFrameId() {
184+
return `${Array(8)
185+
.fill(0)
186+
.map(() => Math.floor(Math.random() * 36).toString(36))
187+
.join("")}`;
188+
}
189+
190+
class ExecutionContextWrapper {
191+
client!: ScramjetGlobal.ScramjetClient;
192+
cookieJar: CookieJar;
193+
transport: RemoteTransport;
184194

195+
constructor(
196+
public global: typeof globalThis,
197+
public init: Init
198+
) {
185199
const channel = new MessageChannel();
186-
const transport = new RemoteTransport(channel.port1);
200+
this.transport = new RemoteTransport(channel.port1);
187201
sw?.postMessage(
188202
{
189203
$sw$initRemoteTransport: {
190204
port: channel.port2,
191-
prefix: prefix.href,
205+
prefix: this.init.prefix.href,
192206
},
193207
},
194208
[channel.port2]
195209
);
196210

197-
const cookieJar = new CookieJar();
198-
cookieJar.load(cookies);
211+
this.cookieJar = new CookieJar();
212+
this.cookieJar.load(this.init.cookies);
199213

200-
const context = {
201-
interface: {
202-
getInjectScripts: yieldGetInjectScripts(
203-
cookieJar,
204-
config,
205-
sjconfig,
206-
prefix,
207-
codecEncode,
208-
codecDecode
209-
),
210-
codecEncode,
211-
codecDecode,
212-
},
213-
prefix,
214-
cookieJar,
215-
config: sjconfig,
216-
};
217-
function createFrameId() {
218-
return `${Array(8)
219-
.fill(0)
220-
.map(() => Math.floor(Math.random() * 36).toString(36))
221-
.join("")}`;
222-
}
214+
this.injectScramjet();
215+
}
223216

224-
const frame = globalThis.frameElement as HTMLIFrameElement | null;
217+
injectScramjet() {
218+
const frame = this.global.frameElement as HTMLIFrameElement | null;
225219
if (frame && !frame.name) {
226220
frame.name = createFrameId();
227221
}
228222

229-
client = new ScramjetClient(globalThis, {
223+
const context: ScramjetGlobal.ScramjetContext = {
224+
interface: {
225+
getInjectScripts: this.init.yieldGetInjectScripts(
226+
this.cookieJar,
227+
this.init.config,
228+
this.init.sjconfig,
229+
this.init.prefix,
230+
this.init.codecEncode,
231+
this.init.codecDecode
232+
),
233+
codecEncode: this.init.codecEncode,
234+
codecDecode: this.init.codecDecode,
235+
},
236+
config: this.init.sjconfig,
237+
prefix: this.init.prefix,
238+
cookieJar: this.cookieJar,
239+
};
240+
241+
this.client = new ScramjetClient(this.global, {
230242
context,
231-
transport,
243+
transport: this.transport,
232244
sendSetCookie: async (url, cookie) => {
233245
// sw.postMessage({
234246
// $controller$setCookie: {
@@ -243,8 +255,12 @@ export function load({
243255
shouldBlockMessageEvent(i) {
244256
return false;
245257
},
258+
hookSubcontext: (frameself, frame) => {
259+
const context = new ExecutionContextWrapper(frameself, this.init);
260+
return context.client;
261+
},
246262
});
247263

248-
client.hook();
264+
this.client.hook();
249265
}
250266
}

0 commit comments

Comments
 (0)