Search code examples
node.jsnode-streams

Node.js Streams: When will _writev Be Invoked?


The Node.js documentation makes the following comments about a Writable stream's _writev method.

The writable._writev() method may be implemented in addition or alternatively to writable._write() in stream implementations that are capable of processing multiple chunks of data at once. If implemented and if there is buffered data from previous writes, _writev() will be called instead of _write().

Emphasis mine. In what scenarios can a Node.js writable stream have buffered data from previous writes?

Is the _writev method only called after uncorking a corked stream that's had data written to it? Or are there other scenarios where a stream can have buffered date from previous writes? Bonus point if you can point to the place in the Node.js source code where it makes a decisions w/r/t to calling _write or _writev.


Solution

  • _writev() will be called whenever there is more than one piece of data buffered from the stream and the function has been defined. Using cork() could cause more data to be buffered, but so could slow processing.

    The code that guards _writev is in lib/internal/streams/writable.js. There is a buffer decision and then the guard for the write.