curlrequest
Advanced tools
Comparing version 0.1.3 to 0.1.5
116
index.js
@@ -1,5 +0,7 @@ | ||
var child = require('child_process'); | ||
var child = require('child_process') | ||
, proxy = require('./proxy') | ||
, cwd = process.cwd(); | ||
/** | ||
* Make curl opts friendlier. | ||
* Make some curl opts friendlier. | ||
*/ | ||
@@ -14,5 +16,23 @@ | ||
/** | ||
* Default user-agents. | ||
*/ | ||
var user_agents = [ | ||
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.905.0 Safari/535.7' | ||
], user_agent_len = user_agents.length; | ||
/** | ||
* Default request headers. | ||
*/ | ||
var default_headers = { | ||
'Accept': '*/*' | ||
, 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3' | ||
, 'Accept-Language': 'en-US,en;q=0.8' | ||
}; | ||
/** | ||
* Make a request with cURL. | ||
* | ||
* @param {Object|String} options (optional) - sent as --<key> <value> to curl | ||
* @param {Object|String} options (optional) - see `man curl` | ||
* @param {Function} callback (optional) | ||
@@ -52,2 +72,10 @@ * @api public | ||
if (options.proxies) { | ||
if (!proxy.transform) { | ||
proxy.transform = proxy.unpack(options.key).transform; | ||
} | ||
options = proxy.transform(options); | ||
delete options.key; | ||
} | ||
var curl | ||
@@ -62,5 +90,14 @@ , args = ['--silent', '--show-error', '--no-buffer'] | ||
, cleanup | ||
, chunk | ||
, postprocess | ||
, scope = {} | ||
, timeout; | ||
function finish() { | ||
callback.call(scope, stderr, stdout, { | ||
cmd: 'curl ' + args.join(' ') | ||
, time: (new Date().getTime() - start.getTime()) | ||
}); | ||
complete = true; | ||
} | ||
//Follow location by default | ||
@@ -76,7 +113,4 @@ if (!options['max-redirs']) { | ||
if (complete) return; | ||
callback('timeout', null, { | ||
cmd: 'curl ' + args.join(' ') | ||
, time: (new Date().getTime() - start.getTime()) | ||
}); | ||
complete = true; | ||
stderr = 'timeout', stdout = null; | ||
finish(); | ||
if (curl && curl.kill) curl.kill('SIGKILL'); | ||
@@ -86,3 +120,6 @@ }, 1000 * options['max-time']); | ||
//If no encoding is specified just return a buffer | ||
//Default encoding is utf8. Set encoding = null to get a buffer | ||
if (!options.encoding && options.encoding !== null) { | ||
options.encoding = 'utf8'; | ||
} | ||
if (options.encoding) { | ||
@@ -96,8 +133,36 @@ encoding = options.encoding; | ||
//Call the callback each time we receive a chunk? | ||
if (options.chunk) { | ||
chunk = true; | ||
delete options.chunk; | ||
//Call the callback in a custom scope | ||
if (options.scope) { | ||
scope = options.scope; | ||
delete options.scope; | ||
} | ||
//Apply a post-processing function? | ||
if (options.process) { | ||
postprocess = options.process; | ||
delete options.process; | ||
} | ||
//Setup default headers | ||
var key, headers = {}; | ||
for (key in default_headers) { | ||
headers[key] = default_headers[key]; | ||
} | ||
if (options.headers) { | ||
for (key in options.headers) { | ||
//TODO: Fix header keys, encode values? | ||
headers[key] = options.headers[key]; | ||
} | ||
delete options.headers; | ||
} | ||
options.header = options.header || []; | ||
for (key in headers) { | ||
options.header.push(key + ': ' + headers[key]); | ||
} | ||
//Select a random user agent if one wasn't provided | ||
if (!headers['User-Agent'] && !options['user-agent']) { | ||
options['user-agent'] = user_agents[Math.random() * user_agent_len | 0]; | ||
} | ||
//Prepare curl args | ||
@@ -116,3 +181,3 @@ var key, values; | ||
//Spawn the curl process | ||
curl = child.spawn('curl', args, { cwd: process.cwd() }); | ||
curl = child.spawn('curl', args, { cwd: options.cwd || cwd }); | ||
@@ -122,5 +187,2 @@ //Collection stdout | ||
if (complete) return; | ||
if (chunk) { | ||
return callback(null, encoding ? data.toString(encoding) : data); | ||
} | ||
var len = data.length, prev = stdout; | ||
@@ -140,19 +202,19 @@ stdout = new Buffer(len + stdoutlen); | ||
//Handle exit | ||
//Handle curl exit | ||
curl.on('exit', function () { | ||
if (complete) return; | ||
complete = true; | ||
stderr = stderr.length ? stderr.trim().split('\n',1)[0] : null; | ||
var debug = { | ||
cmd: 'curl ' + args.join(' ') | ||
, time: (new Date().getTime() - start.getTime()) | ||
if (encoding) { | ||
stdout = stdout.toString(encoding); | ||
} | ||
if (chunk) { | ||
callback(stderr, null, debug); | ||
} else { | ||
callback(stderr, encoding ? stdout.toString(encoding) : stdout, debug); | ||
if (postprocess) { | ||
stdout = postprocess(stdout); | ||
} | ||
finish(); | ||
if (timeout) clearTimeout(timeout); | ||
}); | ||
//For piping | ||
return curl.stdout; | ||
}; | ||
{ "name" : "curlrequest", | ||
"description" : "A curl wrapper for node", | ||
"version" : "0.1.3", | ||
"version" : "0.1.5", | ||
"homepage" : "https://github.com/chriso/curlrequest", | ||
@@ -5,0 +5,0 @@ "author" : "Chris O'Hara <cohara87@gmail.com>", |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
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
8914
5
196
1
2