Skip to content

Commit

Permalink
zlib soft-dependent (closes danwrong#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
disfated committed Jan 15, 2012
1 parent 4fe9cc4 commit 6595b96
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Features
* Transparently handle SSL (just specify https in the URL)
* Deals with basic auth for you, just provide username and password options
* Simple service wrapper that allows you to easily put together REST API libraries
* Transparently handle content-encoded responses (gzip, deflate)
* Transparently handle content-encoded responses (gzip, deflate) (requires node 0.6+)
* Transparently handle different content charsets via `iconv`


Expand Down
16 changes: 12 additions & 4 deletions lib/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ var sys = require('util'),
url = require('url'),
qs = require('querystring'),
multipart = require('./multipartform'),
zlib = require('zlib'),
zlib = null,
Iconv = require('iconv').Iconv;


try {
zlib = require('zlib');
} catch (err) {}

function mixin(target, source) {
Object.keys(source).forEach(function(key) {
target[key] = source[key];
});

return target;
}

function Request(uri, options) {
this.url = url.parse(uri);
this.options = options;
Expand All @@ -23,6 +27,10 @@ function Request(uri, options) {
'User-Agent': 'Restler for node.js',
'Host': this.url.host
};

if (zlib) {
this.headers['Accept-Encoding'] = 'gzip, deflate';
}

mixin(this.headers, options.headers || {});

Expand Down Expand Up @@ -97,7 +105,7 @@ mixin(Request.prototype, {
_responseHandler: function(response) {
var self = this;

if (this._isRedirect(response) && this.options.followRedirects == true) {
if (this._isRedirect(response) && this.options.followRedirects) {
try {
var location = url.resolve(this.url, response.headers['location']);
Request.call(this, location, this.options); // reusing request object to handle recursive redirects
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"main" : "./lib/restler",
"engines": { "node": ">= 0.3.7" },
"dependencies": {
"iconv": ">=1.0.0",
"zlib" : ">=1.0.0"
"iconv": ">=1.0.0"
},
"devDependencies": {
"nodeunit": ">=0.5.0",
Expand Down
1 change: 1 addition & 0 deletions test/all.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

require('./restler'); // debug
var nodeunit = require('nodeunit');
var reporter = nodeunit.reporters['default'];
process.chdir(__dirname);
Expand Down
56 changes: 37 additions & 19 deletions test/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
var rest = require('../lib/restler');
var http = require('http');
var sys = require('util');
var zlib = require('zlib');
var path = require('path');
var fs = require('fs');
var crypto = require('crypto');
var zlib = null;
try {
zlib = require('zlib');
} catch (err) {}

var p = sys.inspect;

Expand Down Expand Up @@ -322,31 +325,43 @@ module.exports['Deserialization'] = {
},

'Should gunzip': function(test) {
rest.get(host + '/gzip').on('complete', function(data) {
test.re(data, /^(compressed data){10}$/, 'returned: ' + p(data));
if (zlib) {
rest.get(host + '/gzip').on('complete', function(data) {
test.re(data, /^(compressed data){10}$/, 'returned: ' + p(data));
test.done();
});
} else {
test.done();
})
}
},

'Should inflate': function(test) {
rest.get(host + '/deflate').on('complete', function(data) {
test.re(data, /^(compressed data){10}$/, 'returned: ' + p(data));
if (zlib) {
rest.get(host + '/deflate').on('complete', function(data) {
test.re(data, /^(compressed data){10}$/, 'returned: ' + p(data));
test.done();
})
} else {
test.done();
})
}
},

'Should decode and parse': function(test) {
rest.get(host + '/truth').on('complete', function(data) {
try {
with (data) {
var result = what + (is + the + answer + to + life + the + universe + and + everything).length;
if (zlib) {
rest.get(host + '/truth').on('complete', function(data) {
try {
with (data) {
var result = what + (is + the + answer + to + life + the + universe + and + everything).length;
}
test.equal(result, 42, 'returned: ' + p(data));
} catch (err) {
test.ok(false, 'returned: ' + p(data));
}
test.equal(result, 42, 'returned: ' + p(data));
} catch (err) {
test.ok(false, 'returned: ' + p(data));
}
test.done();
})
} else {
test.done();
})
}
},

'Should decode as buffer': function(test) {
Expand Down Expand Up @@ -487,9 +502,13 @@ function charsetsResponse(request, response) {
var charset = request.url.substr(1);
response.writeHead(200, {
'content-type': 'text/plain; charset=' + charset,
'content-encoding': 'gzip'
'content-encoding': zlib ? 'gzip' : ''
});
fs.createReadStream(path.join(__dirname, charsetsDir, charset)).pipe(zlib.createGzip()).pipe(response);
var stream = fs.createReadStream(path.join(__dirname, charsetsDir, charset));
if (zlib) {
stream = stream.pipe(zlib.createGzip());
}
stream.pipe(response);
}

module.exports['Charsets'] = {
Expand Down Expand Up @@ -600,4 +619,3 @@ module.exports['Content-Length'] = {
}

};

0 comments on commit 6595b96

Please sign in to comment.