089db21333
Three failure modes shared the same root cause on the StdinBuffer.process hot path — unbounded synchronous work in a single call. - `StdinBuffer.extractCompleteSequences` grew each escape candidate one code unit at a time and re-tested every prefix, so a malformed CSI/OSC/DCS/APC streamed across chunks re-inspected the accumulated buffer on every `process()` call (issue A). Replaced the grow-and-recheck loop with a single linear scan bounded by a per-type cap (CSI 4 KiB, OSC/DCS/APC 16 MiB) and a resume-search offset carried on `StdinBuffer` so a legit chunked OSC 5522 image paste stays O(total) instead of O(total²). - `BracketedPasteHandler` had no cap or timeout: a caller that bypassed StdinBuffer (fed the start marker with no end marker) grew `#buffer` forever (issue B, defense in depth). Added an optional `byteLimit` option (default 64 MiB) that aborts paste mode and delivers accumulated bytes as `pasteContent`, mirroring `StdinBuffer#abortPaste`. - The `ProcessTerminal` data handler ran six always-executed escape-probe regex tests per `data` event; a 100 KB non-bracketed paste (terminal without DEC 2004 support) turned into ~600K regex executions (issue C). Added a fast path that forwards non-ESC sequences straight to the input handler when no reassembly buffer is holding state. Verified with new stdin-buffer + bracketed-paste tests plus empirical repros: a streamed 1 MiB malformed CSI drops from ~4300 ms to ~50 ms of synchronous work, and a 1 MiB chunked OSC 5522 payload drops from ~6800 ms to ~25 ms. Fixes #4073
88 lines
3.6 KiB
TypeScript
88 lines
3.6 KiB
TypeScript
/**
|
|
* Tests for BracketedPasteHandler
|
|
*
|
|
* Covers the byte-cap defense-in-depth (issue #4073 case B).
|
|
* The normal ProcessTerminal path re-wraps StdinBuffer's bounded paste with
|
|
* both markers so BracketedPasteHandler always receives them together; the
|
|
* byte cap only fires on alternate callers that bypass StdinBuffer.
|
|
*/
|
|
import { describe, expect, it } from "bun:test";
|
|
import { BracketedPasteHandler } from "@oh-my-pi/pi-tui/bracketed-paste";
|
|
|
|
const PASTE_START = "\x1b[200~";
|
|
const PASTE_END = "\x1b[201~";
|
|
|
|
describe("BracketedPasteHandler", () => {
|
|
describe("Byte cap (issue #4073 case B)", () => {
|
|
it("aborts paste mode and delivers accumulated bytes when the cap is exceeded", () => {
|
|
// A caller that bypasses StdinBuffer (feeds PASTE_START without an
|
|
// end marker) must not accumulate memory forever. The cap fires
|
|
// on the very chunk that would push the buffer past the limit,
|
|
// delivering the buffered bytes so they are neither lost nor held.
|
|
const handler = new BracketedPasteHandler({ byteLimit: 16 });
|
|
handler.process(PASTE_START);
|
|
const first = handler.process("0123456789");
|
|
expect(first).toEqual({ handled: true, remaining: "" });
|
|
|
|
const overflow = handler.process("abcdefgh");
|
|
expect(overflow.handled).toBe(true);
|
|
// @ts-expect-error - narrowed at runtime by the assertion above
|
|
expect(overflow.pasteContent).toBe("0123456789abcdefgh");
|
|
// @ts-expect-error - narrowed at runtime by the assertion above
|
|
expect(overflow.remaining).toBe("");
|
|
});
|
|
|
|
it("resets state after a cap-abort so subsequent input is not eaten as paste", () => {
|
|
const handler = new BracketedPasteHandler({ byteLimit: 8 });
|
|
handler.process(PASTE_START);
|
|
// One over the cap → cap-flush.
|
|
const abort = handler.process("0123456789");
|
|
expect(abort.handled).toBe(true);
|
|
// A follow-up plain byte after recovery must go through as
|
|
// unhandled so callers process it normally.
|
|
const next = handler.process("x");
|
|
expect(next).toEqual({ handled: false });
|
|
});
|
|
|
|
it("does not truncate a legitimate multi-chunk paste under the cap", () => {
|
|
const handler = new BracketedPasteHandler({ byteLimit: 1024 });
|
|
handler.process(PASTE_START);
|
|
handler.process("hello ");
|
|
handler.process("world");
|
|
const result = handler.process(PASTE_END);
|
|
expect(result.handled).toBe(true);
|
|
// @ts-expect-error - handled=true carries pasteContent
|
|
expect(result.pasteContent).toBe("hello world");
|
|
});
|
|
|
|
it("defaults to a generous cap that fits a small multi-chunk paste", () => {
|
|
// Default byte limit is 64 MiB — a normal-sized paste completes
|
|
// via the end marker, not via the cap.
|
|
const handler = new BracketedPasteHandler();
|
|
handler.process(PASTE_START);
|
|
handler.process("x".repeat(100_000));
|
|
const finish = handler.process(PASTE_END);
|
|
expect(finish.handled).toBe(true);
|
|
// @ts-expect-error - handled=true carries pasteContent
|
|
expect(finish.pasteContent.length).toBe(100_000);
|
|
});
|
|
});
|
|
|
|
describe("Baseline flow", () => {
|
|
it("returns handled=false when no paste marker has been seen", () => {
|
|
const handler = new BracketedPasteHandler();
|
|
expect(handler.process("plain text")).toEqual({ handled: false });
|
|
});
|
|
|
|
it("assembles a paste delivered as a single chunk with both markers", () => {
|
|
const handler = new BracketedPasteHandler();
|
|
const result = handler.process(`${PASTE_START}payload${PASTE_END}tail`);
|
|
expect(result.handled).toBe(true);
|
|
// @ts-expect-error - handled=true carries pasteContent + remaining
|
|
expect(result.pasteContent).toBe("payload");
|
|
// @ts-expect-error - remaining carries post-marker input
|
|
expect(result.remaining).toBe("tail");
|
|
});
|
|
});
|
|
});
|