Permalink
Browse files

replace node-compress module with built-in zlib

  • Loading branch information...
1 parent d2f03c0 commit 5787a3625a0f463a6b97b99778349b1884ea3839 @sphericalhorse sphericalhorse committed Jul 25, 2012
Showing with 19 additions and 20 deletions.
  1. +11 −15 lib/schemes/http.js
  2. +8 −5 lib/utils.js
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 )
-}
+}

0 comments on commit 5787a36

Please sign in to comment.