chrisdew chrisdew - 4 months ago 19
Node.js Question

NodeJS streams - a leaky pipe?

I'm doing a lot of work with NodeJS streams at the moment.

One thing I find myself needing is a 'leaky pipe'.

Like

stream.PassThrough
, but which just drops data if (and only if) it has no where to send it.

Does such a thing already exist?

Is there a way of finding out (within a
stream.Transform
) how many downstream pipes are connected?

Answer

I eventually came up with a solution:

LeakyTransform.prototype._transform = function(chunk, encoding, done) {
  if (this._readableState.pipesCount > 0) {
    this.push(chunk);
  }
  done();
}
Comments