GitXplorerGitXplorer
B

read-next-line

public
0 stars
0 forks
2 issues

Commits

List of commits on branch master.
Unverified
4e3c790e50d8fa15f67070200108e96f397e3323

0.4.0

BBorewit committed 2 days ago
Verified
34440de925c8f47152b67c6a0c7936ebaf88c6f2

Merge pull request #8 from Borewit/add-unittests-for-utf-16

BBorewit committed 2 days ago
Unverified
c70d3c565a19424cc5af8bf1047b0bd294e61c71

Add unit tests for UTF-16

BBorewit committed 2 days ago
Verified
9a22b0d5917c3d126b8573e0ec4bc86fc1ecc1d7

Merge pull request #7 from Borewit/add-support-for-nodejs-readable

BBorewit committed 2 days ago
Unverified
8b5d832a8cd4eea7d512d5c647a6855c306090c7

Add support for Node.js `Readable` stream

BBorewit committed 2 days ago
Verified
cc407447c60d0018c321c21f53e268a78b52a647

Merge pull request #6 from Borewit/improve-cjs-wrapping

BBorewit committed 2 days ago

README

The README file for this repository.

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:
    • Windows (CR LF)
    • Unix (LF)
    • Acorn BBC / RISC OS (LF CR).

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
	}
}

Parsing a Blob/File

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);
}

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
    • Release the internal Reader.

License

This project is licensed under the MIT License. Feel free to use, modify, and distribute as needed.