@mercuryworkshop/scramjet-controller 0.0.9 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/inject.ts CHANGED
@@ -1,7 +1,5 @@
1
- import type { CookieJar, ScramjetConfig } from "@mercuryworkshop/scramjet";
2
1
  import type * as ScramjetGlobal from "@mercuryworkshop/scramjet";
3
2
  declare const $scramjet: typeof ScramjetGlobal;
4
-
5
3
  import type {
6
4
  RawHeaders,
7
5
  ProxyTransport,
@@ -9,10 +7,12 @@ import type {
9
7
  } from "@mercuryworkshop/proxy-transports";
10
8
 
11
9
  import { RpcHelper } from "@mercuryworkshop/rpc";
10
+ import type { Config } from ".";
11
+ import { CONTROLLERFRAME } from "./symbols";
12
12
  import type {
13
+ SerializedCookieSyncEntry,
13
14
  ControllerToTransport,
14
15
  TransportToController,
15
- WebSocketData,
16
16
  WebSocketMessage,
17
17
  } from "./types";
18
18
 
@@ -71,7 +71,7 @@ class RemoteTransport implements ProxyTransport {
71
71
  (code: number, reason: string) => void,
72
72
  ] {
73
73
  const channel = new MessageChannel();
74
- let port = channel.port1;
74
+ const port = channel.port1;
75
75
  console.warn("connecting");
76
76
  this.rpc
77
77
  .call(
@@ -130,7 +130,7 @@ class RemoteTransport implements ProxyTransport {
130
130
  method: string,
131
131
  body: BodyInit | null,
132
132
  headers: RawHeaders,
133
- signal: AbortSignal | undefined
133
+ _signal: AbortSignal | undefined
134
134
  ): Promise<TransferrableResponse> {
135
135
  return await this.rpc.call("request", {
136
136
  remote: remote.href,
@@ -140,107 +140,231 @@ class RemoteTransport implements ProxyTransport {
140
140
  });
141
141
  }
142
142
 
143
- meta() {
144
- return {};
143
+ async sendSetCookie(
144
+ cookies: Array<{ url: URL; cookie: string }>,
145
+ options: ScramjetGlobal.CookieSyncOptions = {}
146
+ ): Promise<void> {
147
+ await this.rpc.call("sendSetCookie", {
148
+ cookies: cookies.map(({ url, cookie }) => ({
149
+ url: url.href,
150
+ cookie,
151
+ })),
152
+ options,
153
+ });
145
154
  }
146
155
  }
147
156
 
148
157
  const sw = navigator.serviceWorker.controller;
149
158
  const { SCRAMJETCLIENT, ScramjetClient, CookieJar, setWasm } = $scramjet;
150
159
 
151
- type Config = any;
152
160
  type Init = {
153
161
  config: Config;
154
- sjconfig: ScramjetConfig;
155
- cookies: string;
162
+ sjconfig: ScramjetGlobal.ScramjetConfig;
156
163
  prefix: URL;
164
+ cookies: string;
157
165
  yieldGetInjectScripts: (
158
- cookieJar: CookieJar,
159
166
  config: Config,
160
- sjconfig: ScramjetConfig,
161
- prefix: URL
167
+ sjconfig: ScramjetGlobal.ScramjetConfig,
168
+ prefix: URL,
169
+ cookieJar: ScramjetGlobal.CookieJar,
170
+ codecEncode: (input: string) => string,
171
+ codecDecode: (input: string) => string
162
172
  ) => any;
163
173
  codecEncode: (input: string) => string;
164
174
  codecDecode: (input: string) => string;
175
+ initHeaders: RawHeaders;
176
+ history: ScramjetGlobal.TrackedHistoryState[];
165
177
  };
166
178
 
167
- export function load({
168
- config,
169
- sjconfig,
170
- cookies,
171
- prefix,
172
- yieldGetInjectScripts,
173
- codecEncode,
174
- codecDecode,
175
- }: Init) {
176
- let client;
179
+ export function load(init: Init) {
177
180
  if (SCRAMJETCLIENT in globalThis) {
178
- client = globalThis[SCRAMJETCLIENT];
179
- } else {
180
- setWasm(Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)));
181
- delete self.WASM;
181
+ (
182
+ (globalThis as any)[SCRAMJETCLIENT] as ScramjetGlobal.ScramjetClient
183
+ ).syncDocumentInit({
184
+ initHeaders: init.initHeaders,
185
+ history: init.history,
186
+ cookies: init.cookies,
187
+ });
188
+ return;
189
+ }
190
+ if (!("WASM" in self)) {
191
+ throw new Error("WASM not found in global scope!");
192
+ }
193
+ const wasm = Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0));
194
+ delete (self as any).WASM;
195
+ setWasm(wasm);
196
+
197
+ new ExecutionContextWrapper(globalThis, init);
198
+ }
199
+
200
+ function createFrameId() {
201
+ return `${Array(8)
202
+ .fill(0)
203
+ .map(() => Math.floor(Math.random() * 36).toString(36))
204
+ .join("")}`;
205
+ }
206
+
207
+ class ExecutionContextWrapper {
208
+ client!: ScramjetGlobal.ScramjetClient;
209
+ cookieJar: ScramjetGlobal.CookieJar;
210
+ transport: RemoteTransport;
211
+ private handleServiceWorkerCookieMessage: (event: MessageEvent) => void;
182
212
 
213
+ constructor(
214
+ public global: typeof globalThis,
215
+ public init: Init
216
+ ) {
183
217
  const channel = new MessageChannel();
184
- const transport = new RemoteTransport(channel.port1);
218
+ this.transport = new RemoteTransport(channel.port1);
185
219
  sw?.postMessage(
186
220
  {
187
221
  $sw$initRemoteTransport: {
188
222
  port: channel.port2,
189
- prefix: prefix.href,
223
+ prefix: this.init.prefix.href,
190
224
  },
191
225
  },
192
226
  [channel.port2]
193
227
  );
194
228
 
195
- const cookieJar = new CookieJar();
196
- cookieJar.load(cookies);
229
+ this.cookieJar = new CookieJar();
230
+ this.cookieJar.load(this.init.cookies);
197
231
 
198
- const context = {
199
- interface: {
200
- getInjectScripts: yieldGetInjectScripts(
201
- cookieJar,
202
- config,
203
- sjconfig,
204
- prefix
205
- ),
206
- codecEncode,
207
- codecDecode,
208
- },
209
- prefix,
210
- cookieJar,
211
- config: sjconfig,
232
+ this.handleServiceWorkerCookieMessage = (event: MessageEvent) => {
233
+ if (
234
+ !event.data?.$controller$setCookie ||
235
+ typeof event.data.$controller$setCookie !== "object"
236
+ ) {
237
+ return;
238
+ }
239
+
240
+ const payload = event.data.$controller$setCookie as {
241
+ cookies?: SerializedCookieSyncEntry[];
242
+ options?: ScramjetGlobal.CookieSyncOptions;
243
+ id?: string;
244
+ };
245
+
246
+ if (payload.options?.clear) {
247
+ this.cookieJar.clear();
248
+ }
249
+
250
+ if (Array.isArray(payload.cookies)) {
251
+ for (const cookie of payload.cookies) {
252
+ if (
253
+ typeof cookie?.url !== "string" ||
254
+ typeof cookie.cookie !== "string"
255
+ ) {
256
+ continue;
257
+ }
258
+
259
+ try {
260
+ this.cookieJar.setCookies(cookie.cookie, new URL(cookie.url));
261
+ } catch {
262
+ console.error("Failed to set cookie", cookie);
263
+ }
264
+ }
265
+ }
266
+
267
+ if (typeof payload.id === "string") {
268
+ const targetSw = navigator.serviceWorker?.controller ?? sw;
269
+ targetSw?.postMessage({
270
+ $sw$setCookieDone: {
271
+ id: payload.id,
272
+ },
273
+ });
274
+ }
212
275
  };
213
- function createFrameId() {
214
- return `${Array(8)
215
- .fill(0)
216
- .map(() => Math.floor(Math.random() * 36).toString(36))
217
- .join("")}`;
218
- }
219
276
 
220
- const frame = globalThis.frameElement as HTMLIFrameElement | null;
277
+ navigator.serviceWorker?.addEventListener(
278
+ "message",
279
+ this.handleServiceWorkerCookieMessage
280
+ );
281
+
282
+ this.injectScramjet();
283
+ }
284
+
285
+ injectScramjet() {
286
+ const frame = this.global.frameElement as HTMLIFrameElement | null;
221
287
  if (frame && !frame.name) {
222
- frame.name = createFrameId();
288
+ window.name = frame.name = createFrameId();
223
289
  }
224
-
225
- client = new ScramjetClient(globalThis, {
290
+ let controllerFrame = frame?.[CONTROLLERFRAME];
291
+ let isTopLevel = true;
292
+ if (!controllerFrame) {
293
+ isTopLevel = false;
294
+ let currentwin = this.global.window;
295
+ while (currentwin.parent !== currentwin) {
296
+ const currentclient = currentwin[$scramjet.SCRAMJETCLIENT];
297
+ if (!currentclient) {
298
+ currentwin = currentwin.parent.window;
299
+ continue;
300
+ }
301
+ const currentFrame = currentclient.descriptors.get(
302
+ "window.frameElement",
303
+ currentwin
304
+ );
305
+ if (currentFrame && currentFrame[CONTROLLERFRAME]) {
306
+ controllerFrame = currentFrame[CONTROLLERFRAME];
307
+ break;
308
+ }
309
+ currentwin = currentwin.parent.window;
310
+ }
311
+ }
312
+ const context: ScramjetGlobal.ScramjetContext = {
313
+ config: this.init.sjconfig,
314
+ prefix: this.init.prefix,
315
+ cookieJar: this.cookieJar,
316
+ interface: {
317
+ getInjectScripts: this.init.yieldGetInjectScripts(
318
+ this.init.config,
319
+ this.init.sjconfig,
320
+ this.init.prefix,
321
+ this.cookieJar,
322
+ this.init.codecEncode,
323
+ this.init.codecDecode
324
+ ),
325
+ codecEncode: this.init.codecEncode,
326
+ codecDecode: this.init.codecDecode,
327
+ },
328
+ };
329
+ this.client = new ScramjetClient(this.global, {
226
330
  context,
227
- transport,
228
- sendSetCookie: async (url, cookie) => {
229
- // sw.postMessage({
230
- // $controller$setCookie: {
231
- // url,
232
- // cookie
233
- // }
234
- // });
331
+ transport: this.transport,
332
+ sendSetCookie: async (cookies, options) => {
333
+ await this.transport.sendSetCookie(cookies, options);
235
334
  },
236
- shouldPassthroughWebsocket: (url) => {
237
- return url === "wss://anura.pro/";
335
+ shouldPassthroughWebsocket: () => {
336
+ return false;
238
337
  },
239
- shouldBlockMessageEvent(i) {
338
+ shouldBlockMessageEvent: () => {
240
339
  return false;
241
340
  },
341
+ hookSubcontext: (frameself) => {
342
+ const context = new ExecutionContextWrapper(frameself, {
343
+ ...this.init,
344
+ cookies: this.cookieJar.dump(),
345
+ });
346
+ return context.client;
347
+ },
348
+ initHeaders: this.init.initHeaders,
349
+ history: this.init.history,
242
350
  });
243
-
244
- client.hook();
351
+ const frameInitContext = {
352
+ window: this.global.window,
353
+ client: this.client,
354
+ isTopLevel,
355
+ };
356
+ if (controllerFrame)
357
+ $scramjet.Tap.dispatch(
358
+ controllerFrame.hooks.init.pre,
359
+ frameInitContext,
360
+ {}
361
+ );
362
+ this.client.hook();
363
+ if (controllerFrame)
364
+ $scramjet.Tap.dispatch(
365
+ controllerFrame.hooks.init.post,
366
+ frameInitContext,
367
+ {}
368
+ );
245
369
  }
246
370
  }
package/src/sw.ts CHANGED
@@ -1,14 +1,14 @@
1
- declare var clients: Clients;
1
+ /// <reference lib="WebWorker" />
2
+ /// <reference types="@types/serviceworker" />
2
3
  import { RpcHelper } from "@mercuryworkshop/rpc";
3
4
  import type { Controllerbound, SWbound } from "./types";
4
5
  import type { RawHeaders } from "@mercuryworkshop/proxy-transports";
5
- import { ScramjetHeaders } from "@mercuryworkshop/scramjet";
6
6
 
7
7
  function makeId(): string {
8
8
  return Math.random().toString(36).substring(2, 10);
9
9
  }
10
10
 
11
- let cookieResolvers: Record<string, (value: void) => void> = {};
11
+ const cookieResolvers: Record<string, (value: void) => void> = {};
12
12
  addEventListener("message", (e) => {
13
13
  if (!e.data) return;
14
14
  if (typeof e.data != "object") return;
@@ -49,36 +49,82 @@ class ControllerReference {
49
49
  ) {
50
50
  this.rpc = new RpcHelper(
51
51
  {
52
- sendSetCookie: async ({ url, cookie }) => {
53
- let clients = await self.clients.matchAll();
54
- let promises = [];
52
+ sendSetCookie: async ({ cookies, options }) => {
53
+ const clients = await self.clients.matchAll();
54
+ const ids: string[] = [];
55
+ const promises: Promise<string>[] = [];
56
+
57
+ // Navigation fetches (document/iframe) deliver cookies via the inject
58
+ // script's embedded cookieJar dump — the destination page doesn't have
59
+ // inject.ts loaded yet to ack, so awaiting would deadlock. Broadcast
60
+ // so any already-loaded clients can update their jars, but don't wait.
61
+ const isNavigation =
62
+ options?.destination === "document" ||
63
+ options?.destination === "iframe";
55
64
 
56
65
  for (const client of clients) {
57
- let id = makeId();
66
+ const id = makeId();
67
+ ids.push(id);
58
68
  client.postMessage({
59
69
  $controller$setCookie: {
60
- url,
61
- cookie,
70
+ cookies,
71
+ options,
62
72
  id,
63
73
  },
64
74
  });
65
- promises.push(
66
- new Promise<void>((resolve) => {
67
- cookieResolvers[id] = resolve;
68
- })
69
- );
75
+ if (!isNavigation) {
76
+ promises.push(
77
+ new Promise<string>((resolve) => {
78
+ // Resolve with the id so we know which client replied.
79
+ cookieResolvers[id] = () => resolve(id);
80
+ })
81
+ );
82
+ }
70
83
  }
71
- await Promise.race([
72
- new Promise<void>((resolve) =>
73
- setTimeout(() => {
74
- console.error(
75
- "timed out waiting for set cookie response (deadlock?)"
76
- );
84
+ // Wait for the first client to acknowledge the cookie sync.
85
+ // Using Promise.any (not Promise.all) so that extra SW clients created by
86
+ // window.open (e.g. test popup windows) don't cause timeouts — only the
87
+ // main controller client needs to respond.
88
+ if (promises.length > 0) {
89
+ let timeoutId: ReturnType<typeof setTimeout> | undefined;
90
+ let responded = false;
91
+ const timeoutPromise = new Promise<void>((resolve) => {
92
+ timeoutId = setTimeout(() => {
93
+ if (!responded) {
94
+ const pending = ids.filter(
95
+ (id) => cookieResolvers[id] !== undefined
96
+ );
97
+ console.error(
98
+ "timed out waiting for set cookie response (deadlock?): " +
99
+ `cookies=${cookies.length} clients=${clients.length} ` +
100
+ `pending=${pending.length}/${ids.length} ` +
101
+ `clientUrls=${clients.map((c) => c.url).join(",")}`
102
+ );
103
+ }
77
104
  resolve();
78
- }, 1000)
79
- ),
80
- promises,
81
- ]);
105
+ }, 1000);
106
+ });
107
+
108
+ try {
109
+ await Promise.race([
110
+ timeoutPromise,
111
+ Promise.any(promises)
112
+ .then(() => {
113
+ responded = true;
114
+ })
115
+ .catch(() => {}),
116
+ ]);
117
+ } finally {
118
+ // Clear the timeout so it doesn't fire spuriously after the
119
+ // race has already been won by Promise.any.
120
+ if (timeoutId !== undefined) clearTimeout(timeoutId);
121
+ // Clean up any pending resolvers so clients that never
122
+ // responded don't leak entries in cookieResolvers.
123
+ for (const id of ids) {
124
+ delete cookieResolvers[id];
125
+ }
126
+ }
127
+ }
82
128
  },
83
129
  },
84
130
  "tabchannel-" + id,
@@ -86,12 +132,12 @@ class ControllerReference {
86
132
  port.postMessage(data, transfer);
87
133
  }
88
134
  );
89
- port.addEventListener("message", (e) => {
135
+ port.onmessage = (e: MessageEvent) => {
90
136
  this.rpc.recieve(e.data);
91
- });
137
+ };
92
138
  port.onmessageerror = console.error;
93
139
 
94
- this.rpc.call("ready", null);
140
+ this.rpc.call("ready", undefined);
95
141
  }
96
142
  }
97
143
 
@@ -104,6 +150,10 @@ addEventListener("message", (e) => {
104
150
  if (typeof e.data.$controller$init != "object") return;
105
151
  const init = e.data.$controller$init;
106
152
 
153
+ const existing = tabs.findIndex((t) => t.id === init.id);
154
+ if (existing !== -1) {
155
+ tabs.splice(existing, 1);
156
+ }
107
157
  tabs.push(new ControllerReference(init.prefix, init.id, e.ports[0]));
108
158
  });
109
159
 
@@ -125,6 +175,7 @@ export async function route(event: FetchEvent): Promise<Response> {
125
175
  "request",
126
176
  {
127
177
  rawUrl: event.request.url,
178
+ rawReferrer: event.request.referrer,
128
179
  destination: event.request.destination,
129
180
  mode: event.request.mode,
130
181
  referrer: event.request.referrer,
@@ -134,6 +185,7 @@ export async function route(event: FetchEvent): Promise<Response> {
134
185
  forceCrossOriginIsolated: false,
135
186
  initialHeaders: rawheaders,
136
187
  rawClientUrl: client ? client.url : undefined,
188
+ clientId: event.clientId || event.resultingClientId,
137
189
  },
138
190
  event.request.body instanceof ReadableStream ||
139
191
  // @ts-expect-error the types for fetchevent are messed up
@@ -157,3 +209,23 @@ export async function route(event: FetchEvent): Promise<Response> {
157
209
  );
158
210
  }
159
211
  }
212
+
213
+ addEventListener("install", () => {
214
+ self.skipWaiting();
215
+ });
216
+
217
+ addEventListener("activate", (event: ExtendableEvent) => {
218
+ event.waitUntil(clients.claim());
219
+ });
220
+
221
+ // the only way to know if a service worker has suddenly died is if this code runs again
222
+ // notify all clients to send over their messageports again
223
+ setTimeout(async () => {
224
+ console.log("service worker activated, notifying clients to revive");
225
+ for (const client of await clients.matchAll()) {
226
+ client.postMessage({
227
+ $controller$swrevive: {},
228
+ });
229
+ }
230
+ // short delay is apparently needed
231
+ }, 100);
package/src/symbols.ts ADDED
@@ -0,0 +1 @@
1
+ export const CONTROLLERFRAME = Symbol.for("controller frame handle");
package/src/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { RawHeaders } from "@mercuryworkshop/proxy-transports";
2
-
2
+ import type { CONTROLLERFRAME } from "./symbols";
3
+ import type { Frame } from ".";
3
4
  export type BodyType =
4
5
  | string
5
6
  | ArrayBuffer
@@ -8,6 +9,7 @@ export type BodyType =
8
9
 
9
10
  export type TransferRequest = {
10
11
  rawUrl: string;
12
+ rawReferrer: string | null;
11
13
  destination: RequestDestination;
12
14
  mode: RequestMode;
13
15
  referrer: string;
@@ -17,6 +19,7 @@ export type TransferRequest = {
17
19
  forceCrossOriginIsolated: boolean;
18
20
  initialHeaders: RawHeaders;
19
21
  rawClientUrl?: string;
22
+ clientId?: string;
20
23
  };
21
24
 
22
25
  export type TransferResponse = {
@@ -26,23 +29,22 @@ export type TransferResponse = {
26
29
  statusText: string;
27
30
  };
28
31
 
32
+ export type SerializedCookieSyncEntry = {
33
+ url: string;
34
+ cookie: string;
35
+ };
36
+
29
37
  export type Controllerbound = {
30
38
  ready: [];
31
39
  request: [TransferRequest, TransferResponse];
32
- sendSetCookie: [
33
- {
34
- url: string;
35
- cookie: string;
36
- },
37
- ];
38
40
  initRemoteTransport: [MessagePort];
39
41
  };
40
42
 
41
43
  export type SWbound = {
42
44
  sendSetCookie: [
43
45
  {
44
- url: string;
45
- cookie: string;
46
+ cookies: SerializedCookieSyncEntry[];
47
+ options?: CookieSyncOptions;
46
48
  },
47
49
  ];
48
50
  };
@@ -58,6 +60,12 @@ export type TransportToController = {
58
60
  },
59
61
  TransferrableResponse,
60
62
  ];
63
+ sendSetCookie: [
64
+ {
65
+ cookies: SerializedCookieSyncEntry[];
66
+ options?: CookieSyncOptions;
67
+ },
68
+ ];
61
69
  connect: [
62
70
  {
63
71
  url: string;
@@ -93,3 +101,39 @@ export type WebSocketMessage =
93
101
  code: number;
94
102
  reason: string;
95
103
  };
104
+ export type FrameInitHooks = {
105
+ pre: {
106
+ context: {
107
+ window: Window;
108
+ client: ScramjetClient;
109
+ isTopLevel: boolean;
110
+ };
111
+ props: {};
112
+ };
113
+ post: {
114
+ context: {
115
+ window: Window;
116
+ client: ScramjetClient;
117
+ isTopLevel: boolean;
118
+ };
119
+ props: {};
120
+ };
121
+ };
122
+
123
+ export type FrameErrorHooks = {
124
+ request: {
125
+ context: {
126
+ rawrequest: TransferRequest;
127
+ };
128
+ props: {
129
+ setResponse?: TransferResponse;
130
+ suppressError?: boolean;
131
+ };
132
+ };
133
+ };
134
+
135
+ declare global {
136
+ interface HTMLIFrameElement {
137
+ [CONTROLLERFRAME]: Frame;
138
+ }
139
+ }
@@ -0,0 +1,6 @@
1
+ import * as Types from "./index";
2
+ export * from "./index";
3
+
4
+ declare global {
5
+ const $scramjetController: typeof Types;
6
+ }
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "target": "ES2022",
4
4
  "useDefineForClassFields": true,
5
5
  "module": "ESNext",
6
- "lib": ["ES2022", "webworker", "DOM", "DOM.Iterable"],
6
+ "lib": ["ES2022", "WebWorker", "DOM", "DOM.Iterable"],
7
7
  "types": [],
8
8
  "skipLibCheck": true,
9
9
  /* Bundler mode */