through2
Last updated
Last updated
A tiny wrapper around Node streams.Transform (Streams2/3) to avoid explicit subclassing noise
Inspired by Dominic Tarr's through in that it's so much easier to make a stream out of a function than it is to set up the prototype chain properly: through(function (chunk) { ... })
.
Note: As 2.x.x this module starts using Streams3 instead of Stream2. To continue using a Streams2 version use npm install through2@0
to fetch the latest version of 0.x.x. More information about Streams2 vs Streams3 and recommendations see the article Why I don't use Node's core 'stream' module.
Or object streams:
Note that through2.obj(fn)
is a convenience wrapper around through2({ objectMode: true }, fn)
.
through2([ options, ] [ transformFunction ] [, flushFunction ])
Consult the stream.Transform documentation for the exact rules of the transformFunction
(i.e. this._transform
) and the optional flushFunction
(i.e. this._flush
).
The options argument is optional and is passed straight through to stream.Transform
. So you can use objectMode:true
if you are processing non-binary streams (or just use through2.obj()
).
The options
argument is first, unlike standard convention, because if I'm passing in an anonymous function then I'd prefer for the options argument to not get lost at the end of the call:
The transformFunction
must have the following signature: function (chunk, encoding, callback) {}
. A minimal implementation should call the callback
function to indicate that the transformation is done, even if that transformation means discarding the chunk.
To queue a new chunk, call this.push(chunk)
—this can be called as many times as required before the callback()
if you have multiple pieces to send on.
Alternatively, you may use callback(err, chunk)
as shorthand for emitting a single chunk or an error.
If you do not provide a transformFunction
then you will get a simple pass-through stream.
The optional flushFunction
is provided as the last argument (2nd or 3rd, depending on whether you've supplied options) is called just prior to the stream ending. Can be used to finish up any processing that may be in progress.
through2.ctor([ options, ] transformFunction[, flushFunction ])
Instead of returning a stream.Transform
instance, through2.ctor()
returns a constructor for a custom Transform. This is useful when you want to use the same transform logic in multiple instances.
through2-map - Array.prototype.map analog for streams.
through2-filter - Array.prototype.filter analog for streams.
through2-reduce - Array.prototype.reduce analog for streams.
through2-spy - Wrapper for simple stream.PassThrough spies.
the mississippi stream utility collection includes through2
as well as many more useful stream modules similar to this one
through2 is Copyright (c) Rod Vagg @rvagg and additional contributors and licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.