What is first-chunk-stream?
The 'first-chunk-stream' npm package is a Node.js module that allows you to transform the first chunk of a stream. This can be particularly useful for tasks that require modifying or inspecting the beginning of a stream before processing the rest, such as adjusting headers, parsing initial metadata, or prepending data to a stream.
What are first-chunk-stream's main functionalities?
Modify the first chunk of a stream
This code sample demonstrates how to use first-chunk-stream to modify the first 10 bytes of data from a file stream. It prepends the text 'Modified: ' to the beginning of the first chunk.
const firstChunkStream = require('first-chunk-stream');
const fs = require('fs');
const modifyFirstChunk = firstChunkStream({ chunkSize: 10 }, (chunk, enc, callback) => {
const modifiedChunk = Buffer.from(`Modified: ${chunk.toString()}`);
callback(null, modifiedChunk);
});
fs.createReadStream('example.txt')
.pipe(modifyFirstChunk)
.pipe(fs.createWriteStream('modifiedExample.txt'));
Inspect and conditionally modify the first chunk
This example shows how to inspect the first 15 bytes of a stream and modify it only if it contains the word 'Important'. If the condition is met, it prepends 'Alert: ' to the chunk.
const firstChunkStream = require('first-chunk-stream');
const fs = require('fs');
const inspectFirstChunk = firstChunkStream({ chunkSize: 15 }, (chunk, enc, callback) => {
if (chunk.toString().includes('Important')) {
callback(null, Buffer.from(`Alert: ${chunk.toString()}`));
} else {
callback(null, chunk);
}
});
fs.createReadStream('example.txt')
.pipe(inspectFirstChunk)
.pipe(fs.createWriteStream('inspectedExample.txt'));
Other packages similar to first-chunk-stream
through2
Through2 is a tiny wrapper around Node streams.Transform, making it easier to create transform streams. It is similar to first-chunk-stream but offers more general functionality for transforming streams, not limited to just the first chunk.
peek-stream
Peek-stream allows you to peek at the first few bytes of a stream to determine how to parse it, then lets the stream continue unmodified or modified. It is similar to first-chunk-stream but focuses on peeking rather than modifying.
first-chunk-stream
Buffer and transform the n first bytes of a stream
Install
$ npm install first-chunk-stream
Usage
const fs = require('fs');
const getStream = require('get-stream');
const FirstChunkStream = require('first-chunk-stream');
const stream = fs.createReadStream('unicorn.txt')
.pipe(new FirstChunkStream({chunkLength: 7}, (error, chunk, encoding, callback) => {
if (error) {
callback(error);
return;
}
callback(null, chunk.toString(encoding).toUpperCase());
}));
(async () => {
const data = await getStream(stream);
if (data.length < 7) {
throw new Error('Couldn\'t get the minimum required first chunk length');
}
console.log(data);
})();
API
firstChunkStream(options, transform)
Returns a FirstChunkStream
instance.
transform(error, chunk, encoding, callback)
Type: Function
The function that gets the required options.chunkLength
bytes.
Note that the buffer can have a smaller length than the required one. In that case, it will be due to the fact that the complete stream contents has a length less than the options.chunkLength
value. You should check for this yourself if you strictly depend on the length.
options
Type: object
The options object is passed to the Duplex
stream constructor allowing you to customize your stream behavior. In addition you can specify the following option:
chunkLength
Type: number
How many bytes you want to buffer.
License
MIT © Sindre Sorhus