What is read-chunk?
The 'read-chunk' npm package allows you to read a specific chunk of a file. This can be useful for tasks such as file type detection, partial file processing, and more.
What are read-chunk's main functionalities?
Read a specific chunk of a file
This feature allows you to read a specific chunk of a file. In this example, it reads the first 10 bytes from 'example.txt' and logs it to the console.
const readChunk = require('read-chunk');
const buffer = readChunk.sync('example.txt', 0, 10);
console.log(buffer.toString());
Asynchronous chunk reading
This feature allows you to read a specific chunk of a file asynchronously. In this example, it reads the first 10 bytes from 'example.txt' and logs it to the console.
const readChunk = require('read-chunk');
readChunk('example.txt', 0, 10).then(buffer => {
console.log(buffer.toString());
}).catch(err => {
console.error(err);
});
Other packages similar to read-chunk
fs-extra
The 'fs-extra' package extends the native Node.js 'fs' module with additional methods, including methods for reading and writing files. While it does not specifically focus on reading chunks, it provides a more comprehensive set of file system utilities.
node-buffer
The 'node-buffer' package provides utilities for working with binary data directly in Node.js. It allows for more granular control over reading and writing data, including reading specific chunks of a file.
file-type
The 'file-type' package is used to detect the file type of a buffer or stream. It often uses 'read-chunk' internally to read the first few bytes of a file for type detection. While its primary focus is on file type detection, it shares the functionality of reading specific chunks of a file.
read-chunk
Read a chunk from a file
Because the built-in way requires way too much boilerplate.
Install
npm install read-chunk
Usage
import {readChunk} from 'read-chunk';
await readChunk('foo.txt', {length: 3, startPosition: 1});
API
readChunk(filePath, {length, startPosition})
Returns a Promise<Uint8Array>
with the read chunk.
readChunkSync(filePath, {length, startPosition})
Returns a Uint8Array
with the read chunk.
filePath
Type: string
length
Type: number
The number of bytes to read.
startPosition
Type: number
The position to start reading from.