@waffle-os/runtime 0.0.152 → 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) => {
@@ -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) => {
@@ -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),
@@ -30,6 +30,9 @@ function snapshot(executable, options) {
30
30
  throw new TypeError("Command executable must be an absolute path");
31
31
  const { args, cwd = "/", env, stdin, stdout, stderr, deadlineMs } = options;
32
32
  const { programs, onLeaderExit } = policies(options);
33
+ const { terminal = false } = options;
34
+ if (typeof terminal !== "boolean")
35
+ throw new TypeError("Command terminal must be a boolean");
33
36
  if (!path(cwd))
34
37
  throw new TypeError("Command cwd must be an absolute path");
35
38
  if (args !== undefined && !Array.isArray(args))
@@ -52,6 +55,7 @@ function snapshot(executable, options) {
52
55
  filesystem: "runtime-root",
53
56
  programs,
54
57
  onLeaderExit,
58
+ terminal,
55
59
  args: argv,
56
60
  cwd,
57
61
  env: Object.fromEntries(entries),
@@ -1 +1 @@
1
- {"commit":"72849910ef1260a1e27ec37e6b3a8e0371f71de3","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
1
+ {"commit":"5dcc58a08c0c1b3044dc6d47b4c13b688d70fa6d","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waffle-os/runtime",
3
- "version": "0.0.152",
3
+ "version": "0.0.153",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/ShaulLavo/waffle.git",
@@ -26,6 +26,12 @@ export interface CommandOptions {
26
26
  * stops them as `kill()` would once the first process has its outcome, which the result keeps.
27
27
  */
28
28
  readonly onLeaderExit?: CommandLeaderExit;
29
+ /**
30
+ * Present the standard descriptors Waffle provides (captured stdout and stderr, and stdin
31
+ * when it reads end of file) as a terminal. By default they are pipes, as a captured command
32
+ * sees on Unix: `isatty` is false. Stdin from a byte array or stream is always a pipe.
33
+ */
34
+ readonly terminal?: boolean;
29
35
  readonly args?: readonly string[];
30
36
  readonly cwd?: string;
31
37
  readonly env?: Readonly<Record<string, string>>;
@@ -94,6 +100,8 @@ function snapshot(executable: string, options: CommandOptions): CommandOptions {
94
100
  if (!path(executable)) throw new TypeError("Command executable must be an absolute path");
95
101
  const { args, cwd = "/", env, stdin, stdout, stderr, deadlineMs } = options;
96
102
  const { programs, onLeaderExit } = policies(options);
103
+ const { terminal = false } = options;
104
+ if (typeof terminal !== "boolean") throw new TypeError("Command terminal must be a boolean");
97
105
  if (!path(cwd)) throw new TypeError("Command cwd must be an absolute path");
98
106
  if (args !== undefined && !Array.isArray(args)) throw new TypeError("Invalid command args");
99
107
  const argv = Array.from(args ?? []);
@@ -121,6 +129,7 @@ function snapshot(executable: string, options: CommandOptions): CommandOptions {
121
129
  filesystem: "runtime-root",
122
130
  programs,
123
131
  onLeaderExit,
132
+ terminal,
124
133
  args: argv,
125
134
  cwd,
126
135
  env: Object.fromEntries(entries),
@@ -1 +1 @@
1
- {"commit":"72849910ef1260a1e27ec37e6b3a8e0371f71de3","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
1
+ {"commit":"5dcc58a08c0c1b3044dc6d47b4c13b688d70fa6d","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}