New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

chunk-reader2

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chunk-reader2

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

  • 1.1.4
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

chunk-reader2

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

Install

NPM

npm install chunk-reader2

yarn

yarn add chunk-reader2

Usage

Import

ES6

import { ChunkReader } from 'chunk-reader2';

CommonJS

const { ChunkReader } = require('chunk-reader2');
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:

NameTypeDefaultDescription
filePathstringnoneThe path or location of your file (required)
bufferSizenumber1024Chunk/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
removeInvisibleUnicodebooleanfalseRemove all (or perhaps just "common") non-printable Unicode characters except line breaks. Using regex: /[\x00-\x09\x0B-\x0C\x0E-\x1F\x7F-\x9F]/g

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).

Instance Property

The property of ChunkReader instance you can access are:

NameTypeDescription
bytesLengthnumberSize of the file in bytes. Value assigned on open() operation
bytesReadnumberSize of the bytes read in the file by read() operation
readCountnumberCount of read() operation called
isOpenedbooleanIndicates whether the reader has opened the file or open() has been called
isClosedbooleanIndicates whether the reader has closed the file or close() has been called

Testing

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

NPM

npm test

yarn

yarn test
  • line-reader2 - 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.

Keywords

FAQs

Package last updated on 04 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc