rvagg / through2

Tiny wrapper around Node streams2 Transform to avoid explicit subclassing noise
MIT License
1.9k stars 106 forks source link

No transformFunction, but not passing the same data #54

Closed kevinsimper closed 9 years ago

kevinsimper commented 9 years ago

I am using through2 to inspect a stream that i proxy.

But they input is not the same as the output apparently, even if i do a pass through stream like this:

.pipe(through2())

I also tried .pipe(through2({decodeStrings: false})), but that did not help.

How can that be? I am piping a request to a node http res. I am really lost on this!

and according to the docs if you do not pass anything it

If you do not provide a transformFunction then you will get a simple pass-through stream.
mafintosh commented 9 years ago

Could you share a full example?

juliangruber commented 9 years ago

does it work if you pipe to a require('stream').PassThrough(), the node core implementation?

kevinsimper commented 9 years ago

@juliangruber No, it does not, was does that tell? @mafintosh I just tried to replicate the problem in a condensed script, which works perfectly (irritating) https://gist.github.com/kevinsimper/5b962095aea3cb833d6c

But basicly a express app that proxies requests, it works perfect without throught2, so that is good, but whenever I insert through2 into the pipes, it fails.

I tried zlib.createGUnzip and zlib.createGzip because I did not know if it was because I was reading gziped and passing on something else, but did get any clevere.

var proxyRequest = request({
      url: url,
      method: req.method,
      headers: {
        'Host': host
      }
    })

    req
    .pipe(through2()) // Works fine if leaved in
    .pipe(proxyRequest)
    .pipe(through2()) // does not work when inserted here, even through it should just pass through to res
    .pipe(res)
mafintosh commented 9 years ago

request actually does a lot of magic stuff with the destination you pipe it to (https://github.com/request/request/blob/master/request.js#L1338-L1356) like setting headers if it's a http response. by inserting the passthrough stream you loose this. could it be related to something like this?

kevinsimper commented 9 years ago

@mafintosh Yes, your right! Thanks for the hint!

Pretty stupid to rewrite pipe just to make it magic without mentioning it in the docs. You would assume using other libs that pipe is standard across all, but apparently not.