1/**
2 * Buffers arbitrary text chunks and emits complete lines. Call `flush` when
3 * the source closes to emit a trailing partial line.
4 */
5export const makeLineBuffer = (onLine: (line: string) => void) => {
6 let buffer = "";
7 return {
8 push(chunk: string) {
9 buffer += chunk;
10 const lines = buffer.split(/\r\n|\n|\r/);
11 buffer = lines.pop() ?? "";
12 for (const line of lines) onLine(line);
13 },
14 flush() {
15 if (buffer === "") return;
16 onLine(buffer);
17 buffer = "";
18 },
19 };
20};