Chunk stream to lines: handles CRLF/LF/CR, split across boundaries, with NDJSON parsing wrapper.
Process streaming or chunked text that may split lines across chunk boundaries. Handles all common line endings (CRLF, LF, CR), buffers incomplete lines, and extracts complete ones. Includes NDJSON parser that isolates errors per line.
export class LineSplitter { feed(chunk: string): string[]; flush(): string[] }
export class NDJSONParser { feed(chunk: string): void; flush(): void }npm install @ferrow/stream-line-splitterimport { LineSplitter, NDJSONParser } from 'stream-line-splitter';
const splitter = new LineSplitter();
const lines = splitter.feed('line1\nlin');
console.log(lines); // ['line1']
const moreLines = splitter.feed('e2\nline3');
console.log(moreLines); // ['line2', 'line3']
// NDJSON parsing
const parser = new NDJSONParser();
parser.onLine = (obj) => console.log('Parsed:', obj);
parser.onError = (err, line) => console.log('Bad JSON:', line);
parser.feed('{"x":1}\n{"bad json"\n{"y":2}\n');
parser.flush();- Default maxLineLength is 1 MB; configure via LineSplitterConfig.
- Line splitting happens at first \r\n, then \n, then \r (prefers paired endings).
- NDJSON parser skips empty lines silently.
Part of the ferrow-toolkit collection · Sponsored by Ferrow