You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

simple-chunk-reader

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

simple-chunk-reader

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

1.0.5
unpublished
latest
Source
npm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

simple-chunk-reader

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

Install

npm install simple-chunk-reader
yarn add simple-chunk-reader

Usage

const ChunkReader = require("simple-chunk-reader");

const filePath = "./somefile.txt";
const chunkSize = 1024;
const reader = new ChunkReader(filePath, chunkSize);

let content = "";
while (!reader.isDone) {
  content += reader.read();
}

API

The module exports the following functions:

  • constructor
  • read
  • readMultiple
  • closej

constructor

  • new ChunkReader(path: string, size: number): ChunkReader
ParameterTypeDescription
pathstringThe path or location of your file
sizenumberChunk/buffer size in bytes, default: 1024 (1 kB)

read

  • read(): string

It returns a next chunk of current file stream.

Syntax:

const ChunkReader = require("simple-chunk-reader");

const filePath = "./file.txt";
const chunkSize = 8;
const reader = new ChunkReader(filePath, chunkSize);

while (!reader.isDone) {
  console.log(reader.read());
}

./file.txt

aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooo

Output:

aaaabbbb
ccccdddd
eeeeffff
gggghhhh
iiiijjjj
kkkkllll
mmmmnnnn
oooo

readMultiple

  • readMultiple(total: number): string[]

It returns a next multiple chunk of current file stream.

Syntax:

const ChunkReader = require("simple-chunk-reader");

const filePath = "./file.txt";
const chunkSize = 8;
const reader = new ChunkReader(filePath, chunkSize);

while (!reader.isDone) {
  console.log(reader.readMultiple(3));
}

./file.txt

aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooo

Output:

['aaaabbbb', 'ccccdddd', 'eeeeffff']
['gggghhhh', 'iiiijjjj', 'kkkkllll']
['mmmmnnnn', 'oooo']

close

  • close(): void

Close the current file descriptor thereby clearing the file stream that is associated with it.

You need to call this method only if you are done before reading the whole content stream. read() and readMutiple() will call this function automatically when reaching the end of the stream.

Syntax:

const ChunkReader = require("simple-chunk-reader");

const filePath = "./file.txt";
const chunkSize = 8;
const reader = new ChunkReader(filePath, chunkSize);

console.log(reader.readMultiple(2));
console.log(reader.readMultiple(4));
console.log(reader.read());

reader.close();

./file.txt

aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooo

Output:

['aaaabbbb', 'ccccdddd']
['eeeeffff', 'gggghhhh', 'iiiijjjj', 'kkkkllll']
mmmmnnnn

Keywords

file

FAQs

Package last updated on 14 Jan 2022

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