diff --git a/binding.js b/binding.js index f600dec5..26cb8526 100644 --- a/binding.js +++ b/binding.js @@ -4,12 +4,12 @@ const fs = require('fs'); const os = require('os'); const path = require('path'); -const caw = require('caw'); const simpleGet = require('simple-get'); const semver = require('semver'); const tar = require('tar'); const detectLibc = require('detect-libc'); +const agent = require('./lib/agent'); const platform = require('./lib/platform'); // Use NPM-provided environment variable where available, falling back to require-based method for Electron @@ -81,9 +81,7 @@ module.exports.download_vips = function () { const url = distBaseUrl + tarFilename; const simpleGetOpt = { url: url, - agent: caw(null, { - protocol: 'https' - }) + agent: agent() }; simpleGet(simpleGetOpt, function (err, response) { if (err) { diff --git a/lib/agent.js b/lib/agent.js new file mode 100644 index 00000000..cae55e1b --- /dev/null +++ b/lib/agent.js @@ -0,0 +1,37 @@ +'use strict'; + +const url = require('url'); +const tunnelAgent = require('tunnel-agent'); + +const is = require('./is'); + +const proxies = [ + 'HTTPS_PROXY', + 'https_proxy', + 'HTTP_PROXY', + 'http_proxy', + 'npm_config_https_proxy', + 'npm_config_proxy' +]; + +function env (key) { + return process.env[key]; +} + +module.exports = function () { + try { + const proxy = url.parse(proxies.map(env).find(is.string)); + const tunnel = proxy.protocol === 'https:' + ? tunnelAgent.httpsOverHttps + : tunnelAgent.httpsOverHttp; + return tunnel({ + proxy: { + port: Number(proxy.port), + host: proxy.hostname, + proxyAuth: proxy.auth + } + }); + } catch (err) { + return null; + } +}; diff --git a/package.json b/package.json index 1928b484..96bdb3de 100644 --- a/package.json +++ b/package.json @@ -69,13 +69,13 @@ "vips" ], "dependencies": { - "caw": "^2.0.0", "color": "^2.0.0", - "detect-libc": "^0.2.0", + "detect-libc": "^1.0.2", "nan": "^2.7.0", "semver": "^5.4.1", "simple-get": "^2.7.0", - "tar": "^4.0.1" + "tar": "^4.0.1", + "tunnel-agent": "^0.6.0" }, "devDependencies": { "async": "^2.5.0", @@ -84,7 +84,7 @@ "exif-reader": "^1.0.2", "icc": "^1.0.0", "mocha": "^4.0.1", - "nyc": "^11.1.0", + "nyc": "^11.3.0", "rimraf": "^2.6.1", "semistandard": "^11.0.0", "unzip": "^0.1.11" diff --git a/test/unit/agent.js b/test/unit/agent.js new file mode 100644 index 00000000..a49e0ed6 --- /dev/null +++ b/test/unit/agent.js @@ -0,0 +1,32 @@ +'use strict'; + +const assert = require('assert'); +const agent = require('../../lib/agent'); + +describe('HTTP agent', function () { + it('Without proxy', function () { + assert.strictEqual(null, agent()); + }); + + it('HTTPS proxy with auth from HTTPS_PROXY', function () { + process.env.HTTPS_PROXY = 'https://user:pass@secure:123'; + const proxy = agent(); + delete process.env.HTTPS_PROXY; + assert.strictEqual('object', typeof proxy); + assert.strictEqual('secure', proxy.options.proxy.host); + assert.strictEqual(123, proxy.options.proxy.port); + assert.strictEqual('user:pass', proxy.options.proxy.proxyAuth); + assert.strictEqual(443, proxy.defaultPort); + }); + + it('HTTP proxy without auth from npm_config_proxy', function () { + process.env.npm_config_proxy = 'http://plaintext:456'; + const proxy = agent(); + delete process.env.npm_config_proxy; + assert.strictEqual('object', typeof proxy); + assert.strictEqual('plaintext', proxy.options.proxy.host); + assert.strictEqual(456, proxy.options.proxy.port); + assert.strictEqual(null, proxy.options.proxy.proxyAuth); + assert.strictEqual(443, proxy.defaultPort); + }); +});