@pi-oxide/extension-js 0.4.0 → 0.4.1
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/package.json +1 -1
- package/worker.js +273 -226
package/package.json
CHANGED
package/worker.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
const R = {
|
|
1
|
+
import x, { setLogLevel as C, ExtensionSession as ee, registerJsCallBatch as re } from "./extension_js.js";
|
|
2
|
+
const T = {
|
|
4
3
|
trace: 0,
|
|
5
4
|
debug: 1,
|
|
6
5
|
info: 2,
|
|
7
6
|
warn: 3,
|
|
8
7
|
error: 4,
|
|
9
8
|
none: 5
|
|
10
|
-
},
|
|
9
|
+
}, te = [
|
|
11
10
|
"trace",
|
|
12
11
|
"debug",
|
|
13
12
|
"info",
|
|
@@ -15,22 +14,22 @@ const R = {
|
|
|
15
14
|
"error",
|
|
16
15
|
"none"
|
|
17
16
|
];
|
|
18
|
-
function
|
|
17
|
+
function se(r) {
|
|
19
18
|
const e = Math.max(0, Math.min(5, Math.round(r)));
|
|
20
|
-
return
|
|
19
|
+
return te[e] ?? "error";
|
|
21
20
|
}
|
|
22
|
-
let
|
|
23
|
-
function
|
|
24
|
-
|
|
21
|
+
let P = "trace", L = null;
|
|
22
|
+
function ne(r) {
|
|
23
|
+
P = r, L && L(T[r]);
|
|
25
24
|
}
|
|
26
|
-
function
|
|
27
|
-
L = r, r(
|
|
25
|
+
function ae(r) {
|
|
26
|
+
L = r, r(T[P]);
|
|
28
27
|
}
|
|
29
|
-
function
|
|
30
|
-
return
|
|
28
|
+
function oe(r) {
|
|
29
|
+
return T[r] >= T[P];
|
|
31
30
|
}
|
|
32
|
-
function
|
|
33
|
-
var
|
|
31
|
+
function V(r, e = "info") {
|
|
32
|
+
var s;
|
|
34
33
|
if (r === null) return "null";
|
|
35
34
|
if (r === void 0) return "undefined";
|
|
36
35
|
if (typeof r == "string") return r;
|
|
@@ -38,7 +37,7 @@ function H(r, e = "info") {
|
|
|
38
37
|
return String(r);
|
|
39
38
|
if (typeof r == "bigint") return `${r}n`;
|
|
40
39
|
if (r instanceof Error) {
|
|
41
|
-
const t = e === "debug" || e === "trace" ? r.stack : (
|
|
40
|
+
const t = e === "debug" || e === "trace" ? r.stack : (s = r.stack) == null ? void 0 : s.split(`
|
|
42
41
|
`)[0];
|
|
43
42
|
return JSON.stringify({ message: r.message, name: r.name, stack: t });
|
|
44
43
|
}
|
|
@@ -52,34 +51,34 @@ function H(r, e = "info") {
|
|
|
52
51
|
}
|
|
53
52
|
return String(r);
|
|
54
53
|
}
|
|
55
|
-
function
|
|
54
|
+
function ie(r) {
|
|
56
55
|
return `[extension-js][${r}]`;
|
|
57
56
|
}
|
|
58
|
-
function
|
|
57
|
+
function le(r, e) {
|
|
59
58
|
if (!r) return "";
|
|
60
|
-
const
|
|
59
|
+
const s = [];
|
|
61
60
|
try {
|
|
62
|
-
for (const [t,
|
|
63
|
-
|
|
61
|
+
for (const [t, n] of Object.entries(r))
|
|
62
|
+
s.push(`${t}=${V(n, e)}`);
|
|
64
63
|
} catch {
|
|
65
64
|
return " metadata=[unreadable]";
|
|
66
65
|
}
|
|
67
|
-
return
|
|
66
|
+
return s.length > 0 ? ` ${s.join(" ")}` : "";
|
|
68
67
|
}
|
|
69
|
-
function
|
|
68
|
+
function E(r, e) {
|
|
70
69
|
return e.length === 0 ? { event: r } : e.length === 1 && typeof e[0] == "object" && e[0] !== null && !Array.isArray(e[0]) ? { event: r, metadata: e[0] } : {
|
|
71
70
|
event: r,
|
|
72
|
-
metadata: { _args: e.map((
|
|
71
|
+
metadata: { _args: e.map((s) => V(s)).join(" ") }
|
|
73
72
|
};
|
|
74
73
|
}
|
|
75
|
-
class
|
|
74
|
+
class $ {
|
|
76
75
|
constructor(e = "root") {
|
|
77
76
|
this.namespace = e;
|
|
78
77
|
}
|
|
79
|
-
log(e,
|
|
78
|
+
log(e, s, t) {
|
|
80
79
|
try {
|
|
81
|
-
if (!
|
|
82
|
-
const
|
|
80
|
+
if (!oe(e)) return;
|
|
81
|
+
const n = ie(this.namespace), o = le(t, e), i = `${n} ${s}${o}`;
|
|
83
82
|
switch (e) {
|
|
84
83
|
case "trace":
|
|
85
84
|
case "debug":
|
|
@@ -102,45 +101,45 @@ class P {
|
|
|
102
101
|
} catch {
|
|
103
102
|
}
|
|
104
103
|
}
|
|
105
|
-
trace(e, ...
|
|
106
|
-
const { event: t, metadata:
|
|
107
|
-
this.log("trace", t,
|
|
104
|
+
trace(e, ...s) {
|
|
105
|
+
const { event: t, metadata: n } = E(e, s);
|
|
106
|
+
this.log("trace", t, n);
|
|
108
107
|
}
|
|
109
|
-
debug(e, ...
|
|
110
|
-
const { event: t, metadata:
|
|
111
|
-
this.log("debug", t,
|
|
108
|
+
debug(e, ...s) {
|
|
109
|
+
const { event: t, metadata: n } = E(e, s);
|
|
110
|
+
this.log("debug", t, n);
|
|
112
111
|
}
|
|
113
|
-
info(e, ...
|
|
114
|
-
const { event: t, metadata:
|
|
115
|
-
this.log("info", t,
|
|
112
|
+
info(e, ...s) {
|
|
113
|
+
const { event: t, metadata: n } = E(e, s);
|
|
114
|
+
this.log("info", t, n);
|
|
116
115
|
}
|
|
117
|
-
warn(e, ...
|
|
118
|
-
const { event: t, metadata:
|
|
119
|
-
this.log("warn", t,
|
|
116
|
+
warn(e, ...s) {
|
|
117
|
+
const { event: t, metadata: n } = E(e, s);
|
|
118
|
+
this.log("warn", t, n);
|
|
120
119
|
}
|
|
121
|
-
error(e, ...
|
|
122
|
-
const { event: t, metadata:
|
|
123
|
-
this.log("error", t,
|
|
120
|
+
error(e, ...s) {
|
|
121
|
+
const { event: t, metadata: n } = E(e, s);
|
|
122
|
+
this.log("error", t, n);
|
|
124
123
|
}
|
|
125
124
|
child(e) {
|
|
126
|
-
return new
|
|
125
|
+
return new $(`${this.namespace}.${e}`);
|
|
127
126
|
}
|
|
128
|
-
timer(e,
|
|
129
|
-
const
|
|
127
|
+
timer(e, s, t = "info") {
|
|
128
|
+
const n = typeof performance < "u" && performance.now, o = n ? performance.now() : Date.now();
|
|
130
129
|
return (i) => {
|
|
131
130
|
try {
|
|
132
|
-
const a =
|
|
133
|
-
...
|
|
131
|
+
const a = n ? performance.now() : Date.now(), g = Math.round(a - o), u = {
|
|
132
|
+
...s,
|
|
134
133
|
...i,
|
|
135
134
|
duration_ms: g
|
|
136
135
|
};
|
|
137
|
-
this.log(t, e,
|
|
136
|
+
this.log(t, e, u);
|
|
138
137
|
} catch {
|
|
139
138
|
}
|
|
140
139
|
};
|
|
141
140
|
}
|
|
142
141
|
}
|
|
143
|
-
const l = new
|
|
142
|
+
const l = new $("root"), ce = /* @__PURE__ */ new Set([
|
|
144
143
|
"page_url",
|
|
145
144
|
"page_title",
|
|
146
145
|
"page_click",
|
|
@@ -169,38 +168,41 @@ const l = new P("root"), ae = /* @__PURE__ */ new Set([
|
|
|
169
168
|
"tab_dblclick",
|
|
170
169
|
"tab_back"
|
|
171
170
|
]);
|
|
172
|
-
function
|
|
173
|
-
return
|
|
171
|
+
function ue(r) {
|
|
172
|
+
return ce.has(r);
|
|
174
173
|
}
|
|
175
|
-
const
|
|
176
|
-
function
|
|
177
|
-
|
|
174
|
+
const J = /* @__PURE__ */ new Map();
|
|
175
|
+
function de(r, e) {
|
|
176
|
+
J.set(r, e);
|
|
178
177
|
}
|
|
179
|
-
function
|
|
178
|
+
function fe(r) {
|
|
179
|
+
return J.get(r);
|
|
180
|
+
}
|
|
181
|
+
function ge(r) {
|
|
180
182
|
return r.startsWith("tab_") ? "required" : "active";
|
|
181
183
|
}
|
|
182
|
-
function
|
|
184
|
+
function pe(r) {
|
|
183
185
|
return r === "content-script" ? "content-script" : r === "main-thread" ? "main-thread" : r === "worker" ? "worker:default" : r.startsWith("worker:") ? r : "main-thread";
|
|
184
186
|
}
|
|
185
|
-
function
|
|
186
|
-
return e !== "main-thread" ? e :
|
|
187
|
+
function me(r, e) {
|
|
188
|
+
return e !== "main-thread" ? e : ue(r) ? "content-script" : e;
|
|
187
189
|
}
|
|
188
|
-
function
|
|
190
|
+
function ye(r, e) {
|
|
189
191
|
return {
|
|
190
|
-
endpoint:
|
|
191
|
-
tabPolicy:
|
|
192
|
+
endpoint: pe(me(r, e)),
|
|
193
|
+
tabPolicy: ge(r)
|
|
192
194
|
};
|
|
193
195
|
}
|
|
194
|
-
function
|
|
196
|
+
function _e(r) {
|
|
195
197
|
for (const e of r)
|
|
196
|
-
|
|
198
|
+
de(e.action, ye(e.action, e.owner));
|
|
197
199
|
}
|
|
198
|
-
function
|
|
200
|
+
function k(r) {
|
|
199
201
|
return r == null ? {} : r instanceof Map ? Object.fromEntries(
|
|
200
|
-
[...r.entries()].map(([e,
|
|
201
|
-
) : Array.isArray(r) ? r.map(
|
|
202
|
+
[...r.entries()].map(([e, s]) => [e, k(s)])
|
|
203
|
+
) : Array.isArray(r) ? r.map(k) : r;
|
|
202
204
|
}
|
|
203
|
-
function
|
|
205
|
+
function be(r) {
|
|
204
206
|
return {
|
|
205
207
|
action: r.action,
|
|
206
208
|
namespace: r.namespace,
|
|
@@ -230,30 +232,30 @@ function me(r) {
|
|
|
230
232
|
};
|
|
231
233
|
}
|
|
232
234
|
l.child("tool-registry");
|
|
233
|
-
let
|
|
234
|
-
const
|
|
235
|
+
let _ = null, v = !1;
|
|
236
|
+
const b = /* @__PURE__ */ new Map();
|
|
235
237
|
function A(r, e) {
|
|
236
238
|
try {
|
|
237
239
|
return r.postMessage(e), !0;
|
|
238
|
-
} catch (
|
|
239
|
-
const t =
|
|
240
|
+
} catch (s) {
|
|
241
|
+
const t = s instanceof Error ? s.message : String(s);
|
|
240
242
|
return l.error("port_post_failed", { error: t }), !1;
|
|
241
243
|
}
|
|
242
244
|
}
|
|
243
|
-
const
|
|
244
|
-
function
|
|
245
|
-
e === "main-thread" || e === "content-script" ? A(
|
|
245
|
+
const G = 1e3;
|
|
246
|
+
function K(r, e, s) {
|
|
247
|
+
e === "main-thread" || e === "content-script" ? A(s, { type: "relayCancel", id: r, owner: e }) : A(s, { type: "registryCallCancel", id: r });
|
|
246
248
|
}
|
|
247
|
-
function
|
|
248
|
-
for (const [
|
|
249
|
-
clearTimeout(t.timeoutId),
|
|
249
|
+
function z(r, e) {
|
|
250
|
+
for (const [s, t] of b)
|
|
251
|
+
clearTimeout(t.timeoutId), K(s, t.owner, t.port), t.settle({
|
|
250
252
|
ok: !1,
|
|
251
253
|
error: { message: e, code: r }
|
|
252
|
-
}),
|
|
254
|
+
}), b.delete(s);
|
|
253
255
|
}
|
|
254
|
-
const
|
|
255
|
-
let q = Promise.resolve(),
|
|
256
|
-
function
|
|
256
|
+
const h = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map();
|
|
257
|
+
let q = Promise.resolve(), f = null;
|
|
258
|
+
function he(r) {
|
|
257
259
|
l.trace("sessionQueue_enqueue");
|
|
258
260
|
const e = q.then(r);
|
|
259
261
|
return q = e.then(
|
|
@@ -264,160 +266,160 @@ function ye(r) {
|
|
|
264
266
|
), e;
|
|
265
267
|
}
|
|
266
268
|
function Y(r) {
|
|
267
|
-
|
|
268
|
-
runId:
|
|
269
|
-
callId:
|
|
269
|
+
f && (l.error("runCell_worker_failure", {
|
|
270
|
+
runId: f.runId,
|
|
271
|
+
callId: f.id,
|
|
270
272
|
error: r
|
|
271
273
|
}), self.postMessage({
|
|
272
274
|
type: "error",
|
|
273
|
-
id:
|
|
275
|
+
id: f.id,
|
|
274
276
|
error: r,
|
|
275
|
-
runId:
|
|
276
|
-
}),
|
|
277
|
+
runId: f.runId
|
|
278
|
+
}), f = null);
|
|
277
279
|
}
|
|
278
280
|
self.addEventListener("error", (r) => {
|
|
279
281
|
const e = r.message || (r.error instanceof Error ? r.error.message : "Worker uncaught error");
|
|
280
282
|
l.error("worker_uncaught_error", { error: e }), Y(e);
|
|
281
283
|
});
|
|
282
284
|
self.addEventListener("unhandledrejection", (r) => {
|
|
283
|
-
const e = r.reason,
|
|
284
|
-
l.error("worker_unhandled_rejection", { error:
|
|
285
|
+
const e = r.reason, s = e instanceof Error ? e.message : String(e ?? "Unhandled rejection");
|
|
286
|
+
l.error("worker_unhandled_rejection", { error: s }), Y(s);
|
|
285
287
|
});
|
|
286
288
|
function c(r, e) {
|
|
287
|
-
|
|
289
|
+
h.set(r, e);
|
|
288
290
|
}
|
|
289
|
-
function
|
|
291
|
+
function Ee() {
|
|
290
292
|
const r = new Uint8Array(16);
|
|
291
293
|
return crypto.getRandomValues(r), Array.from(r, (e) => e.toString(16).padStart(2, "0")).join("");
|
|
292
294
|
}
|
|
293
|
-
function
|
|
294
|
-
return
|
|
295
|
+
function we(r) {
|
|
296
|
+
return se(r);
|
|
295
297
|
}
|
|
296
|
-
const O = /* @__PURE__ */ new Map(),
|
|
297
|
-
function
|
|
298
|
-
var
|
|
298
|
+
const O = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map();
|
|
299
|
+
function ke(r, e) {
|
|
300
|
+
var s;
|
|
299
301
|
if (O.has(r))
|
|
300
302
|
throw new Error(`Worker port already registered for owner: ${r}`);
|
|
301
303
|
if (typeof e.addEventListener != "function")
|
|
302
304
|
throw new Error(`Worker port for owner "${r}" cannot receive responses`);
|
|
303
305
|
O.set(r, e), e.addEventListener("message", async (t) => {
|
|
304
306
|
var o;
|
|
305
|
-
const
|
|
306
|
-
if (
|
|
307
|
-
N(
|
|
307
|
+
const n = t.data;
|
|
308
|
+
if (n !== null && (n.type === "asyncRelayResult" || n.type === "registryCallResult") && typeof n.id == "string") {
|
|
309
|
+
N(n.id, n.result);
|
|
308
310
|
return;
|
|
309
311
|
}
|
|
310
|
-
if (
|
|
311
|
-
(o =
|
|
312
|
+
if (n !== null && n.type === "registryCallCancel" && typeof n.id == "string") {
|
|
313
|
+
(o = I.get(n.id)) == null || o.abort(), I.delete(n.id);
|
|
312
314
|
return;
|
|
313
315
|
}
|
|
314
|
-
if (
|
|
315
|
-
const i =
|
|
316
|
-
|
|
317
|
-
const g =
|
|
318
|
-
let
|
|
316
|
+
if (n !== null && n.type === "registryCall" && typeof n.id == "string" && typeof n.action == "string") {
|
|
317
|
+
const i = n.id, a = new AbortController();
|
|
318
|
+
I.set(i, a);
|
|
319
|
+
const g = h.get(n.action);
|
|
320
|
+
let u;
|
|
319
321
|
if (!g)
|
|
320
|
-
|
|
322
|
+
u = {
|
|
321
323
|
ok: !1,
|
|
322
|
-
error: { message: `Unknown worker action: ${
|
|
324
|
+
error: { message: `Unknown worker action: ${n.action}`, code: "E_UNKNOWN" }
|
|
323
325
|
};
|
|
324
326
|
else
|
|
325
327
|
try {
|
|
326
|
-
const
|
|
327
|
-
action:
|
|
328
|
-
callId:
|
|
329
|
-
runId:
|
|
328
|
+
const d = await g(n.params, {
|
|
329
|
+
action: n.action,
|
|
330
|
+
callId: n.callId,
|
|
331
|
+
runId: n.runId,
|
|
330
332
|
signal: a.signal
|
|
331
333
|
});
|
|
332
|
-
|
|
333
|
-
} catch (
|
|
334
|
-
|
|
334
|
+
u = a.signal.aborted ? { ok: !1, error: { message: "Relay aborted", code: "E_ABORT" } } : { ok: !0, value: d };
|
|
335
|
+
} catch (d) {
|
|
336
|
+
u = {
|
|
335
337
|
ok: !1,
|
|
336
338
|
error: {
|
|
337
|
-
message:
|
|
339
|
+
message: d instanceof Error ? d.message : String(d),
|
|
338
340
|
code: a.signal.aborted ? "E_ABORT" : "E_WORKER_HANDLER"
|
|
339
341
|
}
|
|
340
342
|
};
|
|
341
343
|
}
|
|
342
|
-
|
|
344
|
+
I.delete(i), A(e, { type: "registryCallResult", id: i, result: u }) || N(i, {
|
|
343
345
|
ok: !1,
|
|
344
346
|
error: { message: "Failed to deliver worker handler response", code: "E_PORT" }
|
|
345
347
|
});
|
|
346
348
|
}
|
|
347
|
-
}), (
|
|
349
|
+
}), (s = e.start) == null || s.call(e);
|
|
348
350
|
}
|
|
349
351
|
function N(r, e) {
|
|
350
352
|
l.trace("resolveAsyncRelayResult", { id: r });
|
|
351
|
-
const
|
|
352
|
-
return
|
|
353
|
+
const s = b.get(r);
|
|
354
|
+
return s ? (s.settle(e), !0) : (l.warn("asyncRelayResult_no_pending_relay", { id: r }), !1);
|
|
353
355
|
}
|
|
354
|
-
function
|
|
356
|
+
function Re(r) {
|
|
355
357
|
if (r === "main-thread" || r === "content-script")
|
|
356
358
|
return self;
|
|
357
359
|
const e = O.get(r);
|
|
358
360
|
return e || null;
|
|
359
361
|
}
|
|
360
|
-
function
|
|
361
|
-
const { owner: e, action:
|
|
362
|
+
function Q(r) {
|
|
363
|
+
const { owner: e, action: s, tabPolicy: t, resolveTimeoutMs: n } = r, o = r.timeoutMs ?? w;
|
|
362
364
|
return (i, a) => {
|
|
363
|
-
l.trace("safePostAsCall_invoke", { owner: e, action:
|
|
364
|
-
const g = (
|
|
365
|
-
return new Promise((
|
|
365
|
+
l.trace("safePostAsCall_invoke", { owner: e, action: s, callId: a == null ? void 0 : a.callId, runId: a == null ? void 0 : a.runId });
|
|
366
|
+
const g = (n == null ? void 0 : n(i)) ?? o;
|
|
367
|
+
return new Promise((u, d) => {
|
|
366
368
|
var j;
|
|
367
369
|
if ((j = a == null ? void 0 : a.signal) != null && j.aborted) {
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
+
const m = new Error(`Relay aborted for action: ${s}`);
|
|
371
|
+
m.code = "E_ABORT", d(m);
|
|
370
372
|
return;
|
|
371
373
|
}
|
|
372
|
-
const
|
|
373
|
-
if (!
|
|
374
|
-
|
|
374
|
+
const y = Re(e);
|
|
375
|
+
if (!y) {
|
|
376
|
+
d(new Error(`No port available for action: ${s}`));
|
|
375
377
|
return;
|
|
376
378
|
}
|
|
377
|
-
if (
|
|
378
|
-
|
|
379
|
-
`Too many pending calls (${
|
|
379
|
+
if (b.size >= G) {
|
|
380
|
+
d(new Error(
|
|
381
|
+
`Too many pending calls (${G} limit exceeded). Action: ${s}`
|
|
380
382
|
));
|
|
381
383
|
return;
|
|
382
384
|
}
|
|
383
|
-
const
|
|
385
|
+
const R = Ee();
|
|
384
386
|
let W = !1;
|
|
385
|
-
const
|
|
386
|
-
W || (W = !0, clearTimeout(U), a != null && a.signal && a.signal.removeEventListener("abort",
|
|
387
|
+
const M = (m) => {
|
|
388
|
+
W || (W = !0, clearTimeout(U), a != null && a.signal && a.signal.removeEventListener("abort", S), b.delete(R), m());
|
|
387
389
|
}, D = () => {
|
|
388
|
-
|
|
389
|
-
},
|
|
390
|
+
K(R, e, y);
|
|
391
|
+
}, S = () => {
|
|
390
392
|
D();
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
+
const m = new Error(`Relay aborted for action: ${s}`);
|
|
394
|
+
m.code = "E_ABORT", M(() => d(m));
|
|
393
395
|
};
|
|
394
|
-
a != null && a.signal && a.signal.addEventListener("abort",
|
|
396
|
+
a != null && a.signal && a.signal.addEventListener("abort", S);
|
|
395
397
|
const U = setTimeout(() => {
|
|
396
|
-
D(),
|
|
398
|
+
D(), M(() => d(new Error(`Relay timeout for action: ${s}`)));
|
|
397
399
|
}, g);
|
|
398
|
-
|
|
399
|
-
settle: (
|
|
400
|
+
b.set(R, {
|
|
401
|
+
settle: (m) => M(() => u(m)),
|
|
400
402
|
timeoutId: U,
|
|
401
|
-
abort:
|
|
403
|
+
abort: S,
|
|
402
404
|
owner: e,
|
|
403
|
-
port:
|
|
405
|
+
port: y
|
|
404
406
|
});
|
|
405
407
|
const F = a == null ? void 0 : a.runId, B = a == null ? void 0 : a.callId;
|
|
406
|
-
A(
|
|
408
|
+
A(y, {
|
|
407
409
|
type: e === "main-thread" || e === "content-script" ? "asyncRelay" : "registryCall",
|
|
408
|
-
id:
|
|
410
|
+
id: R,
|
|
409
411
|
owner: e,
|
|
410
|
-
action:
|
|
412
|
+
action: s,
|
|
411
413
|
params: i,
|
|
412
414
|
callId: B,
|
|
413
415
|
tabPolicy: t,
|
|
414
|
-
command: { action:
|
|
416
|
+
command: { action: s, params: i, runId: F, callId: B },
|
|
415
417
|
runId: F
|
|
416
|
-
}) ||
|
|
418
|
+
}) || M(() => d(new Error(`Failed to post relay for action: ${s}`)));
|
|
417
419
|
});
|
|
418
420
|
};
|
|
419
421
|
}
|
|
420
|
-
const
|
|
422
|
+
const Me = "worker", w = 3e4, H = 5e3, Ie = 500, Te = {
|
|
421
423
|
page_goto: "timeout",
|
|
422
424
|
page_wait_for: "timeout",
|
|
423
425
|
tab_wait_for_load: "timeout",
|
|
@@ -425,84 +427,118 @@ const ke = "worker", h = 3e4, V = 5e3, Me = 500, Re = {
|
|
|
425
427
|
sleep: "duration",
|
|
426
428
|
page_wait: "duration",
|
|
427
429
|
sidepanel_wait: "duration"
|
|
428
|
-
},
|
|
429
|
-
function
|
|
430
|
+
}, X = /* @__PURE__ */ new Set(["page_goto"]);
|
|
431
|
+
function Ae(r, e) {
|
|
430
432
|
if (r === null || typeof r != "object" || Array.isArray(r))
|
|
431
433
|
return null;
|
|
432
|
-
const
|
|
433
|
-
return typeof
|
|
434
|
-
}
|
|
435
|
-
function Ae(r, e) {
|
|
436
|
-
return K.has(r) ? e * 2 + Me + V : e + V;
|
|
434
|
+
const s = r[e];
|
|
435
|
+
return typeof s == "bigint" ? Number(s) : typeof s == "number" && Number.isFinite(s) ? s : null;
|
|
437
436
|
}
|
|
438
437
|
function Se(r, e) {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
438
|
+
return X.has(r) ? e * 2 + Ie + H : e + H;
|
|
439
|
+
}
|
|
440
|
+
function Z(r, e) {
|
|
441
|
+
const s = Te[r];
|
|
442
|
+
if (!s) return w;
|
|
443
|
+
let t = Ae(e, s);
|
|
444
|
+
return t === null && X.has(r) && (t = w), t === null ? w : Math.max(
|
|
445
|
+
w,
|
|
446
|
+
Se(r, t)
|
|
445
447
|
);
|
|
446
448
|
}
|
|
449
|
+
function Ne(r, e) {
|
|
450
|
+
var o, i;
|
|
451
|
+
r = k(r);
|
|
452
|
+
const s = e == null ? void 0 : e.action;
|
|
453
|
+
if (l.trace("extensionDispatch", { action: s, callId: e == null ? void 0 : e.callId, runId: e == null ? void 0 : e.runId }), !s)
|
|
454
|
+
return Promise.resolve({
|
|
455
|
+
ok: !1,
|
|
456
|
+
error: { message: "Missing action in dispatch context", code: "E_MISSING_ACTION" }
|
|
457
|
+
});
|
|
458
|
+
if (h.has(s)) {
|
|
459
|
+
const a = h.get(s), g = (e == null ? void 0 : e.signal) ?? (e != null && e.runId ? (o = p.get(e.runId)) == null ? void 0 : o.signal : void 0);
|
|
460
|
+
return (async () => {
|
|
461
|
+
try {
|
|
462
|
+
return { ok: !0, value: await a(r, { ...e, signal: g }) };
|
|
463
|
+
} catch (u) {
|
|
464
|
+
const d = u instanceof Error ? u.message : String(u), y = typeof u == "object" && u !== null && "code" in u && typeof u.code == "string" ? u.code : "E_WORKER_HANDLER";
|
|
465
|
+
return { ok: !1, error: { message: d, code: y } };
|
|
466
|
+
}
|
|
467
|
+
})();
|
|
468
|
+
}
|
|
469
|
+
const t = fe(s);
|
|
470
|
+
return t ? Q({
|
|
471
|
+
owner: t.endpoint,
|
|
472
|
+
action: s,
|
|
473
|
+
resolveTimeoutMs: (a) => Z(s, a),
|
|
474
|
+
tabPolicy: t.tabPolicy
|
|
475
|
+
})(r, {
|
|
476
|
+
...e,
|
|
477
|
+
signal: (e == null ? void 0 : e.signal) ?? (e != null && e.runId ? (i = p.get(e.runId)) == null ? void 0 : i.signal : void 0)
|
|
478
|
+
}) : Promise.resolve({
|
|
479
|
+
ok: !1,
|
|
480
|
+
error: { message: `No route registered for action: ${s}`, code: "E_NO_ROUTE" }
|
|
481
|
+
});
|
|
482
|
+
}
|
|
447
483
|
function Ce(r) {
|
|
448
|
-
if (r.owner ===
|
|
449
|
-
const e =
|
|
484
|
+
if (r.owner === Me) {
|
|
485
|
+
const e = h.get(r.action);
|
|
450
486
|
if (!e)
|
|
451
487
|
throw new Error(
|
|
452
488
|
`No worker-local handler registered for action: ${r.action}`
|
|
453
489
|
);
|
|
454
|
-
return async (
|
|
490
|
+
return async (s, t) => {
|
|
455
491
|
var o;
|
|
456
|
-
|
|
457
|
-
const
|
|
492
|
+
s = k(s);
|
|
493
|
+
const n = (t == null ? void 0 : t.signal) ?? (t != null && t.runId ? (o = p.get(t.runId)) == null ? void 0 : o.signal : void 0);
|
|
458
494
|
try {
|
|
459
|
-
return { ok: !0, value: await e(
|
|
495
|
+
return { ok: !0, value: await e(s, { ...t, action: r.action, signal: n }) };
|
|
460
496
|
} catch (i) {
|
|
461
497
|
const a = i instanceof Error ? i.message : String(i), g = typeof i == "object" && i !== null && "code" in i && typeof i.code == "string" ? i.code : r.errorCode;
|
|
462
498
|
return { ok: !1, error: { message: a, code: g } };
|
|
463
499
|
}
|
|
464
500
|
};
|
|
465
501
|
} else {
|
|
466
|
-
const e =
|
|
502
|
+
const e = Q({
|
|
467
503
|
owner: r.owner,
|
|
468
504
|
action: r.action,
|
|
469
|
-
resolveTimeoutMs: (
|
|
505
|
+
resolveTimeoutMs: (s) => Z(r.action, s)
|
|
470
506
|
});
|
|
471
|
-
return (
|
|
472
|
-
var
|
|
473
|
-
return e(
|
|
507
|
+
return (s, t) => {
|
|
508
|
+
var n;
|
|
509
|
+
return e(k(s), {
|
|
474
510
|
...t,
|
|
475
|
-
signal: (t == null ? void 0 : t.signal) ?? (t != null && t.runId ? (
|
|
511
|
+
signal: (t == null ? void 0 : t.signal) ?? (t != null && t.runId ? (n = p.get(t.runId)) == null ? void 0 : n.signal : void 0)
|
|
476
512
|
});
|
|
477
513
|
};
|
|
478
514
|
}
|
|
479
515
|
}
|
|
480
|
-
function
|
|
516
|
+
function Le(r) {
|
|
481
517
|
c("exists", (e) => r.fsExists(e)), c("stat", (e) => r.fsStat(e)), c("read", (e) => r.fsRead(e)), c("readText", (e) => r.fsReadText(e)), c("readBase64", (e) => r.fsReadBase64(e)), c("list", (e) => r.fsList(e)), c("mkdir", (e) => r.fsMkdir(e)), c("delete", (e) => r.fsDelete(e)), c("copy", (e) => r.fsCopy(e)), c("move", (e) => r.fsMove(e)), c("write", (e) => r.fsWrite(e)), c("writeText", (e) => r.fsWriteText(e)), c("writeBase64", (e) => r.fsWriteBase64(e)), c("append", (e) => r.fsAppend(e)), c("appendText", (e) => r.fsAppendText(e)), c("appendBase64", (e) => r.fsAppendBase64(e)), c("readRange", (e) => r.fsReadRange(e)), c("update", (e) => r.fsUpdate(e)), c("hash", (e) => r.fsHash(e));
|
|
482
518
|
}
|
|
483
|
-
async function
|
|
519
|
+
async function ve(r, e) {
|
|
484
520
|
if (v) return;
|
|
485
|
-
await
|
|
521
|
+
await x(), _ = new ee(), C(0), ae(C), l.trace("initWasm_start"), Le(_), _e(r);
|
|
486
522
|
const t = r.map((o) => ({
|
|
487
|
-
entry:
|
|
523
|
+
entry: be(o),
|
|
488
524
|
callback: Ce(o)
|
|
489
525
|
}));
|
|
490
526
|
try {
|
|
491
|
-
|
|
527
|
+
re(t);
|
|
492
528
|
} catch (o) {
|
|
493
529
|
const i = o instanceof Error ? o.message : String(o);
|
|
494
530
|
throw new Error(`Registry registration failed: ${i}`);
|
|
495
531
|
}
|
|
496
|
-
const { freezeManifest:
|
|
532
|
+
const { freezeManifest: n } = await import("./extension_js.js");
|
|
497
533
|
try {
|
|
498
|
-
|
|
534
|
+
n();
|
|
499
535
|
} catch (o) {
|
|
500
536
|
const i = o instanceof Error ? o.message : String(o);
|
|
501
537
|
throw new Error(`Manifest freeze failed: ${i}`);
|
|
502
538
|
}
|
|
503
|
-
if (
|
|
539
|
+
if (_.injectRegistryBindings(), e) {
|
|
504
540
|
const o = JSON.stringify(e);
|
|
505
|
-
await
|
|
541
|
+
await _.runCellAsync(
|
|
506
542
|
`(function(){var r=globalThis.chrome&&globalThis.chrome.runtime;if(!r){r={};if(!globalThis.chrome)globalThis.chrome={};globalThis.chrome.runtime=r;}r.id=${o};})();`,
|
|
507
543
|
"",
|
|
508
544
|
"inject-runtime-id"
|
|
@@ -522,23 +558,23 @@ self.onmessage = async (r) => {
|
|
|
522
558
|
l.error("register_worker_port_missing", { owner: e.owner });
|
|
523
559
|
return;
|
|
524
560
|
}
|
|
525
|
-
|
|
561
|
+
ke(e.owner, t);
|
|
526
562
|
return;
|
|
527
563
|
}
|
|
528
564
|
if (e.type === "init") {
|
|
529
565
|
try {
|
|
530
|
-
await
|
|
566
|
+
await ve(e.manifest, e.extensionId), self.postMessage({ type: "ready" });
|
|
531
567
|
} catch (t) {
|
|
532
|
-
const
|
|
533
|
-
l.error("worker_init_failed", { error:
|
|
568
|
+
const n = t instanceof Error ? t.message : String(t);
|
|
569
|
+
l.error("worker_init_failed", { error: n }), self.postMessage({ type: "error", error: `WASM init failed: ${n}` });
|
|
534
570
|
}
|
|
535
571
|
return;
|
|
536
572
|
}
|
|
537
573
|
if (e.type === "setLogLevel") {
|
|
538
|
-
|
|
574
|
+
C(e.level), ne(we(e.level)), l.trace("set_log_level", { level: e.level });
|
|
539
575
|
return;
|
|
540
576
|
}
|
|
541
|
-
if (!v || !
|
|
577
|
+
if (!v || !_) {
|
|
542
578
|
self.postMessage({
|
|
543
579
|
type: "error",
|
|
544
580
|
id: e.id,
|
|
@@ -546,14 +582,14 @@ self.onmessage = async (r) => {
|
|
|
546
582
|
});
|
|
547
583
|
return;
|
|
548
584
|
}
|
|
549
|
-
const
|
|
550
|
-
await
|
|
585
|
+
const s = _;
|
|
586
|
+
await he(async () => {
|
|
551
587
|
switch (e.type) {
|
|
552
588
|
case "runCell": {
|
|
553
|
-
const t = e.runId,
|
|
554
|
-
t &&
|
|
589
|
+
const t = e.runId, n = new AbortController();
|
|
590
|
+
t && p.set(t, n), f = { id: e.id, runId: t }, l.trace("runCell_start", { runId: t, callId: e.id, codeLen: e.code.length });
|
|
555
591
|
try {
|
|
556
|
-
const o = await
|
|
592
|
+
const o = await s.runCellAsync(
|
|
557
593
|
e.code,
|
|
558
594
|
e.stdin || "",
|
|
559
595
|
t || ""
|
|
@@ -573,71 +609,71 @@ self.onmessage = async (r) => {
|
|
|
573
609
|
runId: t
|
|
574
610
|
});
|
|
575
611
|
} finally {
|
|
576
|
-
(
|
|
612
|
+
(f == null ? void 0 : f.id) === e.id && (f = null), t && p.delete(t);
|
|
577
613
|
}
|
|
578
614
|
break;
|
|
579
615
|
}
|
|
580
616
|
case "reset": {
|
|
581
|
-
|
|
582
|
-
for (const t of
|
|
583
|
-
|
|
617
|
+
s.setAborted(!0);
|
|
618
|
+
for (const t of p.values()) t.abort();
|
|
619
|
+
p.clear(), z("E_RESET", "Worker reset");
|
|
584
620
|
try {
|
|
585
|
-
|
|
621
|
+
s.reset(), self.postMessage({ type: "result", id: e.id, data: { ok: !0 } });
|
|
586
622
|
} catch (t) {
|
|
587
|
-
const
|
|
588
|
-
self.postMessage({ type: "error", id: e.id, error:
|
|
623
|
+
const n = t instanceof Error ? t.message : String(t);
|
|
624
|
+
self.postMessage({ type: "error", id: e.id, error: n });
|
|
589
625
|
}
|
|
590
626
|
break;
|
|
591
627
|
}
|
|
592
628
|
case "stop": {
|
|
593
|
-
|
|
594
|
-
for (const t of
|
|
595
|
-
|
|
629
|
+
s.setAborted(!0);
|
|
630
|
+
for (const t of p.values()) t.abort();
|
|
631
|
+
p.clear(), z("E_STOPPED", "Worker stopped"), self.postMessage({ type: "result", id: e.id, data: { ok: !0 } });
|
|
596
632
|
break;
|
|
597
633
|
}
|
|
598
634
|
case "setFuelLimit": {
|
|
599
635
|
try {
|
|
600
|
-
|
|
636
|
+
s.set_fuel_limit(e.limit), e.id && self.postMessage({ type: "result", id: e.id, data: { ok: !0 } });
|
|
601
637
|
} catch (t) {
|
|
602
638
|
if (e.id) {
|
|
603
|
-
const
|
|
604
|
-
self.postMessage({ type: "error", id: e.id, error:
|
|
639
|
+
const n = t instanceof Error ? t.message : String(t);
|
|
640
|
+
self.postMessage({ type: "error", id: e.id, error: n });
|
|
605
641
|
}
|
|
606
642
|
}
|
|
607
643
|
break;
|
|
608
644
|
}
|
|
609
645
|
case "inspectGlobals": {
|
|
610
646
|
try {
|
|
611
|
-
const t =
|
|
647
|
+
const t = s.inspect_globals();
|
|
612
648
|
self.postMessage({ type: "result", id: e.id, data: t });
|
|
613
649
|
} catch (t) {
|
|
614
|
-
const
|
|
615
|
-
self.postMessage({ type: "error", id: e.id, error:
|
|
650
|
+
const n = t instanceof Error ? t.message : String(t);
|
|
651
|
+
self.postMessage({ type: "error", id: e.id, error: n });
|
|
616
652
|
}
|
|
617
653
|
break;
|
|
618
654
|
}
|
|
619
655
|
case "apiDocs": {
|
|
620
656
|
try {
|
|
621
|
-
const t =
|
|
657
|
+
const t = s.apiDocs(e.format);
|
|
622
658
|
self.postMessage({ type: "result", id: e.id, data: t });
|
|
623
659
|
} catch (t) {
|
|
624
|
-
const
|
|
625
|
-
self.postMessage({ type: "error", id: e.id, error:
|
|
660
|
+
const n = t instanceof Error ? t.message : String(t);
|
|
661
|
+
self.postMessage({ type: "error", id: e.id, error: n });
|
|
626
662
|
}
|
|
627
663
|
break;
|
|
628
664
|
}
|
|
629
665
|
case "loadLibrary": {
|
|
630
666
|
try {
|
|
631
|
-
const t =
|
|
667
|
+
const t = s.load_library(e.source);
|
|
632
668
|
self.postMessage({ type: "result", id: e.id, data: t });
|
|
633
669
|
} catch (t) {
|
|
634
|
-
const
|
|
635
|
-
self.postMessage({ type: "error", id: e.id, error:
|
|
670
|
+
const n = t instanceof Error ? t.message : String(t);
|
|
671
|
+
self.postMessage({ type: "error", id: e.id, error: n });
|
|
636
672
|
}
|
|
637
673
|
break;
|
|
638
674
|
}
|
|
639
675
|
case "fsCall": {
|
|
640
|
-
const t =
|
|
676
|
+
const t = h.get(e.action);
|
|
641
677
|
if (!t) {
|
|
642
678
|
self.postMessage({
|
|
643
679
|
type: "error",
|
|
@@ -647,10 +683,10 @@ self.onmessage = async (r) => {
|
|
|
647
683
|
break;
|
|
648
684
|
}
|
|
649
685
|
try {
|
|
650
|
-
const
|
|
651
|
-
self.postMessage({ type: "result", id: e.id, data:
|
|
652
|
-
} catch (
|
|
653
|
-
const o =
|
|
686
|
+
const n = await t(e.params);
|
|
687
|
+
self.postMessage({ type: "result", id: e.id, data: n });
|
|
688
|
+
} catch (n) {
|
|
689
|
+
const o = n instanceof Error ? n.message : String(n);
|
|
654
690
|
self.postMessage({ type: "error", id: e.id, error: o });
|
|
655
691
|
}
|
|
656
692
|
break;
|
|
@@ -662,3 +698,14 @@ self.onmessage = async (r) => {
|
|
|
662
698
|
}
|
|
663
699
|
});
|
|
664
700
|
};
|
|
701
|
+
export {
|
|
702
|
+
w as DEFAULT_RELAY_TIMEOUT_MS,
|
|
703
|
+
Ce as createExecutableCallback,
|
|
704
|
+
Ne as extensionDispatch,
|
|
705
|
+
c as registerWorkerHandler,
|
|
706
|
+
ke as registerWorkerPort,
|
|
707
|
+
N as resolveAsyncRelayResult,
|
|
708
|
+
Z as resolveRelayTimeoutMs,
|
|
709
|
+
Q as safePostAsCall,
|
|
710
|
+
z as settleAllPendingRelays
|
|
711
|
+
};
|