0.5.0 • Published 5 months ago

read-next-line v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Node.js CI NPM version npm downloads

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:

    TypeAbbreviationEscape sequence
    WindowsCR LF\r\n
    UnixLF\n
    Acorn BBC / RISC OSLF CR\n\r
    classic Mac OSCR\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.

0.5.0

5 months ago

0.4.1

5 months ago

0.4.0

6 months ago

0.3.2

6 months ago

0.3.1

6 months ago

0.3.0

6 months ago

0.2.0

6 months ago

0.1.0

6 months ago