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