chore: stabilize Zhinian pilot delivery
This commit is contained in:
@@ -43,6 +43,54 @@ let logDir: string | null = null;
|
||||
const RING_BUFFER_SIZE = 500;
|
||||
const recentLogs: string[] = [];
|
||||
|
||||
// ── Console safety ───────────────────────────────────────────────
|
||||
|
||||
type ConsoleMethod = 'debug' | 'info' | 'warn' | 'error' | 'log';
|
||||
|
||||
const consoleMethods: ConsoleMethod[] = ['debug', 'info', 'warn', 'error', 'log'];
|
||||
let consolePatched = false;
|
||||
let consoleOutputDisabled = false;
|
||||
|
||||
function isBrokenConsoleOutput(error: unknown): boolean {
|
||||
const code = (error as NodeJS.ErrnoException | undefined)?.code;
|
||||
return code === 'EIO' || code === 'EPIPE' || code === 'ERR_STREAM_DESTROYED';
|
||||
}
|
||||
|
||||
function patchConsoleForBrokenOutput(): void {
|
||||
if (consolePatched) return;
|
||||
consolePatched = true;
|
||||
|
||||
const mutableConsole = console as unknown as Record<ConsoleMethod, (...args: unknown[]) => void>;
|
||||
for (const method of consoleMethods) {
|
||||
const original = mutableConsole[method].bind(console);
|
||||
mutableConsole[method] = (...args: unknown[]) => {
|
||||
if (consoleOutputDisabled) return;
|
||||
try {
|
||||
original(...args);
|
||||
} catch (error) {
|
||||
if (isBrokenConsoleOutput(error)) {
|
||||
consoleOutputDisabled = true;
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
process.stdout?.on('error', (error) => {
|
||||
if (isBrokenConsoleOutput(error)) {
|
||||
consoleOutputDisabled = true;
|
||||
}
|
||||
});
|
||||
process.stderr?.on('error', (error) => {
|
||||
if (isBrokenConsoleOutput(error)) {
|
||||
consoleOutputDisabled = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
patchConsoleForBrokenOutput();
|
||||
|
||||
// ── Async write buffer ───────────────────────────────────────────
|
||||
|
||||
/** Pending log lines waiting to be flushed to disk. */
|
||||
|
||||
Reference in New Issue
Block a user