Commit 07b35712 by Adeel Committed by vagrant

Install: Use npmconf to get proxy config

parent 97e15634
......@@ -51,6 +51,7 @@
"mkdirp": "^0.5.0",
"mocha": "^2.0.1",
"nan": "^1.3.0",
"npmconf": "^2.1.1",
"object-assign": "^2.0.0",
"replace-ext": "0.0.1",
"request": "^2.48.0",
......
......@@ -9,13 +9,14 @@ var fs = require('fs'),
*
* @param {String} url
* @param {String} dest
* @param {function} cb
* @param {Function} cb
* @api private
*/
function download(url, dest, cb) {
var file = fs.createWriteStream(dest);
var options = { proxy: getProxy(), rejectUnauthorized: false };
applyProxy({ rejectUnauthorized: false }, function(options) {
var returnError = function(err) {
fs.unlink(dest);
cb(typeof err.message === 'string' ? err.message : err);
......@@ -34,45 +35,38 @@ function download(url, dest, cb) {
req.end();
req.on('error', returnError);
});
}
/**
* Get proxy settings
* Get applyProxy settings
*
* @param {Object} options
* @param {Function} cb
* @api private
*/
function getProxy() {
var result = ['https-proxy', 'proxy', 'http-proxy'].filter(function(config) {
var proxy = exec('npm config get ' + config, {silent: true});
function applyProxy(options, cb) {
require('npmconf').load({}, function (er, conf) {
var getProxyFromEnv = true;
['https-proxy', 'proxy', 'http-proxy'].forEach(function(setting) {
var proxyUrl = conf.get(setting);
return proxy.code === 0 && validateProxyUrl(proxy.output.trim());
})[0];
if (result) {
return result;
if(proxyUrl && proxyUrl === require('url').parse(proxyUrl)) {
options.proxy = proxyUrl;
getProxyFromEnv = false;
cb(options);
return;
}
});
if(getProxyFromEnv) {
var env = process.env;
return env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy;
}
/**
* Validates Proxy URL
*
* @param {String} url
* @api private
*/
options.proxy = env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy;
function validateProxyUrl(url) {
if (/\n/.test(url)) {
url = url.replace(/\r?\n+/, '\n').split('\n');
url = url[url.length - 3].trim(); // get the second last element.
cb(options);
}
return url !== 'null' &&
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