How to force Node.js Transform stream to finish? -


consider following scenario. have 2 node transform streams:

transform stream 1

function t1(options) {   if (! (this instanceof t1)) {     return new t1(options);   }    transform.call(this, options); } util.inherits(t1, transform);  t1.prototype._transform = function(chunk, encoding, done) {   console.log("### transforming in t1");   this.push(chunk);   done(); };  t1.prototype._flush = function(done) {   console.log("### done in t1");   done(); }; 

transform stream 2

function t2(options) {   if (! (this instanceof t2)) {     return new t2(options);   }    transform.call(this, options); } util.inherits(t2, transform);  t2.prototype._transform = function(chunk, encoding, done) {   console.log("### transforming in t2");   this.push(chunk);   done(); };  t2.prototype._flush = function(done) {   console.log("### done in t2");   done(); }; 

and, i'm wanting apply these transform streams before returning response. have simple http server, , on each request, fetch resource , these transformations applied fetched resource , send result of second transformation original response:

var options = require('url').parse('http://localhost:1234/data.json'); options.method = 'get';  http.createserver(function(req, res) {   var req = http.request(options, function(httpres) {     var t1 = new t1({});     var t2 = new t2({});      httpres       .pipe(t1)       .pipe(t2)       .on('finish', function() {         // other stuff in here before sending request         t2.pipe(res, { end : true });       });   });    req.end(); }).listen(3001); 

ultimately, finish event never gets called, , request hangs , times out because response never resolved. i've noticed if pipe t2 res, seems work fine:

  .pipe(t1)   .pipe(t2)   .pipe(res, { end : true }); 

but, scenario doesn't seem feasible because need work before returning response.

this happens because need let node know stream being consumed somewhere, otherwise last stream fill buffer , considering data longer highwatermark option (usually 16) , halt waiting data consumed.

there 3 ways of consuming stream in full:

  • piping readable stream (what did in second part of question)
  • reading consecutive chunks calling read method of stream
  • listening on "data" events (essentially stream.on("data", somefunc)).

the last option quickest, result in consuming stream without looking @ memory usage.

i'd note using "finish" event might little misleading, since called when last data read, not emitted. on transform stream, since it's readable it's better use "end" event.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -