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 🙏

© 2024 – Pkg Stats / Ryan Hefner

chunkreader2

v1.1.7

Published

Asynchronous, buffered, chunk-by-chunk file reader with customizable buffer size on Node.js.

Downloads

13

Readme

chunkreader2

Asynchronous, buffered, chunk-by-chunk file reader with customizable buffer size.

Install

NPM

npm install chunkreader2

yarn

yarn add chunkreader2

Usage

Import

ES6

import { ChunkReader } from 'chunkreader2';

CommonJS

const { ChunkReader } = require('chunkreader2');

Example

const reader = new ChunkReader({
  filePath: './file.txt',
  bufferSize: 1024,
});

while (!reader.isClosed) {
  const chunk = await reader.read();
  console.log(chunk);
}

API

new ChunkReader(options: ChunkReaderOptions): ChunkReader

The options you can pass are:

| Name | Type | Default | Description | | ---------------------- | ------------------------------------------------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | filePath | string | none | The path or location of your file (required) | | bufferSize | number | 1024 | Chunk/buffer size in bytes | | bufferEncoding | 'ascii' \| 'utf8' \| 'utf-8' \| 'utf16le' \| 'ucs2' \| 'ucs-2' \| 'base64' \| 'latin1' \| 'binary' \| 'hex' | 'utf8' | Character encoding to use on read() operation | | removeInvisibleUnicode | boolean | false | Remove all (or perhaps just "common") non-printable Unicode characters except line breaks. Using regex: /[\x00-\x09\x0B-\x0C\x0E-\x1F\x7F-\x9F]/g |

Instance Property

The property of ChunkReader instance you can access are:

| Name | Type | Description | | ----------- | --------- | ----------------------------------------------------------------------------- | | bytesLength | number | Size of the file in bytes. Value assigned on open() operation | | bytesRead | number | Size of the bytes read in the file by read() operation | | readCount | number | Count of read() operation called | | isOpened | boolean | Indicates whether the reader has opened the file or open() has been called | | isClosed | boolean | Indicates whether the reader has closed the file or close() has been called |

Instance Methods

read(): Promise<string>

Asynchronously read next chunk of current file stream.

Example:

const reader = new ChunkReader({
  filePath: './file.txt',
  bufferSize: 8,
});

while (!reader.isClosed) {
  const chunk = await reader.read();
  console.log(chunk);
}

./file.txt

aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooo

Output:

aaaabbbb
ccccdddd
eeeeffff
gggghhhh
iiiijjjj
kkkkllll
mmmmnnnn
oooo

NOTE: This method can be called concurrently with safe because it used async-mutex module to handle Mutual Exclusion.

reset(): void

Reset the reader, so it will repeat the reading from the beginning.

Example:

const reader = new ChunkReader({
  filePath: './file.txt',
  bufferSize: 1,
});

for (let i = 0; i < 2; i++) {
  const chunk = await reader.read();
  console.log(chunk);
}

console.log('reset');
reader.reset();

while (!reader.isClosed) {
  const chunk = await reader.read();
  console.log(chunk);
}

./file.txt

12345

Output:

1
2
reset
1
2
3
4
5

open(): void

Manually open the file descriptor and get bytesLength. This method will be called automatically on the first read() operation. Throws an error when file doesn't exist.

close(): void

Manually close the file descriptor. This method will be called automatically on the last read() operation (last file stream).

Testing

This library is well tested. You can test the code as follows:

NPM

npm test

yarn

yarn test

Related

  • linereader2 - Asynchronous, buffered, line-by-line file reader with customizable buffer size and separator.

Contribute

If you have anything to contribute, or functionality that you lack - you are more than welcome to participate in this!

License

Feel free to use this library under the conditions of the MIT license.