2.6 KiB
2.6 KiB
duplexer3

Like duplexer2 but using Streams3 without readable-stream dependency
var stream = require("stream");
var duplexer3 = require("duplexer3");
var writable = new stream.Writable({objectMode: true}),
= new stream.Readable({objectMode: true});
readable
._write = function _write(input, encoding, done) {
writableif (readable.push(input)) {
return done();
else {
} .once("drain", done);
readable
};
}
._read = function _read(n) {
readable// no-op
;
}
// simulate the readable thing closing after a bit
.once("finish", function() {
writablesetTimeout(function() {
.push(null);
readable, 500);
};
})
var duplex = duplexer3(writable, readable);
.on("data", function(e) {
duplexconsole.log("got data", JSON.stringify(e));
;
})
.on("finish", function() {
duplexconsole.log("got finish event");
;
})
.on("end", function() {
duplexconsole.log("got end event");
;
})
.write("oh, hi there", function() {
duplexconsole.log("finished writing");
;
})
.end(function() {
duplexconsole.log("finished ending");
; })
got data "oh, hi there"
finished writing
got finish event
finished ending
got end event
Overview
This is a reimplementation of duplexer using the Streams3 API which is standard in Node as of v4. Everything largely works the same.
Installation
$ npm i duplexer3
API
duplexer3
Creates a new DuplexWrapper
object, which is the actual
class that implements most of the fun stuff. All that fun stuff is
hidden. DON’T LOOK.
duplexer3([options], writable, readable)
const duplex = duplexer3(new stream.Writable(), new stream.Readable());
Arguments
- options - an object specifying the regular
stream.Duplex
options, as well as the properties described below. - writable - a writable stream
- readable - a readable stream
Options
- bubbleErrors - a boolean value that specifies
whether to bubble errors from the underlying readable/writable streams.
Default is
true
.
License
3-clause BSD. A copy is included with the source.
Contact
- GitHub (deoxxa)
- Twitter (@deoxxa)
- Email (deoxxa@fknsrs.biz)