
read-next-line
Is s a lightweight, efficient utility for reading lines from a ReadableStream in JavaScript.
The primary goal of this module is to enable memory-efficient line-by-line processing of large data streams,
such as logs, files, or real-time data feeds.
Features
-
Line-based processing: Reads lines directly from any ReadableStream.
-
Memory efficiency: Keeps memory usage low by processing one line at a time.
-
Browser compatibility: Works seamlessly with modern web browsers.
-
Node.js compatibility: Works seamlessly with Node.js Web Streams API.
-
Supports the following text encoding:
- UTF-8 (default)
- UTF-8 with the BOM field set
- UTF-16LE with the BOM field is set
- UTF-16BE with the BOM field is set
-
Supports different line endings:
Type | Abbreviation | Escape sequence |
---|
Windows | CR LF | \r\n |
Unix | LF | \n |
Acorn BBC / RISC OS | LF CR | \n\r |
classic Mac OS | CR | \r |
Installation
Install the package via npm:
npm install read-next-line
Compatibility
read-next-line is a hybrid JavaScript module, supporting:
- ECMAScript Module (ESM)
- CommonJS backwards compatibility support
Designed to work with Works seamlessly with either:
Compatible with modern web browsers or Node.js ≥ 18.
Usage
Import and use StreamLineReader
in your project:
In ESM projects or any TypeScript project use:
import {ReadNextLine} from 'read-next-line';
In CommonJS projects use:
const {ReadNextLine} = require('read-next-line');
Using read-next-line to read lines of text of a binary ReadableStream or Node.js Readable streams:
async function processStream(stream) {
const reader = new ReadNextLine(stream);
let line;
while ((line = await reader.readLine()) !== null) {
console.log(line); // Process each line as needed
}
}
You may as well use the LineSplitter
which is a TransformStream<Uint8Array, string>
,
converting a binary stream into stream of lines (strings).
import {LineSplitter} from 'read-next-line';
const lineStream = webStream.pipeThrough(lineSplitter);
const lineReader = lineStream.getReader();
Parsing a Blob/File
To process a file input, wrap the file's stream with ReadNextLine
:
async function proccessTextFile(blob) {
const reader = new ReadNextLine(blob.stream());
try {
let line;
do {
line = await reader.readLine();
console.log(line);
}
while (line !== null);
} finally {
reader.release();
}
}
const file = document.querySelector('#fileInput').files[0];
proccessTextFile(file);
Online example can be found here: https://playcode.io/2226348
API
StreamLineReader
Class
Constructor
new StreamLineReader(stream: ReadableStream<Uint8Array>);
- stream: The
ReadableStream
to process.
Methods
readLine(): Promise<string | null>
- Reads the next line from the stream. Returns
null
if the stream ends.
release(): void
License
This project is licensed under the MIT License. Feel free to use, modify, and distribute as needed.