Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #4 from sphericalhorse/master

Replace node-compress with built-in zlib module
  • Loading branch information...
commit 02ffb19abe182fe006cc57e45a6c444c72eaf1fc 2 parents 776b0ff + 5787a36
@slaskis slaskis authored
Showing with 21 additions and 22 deletions.
  1. +2 −2 README.md
  2. +11 −15 lib/schemes/http.js
  3. +8 −5 lib/utils.js
View
4 README.md
@@ -47,7 +47,7 @@ Uses the [addressable module](https://github.com/publicclass/addressable) for pa
### Options for HTTP(S)
* `follow` (boolean) Follow redirects. Defaults to `true`.
-* `gzip` (boolean) If the request should be attempted with gzip. Defaults to `true` if the [node-compress module](https://github.com/waveto/node-compress) is available.
+* `gzip` (boolean) If the request should be attempted with gzip. Defaults to `true`.
* `headers` (object) Headers to pass along with the HTTP request.
* `method` (string) HTTP request method. Default 'GET'.
* `body` (readablestream|string|buffer) A body of data to pass with the request.
@@ -196,4 +196,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
26 lib/schemes/http.js
@@ -10,7 +10,7 @@
* {Object} headers
* Headers to pass along with the HTTP request.
* {Boolean} gzip
-* If the request should be attempted with gzip. Defaults to `true` if the 'node-compress'-module is available.
+* If the request should be attempted with gzip. Defaults to `true`.
* {String} method
* HTTP request method. Default 'GET'.
* {ReadableStream|Buffer|String} body
@@ -21,18 +21,15 @@ var addressable = require("addressable")
, open = require("../open-uri")
, mime = require("mime")
, utils = require("../utils")
- , Buffer = require("buffer").Buffer;
+ , Buffer = require("buffer").Buffer
+ , zlib = require("zlib");
-var gzip = true;
-try {
- var compress = require("compress") }
-catch(e) {
- gzip = false
-}
-
-module.exports = function http(uri,opts,output){
+module.exports = function http(uri,opts,output){
uri.headers = opts.headers = opts.headers || {}
+ if( !opts.hasOwnProperty("gzip") )
+ opts.gzip = true
+
// Default to buffer (using `opts.stream` for backward compability)
if( !opts.hasOwnProperty('buffer') )
opts.buffer = !opts.stream;
@@ -42,8 +39,7 @@ module.exports = function http(uri,opts,output){
uri.headers.authorization = "Basic " + utils.toBase64(uri.userinfo);
// Use gzip?
- var gzip = typeof opts.gzip == "undefined" ? gzip : opts.gzip;
- if( gzip && !(uri.headers["accept-encoding"] || uri.headers["Accept-Encoding"] || "") )
+ if( opts.gzip && !uri.headers["accept-encoding"] && !uri.headers["Accept-Encoding"] )
uri.headers["accept-encoding"] = "gzip";
// HTTP method
@@ -52,7 +48,7 @@ module.exports = function http(uri,opts,output){
// Body Content-Length
if( typeof opts.body == "string" )
uri.headers["content-length"] = Buffer.byteLength( opts.body );
- if( Buffer.isBuffer( opts.body ) )
+ else if( Buffer.isBuffer( opts.body ) )
uri.headers["content-length"] = opts.body.length;
var req = require(uri.scheme).request(uri,function(res){
@@ -68,8 +64,8 @@ module.exports = function http(uri,opts,output){
// Handle gzip responses
var stream = res;
- if( gzip && ~(res.headers['content-encoding']||"").indexOf("gzip") )
- res.pipe(stream = new compress.GunzipStream());
+ if( opts.gzip && ~(res.headers['content-encoding']||"").indexOf("gzip") )
+ res.pipe(stream = zlib.createUnzip());
if( typeof output == "function" ){
if( opts.buffer ){
View
13 lib/utils.js
@@ -16,16 +16,19 @@ var mime = require("mime")
exports.buffer = function buffer(type,path,stream,output){
// Fix the type if it comes from a HTTP header
type = (type||'').split(";")[0];
- var mimeType = mime.lookup(type||path||'')
+ var mimeType = mime.lookup(type||path||''), encoding = mime.charsets.lookup(mimeType, 'utf8');
var buf = [], len = 0;
- stream.setEncoding(mime.charsets.lookup(mimeType))
stream.on("error",function(err){ error(output, err); }) // TODO Close the stream on error?
stream.on("timeout",function(){ error(output,new Error("[OpenURI] Connection timed out.")); }) // TODO Close the stream on timeout?
- stream.on("data",function(chunk){ buf.push(chunk); len += Buffer.byteLength(chunk); })
+ stream.on("data",function(chunk){
+ chunk = chunk.toString(encoding);
+ buf.push(chunk);
+ len += Buffer.byteLength(chunk);
+ })
stream.on("end",function(){
try {
var data = parse(type,buf,len) || parse(mimeType,buf,len) || parse.default(buf,len);
- output(null,data,stream)
+ output(null,data,stream);
} catch(e){
error(output,e)
}
@@ -98,4 +101,4 @@ exports.toBase64 = function toBase64(str){
*/
exports.isValidOutput = function isValidOutput(output){
return typeof output == "function" || ( typeof output == "object" && output.writable && "pipe" in output )
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.