@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.
- package/README.md +246 -2
- package/dist/agent/backend.d.ts +68 -0
- package/dist/agent/backend.js +417 -0
- package/dist/agent/backend.js.map +1 -0
- package/dist/agent/fs.d.ts +45 -0
- package/dist/agent/fs.js +157 -0
- package/dist/agent/fs.js.map +1 -0
- package/dist/agent/protocol.d.ts +236 -0
- package/dist/agent/protocol.js +368 -0
- package/dist/agent/protocol.js.map +1 -0
- package/dist/coi-serviceworker.d.ts +0 -0
- package/dist/coi-serviceworker.js +33 -0
- package/dist/coi-serviceworker.js.map +1 -0
- package/dist/coi.d.ts +19 -0
- package/dist/coi.js +54 -0
- package/dist/coi.js.map +1 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +9 -0
- package/dist/errors.js.map +1 -0
- package/dist/features.d.ts +37 -0
- package/dist/features.js +125 -0
- package/dist/features.js.map +1 -0
- package/dist/fs/tar.d.ts +21 -0
- package/dist/fs/tar.js +332 -0
- package/dist/fs/tar.js.map +1 -0
- package/dist/fs/workspace.d.ts +19 -0
- package/dist/fs/workspace.js +233 -0
- package/dist/fs/workspace.js.map +1 -0
- package/dist/fs-router.d.ts +8 -0
- package/dist/fs-router.js +50 -0
- package/dist/fs-router.js.map +1 -0
- package/dist/host/backend.d.ts +115 -0
- package/dist/host/backend.js +475 -0
- package/dist/host/backend.js.map +1 -0
- package/dist/host/binding.d.ts +33 -0
- package/dist/host/binding.js +72 -0
- package/dist/host/binding.js.map +1 -0
- package/dist/host/channel.d.ts +41 -0
- package/dist/host/channel.js +168 -0
- package/dist/host/channel.js.map +1 -0
- package/dist/host/controller.d.ts +264 -0
- package/dist/host/controller.js +484 -0
- package/dist/host/controller.js.map +1 -0
- package/dist/host/devices.d.ts +134 -0
- package/dist/host/devices.js +317 -0
- package/dist/host/devices.js.map +1 -0
- package/dist/host/pump-worker.d.ts +1 -0
- package/dist/host/pump-worker.js +102 -0
- package/dist/host/pump-worker.js.map +1 -0
- package/dist/host/qmp.d.ts +8 -0
- package/dist/host/qmp.js +78 -0
- package/dist/host/qmp.js.map +1 -0
- package/dist/host/ring.d.ts +85 -0
- package/dist/host/ring.js +389 -0
- package/dist/host/ring.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/net/client.d.ts +19 -0
- package/dist/net/client.js +230 -0
- package/dist/net/client.js.map +1 -0
- package/dist/net/options.d.ts +65 -0
- package/dist/net/options.js +8 -0
- package/dist/net/options.js.map +1 -0
- package/dist/net/preview.d.ts +27 -0
- package/dist/net/preview.js +233 -0
- package/dist/net/preview.js.map +1 -0
- package/dist/net/protocol.d.ts +65 -0
- package/dist/net/protocol.js +13 -0
- package/dist/net/protocol.js.map +1 -0
- package/dist/pod.d.ts +43 -0
- package/dist/pod.js +206 -0
- package/dist/pod.js.map +1 -0
- package/dist/preview-relay.d.ts +0 -0
- package/dist/preview-relay.html +4 -0
- package/dist/preview-relay.js +107 -0
- package/dist/preview-relay.js.map +1 -0
- package/dist/preview-serviceworker.d.ts +0 -0
- package/dist/preview-serviceworker.js +405 -0
- package/dist/preview-serviceworker.js.map +1 -0
- package/dist/streams.d.ts +14 -0
- package/dist/streams.js +58 -0
- package/dist/streams.js.map +1 -0
- package/dist/testing/index.d.ts +14 -0
- package/dist/testing/index.js +354 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/timers.d.ts +18 -0
- package/dist/timers.js +68 -0
- package/dist/timers.js.map +1 -0
- package/dist/types.d.ts +163 -0
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -0
- package/dist/units.d.ts +2 -0
- package/dist/units.js +31 -0
- package/dist/units.js.map +1 -0
- package/package.json +53 -4
package/README.md
CHANGED
|
@@ -1,3 +1,247 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @webpodjs/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
High-level JS API for WebPod: `Pod.boot()`, `pod.spawn()`, `pod.fs`, feature detection and the
|
|
4
|
+
cross-origin isolation fallback.
|
|
5
|
+
|
|
6
|
+
`Pod.boot({ engine: { manifestUrl, fs, net } })` boots a real pod on the QEMU-Wasm engine ("Engine"
|
|
7
|
+
below); in a Vite app, `bootPod()` from `@webpodjs/vite-plugin` wires the engine, the FS and net Workers
|
|
8
|
+
and the image for you (`docs/quickstart.md`). Without `engine` or `backend`, `Pod.boot()` rejects
|
|
9
|
+
with `WebPodError` code `ENGINE_UNAVAILABLE`. `createMemoryBackend()` from `@webpodjs/sdk/testing`
|
|
10
|
+
runs the API without an engine, for tests.
|
|
11
|
+
|
|
12
|
+
## Build artifacts (contract for E2E)
|
|
13
|
+
|
|
14
|
+
`npm run build` (from the repo root or `packages/sdk`) emits plain ES modules with no runtime
|
|
15
|
+
dependencies, loadable directly with `<script type="module">`:
|
|
16
|
+
|
|
17
|
+
| Path | Purpose |
|
|
18
|
+
|---|---|
|
|
19
|
+
| `packages/sdk/dist/index.js` | Main entry (`@webpodjs/sdk`) |
|
|
20
|
+
| `packages/sdk/dist/testing/index.js` | `createMemoryBackend()` (`@webpodjs/sdk/testing`) |
|
|
21
|
+
| `packages/sdk/dist/coi-serviceworker.js` | Classic service worker for mode `coi-sw`; serve it from the page's origin (`?preview` also loads the preview worker) |
|
|
22
|
+
| `packages/sdk/dist/preview-serviceworker.js` | Classic service worker serving `pod.net.expose()` previews; serve it next to the page (or load it from `coi-serviceworker.js?preview`) |
|
|
23
|
+
|
|
24
|
+
```html
|
|
25
|
+
<script type="module">
|
|
26
|
+
import { Pod, detectFeatures, ensureCrossOriginIsolation } from "/sdk/dist/index.js";
|
|
27
|
+
import { createMemoryBackend } from "/sdk/dist/testing/index.js";
|
|
28
|
+
|
|
29
|
+
console.log(await detectFeatures()); // RT-004
|
|
30
|
+
const pod = await Pod.boot({ backend: createMemoryBackend() });
|
|
31
|
+
console.log(pod.mode, await pod.exec("echo", ["hi"])); // "iso" | "noiso", { code: 0, stdout: "hi\n", ... }
|
|
32
|
+
</script>
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Streams on iOS
|
|
36
|
+
|
|
37
|
+
iOS Safari (18.6, 26.0) cannot `for await` a `ReadableStream`. The SDK adds async iteration to
|
|
38
|
+
the streams it returns (`stdout`, `stderr`, `console.readable`, `fs.createReadStream`,
|
|
39
|
+
`net.connect` sockets), so `for await (const c of p.stdout)` works everywhere. For any other
|
|
40
|
+
stream use `for await (const c of iterate(stream))` (exported by the SDK).
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const pod = await Pod.boot({ image: "webpod/node", memory: "1GiB", cpus: 2, persist: "my-project" });
|
|
46
|
+
pod.mode; // "iso" (cross-origin isolated) or "noiso"
|
|
47
|
+
|
|
48
|
+
const p = pod.spawn("node", ["index.js"], { cwd: "/workspace", env: { FOO: "1" }, pty: { cols: 80, rows: 24 } });
|
|
49
|
+
p.stdout.pipeTo(terminal.writable); // ReadableStream<Uint8Array>; stderr separate unless pty
|
|
50
|
+
const w = p.stdin.getWriter(); // WritableStream<Uint8Array>, or pass { stdin: stream | bytes | string }
|
|
51
|
+
await p.resize({ cols: 120, rows: 40 });
|
|
52
|
+
await p.kill("SIGINT");
|
|
53
|
+
await p.exit; // { code: number | null, signal: Signal | null }
|
|
54
|
+
|
|
55
|
+
await pod.exec("ls", ["-l"]); // { code, signal, stdout, stderr } as text
|
|
56
|
+
await pod.fs.writeFile("/workspace/a.txt", "hi");
|
|
57
|
+
await pod.fs.readFile("/workspace/a.txt", "utf8");
|
|
58
|
+
await pod.fs.readdir("/"); await pod.fs.mkdir("/x", { recursive: true }); await pod.fs.stat("/x"); await pod.fs.rm("/x", { recursive: true });
|
|
59
|
+
await pod.dispose(); // further calls fail with DISPOSED
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Errors are `WebPodError` with a `code` (`ENOENT`, `EEXIST`, `ENOTDIR`, `EISDIR`, `ENOTEMPTY`, `EINVAL`,
|
|
63
|
+
`ENGINE_UNAVAILABLE`, `UNSUPPORTED`, `DISPOSED`).
|
|
64
|
+
|
|
65
|
+
Isolation: `isolation: "auto"` (default) picks `iso` when `crossOriginIsolated` and `noiso` otherwise
|
|
66
|
+
(`cpus > 1` requires `iso`); `"require"` fails without isolation. For hosts that cannot send
|
|
67
|
+
COOP/COEP, call `ensureCrossOriginIsolation({ serviceWorkerUrl: "/coi-serviceworker.js" })` before
|
|
68
|
+
booting: it registers the worker, reloads once, and resolves `{ isolated, via | reason }`.
|
|
69
|
+
|
|
70
|
+
## Inbound networking (previews, `pod.fetch`, `pod.net.connect`)
|
|
71
|
+
|
|
72
|
+
Needs a backend with `net` (a `NetPortSink` that hands MessagePorts to the net Worker, where
|
|
73
|
+
webpod-net's `InboundBridge` serves them).
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
const url = await pod.net.expose(5173); // https://app.example/__webpod/<pod.id>/5173/
|
|
77
|
+
iframe.src = url; // served by the preview service worker
|
|
78
|
+
// On a preview origin of its own: the guest's pages cannot reach the app.
|
|
79
|
+
const isolated = await pod.net.expose(5173, { relay: "https://{id}.preview.example-usercontent.com/preview-relay.html" });
|
|
80
|
+
const r = await pod.fetch("http://localhost:8000/api", { method: "POST", body: "{}" });
|
|
81
|
+
const sock = await pod.net.connect(6379); // { readable, writable, abort() }; ECONNREFUSED if closed
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Preview worker behaviour: absolute-path subresources and links of a preview document are routed to
|
|
85
|
+
the same pod/port (by client id, then Referer); `Location` headers pointing at the guest are
|
|
86
|
+
rewritten; requests ask the guest for identity encoding (synthetic responses are not decoded);
|
|
87
|
+
`Set-Cookie` cannot be set on synthetic responses and is dropped; responses carry
|
|
88
|
+
`Cross-Origin-Embedder-Policy` (`?coep=require-corp|credentialless|none`) so they load inside
|
|
89
|
+
isolated pages. When the page that owns the pod is gone, previews answer 503 `Pod not running`.
|
|
90
|
+
By default previews run on the app's origin, with the app's privileges: only preview code you
|
|
91
|
+
trust there. With `relay` they run on a preview origin of its own, which serves
|
|
92
|
+
`preview-relay.html`, `preview-relay.js` and `preview-serviceworker.js` from this package's `dist`
|
|
93
|
+
and a `webpod-preview.json` listing the app's origins (`{ "hosts": [...] }`); the SDK embeds the
|
|
94
|
+
relay in a hidden iframe and serves the previews' requests through it, and `{id}` in the URL
|
|
95
|
+
gives each preview (pod and port) an origin of its own (`docs/deploy.md`, "Previews and isolation").
|
|
96
|
+
Only one service worker can control a scope: with `coi-sw`, register
|
|
97
|
+
`coi-serviceworker.js?preview` instead of two workers, e.g.
|
|
98
|
+
`navigator.serviceWorker.register("/coi-serviceworker.js?preview&coep=require-corp")`.
|
|
99
|
+
|
|
100
|
+
WebSockets: HTML documents served through the preview get a small shim (`__webpod/__shim.js`)
|
|
101
|
+
that sends WebSockets aimed at the preview origin (absolute paths such as Vite's HMR endpoint) or
|
|
102
|
+
`localhost:PORT` to the guest; other URLs use the browser's WebSocket.
|
|
103
|
+
|
|
104
|
+
## Agent backend
|
|
105
|
+
|
|
106
|
+
`createAgentBackend({ transport: { readable, writable }, mounts? })` speaks the guest agent protocol
|
|
107
|
+
(docs/agent-protocol.md) over any byte stream, e.g. the `org.webpod.agent` channel webpod-host
|
|
108
|
+
exposes, and returns a `PodBackend` for `Pod.boot({ backend })`. Output is acknowledged only as the
|
|
109
|
+
page reads it, so a slow consumer backpressures the guest process; stdin writes wait for agent
|
|
110
|
+
credit. Spawn failures reject `exit`/`pid` with `ENOENT`, `EACCES`, ... or `SPAWN_FAILED`.
|
|
111
|
+
|
|
112
|
+
Pass `caCertificate` (the Pod CA PEM from webpod-net, never the key) to have the agent append it to
|
|
113
|
+
every guest trust store and CA env var before the backend resolves (fast path, milliseconds);
|
|
114
|
+
`backend.caSettled` resolves once the background tools (update-ca-certificates, JDK keytool) are
|
|
115
|
+
done, before which Java may not trust it. `backend.installCa(pem)` does the same later and returns
|
|
116
|
+
`{ bundle, warnings, settled }`.
|
|
117
|
+
|
|
118
|
+
`pod.fs` then covers the whole guest file system through the agent. `mounts` routes subtrees to
|
|
119
|
+
other `PodFs` implementations (longest prefix wins, paths passed through unchanged), e.g.
|
|
120
|
+
`{ "/workspace": opfsWorkspaceFs }` for the OPFS-backed shared workspace; `createFsRouter(root,
|
|
121
|
+
mounts)` is exported for custom setups (a `null` mount gives a subtree back to the agent). Renames
|
|
122
|
+
across mounts fail with `EXDEV`. The workspace's local paths (FS Worker `workspace.localPaths`,
|
|
123
|
+
default `["node_modules"]`) stay on the guest's data disk, bind mounted over the share after it is
|
|
124
|
+
mounted, and `pod.fs` reaches them through the agent (`docs/design/workspace-fs.md` §5).
|
|
125
|
+
|
|
126
|
+
`writeFile` resolves once the file is on storage (`durable`, default true): the guest fsyncs it and
|
|
127
|
+
its directory, the workspace syncs, so a reload right after keeps it; `{ durable: false }` leaves
|
|
128
|
+
it to the guest's writeback (a few seconds). On the guest's data disk a durable write costs about
|
|
129
|
+
37 ms more per file under emulation, so write many files with `{ durable: false }` and call
|
|
130
|
+
`pod.fs.sync()` once at the end (1000 files of 1 KiB: 48 s durable, 14 s this way); `importTar`
|
|
131
|
+
does so. `/workspace` writes are cheap either way. The pod also flushes the guest and the FS Worker
|
|
132
|
+
when the page is hidden or goes away (`pagehide`), and `pod.dispose()` resolves once the FS Worker
|
|
133
|
+
has flushed.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
for (const [path, text] of files) await pod.fs.writeFile(path, text, { durable: false });
|
|
137
|
+
await pod.fs.sync(); // all of them on storage
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
await pod.fs.writeFile("/workspace/big.bin", readableStream); // chunked, any size
|
|
142
|
+
for await (const chunk of pod.fs.createReadStream("/var/log/x")) { /* ... */ }
|
|
143
|
+
await pod.fs.lstat("/l"); await pod.fs.symlink("t", "/l"); await pod.fs.readlink("/l");
|
|
144
|
+
await pod.fs.rename("/a", "/b"); await pod.fs.chmod("/b", 0o755);
|
|
145
|
+
const w = await pod.fs.watch("/workspace", (events) => { // [{ type: "create" | "modify" | "delete" | "overflow", path }]
|
|
146
|
+
});
|
|
147
|
+
await w.close();
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Tar archives of any `PodFs` tree (POSIX ustar with pax headers: modes, mtimes, symlinks, long and
|
|
151
|
+
UTF-8 names; tar's GNU and POSIX formats read back), e.g. a project download or a copy into
|
|
152
|
+
another pod:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
import { exportTar, importTar } from "@webpodjs/sdk";
|
|
156
|
+
const tar = await new Response(exportTar(pod.fs, "/workspace")).blob(); // files read as it streams
|
|
157
|
+
await importTar(other.fs, tar, "/workspace"); // -> { files, dirs, symlinks, skipped }; skips ".."
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Engine (webpod-host)
|
|
161
|
+
|
|
162
|
+
`Pod.boot({ engine: { manifestUrl } })` starts the engine Worker named by the manifest, attaches the
|
|
163
|
+
console and agent rings it allocates in its own memory (docs/host-engine.md), and connects to the
|
|
164
|
+
guest agent; `pod.console` exposes the guest console as byte streams. Requires cross-origin
|
|
165
|
+
isolation (`SharedArrayBuffer`). `engine.net` / `engine.blocks` attach device Workers; the
|
|
166
|
+
lower-level `PodHost` and the ring classes (`RingProducer`, `RingConsumer`, `waitReadable`, ...)
|
|
167
|
+
are exported for those Workers.
|
|
168
|
+
|
|
169
|
+
With a shared workspace (`fsWorker(url, { workspace: { mountPoint } })`), the guest mounts it when the
|
|
170
|
+
agent first answers (after a cold boot or a resume), not from its init, so build-time boot
|
|
171
|
+
snapshots are taken before any 9p attach; `Pod.boot` resolves once it is mounted.
|
|
172
|
+
|
|
173
|
+
The engine Worker starts while the FS Worker opens its volumes (`engine.prepare`, default true):
|
|
174
|
+
engines that support `prepare` compile and instantiate the engine and start its threads meanwhile,
|
|
175
|
+
so the boot itself only runs QEMU's main (about 0.1–0.3 s off a warm resume). To start it earlier,
|
|
176
|
+
e.g. on page load, pass `engine.prepared: PodHost.prepare({ manifestUrl, memoryBytes })`; a prepared
|
|
177
|
+
engine serves one boot.
|
|
178
|
+
|
|
179
|
+
## Hidden pages
|
|
180
|
+
|
|
181
|
+
Browsers throttle hidden pages. Chromium and Firefox run a hidden page's main-thread timers at
|
|
182
|
+
about 1 Hz, so the host pumps the console, agent and QMP rings in a Worker (`engine.pumps`,
|
|
183
|
+
default `"worker"`) and the guest keeps its speed. Safari freezes a background tab's JavaScript
|
|
184
|
+
entirely after about 7 s (a covered window is only throttled): the pod, the agent connection and
|
|
185
|
+
pending commands stop until the tab is visible again. That is WebKit platform behaviour; nothing in
|
|
186
|
+
the page can keep running.
|
|
187
|
+
|
|
188
|
+
Boot and command timeouts (`engine.engineTimeoutMs`, `engine.bootTimeoutMs`, agent, QMP and device
|
|
189
|
+
Worker requests) count visible time only, so a pod booted or used in a background tab does not fail
|
|
190
|
+
for time it never got. A timeout that fires after the page was hidden during the wait says so:
|
|
191
|
+
`guest agent did not say hello (page hidden for 37 s meanwhile, not counted)`.
|
|
192
|
+
|
|
193
|
+
## Guest networking (egress)
|
|
194
|
+
|
|
195
|
+
`engine.net: netWorker("/net-worker/worker.js")` attaches the net Worker (`packages/net-worker`,
|
|
196
|
+
README there): DHCP/DNS gateway, transparent HTTP(S) through `fetch()` with TLS interception,
|
|
197
|
+
WebSocket, and optionally a CORS proxy and an L4 relay. `Pod.boot({ network })` configures it:
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
const pod = await Pod.boot({
|
|
201
|
+
persist: "my-project", // also keys the pod CA (stable fingerprint)
|
|
202
|
+
engine: { manifestUrl, net: netWorker("/net-worker/worker.js") },
|
|
203
|
+
network: {
|
|
204
|
+
mode: "browser", // or "none": no egress unless a route allows it
|
|
205
|
+
corsProxy: "https://cors.example/", // or { url, token } (docs/cors-proxy-protocol.md)
|
|
206
|
+
relay: { url: "wss://relay.example/v1/connect", getToken: () => myBackend.relayToken() },
|
|
207
|
+
routes: [{ host: "*.pypi.org", via: "cors-proxy" }, { host: "10.0.0.0/8", via: "deny" }],
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
The pod CA (certificate only) comes from the net Worker and becomes the agent's default
|
|
213
|
+
`caCertificate`, so guest tools trust intercepted HTTPS. `getToken` runs in the page; the Worker
|
|
214
|
+
asks for a token per relay connection (docs/relay-protocol.md, "Issuing tokens").
|
|
215
|
+
|
|
216
|
+
Pods of one page on the same LAN reach each other by name (NET-506):
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
const db = await Pod.boot({ engine: { manifestUrl, net: netWorker(url) }, network: { lan: "dev", hostname: "db" } });
|
|
220
|
+
const app = await Pod.boot({ engine: { manifestUrl, net: netWorker(url) }, network: { lan: { name: "dev", allow: ["db:5432"] }, hostname: "app" } });
|
|
221
|
+
await app.exec("sh", ["-c", "psql -h db ..."]); // any protocol; app may only open db:5432
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Each member gets a stable /26 in 198.19.0.0/16 from its host name (packages/net-worker README).
|
|
225
|
+
|
|
226
|
+
## Snapshots
|
|
227
|
+
|
|
228
|
+
With an FS Worker (`engine.fs`), `pod.snapshot(name, meta?)` freezes the guest's data file system
|
|
229
|
+
(`engine.freezePaths`, default `["/data"]`), snapshots the data disk and `/workspace` (content
|
|
230
|
+
addressed, incremental), and thaws. `pod.snapshots.list() / delete(name) / export(name)` (one
|
|
231
|
+
`File`) `/ import(file, name?)`. Restore by booting with
|
|
232
|
+
`fsWorker(url, { ...spec, restore: name })`; snapshots taken on another image are refused.
|
|
233
|
+
|
|
234
|
+
A published boot snapshot (`<image>/boot/`, `webpod-build snapshot`) makes even a pod's first boot
|
|
235
|
+
warm: `engine: { bootSnapshot: "/images/base/boot/", fs: () => fsWorker(url, spec) }`. When the FS
|
|
236
|
+
Worker reports a data disk never written, the SDK imports the snapshot (only the chunks it lacks),
|
|
237
|
+
restores the data disk from it and resumes; any failure boots cold with a fresh FS Worker. Memory,
|
|
238
|
+
vCPUs and the kernel command line left to their defaults come from its `boot.json` on every boot of
|
|
239
|
+
the pod; the data disk must be at least as large as the snapshot's, and the devices the same (NIC:
|
|
240
|
+
`engine.net`; workspace share: needs a snapshot with it).
|
|
241
|
+
|
|
242
|
+
## Tests
|
|
243
|
+
|
|
244
|
+
`npm test` runs `node --test` against `dist/` (build first). `test/host.test.js` boots a mock engine
|
|
245
|
+
Worker (node worker_threads) whose agent port is bridged to the real agent. `test/agent.test.js` drives the real
|
|
246
|
+
agent binary over `--stdio` and is skipped unless `cargo build -p webpod-agent` has run (or
|
|
247
|
+
`WEBPOD_AGENT_BIN` points at a binary).
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { FsEvent, PodBackend, PodFs } from "../types.js";
|
|
2
|
+
/** A reliable byte stream to the guest agent (e.g. the org.webpod.agent channel from webpod-host). */
|
|
3
|
+
export interface AgentTransport {
|
|
4
|
+
readable: ReadableStream<Uint8Array>;
|
|
5
|
+
writable: WritableStream<Uint8Array>;
|
|
6
|
+
}
|
|
7
|
+
export interface AgentBackendOptions {
|
|
8
|
+
transport: AgentTransport;
|
|
9
|
+
/**
|
|
10
|
+
* File systems that serve guest subtrees directly (e.g. `{ "/workspace": opfsFs }`); every other
|
|
11
|
+
* path goes through the agent, as do subtrees mounted as `null` inside them.
|
|
12
|
+
*/
|
|
13
|
+
mounts?: Record<string, PodFs | null>;
|
|
14
|
+
/**
|
|
15
|
+
* Pod root CA certificate (PEM, never the key) to install before the backend is returned, so
|
|
16
|
+
* TLS interception is trusted by curl, node, pip, git, java, ... in the guest.
|
|
17
|
+
*/
|
|
18
|
+
caCertificate?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Take over a running agent (after a memory snapshot restore): the stream may start mid-frame, so
|
|
21
|
+
* it is scanned for the agent's hello, requested with one Resume.
|
|
22
|
+
*/
|
|
23
|
+
resume?: boolean;
|
|
24
|
+
/** Milliseconds to wait for the agent's hello (visible time: a hidden page pauses it). Default 30000. */
|
|
25
|
+
helloTimeoutMs?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Called before each spawn; when it returns a promise, the spawn (and what the process is sent
|
|
28
|
+
* meanwhile, such as stdin) waits for it, failed or not. The engine backend passes the shared
|
|
29
|
+
* workspace's `settle`: page changes are replayed in the guest first.
|
|
30
|
+
*/
|
|
31
|
+
beforeSpawn?: () => Promise<unknown> | undefined;
|
|
32
|
+
}
|
|
33
|
+
export interface CaInstallResult {
|
|
34
|
+
/** System bundle that now includes the CA; the CA env vars point at it. */
|
|
35
|
+
bundle: string | null;
|
|
36
|
+
/** Trust stores that could not be updated. */
|
|
37
|
+
warnings: string[];
|
|
38
|
+
/**
|
|
39
|
+
* Resolves with warnings once the background work (update-ca-certificates, update-ca-trust,
|
|
40
|
+
* JDK keytool) is done. Until then Java may not trust the CA; curl, node, Python, git already do.
|
|
41
|
+
*/
|
|
42
|
+
settled: Promise<string[]>;
|
|
43
|
+
}
|
|
44
|
+
export interface AgentBackend extends PodBackend {
|
|
45
|
+
/** Agent identification from its hello, e.g. `webpod-agent 0.0.0`. */
|
|
46
|
+
readonly agent: string;
|
|
47
|
+
/** Appends a CA certificate to the guest's trust stores (idempotent). */
|
|
48
|
+
installCa(pem: string): Promise<CaInstallResult>;
|
|
49
|
+
/** `settled` of the `caCertificate` install, or null when none was given. */
|
|
50
|
+
readonly caSettled: Promise<string[]> | null;
|
|
51
|
+
/**
|
|
52
|
+
* Tells the guest about changes made outside it (e.g. the page writing the shared workspace) so
|
|
53
|
+
* inotify watchers in the guest fire. Resolves to true once the agent has replayed them (agents
|
|
54
|
+
* with `fs-replay`; nothing to replay while nothing in the guest watches), to false once sent
|
|
55
|
+
* to an older agent.
|
|
56
|
+
*/
|
|
57
|
+
notifyChanges(changes: FsEvent[]): Promise<boolean>;
|
|
58
|
+
/** Every guest file system to disk (`sync`; agents with `fs-sync`, else nothing). */
|
|
59
|
+
sync(): Promise<void>;
|
|
60
|
+
/** sync + FIFREEZE of each mount point; the agent thaws by itself after `timeoutMs`. */
|
|
61
|
+
freeze(paths: string[], timeoutMs?: number): Promise<void>;
|
|
62
|
+
thaw(): Promise<void>;
|
|
63
|
+
/** After restoring a memory snapshot: wall clock and RNG reseed (defaults: now, 64 random bytes). */
|
|
64
|
+
reseed(unixMs?: number, entropy?: Uint8Array): Promise<void>;
|
|
65
|
+
mount(source: string, target: string, fstype: string, options?: string): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
/** Connects to a guest agent over `transport` and waits for its hello. */
|
|
68
|
+
export declare function createAgentBackend(options: AgentBackendOptions): Promise<AgentBackend>;
|