![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
chunk-reader2
Advanced tools
Asynchronous, buffered, chunk-by-chunk file reader with customizable buffer size on Node.js.
Asynchronous, buffered, chunk-by-chunk file reader with customizable buffer size.
NPM
npm install chunk-reader2
yarn
yarn add chunk-reader2
ES6
import { ChunkReader } from 'chunk-reader2';
CommonJS
const { ChunkReader } = require('chunk-reader2');
const reader = new ChunkReader({
filePath: './file.txt',
bufferSize: 1024,
});
while (!reader.isClosed) {
const chunk = await reader.read();
console.log(chunk);
}
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 |
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).
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 |
This library is well tested. You can test the code as follows:
NPM
npm test
yarn
yarn test
If you have anything to contribute, or functionality that you lack - you are more than welcome to participate in this!
Feel free to use this library under the conditions of the MIT license.
FAQs
Asynchronous, buffered, chunk-by-chunk file reader with customizable buffer size on Node.js.
We found that chunk-reader2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.