diff --git a/src/process/Utf8NdjsonLineBuffer.test.ts b/src/process/Utf8NdjsonLineBuffer.test.ts index 726cc26066..75b1632294 100644 --- a/src/process/Utf8NdjsonLineBuffer.test.ts +++ b/src/process/Utf8NdjsonLineBuffer.test.ts @@ -130,6 +130,20 @@ describe("UTF-8 NDJSON line buffer", () => { }); }); + it("releases a drained large frame when only a partial next frame remains", () => { + const bytes = 1024 * 1024; + const lines = new Utf8NdjsonLineBuffer(() => bytes); + expect(texts(lines.push(Buffer.alloc(bytes, 0x78)))).toEqual([]); + + const [first] = texts(lines.push(Buffer.from("\npartial"))); + expect(first?.length).toBe(bytes); + expect(lines.retainedTail()).toEqual({ + pendingBytes: 7, + backingBytes: 7, + }); + expect(texts(lines.push(Buffer.from(" line\n")))).toEqual(["partial line"]); + }); + it("discards an unterminated tail once it passes the ceiling", () => { const lines = new Utf8NdjsonLineBuffer(() => 16); expect([...lines.push(Buffer.alloc(16, 0x78))]).toEqual([]); diff --git a/src/process/Utf8NdjsonLineBuffer.ts b/src/process/Utf8NdjsonLineBuffer.ts index f57a024121..8b27ecaabf 100644 --- a/src/process/Utf8NdjsonLineBuffer.ts +++ b/src/process/Utf8NdjsonLineBuffer.ts @@ -101,7 +101,20 @@ export class Utf8NdjsonLineBuffer { if (!line.ok) return; } const refused = this.#refuseTail(maximumBytes); - if (refused !== undefined) yield err(refused); + if (refused !== undefined) { + yield err(refused); + return; + } + // A short tail is a view into the drained frames. Copy it once after + // draining, when doing so releases more bytes than it copies. + if ( + this.#buffer.buffer.byteLength > + Math.max(Buffer.poolSize, 2 * this.#buffer.length) + ) { + const tail = Buffer.allocUnsafeSlow(this.#buffer.length); + this.#buffer.copy(tail); + this.#buffer = tail; + } } /** Validate the unfinished tail at EOF without treating it as a complete response. */