@waffle-os/runtime 0.0.151 → 0.0.153

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.
@@ -1,6 +1,6 @@
1
1
  import { allocateSharedControl } from "../memory/control.js";
2
2
  import { commandTransferBytes } from "../proc/command-transfer.js";
3
- import { DescriptorTable } from "../core/descriptors.js";
3
+ import { DescriptorTable, type Stream } from "../core/descriptors.js";
4
4
  import type { Runtime } from "../runtime.js";
5
5
  import { commandDisposals, commandOwners } from "./command-owner.js";
6
6
  import { controlLoad } from "../proc/control.js";
@@ -21,7 +21,7 @@ import {
21
21
  type WorkerSpawnTransfer,
22
22
  } from "../proc/worker-client.js";
23
23
  import type { BrowserProcessBroker } from "./process-broker.js";
24
- import { createRuntimeTerminalCodec } from "./runtime-terminals.js";
24
+ import { createRuntimeTerminalCodec, hostPipeDestination } from "./runtime-terminals.js";
25
25
  import { CommandInput } from "./command-input.js";
26
26
  import { yieldTask } from "./yield-task.js";
27
27
 
@@ -328,18 +328,24 @@ class Command {
328
328
  continue;
329
329
  this.descriptors.openAt(
330
330
  fd,
331
- {
332
- kind: "terminal",
333
- read: () => 0,
334
- write: () => 0,
335
- readiness: () => ({ readable: fd === 0, writable: fd !== 0, hangup: fd === 0 }),
336
- },
331
+ this.hostStdio(fd),
337
332
  (fd === 0 ? READ : WRITE) | POLL | FD_FILESTAT_GET,
338
333
  0n,
339
334
  );
340
335
  }
341
336
  }
342
337
 
338
+ /** Captured stdio is a pipe, as on Unix, unless the caller asked for a terminal. */
339
+ private hostStdio(fd: 0 | 1 | 2): Stream {
340
+ const stream: Stream = {
341
+ kind: this.options.terminal ? "terminal" : "pipe",
342
+ read: () => 0,
343
+ write: () => 0,
344
+ readiness: () => ({ readable: fd === 0, writable: fd !== 0, hangup: fd === 0 }),
345
+ };
346
+ return this.options.terminal ? stream : hostPipeDestination(fd, stream);
347
+ }
348
+
343
349
  private deliver(fd: 1 | 2, bytes: Uint8Array): number {
344
350
  if (this.outputBatch === 0) {
345
351
  this.outputTask ??= yieldTask().then(() => {
@@ -11,6 +11,8 @@ type TerminalTarget = 0 | 1 | 2;
11
11
  interface TerminalToken {
12
12
  kind: "web-terminal";
13
13
  target: TerminalTarget;
14
+ /** The destination is presented to guests as a pipe, as captured output is on Unix. */
15
+ pipe?: true;
14
16
  }
15
17
  interface NullDeviceToken {
16
18
  kind: "null-device";
@@ -19,7 +21,18 @@ interface NullDeviceToken {
19
21
  // A remapped fd is not a new destination. Imported terminals retain their provenance across
20
22
  // codec instances and later generations; WeakMap entries disappear with their local streams.
21
23
  const targets = new WeakMapConstructor<Stream, TerminalTarget>();
24
+ // Host destinations a guest sees as pipes. They keep their target, so their writes still reach
25
+ // the same host output, but fd_kind and fdstat report a pipe and isatty is false.
26
+ const pipeDestinations = new WeakMapConstructor<Stream, true>();
22
27
  const NOSYS = 52;
28
+
29
+ /** A pipe-kind stream at fd `target` that a codec exports as that host destination. */
30
+ export function hostPipeDestination(target: TerminalTarget, stream: Stream): Stream {
31
+ if (stream.kind !== "pipe") throw new TypeError("host pipe destination must be a pipe");
32
+ weakMapSet(targets, stream, target);
33
+ weakMapSet(pipeDestinations, stream, true);
34
+ return stream;
35
+ }
23
36
  export interface RuntimeTerminalCodecOptions {
24
37
  fallback: WorkerDescriptorCodec;
25
38
  /** Initial default stdio only. Existing imported provenance wins over the current fd number. */
@@ -56,6 +69,7 @@ function terminalToken(value: unknown): TerminalToken | undefined {
56
69
  return;
57
70
  if (!("target" in value) || (value.target !== 0 && value.target !== 1 && value.target !== 2))
58
71
  throw new TypeError("invalid browser terminal target");
72
+ if ("pipe" in value && value.pipe !== true) throw new TypeError("invalid browser terminal kind");
59
73
  return value as TerminalToken;
60
74
  }
61
75
  function resolveTerminal(
@@ -95,6 +109,8 @@ export function createRuntimeTerminalCodec(
95
109
  // transfers. A receiving executor may resolve that destination to an interactive stream.
96
110
  if (target === undefined)
97
111
  throw new ProcessError(NOSYS, "terminal has no transferable browser destination");
112
+ if (weakMapHas(pipeDestinations, resource))
113
+ return { kind: "web-terminal", target, pipe: true } satisfies TerminalToken;
98
114
  return { kind: "web-terminal", target } satisfies TerminalToken;
99
115
  },
100
116
  import(value, pid) {
@@ -102,6 +118,16 @@ export function createRuntimeTerminalCodec(
102
118
  if (nullDevice) return new OpenDescription(createNullDevice(nullDevice.fdflags));
103
119
  const token = terminalToken(value);
104
120
  if (!token) return options.fallback.import(value, pid);
121
+ // A pipe destination is never an interactive terminal, so it never resolves to one.
122
+ if (token.pipe) {
123
+ const stream = hostTerminalStream(
124
+ token.target,
125
+ options.output,
126
+ options.checkActive,
127
+ "pipe",
128
+ );
129
+ return new OpenDescription(hostPipeDestination(token.target, stream));
130
+ }
105
131
  const resolved = resolveTerminal(options, token.target);
106
132
  if (resolved !== undefined) {
107
133
  const remember = (stream: Stream) => {
@@ -219,10 +219,25 @@ export function createPreviewServiceWorker(options: PreviewServiceWorkerOptions)
219
219
  };
220
220
  }
221
221
 
222
- /** Await activation even when the registering page is outside the worker's scope. */
222
+ /**
223
+ * Await activation even when the registering page is outside the worker's scope.
224
+ *
225
+ * A worker in the registration's active slot is already activating or activated, and fetches it
226
+ * handles wait for its activation, so it needs no wait. Only an installing or waiting worker is
227
+ * watched. WebKit can resolve `register()` in a second page that registered at the same moment with
228
+ * the active worker's state still reading "installing", and never sends that object another
229
+ * `statechange`; waiting on it would hang forever. A "redundant" state is still a failure: the spec
230
+ * queues that state update before it clears the active slot, so the two can briefly disagree.
231
+ */
223
232
  export function activated(registration: ServiceWorkerRegistration): Promise<void> {
224
- const worker = registration.installing ?? registration.waiting ?? registration.active;
225
- if (!worker) return Promise.reject(new Error("service worker registration has no worker"));
233
+ const worker = registration.installing ?? registration.waiting;
234
+ if (!worker) {
235
+ const active = registration.active;
236
+ if (!active) return Promise.reject(new Error("service worker registration has no worker"));
237
+ if (active.state === "redundant")
238
+ return Promise.reject(new Error("service worker became redundant before activation"));
239
+ return Promise.resolve();
240
+ }
226
241
  return new Promise((resolve, reject) => {
227
242
  const check = () => {
228
243
  if (worker.state !== "activated" && worker.state !== "redundant") return;
@@ -458,10 +458,12 @@ export function deliveredTerminalOutput(deliver: TerminalOutputDelivery): Runtim
458
458
  }
459
459
 
460
460
  /** A host terminal description. Without `output`, writes fail with NOSYS. */
461
+ /** A host destination. `kind: "pipe"` presents it to the guest as a pipe rather than a TTY. */
461
462
  export function hostTerminalStream(
462
463
  target: 0 | 1 | 2,
463
464
  output: RuntimeTerminalOutput | undefined,
464
465
  checkActive?: () => void,
466
+ kind: "terminal" | "pipe" = "terminal",
465
467
  ): Stream {
466
468
  const destination = target === 2 ? 2 : 1;
467
469
  const read = () => {
@@ -477,7 +479,7 @@ export function hostTerminalStream(
477
479
  : output.tryWrite(destination, chunks);
478
480
  };
479
481
  return {
480
- kind: "terminal",
482
+ kind,
481
483
  read,
482
484
  tryRead: read,
483
485
  write: (chunks) => send(true, chunks),
@@ -13,7 +13,7 @@ import {
13
13
  WorkerProcessClient,
14
14
  workerProcessState
15
15
  } from "../proc/worker-client.js";
16
- import { createRuntimeTerminalCodec } from "./runtime-terminals.js";
16
+ import { createRuntimeTerminalCodec, hostPipeDestination } from "./runtime-terminals.js";
17
17
  import { CommandInput } from "./command-input.js";
18
18
  import { yieldTask } from "./yield-task.js";
19
19
  const READ = 1n << 1n;
@@ -318,14 +318,18 @@ class Command {
318
318
  for (const fd of [0, 1, 2]) {
319
319
  if (fd === 0 && stdin !== undefined || fd === 1 && this.options.stdout === "closed" || fd === 2 && this.options.stderr === "closed")
320
320
  continue;
321
- this.descriptors.openAt(fd, {
322
- kind: "terminal",
323
- read: () => 0,
324
- write: () => 0,
325
- readiness: () => ({ readable: fd === 0, writable: fd !== 0, hangup: fd === 0 })
326
- }, (fd === 0 ? READ : WRITE) | POLL | FD_FILESTAT_GET, 0n);
321
+ this.descriptors.openAt(fd, this.hostStdio(fd), (fd === 0 ? READ : WRITE) | POLL | FD_FILESTAT_GET, 0n);
327
322
  }
328
323
  }
324
+ hostStdio(fd) {
325
+ const stream = {
326
+ kind: this.options.terminal ? "terminal" : "pipe",
327
+ read: () => 0,
328
+ write: () => 0,
329
+ readiness: () => ({ readable: fd === 0, writable: fd !== 0, hangup: fd === 0 })
330
+ };
331
+ return this.options.terminal ? stream : hostPipeDestination(fd, stream);
332
+ }
329
333
  deliver(fd, bytes) {
330
334
  if (this.outputBatch === 0) {
331
335
  this.outputTask ??= yieldTask().then(() => {
@@ -5,7 +5,15 @@ import { ProcessError } from "../proc/types.js";
5
5
  import { WeakMapConstructor, weakMapGet, weakMapHas, weakMapSet } from "../proc/intrinsics.js";
6
6
  import { hostTerminalStream } from "./terminal-output.js";
7
7
  const targets = new WeakMapConstructor;
8
+ const pipeDestinations = new WeakMapConstructor;
8
9
  const NOSYS = 52;
10
+ export function hostPipeDestination(target, stream) {
11
+ if (stream.kind !== "pipe")
12
+ throw new TypeError("host pipe destination must be a pipe");
13
+ weakMapSet(targets, stream, target);
14
+ weakMapSet(pipeDestinations, stream, true);
15
+ return stream;
16
+ }
9
17
  function nullDeviceToken(value) {
10
18
  if (typeof value !== "object" || value === null || !("kind" in value) || value.kind !== "null-device")
11
19
  return;
@@ -19,6 +27,8 @@ function terminalToken(value) {
19
27
  return;
20
28
  if (!("target" in value) || value.target !== 0 && value.target !== 1 && value.target !== 2)
21
29
  throw new TypeError("invalid browser terminal target");
30
+ if ("pipe" in value && value.pipe !== true)
31
+ throw new TypeError("invalid browser terminal kind");
22
32
  return value;
23
33
  }
24
34
  function resolveTerminal(options, target) {
@@ -52,6 +62,8 @@ export function createRuntimeTerminalCodec(options) {
52
62
  return options.fallback.export(descriptor, pid);
53
63
  if (target === undefined)
54
64
  throw new ProcessError(NOSYS, "terminal has no transferable browser destination");
65
+ if (weakMapHas(pipeDestinations, resource))
66
+ return { kind: "web-terminal", target, pipe: true };
55
67
  return { kind: "web-terminal", target };
56
68
  },
57
69
  import(value, pid) {
@@ -61,6 +73,10 @@ export function createRuntimeTerminalCodec(options) {
61
73
  const token = terminalToken(value);
62
74
  if (!token)
63
75
  return options.fallback.import(value, pid);
76
+ if (token.pipe) {
77
+ const stream = hostTerminalStream(token.target, options.output, options.checkActive, "pipe");
78
+ return new OpenDescription(hostPipeDestination(token.target, stream));
79
+ }
64
80
  const resolved = resolveTerminal(options, token.target);
65
81
  if (resolved !== undefined) {
66
82
  const remember = (stream) => {
@@ -177,9 +177,15 @@ export function createPreviewServiceWorker(options) {
177
177
  };
178
178
  }
179
179
  export function activated(registration) {
180
- const worker = registration.installing ?? registration.waiting ?? registration.active;
181
- if (!worker)
182
- return Promise.reject(new Error("service worker registration has no worker"));
180
+ const worker = registration.installing ?? registration.waiting;
181
+ if (!worker) {
182
+ const active = registration.active;
183
+ if (!active)
184
+ return Promise.reject(new Error("service worker registration has no worker"));
185
+ if (active.state === "redundant")
186
+ return Promise.reject(new Error("service worker became redundant before activation"));
187
+ return Promise.resolve();
188
+ }
183
189
  return new Promise((resolve, reject) => {
184
190
  const check = () => {
185
191
  if (worker.state !== "activated" && worker.state !== "redundant")
@@ -386,7 +386,7 @@ export function deliveredTerminalOutput(deliver) {
386
386
  writable: () => true
387
387
  };
388
388
  }
389
- export function hostTerminalStream(target, output, checkActive) {
389
+ export function hostTerminalStream(target, output, checkActive, kind = "terminal") {
390
390
  const destination = target === 2 ? 2 : 1;
391
391
  const read = () => {
392
392
  checkActive?.();
@@ -401,7 +401,7 @@ export function hostTerminalStream(target, output, checkActive) {
401
401
  return blocking && output.write ? output.write(destination, chunks) : output.tryWrite(destination, chunks);
402
402
  };
403
403
  return {
404
- kind: "terminal",
404
+ kind,
405
405
  read,
406
406
  tryRead: read,
407
407
  write: (chunks) => send(true, chunks),