MCPcopy Create free account
hub / github.com/Noumena-Network/code / patchStderr

Method patchStderr

src/ink/ink.tsx:1764–1799  ·  view source on GitHub ↗

* Intercept process.stderr.write so stray writes (config.ts, hooks.ts, * third-party deps) don't corrupt the alt-screen buffer. patchConsole only * hooks console.* methods — direct stderr writes bypass it, land at the * parked cursor, scroll the alt-screen, and desync frontFrame from the

()

Source from the content-addressed store, hash-verified

1762 * process.stdout — Ink itself writes there.
1763 */
1764 private patchStderr(): () => void {
1765 const stderr = process.stderr;
1766 const originalWrite = stderr.write;
1767 let reentered = false;
1768 const intercept = (chunk: Uint8Array | string, encodingOrCb?: BufferEncoding | ((err?: Error) => void), cb?: (err?: Error) => void): boolean => {
1769 const callback = typeof encodingOrCb === 'function' ? encodingOrCb : cb;
1770 // Reentrancy guard: logForDebugging → writeToStderr → here. Pass
1771 // through to the original so --debug-to-stderr still works and we
1772 // don't stack-overflow.
1773 if (reentered) {
1774 const encoding = typeof encodingOrCb === 'string' ? encodingOrCb : undefined;
1775 return originalWrite.call(stderr, chunk, encoding, callback);
1776 }
1777 reentered = true;
1778 try {
1779 const text = typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf8');
1780 logForDebugging(`[stderr] ${text}`, {
1781 level: 'warn'
1782 });
1783 if (this.altScreenActive && !this.isUnmounted && !this.isPaused) {
1784 this.prevFrameContaminated = true;
1785 this.scheduleRender();
1786 }
1787 } finally {
1788 reentered = false;
1789 callback?.();
1790 }
1791 return true;
1792 };
1793 stderr.write = intercept;
1794 return () => {
1795 if (stderr.write === intercept) {
1796 stderr.write = originalWrite;
1797 }
1798 };
1799 }
1800}
1801
1802/**

Callers 1

constructorMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected