Comparing version 0.3.0 to 0.3.1
@@ -9,10 +9,9 @@ ////////////////////////////////////////// | ||
var fs = require('fs'), | ||
path = require('path'), | ||
basename = require('path').basename, | ||
http = require('http'), | ||
https = require('https'), | ||
url = require('url'), | ||
url_parse = require('url').parse, | ||
stringify = require('qs').stringify, | ||
version = JSON.parse(fs.readFileSync(__dirname + '/../package.json').toString()).version; | ||
// http.globalAgent.maxSockets = 128; | ||
exports.version = version; | ||
@@ -23,8 +22,12 @@ try { var unzip = require('zlib').unzip; } catch(e){ /* zlib not supported */ } | ||
default_user_agent += " (Node.js " + process.version + "; " + process.platform + " " + process.arch + ")"; | ||
var node_http_opts = ['agent', 'pfx', 'key', 'passphrase', 'cert', 'ca', 'rejectUnauthorized', 'requestCert']; | ||
var debug = !!process.env.DEBUG; | ||
var additional_http_opts = ['agent', 'pfx', 'key', 'passphrase', 'cert', 'ca', 'rejectUnauthorized', 'requestCert']; | ||
var parsers = { | ||
'application/json': function(data, callback){ | ||
callback(data && JSON.parse(data)); | ||
try { | ||
callback(null, data && JSON.parse(data)); | ||
} catch(e) { | ||
callback(e, data); | ||
} | ||
} | ||
@@ -36,13 +39,4 @@ }; | ||
parsers['application/xml'] = function(data, callback){ | ||
var xml_parser = new xml2js.Parser(); | ||
xml_parser.on('end', callback); | ||
xml_parser.on('error', function(result) { | ||
throw("Error parsing XML!") | ||
// callback(null); | ||
}); | ||
xml_parser.parseString(data); | ||
xml_parser.parseString(data, callback); | ||
}; | ||
@@ -78,6 +72,4 @@ } catch(e) { } | ||
var options = options || {}; | ||
if(uri.indexOf('http') == -1) uri = 'http://' + uri; | ||
if (uri.indexOf('http') == -1) uri = 'http://' + uri; | ||
var method = options.method || 'GET'; | ||
var config = { | ||
@@ -92,3 +84,3 @@ base_opts: {}, | ||
additional_http_opts.forEach(function(key){ | ||
node_http_opts.forEach(function(key){ | ||
if(typeof options[key] != 'undefined') | ||
@@ -107,6 +99,6 @@ config.base_opts[key] = options[key]; | ||
for(h in options.headers) | ||
for (h in options.headers) | ||
config.headers[h] = options.headers[h]; | ||
if(options.username && options.password){ | ||
if (options.username && options.password){ | ||
var b = new Buffer([options.username, options.password].join(':')); | ||
@@ -116,4 +108,4 @@ config.headers['Authorization'] = "Basic " + b.toString('base64'); | ||
if(data) { | ||
if(options.multipart){ | ||
if (data){ | ||
if (options.multipart){ | ||
@@ -139,9 +131,7 @@ var boundary = options.boundary || this.default_boundary; | ||
this.send_request(1, method, uri, config, post_data, callback); | ||
}, | ||
get_request_opts: function(method, uri, config){ | ||
var opts = config.base_opts, proxy = config.proxy; | ||
var remote = proxy ? url.parse(proxy) : url.parse(uri); | ||
var remote = proxy ? url_parse(proxy) : url_parse(uri); | ||
@@ -154,3 +144,3 @@ opts.host = remote.hostname; | ||
opts.headers = config.headers; | ||
opts.headers["Host"] = proxy ? url.parse(uri).hostname : remote.hostname; | ||
opts.headers["Host"] = proxy ? url_parse(uri).hostname : remote.hostname; | ||
@@ -168,8 +158,8 @@ return opts; | ||
if(timer) clearTimeout(timer); | ||
if (timer) clearTimeout(timer); | ||
if((response.statusCode == 301 || response.statusCode == 302) && response.headers.location){ | ||
if(count <= config.follow) | ||
if ((response.statusCode == 301 || response.statusCode == 302) && response.headers.location){ | ||
if (count <= config.follow) | ||
return self.send_request(++count, 'GET', response.headers.location, config, null, callback); | ||
else if(config.follow > 0) | ||
else if (config.follow > 0) | ||
return callback(new Error("Too many redirects. Possible redirect loop in " + response.headers.location)) | ||
@@ -205,8 +195,8 @@ } | ||
request.on('error', function(err) { | ||
if(process.env.DEBUG) console.log('Error on request: ' + err.toString()); | ||
if(timer) clearTimeout(timer); | ||
if(callback) callback(err || new Error("Unkown error on request.")); | ||
if (debug) console.log('Error on request: ' + err.toString()); | ||
if (timer) clearTimeout(timer); | ||
if (callback) callback(err || new Error("Unkown error on request.")); | ||
}); | ||
if(post_data) request.write(post_data, config.encoding); | ||
if (post_data) request.write(post_data, config.encoding); | ||
request.end(); | ||
@@ -218,9 +208,9 @@ | ||
if(process.env.DEBUG) console.log(response.headers); | ||
if(!callback) return; | ||
if (debug) console.log(response.headers); | ||
if (!callback) return; | ||
var content_type = response.headers['content-type'] && response.headers['content-type'].split(';')[0]; | ||
if(opts.parse_response && parsers[content_type]) { | ||
parsers[content_type](body, function(result){ | ||
callback(null, response, result); | ||
if (opts.parse_response && parsers[content_type]) { | ||
parsers[content_type](body, function(err, result){ | ||
callback(err, response, result); | ||
}); | ||
@@ -239,6 +229,6 @@ } else { | ||
for(var key in object){ | ||
for (var key in object){ | ||
var value = object[key]; | ||
if(value === null || typeof value == 'undefined') return --count; | ||
if (value === null || typeof value == 'undefined') return --count; | ||
@@ -248,3 +238,3 @@ var part = (value.buffer || value.file) && value.content_type ? value : {value: value}; | ||
this.generate_part(key, part, boundary, function(err, section){ | ||
if(err) return callback(err); | ||
if (err) return callback(err); | ||
body += section; | ||
@@ -265,3 +255,3 @@ --count || callback(null, body + '--' + boundary + '--'); | ||
if(data){ | ||
if (data){ | ||
return_part += "; filename=\"" + encodeURIComponent(filename) + "\"\r\n"; | ||
@@ -277,12 +267,10 @@ return_part += "Content-Type: " + part.content_type + "\r\n\r\n"; | ||
if((part.file || part.buffer) && part.content_type){ | ||
if ((part.file || part.buffer) && part.content_type){ | ||
var filename = part.filename ? part.filename : part.file ? path.basename(part.file) : name; | ||
if(part.buffer) return append(part.buffer, filename); | ||
var filename = part.filename ? part.filename : part.file ? basename(part.file) : name; | ||
if (part.buffer) return append(part.buffer, filename); | ||
fs.readFile(part.file, function(err, data){ | ||
if(err) return callback(err); | ||
append(data, filename); | ||
}); | ||
@@ -311,3 +299,3 @@ | ||
exports.post = function(uri, data, options, callback){ | ||
if(!data) throw('POST request expects data.'); | ||
if(!data || typeof data == 'function') throw('POST request expects data.'); | ||
return Needle.request(uri, 'POST', data, options, callback); | ||
@@ -317,3 +305,3 @@ } | ||
exports.put = function(uri, data, options, callback){ | ||
if(!data) throw('PUT request expects data.'); | ||
if(!data || typeof data == 'function') throw('PUT request expects data.'); | ||
return Needle.request(uri, 'PUT', data, options, callback); | ||
@@ -320,0 +308,0 @@ } |
{ | ||
"name": "needle" | ||
, "version": "0.3.0" | ||
, "version": "0.3.1" | ||
, "description": "Tiny yet feature-packed HTTP client. With deflate & multipart support." | ||
@@ -5,0 +5,0 @@ , "keywords": ["http", "https", "client", "multipart", "deflate", "timeout", "basic-auth", "simple"] |
@@ -51,3 +51,3 @@ Needle | ||
More examples | ||
Examples | ||
------------- | ||
@@ -54,0 +54,0 @@ |
4
29776
360