repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/rtk/src/exec.ts
Rawimport { execFile } from "node:child_process";
/** RTK probes need bounded capture even on Pi 1.4, whose exec ignores maxBuffer. */
export function execRtkProcess(
command: string,
args: string[],
options: { timeout: number; maxBuffer: number; signal?: AbortSignal },
): Promise<{
stdout: string;
stderr: string;
code: number;
killed: boolean;
truncated: boolean;
}> {
return new Promise((resolve) => {
if (options.signal?.aborted) {
resolve({
stdout: "",
stderr: "",
code: -1,
killed: true,
truncated: false,
});
return;
}
execFile(
command,
args,
{
...options,
encoding: "utf8",
windowsHide: true,
// These short-lived probes need no graceful shutdown.
killSignal: "SIGKILL",
},
(error, stdout, stderr) => {
// Node caps each stream separately, so total capture is at most 2 × maxBuffer.
const truncated = error?.code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER";
resolve({
stdout,
stderr,
code: error ? (typeof error.code === "number" ? error.code : -1) : 0,
killed: Boolean(error?.killed) || truncated,
truncated,
});
},
);
});
}