curlrequest
Advanced tools
Comparing version 0.1.0 to 0.1.1
41
index.js
@@ -6,3 +6,3 @@ var child = require('child_process'); | ||
* | ||
* @param {Object} options (optional) - sent as --<key> <value> to curl | ||
* @param {Object|String} options (optional) - sent as --<key> <value> to curl | ||
* @param {Function} callback (optional) | ||
@@ -32,4 +32,9 @@ * @api public | ||
if (typeof options === 'string') { | ||
options = { url: options }; | ||
} | ||
var curl | ||
, args = ['-sS'] | ||
, args = ['--silent', '--show-error', '--no-buffer'] | ||
, start = new Date | ||
, stderr = '' | ||
@@ -44,18 +49,22 @@ , stdoutlen | ||
//Set a timeout? | ||
if (options.timeout) { | ||
options['retry-max-time'] = options.timeout; | ||
options['max-time'] = options.timeout; | ||
//Follow location by default | ||
if (!options['max-redirs']) { | ||
options.location = true; | ||
options['max-redirs'] = 3; | ||
} | ||
//Add an additional setTimeout for max-time | ||
if (options['max-time']) { | ||
timeout = setTimeout(function () { | ||
if (complete) return; | ||
callback('timeout'); | ||
callback('timeout', null, { | ||
cmd: 'curl ' + args.join(' ') | ||
, time: (new Date().getTime() - start.getTime()) | ||
}); | ||
complete = true; | ||
if (curl && curl.kill) curl.kill('SIGKILL'); | ||
}, 1000 * options.timeout); | ||
delete options.timeout; | ||
}, 1000 * options['max-time']); | ||
} | ||
//If no encoding is specified, just return a buffer | ||
//If no encoding is specified just return a buffer | ||
if (options.encoding) { | ||
@@ -114,7 +123,11 @@ encoding = options.encoding; | ||
complete = true; | ||
stderr = stderr.length ? stderr.trim() : null; | ||
stderr = stderr.length ? stderr.trim().split('\n',1)[0] : null; | ||
var debug = { | ||
cmd: 'curl ' + args.join(' ') | ||
, time: (new Date().getTime() - start.getTime()) | ||
} | ||
if (chunk) { | ||
callback(stderr, null); | ||
callback(stderr, null, debug); | ||
} else { | ||
callback(stderr, encoding ? stdout.toString(encoding) : stdout); | ||
callback(stderr, encoding ? stdout.toString(encoding) : stdout, debug); | ||
} | ||
@@ -121,0 +134,0 @@ if (timeout) clearTimeout(timeout); |
{ "name" : "curlrequest", | ||
"description" : "A curl wrapper for node", | ||
"version" : "0.1.0", | ||
"version" : "0.1.1", | ||
"homepage" : "https://github.com/chriso/curlrequest", | ||
@@ -5,0 +5,0 @@ "author" : "Chris O'Hara <cohara87@gmail.com>", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5340
119