@mercuryworkshop/scramjet-controller 0.0.5 → 0.0.7
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 +437 -14
- package/dist/controller.api.js.map +1 -1
- package/dist/controller.inject.js +296 -1
- package/dist/controller.inject.js.map +1 -1
- package/dist/controller.sw.js +264 -1
- package/dist/controller.sw.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +13 -6
- package/tsconfig.json +0 -1
|
@@ -1,2 +1,297 @@
|
|
|
1
|
-
var $
|
|
1
|
+
var $scramjetControllerInit;
|
|
2
|
+
(() => {
|
|
3
|
+
var __webpack_modules__ = ({
|
|
4
|
+
"./packages/scramjet/packages/rpc/index.ts"(__unused_rspack_module, __webpack_exports__, __webpack_require__) {
|
|
5
|
+
__webpack_require__.r(__webpack_exports__);
|
|
6
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
7
|
+
RpcHelper: () => (RpcHelper)
|
|
8
|
+
});
|
|
9
|
+
class RpcHelper {
|
|
10
|
+
methods;
|
|
11
|
+
id;
|
|
12
|
+
sendRaw;
|
|
13
|
+
counter = 0;
|
|
14
|
+
promiseCallbacks = new Map();
|
|
15
|
+
constructor(methods, id, sendRaw){
|
|
16
|
+
this.methods = methods;
|
|
17
|
+
this.id = id;
|
|
18
|
+
this.sendRaw = sendRaw;
|
|
19
|
+
}
|
|
20
|
+
recieve(data) {
|
|
21
|
+
if (data === undefined || data === null || typeof data !== "object") return;
|
|
22
|
+
const dt = data[this.id];
|
|
23
|
+
if (dt === undefined || dt === null || typeof dt !== "object") return;
|
|
24
|
+
const type = dt.$type;
|
|
25
|
+
if (type === "response") {
|
|
26
|
+
const token = dt.$token;
|
|
27
|
+
const data = dt.$data;
|
|
28
|
+
const error = dt.$error;
|
|
29
|
+
const cb = this.promiseCallbacks.get(token);
|
|
30
|
+
if (!cb) return;
|
|
31
|
+
this.promiseCallbacks.delete(token);
|
|
32
|
+
if (error !== undefined) {
|
|
33
|
+
cb.reject(new Error(error));
|
|
34
|
+
} else {
|
|
35
|
+
cb.resolve(data);
|
|
36
|
+
}
|
|
37
|
+
} else if (type === "request") {
|
|
38
|
+
const method = dt.$method;
|
|
39
|
+
const args = dt.$args;
|
|
40
|
+
this.methods[method](args).then((r)=>{
|
|
41
|
+
this.sendRaw({
|
|
42
|
+
[this.id]: {
|
|
43
|
+
$type: "response",
|
|
44
|
+
$token: dt.$token,
|
|
45
|
+
$data: r?.[0]
|
|
46
|
+
}
|
|
47
|
+
}, r?.[1]);
|
|
48
|
+
}).catch((err)=>{
|
|
49
|
+
console.error(err);
|
|
50
|
+
this.sendRaw({
|
|
51
|
+
[this.id]: {
|
|
52
|
+
$type: "response",
|
|
53
|
+
$token: dt.$token,
|
|
54
|
+
$error: err?.toString() || "Unknown error"
|
|
55
|
+
}
|
|
56
|
+
}, []);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
call(method, args, transfer = []) {
|
|
61
|
+
let token = this.counter++;
|
|
62
|
+
return new Promise((resolve, reject)=>{
|
|
63
|
+
this.promiseCallbacks.set(token, {
|
|
64
|
+
resolve,
|
|
65
|
+
reject
|
|
66
|
+
});
|
|
67
|
+
this.sendRaw({
|
|
68
|
+
[this.id]: {
|
|
69
|
+
$type: "request",
|
|
70
|
+
$method: method,
|
|
71
|
+
$args: args,
|
|
72
|
+
$token: token
|
|
73
|
+
}
|
|
74
|
+
}, transfer);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
});
|
|
83
|
+
// The module cache
|
|
84
|
+
var __webpack_module_cache__ = {};
|
|
85
|
+
|
|
86
|
+
// The require function
|
|
87
|
+
function __webpack_require__(moduleId) {
|
|
88
|
+
|
|
89
|
+
// Check if module is in cache
|
|
90
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
91
|
+
if (cachedModule !== undefined) {
|
|
92
|
+
return cachedModule.exports;
|
|
93
|
+
}
|
|
94
|
+
// Create a new module (and put it into the cache)
|
|
95
|
+
var module = (__webpack_module_cache__[moduleId] = {
|
|
96
|
+
exports: {}
|
|
97
|
+
});
|
|
98
|
+
// Execute the module function
|
|
99
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
100
|
+
|
|
101
|
+
// Return the exports of the module
|
|
102
|
+
return module.exports;
|
|
103
|
+
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// webpack/runtime/define_property_getters
|
|
107
|
+
(() => {
|
|
108
|
+
__webpack_require__.d = (exports, definition) => {
|
|
109
|
+
for(var key in definition) {
|
|
110
|
+
if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
111
|
+
Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
})();
|
|
116
|
+
// webpack/runtime/has_own_property
|
|
117
|
+
(() => {
|
|
118
|
+
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
119
|
+
})();
|
|
120
|
+
// webpack/runtime/make_namespace_object
|
|
121
|
+
(() => {
|
|
122
|
+
// define __esModule on exports
|
|
123
|
+
__webpack_require__.r = (exports) => {
|
|
124
|
+
if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
125
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
126
|
+
}
|
|
127
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
128
|
+
};
|
|
129
|
+
})();
|
|
130
|
+
var __webpack_exports__ = {};
|
|
131
|
+
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
|
|
132
|
+
(() => {
|
|
133
|
+
__webpack_require__.r(__webpack_exports__);
|
|
134
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
135
|
+
load: () => (load)
|
|
136
|
+
});
|
|
137
|
+
/* import */ var _mercuryworkshop_rpc__rspack_import_0 = __webpack_require__("./packages/scramjet/packages/rpc/index.ts");
|
|
138
|
+
|
|
139
|
+
const MessagePort_postMessage = MessagePort.prototype.postMessage;
|
|
140
|
+
const postMessage = (port, data, transfer)=>{
|
|
141
|
+
MessagePort_postMessage.call(port, data, transfer);
|
|
142
|
+
};
|
|
143
|
+
class RemoteTransport {
|
|
144
|
+
port;
|
|
145
|
+
readyResolve;
|
|
146
|
+
readyPromise = new Promise((resolve)=>{
|
|
147
|
+
this.readyResolve = resolve;
|
|
148
|
+
});
|
|
149
|
+
ready = false;
|
|
150
|
+
async init() {
|
|
151
|
+
await this.readyPromise;
|
|
152
|
+
this.ready = true;
|
|
153
|
+
}
|
|
154
|
+
rpc;
|
|
155
|
+
constructor(port){
|
|
156
|
+
this.port = port;
|
|
157
|
+
this.rpc = new _mercuryworkshop_rpc__rspack_import_0.RpcHelper({
|
|
158
|
+
ready: async ()=>{
|
|
159
|
+
this.readyResolve();
|
|
160
|
+
}
|
|
161
|
+
}, "transport", (data, transfer)=>{
|
|
162
|
+
postMessage(port, data, transfer);
|
|
163
|
+
});
|
|
164
|
+
port.onmessageerror = (ev)=>{
|
|
165
|
+
console.error("onmessageerror (this should never happen!)", ev);
|
|
166
|
+
};
|
|
167
|
+
port.onmessage = (ev)=>{
|
|
168
|
+
this.rpc.recieve(ev.data);
|
|
169
|
+
};
|
|
170
|
+
port.start();
|
|
171
|
+
}
|
|
172
|
+
connect(url, protocols, requestHeaders, onopen, onmessage, onclose, onerror) {
|
|
173
|
+
const channel = new MessageChannel();
|
|
174
|
+
let port = channel.port1;
|
|
175
|
+
console.warn("connecting");
|
|
176
|
+
this.rpc.call("connect", {
|
|
177
|
+
url: url.href,
|
|
178
|
+
protocols,
|
|
179
|
+
requestHeaders,
|
|
180
|
+
port: channel.port2
|
|
181
|
+
}, [
|
|
182
|
+
channel.port2
|
|
183
|
+
]).then((response)=>{
|
|
184
|
+
console.log(response);
|
|
185
|
+
if (response.result === "success") {
|
|
186
|
+
onopen(response.protocol, response.extensions);
|
|
187
|
+
} else {
|
|
188
|
+
onerror(response.error);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
port.onmessage = (ev)=>{
|
|
192
|
+
const message = ev.data;
|
|
193
|
+
if (message.type === "data") {
|
|
194
|
+
onmessage(message.data);
|
|
195
|
+
} else if (message.type === "close") {
|
|
196
|
+
onclose(message.code, message.reason);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
port.onmessageerror = (ev)=>{
|
|
200
|
+
console.error("onmessageerror (this should never happen!)", ev);
|
|
201
|
+
onerror("Message error in transport port");
|
|
202
|
+
};
|
|
203
|
+
return [
|
|
204
|
+
(data)=>{
|
|
205
|
+
postMessage(port, {
|
|
206
|
+
type: "data",
|
|
207
|
+
data: data
|
|
208
|
+
}, data instanceof ArrayBuffer ? [
|
|
209
|
+
data
|
|
210
|
+
] : []);
|
|
211
|
+
},
|
|
212
|
+
(code)=>{
|
|
213
|
+
postMessage(port, {
|
|
214
|
+
type: "close",
|
|
215
|
+
code: code
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
async request(remote, method, body, headers, signal) {
|
|
221
|
+
return await this.rpc.call("request", {
|
|
222
|
+
remote: remote.href,
|
|
223
|
+
method,
|
|
224
|
+
body,
|
|
225
|
+
headers
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
meta() {
|
|
229
|
+
return {};
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const sw = navigator.serviceWorker.controller;
|
|
233
|
+
const { SCRAMJETCLIENT, ScramjetClient, CookieJar, setWasm } = $scramjet;
|
|
234
|
+
function load({ config, sjconfig, cookies, prefix, yieldGetInjectScripts, codecEncode, codecDecode }) {
|
|
235
|
+
let client;
|
|
236
|
+
if (SCRAMJETCLIENT in globalThis) {
|
|
237
|
+
client = globalThis[SCRAMJETCLIENT];
|
|
238
|
+
} else {
|
|
239
|
+
setWasm(Uint8Array.from(atob(self.WASM), (c)=>c.charCodeAt(0)));
|
|
240
|
+
delete self.WASM;
|
|
241
|
+
const channel = new MessageChannel();
|
|
242
|
+
const transport = new RemoteTransport(channel.port1);
|
|
243
|
+
sw?.postMessage({
|
|
244
|
+
$sw$initRemoteTransport: {
|
|
245
|
+
port: channel.port2,
|
|
246
|
+
prefix: prefix.href
|
|
247
|
+
}
|
|
248
|
+
}, [
|
|
249
|
+
channel.port2
|
|
250
|
+
]);
|
|
251
|
+
const cookieJar = new CookieJar();
|
|
252
|
+
cookieJar.load(cookies);
|
|
253
|
+
const context = {
|
|
254
|
+
interface: {
|
|
255
|
+
getInjectScripts: yieldGetInjectScripts(cookieJar, config, sjconfig, prefix),
|
|
256
|
+
codecEncode,
|
|
257
|
+
codecDecode
|
|
258
|
+
},
|
|
259
|
+
prefix,
|
|
260
|
+
cookieJar,
|
|
261
|
+
config: sjconfig
|
|
262
|
+
};
|
|
263
|
+
function createFrameId() {
|
|
264
|
+
return `${Array(8).fill(0).map(()=>Math.floor(Math.random() * 36).toString(36)).join("")}`;
|
|
265
|
+
}
|
|
266
|
+
const frame = globalThis.frameElement;
|
|
267
|
+
if (frame && !frame.name) {
|
|
268
|
+
frame.name = createFrameId();
|
|
269
|
+
}
|
|
270
|
+
client = new ScramjetClient(globalThis, {
|
|
271
|
+
context,
|
|
272
|
+
transport,
|
|
273
|
+
sendSetCookie: async (url, cookie)=>{
|
|
274
|
+
// sw.postMessage({
|
|
275
|
+
// $controller$setCookie: {
|
|
276
|
+
// url,
|
|
277
|
+
// cookie
|
|
278
|
+
// }
|
|
279
|
+
// });
|
|
280
|
+
},
|
|
281
|
+
shouldPassthroughWebsocket: (url)=>{
|
|
282
|
+
return url === "wss://anura.pro/";
|
|
283
|
+
},
|
|
284
|
+
shouldBlockMessageEvent (i) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
client.hook();
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
})();
|
|
293
|
+
|
|
294
|
+
$scramjetControllerInit = __webpack_exports__;
|
|
295
|
+
})()
|
|
296
|
+
;
|
|
2
297
|
//# sourceMappingURL=controller.inject.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.inject.js","sources":["webpack://$scramjetController/./packages/rpc/index.ts","webpack://$scramjetController/webpack/runtime/define_property_getters","webpack://$scramjetController/webpack/runtime/has_own_property","webpack://$scramjetController/webpack/runtime/make_namespace_object","webpack://$scramjetController/./packages/controller/src/inject.ts"],"sourcesContent":["type Serverbound = {\n\tmethod1: [{ paramA: string; paramB: number }, boolean];\n\tmethod2: [string, number];\n};\n\ntype Clientbound = {\n\tmethod1: [number];\n\tmethod2: [boolean, string];\n};\n\nexport type RpcDescription = {\n\t[method: string]: [args: any, returnType: any] | [args: any] | [];\n};\n\nexport type MethodsDefinition<Description extends RpcDescription> = {\n\t[Method in keyof Description]: (\n\t\t...args: Description[Method] extends [infer A, ...any[]] ? [A] : []\n\t) => Description[Method] extends [any, infer R]\n\t\t? Promise<[R, Transferable[]]>\n\t\t: Promise<void>;\n};\n\nexport class RpcHelper<\n\tLocal extends RpcDescription,\n\tRemote extends RpcDescription,\n> {\n\tcounter: number = 0;\n\tpromiseCallbacks: Map<\n\t\tnumber,\n\t\t{ resolve: (value: any) => void; reject: (reason?: any) => void }\n\t> = new Map();\n\tconstructor(\n\t\tprivate methods: MethodsDefinition<Local>,\n\t\tprivate id: string,\n\t\tprivate sendRaw: (data: any, transfer: Transferable[]) => void\n\t) {}\n\n\trecieve(data: any) {\n\t\tif (data === undefined || data === null || typeof data !== \"object\") return;\n\t\tconst dt = data[this.id];\n\t\tif (dt === undefined || dt === null || typeof dt !== \"object\") return;\n\n\t\tconst type = dt.$type;\n\n\t\tif (type === \"response\") {\n\t\t\tconst token = dt.$token;\n\t\t\tconst data = dt.$data;\n\t\t\tconst error = dt.$error;\n\t\t\tconst cb = this.promiseCallbacks.get(token);\n\t\t\tif (!cb) return;\n\t\t\tthis.promiseCallbacks.delete(token);\n\t\t\tif (error !== undefined) {\n\t\t\t\tcb.reject(new Error(error));\n\t\t\t} else {\n\t\t\t\tcb.resolve(data);\n\t\t\t}\n\t\t} else if (type === \"request\") {\n\t\t\tconst method = dt.$method as keyof Local;\n\t\t\tconst args = dt.$args as Local[typeof method][0];\n\t\t\t(this.methods[method] as any)(args)\n\t\t\t\t.then((r: any) => {\n\t\t\t\t\tthis.sendRaw(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t\t\t$type: \"response\",\n\t\t\t\t\t\t\t\t$token: dt.$token,\n\t\t\t\t\t\t\t\t$data: r?.[0],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tr?.[1]\n\t\t\t\t\t);\n\t\t\t\t})\n\t\t\t\t.catch((err: any) => {\n\t\t\t\t\tconsole.error(err);\n\t\t\t\t\tthis.sendRaw(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t\t\t$type: \"response\",\n\t\t\t\t\t\t\t\t$token: dt.$token,\n\t\t\t\t\t\t\t\t$error: err?.toString() || \"Unknown error\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[]\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t}\n\t}\n\n\tcall<Method extends keyof Remote>(\n\t\tmethod: Method,\n\t\targs: Remote[Method][0],\n\t\ttransfer: Transferable[] = []\n\t): Promise<Remote[Method][1]> {\n\t\tlet token = this.counter++;\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tthis.promiseCallbacks.set(token, { resolve, reject });\n\t\t\tthis.sendRaw(\n\t\t\t\t{\n\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t$type: \"request\",\n\t\t\t\t\t\t$method: method,\n\t\t\t\t\t\t$args: args,\n\t\t\t\t\t\t$token: token,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\ttransfer\n\t\t\t);\n\t\t});\n\t}\n}\n","__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","import type { CookieJar, ScramjetConfig } from \"@mercuryworkshop/scramjet\";\nimport type * as ScramjetGlobal from \"@mercuryworkshop/scramjet\";\ndeclare const $scramjet: typeof ScramjetGlobal;\n\nimport type {\n\tRawHeaders,\n\tProxyTransport,\n\tTransferrableResponse,\n} from \"@mercuryworkshop/proxy-transports\";\n\nimport { RpcHelper } from \"@mercuryworkshop/rpc\";\nimport type {\n\tControllerToTransport,\n\tTransportToController,\n\tWebSocketData,\n\tWebSocketMessage,\n} from \"./types\";\n\nconst MessagePort_postMessage = MessagePort.prototype.postMessage;\nconst postMessage = (\n\tport: MessagePort,\n\tdata: any,\n\ttransfer?: Transferable[]\n) => {\n\tMessagePort_postMessage.call(port, data, transfer as any);\n};\n\nclass RemoteTransport implements ProxyTransport {\n\tprivate readyResolve!: () => void;\n\tprivate readyPromise: Promise<void> = new Promise((resolve) => {\n\t\tthis.readyResolve = resolve;\n\t});\n\n\tpublic ready = false;\n\tasync init() {\n\t\tawait this.readyPromise;\n\t\tthis.ready = true;\n\t}\n\n\tprivate rpc: RpcHelper<ControllerToTransport, TransportToController>;\n\tconstructor(public port: MessagePort) {\n\t\tthis.rpc = new RpcHelper<ControllerToTransport, TransportToController>(\n\t\t\t{\n\t\t\t\tready: async () => {\n\t\t\t\t\tthis.readyResolve();\n\t\t\t\t},\n\t\t\t},\n\t\t\t\"transport\",\n\t\t\t(data, transfer) => {\n\t\t\t\tpostMessage(port, data, transfer);\n\t\t\t}\n\t\t);\n\t\tport.onmessageerror = (ev) => {\n\t\t\tconsole.error(\"onmessageerror (this should never happen!)\", ev);\n\t\t};\n\t\tport.onmessage = (ev) => {\n\t\t\tthis.rpc.recieve(ev.data);\n\t\t};\n\t\tport.start();\n\t}\n\tconnect(\n\t\turl: URL,\n\t\tprotocols: string[],\n\t\trequestHeaders: RawHeaders,\n\t\tonopen: (protocol: string, extensions: string) => void,\n\t\tonmessage: (data: Blob | ArrayBuffer | string) => void,\n\t\tonclose: (code: number, reason: string) => void,\n\t\tonerror: (error: string) => void\n\t): [\n\t\t(data: Blob | ArrayBuffer | string) => void,\n\t\t(code: number, reason: string) => void,\n\t] {\n\t\tconst channel = new MessageChannel();\n\t\tlet port = channel.port1;\n\t\tconsole.warn(\"connecting\");\n\t\tthis.rpc\n\t\t\t.call(\n\t\t\t\t\"connect\",\n\t\t\t\t{\n\t\t\t\t\turl: url.href,\n\t\t\t\t\tprotocols,\n\t\t\t\t\trequestHeaders,\n\t\t\t\t\tport: channel.port2,\n\t\t\t\t},\n\t\t\t\t[channel.port2]\n\t\t\t)\n\t\t\t.then((response) => {\n\t\t\t\tconsole.log(response);\n\t\t\t\tif (response.result === \"success\") {\n\t\t\t\t\tonopen(response.protocol, response.extensions);\n\t\t\t\t} else {\n\t\t\t\t\tonerror(response.error);\n\t\t\t\t}\n\t\t\t});\n\t\tport.onmessage = (ev) => {\n\t\t\tconst message = ev.data as WebSocketMessage;\n\t\t\tif (message.type === \"data\") {\n\t\t\t\tonmessage(message.data);\n\t\t\t} else if (message.type === \"close\") {\n\t\t\t\tonclose(message.code, message.reason);\n\t\t\t}\n\t\t};\n\t\tport.onmessageerror = (ev) => {\n\t\t\tconsole.error(\"onmessageerror (this should never happen!)\", ev);\n\t\t\tonerror(\"Message error in transport port\");\n\t\t};\n\n\t\treturn [\n\t\t\t(data) => {\n\t\t\t\tpostMessage(\n\t\t\t\t\tport,\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"data\",\n\t\t\t\t\t\tdata: data,\n\t\t\t\t\t},\n\t\t\t\t\tdata instanceof ArrayBuffer ? [data] : []\n\t\t\t\t);\n\t\t\t},\n\t\t\t(code) => {\n\t\t\t\tpostMessage(port, {\n\t\t\t\t\ttype: \"close\",\n\t\t\t\t\tcode: code,\n\t\t\t\t});\n\t\t\t},\n\t\t];\n\t}\n\n\tasync request(\n\t\tremote: URL,\n\t\tmethod: string,\n\t\tbody: BodyInit | null,\n\t\theaders: RawHeaders,\n\t\tsignal: AbortSignal | undefined\n\t): Promise<TransferrableResponse> {\n\t\treturn await this.rpc.call(\"request\", {\n\t\t\tremote: remote.href,\n\t\t\tmethod,\n\t\t\tbody,\n\t\t\theaders,\n\t\t});\n\t}\n\n\tmeta() {\n\t\treturn {};\n\t}\n}\n\nconst sw = navigator.serviceWorker.controller;\nconst { SCRAMJETCLIENT, ScramjetClient, CookieJar, setWasm } = $scramjet;\n\ntype Config = any;\ntype Init = {\n\tconfig: Config;\n\tsjconfig: ScramjetConfig;\n\tcookies: string;\n\tprefix: URL;\n\tyieldGetInjectScripts: (\n\t\tcookieJar: CookieJar,\n\t\tconfig: Config,\n\t\tsjconfig: ScramjetConfig,\n\t\tprefix: URL\n\t) => any;\n\tcodecEncode: (input: string) => string;\n\tcodecDecode: (input: string) => string;\n};\n\nexport function load({\n\tconfig,\n\tsjconfig,\n\tcookies,\n\tprefix,\n\tyieldGetInjectScripts,\n\tcodecEncode,\n\tcodecDecode,\n}: Init) {\n\tlet client;\n\tif (SCRAMJETCLIENT in globalThis) {\n\t\tclient = globalThis[SCRAMJETCLIENT];\n\t} else {\n\t\tsetWasm(Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)));\n\t\tdelete self.WASM;\n\n\t\tconst channel = new MessageChannel();\n\t\tconst transport = new RemoteTransport(channel.port1);\n\t\tsw?.postMessage(\n\t\t\t{\n\t\t\t\t$sw$initRemoteTransport: {\n\t\t\t\t\tport: channel.port2,\n\t\t\t\t\tprefix: prefix.href,\n\t\t\t\t},\n\t\t\t},\n\t\t\t[channel.port2]\n\t\t);\n\n\t\tconst cookieJar = new CookieJar();\n\t\tcookieJar.load(cookies);\n\n\t\tconst context = {\n\t\t\tinterface: {\n\t\t\t\tgetInjectScripts: yieldGetInjectScripts(\n\t\t\t\t\tcookieJar,\n\t\t\t\t\tconfig,\n\t\t\t\t\tsjconfig,\n\t\t\t\t\tprefix\n\t\t\t\t),\n\t\t\t\tcodecEncode,\n\t\t\t\tcodecDecode,\n\t\t\t},\n\t\t\tprefix,\n\t\t\tcookieJar,\n\t\t\tconfig: sjconfig,\n\t\t};\n\t\tfunction createFrameId() {\n\t\t\treturn `${Array(8)\n\t\t\t\t.fill(0)\n\t\t\t\t.map(() => Math.floor(Math.random() * 36).toString(36))\n\t\t\t\t.join(\"\")}`;\n\t\t}\n\n\t\tconst frame = globalThis.frameElement as HTMLIFrameElement | null;\n\t\tif (frame && !frame.name) {\n\t\t\tframe.name = createFrameId();\n\t\t}\n\n\t\tclient = new ScramjetClient(globalThis, {\n\t\t\tcontext,\n\t\t\ttransport,\n\t\t\tsendSetCookie: async (url, cookie) => {\n\t\t\t\t// sw.postMessage({\n\t\t\t\t// \t$controller$setCookie: {\n\t\t\t\t// \t\turl,\n\t\t\t\t// \t\tcookie\n\t\t\t\t// \t}\n\t\t\t\t// });\n\t\t\t},\n\t\t\tshouldPassthroughWebsocket: (url) => {\n\t\t\t\treturn url === \"wss://anura.pro/\";\n\t\t\t},\n\t\t\tshouldBlockMessageEvent(i) {\n\t\t\t\treturn false;\n\t\t\t},\n\t\t});\n\n\t\tclient.hook();\n\t}\n}\n"],"names":["RpcHelper","Map","methods","id","sendRaw","data","dt","type","token","error","cb","undefined","Error","method","args","r","err","console","transfer","Promise","resolve","reject","e","Object","Symbol","MessagePort_postMessage","MessagePort","postMessage","port","RemoteTransport","ev","url","protocols","requestHeaders","onopen","onmessage","onclose","onerror","channel","MessageChannel","response","message","ArrayBuffer","code","remote","body","headers","signal","sw","navigator","SCRAMJETCLIENT","ScramjetClient","CookieJar","setWasm","$scramjet","load","config","sjconfig","cookies","prefix","yieldGetInjectScripts","codecEncode","codecDecode","globalThis","Uint8Array","atob","self","c","transport","cookieJar","context","frame","Array","Math","client","cookie","i"],"mappings":"yEAsBO,OAAMA,E,kBAIZ,SAAkB,CAAE,AACpB,kBAGI,IAAIC,GAAM,AACd,aACSC,CAAiC,CACjCC,CAAU,CACVC,CAAsD,CAC7D,C,KAHOF,OAAO,CAAPA,E,KACAC,EAAE,CAAFA,E,KACAC,OAAO,CAAPA,CACN,CAEH,QAAQC,CAAS,CAAE,CAClB,GAAIA,MAAAA,GAAuC,AAAgB,UAAhB,OAAOA,EAAmB,OACrE,IAAMC,EAAKD,CAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CACxB,GAAIC,MAAAA,GAAmC,AAAc,UAAd,OAAOA,EAAiB,OAE/D,IAAMC,EAAOD,EAAG,KAAK,CAErB,GAAIC,AAAS,aAATA,EAAqB,CACxB,IAAMC,EAAQF,EAAG,MAAM,CACjBD,EAAOC,EAAG,KAAK,CACfG,EAAQH,EAAG,MAAM,CACjBI,EAAK,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAACF,GACrC,GAAI,CAACE,EAAI,OACT,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAACF,GACzBC,AAAUE,SAAVF,EACHC,EAAG,MAAM,CAAC,AAAIE,MAAMH,IAEpBC,EAAG,OAAO,CAACL,EAEb,MAAO,GAAIE,AAAS,YAATA,EAAoB,CAC9B,IAAMM,EAASP,EAAG,OAAO,CACnBQ,EAAOR,EAAG,KAAK,CACpB,IAAI,CAAC,OAAO,CAACO,EAAO,CAASC,GAC5B,IAAI,CAAC,AAACC,IACN,IAAI,CAAC,OAAO,CACX,CACC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAE,CACV,MAA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AU,CAAC,KAC7D,OAAOD,KAAK,IAAI,CAEhB,IAAM5B,EAAU,IAAIC,eACd6B,EAAY,IAAIvC,EAAgBS,EAAQ,KAAK,EACnDU,GAAI,YACH,CACC,wBAAyB,CACxB,KAAMV,EAAQ,KAAK,CACnB,OAAQqB,EAAO,IAAI,AACpB,CACD,EACA,CAACrB,EAAQ,KAAK,CAAC,EAGhB,IAAM+B,EAAY,IAAIjB,EACtBiB,EAAU,IAAI,CAACX,GAEf,IAAMY,EAAU,CACf,UAAW,CACV,iBAAkBV,EACjBS,EACAb,EACAC,EACAE,GAEDE,YAAAA,EACAC,YAAAA,CACD,EACAH,OAAAA,EACAU,UAAAA,EACA,OAAQZ,CACT,EAQMc,EAAQR,WAAW,YAAY,AACjCQ,CAAAA,GAAS,CAACA,EAAM,IAAI,EACvBA,CAAAA,EAAM,IAAI,CARH,CAAC,EAAEC,MAAM,GACd,IAAI,CAAC,GACL,GAAG,CAAC,IAAMC,KAAK,KAAK,CAACA,AAAgB,GAAhBA,KAAK,MAAM,IAAS,QAAQ,CAAC,KAClD,IAAI,CAAC,IAAI,CAAC,AAKe,EAsB5BC,AAnBS,IAAIvB,EAAeY,WAAY,CACvCO,QAAAA,EACAF,UAAAA,EACA,cAAe,MAAOrC,EAAK4C,KAO3B,EACA,2BAA4B,AAAC5C,GACrBA,AAAQ,qBAARA,EAER,wBAAwB6C,GAChB,EAET,GAEO,IAAI,EACZ,CACD,C"}
|
|
1
|
+
{"version":3,"file":"controller.inject.js","sources":["webpack://$scramjetControllerInit/./packages/scramjet/packages/rpc/index.ts","webpack://$scramjetControllerInit/webpack/runtime/define_property_getters","webpack://$scramjetControllerInit/webpack/runtime/has_own_property","webpack://$scramjetControllerInit/webpack/runtime/make_namespace_object","webpack://$scramjetControllerInit/./packages/scramjet/packages/controller/src/inject.ts"],"sourcesContent":["type Serverbound = {\n\tmethod1: [{ paramA: string; paramB: number }, boolean];\n\tmethod2: [string, number];\n};\n\ntype Clientbound = {\n\tmethod1: [number];\n\tmethod2: [boolean, string];\n};\n\nexport type RpcDescription = {\n\t[method: string]: [args: any, returnType: any] | [args: any] | [];\n};\n\nexport type MethodsDefinition<Description extends RpcDescription> = {\n\t[Method in keyof Description]: (\n\t\t...args: Description[Method] extends [infer A, ...any[]] ? [A] : []\n\t) => Description[Method] extends [any, infer R]\n\t\t? Promise<[R, Transferable[]]>\n\t\t: Promise<void>;\n};\n\nexport class RpcHelper<\n\tLocal extends RpcDescription,\n\tRemote extends RpcDescription,\n> {\n\tcounter: number = 0;\n\tpromiseCallbacks: Map<\n\t\tnumber,\n\t\t{ resolve: (value: any) => void; reject: (reason?: any) => void }\n\t> = new Map();\n\tconstructor(\n\t\tprivate methods: MethodsDefinition<Local>,\n\t\tprivate id: string,\n\t\tprivate sendRaw: (data: any, transfer: Transferable[]) => void\n\t) {}\n\n\trecieve(data: any) {\n\t\tif (data === undefined || data === null || typeof data !== \"object\") return;\n\t\tconst dt = data[this.id];\n\t\tif (dt === undefined || dt === null || typeof dt !== \"object\") return;\n\n\t\tconst type = dt.$type;\n\n\t\tif (type === \"response\") {\n\t\t\tconst token = dt.$token;\n\t\t\tconst data = dt.$data;\n\t\t\tconst error = dt.$error;\n\t\t\tconst cb = this.promiseCallbacks.get(token);\n\t\t\tif (!cb) return;\n\t\t\tthis.promiseCallbacks.delete(token);\n\t\t\tif (error !== undefined) {\n\t\t\t\tcb.reject(new Error(error));\n\t\t\t} else {\n\t\t\t\tcb.resolve(data);\n\t\t\t}\n\t\t} else if (type === \"request\") {\n\t\t\tconst method = dt.$method as keyof Local;\n\t\t\tconst args = dt.$args as Local[typeof method][0];\n\t\t\t(this.methods[method] as any)(args)\n\t\t\t\t.then((r: any) => {\n\t\t\t\t\tthis.sendRaw(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t\t\t$type: \"response\",\n\t\t\t\t\t\t\t\t$token: dt.$token,\n\t\t\t\t\t\t\t\t$data: r?.[0],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tr?.[1]\n\t\t\t\t\t);\n\t\t\t\t})\n\t\t\t\t.catch((err: any) => {\n\t\t\t\t\tconsole.error(err);\n\t\t\t\t\tthis.sendRaw(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t\t\t$type: \"response\",\n\t\t\t\t\t\t\t\t$token: dt.$token,\n\t\t\t\t\t\t\t\t$error: err?.toString() || \"Unknown error\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[]\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t}\n\t}\n\n\tcall<Method extends keyof Remote>(\n\t\tmethod: Method,\n\t\targs: Remote[Method][0],\n\t\ttransfer: Transferable[] = []\n\t): Promise<Remote[Method][1]> {\n\t\tlet token = this.counter++;\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tthis.promiseCallbacks.set(token, { resolve, reject });\n\t\t\tthis.sendRaw(\n\t\t\t\t{\n\t\t\t\t\t[this.id]: {\n\t\t\t\t\t\t$type: \"request\",\n\t\t\t\t\t\t$method: method,\n\t\t\t\t\t\t$args: args,\n\t\t\t\t\t\t$token: token,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\ttransfer\n\t\t\t);\n\t\t});\n\t}\n}\n","__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","import type { CookieJar, ScramjetConfig } from \"@mercuryworkshop/scramjet\";\nimport type * as ScramjetGlobal from \"@mercuryworkshop/scramjet\";\ndeclare const $scramjet: typeof ScramjetGlobal;\n\nimport type {\n\tRawHeaders,\n\tProxyTransport,\n\tTransferrableResponse,\n} from \"@mercuryworkshop/proxy-transports\";\n\nimport { RpcHelper } from \"@mercuryworkshop/rpc\";\nimport type {\n\tControllerToTransport,\n\tTransportToController,\n\tWebSocketData,\n\tWebSocketMessage,\n} from \"./types\";\n\nconst MessagePort_postMessage = MessagePort.prototype.postMessage;\nconst postMessage = (\n\tport: MessagePort,\n\tdata: any,\n\ttransfer?: Transferable[]\n) => {\n\tMessagePort_postMessage.call(port, data, transfer as any);\n};\n\nclass RemoteTransport implements ProxyTransport {\n\tprivate readyResolve!: () => void;\n\tprivate readyPromise: Promise<void> = new Promise((resolve) => {\n\t\tthis.readyResolve = resolve;\n\t});\n\n\tpublic ready = false;\n\tasync init() {\n\t\tawait this.readyPromise;\n\t\tthis.ready = true;\n\t}\n\n\tprivate rpc: RpcHelper<ControllerToTransport, TransportToController>;\n\tconstructor(public port: MessagePort) {\n\t\tthis.rpc = new RpcHelper<ControllerToTransport, TransportToController>(\n\t\t\t{\n\t\t\t\tready: async () => {\n\t\t\t\t\tthis.readyResolve();\n\t\t\t\t},\n\t\t\t},\n\t\t\t\"transport\",\n\t\t\t(data, transfer) => {\n\t\t\t\tpostMessage(port, data, transfer);\n\t\t\t}\n\t\t);\n\t\tport.onmessageerror = (ev) => {\n\t\t\tconsole.error(\"onmessageerror (this should never happen!)\", ev);\n\t\t};\n\t\tport.onmessage = (ev) => {\n\t\t\tthis.rpc.recieve(ev.data);\n\t\t};\n\t\tport.start();\n\t}\n\tconnect(\n\t\turl: URL,\n\t\tprotocols: string[],\n\t\trequestHeaders: RawHeaders,\n\t\tonopen: (protocol: string, extensions: string) => void,\n\t\tonmessage: (data: Blob | ArrayBuffer | string) => void,\n\t\tonclose: (code: number, reason: string) => void,\n\t\tonerror: (error: string) => void\n\t): [\n\t\t(data: Blob | ArrayBuffer | string) => void,\n\t\t(code: number, reason: string) => void,\n\t] {\n\t\tconst channel = new MessageChannel();\n\t\tlet port = channel.port1;\n\t\tconsole.warn(\"connecting\");\n\t\tthis.rpc\n\t\t\t.call(\n\t\t\t\t\"connect\",\n\t\t\t\t{\n\t\t\t\t\turl: url.href,\n\t\t\t\t\tprotocols,\n\t\t\t\t\trequestHeaders,\n\t\t\t\t\tport: channel.port2,\n\t\t\t\t},\n\t\t\t\t[channel.port2]\n\t\t\t)\n\t\t\t.then((response) => {\n\t\t\t\tconsole.log(response);\n\t\t\t\tif (response.result === \"success\") {\n\t\t\t\t\tonopen(response.protocol, response.extensions);\n\t\t\t\t} else {\n\t\t\t\t\tonerror(response.error);\n\t\t\t\t}\n\t\t\t});\n\t\tport.onmessage = (ev) => {\n\t\t\tconst message = ev.data as WebSocketMessage;\n\t\t\tif (message.type === \"data\") {\n\t\t\t\tonmessage(message.data);\n\t\t\t} else if (message.type === \"close\") {\n\t\t\t\tonclose(message.code, message.reason);\n\t\t\t}\n\t\t};\n\t\tport.onmessageerror = (ev) => {\n\t\t\tconsole.error(\"onmessageerror (this should never happen!)\", ev);\n\t\t\tonerror(\"Message error in transport port\");\n\t\t};\n\n\t\treturn [\n\t\t\t(data) => {\n\t\t\t\tpostMessage(\n\t\t\t\t\tport,\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"data\",\n\t\t\t\t\t\tdata: data,\n\t\t\t\t\t},\n\t\t\t\t\tdata instanceof ArrayBuffer ? [data] : []\n\t\t\t\t);\n\t\t\t},\n\t\t\t(code) => {\n\t\t\t\tpostMessage(port, {\n\t\t\t\t\ttype: \"close\",\n\t\t\t\t\tcode: code,\n\t\t\t\t});\n\t\t\t},\n\t\t];\n\t}\n\n\tasync request(\n\t\tremote: URL,\n\t\tmethod: string,\n\t\tbody: BodyInit | null,\n\t\theaders: RawHeaders,\n\t\tsignal: AbortSignal | undefined\n\t): Promise<TransferrableResponse> {\n\t\treturn await this.rpc.call(\"request\", {\n\t\t\tremote: remote.href,\n\t\t\tmethod,\n\t\t\tbody,\n\t\t\theaders,\n\t\t});\n\t}\n\n\tmeta() {\n\t\treturn {};\n\t}\n}\n\nconst sw = navigator.serviceWorker.controller;\nconst { SCRAMJETCLIENT, ScramjetClient, CookieJar, setWasm } = $scramjet;\n\ntype Config = any;\ntype Init = {\n\tconfig: Config;\n\tsjconfig: ScramjetConfig;\n\tcookies: string;\n\tprefix: URL;\n\tyieldGetInjectScripts: (\n\t\tcookieJar: CookieJar,\n\t\tconfig: Config,\n\t\tsjconfig: ScramjetConfig,\n\t\tprefix: URL\n\t) => any;\n\tcodecEncode: (input: string) => string;\n\tcodecDecode: (input: string) => string;\n};\n\nexport function load({\n\tconfig,\n\tsjconfig,\n\tcookies,\n\tprefix,\n\tyieldGetInjectScripts,\n\tcodecEncode,\n\tcodecDecode,\n}: Init) {\n\tlet client;\n\tif (SCRAMJETCLIENT in globalThis) {\n\t\tclient = globalThis[SCRAMJETCLIENT];\n\t} else {\n\t\tsetWasm(Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)));\n\t\tdelete self.WASM;\n\n\t\tconst channel = new MessageChannel();\n\t\tconst transport = new RemoteTransport(channel.port1);\n\t\tsw?.postMessage(\n\t\t\t{\n\t\t\t\t$sw$initRemoteTransport: {\n\t\t\t\t\tport: channel.port2,\n\t\t\t\t\tprefix: prefix.href,\n\t\t\t\t},\n\t\t\t},\n\t\t\t[channel.port2]\n\t\t);\n\n\t\tconst cookieJar = new CookieJar();\n\t\tcookieJar.load(cookies);\n\n\t\tconst context = {\n\t\t\tinterface: {\n\t\t\t\tgetInjectScripts: yieldGetInjectScripts(\n\t\t\t\t\tcookieJar,\n\t\t\t\t\tconfig,\n\t\t\t\t\tsjconfig,\n\t\t\t\t\tprefix\n\t\t\t\t),\n\t\t\t\tcodecEncode,\n\t\t\t\tcodecDecode,\n\t\t\t},\n\t\t\tprefix,\n\t\t\tcookieJar,\n\t\t\tconfig: sjconfig,\n\t\t};\n\t\tfunction createFrameId() {\n\t\t\treturn `${Array(8)\n\t\t\t\t.fill(0)\n\t\t\t\t.map(() => Math.floor(Math.random() * 36).toString(36))\n\t\t\t\t.join(\"\")}`;\n\t\t}\n\n\t\tconst frame = globalThis.frameElement as HTMLIFrameElement | null;\n\t\tif (frame && !frame.name) {\n\t\t\tframe.name = createFrameId();\n\t\t}\n\n\t\tclient = new ScramjetClient(globalThis, {\n\t\t\tcontext,\n\t\t\ttransport,\n\t\t\tsendSetCookie: async (url, cookie) => {\n\t\t\t\t// sw.postMessage({\n\t\t\t\t// \t$controller$setCookie: {\n\t\t\t\t// \t\turl,\n\t\t\t\t// \t\tcookie\n\t\t\t\t// \t}\n\t\t\t\t// });\n\t\t\t},\n\t\t\tshouldPassthroughWebsocket: (url) => {\n\t\t\t\treturn url === \"wss://anura.pro/\";\n\t\t\t},\n\t\t\tshouldBlockMessageEvent(i) {\n\t\t\t\treturn false;\n\t\t\t},\n\t\t});\n\n\t\tclient.hook();\n\t}\n}\n"],"names":["RpcHelper","Map","methods","id","sendRaw","data","undefined","dt","type","token","error","cb","Error","method","args","r","err","console","transfer","Promise","resolve","reject","MessagePort_postMessage","MessagePort","postMessage","port","RemoteTransport","ev","url","protocols","requestHeaders","onopen","onmessage","onclose","onerror","channel","MessageChannel","response","message","ArrayBuffer","code","remote","body","headers","signal","sw","navigator","SCRAMJETCLIENT","ScramjetClient","CookieJar","setWasm","$scramjet","load","config","sjconfig","cookies","prefix","yieldGetInjectScripts","codecEncode","codecDecode","client","globalThis","Uint8Array","atob","self","c","transport","cookieJar","context","createFrameId","Array","Math","frame","cookie","i"],"mappings":";;;;;;;;AAsBO,MAAMA;;;;IAIZ,UAAkB,EAAE;IACpB,mBAGI,IAAIC,MAAM;IACd,YACSC,OAAiC,EACjCC,EAAU,EACVC,OAAsD,CAC7D;aAHOF,UAAAA;aACAC,KAAAA;aACAC,UAAAA;IACN;IAEH,QAAQC,IAAS,EAAE;QAClB,IAAIA,SAASC,aAAaD,SAAS,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C5B,SAAS;YACT,WAAW;YACX,KAAK;YACL,MAAM;YACP;YACA,4BAA4B,CAAC7C;gBAC5B,OAAOA,QAAQ;YAChB;YACA,yBAAwB8C,CAAC;gBACxB,OAAO;YACR;QACD;QAEAd,OAAO,IAAI;IACZ;AACD"}
|
package/dist/controller.sw.js
CHANGED
|
@@ -1,2 +1,265 @@
|
|
|
1
|
-
var $
|
|
1
|
+
var $scramjetControllerInit;
|
|
2
|
+
(() => {
|
|
3
|
+
var __webpack_modules__ = ({
|
|
4
|
+
"./packages/scramjet/packages/rpc/index.ts"(__unused_rspack_module, __webpack_exports__, __webpack_require__) {
|
|
5
|
+
__webpack_require__.r(__webpack_exports__);
|
|
6
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
7
|
+
RpcHelper: () => (RpcHelper)
|
|
8
|
+
});
|
|
9
|
+
class RpcHelper {
|
|
10
|
+
methods;
|
|
11
|
+
id;
|
|
12
|
+
sendRaw;
|
|
13
|
+
counter = 0;
|
|
14
|
+
promiseCallbacks = new Map();
|
|
15
|
+
constructor(methods, id, sendRaw){
|
|
16
|
+
this.methods = methods;
|
|
17
|
+
this.id = id;
|
|
18
|
+
this.sendRaw = sendRaw;
|
|
19
|
+
}
|
|
20
|
+
recieve(data) {
|
|
21
|
+
if (data === undefined || data === null || typeof data !== "object") return;
|
|
22
|
+
const dt = data[this.id];
|
|
23
|
+
if (dt === undefined || dt === null || typeof dt !== "object") return;
|
|
24
|
+
const type = dt.$type;
|
|
25
|
+
if (type === "response") {
|
|
26
|
+
const token = dt.$token;
|
|
27
|
+
const data = dt.$data;
|
|
28
|
+
const error = dt.$error;
|
|
29
|
+
const cb = this.promiseCallbacks.get(token);
|
|
30
|
+
if (!cb) return;
|
|
31
|
+
this.promiseCallbacks.delete(token);
|
|
32
|
+
if (error !== undefined) {
|
|
33
|
+
cb.reject(new Error(error));
|
|
34
|
+
} else {
|
|
35
|
+
cb.resolve(data);
|
|
36
|
+
}
|
|
37
|
+
} else if (type === "request") {
|
|
38
|
+
const method = dt.$method;
|
|
39
|
+
const args = dt.$args;
|
|
40
|
+
this.methods[method](args).then((r)=>{
|
|
41
|
+
this.sendRaw({
|
|
42
|
+
[this.id]: {
|
|
43
|
+
$type: "response",
|
|
44
|
+
$token: dt.$token,
|
|
45
|
+
$data: r?.[0]
|
|
46
|
+
}
|
|
47
|
+
}, r?.[1]);
|
|
48
|
+
}).catch((err)=>{
|
|
49
|
+
console.error(err);
|
|
50
|
+
this.sendRaw({
|
|
51
|
+
[this.id]: {
|
|
52
|
+
$type: "response",
|
|
53
|
+
$token: dt.$token,
|
|
54
|
+
$error: err?.toString() || "Unknown error"
|
|
55
|
+
}
|
|
56
|
+
}, []);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
call(method, args, transfer = []) {
|
|
61
|
+
let token = this.counter++;
|
|
62
|
+
return new Promise((resolve, reject)=>{
|
|
63
|
+
this.promiseCallbacks.set(token, {
|
|
64
|
+
resolve,
|
|
65
|
+
reject
|
|
66
|
+
});
|
|
67
|
+
this.sendRaw({
|
|
68
|
+
[this.id]: {
|
|
69
|
+
$type: "request",
|
|
70
|
+
$method: method,
|
|
71
|
+
$args: args,
|
|
72
|
+
$token: token
|
|
73
|
+
}
|
|
74
|
+
}, transfer);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
});
|
|
83
|
+
// The module cache
|
|
84
|
+
var __webpack_module_cache__ = {};
|
|
85
|
+
|
|
86
|
+
// The require function
|
|
87
|
+
function __webpack_require__(moduleId) {
|
|
88
|
+
|
|
89
|
+
// Check if module is in cache
|
|
90
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
91
|
+
if (cachedModule !== undefined) {
|
|
92
|
+
return cachedModule.exports;
|
|
93
|
+
}
|
|
94
|
+
// Create a new module (and put it into the cache)
|
|
95
|
+
var module = (__webpack_module_cache__[moduleId] = {
|
|
96
|
+
exports: {}
|
|
97
|
+
});
|
|
98
|
+
// Execute the module function
|
|
99
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
100
|
+
|
|
101
|
+
// Return the exports of the module
|
|
102
|
+
return module.exports;
|
|
103
|
+
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// webpack/runtime/define_property_getters
|
|
107
|
+
(() => {
|
|
108
|
+
__webpack_require__.d = (exports, definition) => {
|
|
109
|
+
for(var key in definition) {
|
|
110
|
+
if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
111
|
+
Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
})();
|
|
116
|
+
// webpack/runtime/has_own_property
|
|
117
|
+
(() => {
|
|
118
|
+
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
119
|
+
})();
|
|
120
|
+
// webpack/runtime/make_namespace_object
|
|
121
|
+
(() => {
|
|
122
|
+
// define __esModule on exports
|
|
123
|
+
__webpack_require__.r = (exports) => {
|
|
124
|
+
if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
125
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
126
|
+
}
|
|
127
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
128
|
+
};
|
|
129
|
+
})();
|
|
130
|
+
var __webpack_exports__ = {};
|
|
131
|
+
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
|
|
132
|
+
(() => {
|
|
133
|
+
__webpack_require__.r(__webpack_exports__);
|
|
134
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
135
|
+
route: () => (route),
|
|
136
|
+
shouldRoute: () => (shouldRoute)
|
|
137
|
+
});
|
|
138
|
+
/* import */ var _mercuryworkshop_rpc__rspack_import_0 = __webpack_require__("./packages/scramjet/packages/rpc/index.ts");
|
|
139
|
+
|
|
140
|
+
function makeId() {
|
|
141
|
+
return Math.random().toString(36).substring(2, 10);
|
|
142
|
+
}
|
|
143
|
+
let cookieResolvers = {};
|
|
144
|
+
addEventListener("message", (e)=>{
|
|
145
|
+
if (!e.data) return;
|
|
146
|
+
if (typeof e.data != "object") return;
|
|
147
|
+
if (e.data.$sw$setCookieDone && typeof e.data.$sw$setCookieDone == "object") {
|
|
148
|
+
const done = e.data.$sw$setCookieDone;
|
|
149
|
+
const resolver = cookieResolvers[done.id];
|
|
150
|
+
if (resolver) {
|
|
151
|
+
resolver();
|
|
152
|
+
delete cookieResolvers[done.id];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (e.data.$sw$initRemoteTransport && typeof e.data.$sw$initRemoteTransport == "object") {
|
|
156
|
+
const { port, prefix } = e.data.$sw$initRemoteTransport;
|
|
157
|
+
console.error(prefix, tabs);
|
|
158
|
+
const relevantcontroller = tabs.find((tab)=>new URL(prefix).pathname.startsWith(tab.prefix));
|
|
159
|
+
if (!relevantcontroller) {
|
|
160
|
+
console.error("No relevant controller found for transport init");
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
relevantcontroller.rpc.call("initRemoteTransport", port, [
|
|
164
|
+
port
|
|
165
|
+
]);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
class ControllerReference {
|
|
169
|
+
prefix;
|
|
170
|
+
id;
|
|
171
|
+
rpc;
|
|
172
|
+
constructor(prefix, id, port){
|
|
173
|
+
this.prefix = prefix;
|
|
174
|
+
this.id = id;
|
|
175
|
+
this.rpc = new _mercuryworkshop_rpc__rspack_import_0.RpcHelper({
|
|
176
|
+
sendSetCookie: async ({ url, cookie })=>{
|
|
177
|
+
let clients1 = await self.clients.matchAll();
|
|
178
|
+
let promises = [];
|
|
179
|
+
for (const client of clients1){
|
|
180
|
+
let id = makeId();
|
|
181
|
+
client.postMessage({
|
|
182
|
+
$controller$setCookie: {
|
|
183
|
+
url,
|
|
184
|
+
cookie,
|
|
185
|
+
id
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
promises.push(new Promise((resolve)=>{
|
|
189
|
+
cookieResolvers[id] = resolve;
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
192
|
+
await Promise.race([
|
|
193
|
+
new Promise((resolve)=>setTimeout(()=>{
|
|
194
|
+
console.error("timed out waiting for set cookie response (deadlock?)");
|
|
195
|
+
resolve();
|
|
196
|
+
}, 1000)),
|
|
197
|
+
promises
|
|
198
|
+
]);
|
|
199
|
+
}
|
|
200
|
+
}, "tabchannel-" + id, (data, transfer)=>{
|
|
201
|
+
port.postMessage(data, transfer);
|
|
202
|
+
});
|
|
203
|
+
port.addEventListener("message", (e)=>{
|
|
204
|
+
this.rpc.recieve(e.data);
|
|
205
|
+
});
|
|
206
|
+
port.onmessageerror = console.error;
|
|
207
|
+
this.rpc.call("ready", null);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const tabs = [];
|
|
211
|
+
addEventListener("message", (e)=>{
|
|
212
|
+
if (!e.data) return;
|
|
213
|
+
if (typeof e.data != "object") return;
|
|
214
|
+
if (!e.data.$controller$init) return;
|
|
215
|
+
if (typeof e.data.$controller$init != "object") return;
|
|
216
|
+
const init = e.data.$controller$init;
|
|
217
|
+
tabs.push(new ControllerReference(init.prefix, init.id, e.ports[0]));
|
|
218
|
+
});
|
|
219
|
+
function shouldRoute(event) {
|
|
220
|
+
const url = new URL(event.request.url);
|
|
221
|
+
const tab = tabs.find((tab)=>url.pathname.startsWith(tab.prefix));
|
|
222
|
+
return tab !== undefined;
|
|
223
|
+
}
|
|
224
|
+
async function route(event) {
|
|
225
|
+
try {
|
|
226
|
+
const url = new URL(event.request.url);
|
|
227
|
+
const tab = tabs.find((tab)=>url.pathname.startsWith(tab.prefix));
|
|
228
|
+
const client = await clients.get(event.clientId);
|
|
229
|
+
const rawheaders = [
|
|
230
|
+
...event.request.headers
|
|
231
|
+
];
|
|
232
|
+
const response = await tab.rpc.call("request", {
|
|
233
|
+
rawUrl: event.request.url,
|
|
234
|
+
destination: event.request.destination,
|
|
235
|
+
mode: event.request.mode,
|
|
236
|
+
referrer: event.request.referrer,
|
|
237
|
+
method: event.request.method,
|
|
238
|
+
body: event.request.body,
|
|
239
|
+
cache: event.request.cache,
|
|
240
|
+
forceCrossOriginIsolated: false,
|
|
241
|
+
initialHeaders: rawheaders,
|
|
242
|
+
rawClientUrl: client ? client.url : undefined
|
|
243
|
+
}, event.request.body instanceof ReadableStream || // @ts-expect-error the types for fetchevent are messed up
|
|
244
|
+
event.request.body instanceof ArrayBuffer ? [
|
|
245
|
+
event.request.body
|
|
246
|
+
] : undefined);
|
|
247
|
+
return new Response(response.body, {
|
|
248
|
+
status: response.status,
|
|
249
|
+
statusText: response.statusText,
|
|
250
|
+
headers: response.headers
|
|
251
|
+
});
|
|
252
|
+
} catch (e) {
|
|
253
|
+
console.error("Service Worker error:", e);
|
|
254
|
+
return new Response("Internal Service Worker Error: " + e.message, {
|
|
255
|
+
status: 500
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
})();
|
|
261
|
+
|
|
262
|
+
$scramjetControllerInit = __webpack_exports__;
|
|
263
|
+
})()
|
|
264
|
+
;
|
|
2
265
|
//# sourceMappingURL=controller.sw.js.map
|