Commit 07b35712 by Adeel Committed by vagrant

Install: Use npmconf to get proxy config

parent 97e15634
...@@ -51,6 +51,7 @@ ...@@ -51,6 +51,7 @@
"mkdirp": "^0.5.0", "mkdirp": "^0.5.0",
"mocha": "^2.0.1", "mocha": "^2.0.1",
"nan": "^1.3.0", "nan": "^1.3.0",
"npmconf": "^2.1.1",
"object-assign": "^2.0.0", "object-assign": "^2.0.0",
"replace-ext": "0.0.1", "replace-ext": "0.0.1",
"request": "^2.48.0", "request": "^2.48.0",
......
...@@ -9,70 +9,64 @@ var fs = require('fs'), ...@@ -9,70 +9,64 @@ var fs = require('fs'),
* *
* @param {String} url * @param {String} url
* @param {String} dest * @param {String} dest
* @param {function} cb * @param {Function} cb
* @api private * @api private
*/ */
function download(url, dest, cb) { function download(url, dest, cb) {
var file = fs.createWriteStream(dest); var file = fs.createWriteStream(dest);
var options = { proxy: getProxy(), rejectUnauthorized: false };
var returnError = function(err) {
fs.unlink(dest);
cb(typeof err.message === 'string' ? err.message : err);
};
var req = request.get(url, options).on('response', function(response) {
if (response.statusCode < 200 || response.statusCode >= 300) {
returnError('Can not download file from ' + url);
return;
}
response.pipe(file);
file.on('finish', function() { applyProxy({ rejectUnauthorized: false }, function(options) {
var returnError = function(err) {
fs.unlink(dest);
cb(typeof err.message === 'string' ? err.message : err);
};
var req = request.get(url, options).on('response', function(response) {
if (response.statusCode < 200 || response.statusCode >= 300) {
returnError('Can not download file from ' + url);
return;
}
response.pipe(file);
file.on('finish', function() {
file.close(cb); file.close(cb);
}); });
}).on('error', returnError); }).on('error', returnError);
req.end(); req.end();
req.on('error', returnError); req.on('error', returnError);
});
} }
/** /**
* Get proxy settings * Get applyProxy settings
* *
* @param {Object} options
* @param {Function} cb
* @api private * @api private
*/ */
function getProxy() { function applyProxy(options, cb) {
var result = ['https-proxy', 'proxy', 'http-proxy'].filter(function(config) { require('npmconf').load({}, function (er, conf) {
var proxy = exec('npm config get ' + config, {silent: true}); var getProxyFromEnv = true;
['https-proxy', 'proxy', 'http-proxy'].forEach(function(setting) {
return proxy.code === 0 && validateProxyUrl(proxy.output.trim()); var proxyUrl = conf.get(setting);
})[0];
if (result) {
return result;
}
var env = process.env;
return env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy;
}
/** if(proxyUrl && proxyUrl === require('url').parse(proxyUrl)) {
* Validates Proxy URL options.proxy = proxyUrl;
* getProxyFromEnv = false;
* @param {String} url cb(options);
* @api private return;
*/ }
});
function validateProxyUrl(url) { if(getProxyFromEnv) {
if (/\n/.test(url)) { var env = process.env;
url = url.replace(/\r?\n+/, '\n').split('\n'); options.proxy = env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy;
url = url[url.length - 3].trim(); // get the second last element.
}
return url !== 'null' && cb(options);
url !== 'undefined' && }
url === require('url').parse(url); });
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment