feat(backend): LF-only JSONL line splitter

Implement attachJsonlReader to split streams on \n only, handling
trailing \r, and reassembling lines split across chunk boundaries.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-27 20:49:00 +02:00
co-authored by Claude Opus 4.8
parent b09de85c6f
commit de56694433
2 changed files with 29 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
import { StringDecoder } from "node:string_decoder";
import type { Readable } from "node:stream";
export function attachJsonlReader(stream: Readable, onLine: (line: string) => void): () => void {
const decoder = new StringDecoder("utf8");
let buffer = "";
const emit = (line: string) => onLine(line.endsWith("\r") ? line.slice(0, -1) : line);
const onData = (chunk: Buffer | string) => {
buffer += typeof chunk === "string" ? chunk : decoder.write(chunk);
for (let nl; (nl = buffer.indexOf("\n")) !== -1; ) {
emit(buffer.slice(0, nl)); buffer = buffer.slice(nl + 1);
}
};
const onEnd = () => { buffer += decoder.end(); if (buffer) { emit(buffer); buffer = ""; } };
stream.on("data", onData); stream.on("end", onEnd);
return () => { stream.off("data", onData); stream.off("end", onEnd); };
}