Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - the host runs without the Effect runtime to stay small.
2// `t3 __spawn-host` must import only Node builtins to keep its heap small.
3// Transfer stdio sockets over IPC so output bypasses the host.
4import * as NodeChildProcess from "node:child_process";
5import type * as NodeNet from "node:net";
6
7export interface SpawnHostSpawnRequest {
8 readonly type: "spawn";
9 readonly id: number;
10 readonly command: string;
11 readonly args: ReadonlyArray<string>;
12 readonly options: NodeChildProcess.SpawnOptions;
13}
14
15export interface SpawnHostError {
16 readonly message: string;
17 readonly code?: string | undefined;
18 readonly errno?: number | undefined;
19 readonly syscall?: string | undefined;
20 readonly path?: string | undefined;
21 readonly spawnargs?: ReadonlyArray<string> | undefined;
22}
23
24/** Sent in order per child: `stdio` once per piped fd (carrying the socket), `spawn`, then `exit`. */
25export type SpawnHostEvent =
26 | { readonly type: "ready" }
27 | { readonly type: "stdio"; readonly id: number; readonly fd: number }
28 | { readonly type: "spawn"; readonly id: number; readonly pid: number }
29 | {
30 readonly type: "exit";
31 readonly id: number;
32 readonly code: number | null;
33 readonly signal: NodeJS.Signals | null;
34 }
35 | { readonly type: "error"; readonly id: number; readonly error: SpawnHostError };
36
37const toSpawnHostError = (cause: unknown): SpawnHostError => {
38 const error = cause as Partial<NodeJS.ErrnoException> & { spawnargs?: ReadonlyArray<string> };
39 return {
40 message: typeof error?.message === "string" ? error.message : String(cause),
41 code: error?.code,
42 errno: error?.errno,
43 syscall: error?.syscall,
44 path: error?.path,
45 spawnargs: error?.spawnargs,
46 };
47};
48
49// Child stdio has no public paused option; use net's pauseOnCreate mechanism.
50const stopReading = (socket: NodeNet.Socket) =>
51 (socket as unknown as { _handle?: { readStop?: () => void } | null })._handle?.readStop?.();
52
53export function runSpawnHost(): void {
54 const channel = process.send?.bind(process);
55 if (!channel) {
56 process.stderr.write("t3 __spawn-host is started by the T3 server.\n");
57 process.exitCode = 1;
58 return;
59 }
60 const send = (event: SpawnHostEvent, socket?: NodeNet.Socket) => channel(event, socket);
61
62 // Keep reporting exits during group shutdown until the server closes IPC.
63 for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) process.on(signal, () => {});
64 process.on("disconnect", () => process.exit(0));
65
66 process.on("message", (request: SpawnHostSpawnRequest) => {
67 if (request.type !== "spawn") return;
68 const { id } = request;
69 let child: NodeChildProcess.ChildProcess;
70 try {
71 child = NodeChildProcess.spawn(request.command, [...request.args], request.options);
72 } catch (cause) {
73 send({ type: "error", id, error: toSpawnHostError(cause) });
74 return;
75 }
76 // Stop reads before the loop polls, preserving every byte for the server.
77 const stdio = child.stdio.map((socket) => {
78 if (socket) stopReading(socket as NodeNet.Socket);
79 return socket as NodeNet.Socket | null;
80 });
81 // ChildProcess destroys stdin on exit, which can come before its socket leaves the send queue.
82 child.stdin = null;
83 child.on("error", (cause) => send({ type: "error", id, error: toSpawnHostError(cause) }));
84 child.once("spawn", () => {
85 stdio.forEach((socket, fd) => {
86 if (socket) send({ type: "stdio", id, fd }, socket);
87 });
88 send({ type: "spawn", id, pid: child.pid! });
89 });
90 child.on("exit", (code, signal) => send({ type: "exit", id, code, signal }));
91 });
92 send({ type: "ready" });
93}