@webpodjs/sdk 0.0.0-stage → 0.1.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.
Files changed (96) hide show
  1. package/README.md +246 -2
  2. package/dist/agent/backend.d.ts +68 -0
  3. package/dist/agent/backend.js +417 -0
  4. package/dist/agent/backend.js.map +1 -0
  5. package/dist/agent/fs.d.ts +45 -0
  6. package/dist/agent/fs.js +157 -0
  7. package/dist/agent/fs.js.map +1 -0
  8. package/dist/agent/protocol.d.ts +236 -0
  9. package/dist/agent/protocol.js +368 -0
  10. package/dist/agent/protocol.js.map +1 -0
  11. package/dist/coi-serviceworker.d.ts +0 -0
  12. package/dist/coi-serviceworker.js +33 -0
  13. package/dist/coi-serviceworker.js.map +1 -0
  14. package/dist/coi.d.ts +19 -0
  15. package/dist/coi.js +54 -0
  16. package/dist/coi.js.map +1 -0
  17. package/dist/errors.d.ts +5 -0
  18. package/dist/errors.js +9 -0
  19. package/dist/errors.js.map +1 -0
  20. package/dist/features.d.ts +37 -0
  21. package/dist/features.js +125 -0
  22. package/dist/features.js.map +1 -0
  23. package/dist/fs/tar.d.ts +21 -0
  24. package/dist/fs/tar.js +332 -0
  25. package/dist/fs/tar.js.map +1 -0
  26. package/dist/fs/workspace.d.ts +19 -0
  27. package/dist/fs/workspace.js +233 -0
  28. package/dist/fs/workspace.js.map +1 -0
  29. package/dist/fs-router.d.ts +8 -0
  30. package/dist/fs-router.js +50 -0
  31. package/dist/fs-router.js.map +1 -0
  32. package/dist/host/backend.d.ts +115 -0
  33. package/dist/host/backend.js +475 -0
  34. package/dist/host/backend.js.map +1 -0
  35. package/dist/host/binding.d.ts +33 -0
  36. package/dist/host/binding.js +72 -0
  37. package/dist/host/binding.js.map +1 -0
  38. package/dist/host/channel.d.ts +41 -0
  39. package/dist/host/channel.js +168 -0
  40. package/dist/host/channel.js.map +1 -0
  41. package/dist/host/controller.d.ts +264 -0
  42. package/dist/host/controller.js +484 -0
  43. package/dist/host/controller.js.map +1 -0
  44. package/dist/host/devices.d.ts +134 -0
  45. package/dist/host/devices.js +317 -0
  46. package/dist/host/devices.js.map +1 -0
  47. package/dist/host/pump-worker.d.ts +1 -0
  48. package/dist/host/pump-worker.js +102 -0
  49. package/dist/host/pump-worker.js.map +1 -0
  50. package/dist/host/qmp.d.ts +8 -0
  51. package/dist/host/qmp.js +78 -0
  52. package/dist/host/qmp.js.map +1 -0
  53. package/dist/host/ring.d.ts +85 -0
  54. package/dist/host/ring.js +389 -0
  55. package/dist/host/ring.js.map +1 -0
  56. package/dist/index.d.ts +33 -0
  57. package/dist/index.js +19 -0
  58. package/dist/index.js.map +1 -0
  59. package/dist/net/client.d.ts +19 -0
  60. package/dist/net/client.js +230 -0
  61. package/dist/net/client.js.map +1 -0
  62. package/dist/net/options.d.ts +65 -0
  63. package/dist/net/options.js +8 -0
  64. package/dist/net/options.js.map +1 -0
  65. package/dist/net/preview.d.ts +27 -0
  66. package/dist/net/preview.js +233 -0
  67. package/dist/net/preview.js.map +1 -0
  68. package/dist/net/protocol.d.ts +65 -0
  69. package/dist/net/protocol.js +13 -0
  70. package/dist/net/protocol.js.map +1 -0
  71. package/dist/pod.d.ts +43 -0
  72. package/dist/pod.js +206 -0
  73. package/dist/pod.js.map +1 -0
  74. package/dist/preview-relay.d.ts +0 -0
  75. package/dist/preview-relay.html +4 -0
  76. package/dist/preview-relay.js +107 -0
  77. package/dist/preview-relay.js.map +1 -0
  78. package/dist/preview-serviceworker.d.ts +0 -0
  79. package/dist/preview-serviceworker.js +405 -0
  80. package/dist/preview-serviceworker.js.map +1 -0
  81. package/dist/streams.d.ts +14 -0
  82. package/dist/streams.js +58 -0
  83. package/dist/streams.js.map +1 -0
  84. package/dist/testing/index.d.ts +14 -0
  85. package/dist/testing/index.js +354 -0
  86. package/dist/testing/index.js.map +1 -0
  87. package/dist/timers.d.ts +18 -0
  88. package/dist/timers.js +68 -0
  89. package/dist/timers.js.map +1 -0
  90. package/dist/types.d.ts +163 -0
  91. package/dist/types.js +14 -0
  92. package/dist/types.js.map +1 -0
  93. package/dist/units.d.ts +2 -0
  94. package/dist/units.js +31 -0
  95. package/dist/units.js.map +1 -0
  96. package/package.json +53 -4
@@ -0,0 +1,317 @@
1
+ import { workerNetworkConfig } from "../net/options.js";
2
+ import { WebPodError } from "../errors.js";
3
+ import { hiddenNote, visibleDeadline, visibleTimeout } from "../timers.js";
4
+ /** A host-side device Worker speaking docs/host-engine.md §4 (attach / ready / stop). */
5
+ export class DeviceWorker {
6
+ url;
7
+ #worker;
8
+ #waiters = new Set();
9
+ #timeoutMs;
10
+ #error;
11
+ constructor(url, options = {}) {
12
+ this.url = url;
13
+ this.#timeoutMs = options.timeoutMs ?? 30000;
14
+ this.#worker = (options.createWorker ?? ((u) => new Worker(u, { type: "module" })))(url);
15
+ const fail = (message) => {
16
+ this.#error = new WebPodError("ENGINE_UNAVAILABLE", `device worker ${url}: ${message}`);
17
+ for (const w of this.#waiters)
18
+ w.reject(this.#error);
19
+ this.#waiters.clear();
20
+ };
21
+ this.#worker.onmessage = ({ data }) => {
22
+ const msg = data;
23
+ if (msg.type === "log")
24
+ options.onLog?.(String(msg.level ?? "info"), String(msg.text ?? ""));
25
+ else if (msg.type === "error") {
26
+ // "<request type>: ..." fails that request only; anything else means the Worker is broken.
27
+ const message = String(msg.message ?? "error");
28
+ const waiter = [...this.#waiters].find((w) => message.startsWith(`${w.type}:`));
29
+ if (waiter) {
30
+ this.#waiters.delete(waiter);
31
+ waiter.reject(new WebPodError(/no snapshot/.test(message) ? "ENOENT" : /need OPFS/.test(message) ? "UNSUPPORTED" : "EIO", message));
32
+ }
33
+ else
34
+ fail(message);
35
+ return;
36
+ }
37
+ for (const w of [...this.#waiters]) {
38
+ if (w.match(msg)) {
39
+ this.#waiters.delete(w);
40
+ w.resolve(msg);
41
+ }
42
+ }
43
+ options.onMessage?.(msg);
44
+ };
45
+ this.#worker.onerror = (e) => fail(e.message ?? "worker error");
46
+ }
47
+ post(message, transfer = []) {
48
+ this.#worker.postMessage(message, transfer);
49
+ }
50
+ /** Posts `message` and resolves with the first reply `match` accepts. */
51
+ request(message, match, transfer = []) {
52
+ if (this.#error)
53
+ return Promise.reject(this.#error);
54
+ return new Promise((resolve, reject) => {
55
+ const cancel = visibleTimeout(this.#timeoutMs, (hidden) => {
56
+ this.#waiters.delete(waiter);
57
+ reject(new WebPodError("ENGINE_UNAVAILABLE", `device worker ${this.url}: no reply to ${message.type}${hiddenNote(hidden)}`));
58
+ });
59
+ const waiter = {
60
+ type: String(message.type),
61
+ match,
62
+ resolve: (m) => {
63
+ cancel();
64
+ resolve(m);
65
+ },
66
+ reject: (e) => {
67
+ cancel();
68
+ reject(e);
69
+ },
70
+ };
71
+ this.#waiters.add(waiter);
72
+ this.post(message, transfer);
73
+ });
74
+ }
75
+ #stopping;
76
+ /** Asks the Worker to stop (it may flush), then terminates it. Idempotent. */
77
+ stop(graceMs = 2000) {
78
+ this.#stopping ??= this.#stop(graceMs);
79
+ return this.#stopping;
80
+ }
81
+ async #stop(graceMs) {
82
+ if (!this.#error) {
83
+ let timer;
84
+ await Promise.race([
85
+ this.request({ type: "stop" }, (m) => m.type === "stopped").catch(() => { }),
86
+ new Promise((r) => (timer = setTimeout(r, graceMs))),
87
+ ]);
88
+ clearTimeout(timer);
89
+ }
90
+ this.#worker.terminate();
91
+ }
92
+ }
93
+ /** Page LANs by name: their members by id. */
94
+ const lans = new Map();
95
+ let nextMember = 1;
96
+ /**
97
+ * The LAN link index (0..1023, a /26 in 198.19.0.0/16) for `hostname`: a hash of the name, so it
98
+ * is the same in every session; the next free one if another member of the LAN already has it.
99
+ */
100
+ export function lanIndex(hostname, taken = new Set()) {
101
+ let h = 0x811c9dc5;
102
+ for (const c of hostname.toLowerCase())
103
+ h = Math.imul(h ^ c.codePointAt(0), 0x01000193) >>> 0;
104
+ let i = h % 1024;
105
+ while (taken.has(i))
106
+ i = (i + 1) % 1024;
107
+ return i;
108
+ }
109
+ function lanHostname(n, persist) {
110
+ const raw = n.hostname ?? persist?.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 63) ?? "";
111
+ const name = (raw || `pod-${nextMember}`).toLowerCase();
112
+ if (!/^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/.test(name))
113
+ throw new WebPodError("EINVAL", `network.hostname must be a DNS label: ${name}`);
114
+ return name;
115
+ }
116
+ /** Net device served by the net Worker (packages/net-worker); also takes page connections (T14). */
117
+ export function netWorker(url, options = {}) {
118
+ let network = options.network ?? {};
119
+ let caKey = options.caKey ?? null;
120
+ let worker;
121
+ const onMessage = (m) => {
122
+ options.onMessage?.(m);
123
+ if (m.type !== "relay-token-request")
124
+ return;
125
+ const id = m.id;
126
+ const reply = (fields) => worker.post({ type: "relay-token", id, ...fields });
127
+ const getToken = network.relay?.getToken;
128
+ if (!getToken)
129
+ return reply({ error: "no getToken configured" });
130
+ Promise.resolve()
131
+ .then(getToken)
132
+ .then((token) => reply({ token: String(token) }), (e) => reply({ error: String(e?.message ?? e) }));
133
+ };
134
+ // A boot that ends detaches (stops) the Worker; the next attach (the cold boot after a warm
135
+ // resume that failed, or a new Pod with the same attachment) starts a fresh one.
136
+ let stopped = false;
137
+ // The guest only sees a 502 when a fetch fails, and the browser does not say why: without a
138
+ // handler the Worker's warnings (failed fetches, TLS, relays) still reach the console.
139
+ const onLog = options.onLog ??
140
+ ((level, text) => {
141
+ if (level === "warn" || level === "error")
142
+ console.warn(`webpod net: ${text}`);
143
+ });
144
+ const start = () => {
145
+ worker = new DeviceWorker(url, { ...options, onLog, onMessage });
146
+ stopped = false;
147
+ };
148
+ start();
149
+ const mac = options.mac ?? "52:54:00:12:34:56";
150
+ let attached = false;
151
+ let member = null;
152
+ const leave = () => {
153
+ if (!member)
154
+ return;
155
+ member.lan.delete(member.me.id);
156
+ for (const other of member.lan.values())
157
+ other.worker.post({ type: "peer-gone", id: member.me.id });
158
+ member = null;
159
+ };
160
+ return {
161
+ get worker() {
162
+ return worker;
163
+ },
164
+ mac,
165
+ configure(n, key) {
166
+ network = n;
167
+ if (key !== undefined)
168
+ caKey = key;
169
+ },
170
+ async attach(rings) {
171
+ if (stopped)
172
+ start();
173
+ const config = workerNetworkConfig(network);
174
+ const lanName = typeof network.lan === "string" ? network.lan : network.lan?.name;
175
+ let me = null;
176
+ let lan;
177
+ if (lanName) {
178
+ lan = lans.get(lanName) ?? new Map();
179
+ lans.set(lanName, lan);
180
+ const hostname = lanHostname(network, caKey);
181
+ const members = [...lan.values()];
182
+ if (members.some((m) => m.hostname === hostname))
183
+ throw new WebPodError("EINVAL", `host name ${hostname} is already on LAN ${lanName}`);
184
+ me = { id: nextMember++, hostname, index: lanIndex(hostname, new Set(members.map((m) => m.index))), worker };
185
+ const allow = typeof network.lan === "object" ? network.lan.allow : undefined;
186
+ config.lan = { index: me.index, hostname, ...(allow ? { allow } : {}) };
187
+ }
188
+ const init = { ...options.init, network: config, caKey };
189
+ await worker.request({ type: "attach", ...init, ...rings, mac }, (m) => m.type === "ready");
190
+ attached = true;
191
+ if (me && lan) {
192
+ // Worker to Worker: frames between the pods never touch this thread. Both ends hold the
193
+ // route before the guest can send anything (Pod.boot resolves after this).
194
+ const joined = me;
195
+ const acks = [];
196
+ for (const other of lan.values()) {
197
+ const { port1, port2 } = new MessageChannel();
198
+ const added = (id) => (m) => m.type === "peer-added" && m.id === id;
199
+ acks.push(worker.request({ type: "peer", id: other.id, hostname: other.hostname, index: other.index, port: port1 }, added(other.id), [port1]));
200
+ acks.push(other.worker.request({ type: "peer", id: joined.id, hostname: joined.hostname, index: joined.index, port: port2 }, added(joined.id), [port2]));
201
+ }
202
+ lan.set(me.id, me);
203
+ await Promise.all(acks);
204
+ member = { lan, me };
205
+ }
206
+ },
207
+ async caCertificate() {
208
+ // Only an attached net Worker has a CA; one that cannot answer means no TLS interception.
209
+ if (!attached)
210
+ return "";
211
+ const timeout = visibleDeadline(5000, () => new Error("timeout"));
212
+ try {
213
+ const m = await Promise.race([worker.request({ type: "ca" }, (r) => r.type === "ca"), timeout.promise]);
214
+ return String(m.pem ?? "");
215
+ }
216
+ catch {
217
+ onLog("warn", "net worker did not provide a CA certificate; TLS interception will not be trusted");
218
+ return "";
219
+ }
220
+ finally {
221
+ timeout.cancel();
222
+ }
223
+ },
224
+ async stats() {
225
+ if (!attached)
226
+ return null;
227
+ const m = await worker.request({ type: "stats" }, (r) => r.type === "stats");
228
+ return m.stats ?? null;
229
+ },
230
+ openPort(port) {
231
+ worker.post({ type: "inbound", port }, [port]);
232
+ },
233
+ detach() {
234
+ stopped = true;
235
+ attached = false;
236
+ leave();
237
+ return worker.stop();
238
+ },
239
+ };
240
+ }
241
+ /**
242
+ * The FS Worker (webpod-fs: disks + shared workspace). `init` is sent first; the Worker answers
243
+ * with the volumes it serves, which become the engine's blk / 9p devices.
244
+ */
245
+ export async function fsWorker(url, init, options = {}) {
246
+ const worker = new DeviceWorker(url, options);
247
+ let reply;
248
+ try {
249
+ reply = await worker.request({ type: "init", ...init }, (m) => m.type === "volumes");
250
+ }
251
+ catch (e) {
252
+ await worker.stop(0);
253
+ throw e;
254
+ }
255
+ const volumes = reply.devices;
256
+ // One Worker serves every volume: stop it once no attached volume remains (detach is per volume,
257
+ // idempotent; volumes the engine skipped detach without ever attaching).
258
+ const live = new Set();
259
+ let stopping;
260
+ const detachVolume = (key) => () => {
261
+ live.delete(key);
262
+ if (live.size === 0)
263
+ stopping ??= worker.stop();
264
+ return stopping ?? Promise.resolve();
265
+ };
266
+ const blocks = [];
267
+ const shares = [];
268
+ const ws = init.workspace ?? undefined;
269
+ for (const v of volumes) {
270
+ if (v.class === "9p") {
271
+ shares.push({
272
+ id: v.id,
273
+ msize: v.msize,
274
+ mountPoint: ws?.mountPoint ?? "/workspace",
275
+ localPaths: v.localPaths ?? [],
276
+ async attach(rings) {
277
+ live.add(`9p:${v.id}`);
278
+ await worker.request({ type: "attach", id: v.id, ...rings }, (m) => m.type === "ready" && m.id === v.id);
279
+ },
280
+ detach: detachVolume(`9p:${v.id}`),
281
+ });
282
+ }
283
+ else {
284
+ blocks.push({
285
+ serial: v.serial,
286
+ sizeBytes: v.sizeBytes,
287
+ readOnly: v.readOnly,
288
+ blockSize: v.blockSize,
289
+ indirect: v.indirect,
290
+ indirectMin: v.indirectMin,
291
+ async attach(rings) {
292
+ live.add(`blk:${v.serial}`);
293
+ await worker.request({ type: "attach", serial: v.serial, ...rings }, (m) => m.type === "ready" && m.serial === v.serial);
294
+ },
295
+ detach: detachVolume(`blk:${v.serial}`),
296
+ });
297
+ }
298
+ }
299
+ return {
300
+ worker,
301
+ blocks,
302
+ shares,
303
+ ephemeral: reply.ephemeral ?? null,
304
+ dataFresh: reply.dataFresh === true,
305
+ workspaceMount: shares.length > 0 ? (ws?.mountPoint ?? "/workspace") : null,
306
+ openPort: (port) => worker.post({ type: "fs-port", port }, [port]),
307
+ flush: () => worker.post({ type: "flush" }),
308
+ snapshots: {
309
+ take: async (name, meta = {}) => (await worker.request({ type: "snapshot", name, meta }, (m) => m.type === "snapshot")).info,
310
+ list: async () => (await worker.request({ type: "snapshots" }, (m) => m.type === "snapshots")).list,
311
+ delete: async (name) => (await worker.request({ type: "delete-snapshot", name }, (m) => m.type === "snapshot-deleted" && m.name === name)).deleted,
312
+ export: async (name) => (await worker.request({ type: "export-snapshot", name }, (m) => m.type === "exported" && m.name === name)).file,
313
+ import: async (file, name) => (await worker.request({ type: "import-snapshot", file, name }, (m) => m.type === "imported")).info,
314
+ },
315
+ };
316
+ }
317
+ //# sourceMappingURL=devices.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"devices.js","sourceRoot":"","sources":["../../src/host/devices.ts"],"names":[],"mappings":"AACA,OAAO,EAAuB,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAC7E,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;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C,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,OAAkB;YACvI,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,MAAM,MAAM,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,iBAAiB,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAY;YAC/I,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,MAAM,MAAM,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,iBAAiB,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,IAAoB;SACjJ;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,102 @@
1
+ // Ring pumps for the host's byte channels (console, agent, qmp), off the page's main thread: a
2
+ // hidden page has its timers throttled to ~1 Hz, which would slow the guest down with it.
3
+ // Protocol (main <-> this module Worker):
4
+ // main -> { type: "open", id, memory, toEngine, fromEngine } | { type: "demand", id }
5
+ // | { type: "write", id, seq, data: ArrayBuffer } | { type: "close", id } | { type: "stop" }
6
+ // worker -> { type: "data", id, chunks: ArrayBuffer[] } (answers one demand)
7
+ // | { type: "written", id, seq } | { type: "error", id, message } | { type: "stopped" }
8
+ // Reads are demand-driven, so a page that stops reading leaves the ring full and the guest
9
+ // backpressured, never an unbounded queue here.
10
+ import { RingConsumer, RingProducer, settleWaits, waitReadable, waitWritable } from "./ring.js";
11
+ const scope = self;
12
+ const channels = new Map();
13
+ const WAIT = { watchdogMs: 20 };
14
+ async function serveReads(id, ch) {
15
+ if (ch.reading)
16
+ return;
17
+ ch.reading = true;
18
+ try {
19
+ while (ch.demand && !ch.closed) {
20
+ const batch = ch.rx.drain(256);
21
+ if (batch.length === 0) {
22
+ await waitReadable(ch.rx, WAIT);
23
+ continue;
24
+ }
25
+ ch.demand = false;
26
+ const chunks = batch.map((b) => b.buffer);
27
+ scope.postMessage({ type: "data", id, chunks }, chunks);
28
+ }
29
+ }
30
+ catch (e) {
31
+ scope.postMessage({ type: "error", id, message: String(e?.message ?? e) });
32
+ }
33
+ finally {
34
+ ch.reading = false;
35
+ }
36
+ }
37
+ async function serveWrites(id, ch) {
38
+ if (ch.writing)
39
+ return;
40
+ ch.writing = true;
41
+ try {
42
+ const max = ch.tx.maxMessageLen;
43
+ for (let w = ch.writes.shift(); w && !ch.closed; w = ch.writes.shift()) {
44
+ for (let off = 0; off < w.data.length; off += max) {
45
+ const part = w.data.subarray(off, off + max);
46
+ while (!ch.tx.tryPush(part)) {
47
+ if (ch.closed)
48
+ return;
49
+ await waitWritable(ch.tx, part.length, WAIT);
50
+ }
51
+ }
52
+ scope.postMessage({ type: "written", id, seq: w.seq });
53
+ }
54
+ }
55
+ catch (e) {
56
+ scope.postMessage({ type: "error", id, message: String(e?.message ?? e) });
57
+ }
58
+ finally {
59
+ ch.writing = false;
60
+ }
61
+ }
62
+ scope.onmessage = ({ data: m }) => {
63
+ const ch = channels.get(m.id);
64
+ switch (m.type) {
65
+ case "open":
66
+ channels.set(m.id, {
67
+ rx: new RingConsumer(m.memory, m.fromEngine),
68
+ tx: new RingProducer(m.memory, m.toEngine),
69
+ demand: false,
70
+ reading: false,
71
+ writes: [],
72
+ writing: false,
73
+ closed: false,
74
+ });
75
+ break;
76
+ case "demand":
77
+ if (ch) {
78
+ ch.demand = true;
79
+ void serveReads(m.id, ch);
80
+ }
81
+ break;
82
+ case "write":
83
+ if (ch) {
84
+ ch.writes.push({ seq: m.seq, data: new Uint8Array(m.data) });
85
+ void serveWrites(m.id, ch);
86
+ }
87
+ break;
88
+ case "close":
89
+ if (ch)
90
+ ch.closed = true;
91
+ channels.delete(m.id);
92
+ break;
93
+ case "stop":
94
+ // Before terminate(): the ring waits have no timeout, settle them first.
95
+ for (const c of channels.values())
96
+ c.closed = true;
97
+ channels.clear();
98
+ void settleWaits().then(() => scope.postMessage({ type: "stopped" }));
99
+ break;
100
+ }
101
+ };
102
+ //# sourceMappingURL=pump-worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pump-worker.js","sourceRoot":"","sources":["../../src/host/pump-worker.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,0FAA0F;AAC1F,0CAA0C;AAC1C,wFAAwF;AACxF,uGAAuG;AACvG,+EAA+E;AAC/E,kGAAkG;AAClG,2FAA2F;AAC3F,gDAAgD;AAChD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAYhG,MAAM,KAAK,GAAG,IAAwH,CAAC;AACvI,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAmB,CAAC;AAC5C,MAAM,IAAI,GAAG,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;AAEhC,KAAK,UAAU,UAAU,CAAC,EAAU,EAAE,EAAW;IAC/C,IAAI,EAAE,CAAC,OAAO;QAAE,OAAO;IACvB,EAAE,CAAC,OAAO,GAAG,IAAI,CAAC;IAClB,IAAI,CAAC;QACH,OAAO,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC;YAC/B,MAAM,KAAK,GAAG,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YAC/B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACvB,MAAM,YAAY,CAAC,EAAE,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;gBAChC,SAAS;YACX,CAAC;YACD,EAAE,CAAC,MAAM,GAAG,KAAK,CAAC;YAClB,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAA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AK,OAAO;YACV,IAAI,EAAE,EAAE,CAAC;gBACP,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBAC7D,KAAK,WAAW,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;YAC7B,CAAC;YACD,MAAM;QACR,KAAK,OAAO;YACV,IAAI,EAAE;gBAAE,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC;YACzB,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YACtB,MAAM;QACR,KAAK,MAAM;YACT,yEAAyE;YACzE,KAAK,MAAM,CAAC,IAAI,QAAQ,CAAC,MAAM,EAAE;gBAAE,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC;YACnD,QAAQ,CAAC,KAAK,EAAE,CAAC;YACjB,KAAK,WAAW,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;YACtE,MAAM;IACV,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { ByteChannel } from "./channel.js";
2
+ /** QMP (QEMU Machine Protocol) over the engine's qmp channel: one JSON object per line. */
3
+ export declare class QmpClient {
4
+ #private;
5
+ constructor(channel: ByteChannel);
6
+ /** Runs a QMP command (after the capabilities handshake) and resolves with its `return`. */
7
+ execute<T = unknown>(command: string, args?: Record<string, unknown>, timeoutMs?: number): Promise<T>;
8
+ }
@@ -0,0 +1,78 @@
1
+ import { WebPodError } from "../errors.js";
2
+ import { visibleDeadline } from "../timers.js";
3
+ /** QMP (QEMU Machine Protocol) over the engine's qmp channel: one JSON object per line. */
4
+ export class QmpClient {
5
+ #writer;
6
+ #pending = new Map();
7
+ #ready;
8
+ #nextId = 1;
9
+ #closed;
10
+ constructor(channel) {
11
+ this.#writer = channel.writable.getWriter();
12
+ let greeted;
13
+ const greeting = new Promise((r) => (greeted = r));
14
+ void (async () => {
15
+ const reader = channel.readable.getReader();
16
+ const decoder = new TextDecoder();
17
+ let buf = "";
18
+ for (;;) {
19
+ const { done, value } = await reader.read();
20
+ if (done)
21
+ break;
22
+ buf += decoder.decode(value, { stream: true });
23
+ let nl;
24
+ while ((nl = buf.indexOf("\n")) >= 0) {
25
+ const line = buf.slice(0, nl).trim();
26
+ buf = buf.slice(nl + 1);
27
+ if (!line)
28
+ continue;
29
+ let msg;
30
+ try {
31
+ msg = JSON.parse(line);
32
+ }
33
+ catch {
34
+ continue;
35
+ }
36
+ if (msg.QMP)
37
+ greeted();
38
+ else if (typeof msg.id === "number") {
39
+ const p = this.#pending.get(msg.id);
40
+ this.#pending.delete(msg.id);
41
+ if (msg.error)
42
+ p?.reject(new WebPodError("EIO", `QMP: ${msg.error.class ?? "error"}: ${msg.error.desc ?? ""}`));
43
+ else
44
+ p?.resolve(msg.return);
45
+ }
46
+ }
47
+ }
48
+ this.#closed = new WebPodError("DISPOSED", "QMP channel closed");
49
+ for (const p of this.#pending.values())
50
+ p.reject(this.#closed);
51
+ this.#pending.clear();
52
+ })().catch(() => { });
53
+ this.#ready = greeting.then(() => this.#send("qmp_capabilities").then(() => { }));
54
+ this.#ready.catch(() => { }); // surfaced by execute()
55
+ }
56
+ #send(execute, args) {
57
+ if (this.#closed)
58
+ return Promise.reject(this.#closed);
59
+ const id = this.#nextId++;
60
+ return new Promise((resolve, reject) => {
61
+ this.#pending.set(id, { resolve, reject });
62
+ const line = JSON.stringify(args ? { execute, arguments: args, id } : { execute, id }) + "\n";
63
+ this.#writer.write(new TextEncoder().encode(line)).catch(reject);
64
+ });
65
+ }
66
+ /** Runs a QMP command (after the capabilities handshake) and resolves with its `return`. */
67
+ async execute(command, args, timeoutMs = 30000) {
68
+ const timeout = visibleDeadline(timeoutMs, (note) => new WebPodError("EIO", `QMP ${command}: no answer in ${timeoutMs} ms${note}`));
69
+ try {
70
+ await Promise.race([this.#ready, timeout.promise]);
71
+ return (await Promise.race([this.#send(command, args), timeout.promise]));
72
+ }
73
+ finally {
74
+ timeout.cancel();
75
+ }
76
+ }
77
+ }
78
+ //# sourceMappingURL=qmp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"qmp.js","sourceRoot":"","sources":["../../src/host/qmp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAG/C,2FAA2F;AAC3F,MAAM,OAAO,SAAS;IACX,OAAO,CAA0C;IACjD,QAAQ,GAAG,IAAI,GAAG,EAA2E,CAAC;IAC9F,MAAM,CAAgB;IAC/B,OAAO,GAAG,CAAC,CAAC;IACZ,OAAO,CAAU;IAEjB,YAAY,OAAoB;QAC9B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;QAC5C,IAAI,OAAoB,CAAC;QACzB,MAAM,QAAQ,GAAG,IAAI,OAAO,CAAO,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC;QACzD,KAAK,CAAC,KAAK,IAAI,EAAE;YACf,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;YAC5C,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;YAClC,IAAI,GAAG,GAAG,EAAE,CAAC;YACb,SAAS,CAAC;gBACR,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;gBAC5C,IAAI,IAAI;oBAAE,MAAM;gBAChB,GAAG,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC/C,IAAI,EAAU,C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IAA8B,EAAE,SAAS,GAAG,KAAK;QAC3F,MAAM,OAAO,GAAG,eAAe,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,WAAW,CAAC,KAAK,EAAE,OAAO,OAAO,kBAAkB,SAAS,MAAM,IAAI,EAAE,CAAC,CAAC,CAAC;QACpI,IAAI,CAAC;YACH,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;YACnD,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAM,CAAC;QACjF,CAAC;gBAAS,CAAC;YACT,OAAO,CAAC,MAAM,EAAE,CAAC;QACnB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,85 @@
1
+ export declare const RING_MAGIC = 1196576855;
2
+ export declare const RING_VERSION = 1;
3
+ export declare const RING_HEADER_SIZE = 256;
4
+ export declare class RingError extends Error {
5
+ readonly kind: "corrupt" | "too-large" | "bad-header";
6
+ constructor(kind: "corrupt" | "too-large" | "bad-header", message: string);
7
+ }
8
+ /** Formats a ring with `capacity` data bytes (power of two, 64 B .. 1 GiB) at `byteOffset`. */
9
+ export declare function initRing(buffer: SharedArrayBuffer, byteOffset: number, capacity: number): void;
10
+ /** Allocates a SharedArrayBuffer holding one formatted ring (tests, host-owned rings). */
11
+ export declare function createRing(capacity: number): SharedArrayBuffer;
12
+ /** Producer half. Only one producer may exist per ring. */
13
+ export declare class RingProducer {
14
+ #private;
15
+ /** Attaches to a formatted ring at `byteOffset` (e.g. inside the engine's wasm memory). */
16
+ constructor(buffer: SharedArrayBuffer, byteOffset?: number);
17
+ get maxMessageLen(): number;
18
+ /** Pushes `msg`, waiting (without blocking the thread) while the ring is full. */
19
+ pushAsync(msg: Uint8Array, options?: WaitOptions): Promise<void>;
20
+ /** Pushes one message; false when the ring is full. Wakes a sleeping consumer. */
21
+ tryPush(msg: Uint8Array): boolean;
22
+ /** Announces that the producer will sleep until `len` bytes fit; false if they already do. */
23
+ prepareWait(len: number): boolean;
24
+ /**
25
+ * The word and value to wait on after a successful prepareWait: the tail that prepareWait saw
26
+ * the ring full at. Re-reading tail here would lose the wake-up when the consumer frees space
27
+ * in between (it clears producer_waiting and notifies before we sleep, then never again).
28
+ */
29
+ waitTarget(): [Int32Array, number, number];
30
+ }
31
+ /** Consumer half. Only one consumer may exist per ring. */
32
+ export declare class RingConsumer {
33
+ #private;
34
+ constructor(buffer: SharedArrayBuffer, byteOffset?: number);
35
+ hasMessages(): boolean;
36
+ /**
37
+ * The next message without copying, or null when empty. `bytes` views shared memory and is valid
38
+ * until `release()`, which consumes it and wakes a waiting producer (returns whether it did).
39
+ * Not releasing leaves the message in place: the next peek returns it again.
40
+ *
41
+ * head is cached and reloaded (and validated) only when the cached value shows the ring empty,
42
+ * so a producer that corrupts head is detected at that reload rather than on the very next peek.
43
+ * Records are validated either way, so a corrupt peer can never cause an out-of-bounds access.
44
+ */
45
+ peek(): RingMessage | null;
46
+ /** Pops (copies and releases) the next message, or null when empty. Wakes a waiting producer. */
47
+ tryPop(): Uint8Array | null;
48
+ /** Alias of `tryPop` (webpod_abi naming). */
49
+ pop(): Uint8Array | null;
50
+ /** Pops everything currently in the ring. */
51
+ drain(max?: number): Uint8Array[];
52
+ /** Announces that the consumer will sleep; false if messages arrived meanwhile. */
53
+ prepareWait(): boolean;
54
+ waitTarget(): [Int32Array, number, number];
55
+ }
56
+ export interface RingMessage {
57
+ readonly bytes: Uint8Array;
58
+ release(): boolean;
59
+ }
60
+ export interface WaitOptions {
61
+ /** Upper bound on one sleep: WebKit can lose waitAsync wake-ups (P1 #133). Default 50 ms. */
62
+ watchdogMs?: number;
63
+ signal?: AbortSignal;
64
+ }
65
+ /**
66
+ * Settles this thread's outstanding ring waits (a notify on each word) and resolves once they have
67
+ * settled, or after `timeoutMs`. A Worker calls it before it is terminated: the waits have no
68
+ * timeout, and iOS 26 hung `Worker.terminate()` on the page while one was pending (T49).
69
+ */
70
+ export declare function settleWaits(timeoutMs?: number): Promise<void>;
71
+ /** Waits until the consumer has a message (or the watchdog fires; callers just re-check). */
72
+ export declare function waitReadable(c: RingConsumer, options?: WaitOptions): Promise<void>;
73
+ /** Waits until `len` bytes may fit (or the watchdog fires). */
74
+ export declare function waitWritable(p: RingProducer, len: number, options?: WaitOptions): Promise<void>;
75
+ /** Names matching webpod_abi (`Ring::init` / `Ring::attach`), for engine-side code. */
76
+ export declare const Ring: {
77
+ init: typeof initRing;
78
+ /** Validates an existing ring at `byteOffset` (never rewrites its header). */
79
+ attach(buffer: SharedArrayBuffer, byteOffset?: number): {
80
+ producer: () => RingProducer;
81
+ consumer: () => RingConsumer;
82
+ };
83
+ };
84
+ export declare const waitForMessage: typeof waitReadable;
85
+ export declare const waitForSpace: typeof waitWritable;