Permalink
Browse files

zlib soft-dependent (closes #60)

  • Loading branch information...
1 parent 4fe9cc4 commit 6595b96cb0868d6ac4015456e9a45607a1ff8498 @disfated disfated committed Jan 15, 2012
Showing with 52 additions and 26 deletions.
  1. +1 −1 README.md
  2. +12 −4 lib/restler.js
  3. +1 −2 package.json
  4. +1 −0 test/all.js
  5. +37 −19 test/restler.js
View
@@ -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`
View
@@ -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;
@@ -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 || {});
@@ -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
View
@@ -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",
View
@@ -1,4 +1,5 @@
+require('./restler'); // debug
var nodeunit = require('nodeunit');
var reporter = nodeunit.reporters['default'];
process.chdir(__dirname);
View
@@ -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;
@@ -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) {
@@ -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'] = {
@@ -600,4 +619,3 @@ module.exports['Content-Length'] = {
}
};
-

0 comments on commit 6595b96

Please sign in to comment.