@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
48 lines • 1.68 kB
JavaScript
import { fstatSync } from "node:fs";
// Harnesses (ssh, CI runners, spawn with stdio "pipe") hand children a stdin that is open but never
// written or closed. Slow real producers (`curl … |`, `git log |` on a big repo) need well over 150 ms.
export const PIPED_STDIN_GRACE_MS = 2_000;
function stdinIsRegularFile(stdin) {
try {
return fstatSync(stdin.fd ?? 0).isFile();
}
catch {
return false;
}
}
export function readPipedStdin(options) {
const stdin = options.stdin ?? process.stdin;
if (!options.forceRead && stdin.isTTY) {
return Promise.resolve(undefined);
}
const isRegularFile = options.isRegularFile ?? stdinIsRegularFile;
const readToEof = options.forceRead || !options.hasPromptArgument || isRegularFile(stdin);
return new Promise((resolve) => {
let data = "";
let graceTimer;
const onData = (chunk) => {
clearTimeout(graceTimer);
data += chunk;
};
const settle = (value) => {
clearTimeout(graceTimer);
stdin.off("data", onData);
stdin.off("end", onEnd);
resolve(value);
};
const onEnd = () => settle(data.trim() || undefined);
stdin.setEncoding("utf8");
stdin.on("data", onData);
stdin.on("end", onEnd);
if (!readToEof) {
graceTimer = setTimeout(() => {
stdin.pause();
stdin.unref?.();
settle(undefined);
options.onSkip?.();
}, options.graceMs ?? PIPED_STDIN_GRACE_MS);
}
stdin.resume();
});
}
//# sourceMappingURL=piped-stdin.js.map