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