mirror of
https://github.com/morluto/rea.git
synced 2026-12-02 13:11:18 -05:00
148 lines
4.3 KiB
TypeScript
148 lines
4.3 KiB
TypeScript
import { spawn, type ChildProcess } from "node:child_process";
|
|
import { fileURLToPath } from "node:url";
|
|
import { OwnedCommandFailure } from "../../src/process/OwnedCommand.js";
|
|
import type {
|
|
ProviderProcessHandle,
|
|
ProviderProcessSnapshot,
|
|
} from "../../src/process/ProviderProcess.js";
|
|
import { testWaitDeadline } from "../support/testWait.js";
|
|
|
|
const fixturePath = fileURLToPath(
|
|
new URL("./providerProcess.mjs", import.meta.url),
|
|
);
|
|
|
|
/** Spawn one deterministic process-lifecycle fixture with piped diagnostics. */
|
|
export const spawnProviderProcessFixture = (
|
|
mode:
|
|
| "burst"
|
|
| "detached-child"
|
|
| "exit"
|
|
| "graceful"
|
|
| "invalid-utf8"
|
|
| "valid-multibyte"
|
|
| "late-stderr"
|
|
| "stubborn",
|
|
value?: number,
|
|
): ChildProcess =>
|
|
spawn(
|
|
process.execPath,
|
|
[fixturePath, mode, ...(value === undefined ? [] : [String(value)])],
|
|
{
|
|
stdio:
|
|
mode === "late-stderr"
|
|
? ["ignore", "pipe", "pipe", "pipe"]
|
|
: ["ignore", "pipe", "pipe"],
|
|
},
|
|
);
|
|
|
|
/** Wait until a long-lived fixture has installed its signal behavior. */
|
|
export const waitForProviderProcessReady = (
|
|
child: ProviderProcessHandle,
|
|
timeoutMs = testWaitDeadline(),
|
|
): Promise<void> =>
|
|
new Promise((resolve, reject) => {
|
|
const stdout = child.stdout;
|
|
if (stdout === null) {
|
|
reject(new Error("Provider process fixture stdout is unavailable"));
|
|
return;
|
|
}
|
|
let output = "";
|
|
const detach = (): void => {
|
|
clearTimeout(timer);
|
|
stdout.off("data", onData);
|
|
child.off("exit", onExit);
|
|
};
|
|
const onData = (chunk: Buffer | string): void => {
|
|
output += chunk.toString();
|
|
if (output.includes("ready\n")) {
|
|
detach();
|
|
resolve();
|
|
}
|
|
};
|
|
const onExit = (): void => {
|
|
detach();
|
|
reject(new Error("Provider process fixture exited before readiness"));
|
|
};
|
|
const timer = setTimeout(() => {
|
|
detach();
|
|
reject(new Error("Provider process fixture readiness timed out"));
|
|
}, timeoutMs);
|
|
stdout.on("data", onData);
|
|
child.once("exit", onExit);
|
|
});
|
|
|
|
/** Read the distinct process-group leader created by the detached-child mode. */
|
|
export const waitForDetachedProviderChild = (
|
|
child: ProviderProcessHandle,
|
|
timeoutMs = testWaitDeadline(),
|
|
): Promise<number> =>
|
|
new Promise((resolve, reject) => {
|
|
const stdout = child.stdout;
|
|
if (stdout === null) {
|
|
reject(new Error("Provider process fixture stdout is unavailable"));
|
|
return;
|
|
}
|
|
let output = "";
|
|
const detach = (): void => {
|
|
clearTimeout(timer);
|
|
stdout.off("data", onData);
|
|
child.off("exit", onExit);
|
|
};
|
|
const onData = (chunk: Buffer | string): void => {
|
|
output += chunk.toString();
|
|
const match = /(?:^|\n)ready:(\d+)\n/u.exec(output);
|
|
if (match?.[1] === undefined) return;
|
|
detach();
|
|
resolve(Number(match[1]));
|
|
};
|
|
const onExit = (): void => {
|
|
detach();
|
|
reject(new Error("Provider process fixture exited before readiness"));
|
|
};
|
|
const timer = setTimeout(() => {
|
|
detach();
|
|
reject(new Error("Provider process fixture readiness timed out"));
|
|
}, timeoutMs);
|
|
stdout.on("data", onData);
|
|
child.once("exit", onExit);
|
|
});
|
|
|
|
/** Kill a fixture if necessary and wait for its process handle to settle. */
|
|
export const stopProviderProcessFixture = async (
|
|
child: ProviderProcessHandle,
|
|
): Promise<void> => {
|
|
if (child.exitCode !== null || child.signalCode !== null) return;
|
|
const exited = new Promise<void>((resolve) => {
|
|
child.once("exit", () => resolve());
|
|
});
|
|
child.kill("SIGKILL");
|
|
await exited;
|
|
};
|
|
|
|
/** Build an owned-process snapshot with a complete V8 heap diagnostic. */
|
|
export const v8HeapExhaustionFailure = (
|
|
message: string,
|
|
stderr: string,
|
|
termination: Pick<ProviderProcessSnapshot, "exitCode" | "signal"> = {
|
|
exitCode: null,
|
|
signal: "SIGABRT",
|
|
},
|
|
): OwnedCommandFailure => {
|
|
const bytes = Buffer.byteLength(stderr);
|
|
return new OwnedCommandFailure("process", message, {
|
|
stdout: { text: "", bytes: 0, observedBytes: 0 },
|
|
stderr: { text: stderr, bytes, observedBytes: bytes },
|
|
exitCode: termination.exitCode,
|
|
signal: termination.signal,
|
|
runtimeExhaustion: {
|
|
runtime: "v8",
|
|
area: "heap",
|
|
stream: "stderr",
|
|
line: stderr.trim(),
|
|
lineComplete: true,
|
|
truncated: false,
|
|
invalidUtf8: false,
|
|
},
|
|
});
|
|
};
|