Search code examples
node.jsstream

How to handle end signal in a node.js Writable stream?


Using the stream.Writable base class of node.js 0.10, how can I handle the case of a consumer calling .end() and thus signaling no more data will be available?

In the documentation the only method told to be implemented in Writable subclasses is _write, but I can't find any clean way to notice when the input has been exhausted.


Solution

  • Embarrassingly, as of 0.10, node's stream.Writable does not support this in a standard, clean way. This issue is already being discussed here: https://github.com/joyent/node/issues/7348

    In that page several workarounds are proposed. TomFrost's flushwritable module looks like an acceptable solution for the time being.

    That module provides a FlushWritable class which is used exactly like stream.Writable but supporting a new _flush method which is invoked after the consumer calls .end() but before the finish event is emitted.