mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 07:04:53 +00:00
* perf(terminal): flush shell output on the event-loop turn, not a fixed 8ms timer SSH/PTY output was coalesced and shipped to the renderer on a fixed 8ms timer. For interactive use that interval is pure added latency: every echoed keystroke waits out the timer before it can paint, so typing feels slightly behind. Replace the timer with turn-based (setImmediate) coalescing in a single shared ptyOutputBuffer module, used by the SSH, local, telnet, and mosh paths. A single echoed keystroke is now forwarded almost immediately, while data arriving in the same turn still collapses into one IPC send, and a 16KB size cap still forces an immediate flush under heavy output. Also de-duplicates two copies of the buffering logic (SSH had an inline copy; local/telnet/mosh shared another) and adds unit tests for the buffer. Related to #1084. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(terminal): drop orphaned flushTimeout reference in SSH close handler The SSH stream "close" handler still cleared `flushTimeout`, a variable that lived in the inline buffer removed when this path moved to the shared ptyOutputBuffer. Reading it now throws ReferenceError on every channel close, aborting the cleanup and exit signaling. The shared buffer's flush() cancels any pending flush internally, so the timer bookkeeping is removed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
"use strict";
|
|
|
|
/**
|
|
* Coalescing output buffer for terminal/PTY data on its way to the renderer.
|
|
*
|
|
* Incoming shell data is accumulated and delivered to `sendFn` in batches to
|
|
* keep IPC traffic down, but the batch is flushed on the *next event-loop turn*
|
|
* (`setImmediate`) rather than after a fixed time interval. A fixed interval
|
|
* adds that whole interval as latency to interactive echo — every keystroke
|
|
* round-trips through the buffer and waits out the timer before it can paint.
|
|
* Turn-based flushing coalesces only the data that has already arrived in the
|
|
* current turn, so a single echoed keystroke is forwarded almost immediately
|
|
* while bursts of output still collapse into one send.
|
|
*
|
|
* A byte cap still forces an immediate, synchronous flush so a flood of output
|
|
* can't grow the buffer without bound between turns.
|
|
*
|
|
* @param {(data: string) => void} sendFn delivers an accumulated batch
|
|
* @param {{ maxBufferSize?: number }} [options]
|
|
* @returns {{ bufferData: (data: string) => void, flush: () => void }}
|
|
*/
|
|
function createPtyOutputBuffer(sendFn, options = {}) {
|
|
const maxBufferSize = options.maxBufferSize ?? 16384; // 16KB
|
|
|
|
let dataBuffer = "";
|
|
let scheduled = null;
|
|
|
|
const cancelScheduled = () => {
|
|
if (scheduled) {
|
|
clearImmediate(scheduled);
|
|
scheduled = null;
|
|
}
|
|
};
|
|
|
|
const flushNow = () => {
|
|
scheduled = null;
|
|
if (dataBuffer.length > 0) {
|
|
const pending = dataBuffer;
|
|
dataBuffer = "";
|
|
sendFn(pending);
|
|
}
|
|
};
|
|
|
|
const bufferData = (data) => {
|
|
dataBuffer += data;
|
|
if (dataBuffer.length >= maxBufferSize) {
|
|
// Large enough to ship right now — don't wait for the turn flush.
|
|
cancelScheduled();
|
|
flushNow();
|
|
} else if (!scheduled) {
|
|
scheduled = setImmediate(flushNow);
|
|
}
|
|
};
|
|
|
|
const flush = () => {
|
|
cancelScheduled();
|
|
flushNow();
|
|
};
|
|
|
|
return { bufferData, flush };
|
|
}
|
|
|
|
module.exports = { createPtyOutputBuffer };
|