@waffle-os/runtime 0.0.145 → 0.0.146
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 +32 -0
- package/browser/command-input.ts +86 -0
- package/browser/command-owner.ts +5 -0
- package/browser/display/guest.ts +2 -18
- package/browser/display/surface-info.ts +19 -0
- package/browser/operations/cells.ts +90 -0
- package/browser/operations/guest.ts +94 -0
- package/browser/operations/owner.ts +201 -0
- package/browser/operations/protocol.ts +16 -0
- package/browser/operations/types.ts +41 -0
- package/browser/operations/writer.ts +106 -0
- package/browser/preview1-worker.ts +8 -1
- package/browser/process-broker.ts +372 -22
- package/browser/process-start.ts +5 -2
- package/browser/run-command.ts +388 -0
- package/browser/shared-fs.ts +20 -3
- package/browser/worker-memory.ts +6 -0
- package/browser/worker.ts +121 -27
- package/dist/browser/command-input.js +82 -0
- package/dist/browser/command-owner.js +2 -0
- package/dist/browser/display/guest.js +2 -18
- package/dist/browser/display/surface-info.js +18 -0
- package/dist/browser/operations/cells.js +80 -0
- package/dist/browser/operations/guest.js +67 -0
- package/dist/browser/operations/owner.js +175 -0
- package/dist/browser/operations/protocol.js +0 -0
- package/dist/browser/operations/types.js +0 -0
- package/dist/browser/operations/writer.js +98 -0
- package/dist/browser/preview1-worker.js +4 -0
- package/dist/browser/process-broker.js +317 -18
- package/dist/browser/process-start.js +3 -0
- package/dist/browser/run-command.js +377 -0
- package/dist/browser/shared-fs.js +8 -3
- package/dist/browser/worker-memory.js +10 -1
- package/dist/browser/worker.js +83 -28
- package/dist/fs/client.js +81 -18
- package/dist/fs/resolve-observation.js +11 -0
- package/dist/fs/service.js +51 -4
- package/dist/proc/command-transfer.js +72 -0
- package/dist/proc/directory.js +9 -1
- package/dist/proc/intrinsics/host.js +4 -1
- package/dist/proc/intrinsics.js +1 -0
- package/dist/proc/pipes.js +37 -4
- package/dist/proc/registry.js +25 -14
- package/dist/proc/start-command.js +101 -0
- package/dist/proc/supervisor.js +34 -10
- package/dist/proc/worker-broker.js +86 -31
- package/dist/proc/worker-client.js +67 -31
- package/dist/programs/collection.js +2 -0
- package/dist/runtime.js +14 -1
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/client.ts +113 -17
- package/fs/resolve-observation.ts +25 -0
- package/fs/service.ts +63 -4
- package/index.ts +5 -0
- package/package.json +9 -1
- package/proc/command-transfer.ts +96 -0
- package/proc/directory.ts +10 -1
- package/proc/intrinsics/host.ts +5 -1
- package/proc/intrinsics.ts +1 -0
- package/proc/pipes.ts +39 -6
- package/proc/registry.ts +52 -17
- package/proc/start-command.ts +157 -0
- package/proc/supervisor.ts +34 -9
- package/proc/worker-broker.ts +100 -38
- package/proc/worker-client.ts +84 -31
- package/programs/collection.ts +4 -0
- package/runtime.ts +44 -3
- package/scripts/test-browser.ts +49 -29
- package/tests/browser-controls-discovery.test.ts +2 -10
- package/tests/browser-matrix.test.ts +105 -59
- package/tests/browser-navigation.test.ts +1 -1
- package/tests/browser-runner-lifecycle.ts +23 -13
- package/tests/command-input.test.ts +40 -0
- package/tests/command-scope.test.ts +422 -0
- package/tests/filesystem-client-intrinsics.test.ts +2 -0
- package/tests/filesystem-refcount.test.ts +2 -0
- package/tests/filesystem-resolve-observation.test.ts +185 -0
- package/tests/fixtures/command-bounded-admission.ts +31 -0
- package/tests/fixtures/command-disposal.ts +47 -0
- package/tests/fixtures/command-guests.ts +123 -0
- package/tests/fixtures/operation-fs-worker.ts +114 -0
- package/tests/fixtures/operation-observer-worker.ts +41 -0
- package/tests/fixtures/operation-page.ts +522 -0
- package/tests/fixtures/process-start-worker.ts +32 -0
- package/tests/link-browser-runner.ts +95 -22
- package/tests/node-exports.test.ts +29 -0
- package/tests/operation-diagnostics-browser.ts +192 -0
- package/tests/operation-diagnostics.test.ts +291 -0
- package/tests/page-runner/browser-controls-process-fixture.ts +24 -0
- package/tests/page-runner/browser-controls-runner.test.ts +20 -0
- package/tests/page-runner/browser-controls-runner.ts +1 -0
- package/tests/page-runner/browser-controls-suite.ts +20 -3
- package/tests/page-runner/control-run.ts +9 -1
- package/tests/page-runner/driver.ts +2 -0
- package/tests/page-runner/playwright-driver.ts +1 -1
- package/tests/page-runner/suite-host.ts +26 -1
- package/tests/page-runner/suite-startup.test.ts +39 -0
- package/tests/page-runner/threads-runtime-browser.test.ts +4 -6
- package/tests/page-runner/threads-runtime-suite.test.ts +14 -5
- package/tests/page-runner/worker-capability-controls.ts +174 -15
- package/tests/pipe-reader-reuse-races.test.ts +44 -0
- package/tests/process-start-browser-runner.ts +12 -4
- package/tests/process-start-browser.ts +66 -5
- package/tests/program-build-tools.test.ts +1 -1
- package/tests/runtime-facade.test.ts +5 -0
- package/tests/start-command.test.ts +109 -0
- package/tests/threads-guest-fixture.ts +1 -1
- package/tests/threads-guest-module.ts +6 -3
- package/tests/threads-guest.test.ts +25 -0
- package/tests/threads-runtime-browser-runner.ts +36 -13
- package/tests/threads-runtime-browser.ts +231 -92
- package/tests/threads-runtime-cases.ts +18 -1
- package/tests/threads-runtime-cleanup.ts +5 -1
- package/tests/threads-runtime-fd-browser.ts +247 -0
- package/tests/threads-runtime-fd-control.ts +3 -385
- package/tests/threads-runtime-fd-descriptors.test.ts +15 -13
- package/tests/threads-runtime-fd-module.ts +13 -2
- package/tests/threads-runtime-fd-service.ts +23 -2
- package/tests/threads-runtime-growth-worker.ts +61 -0
- package/tests/threads-runtime-host.ts +17 -4
- package/tests/threads-runtime-modules.ts +23 -5
- package/tests/threads-runtime-regression-controls.ts +386 -0
- package/tests/threads-runtime-regression-modules.ts +242 -0
- package/tests/threads-runtime-service-barrier.ts +8 -1
- package/tests/webkit-diagnostics.ts +125 -0
- package/tests/webkit-error-output.test.ts +118 -0
- package/tests/worker-memory-bootstrap.test.ts +19 -1
- package/tests/worker-memory-broker.test.ts +16 -8
- package/tooling/ci-paths.test.ts +329 -61
- package/tooling/private-authority.ts +14 -0
- package/tooling/program/program-transformer.json +1 -1
- package/tooling/tarball-smoke.ts +3 -1
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { allocateSharedControl } from "../memory/control.js";
|
|
2
|
+
import { commandTransferBytes } from "../proc/command-transfer.js";
|
|
3
|
+
import { DescriptorTable } from "../core/descriptors.js";
|
|
4
|
+
import type { Runtime } from "../runtime.js";
|
|
5
|
+
import { commandDisposals, commandOwners } from "./command-owner.js";
|
|
6
|
+
import { controlLoad } from "../proc/control.js";
|
|
7
|
+
import { directoryJob } from "../proc/directory.js";
|
|
8
|
+
import type { PipeEndpoint } from "../proc/pipes.js";
|
|
9
|
+
import type { WorkerPipeHost } from "../proc/worker-pipes.js";
|
|
10
|
+
import { atomicsWaitAsync } from "../proc/intrinsics.js";
|
|
11
|
+
import { readOutcome } from "../proc/outcome.js";
|
|
12
|
+
import type { CommandOptions, CommandOutput, CommandOutputOptions } from "../proc/start-command.js";
|
|
13
|
+
import { checkedSignal, type ProcessExit, type ProcessSignal } from "../proc/types.js";
|
|
14
|
+
import {
|
|
15
|
+
createWorkerProcessTransport,
|
|
16
|
+
wakeWorkerProcess,
|
|
17
|
+
WorkerProcessClient,
|
|
18
|
+
workerProcessState,
|
|
19
|
+
type WorkerProcessMessage,
|
|
20
|
+
type WorkerProcessTransport,
|
|
21
|
+
type WorkerSpawnTransfer,
|
|
22
|
+
} from "../proc/worker-client.js";
|
|
23
|
+
import type { BrowserProcessBroker } from "./process-broker.js";
|
|
24
|
+
import { createRuntimeTerminalCodec } from "./runtime-terminals.js";
|
|
25
|
+
import { CommandInput } from "./command-input.js";
|
|
26
|
+
|
|
27
|
+
const READ = 1n << 1n;
|
|
28
|
+
const WRITE = 1n << 6n;
|
|
29
|
+
const POLL = 1n << 27n;
|
|
30
|
+
const FD_FILESTAT_GET = 1n << 21n;
|
|
31
|
+
const OUTPUT_BATCH = 32;
|
|
32
|
+
|
|
33
|
+
class Capture {
|
|
34
|
+
private bytes = new Uint8Array(0);
|
|
35
|
+
private readonly limit: number;
|
|
36
|
+
private size = 0;
|
|
37
|
+
private truncated = false;
|
|
38
|
+
private onChunk?: CommandOutputOptions["onChunk"];
|
|
39
|
+
|
|
40
|
+
constructor(options: CommandOptions["stdout"]) {
|
|
41
|
+
this.limit = options === "closed" ? 0 : (options?.limitBytes ?? 64 * 1024);
|
|
42
|
+
this.onChunk = options === "closed" ? undefined : options?.onChunk;
|
|
43
|
+
}
|
|
44
|
+
append(data: Uint8Array): void {
|
|
45
|
+
const length = Math.min(data.length, this.limit - this.size);
|
|
46
|
+
if (length) {
|
|
47
|
+
const size = this.size + length;
|
|
48
|
+
if (size > this.bytes.length) this.grow(size);
|
|
49
|
+
this.bytes.set(data.subarray(0, length), this.size);
|
|
50
|
+
this.size = size;
|
|
51
|
+
}
|
|
52
|
+
if (length < data.length) this.truncated = true;
|
|
53
|
+
try {
|
|
54
|
+
const onChunk = this.onChunk;
|
|
55
|
+
if (!onChunk) return;
|
|
56
|
+
const returned: unknown = onChunk(data.slice());
|
|
57
|
+
if (returned === undefined) return;
|
|
58
|
+
if (returned instanceof Promise) void returned.catch(() => {});
|
|
59
|
+
throw new TypeError("Command output callbacks must return undefined synchronously");
|
|
60
|
+
} catch (error) {
|
|
61
|
+
this.onChunk = undefined;
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
private grow(size: number): void {
|
|
66
|
+
const capacity = Math.min(this.limit, Math.max(size, 4096, 2 * this.bytes.length));
|
|
67
|
+
const bytes = new Uint8Array(capacity);
|
|
68
|
+
bytes.set(this.bytes);
|
|
69
|
+
this.bytes = bytes;
|
|
70
|
+
}
|
|
71
|
+
result(): CommandOutput {
|
|
72
|
+
const bytes = this.bytes.length === this.size ? this.bytes : this.bytes.slice(0, this.size);
|
|
73
|
+
this.bytes = new Uint8Array(0);
|
|
74
|
+
return { bytes, truncated: this.truncated };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface Leader {
|
|
79
|
+
readonly job: number;
|
|
80
|
+
readonly state: ReturnType<typeof directoryJob>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
class Command {
|
|
84
|
+
private readonly stdout: Capture;
|
|
85
|
+
private readonly stderr: Capture;
|
|
86
|
+
private readonly descriptors = new DescriptorTable();
|
|
87
|
+
private readonly leader = Promise.withResolvers<Leader | undefined>();
|
|
88
|
+
private readonly abortSetup = Promise.withResolvers<undefined>();
|
|
89
|
+
private readonly disposeSignal: AbortSignal;
|
|
90
|
+
private root?: WorkerProcessTransport;
|
|
91
|
+
private owner?: BrowserProcessBroker;
|
|
92
|
+
private input?: CommandInput;
|
|
93
|
+
private outputTask?: ReturnType<typeof setTimeout>;
|
|
94
|
+
private outputBatch = OUTPUT_BATCH;
|
|
95
|
+
private ioFailure?: { error: unknown };
|
|
96
|
+
private settled = false;
|
|
97
|
+
|
|
98
|
+
constructor(
|
|
99
|
+
private readonly runtime: Runtime,
|
|
100
|
+
private readonly executable: string,
|
|
101
|
+
private readonly options: CommandOptions,
|
|
102
|
+
private readonly stopped: AbortSignal,
|
|
103
|
+
) {
|
|
104
|
+
this.stdout = new Capture(options.stdout);
|
|
105
|
+
this.stderr = new Capture(options.stderr);
|
|
106
|
+
let disposing = commandDisposals.get(runtime);
|
|
107
|
+
if (!disposing) commandDisposals.set(runtime, (disposing = new AbortController()));
|
|
108
|
+
this.disposeSignal = disposing.signal;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private readonly stop = () => {
|
|
112
|
+
this.abortSetup.resolve(undefined);
|
|
113
|
+
if (!this.root || this.runtime.disposed) return;
|
|
114
|
+
this.stopInput();
|
|
115
|
+
try {
|
|
116
|
+
this.owner?.stopCommandRoot(this.root);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
this.ioFailure ??= { error };
|
|
119
|
+
wakeWorkerProcess(this.root);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
private readonly dispose = () => {
|
|
124
|
+
this.abortSetup.resolve(undefined);
|
|
125
|
+
if (this.root) wakeWorkerProcess(this.root);
|
|
126
|
+
this.stopInput();
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
private readonly failIO = (error: unknown) => {
|
|
130
|
+
this.ioFailure ??= { error };
|
|
131
|
+
this.stop();
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
private stopInput(): void {
|
|
135
|
+
void this.input?.stop().catch(this.failIO);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async signal(signal: ProcessSignal): Promise<void> {
|
|
139
|
+
if (this.settled) return;
|
|
140
|
+
const sig = checkedSignal(signal);
|
|
141
|
+
const leader = await this.leader.promise;
|
|
142
|
+
if (!leader || this.settled || this.runtime.disposed || !this.root) return;
|
|
143
|
+
if (leader.state.outcome.valid() && controlLoad(leader.state.pending) > 0)
|
|
144
|
+
await this.owner?.killJob(leader.job, sig);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async execute() {
|
|
148
|
+
this.stopped.addEventListener("abort", this.stop, { once: true });
|
|
149
|
+
this.disposeSignal.addEventListener("abort", this.dispose, { once: true });
|
|
150
|
+
try {
|
|
151
|
+
const exit = await this.run();
|
|
152
|
+
if (this.runtime.disposed) await this.disposed();
|
|
153
|
+
if (this.ioFailure) throw this.ioFailure.error;
|
|
154
|
+
return { exit, stdout: this.stdout.result(), stderr: this.stderr.result() };
|
|
155
|
+
} finally {
|
|
156
|
+
this.settled = true;
|
|
157
|
+
this.leader.resolve(undefined);
|
|
158
|
+
this.stopped.removeEventListener("abort", this.stop);
|
|
159
|
+
this.disposeSignal.removeEventListener("abort", this.dispose);
|
|
160
|
+
clearTimeout(this.outputTask);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private async disposed(): Promise<never> {
|
|
165
|
+
await this.runtime.dispose();
|
|
166
|
+
throw new Error("Runtime was disposed while running a command");
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private async run(): Promise<ProcessExit | null> {
|
|
170
|
+
if (this.runtime.disposed) return this.disposed();
|
|
171
|
+
if (this.stopped.aborted) return null;
|
|
172
|
+
const service = await Promise.race([this.runtime.processes(), this.abortSetup.promise]);
|
|
173
|
+
if (this.runtime.disposed) return this.disposed();
|
|
174
|
+
if (!service || this.stopped.aborted) return null;
|
|
175
|
+
const owner = commandOwners.get(service);
|
|
176
|
+
if (!owner) throw new Error("Process service has no command owner");
|
|
177
|
+
await Promise.race([owner.ready(), this.abortSetup.promise]);
|
|
178
|
+
if (this.runtime.disposed) return this.disposed();
|
|
179
|
+
if (this.stopped.aborted) return null;
|
|
180
|
+
return this.runInRoot(owner);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private async runInRoot(owner: BrowserProcessBroker): Promise<ProcessExit | null> {
|
|
184
|
+
const transport = createWorkerProcessTransport({
|
|
185
|
+
identity: this.runtime.identities.buffer,
|
|
186
|
+
foreground: false,
|
|
187
|
+
});
|
|
188
|
+
const root = {
|
|
189
|
+
...transport,
|
|
190
|
+
commandTransfer: allocateSharedControl(commandTransferBytes(transport.directory)),
|
|
191
|
+
};
|
|
192
|
+
const filesystem = this.runtime.filesystem.createIdentity();
|
|
193
|
+
this.owner = owner;
|
|
194
|
+
this.root = root;
|
|
195
|
+
let adopted = false;
|
|
196
|
+
let leader: Leader | undefined;
|
|
197
|
+
try {
|
|
198
|
+
await owner.adoptCommandRoot(root, {
|
|
199
|
+
filesystem,
|
|
200
|
+
memory: [],
|
|
201
|
+
tryOutput: (fd, bytes) => this.deliver(fd, bytes),
|
|
202
|
+
stop: () => this.input?.stop(),
|
|
203
|
+
});
|
|
204
|
+
adopted = true;
|
|
205
|
+
if (this.runtime.disposed) return this.disposed();
|
|
206
|
+
if (!this.stopped.aborted) leader = this.spawn(owner, root);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
if (this.runtime.disposed) return this.disposed();
|
|
209
|
+
if (!adopted) {
|
|
210
|
+
this.runtime.filesystem.revoke(filesystem);
|
|
211
|
+
this.runtime.filesystem.reap(filesystem);
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
this.failIO(error);
|
|
215
|
+
} finally {
|
|
216
|
+
try {
|
|
217
|
+
this.descriptors.dispose();
|
|
218
|
+
} catch (error) {
|
|
219
|
+
this.failIO(error);
|
|
220
|
+
}
|
|
221
|
+
this.leader.resolve(leader);
|
|
222
|
+
}
|
|
223
|
+
if (this.stopped.aborted || this.ioFailure) this.stop();
|
|
224
|
+
try {
|
|
225
|
+
const result = await this.join(owner, root, leader);
|
|
226
|
+
if (this.runtime.disposed) return this.disposed();
|
|
227
|
+
await this.input?.stop();
|
|
228
|
+
await owner.retireCommandRoot(root);
|
|
229
|
+
return result;
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (this.runtime.disposed) return this.disposed();
|
|
232
|
+
owner.stopCommandRoot(root);
|
|
233
|
+
throw error;
|
|
234
|
+
} finally {
|
|
235
|
+
await this.input?.stop();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
private spawn(owner: BrowserProcessBroker, root: WorkerProcessTransport): Leader {
|
|
240
|
+
let leader: Leader | undefined;
|
|
241
|
+
let writer: PipeEndpoint | undefined;
|
|
242
|
+
const post = (message: WorkerProcessMessage) => {
|
|
243
|
+
if (message.operation === "spawn") {
|
|
244
|
+
const { job, jobSlot } = workerProcessState(message.transport);
|
|
245
|
+
leader = { job, state: directoryJob(root.directory, jobSlot, job) };
|
|
246
|
+
}
|
|
247
|
+
void owner.runtimeMessage(message, root.pid).catch((error) => {
|
|
248
|
+
if (message.operation !== "spawn") this.failIO(error);
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
const pipes = owner.commandPipeHost(root, post);
|
|
252
|
+
const stdin = this.options.stdin;
|
|
253
|
+
try {
|
|
254
|
+
if (stdin !== undefined && stdin !== "closed") {
|
|
255
|
+
const [reader, input] = pipes.createPipe();
|
|
256
|
+
writer = input;
|
|
257
|
+
this.descriptors.openAt(0, reader, READ | POLL | FD_FILESTAT_GET, 0n);
|
|
258
|
+
}
|
|
259
|
+
this.openTerminals(stdin);
|
|
260
|
+
const codec = createRuntimeTerminalCodec({
|
|
261
|
+
descriptors: this.descriptors,
|
|
262
|
+
fallback: pipes.codec(),
|
|
263
|
+
});
|
|
264
|
+
const client = new WorkerProcessClient({
|
|
265
|
+
transport: root,
|
|
266
|
+
descriptors: this.descriptors,
|
|
267
|
+
codec,
|
|
268
|
+
postMessage: post,
|
|
269
|
+
prepareSpawn: (pid, cleanupFailed) => this.prepareChild(pipes, pid, cleanupFailed),
|
|
270
|
+
});
|
|
271
|
+
client.spawn(root.pid, {
|
|
272
|
+
executable: this.executable,
|
|
273
|
+
argv: [this.executable].concat(this.options.args ?? []),
|
|
274
|
+
env: { ...this.options.env },
|
|
275
|
+
cwd: this.options.cwd ?? "/",
|
|
276
|
+
job: -1,
|
|
277
|
+
jobLifetime: "caller-fatal",
|
|
278
|
+
fds: this.descriptors.inheritable().map((fd) => ({ parent: fd, child: fd })),
|
|
279
|
+
});
|
|
280
|
+
if (writer && stdin !== undefined && stdin !== "closed")
|
|
281
|
+
this.input = new CommandInput(writer, stdin, this.failIO);
|
|
282
|
+
} catch (error) {
|
|
283
|
+
writer?.close();
|
|
284
|
+
if (!leader) throw error;
|
|
285
|
+
this.failIO(error);
|
|
286
|
+
}
|
|
287
|
+
if (!leader) throw new Error("Command leader was not published");
|
|
288
|
+
return leader;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private prepareChild(
|
|
292
|
+
pipes: WorkerPipeHost,
|
|
293
|
+
pid: number,
|
|
294
|
+
cleanupFailed?: (error: unknown) => void,
|
|
295
|
+
): WorkerSpawnTransfer {
|
|
296
|
+
const filesystem = this.runtime.filesystem.createIdentity();
|
|
297
|
+
let enrolled = false;
|
|
298
|
+
const cancel = () => {
|
|
299
|
+
this.runtime.filesystem.revoke(filesystem);
|
|
300
|
+
this.runtime.filesystem.reap(filesystem);
|
|
301
|
+
if (enrolled) pipes.registry.collectProcess(pid);
|
|
302
|
+
};
|
|
303
|
+
try {
|
|
304
|
+
pipes.registry.registerProcess(pid);
|
|
305
|
+
enrolled = true;
|
|
306
|
+
return { data: { filesystem, pipeRegistry: pipes.registry.handle() }, cancel };
|
|
307
|
+
} catch (error) {
|
|
308
|
+
try {
|
|
309
|
+
cancel();
|
|
310
|
+
} catch (cleanup) {
|
|
311
|
+
cleanupFailed?.(cleanup);
|
|
312
|
+
}
|
|
313
|
+
throw error;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private openTerminals(stdin: CommandOptions["stdin"]): void {
|
|
318
|
+
for (const fd of [0, 1, 2] as const) {
|
|
319
|
+
if (
|
|
320
|
+
(fd === 0 && stdin !== undefined) ||
|
|
321
|
+
(fd === 1 && this.options.stdout === "closed") ||
|
|
322
|
+
(fd === 2 && this.options.stderr === "closed")
|
|
323
|
+
)
|
|
324
|
+
continue;
|
|
325
|
+
this.descriptors.openAt(
|
|
326
|
+
fd,
|
|
327
|
+
{
|
|
328
|
+
kind: "terminal",
|
|
329
|
+
read: () => 0,
|
|
330
|
+
write: () => 0,
|
|
331
|
+
readiness: () => ({ readable: fd === 0, writable: fd !== 0, hangup: fd === 0 }),
|
|
332
|
+
},
|
|
333
|
+
(fd === 0 ? READ : WRITE) | POLL | FD_FILESTAT_GET,
|
|
334
|
+
0n,
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
private deliver(fd: 1 | 2, bytes: Uint8Array): number {
|
|
340
|
+
if (this.outputBatch === 0) {
|
|
341
|
+
this.outputTask ??= setTimeout(() => {
|
|
342
|
+
this.outputTask = undefined;
|
|
343
|
+
this.outputBatch = OUTPUT_BATCH;
|
|
344
|
+
if (!this.root || this.runtime.disposed) return;
|
|
345
|
+
this.owner?.outputDrained(this.root.pid);
|
|
346
|
+
wakeWorkerProcess(this.root);
|
|
347
|
+
}, 0);
|
|
348
|
+
return 0;
|
|
349
|
+
}
|
|
350
|
+
this.outputBatch--;
|
|
351
|
+
if (this.settled) return bytes.length;
|
|
352
|
+
try {
|
|
353
|
+
(fd === 1 ? this.stdout : this.stderr).append(bytes);
|
|
354
|
+
} catch (error) {
|
|
355
|
+
this.failIO(error);
|
|
356
|
+
}
|
|
357
|
+
return bytes.length;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
private async join(owner: BrowserProcessBroker, root: WorkerProcessTransport, leader?: Leader) {
|
|
361
|
+
const wake = workerProcessState(root).wake;
|
|
362
|
+
let exit: ProcessExit | null = null;
|
|
363
|
+
for (;;) {
|
|
364
|
+
const expected = Atomics.load(wake, 0);
|
|
365
|
+
if (this.runtime.disposed) return this.disposed();
|
|
366
|
+
if (this.stopped.aborted || this.ioFailure) this.stopInput();
|
|
367
|
+
if (leader && !exit) exit = readOutcome(leader.state.outcome);
|
|
368
|
+
if (owner.checkCommandCleanup(root, !leader || exit !== null)) {
|
|
369
|
+
if (!leader || exit) return exit;
|
|
370
|
+
throw new Error("Command leader retired without an outcome");
|
|
371
|
+
}
|
|
372
|
+
await atomicsWaitAsync(wake, 0, expected).value;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function runCommand(
|
|
378
|
+
runtime: Runtime,
|
|
379
|
+
executable: string,
|
|
380
|
+
options: CommandOptions,
|
|
381
|
+
stopped: AbortSignal,
|
|
382
|
+
) {
|
|
383
|
+
const command = new Command(runtime, executable, options, stopped);
|
|
384
|
+
return {
|
|
385
|
+
result: command.execute(),
|
|
386
|
+
signal: (signal: ProcessSignal) => command.signal(signal),
|
|
387
|
+
};
|
|
388
|
+
}
|
package/browser/shared-fs.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
type PublicationConfiguration,
|
|
6
6
|
} from "../fs/publication-internal.js";
|
|
7
7
|
import { MemoryBudget } from "../fs/memory-budget.js";
|
|
8
|
+
import type { OperationBinding } from "./operations/protocol.js";
|
|
8
9
|
import { bytes, type Headroom, type RuntimeReservations } from "../memory/reservations.js";
|
|
9
10
|
import {
|
|
10
11
|
isRevoked,
|
|
@@ -508,6 +509,21 @@ export class SharedFilesystem {
|
|
|
508
509
|
return this.client.fork();
|
|
509
510
|
}
|
|
510
511
|
connect(identity: FilesystemIdentity, headroom: Headroom | null = null): FilesystemConnection {
|
|
512
|
+
return this.#connect(identity, headroom);
|
|
513
|
+
}
|
|
514
|
+
/** Trusted broker path. The binding goes only to the dedicated service Worker. */
|
|
515
|
+
connectObserved(
|
|
516
|
+
identity: FilesystemIdentity,
|
|
517
|
+
headroom: Headroom | null | undefined,
|
|
518
|
+
operations: OperationBinding,
|
|
519
|
+
): FilesystemConnection {
|
|
520
|
+
return this.#connect(identity, headroom ?? null, operations);
|
|
521
|
+
}
|
|
522
|
+
#connect(
|
|
523
|
+
identity: FilesystemIdentity,
|
|
524
|
+
headroom: Headroom | null,
|
|
525
|
+
operations?: OperationBinding,
|
|
526
|
+
): FilesystemConnection {
|
|
511
527
|
this.assertLive();
|
|
512
528
|
// An identity can be older than the tree: a blocked guest creates its child's identity from
|
|
513
529
|
// its own directory. This page attaches every announced segment, so a child starts with all
|
|
@@ -518,9 +534,10 @@ export class SharedFilesystem {
|
|
|
518
534
|
};
|
|
519
535
|
const connection = connectFilesystemIdentity(current, this.requestPool, undefined, headroom);
|
|
520
536
|
try {
|
|
521
|
-
|
|
522
|
-
connection.server
|
|
523
|
-
|
|
537
|
+
const message = operations
|
|
538
|
+
? { connect: connection.server, operations }
|
|
539
|
+
: { connect: connection.server };
|
|
540
|
+
this.postServiceConnection(connection.server, message, [connection.server.port]);
|
|
524
541
|
} catch (error) {
|
|
525
542
|
cleanupAfterFailure([
|
|
526
543
|
() => connection.client.port.close(),
|
package/browser/worker-memory.ts
CHANGED
|
@@ -34,6 +34,9 @@ const startProgress = {
|
|
|
34
34
|
"runtime-loaded": 2,
|
|
35
35
|
"group-joined": 3,
|
|
36
36
|
"modules-loaded": 4,
|
|
37
|
+
"installing-bindings": 5,
|
|
38
|
+
"bindings-installed": 6,
|
|
39
|
+
instantiating: 7,
|
|
37
40
|
} as const;
|
|
38
41
|
export type StartProgressStep = keyof typeof startProgress;
|
|
39
42
|
|
|
@@ -48,6 +51,9 @@ export function readStartProgress(
|
|
|
48
51
|
if (code === startProgress["runtime-loaded"]) return { step: "runtime-loaded", at };
|
|
49
52
|
if (code === startProgress["group-joined"]) return { step: "group-joined", at };
|
|
50
53
|
if (code === startProgress["modules-loaded"]) return { step: "modules-loaded", at };
|
|
54
|
+
if (code === startProgress["installing-bindings"]) return { step: "installing-bindings", at };
|
|
55
|
+
if (code === startProgress["bindings-installed"]) return { step: "bindings-installed", at };
|
|
56
|
+
if (code === startProgress.instantiating) return { step: "instantiating", at };
|
|
51
57
|
return undefined;
|
|
52
58
|
}
|
|
53
59
|
|