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>
18 lines
826 B
TypeScript
18 lines
826 B
TypeScript
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); };
|
|
}
|