Skip to content

Commit 833189d

Browse files
committed
[scramjet/controller] fix controller
1 parent b416be9 commit 833189d

File tree

6 files changed

+118
-21
lines changed

6 files changed

+118
-21
lines changed

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

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class Controller {
7373
frames: Frame[] = [];
7474
cookieJar = new $scramjet.CookieJar();
7575

76-
private rpc: RpcHelper<Controllerbound, SWbound>;
76+
rpc: RpcHelper<Controllerbound, SWbound>;
7777
private ready: Promise<void>;
7878
private readyResolve: () => void;
7979

@@ -228,29 +228,58 @@ function yieldGetInjectScripts(
228228
"data:text/javascript;base64," +
229229
btoa(`
230230
(()=>{
231-
$scramjet.setWasm(Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)));
231+
const { ScramjetClient, CookieJar, setWasm } = $scramjet;
232+
233+
setWasm(Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)));
232234
delete self.WASM;
233-
const cookieJar = new $scramjet.CookieJar();
235+
236+
const cookieJar = new CookieJar();
234237
const config = ${JSON.stringify(config)};
235238
const sjconfig = ${JSON.stringify(sjconfig)};
236239
cookieJar.load(${cookieJar.dump()});
237240
238241
const prefix = new URL("${prefix.href}");
242+
const sw = navigator.serviceWorker.controller;
239243
240-
$scramjet.loadAndHook({
241-
context: {
242-
interface: {
243-
getInjectScripts: (${yieldGetInjectScripts.toString()})(cookieJar, config, sjconfig, prefix),
244-
codecEncode: ${codecEncode.toString()},
245-
codecDecode: ${codecDecode.toString()},
246-
},
247-
prefix,
248-
cookieJar,
249-
config: sjconfig
244+
const context = {
245+
interface: {
246+
getInjectScripts: (${yieldGetInjectScripts.toString()})(cookieJar, config, sjconfig, prefix),
247+
codecEncode: ${codecEncode.toString()},
248+
codecDecode: ${codecDecode.toString()},
250249
},
250+
prefix,
251+
cookieJar,
252+
config: sjconfig
253+
};
254+
function createFrameId() {
255+
return \`\${Array(8)
256+
.fill(0)
257+
.map(() => Math.floor(Math.random() * 36).toString(36))
258+
.join("")}\`;
259+
}
260+
261+
const frame = globalThis.frameElement;
262+
if (frame && !frame.name) {
263+
frame.name = createFrameId();
264+
}
265+
266+
const client = new ScramjetClient(globalThis, {
267+
context,
251268
transport: null,
252-
})
269+
sendSetCookie: async (url, cookie) => {
270+
// sw.postMessage({
271+
// $controller$setCookie: {
272+
// url,
273+
// cookie
274+
// }
275+
// });
276+
},
277+
shouldPassthroughWebsocket: (url) => {
278+
return url === "wss://anura.pro/";
279+
}
280+
});
253281
282+
client.hook();
254283
document.currentScript.remove();
255284
})();
256285
`)

packages/scramjet/packages/controller/src/sw.ts

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@ import { RpcHelper } from "@mercuryworkshop/rpc";
33
import { Controllerbound, SWbound } from "./types";
44
import type { BareHeaders } from "@mercuryworkshop/bare-mux-custom";
55

6+
function makeId(): string {
7+
return Math.random().toString(36).substring(2, 10);
8+
}
9+
10+
let cookieResolvers: Record<string, (value: void) => void> = {};
11+
addEventListener("message", (e) => {
12+
if (!e.data) return;
13+
if (typeof e.data != "object") return;
14+
if (!e.data.$sw$setCookieDone) return;
15+
if (typeof e.data.$sw$setCookieDone != "object") return;
16+
const done = e.data.$sw$setCookieDone;
17+
18+
const resolver = cookieResolvers[done.id];
19+
if (resolver) {
20+
resolver();
21+
delete cookieResolvers[done.id];
22+
}
23+
});
24+
625
class Tab {
726
rpc: RpcHelper<SWbound, Controllerbound>;
827

@@ -11,9 +30,45 @@ class Tab {
1130
public id: string,
1231
port: MessagePort
1332
) {
14-
this.rpc = new RpcHelper({}, "tabchannel-" + id, (data, transfer) => {
15-
port.postMessage(data, transfer);
16-
});
33+
this.rpc = new RpcHelper(
34+
{
35+
sendSetCookie: async ({ url, cookie }) => {
36+
let clients = await self.clients.matchAll();
37+
let promises = [];
38+
39+
for (const client of clients) {
40+
let id = makeId();
41+
client.postMessage({
42+
$controller$setCookie: {
43+
url,
44+
cookie,
45+
id,
46+
},
47+
});
48+
promises.push(
49+
new Promise<void>((resolve) => {
50+
cookieResolvers[id] = resolve;
51+
})
52+
);
53+
}
54+
await Promise.race([
55+
new Promise<void>((resolve) =>
56+
setTimeout(() => {
57+
console.error(
58+
"timed out waiting for set cookie response (deadlock?)"
59+
);
60+
resolve();
61+
}, 1000)
62+
),
63+
promises,
64+
]);
65+
},
66+
},
67+
"tabchannel-" + id,
68+
(data, transfer) => {
69+
port.postMessage(data, transfer);
70+
}
71+
);
1772
port.addEventListener("message", (e) => {
1873
this.rpc.recieve(e.data);
1974
});

packages/scramjet/packages/controller/src/types.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ export type TransferResponse = {
2929
export type Controllerbound = {
3030
ready: [];
3131
request: [TransferRequest, TransferResponse];
32+
sendSetCookie: [
33+
{
34+
url: string;
35+
cookie: string;
36+
},
37+
];
3238
};
3339

34-
export type SWbound = {};
40+
export type SWbound = {
41+
sendSetCookie: [
42+
{
43+
url: string;
44+
cookie: string;
45+
},
46+
];
47+
};

packages/scramjet/packages/core/src/client/shared/event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function (client: ScramjetClient, self: Self) {
1010
const handlers = {
1111
message: {
1212
_init() {
13-
if (client.init.shouldBlockMessageEvent(this)) {
13+
if (client.init.shouldBlockMessageEvent?.(this)) {
1414
return false;
1515
}
1616

packages/scramjet/packages/core/src/client/shared/import.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function (client: ScramjetClient, self: Self) {
88
"return import(url)"
99
);
1010

11-
Object.defineProperty(self, config.globals.importfn, {
11+
Object.defineProperty(self, client.config.globals.importfn, {
1212
value: function (base: string, url: string) {
1313
const resolved = new URL(url, base).href;
1414

packages/scramjet/packages/core/src/client/shared/requests/websocket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
3030
new WeakMap();
3131
client.Proxy("WebSocket", {
3232
construct(ctx) {
33-
if (client.init.shouldPassthroughWebsocket(ctx.args[0])) {
33+
if (client.init.shouldPassthroughWebsocket?.(ctx.args[0])) {
3434
return ctx.return(client.natives.construct("WebSocket", ...ctx.args));
3535
}
3636
const fakeWebSocket = new EventTarget() as WebSocket;

0 commit comments

Comments
 (0)