title: How to force Node.js Transform stream to finish?
body: <p>Consider the following scenario. I have two Node Transform streams:</p>
  
  <p><strong>Transform stream 1</strong></p>
  
  <pre><code>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();
  };
  </code></pre>
  
  <p><strong>Transform stream 2</strong></p>
  
  <pre><code>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();
  };
  </code></pre>
  
  <p>And, I'm wanting to <em>apply</em> these transform streams before returning a response. I have a simple HTTP server, and on each request, I fetch a resource and would like these transformations to be applied to this fetched resource and then send the result of the second transformation to the original response:</p>
  
  <pre><code>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() {
          // Do other stuff in here before sending request back
          t2.pipe(res, { end : true });
        });
    });
  
    req.end();
  }).listen(3001);
  </code></pre>
  
  <p>Ultimately, the <code>finish</code> event never gets called, and the request hangs and times out because the response is never resolved. I've noticed that if I just pipe <code>t2</code> into <code>res</code>, it seems to work fine:</p>
  
  <pre><code>  .pipe(t1)
    .pipe(t2)
    .pipe(res, { end : true });
  </code></pre>
  
  <p>But, this scenario doesn't seem feasible because I need to do some extra work before returning the response.</p>