What is minipass-sized?
The minipass-sized npm package is a stream library that extends Minipass with support for ensuring a specific number of bytes are written to the stream. It is useful for scenarios where you need to validate the size of the stream data, such as when receiving files or data packets that must meet specific size requirements.
What are minipass-sized's main functionalities?
Size validation
This feature allows the user to set a specific size for the stream and emits an event or an error if the stream does not match the specified size. It is particularly useful for validating the integrity of data in scenarios where the exact size is critical.
const MinipassSized = require('minipass-sized');
const stream = new MinipassSized({ size: 1024 });
stream.on('size', () => console.log('Size is exactly 1024 bytes'));
stream.on('error', (err) => console.error('Error:', err.message));
Other packages similar to minipass-sized
concat-stream
Concat-stream is a writable stream that concatenates data and calls a callback with the result. It is similar to minipass-sized in that it processes stream data but does not provide built-in size validation.
bl
Buffer list (bl) is a storage object for collections of Node.js Buffers, similar to minipass-sized in handling stream data. Unlike minipass-sized, bl focuses on buffer management and aggregation rather than enforcing size constraints.
minipass-sized
A Minipass stream that raises an error if you get a different number of
bytes than expected.
USAGE
Use just like any old minipass stream, but
provide a size
option to the constructor.
The size
option must be a positive integer, smaller than
Number.MAX_SAFE_INTEGER
.
const MinipassSized = require('minipass-sized')
const expectedSize = +headers['content-length']
const stream = new MinipassSized({ size: expectedSize })
stream.on('error', er => {
})
response.pipe(stream)
Caveats: this does not work with objectMode
streams, and will throw a
TypeError
from the constructor if the size argument is missing or
invalid.