You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varstream=require("stream");varduplexer2=require("duplexer2");varwritable=newstream.Writable({objectMode: true}),readable=newstream.Readable({objectMode: true});writable._write=function_write(input,encoding,done){if(readable.push(input)){returndone();}else{readable.once("drain",done);}};readable._read=function_read(n){// no-op};// simulate the readable thing closing after a bitwritable.once("finish",function(){setTimeout(function(){readable.push(null);},500);});varduplex=duplexer2(writable,readable);duplex.on("data",function(e){console.log("got data",JSON.stringify(e));});duplex.on("finish",function(){console.log("got finish event");});duplex.on("end",function(){console.log("got end event");});duplex.write("oh, hi there",function(){console.log("finished writing");});duplex.end(function(){console.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.