fadedbee
fadedbee

Reputation: 44715

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?

Upvotes: 2

Views: 714

Answers (2)

vkurchatkin
vkurchatkin

Reputation: 13570

PassThrough implements _transfrom as following:

PassThrough.prototype._transform = function(chunk, encoding, cb) {
  cb(null, chunk);
};

I think what you need can be implemented like this:

Leak.prototype._transform = function(chunk, encoding, cb) {
};

i.e. no-op

Upvotes: 0

fadedbee
fadedbee

Reputation: 44715

I eventually came up with a solution:

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

Upvotes: 4

Related Questions