Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stream-line-splitter

CI

Chunk stream to lines: handles CRLF/LF/CR, split across boundaries, with NDJSON parsing wrapper.

What & Why

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.

API

export class LineSplitter { feed(chunk: string): string[]; flush(): string[] }
export class NDJSONParser { feed(chunk: string): void; flush(): void }

Install

npm install @ferrow/stream-line-splitter

Quick Start

import { 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();

Limits

  • 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

About

Chunk stream to lines: handles CRLF/LF/CR, split across boundaries, with NDJSON parsing wrapper

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages