源码快照 @ 2026-09-02 (source-only, 剔除非源码素材)
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
const guardedStreams = new WeakSet();
|
||||
|
||||
export function isBrokenPipeError(error) {
|
||||
return Boolean(error && typeof error === "object" && error.code === "EPIPE");
|
||||
}
|
||||
|
||||
/**
|
||||
* Install once-per-stream error handlers. Only the EventEmitter surface is
|
||||
* needed, so tests can pass plain emitters in place of process streams.
|
||||
*
|
||||
* @param {{ stdout?: NodeJS.EventEmitter, stderr?: NodeJS.EventEmitter }} [streams]
|
||||
*/
|
||||
export function installStdioErrorHandlers({ stdout = process.stdout, stderr = process.stderr } = {}) {
|
||||
for (const stream of [stdout, stderr]) {
|
||||
if (!stream || typeof stream.on !== "function" || guardedStreams.has(stream)) continue;
|
||||
|
||||
stream.on("error", (error) => {
|
||||
if (isBrokenPipeError(error)) return;
|
||||
throw error;
|
||||
});
|
||||
guardedStreams.add(stream);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user