A basic wrapper function around through2@0.4.x
This is the base function used by super-stream
as a standalone module. Also it is a drop in replacement for through2
Why shouldn't you use through2
instead of this module?
You wouldn't if all you need is a basic helper for creating stream.Transform
.
But if you need some functional style transforms and other stream utilities and reduce your dependencies at the same time, this is your basic through stream you are looking for. For all the API, go here
See also.super-stream
super-stream.each
super-stream.map
super-stream.reduce
super-stream.filter
super-stream.junction
super-stream.pipeline
Methods
-
<static> buf(transform, flush) → {Transform}
-
It is a conveniece method for
through({objectMode: false}, transformFn, flushFn);
If called without arguments, returns a passthroughTransform
Parameters:
Name Type Argument Description transform
function <optional>
_transform
function as described hereflush
function <optional>
_flush
function as described here (same link as above)Returns:
A instance of
Transform
stream fromreadable-stream@1.0.x
- Type
- Transform
Example
// see the factory method. var thr = through.factory({objectMode: true}); var myData = new Buffer('my data'); var streamBuf = thr.buf(function(chunk, enc, done){ expect(chunk).to.be.equal(myData); expect(chunk).to.not.be.equal('my data'); done(); }); streamBuf.write(myData);
-
<static> ctor(options, transform, flush) → {Transform}
-
Note: This is the same
ctor
method fromthrough2
If called without arguments, returns a passthroughTransform
Parameters:
Name Type Argument Description options
Object <optional>
Same options as seen here
transform
function <optional>
_transform
function as described hereflush
function <optional>
_flush
function as described here (same link as above)Returns:
A pre-configured
Transform
contructor fromreadable-stream@1.0.x
- Type
- Transform
Example
var Transform = require('readable-stream').Transform; var Ctor = through.ctor({objectMode: true}, transformFn, flushFn); var streamA = new Ctor(); // no need for the new operator var streamB = Ctor(); //overriding options var streamC = Ctor({objectMode: false}); expect(streamA).to.be.an.instanceof(Transform); expect(streamB).to.be.an.instanceof(Transform); expect(streamC).to.be.an.instanceof(Transform);
-
<static> factory(options) → {through}
-
A factory method for creating a custom
through
instance.Parameters:
Name Type Argument Description options
Object <optional>
Object passed to
stream.Transfrom
constructor as described here and hereProperties
Name Type Argument Default Description highWaterMark
Number <optional>
16kb The maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource.
encoding
String <optional>
<nullable>
null If specified, then buffers will be decoded to strings using the specified encoding.
objectMode
Boolean <optional>
false Whether this stream should behave as a stream of objects. Meaning that stream.read(n) returns a single value instead of a Buffer of size n.
allowHalfOpen
Boolean <optional>
true If set to false, then the stream will automatically end the readable side when the writable side ends and vice versa.
Returns:
A through function with
options
pre-configured as default- Type
- through
Example
var thrObj = through.factory({objectMode: true}); var streamObj = thrObj(function(string, enc, done){ expect(string).to.be.equal('my data'); done(); }); streamObj.write('my data'); var myData = new Buffer('my data'); var thrBuf = through.factory({objectMode: false, highWaterMark: 1000*Math.pow(2,6)}); var streamBuf = thrBuf(function(chunk, enc, done){ expect(chunk).to.be.equal(myData); expect(chunk).to.not.be.equal('my data'); done(); }); streamBuf.write(myData);
-
<static> obj(transform, flush) → {Transform}
-
It is a conveniece method for
through({objectMode: true}, transformFn, flushFn);
If called without arguments, returns a passthroughTransform
Note: This is the same
obj
method fromthrough2
Parameters:
Name Type Argument Description transform
function <optional>
_transform
function as described hereflush
function <optional>
_flush
function as described here (same link as above)Returns:
A instance of
Transform
stream fromreadable-stream@1.0.x
- Type
- Transform
Example
var stream = through.obj(function(string, enc, done){ expect(string).to.be.deep.equal({data: 'myData'}); done(); }); stream.write({data: 'myData'});
-
through(options, transform, flush) → {Transform}
-
Parameters:
Name Type Argument Description options
Object <optional>
Same options as seen here
transform
function <optional>
_transform
function as described hereflush
function <optional>
_flush
function as described here (same link as above)Returns:
A instance of
Transform
stream fromreadable-stream@1.0.x
- Type
- Transform
Examples
var expect = require('chai').expect; var through = require("super-stream.through") var streamA = through.obj(function(counter, enc, done){ counter += 1; done(null, counter); }); var streamB = through({objectMode: true}, function(counter, enc, done){ counter += 1; done(null, counter); }); thr = through.factory({objectMode: true}); streamA.pipe(streamB).pipe(thr(function(counter, enc, done){ expect(counter).to.be.equal(2); })); streamA.write(0);
var streamA = through(function(chunk, enc, done){ data = chunk.toString(); done(null, new Buffer(data +'-'+ data)); }); thrObj = through.factory({objectMode: true}); var streamB = thrObj.buf(function(chunk, enc, done){ expect(chunk.toString()).to.be.equal('myData-myData'); done(); }); streamA.pipe(streamB); streamA.write(new Buffer('myData'));