Skip to content

Commit d8fb090

Browse files
committed
[scramjet/controller] add worker support
1 parent db2ed11 commit d8fb090

File tree

1 file changed

+51
-5
lines changed
  • packages/scramjet/packages/controller/src

1 file changed

+51
-5
lines changed

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

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,21 +296,67 @@ class Frame {
296296
getInjectScripts(meta, handler, script) {}
297297

298298
get context() {
299+
let sjcfg = {
300+
...$scramjet.defaultConfig,
301+
...cfg,
302+
};
299303
return {
300304
cookieJar,
301305
prefix: new URL(this.prefix, location.href),
302-
config: {
303-
...$scramjet.defaultConfig,
304-
...cfg,
305-
},
306+
config: sjcfg,
306307
interface: {
307308
getInjectScripts: yieldGetInjectScripts(
308309
this.controller.cookieJar,
309310
config,
310311
{ ...$scramjet.defaultConfig, ...cfg },
311312
new URL(this.prefix, location.href)
312313
),
313-
getWorkerInjectScripts: () => "",
314+
getWorkerInjectScripts: (type: string) => {
315+
const module = type === "module";
316+
let str = "";
317+
const script = (script: string) => {
318+
if (module) {
319+
str += `import "${script}"\n`;
320+
} else {
321+
str += `importScripts("${script}");\n`;
322+
}
323+
};
324+
325+
script(config.scramjetPath);
326+
script(this.prefix + config.virtualWasmPath);
327+
str += `
328+
(()=>{
329+
const { ScramjetClient, CookieJar, setWasm } = $scramjet;
330+
331+
setWasm(Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)));
332+
delete self.WASM;
333+
334+
const sjconfig = ${JSON.stringify(sjcfg)};
335+
const prefix = new URL("${this.prefix}", location.href);
336+
337+
const context = {
338+
interface: {
339+
codecEncode: ${codecEncode.toString()},
340+
codecDecode: ${codecDecode.toString()},
341+
},
342+
prefix,
343+
config: sjconfig
344+
};
345+
346+
const client = new ScramjetClient(globalThis, {
347+
context,
348+
transport: null,
349+
shouldPassthroughWebsocket: (url) => {
350+
return url === "wss://anura.pro/";
351+
}
352+
});
353+
354+
client.hook();
355+
})();
356+
`;
357+
358+
return str;
359+
},
314360
codecEncode,
315361
codecDecode,
316362
},

0 commit comments

Comments
 (0)