@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,41 @@
1
+ import { type WaitOptions } from "./ring.js";
2
+ /** A ring inside a shared buffer (usually the engine's wasm memory). */
3
+ export interface RingRef {
4
+ buffer: SharedArrayBuffer;
5
+ byteOffset: number;
6
+ }
7
+ export interface ByteChannel {
8
+ readable: ReadableStream<Uint8Array>;
9
+ writable: WritableStream<Uint8Array>;
10
+ }
11
+ export interface RingByteChannel extends ByteChannel {
12
+ /** Ends both directions (the peer is gone): pending reads see EOF, writes are dropped. */
13
+ close(): void;
14
+ }
15
+ /**
16
+ * A byte stream over a ring pair (console, agent): ring message boundaries carry no meaning.
17
+ * Reads drain the ring in batches; writes split into ring-sized messages and wait for room.
18
+ */
19
+ export declare function ringByteChannel(toPeer: RingRef, fromPeer: RingRef, options?: WaitOptions): RingByteChannel;
20
+ /** The subset of `Worker` the pump needs (node worker_threads in tests). */
21
+ export interface PumpWorker {
22
+ postMessage(message: unknown, transfer?: Transferable[]): void;
23
+ terminate(): void;
24
+ onmessage: ((event: {
25
+ data: any;
26
+ }) => void) | null;
27
+ }
28
+ /**
29
+ * Byte channels whose rings are pumped by a dedicated Worker (dist/host/pump-worker.js), so the
30
+ * guest keeps its I/O speed while the page is hidden; the page only exchanges messages with it.
31
+ */
32
+ export declare class PumpChannels {
33
+ #private;
34
+ constructor(worker: PumpWorker);
35
+ open(id: string, memory: SharedArrayBuffer, toEngine: number, fromEngine: number): RingByteChannel;
36
+ /**
37
+ * Stops the pump Worker: it settles its ring waits first (they have no timeout, and iOS 26 hung
38
+ * `Worker.terminate()` while one was pending), then it is terminated, after at most 500 ms.
39
+ */
40
+ terminate(): void;
41
+ }
@@ -0,0 +1,168 @@
1
+ import { RingConsumer, RingProducer, waitReadable, waitWritable } from "./ring.js";
2
+ /**
3
+ * A byte stream over a ring pair (console, agent): ring message boundaries carry no meaning.
4
+ * Reads drain the ring in batches; writes split into ring-sized messages and wait for room.
5
+ */
6
+ export function ringByteChannel(toPeer, fromPeer, options = {}) {
7
+ const producer = new RingProducer(toPeer.buffer, toPeer.byteOffset);
8
+ const consumer = new RingConsumer(fromPeer.buffer, fromPeer.byteOffset);
9
+ const max = producer.maxMessageLen;
10
+ let closed = false;
11
+ const readable = new ReadableStream({
12
+ async pull(controller) {
13
+ for (;;) {
14
+ if (closed || options.signal?.aborted)
15
+ return controller.close();
16
+ const batch = consumer.drain();
17
+ if (batch.length > 0) {
18
+ for (const m of batch)
19
+ controller.enqueue(m);
20
+ return;
21
+ }
22
+ await waitReadable(consumer, options);
23
+ }
24
+ },
25
+ cancel() {
26
+ closed = true;
27
+ },
28
+ });
29
+ const writable = new WritableStream({
30
+ async write(chunk) {
31
+ for (let off = 0; off < chunk.length; off += max) {
32
+ const part = chunk.subarray(off, off + max);
33
+ while (!producer.tryPush(part)) {
34
+ if (closed || options.signal?.aborted)
35
+ return;
36
+ await waitWritable(producer, part.length, options);
37
+ }
38
+ }
39
+ },
40
+ close() {
41
+ closed = true;
42
+ },
43
+ abort() {
44
+ closed = true;
45
+ },
46
+ });
47
+ return {
48
+ readable,
49
+ writable,
50
+ close() {
51
+ closed = true;
52
+ },
53
+ };
54
+ }
55
+ /**
56
+ * Byte channels whose rings are pumped by a dedicated Worker (dist/host/pump-worker.js), so the
57
+ * guest keeps its I/O speed while the page is hidden; the page only exchanges messages with it.
58
+ */
59
+ export class PumpChannels {
60
+ #worker;
61
+ #handlers = new Map();
62
+ constructor(worker) {
63
+ this.#worker = worker;
64
+ worker.onmessage = ({ data }) => {
65
+ if (data.type === "stopped")
66
+ this.#stopped?.();
67
+ else
68
+ this.#handlers.get(data.id)?.(data);
69
+ };
70
+ }
71
+ open(id, memory, toEngine, fromEngine) {
72
+ const worker = this.#worker;
73
+ worker.postMessage({ type: "open", id, memory, toEngine, fromEngine });
74
+ let closed = false;
75
+ let error;
76
+ let onData;
77
+ const written = new Map();
78
+ let seq = 0;
79
+ const end = () => {
80
+ closed = true;
81
+ onData?.(null);
82
+ for (const w of written.values())
83
+ w();
84
+ written.clear();
85
+ };
86
+ this.#handlers.set(id, (m) => {
87
+ if (m.type === "data")
88
+ onData?.(m.chunks ?? []);
89
+ else if (m.type === "written") {
90
+ written.get(m.seq)?.();
91
+ written.delete(m.seq);
92
+ }
93
+ else if (m.type === "error") {
94
+ error = new Error(m.message);
95
+ end();
96
+ }
97
+ });
98
+ const readable = new ReadableStream({
99
+ pull: (controller) => new Promise((resolve) => {
100
+ if (closed) {
101
+ if (error)
102
+ controller.error(error);
103
+ else
104
+ controller.close();
105
+ return resolve();
106
+ }
107
+ onData = (chunks) => {
108
+ onData = undefined;
109
+ if (chunks === null) {
110
+ if (error)
111
+ controller.error(error);
112
+ else
113
+ controller.close();
114
+ }
115
+ else
116
+ for (const c of chunks)
117
+ controller.enqueue(new Uint8Array(c));
118
+ resolve();
119
+ };
120
+ worker.postMessage({ type: "demand", id });
121
+ }),
122
+ cancel: () => {
123
+ worker.postMessage({ type: "close", id });
124
+ end();
125
+ },
126
+ }, { highWaterMark: 0 });
127
+ const writable = new WritableStream({
128
+ write: (chunk) => new Promise((resolve) => {
129
+ if (closed)
130
+ return resolve();
131
+ const s = seq++;
132
+ written.set(s, resolve);
133
+ const data = chunk.slice().buffer;
134
+ worker.postMessage({ type: "write", id, seq: s, data }, [data]);
135
+ }),
136
+ close: () => undefined,
137
+ abort: () => {
138
+ worker.postMessage({ type: "close", id });
139
+ end();
140
+ },
141
+ });
142
+ return {
143
+ readable,
144
+ writable,
145
+ close: () => {
146
+ worker.postMessage({ type: "close", id });
147
+ end();
148
+ },
149
+ };
150
+ }
151
+ #stopped;
152
+ /**
153
+ * Stops the pump Worker: it settles its ring waits first (they have no timeout, and iOS 26 hung
154
+ * `Worker.terminate()` while one was pending), then it is terminated, after at most 500 ms.
155
+ */
156
+ terminate() {
157
+ if (this.#stopped)
158
+ return;
159
+ const timer = setTimeout(() => this.#stopped?.(), 500);
160
+ this.#stopped = () => {
161
+ clearTimeout(timer);
162
+ this.#stopped = () => { };
163
+ this.#worker.terminate();
164
+ };
165
+ this.#worker.postMessage({ type: "stop" });
166
+ }
167
+ }
168
+ //# sourceMappingURL=channel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel.js","sourceRoot":"","sources":["../../src/host/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAoB,YAAY,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAkBrG;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,MAAe,EAAE,QAAiB,EAAE,UAAuB,EAAE;IAC3F,MAAM,QAAQ,GAAG,IAAI,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;IACpE,MAAM,QAAQ,GAAG,IAAI,YAAY,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC;IACxE,MAAM,GAAG,GAAG,QAAQ,CAAC,aAAa,CAAC;IACnC,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,MAAM,QAAQ,GAAG,IAAI,cAAc,CAAa;QAC9C,KAAK,CAAC,IAAI,CAAC,UAAU;YACnB,SAAS,CAAC;gBACR,IAAI,MAAM,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO;oBAAE,OAAO,UAAU,CAAC,KAAK,EAAE,CAAC;gBACjE,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,EAAE,CAAC;gBAC/B,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACrB,KAAK,MAAM,CAAC,IAAI,KAAK;wBAAE,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;oBAC7C,OAAO;gBACT,CAAC;gBACD,MAAM,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;YACxC,CAAC;QACH,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,EAAE,MAAM,EAAE,CAAC,CAAC;IAC7C,CAAC;CACF"}
@@ -0,0 +1,264 @@
1
+ import { type ByteChannel, type PumpWorker } from "./channel.js";
2
+ import { QmpClient } from "./qmp.js";
3
+ /** Published next to an engine build (docs/host-engine.md). */
4
+ export interface EngineManifest {
5
+ abi: number;
6
+ engine: string;
7
+ arch: string;
8
+ /** Worker entry, relative to the manifest. */
9
+ worker: string;
10
+ workerType?: "classic" | "module";
11
+ threads?: boolean;
12
+ maxCpus?: number;
13
+ maxMemoryBytes?: number;
14
+ defaultCmdline?: string;
15
+ /** Engine files (wasm, data, kernel, initrd, ...), relative to the manifest; passed to the engine as absolute URLs. */
16
+ assets?: Record<string, string>;
17
+ /** Build identity (QEMU-Wasm: { commit, wasm sha256 }, or an id string); part of memory snapshot bindings. */
18
+ build?: Record<string, unknown> | string;
19
+ /** Guest kernel identity (QEMU-Wasm: { vmlinux, initramfs } sha256, or a string); part of snapshot bindings. */
20
+ kernel?: Record<string, unknown> | string;
21
+ }
22
+ /** The boot message PodHost sent the engine (docs/host-engine.md), without its assets. */
23
+ export interface BootMessage {
24
+ memory: string;
25
+ cpus: number;
26
+ kernelCmdline: string;
27
+ devices: DeviceSpec[];
28
+ incoming?: true;
29
+ tbSize?: number;
30
+ }
31
+ export type DeviceClass = "console" | "agent" | "net" | "blk" | "9p" | "qmp";
32
+ /** A device as requested in `boot`. */
33
+ export interface DeviceSpec {
34
+ slot: number;
35
+ class: DeviceClass;
36
+ id: string;
37
+ /** Ring data capacity, each direction. */
38
+ capacity: number;
39
+ name?: string;
40
+ serial?: string;
41
+ sizeBytes?: number;
42
+ readOnly?: boolean;
43
+ /** blk: logical (and physical) block size the guest must use; I/O is aligned to it. */
44
+ blockSize?: number;
45
+ /** blk: large requests may carry guest-RAM segment lists instead of inline data (both sides opt in). */
46
+ indirect?: boolean;
47
+ indirectMin?: number;
48
+ vnet_hdr?: false;
49
+ mac?: string;
50
+ /** 9p: largest 9P message; the mount tag is `id`. */
51
+ msize?: number;
52
+ }
53
+ /** A device's rings as reported by the engine in `channels`: byte offsets into its memory. */
54
+ export interface ChannelInfo {
55
+ slot: number;
56
+ class: DeviceClass;
57
+ id: string;
58
+ toEngine: number;
59
+ fromEngine: number;
60
+ capacity: number;
61
+ }
62
+ export interface EngineExit {
63
+ reason: "exit" | "error" | "terminated";
64
+ code?: number;
65
+ message?: string;
66
+ }
67
+ /** The subset of `Worker` the host uses (lets tests run the engine in node worker_threads). */
68
+ export interface EngineWorker {
69
+ postMessage(message: unknown, transfer?: Transferable[]): void;
70
+ terminate(): void;
71
+ onmessage: ((event: {
72
+ data: unknown;
73
+ }) => void) | null;
74
+ onerror: ((event: {
75
+ message?: string;
76
+ }) => void) | null;
77
+ }
78
+ /**
79
+ * Host side of one device: `memory` is the engine's shared wasm memory; the host produces into the
80
+ * ring at `toEngine` and consumes the one at `fromEngine`. Can be posted to another Worker.
81
+ */
82
+ export interface DeviceRings {
83
+ memory: SharedArrayBuffer;
84
+ toEngine: number;
85
+ fromEngine: number;
86
+ }
87
+ export interface NetAttachment {
88
+ mac?: string;
89
+ attach(rings: DeviceRings): void | Promise<void>;
90
+ /** Called once the engine is gone. */
91
+ detach?(): void | Promise<void>;
92
+ }
93
+ export interface BlockAttachment {
94
+ serial: string;
95
+ sizeBytes: number;
96
+ readOnly?: boolean;
97
+ /** Default 4096: the FS Worker's BlkServer only accepts block-aligned requests. */
98
+ blockSize?: number;
99
+ /** The server accepts indirect requests (docs/design/blk-indirect.md). */
100
+ indirect?: boolean;
101
+ indirectMin?: number;
102
+ attach(rings: DeviceRings): void | Promise<void>;
103
+ detach?(): void | Promise<void>;
104
+ }
105
+ /** A 9P share (virtio-9p, PDUs passed through the ring); mount tag = `id`. */
106
+ export interface ShareAttachment {
107
+ id: string;
108
+ msize: number;
109
+ /** Where the guest mounts it (the host asks the agent once it answers). */
110
+ mountPoint?: string;
111
+ /**
112
+ * Subdirectories (relative to `mountPoint`) the guest keeps on its data disk instead, bind
113
+ * mounted over the share (e.g. node_modules: many small files the page does not need).
114
+ */
115
+ localPaths?: string[];
116
+ attach(rings: DeviceRings): void | Promise<void>;
117
+ detach?(): void | Promise<void>;
118
+ }
119
+ export interface PodHostOptions {
120
+ manifestUrl: string;
121
+ memoryBytes: number;
122
+ cpus: number;
123
+ /** Appended to the manifest's default command line. */
124
+ cmdline?: string;
125
+ /** The whole kernel command line instead (a boot snapshot's), replacing default + cmdline. */
126
+ kernelCmdline?: string;
127
+ net?: NetAttachment;
128
+ blocks?: BlockAttachment[];
129
+ shares?: ShareAttachment[];
130
+ /** Defaults to `new Worker(url, { type })`. */
131
+ createWorker?: (url: string, type: "classic" | "module") => EngineWorker;
132
+ /** Defaults to `fetch`. */
133
+ loadManifest?: (url: string) => Promise<EngineManifest>;
134
+ /** Engine log lines (QEMU stderr, ...). */
135
+ onLog?: (level: string, text: string) => void;
136
+ /** Milliseconds to wait for the engine's `channels` (visible time: a hidden page pauses it). Default 60000. */
137
+ bootTimeoutMs?: number;
138
+ /** Start paused, waiting for an incoming migration (memory snapshot restore). */
139
+ incoming?: boolean;
140
+ /** TCG translation buffer in MiB (QEMU-Wasm: committed whole, part of the engine's memory). Engine default when unset. */
141
+ tbSize?: number;
142
+ /** An engine Worker started ahead with `PodHost.prepare` (its manifest replaces manifestUrl's). */
143
+ prepared?: PreparedEngine;
144
+ /**
145
+ * Where the console/agent/qmp rings are pumped: "worker" (default where Workers exist) keeps the
146
+ * guest's I/O at full speed while the page is hidden (main-thread timers drop to ~1 Hz); "main"
147
+ * pumps on the calling thread.
148
+ */
149
+ pumps?: "worker" | "main";
150
+ /** Defaults to `new Worker(new URL("./pump-worker.js", import.meta.url), { type: "module" })`. */
151
+ createPumpWorker?: () => PumpWorker;
152
+ }
153
+ /** Guest RAM inside the engine's memory (in place; x-ignore-shared snapshots). */
154
+ export interface RamBlock {
155
+ id: string;
156
+ offset: number;
157
+ size: number;
158
+ }
159
+ /** A boot milestone: milliseconds since `PodHost.start` was called. */
160
+ export interface BootEvent {
161
+ at: number;
162
+ event: string;
163
+ }
164
+ /** Starts an engine Worker and attaches the host ends of its device rings (docs/host-engine.md). */
165
+ export declare class PodHost {
166
+ #private;
167
+ /** Boot milestones (manifest, worker, channels, started, attached; the backend adds console and agent events). */
168
+ readonly timeline: BootEvent[];
169
+ readonly manifest: EngineManifest;
170
+ readonly devices: (DeviceSpec & ChannelInfo)[];
171
+ readonly memory: SharedArrayBuffer;
172
+ /** Guest agent byte stream (org.webpod.agent). */
173
+ readonly agent: ByteChannel;
174
+ /** Guest console byte stream. */
175
+ readonly console: ByteChannel;
176
+ /** QMP monitor, when the engine provides one. */
177
+ readonly qmp: QmpClient | null;
178
+ /** Guest RAM blocks in `memory` (empty until the engine announces them, possibly after channels). */
179
+ readonly ram: RamBlock[];
180
+ /** Resolves once guest RAM has been announced. */
181
+ readonly ramReady: Promise<RamBlock[]>;
182
+ /** Resolves when the engine reports the guest running. */
183
+ readonly started: Promise<void>;
184
+ /** Resolves when the engine stops, for whatever reason. */
185
+ readonly exited: Promise<EngineExit>;
186
+ private constructor();
187
+ /** Reads, writes or removes a file in the engine's own (emscripten) file system: snapshot device state. */
188
+ engineFs(op: "read" | "write" | "unlink", path: string, data?: Uint8Array): Promise<Uint8Array | undefined>;
189
+ /** @internal */
190
+ _fsResult(msg: {
191
+ id: number;
192
+ data?: Uint8Array;
193
+ error?: string;
194
+ }): void;
195
+ /** The boot message sent to the engine (without assets): memory snapshot bindings are computed from it. */
196
+ get boot(): BootMessage;
197
+ /** The engine's QEMU arguments for this boot, from its `qemuArgs` module (`buildArgs(boot).args`). */
198
+ engineArgs(): Promise<string[]>;
199
+ /**
200
+ * 9P requests the engine's 9p device has taken from the guest and not answered yet (stats v4),
201
+ * undefined when the engine does not say. A memory snapshot waits for none.
202
+ */
203
+ p9Inflight(): number | undefined;
204
+ /** Records a boot milestone. */
205
+ mark(event: string): void;
206
+ rings(slot: number): DeviceRings;
207
+ /**
208
+ * Starts an engine Worker ahead of boot: engines that support `{ type: "prepare" }` compile and
209
+ * instantiate the engine, load the guest package and start their threads, so
210
+ * `start({ prepared })` only runs QEMU's main. Others just boot once started. Size it like the
211
+ * pod (`memoryBytes`, `tbSize`); a larger boot grows the engine's memory.
212
+ */
213
+ static prepare(options: PrepareOptions): Promise<PreparedEngine>;
214
+ static start(options: PodHostOptions): Promise<PodHost>;
215
+ /**
216
+ * Asks the engine to stop; terminates the Worker if it has not exited after `timeoutMs`. Resolves
217
+ * once the devices are detached too: the FS Worker has flushed the disks and the workspace (or
218
+ * its grace period ran out: DeviceWorker.stop, 2 s), so the page may close right after; at most
219
+ * DETACH_TIMEOUT_MS for all of them.
220
+ */
221
+ stop(timeoutMs?: number): Promise<EngineExit>;
222
+ /** Hard stop: the engine Worker (and its pthread Workers) is terminated. */
223
+ terminate(): void;
224
+ }
225
+ type PreparedEvent = {
226
+ message: {
227
+ data: unknown;
228
+ };
229
+ } | {
230
+ error: {
231
+ message?: string;
232
+ };
233
+ };
234
+ export interface PrepareOptions {
235
+ manifestUrl: string;
236
+ memoryBytes: number;
237
+ tbSize?: number;
238
+ createWorker?: (url: string, type: "classic" | "module") => EngineWorker;
239
+ loadManifest?: (url: string) => Promise<EngineManifest>;
240
+ }
241
+ /**
242
+ * An engine Worker started by `PodHost.prepare`, waiting for `PodHost.start({ prepared })`. It is
243
+ * used by one start; `terminate()` releases one that will not be.
244
+ */
245
+ export declare class PreparedEngine {
246
+ #private;
247
+ readonly manifest: EngineManifest;
248
+ /** Resolves once the engine reports `prepared`; never for engines without prepare. */
249
+ readonly ready: Promise<void>;
250
+ constructor(manifest: EngineManifest, assets: Record<string, string>, worker: EngineWorker);
251
+ /**
252
+ * @internal The Worker for the one PodHost.start that boots it, and what it posted so far
253
+ * (recorded until that host installs its own handlers, which then replays them).
254
+ */
255
+ take(): {
256
+ manifest: EngineManifest;
257
+ assets: Record<string, string>;
258
+ worker: EngineWorker;
259
+ events: readonly PreparedEvent[];
260
+ };
261
+ /** Stops the Worker unless a PodHost took it over. */
262
+ terminate(): void;
263
+ }
264
+ export {};