From cce9b4b1608d651f81847494b18808caf36d796c Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Fri, 5 Jul 2013 09:34:36 -0700 Subject: [PATCH] fix .pipe() unzipping on more recent nodes. Closes #240 --- lib/node/index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/node/index.js b/lib/node/index.js index 8741321b8..7cacbfb30 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -17,6 +17,7 @@ var https = require('https'); var http = require('http'); var fs = require('fs'); var qs = require('qs'); +var zlib = require('zlib'); var util = require('util'); /** @@ -381,9 +382,14 @@ Request.prototype.write = function(data, encoding){ */ Request.prototype.pipe = function(stream, options){ + this.piped = true; // HACK... this.buffer(false); this.end().req.on('response', function(res){ - res.pipe(stream, options); + if (/^(deflate|gzip)$/.test(res.headers['content-encoding'])) { + res.pipe(zlib.createUnzip()).pipe(stream, options); + } else { + res.pipe(stream, options); + } }); return stream; }; @@ -666,6 +672,7 @@ Request.prototype.end = function(fn){ var type = type[0]; var multipart = 'multipart' == type; var redirect = isRedirect(res.statusCode); + if (self.piped) return; // redirect if (redirect && self._redirects++ != max) {