mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 15:19:16 +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>
91 lines
2.5 KiB
JavaScript
91 lines
2.5 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
|
|
const { createPtyOutputBuffer } = require("./ptyOutputBuffer.cjs");
|
|
|
|
/** Resolve after one event-loop turn (immediates have run). */
|
|
const tick = () => new Promise((resolve) => setImmediate(resolve));
|
|
|
|
test("coalesces data buffered within the same turn into a single send", async () => {
|
|
const sends = [];
|
|
const buffer = createPtyOutputBuffer((data) => sends.push(data));
|
|
|
|
buffer.bufferData("a");
|
|
buffer.bufferData("b");
|
|
buffer.bufferData("c");
|
|
|
|
// Nothing is sent synchronously while still in the same turn.
|
|
assert.equal(sends.length, 0);
|
|
|
|
await tick();
|
|
|
|
assert.deepEqual(sends, ["abc"]);
|
|
});
|
|
|
|
test("flushes within a single event-loop turn (not on a fixed delay)", async () => {
|
|
const sends = [];
|
|
const buffer = createPtyOutputBuffer((data) => sends.push(data));
|
|
|
|
buffer.bufferData("x");
|
|
|
|
// A fixed-interval (e.g. 8ms) buffer would NOT have flushed after one
|
|
// immediate turn. Turn-based flushing must have delivered it by now.
|
|
await tick();
|
|
|
|
assert.deepEqual(sends, ["x"]);
|
|
});
|
|
|
|
test("flushes immediately and synchronously once the size cap is reached", async () => {
|
|
const sends = [];
|
|
const buffer = createPtyOutputBuffer((data) => sends.push(data), {
|
|
maxBufferSize: 4,
|
|
});
|
|
|
|
buffer.bufferData("ab");
|
|
assert.equal(sends.length, 0); // under cap, still pending
|
|
|
|
buffer.bufferData("cd"); // now "abcd" hits the 4-byte cap
|
|
|
|
// Cap flush happens synchronously, without waiting for the turn.
|
|
assert.deepEqual(sends, ["abcd"]);
|
|
|
|
// The pending turn flush must have been cancelled — no empty/duplicate send.
|
|
await tick();
|
|
assert.deepEqual(sends, ["abcd"]);
|
|
});
|
|
|
|
test("flush() forces a synchronous send and cancels the pending turn", async () => {
|
|
const sends = [];
|
|
const buffer = createPtyOutputBuffer((data) => sends.push(data));
|
|
|
|
buffer.bufferData("hello");
|
|
buffer.flush();
|
|
|
|
assert.deepEqual(sends, ["hello"]);
|
|
|
|
await tick();
|
|
assert.deepEqual(sends, ["hello"]); // not sent twice
|
|
});
|
|
|
|
test("flush() with an empty buffer does not send", async () => {
|
|
const sends = [];
|
|
const buffer = createPtyOutputBuffer((data) => sends.push(data));
|
|
|
|
buffer.flush();
|
|
|
|
assert.equal(sends.length, 0);
|
|
});
|
|
|
|
test("keeps batching after a flush", async () => {
|
|
const sends = [];
|
|
const buffer = createPtyOutputBuffer((data) => sends.push(data));
|
|
|
|
buffer.bufferData("first");
|
|
await tick();
|
|
|
|
buffer.bufferData("second");
|
|
await tick();
|
|
|
|
assert.deepEqual(sends, ["first", "second"]);
|
|
});
|