Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set writeable highwatermark?

Tags:

node.js

I found in options to set the highwatermark for readable stream: fs.createReadStream(path[, options])

but cannot find highwatermark options for writeable stream

So how to set it when creating a writeable stream?

like image 370
jiajianrong Avatar asked Sep 19 '25 01:09

jiajianrong


1 Answers

Even though it's not documented on fs.createWriteStream, a stream.Writable can take: highWaterMark as an option.

fs.createWriteStream('out', { highWaterMark: 32000 });

console.log(stream._writableState.highWaterMark); // 32000

And to actually test that it is working:

const lowHWStream = fs.createWriteStream('low', { highWaterMark: 1 });
const highHWStream = fs.createWriteStream('high', { highWaterMark: 32000 });

console.log(lowHWStream.write('a')); // false
console.log(highHWStream.write('a')); // true

The return value is true if the internal buffer is less than the highWaterMark configured when the stream was created after admitting chunk. If false is returned, further attempts to write data to the stream should stop until the 'drain' event is emitted.

Checking the source code, you can see that the options you pass to createWriteStream are passed to stream.Writable

like image 124
Marcos Casagrande Avatar answered Sep 21 '25 21:09

Marcos Casagrande