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.
- 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:
- Windows (
CR LF
) - Unix (
LF
) - Acorn BBC / RISC OS (
LF CR
).
- Windows (
Install the package via npm:
npm install read-next-line
read-next-line is a hybrid JavaScript module, supporting:
- ECMAScript Module (ESM)
- CommonJS backwards compatibility support
Designed to work with Works seamlessly with either:
- Node.js Readable streams
- Node.js Web Streams API
- Streams-API in the browser
Compatible with modern web browsers or Node.js ≥ 18.
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
}
}
To process a file input, wrap the file's stream with ReadNextLine
:
const file = document.querySelector('input[type="file"]').files[0];
const reader = new ReadNextLine(file.stream());
let line;
while ((line = await reader.readLine()) !== null) {
console.log(line);
}
new StreamLineReader(stream: ReadableStream<Uint8Array>);
-
stream: The
ReadableStream
to process.
-
readLine(): Promise<string | null>
- Reads the next line from the stream. Returns
null
if the stream ends.
- Reads the next line from the stream. Returns
-
release(): void
- Release the internal Reader.
This project is licensed under the MIT License. Feel free to use, modify, and distribute as needed.