Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

iconv soft-dependent

  • Loading branch information...
commit 57d80beb71f0ac34b52f5492bd70d4746617e92d 1 parent c510a4b
@disfated disfated authored
Showing with 47 additions and 31 deletions.
  1. +1 −1  README.md
  2. +27 −19 lib/restler.js
  3. +2 −2 package.json
  4. +17 −9 test/restler.js
View
2  README.md
@@ -22,7 +22,7 @@ Features
* 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) (requires node 0.6+)
-* Transparently handle different content charsets via `iconv`
+* Transparently handle different content charsets via [iconv](https://github.com/bnoordhuis/node-iconv) (if available)
API
View
46 lib/restler.js
@@ -1,16 +1,20 @@
-var sys = require('util'),
- http = require('http'),
- https = require('https'),
- url = require('url'),
- qs = require('querystring'),
- multipart = require('./multipartform'),
- zlib = null,
- Iconv = require('iconv').Iconv;
+var sys = require('util');
+var http = require('http');
+var https = require('https');
+var url = require('url');
+var qs = require('querystring');
+var multipart = require('./multipartform');
+var zlib = null;
+var Iconv = null;
try {
zlib = require('zlib');
} catch (err) {}
+try {
+ Iconv = require('iconv').Iconv;
+} catch (err) {}
+
function mixin(target, source) {
Object.keys(source).forEach(function(key) {
target[key] = source[key];
@@ -153,18 +157,22 @@ mixin(Request.prototype, {
}
},
_iconv: function(body, response) {
- var charset = response.headers['content-type'];
- if (!charset) return body;
- charset = charset.match(/\bcharset=(.+)(?:;|$)/i);
- if (!charset) return body;
- charset = charset[1].trim().toUpperCase();
- if (charset == 'UTF-8') return body;
- try {
- var iconv = new Iconv(charset, 'UTF-8//TRANSLIT//IGNORE');
- return iconv.convert(body);
- } catch (err) {
- return body;
+ if (Iconv) {
+ var charset = response.headers['content-type'];
+ if (charset) {
+ charset = /\bcharset=(.+)(?:;|$)/i.exec(charset);
+ if (charset) {
+ charset = charset[1].trim().toUpperCase();
+ if (charset != 'UTF-8') {
+ try {
+ var iconv = new Iconv(charset, 'UTF-8//TRANSLIT//IGNORE');
+ return iconv.convert(body);
+ } catch (err) {}
+ }
+ }
+ }
}
+ return body;
},
_encode: function(body, response, callback) {
var self = this;
View
4 package.json
@@ -8,11 +8,11 @@
"main" : "./lib/restler",
"engines": { "node": ">= 0.3.7" },
"dependencies": {
- "iconv": ">=1.0.0"
},
"devDependencies": {
"nodeunit": ">=0.5.0",
"xml2js" : ">=0.1.0",
- "yaml" : ">=0.2.0"
+ "yaml" : ">=0.2.0",
+ "iconv": ">=1.0.0"
}
}
View
26 test/restler.js
@@ -6,10 +6,16 @@ var path = require('path');
var fs = require('fs');
var crypto = require('crypto');
var zlib = null;
+var Iconv = null;
+
try {
zlib = require('zlib');
} catch (err) {}
+try {
+ Iconv = require('iconv').Iconv;
+} catch (err) {}
+
var p = sys.inspect;
var port = 9000;
@@ -530,15 +536,17 @@ var charsetCases = {
'gbk' : '01329db97a6a202ecffaf95d4f77a18d'
};
-for (var charset in charsetCases) {
- (function(charset, hash) {
- module.exports['Charsets']['Should correctly convert charset ' + charset] = function(test) {
- rest.get(host + '/' + charset).on('complete', function(data) {
- test.equal(md5(Buffer(data, 'utf8')), hash, 'hashes should match');
- test.done();
- });
- };
- })(charset, charsetCases[charset]);
+if (Iconv) {
+ for (var charset in charsetCases) {
+ (function(charset, hash) {
+ module.exports['Charsets']['Should correctly convert charset ' + charset] = function(test) {
+ rest.get(host + '/' + charset).on('complete', function(data) {
+ test.equal(md5(Buffer(data, 'utf8')), hash, 'hashes should match');
+ test.done();
+ });
+ };
+ })(charset, charsetCases[charset]);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.