import { spawn } from "node:child_process"; import type { ProcessResult } from "../types.js"; export interface RunOptions { timeoutMs?: number; maxOutputBytes?: number; cwd?: string; env?: NodeJS.ProcessEnv; } export function runBounded(command: string, args: string[] = [], options: RunOptions = {}): Promise { const timeoutMs = options.timeoutMs ?? 30_000; const maxOutputBytes = options.maxOutputBytes ?? 1_000_000; return new Promise((resolve, reject) => { const child = spawn(command, args, { cwd: options.cwd, env: options.env ?? process.env, stdio: ["ignore", "pipe", "pipe"], windowsHide: true, }); let stdout = ""; let stderr = ""; let truncated = false; let timedOut = false; let settled = false; const append = (current: string, chunk: Buffer): string => { const next = current + chunk.toString("utf8"); if (Buffer.byteLength(next) <= maxOutputBytes) return next; truncated = true; return Buffer.from(next).subarray(0, maxOutputBytes).toString("utf8"); }; child.stdout.on("data", (chunk: Buffer) => { stdout = append(stdout, chunk); }); child.stderr.on("data", (chunk: Buffer) => { stderr = append(stderr, chunk); }); const timer = setTimeout(() => { timedOut = true; child.kill(); }, timeoutMs); child.once("error", (error) => { clearTimeout(timer); if (settled) return; settled = true; reject(error); }); child.once("close", (code, signal) => { clearTimeout(timer); if (settled) return; settled = true; resolve({ code, signal, stdout, stderr, timedOut, truncated }); }); }); }