fix(setup): retain bounded startup diagnostics without losing failures

This commit is contained in:
morluto committed 2026-10-26 02:33:31 +08:00
1 parent 37a7c2d652
commit db4d3abcda
4 files changed
+114 -62

No files matched your search

+8 -40
View File
@@ -1,4 +1,5 @@
import { spawn } from "node:child_process";
import { BoundedOutputExcerpt } from "./BoundedOutputExcerpt.js";
export interface BoundedCommandOutput {
readonly exitCode: string | number | null;
@@ -21,31 +22,10 @@ export const execFileBoundedOutput = (
env: options.env,
stdio: ["ignore", "pipe", "pipe"],
});
const headLimit = Math.ceil(options.limit / 2);
const tailLimit = options.limit - headLimit;
const stdout = { head: Buffer.alloc(0), tail: Buffer.alloc(0), total: 0 };
const stderr = { head: Buffer.alloc(0), tail: Buffer.alloc(0), total: 0 };
let outputTruncated = false;
const capture =
(stream: typeof stdout) =>
(chunk: Buffer): void => {
stream.total += chunk.length;
const headCount = Math.min(
chunk.length,
Math.max(0, headLimit - stream.head.length),
);
if (headCount > 0)
stream.head = Buffer.concat([
stream.head,
chunk.subarray(0, headCount),
]);
const rest = chunk.subarray(headCount);
if (rest.length > 0 && tailLimit > 0)
stream.tail = Buffer.concat([stream.tail, rest]).subarray(-tailLimit);
if (stream.total > options.limit) outputTruncated = true;
};
child.stdout.on("data", capture(stdout));
child.stderr.on("data", capture(stderr));
const stdout = new BoundedOutputExcerpt(options.limit);
const stderr = new BoundedOutputExcerpt(options.limit);
child.stdout.on("data", (chunk: Buffer) => stdout.append(chunk));
child.stderr.on("data", (chunk: Buffer) => stderr.append(chunk));
let spawnError: NodeJS.ErrnoException | undefined;
child.on("error", (error: NodeJS.ErrnoException) => {
spawnError = error;
@@ -53,21 +33,9 @@ export const execFileBoundedOutput = (
child.on("close", (code, signal) => {
resolve({
exitCode: spawnError?.code ?? code ?? signal,
stdout: retainedOutput(stdout, options.limit),
stderr: retainedOutput(stderr, options.limit),
outputTruncated,
stdout: stdout.text,
stderr: stderr.text,
outputTruncated: stdout.truncated || stderr.truncated,
});
});
});
const retainedOutput = (
stream: {
readonly head: Buffer;
readonly tail: Buffer;
readonly total: number;
},
limit: number,
): string =>
stream.total > limit
? `${stream.head.toString("utf8")} … [output omitted] … ${stream.tail.toString("utf8")}`
: Buffer.concat([stream.head, stream.tail]).toString("utf8");