I am seeing some behavior that I don't understand with busboy and node streams highWaterMark property. I would expect that the size of the chunks are at maximum the highWaterMark value, but the chunk size looks unaffected by the highWaterMark setting.
I've set the fileHwm: 5
option in busboy, and I have my express route set up like so
app.post('/upload', function(req, res, next) {
req.pipe(req.busboy); // Pipe it through busboy
req.busboy.on('file', (fieldname, file, filename) => {
console.log(`Upload of '${filename}' started`);
console.log(file);
file.on('readable', () => {
let chunk;
while (null !== (chunk = file.read())) {
console.log(`Received ${chunk.length} bytes of data.`);
}
});
});
});
when I log file
, it looks ok, and I see the highWaterMark
property has been set nicely
FileStream {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 5,
...
but the size of the chunks that I'm getting out are not 5
like I would expect - instead I'm seeing
Received 65395 bytes of data.
Received 65536 bytes of data.
Received 65536 bytes of data.
Received 65536 bytes of data.
So what gives? I would have expected read
to only return 5 bytes at a time. Not that 65kb is a bad size, it's fine. I just wish I understood what was happening, and if it was possible to restrict the buffer size.