Comparing version 0.1.2 to 0.1.3
57
index.js
"use strict"; | ||
var zlib = require("zlib"); | ||
var stream = require("stream"), | ||
util = require("util"), | ||
zlib = require("zlib"); | ||
@@ -8,2 +10,44 @@ var copy = require("request/lib/copy"), | ||
var ConditionalUnzip = function() { | ||
stream.Transform.call(this); | ||
var dests = [], | ||
source; | ||
this.on("pipe", function(src) { | ||
if (src instanceof _request.Request) { | ||
src.on("response", function(res) { | ||
if (["gzip", "deflate"].some(function(enc) { | ||
return res.headers["content-encoding"] === enc; | ||
})) { | ||
// response was compressed | ||
source = res.pipe(zlib.createUnzip()); | ||
} else { | ||
source = res; | ||
} | ||
dests.forEach(function(dest) { | ||
source.pipe(dest); | ||
}); | ||
}); | ||
} | ||
}); | ||
this.pipe = function(dest) { | ||
if (source) { | ||
source.pipe(dest); | ||
} else { | ||
dests.push(dest); | ||
} | ||
}; | ||
this._transform = function(buffer, encoding, callback) { | ||
// drop (compressed) chunks on the floor since we can't get request to stop | ||
// sending them to us | ||
return callback(); | ||
}; | ||
}; | ||
util.inherits(ConditionalUnzip, stream.Transform); | ||
var request = function(uri, options, callback) { | ||
@@ -40,2 +84,5 @@ | ||
// provide a default callback | ||
callback = callback || function() {}; | ||
options.headers = options.headers || {}; | ||
@@ -46,3 +93,3 @@ | ||
return new _request.Request(options) | ||
var request = new _request.Request(options) | ||
.on("response", function(res) { | ||
@@ -65,3 +112,5 @@ var stream = this; | ||
}) | ||
.on("error", console.warn) | ||
.on("error", function(err) { | ||
console.warn(err.stack); | ||
}) | ||
.on("end", function() { | ||
@@ -91,2 +140,4 @@ res.body = Buffer.concat(chunks); | ||
}); | ||
return request.pipe(new ConditionalUnzip()); | ||
}; | ||
@@ -93,0 +144,0 @@ |
{ | ||
"name": "crequest", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "request+compression--drop-in wrapper for 'request'", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17787
10
209