npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@supercat1337/line-reader

v1.0.0

Published

Line reader for Node.js

Readme

Line Reader

A versatile and efficient Node.js library for reading large files line by line with advanced features like caching, random access, and batch processing.

Features

  • Efficient streaming: Reads files line by line without loading entire file into memory
  • Random access: Jump to specific line numbers with EnhancedLineReader
  • Smart caching: Built-in LRU cache for frequently accessed lines
  • Batch processing: Process lines in batches for better performance
  • Flexible navigation: Skip lines, read multiple lines, or restart from beginning
  • Resource management: Automatic cleanup with async disposer pattern
  • TypeScript ready: Full type definitions included

Installation

npm install @supercat1337/line-reader

Basic Usage

Simple Line-by-Line Reading

import { LineReader } from '@supercat1337/line-reader';

const reader = new LineReader('large-file.txt');

try {
  await reader.open();
  
  let line;
  while ((line = await reader.readLine()) !== null) {
    console.log(line);
  }
} finally {
  await reader.close();
}

Using Async Disposer (Node.js 16+)

import { LineReader } from '@supercat1337/line-reader';

// Automatic resource management
await using reader = new LineReader('large-file.txt');
await reader.open();

while (!reader.isEOF()) {
  const line = await reader.readLine();
  if (line !== null) {
    processLine(line);
  }
}

Advanced Usage

EnhancedLineReader with Caching

import { EnhancedLineReader } from '@supercat1337/line-reader';

const reader = new EnhancedLineReader('large-file.txt', {
  cacheSize: 1000,
  encoding: 'utf-8'
});

await reader.open();

// Read specific line efficiently (cached for subsequent reads)
const line100 = await reader.readLineAt(99); // Line numbers are 0-based
console.log(`Line 100: ${line100}`);

// Continue reading sequentially
const nextLine = await reader.readLine();

await reader.close();

Batch Processing

import { LineReader, withBatchProcessor } from '@supercat1337/line-reader';

const reader = new LineReader('large-file.txt');
await reader.open();

const batchProcessor = withBatchProcessor(reader, 100); // Process 100 lines at a time

for await (const batch of batchProcessor.readBatches()) {
  // Process 100 lines as a batch
  await processBatch(batch);
  console.log(`Processed ${batch.length} lines`);
}

await reader.close();

Skipping Lines and Multiple Reads

const reader = new LineReader('data.csv');
await reader.open();

// Skip headers
await reader.skipLines(1);

// Read 10 data rows
const dataRows = await reader.readLines(10);

// Skip to line 1000
await reader.skipLines(989); // 1 header + 10 rows already read = 11 total

// Get current position in bytes
const position = await reader.getPosition();

await reader.close();

API Reference

LineReader

Constructor

new LineReader(filePath: string, options?: {
  encoding?: BufferEncoding; // Default: 'utf-8'
  highWaterMark?: number;    // Default: 1MB
})

Methods

  • open(): Promise<this> - Opens the file for reading
  • readLine(): Promise<string | null> - Reads next line
  • skipLines(count: number): Promise<number> - Skips specified number of lines
  • readLines(count: number): Promise<string[]> - Reads multiple lines
  • getPosition(): Promise<number> - Returns current byte position
  • close(): Promise<void> - Closes the file
  • restart(): Promise<void> - Restarts from beginning
  • isEOF(): boolean - Checks if end of file reached
  • readAll(): Promise<string[]> - Reads all remaining lines
  • [Symbol.asyncDispose](): Promise<void> - Automatic cleanup

EnhancedLineReader

Extends LineReader with caching capabilities.

Constructor

new EnhancedLineReader(filePath: string, options?: {
  cacheSize?: number;       // Default: 1000
  encoding?: BufferEncoding; // Default: 'utf-8'
})

Additional Methods

  • readLineAt(lineNumber: number): Promise<string | null> - Reads line at specific position
  • getCurrentLineNumber(): number - Returns current line number
  • manageCache(): void - Manages cache size (called automatically)

withBatchProcessor

Function

function withBatchProcessor(
  reader: LineReader,
  batchSize?: number // Default: 100
): { readBatches: () => AsyncGenerator<string[], void, unknown> }

Performance Tips

  1. Use appropriate cache size: For files with frequent random access, increase cacheSize
  2. Batch processing: Use withBatchProcessor for bulk operations
  3. Stream when possible: For sequential reads, use simple LineReader
  4. Manage resources: Always close readers or use async disposer
  5. Adjust buffer size: For very large files, increase highWaterMark

Error Handling

const reader = new LineReader('file.txt');

try {
  await reader.open();
  // ... operations
} catch (error) {
  console.error('Error reading file:', error);
} finally {
  try {
    await reader.close();
  } catch (closeError) {
    console.error('Error closing reader:', closeError);
  }
}

License

MIT