Replace caw with its tunnel-agent dependency

This commit is contained in:
Lovell Fuller
2017-11-07 19:38:10 +00:00
parent 965a97105e
commit ba521fccb4
4 changed files with 75 additions and 8 deletions

37
lib/agent.js Normal file
View File

@@ -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;
}
};