| 1751 | const originalWrite = stderr.write; |
| 1752 | let reentered = false; |
| 1753 | const intercept = ( |
| 1754 | chunk: Uint8Array | string, |
| 1755 | encodingOrCb?: BufferEncoding | ((err?: Error | null) => void), |
| 1756 | cb?: (err?: Error | null) => void, |
| 1757 | ): boolean => { |
| 1758 | const callback = typeof encodingOrCb === 'function' ? encodingOrCb : cb; |
| 1759 | // Reentrancy guard: logger.debug → writeToStderr → here. Pass |
| 1760 | // through to the original so --debug-to-stderr still works and we |
| 1761 | // don't stack-overflow. |
| 1762 | if (reentered) { |
| 1763 | const encoding = typeof encodingOrCb === 'string' ? encodingOrCb : undefined; |
| 1764 | return originalWrite.call(stderr, chunk, encoding, callback); |
| 1765 | } |
| 1766 | reentered = true; |
| 1767 | try { |
| 1768 | const text = typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf8'); |
| 1769 | this.logger.debug(`[stderr] ${text}`, { level: 'warn' }); |
| 1770 | if (this.altScreenActive && !this.isUnmounted && !this.isPaused) { |
| 1771 | this.prevFrameContaminated = true; |
| 1772 | this.scheduleRender(); |
| 1773 | } |
| 1774 | } finally { |
| 1775 | reentered = false; |
| 1776 | callback?.(); |
| 1777 | } |
| 1778 | return true; |
| 1779 | }; |
| 1780 | stderr.write = intercept; |
| 1781 | return () => { |
| 1782 | if (stderr.write === intercept) { |