From 6539e7ba5007fdb2571621b9cfcfcd007599d4fc Mon Sep 17 00:00:00 2001 From: Adeel Date: Thu, 8 Jan 2015 03:22:47 +0200 Subject: [PATCH] Install: Improves code to get proxy. * Credit: @xzyfer. * Addresses #588. --- scripts/install.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/scripts/install.js b/scripts/install.js index a94d2e5b4..9f7db841d 100644 --- a/scripts/install.js +++ b/scripts/install.js @@ -43,17 +43,11 @@ function download(url, dest, cb) { */ function getProxy() { - var result; - - ['https-proxy', 'proxy', 'http-proxy'].map(function(config) { + var result = ['https-proxy', 'proxy', 'http-proxy'].filter(function(config) { var proxy = exec('npm config get ' + config, {silent: true}); - var output = proxy.output.trim(); - if (proxy.code === 0 && output !== 'undefined' && output !== 'null') { - result = proxy.output; - return; - } - }); + return proxy.code === 0 && validateProxyUrl(proxy.output.trim()); + })[0]; if (result) { return result; @@ -63,6 +57,24 @@ function getProxy() { return env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy; } +/** + * Validates Proxy URL + * + * @param {String} url + * @api private + */ + +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. + } + + return url !== 'null' && + url !== 'undefined' && + url === require('url').parse(url); +} + /** * Check if binaries exists *