@webpodjs/sdk 0.1.3 → 0.2.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/README.md +12 -0
- package/dist/agent/backend.d.ts +13 -0
- package/dist/agent/backend.js +63 -0
- package/dist/agent/backend.js.map +1 -1
- package/dist/agent/protocol.d.ts +104 -1
- package/dist/agent/protocol.js +90 -0
- package/dist/agent/protocol.js.map +1 -1
- package/dist/exec/client.d.ts +41 -0
- package/dist/exec/client.js +401 -0
- package/dist/exec/client.js.map +1 -0
- package/dist/exec/frame.d.ts +1 -0
- package/dist/exec/frame.js +236 -0
- package/dist/exec/frame.js.map +1 -0
- package/dist/exec/options.d.ts +39 -0
- package/dist/exec/options.js +113 -0
- package/dist/exec/options.js.map +1 -0
- package/dist/exec/protocol.d.ts +2 -0
- package/dist/exec/protocol.js +3 -0
- package/dist/exec/protocol.js.map +1 -0
- package/dist/exec/wire.d.ts +49 -0
- package/dist/exec/wire.js +333 -0
- package/dist/exec/wire.js.map +1 -0
- package/dist/exec-frame.html +10 -0
- package/dist/fs/workspace.js +32 -7
- package/dist/fs/workspace.js.map +1 -1
- package/dist/host/backend.d.ts +14 -2
- package/dist/host/backend.js +69 -15
- package/dist/host/backend.js.map +1 -1
- package/dist/host/controller.d.ts +9 -0
- package/dist/host/controller.js +13 -3
- package/dist/host/controller.js.map +1 -1
- package/dist/host/devices.d.ts +29 -0
- package/dist/host/devices.js +102 -4
- package/dist/host/devices.js.map +1 -1
- package/dist/host-exec/controller.d.ts +255 -0
- package/dist/host-exec/controller.js +1176 -0
- package/dist/host-exec/controller.js.map +1 -0
- package/dist/host-exec/pipe.d.ts +21 -0
- package/dist/host-exec/pipe.js +83 -0
- package/dist/host-exec/pipe.js.map +1 -0
- package/dist/host-exec/preflight.d.ts +6 -0
- package/dist/host-exec/preflight.js +15 -0
- package/dist/host-exec/preflight.js.map +1 -0
- package/dist/host-exec/protocol.d.ts +340 -0
- package/dist/host-exec/protocol.js +16 -0
- package/dist/host-exec/protocol.js.map +1 -0
- package/dist/host-exec/route.d.ts +22 -0
- package/dist/host-exec/route.js +37 -0
- package/dist/host-exec/route.js.map +1 -0
- package/dist/host-exec/shim.d.ts +28 -0
- package/dist/host-exec/shim.js +456 -0
- package/dist/host-exec/shim.js.map +1 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/net/preview.d.ts +10 -2
- package/dist/net/preview.js +8 -0
- package/dist/net/preview.js.map +1 -1
- package/dist/pod.d.ts +22 -0
- package/dist/pod.js +129 -11
- package/dist/pod.js.map +1 -1
- package/dist/types.d.ts +35 -2
- package/dist/version.d.ts +1 -0
- package/dist/version.js +4 -0
- package/dist/version.js.map +1 -0
- package/package.json +6 -4
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import type { BackendProcess, BackendSpawnRequest, PodBackend, Signal } from "../types.js";
|
|
2
|
+
import { type HostIdentity, type HostSession, type WorkerToPage } from "./protocol.js";
|
|
3
|
+
import { type RouteContext } from "./route.js";
|
|
4
|
+
/** `Worker` subset the controller uses (node worker_threads in tests). */
|
|
5
|
+
export interface HostWorkerLike {
|
|
6
|
+
postMessage(message: unknown, transfer?: Transferable[]): void;
|
|
7
|
+
terminate(): void;
|
|
8
|
+
onmessage: ((event: {
|
|
9
|
+
data: unknown;
|
|
10
|
+
}) => void) | null;
|
|
11
|
+
onerror: ((event: {
|
|
12
|
+
message?: string;
|
|
13
|
+
}) => void) | null;
|
|
14
|
+
}
|
|
15
|
+
/** What a backend offers host processes: 9P sessions of its shared workspace (an FS Worker's). */
|
|
16
|
+
export interface HostSessions {
|
|
17
|
+
/** Mount point of the shared workspace. */
|
|
18
|
+
readonly workspace: string;
|
|
19
|
+
/**
|
|
20
|
+
* Subtrees of the workspace the guest keeps on its data disk (the FS Worker's localPaths, e.g.
|
|
21
|
+
* node_modules): host processes would see a different tree there, so hostExec needs none.
|
|
22
|
+
*/
|
|
23
|
+
readonly localPaths?: readonly string[];
|
|
24
|
+
open(client: string, identity: HostIdentity): Promise<HostSession>;
|
|
25
|
+
/**
|
|
26
|
+
* More shares host processes mount (the image's host shares: `$HOME` at /root, /tmp; e4-integration.md §2.3),
|
|
27
|
+
* a session of each per process. None: those paths are the read-only tree's.
|
|
28
|
+
*/
|
|
29
|
+
readonly shares?: readonly {
|
|
30
|
+
prefix: string;
|
|
31
|
+
open(client: string, identity: HostIdentity): Promise<HostSession>;
|
|
32
|
+
}[];
|
|
33
|
+
}
|
|
34
|
+
/** Host processes' sockets, on the net Worker (same routes and deny rules as the guest's). */
|
|
35
|
+
export interface HostNet {
|
|
36
|
+
/** `port` carries one process's sockets (lookup, connect, tls, data, ...) until `close(id)`. */
|
|
37
|
+
open(id: string, port: MessagePort): void;
|
|
38
|
+
close(id: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Connections to `port` (0: any free one) from the page and from the guest to the gateway go to
|
|
41
|
+
* socket `id` (`accept` on its port): the port, or rejects (EADDRINUSE).
|
|
42
|
+
*/
|
|
43
|
+
listen?(id: string, port: number): Promise<number>;
|
|
44
|
+
unlisten?(id: string, port: number): void;
|
|
45
|
+
}
|
|
46
|
+
/** The guest side of a host process's port (the agent's HostListen, docs/agent-protocol.md "Host listen"). */
|
|
47
|
+
export interface HostGuestPorts {
|
|
48
|
+
/** Holds addr:port in the guest for a process of `uid` (port 0: the guest picks): the port, or a Linux errno. */
|
|
49
|
+
listen(addr: string, port: number, uid: number): Promise<{
|
|
50
|
+
req: number;
|
|
51
|
+
port: number;
|
|
52
|
+
} | {
|
|
53
|
+
errno: number;
|
|
54
|
+
}>;
|
|
55
|
+
unlisten(req: number): void;
|
|
56
|
+
/** A host process connects to the guest's localhost:port: the port of the agent's one-time offer, or an errno. */
|
|
57
|
+
connect?(port: number): Promise<{
|
|
58
|
+
port: number;
|
|
59
|
+
} | {
|
|
60
|
+
errno: number;
|
|
61
|
+
}>;
|
|
62
|
+
}
|
|
63
|
+
/** `Pod.boot({ hostExec })`: run node on the host instead of the guest (experimental, off by default). */
|
|
64
|
+
export interface HostExecOptions {
|
|
65
|
+
/** Host node for `pod.spawn("node" | "npm" | "npx", ...)`. Default false. */
|
|
66
|
+
node?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* The node Worker (@webpodjs/host-exec's dist/worker.js), a classic Worker. Required with `node`, except in an
|
|
69
|
+
* execution origin: there its own, `../host-exec/worker.js` from its exec-frame.html.
|
|
70
|
+
*/
|
|
71
|
+
workerUrl?: string;
|
|
72
|
+
/**
|
|
73
|
+
* Read-only tree mounted at "/" in host processes (npm and the like), or a URL to fetch it from. Default: tree.bin
|
|
74
|
+
* next to `workerUrl`, when there is one (the vite plugin's host-exec/, webpodexec.top's). Without a tree node
|
|
75
|
+
* runs and npm does not.
|
|
76
|
+
*/
|
|
77
|
+
tree?: SharedArrayBuffer | ArrayBuffer | string;
|
|
78
|
+
/**
|
|
79
|
+
* Node's lib (the Worker's node-lib.json), fetched once and shared by every Worker instead of each
|
|
80
|
+
* fetching and holding its own copy. Default: node-lib.json next to `workerUrl` (if the page cannot
|
|
81
|
+
* fetch it, each Worker tries itself); a URL: that one, and failing to fetch it fails the Workers'
|
|
82
|
+
* boot; false: each Worker fetches it.
|
|
83
|
+
*/
|
|
84
|
+
sources?: string | false;
|
|
85
|
+
/** Booted Workers kept ready for the next process. Default 1. */
|
|
86
|
+
pool?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Host processes alive at once; more wait for one to exit. Default 8, 6 on constrained devices (with 4, a vite dev
|
|
89
|
+
* server and vitest started next to it left vitest's forks no slot: they timed out, T106).
|
|
90
|
+
*/
|
|
91
|
+
maxProcesses?: number;
|
|
92
|
+
/**
|
|
93
|
+
* worker_threads threads of host processes alive at once (each a Worker of its own); a thread past it fails to start
|
|
94
|
+
* (its 'error' is ERR_WORKER_INIT_FAILED). Default twice maxProcesses.
|
|
95
|
+
*/
|
|
96
|
+
maxThreads?: number;
|
|
97
|
+
/** node's path in the image. Default /usr/local/bin/node. */
|
|
98
|
+
execPath?: string;
|
|
99
|
+
/** Commands the SDK runs as host node, with the script before the arguments. Default node, npm, npx. */
|
|
100
|
+
commands?: Record<string, string[]>;
|
|
101
|
+
/** Environment of host processes, under the spawn's own `env`. */
|
|
102
|
+
env?: Record<string, string>;
|
|
103
|
+
/** Identity of host processes (the image's user). Default 0:0. */
|
|
104
|
+
uid?: number;
|
|
105
|
+
gid?: number;
|
|
106
|
+
/** Bytes of a process's stdin pipe (a power of two). Default 64 KiB. */
|
|
107
|
+
stdinBytes?: number;
|
|
108
|
+
/** Bytes for a spawnSync child's output. Default 16 MiB. */
|
|
109
|
+
syncBufferBytes?: number;
|
|
110
|
+
/** stdin kept to replay when a process falls back to the guest before reading it. Default 1 MiB. */
|
|
111
|
+
replayBytes?: number;
|
|
112
|
+
/** Passed to the Worker's boot (msize, pipeline, clientSpinUs, batchTurns, debug). */
|
|
113
|
+
workerOptions?: Record<string, unknown>;
|
|
114
|
+
/** Defaults to `new Worker(url)`. */
|
|
115
|
+
createWorker?: (url: string) => HostWorkerLike;
|
|
116
|
+
/** Replaces the browser preflight: resolves with why host processes cannot run, or null. */
|
|
117
|
+
preflight?: () => Promise<string | null>;
|
|
118
|
+
onLog?: (level: string, text: string) => void;
|
|
119
|
+
}
|
|
120
|
+
export type HostRunner = "guest" | "host";
|
|
121
|
+
export interface HostExecStatus {
|
|
122
|
+
node: "on" | "off" | "unavailable";
|
|
123
|
+
/** Why `node` is "unavailable". */
|
|
124
|
+
reason?: string;
|
|
125
|
+
runners: "node"[];
|
|
126
|
+
}
|
|
127
|
+
/** A process that was meant for the host and runs in the guest instead (e4-integration.md §3). */
|
|
128
|
+
export interface HostExecFallback {
|
|
129
|
+
/** The host process's pid; null when routed to the guest at its start. */
|
|
130
|
+
pid: number | null;
|
|
131
|
+
argv: string[];
|
|
132
|
+
reason: string;
|
|
133
|
+
/** Run again from the start in the guest (nothing visible had happened); false: exited with 70. */
|
|
134
|
+
rerun: boolean;
|
|
135
|
+
}
|
|
136
|
+
/** `pod.hostExec`. */
|
|
137
|
+
export interface PodHostExec {
|
|
138
|
+
readonly status: HostExecStatus;
|
|
139
|
+
/** false: processes started from now on run in the guest; running ones are not affected. */
|
|
140
|
+
enabled: boolean;
|
|
141
|
+
on(event: "fallback", listener: (event: HostExecFallback) => void): () => void;
|
|
142
|
+
}
|
|
143
|
+
/** What a Pod drives: the controller itself, or its proxy for a pod in an execution origin. */
|
|
144
|
+
export interface HostExecSide extends PodHostExec {
|
|
145
|
+
/** Starts `req` as a host process here, or null (the backend runs it: the guest, or the execution origin). */
|
|
146
|
+
spawn(req: BackendSpawnRequest): (BackendProcess & {
|
|
147
|
+
runner: HostRunner;
|
|
148
|
+
}) | null;
|
|
149
|
+
/** Where the backend will run `req` when `spawn` returned null (default "guest"). */
|
|
150
|
+
runnerOf?(req: BackendSpawnRequest): HostRunner;
|
|
151
|
+
dispose(): Promise<void>;
|
|
152
|
+
}
|
|
153
|
+
/** A booted node Worker with its 9P sessions. */
|
|
154
|
+
export interface HostWorker {
|
|
155
|
+
readonly w: HostWorkerLike;
|
|
156
|
+
/** Its workspace session. */
|
|
157
|
+
readonly session: HostSession;
|
|
158
|
+
/** Its sessions of the other shares (HostSessions.shares). */
|
|
159
|
+
readonly shares: readonly HostSession[];
|
|
160
|
+
readonly ctl: SharedArrayBuffer;
|
|
161
|
+
/** Its host socket on the net Worker, if any. */
|
|
162
|
+
netId?: string;
|
|
163
|
+
/** Its listens: req -> port on the net Worker, and the agent's req. */
|
|
164
|
+
listens?: Map<number, {
|
|
165
|
+
port: number;
|
|
166
|
+
guest: number | null;
|
|
167
|
+
}>;
|
|
168
|
+
/** Where its process's output and exit go (top level: the PodProcess; children report on their port). */
|
|
169
|
+
report?: (m: WorkerToPage) => void;
|
|
170
|
+
/** Fails its boot (while booting). */
|
|
171
|
+
abort?: () => void;
|
|
172
|
+
pid?: number;
|
|
173
|
+
ppid?: number;
|
|
174
|
+
/** Who its sessions are checked as. */
|
|
175
|
+
identity?: HostIdentity;
|
|
176
|
+
/** Its process's worker_threads threads it started, by threadId (Workers of their own). */
|
|
177
|
+
threads?: Map<number, HostWorker>;
|
|
178
|
+
/** Called once when it is retired (a thread: its slot of maxThreads). */
|
|
179
|
+
onRetire?: () => void;
|
|
180
|
+
dead: boolean;
|
|
181
|
+
}
|
|
182
|
+
/** A process of the table: a node Worker or a guest child of a host process. */
|
|
183
|
+
interface Entry {
|
|
184
|
+
pid: number;
|
|
185
|
+
ppid: number;
|
|
186
|
+
kill(signal: Signal): void;
|
|
187
|
+
}
|
|
188
|
+
export declare class HostExecController implements HostExecSide {
|
|
189
|
+
#private;
|
|
190
|
+
private constructor();
|
|
191
|
+
/** Checks what host processes need; `status.node` tells the outcome. Never throws for a missing capability. */
|
|
192
|
+
static create(backend: PodBackend, options?: HostExecOptions): Promise<HostExecController>;
|
|
193
|
+
/** What routeTopLevel needs, to tell where a request runs without starting it (an execution origin's app side). */
|
|
194
|
+
get routeContext(): RouteContext | null;
|
|
195
|
+
get status(): HostExecStatus;
|
|
196
|
+
get enabled(): boolean;
|
|
197
|
+
set enabled(on: boolean);
|
|
198
|
+
on(event: "fallback", listener: (event: HostExecFallback) => void): () => void;
|
|
199
|
+
/** Starts `req` as a host process, or returns null: it runs in the guest. */
|
|
200
|
+
spawn(req: BackendSpawnRequest): (BackendProcess & {
|
|
201
|
+
runner: HostRunner;
|
|
202
|
+
}) | null;
|
|
203
|
+
dispose(): Promise<void>;
|
|
204
|
+
/**
|
|
205
|
+
* @internal Reads the fallback tables of the guest ({ "guest": [pattern] }: the image's /etc/webpod/host-exec.json and
|
|
206
|
+
* the learned $HOME/.cache/webpod/host-exec-fallback.json), which host processes apply to the node processes they
|
|
207
|
+
* start, as the shim does to the guest's (the project's table, in the workspace, they read themselves).
|
|
208
|
+
*/
|
|
209
|
+
_reloadTables(): Promise<void>;
|
|
210
|
+
/** @internal */
|
|
211
|
+
_guestTable(): string[];
|
|
212
|
+
/** @internal */
|
|
213
|
+
_emit(e: HostExecFallback): void;
|
|
214
|
+
/** @internal */
|
|
215
|
+
_backend(): PodBackend;
|
|
216
|
+
/** @internal */
|
|
217
|
+
_options(): HostExecOptions;
|
|
218
|
+
/** @internal The next pid (the Workers take theirs from the same counter). */
|
|
219
|
+
_pid(): number;
|
|
220
|
+
/** @internal Waits for a process slot (`maxProcesses`); `force` takes one anyway (spawnSync: the parent is blocked). */
|
|
221
|
+
_acquire(force?: boolean): Promise<void>;
|
|
222
|
+
/** @internal */
|
|
223
|
+
_release(): void;
|
|
224
|
+
/** @internal The identity of pooled Workers' sessions. */
|
|
225
|
+
_identity(): HostIdentity;
|
|
226
|
+
/** @internal Whether processes start on the host now (enabled, not disposed). */
|
|
227
|
+
_on(): boolean;
|
|
228
|
+
/** @internal Where the other shares are mounted in host processes (and in the guest). */
|
|
229
|
+
_sharePrefixes(): string[];
|
|
230
|
+
/** @internal */
|
|
231
|
+
_execPath(): string;
|
|
232
|
+
/** @internal The read-only tree, if any. */
|
|
233
|
+
_tree(): Promise<SharedArrayBuffer | ArrayBuffer | undefined>;
|
|
234
|
+
/** @internal */
|
|
235
|
+
_log(level: string, text: string): void;
|
|
236
|
+
/**
|
|
237
|
+
* @internal A booted Worker: from the pool, else a new one; one of its own for another identity than the pool's
|
|
238
|
+
* (supplementary groups count, except for root, whom the FS Worker does not check).
|
|
239
|
+
*/
|
|
240
|
+
_worker(identity?: HostIdentity): Promise<HostWorker>;
|
|
241
|
+
/** @internal */
|
|
242
|
+
_register(e: Entry): void;
|
|
243
|
+
/** @internal */
|
|
244
|
+
_unregister(pid: number): void;
|
|
245
|
+
/** @internal Kills the processes below `pid` (its own exit or kill ends the tree, as a process group). */
|
|
246
|
+
_killChildren(pid: number, signal: Signal): void;
|
|
247
|
+
/** @internal Terminates a Worker and ends its session. */
|
|
248
|
+
_retire(w: HostWorker): void;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* node-lib.json in the form the Workers read without parsing it whole (each keeps only the modules it compiles, T106):
|
|
252
|
+
* "WPNL", a u32 count, a u32 offset and length for each module's id and then its source (UTF-8), then the bytes.
|
|
253
|
+
*/
|
|
254
|
+
export declare function indexSources(json: SharedArrayBuffer): SharedArrayBuffer;
|
|
255
|
+
export {};
|