From 265fdae201394ae1ab6ff138ad9b562b6a0063db Mon Sep 17 00:00:00 2001 From: xzyfer Date: Wed, 27 Apr 2016 23:16:09 +1000 Subject: [PATCH] Replace deprecated npmconf package. This was previously attempted in #1413. Shortly after it's release proxy users started experiencing installation issues so this was reverted. It was later determined that #1458 was likely at fault for the proxy issues. Full credit for this patch goes to @delitescere. I've also taken the liberty of cleaning the request config generation. Fixes #1333 --- package.json | 1 - scripts/install.js | 95 +++++++++++++++++++++++----------------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index b2e335e43..268eeeb5c 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,6 @@ "mkdirp": "^0.5.1", "nan": "^2.2.0", "node-gyp": "^3.3.1", - "npmconf": "^2.1.2", "request": "^2.61.0", "sass-graph": "^2.1.1" }, diff --git a/scripts/install.js b/scripts/install.js index 78aeabc9d..1ff5defed 100644 --- a/scripts/install.js +++ b/scripts/install.js @@ -5,7 +5,6 @@ var fs = require('fs'), eol = require('os').EOL, mkdir = require('mkdirp'), - npmconf = require('npmconf'), path = require('path'), sass = require('../lib/extensions'), request = require('request'), @@ -30,65 +29,67 @@ function download(url, dest, cb) { 'or configure npm proxy via', eol, eol, ' npm config set proxy http://example.com:8080'].join('')); }; + var successful = function(response) { return response.statusCode >= 200 && response.statusCode < 300; }; - applyProxy({ rejectUnauthorized: false }, function(options) { - options.headers = { - 'User-Agent': [ - 'node/', process.version, ' ', - 'node-sass-installer/', pkg.version - ].join('') - }; - try { - request(url, options, function(err, response) { - if (err) { - reportError(err); - } else if (!successful(response)) { - reportError(['HTTP error', response.statusCode, response.statusMessage].join(' ')); - } else { - cb(); - } - }).on('response', function(response) { - if (successful(response)) { - response.pipe(fs.createWriteStream(dest)); - } - }); - } catch (err) { - cb(err); + var options = { + rejectUnauthorized: false, + proxy: getProxy(), + headers: { + 'User-Agent': getUserAgent(), } - }); + }; + + try { + request(url, options, function(err, response) { + if (err) { + reportError(err); + } else if (!successful(response)) { + reportError(['HTTP error', response.statusCode, response.statusMessage].join(' ')); + } else { + cb(); + } + }) + .on('response', function(response) { + if (successful(response)) { + response.pipe(fs.createWriteStream(dest)); + } + }); + } catch (err) { + cb(err); + } +} + +/** + * A custom user agent use for binary downloads. + * + * @api private + */ +function getUserAgent() { + return [ + 'node/', process.version, ' ', + 'node-sass-installer/', pkg.version + ].join(''); } /** - * Get applyProxy settings + * Determine local proxy settings * * @param {Object} options * @param {Function} cb * @api private */ -function applyProxy(options, cb) { - npmconf.load({}, function (er, conf) { - var proxyUrl; - - if (!er) { - proxyUrl = conf.get('https-proxy') || - conf.get('proxy') || - conf.get('http-proxy'); - } - - var env = process.env; - - options.proxy = proxyUrl || - env.HTTPS_PROXY || - env.https_proxy || - env.HTTP_PROXY || - env.http_proxy; - - cb(options); - }); +function getProxy() { + return process.env.npm_config_https_proxy || + process.env.npm_config_proxy || + process.env.npm_config_http_proxy || + process.env.HTTPS_PROXY || + process.env.https_proxy || + process.env.HTTP_PROXY || + process.env.http_proxy; } /** @@ -129,7 +130,7 @@ if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) { } /** - * If binary does not exsit, download it + * If binary does not exist, download it */ checkAndDownloadBinary();