@mercuryworkshop/scramjet-controller 0.0.1 → 0.0.3
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/dist/controller.api.js +469 -0
- package/dist/controller.api.js.map +1 -0
- package/dist/controller.inject.js +308 -0
- package/dist/controller.inject.js.map +1 -0
- package/dist/controller.sw.js +276 -0
- package/dist/controller.sw.js.map +1 -0
- package/package.json +10 -9
- package/src/index.ts +416 -0
- package/src/inject.ts +246 -0
- package/src/sw.ts +160 -0
- package/src/types.d.ts +95 -0
- package/tsconfig.json +25 -0
package/src/sw.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
declare var clients: Clients;
|
|
2
|
+
import { RpcHelper } from "@mercuryworkshop/rpc";
|
|
3
|
+
import type { Controllerbound, SWbound } from "./types";
|
|
4
|
+
import type { RawHeaders } from "@mercuryworkshop/proxy-transports";
|
|
5
|
+
import { ScramjetHeaders } from "@mercuryworkshop/scramjet";
|
|
6
|
+
|
|
7
|
+
function makeId(): string {
|
|
8
|
+
return Math.random().toString(36).substring(2, 10);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let cookieResolvers: Record<string, (value: void) => void> = {};
|
|
12
|
+
addEventListener("message", (e) => {
|
|
13
|
+
if (!e.data) return;
|
|
14
|
+
if (typeof e.data != "object") return;
|
|
15
|
+
if (e.data.$sw$setCookieDone && typeof e.data.$sw$setCookieDone == "object") {
|
|
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
|
+
|
|
25
|
+
if (
|
|
26
|
+
e.data.$sw$initRemoteTransport &&
|
|
27
|
+
typeof e.data.$sw$initRemoteTransport == "object"
|
|
28
|
+
) {
|
|
29
|
+
const { port, prefix } = e.data.$sw$initRemoteTransport;
|
|
30
|
+
|
|
31
|
+
console.error(prefix, tabs);
|
|
32
|
+
const relevantcontroller = tabs.find((tab) =>
|
|
33
|
+
new URL(prefix).pathname.startsWith(tab.prefix)
|
|
34
|
+
);
|
|
35
|
+
if (!relevantcontroller) {
|
|
36
|
+
console.error("No relevant controller found for transport init");
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
relevantcontroller.rpc.call("initRemoteTransport", port, [port]);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
class ControllerReference {
|
|
44
|
+
rpc: RpcHelper<SWbound, Controllerbound>;
|
|
45
|
+
|
|
46
|
+
constructor(
|
|
47
|
+
public prefix: string,
|
|
48
|
+
public id: string,
|
|
49
|
+
port: MessagePort
|
|
50
|
+
) {
|
|
51
|
+
this.rpc = new RpcHelper(
|
|
52
|
+
{
|
|
53
|
+
sendSetCookie: async ({ url, cookie }) => {
|
|
54
|
+
let clients = await self.clients.matchAll();
|
|
55
|
+
let promises = [];
|
|
56
|
+
|
|
57
|
+
for (const client of clients) {
|
|
58
|
+
let id = makeId();
|
|
59
|
+
client.postMessage({
|
|
60
|
+
$controller$setCookie: {
|
|
61
|
+
url,
|
|
62
|
+
cookie,
|
|
63
|
+
id,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
promises.push(
|
|
67
|
+
new Promise<void>((resolve) => {
|
|
68
|
+
cookieResolvers[id] = resolve;
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
await Promise.race([
|
|
73
|
+
new Promise<void>((resolve) =>
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
console.error(
|
|
76
|
+
"timed out waiting for set cookie response (deadlock?)"
|
|
77
|
+
);
|
|
78
|
+
resolve();
|
|
79
|
+
}, 1000)
|
|
80
|
+
),
|
|
81
|
+
promises,
|
|
82
|
+
]);
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
"tabchannel-" + id,
|
|
86
|
+
(data, transfer) => {
|
|
87
|
+
port.postMessage(data, transfer);
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
port.addEventListener("message", (e) => {
|
|
91
|
+
this.rpc.recieve(e.data);
|
|
92
|
+
});
|
|
93
|
+
port.onmessageerror = console.error;
|
|
94
|
+
|
|
95
|
+
this.rpc.call("ready", null);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const tabs: ControllerReference[] = [];
|
|
100
|
+
|
|
101
|
+
addEventListener("message", (e) => {
|
|
102
|
+
if (!e.data) return;
|
|
103
|
+
if (typeof e.data != "object") return;
|
|
104
|
+
if (!e.data.$controller$init) return;
|
|
105
|
+
if (typeof e.data.$controller$init != "object") return;
|
|
106
|
+
const init = e.data.$controller$init;
|
|
107
|
+
|
|
108
|
+
tabs.push(new ControllerReference(init.prefix, init.id, e.ports[0]));
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
export function shouldRoute(event: FetchEvent): boolean {
|
|
112
|
+
const url = new URL(event.request.url);
|
|
113
|
+
const tab = tabs.find((tab) => url.pathname.startsWith(tab.prefix));
|
|
114
|
+
return tab !== undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export async function route(event: FetchEvent): Promise<Response> {
|
|
118
|
+
try {
|
|
119
|
+
const url = new URL(event.request.url);
|
|
120
|
+
const tab = tabs.find((tab) => url.pathname.startsWith(tab.prefix))!;
|
|
121
|
+
const client = await clients.get(event.clientId);
|
|
122
|
+
|
|
123
|
+
const rawheaders: RawHeaders = [...event.request.headers];
|
|
124
|
+
|
|
125
|
+
const response = await tab.rpc.call(
|
|
126
|
+
"request",
|
|
127
|
+
{
|
|
128
|
+
rawUrl: event.request.url,
|
|
129
|
+
destination: event.request.destination,
|
|
130
|
+
mode: event.request.mode,
|
|
131
|
+
referrer: event.request.referrer,
|
|
132
|
+
method: event.request.method,
|
|
133
|
+
body: event.request.body,
|
|
134
|
+
cache: event.request.cache,
|
|
135
|
+
forceCrossOriginIsolated: false,
|
|
136
|
+
initialHeaders: rawheaders,
|
|
137
|
+
rawClientUrl: client ? client.url : undefined,
|
|
138
|
+
},
|
|
139
|
+
event.request.body instanceof ReadableStream ||
|
|
140
|
+
// @ts-expect-error the types for fetchevent are messed up
|
|
141
|
+
event.request.body instanceof ArrayBuffer
|
|
142
|
+
? [event.request.body]
|
|
143
|
+
: undefined
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
return new Response(response.body, {
|
|
147
|
+
status: response.status,
|
|
148
|
+
statusText: response.statusText,
|
|
149
|
+
headers: response.headers,
|
|
150
|
+
});
|
|
151
|
+
} catch (e) {
|
|
152
|
+
console.error("Service Worker error:", e);
|
|
153
|
+
return new Response(
|
|
154
|
+
"Internal Service Worker Error: " + (e as Error).message,
|
|
155
|
+
{
|
|
156
|
+
status: 500,
|
|
157
|
+
}
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
}
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { RawHeaders } from "@mercuryworkshop/proxy-transports";
|
|
2
|
+
|
|
3
|
+
export type BodyType =
|
|
4
|
+
| string
|
|
5
|
+
| ArrayBuffer
|
|
6
|
+
| Blob
|
|
7
|
+
| ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
8
|
+
|
|
9
|
+
export type TransferRequest = {
|
|
10
|
+
rawUrl: string;
|
|
11
|
+
destination: RequestDestination;
|
|
12
|
+
mode: RequestMode;
|
|
13
|
+
referrer: string;
|
|
14
|
+
method: string;
|
|
15
|
+
body: BodyType | null;
|
|
16
|
+
cache: RequestCache;
|
|
17
|
+
forceCrossOriginIsolated: boolean;
|
|
18
|
+
initialHeaders: RawHeaders;
|
|
19
|
+
rawClientUrl?: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type TransferResponse = {
|
|
23
|
+
body: BodyType;
|
|
24
|
+
headers: RawHeaders;
|
|
25
|
+
status: number;
|
|
26
|
+
statusText: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type Controllerbound = {
|
|
30
|
+
ready: [];
|
|
31
|
+
request: [TransferRequest, TransferResponse];
|
|
32
|
+
sendSetCookie: [
|
|
33
|
+
{
|
|
34
|
+
url: string;
|
|
35
|
+
cookie: string;
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
initRemoteTransport: [MessagePort];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type SWbound = {
|
|
42
|
+
sendSetCookie: [
|
|
43
|
+
{
|
|
44
|
+
url: string;
|
|
45
|
+
cookie: string;
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type TransportToController = {
|
|
51
|
+
request: [
|
|
52
|
+
{
|
|
53
|
+
remote: string;
|
|
54
|
+
method: string;
|
|
55
|
+
body: BodyInit | null;
|
|
56
|
+
headers: RawHeaders;
|
|
57
|
+
// signal: AbortSignal | undefined
|
|
58
|
+
},
|
|
59
|
+
TransferrableResponse,
|
|
60
|
+
];
|
|
61
|
+
connect: [
|
|
62
|
+
{
|
|
63
|
+
url: string;
|
|
64
|
+
protocols: string[];
|
|
65
|
+
requestHeaders: RawHeaders;
|
|
66
|
+
port: MessagePort;
|
|
67
|
+
},
|
|
68
|
+
(
|
|
69
|
+
| {
|
|
70
|
+
result: "success";
|
|
71
|
+
protocol: string;
|
|
72
|
+
extensions: string;
|
|
73
|
+
}
|
|
74
|
+
| {
|
|
75
|
+
result: "failure";
|
|
76
|
+
error: string;
|
|
77
|
+
}
|
|
78
|
+
),
|
|
79
|
+
];
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type ControllerToTransport = {
|
|
83
|
+
ready: [];
|
|
84
|
+
};
|
|
85
|
+
export type WebSocketData = string | ArrayBuffer | Blob;
|
|
86
|
+
export type WebSocketMessage =
|
|
87
|
+
| {
|
|
88
|
+
type: "data";
|
|
89
|
+
data: WebSocketData;
|
|
90
|
+
}
|
|
91
|
+
| {
|
|
92
|
+
type: "close";
|
|
93
|
+
code: number;
|
|
94
|
+
reason: string;
|
|
95
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
{
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "ES2022",
|
|
5
|
+
"useDefineForClassFields": true,
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"lib": ["ES2022", "webworker", "DOM", "DOM.Iterable"],
|
|
8
|
+
"types": [],
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
/* Bundler mode */
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"allowImportingTsExtensions": true,
|
|
13
|
+
"verbatimModuleSyntax": true,
|
|
14
|
+
"moduleDetection": "force",
|
|
15
|
+
"noEmit": true,
|
|
16
|
+
/* Linting */
|
|
17
|
+
"strict": true,
|
|
18
|
+
"noUnusedLocals": true,
|
|
19
|
+
"noUnusedParameters": false,
|
|
20
|
+
"erasableSyntaxOnly": false,
|
|
21
|
+
"noFallthroughCasesInSwitch": true,
|
|
22
|
+
"noUncheckedSideEffectImports": true,
|
|
23
|
+
},
|
|
24
|
+
"include": ["src"]
|
|
25
|
+
}
|