Don't try to setup a proxy agent when there's no proxy configured

Change-type: patch
This commit is contained in:
Pagan Gazzard 2020-03-02 11:27:00 +00:00
parent 4e61c00255
commit c4a6086e9c

View File

@ -87,6 +87,8 @@ export interface GlobalTunnelNgConfig {
sockets?: number;
}
type ProxyConfig = string | GlobalTunnelNgConfig;
/**
* Global proxy setup. Originally, `global-tunnel-ng` was used, but it only
* supports Node.js versions older than 10.16.0. For v10.16.0 and later,
@ -110,12 +112,28 @@ export interface GlobalTunnelNgConfig {
* default exclusion patterns are added for all private IPv4 address ranges.
*/
async function setupGlobalHttpProxy(settings: CliSettings) {
const semver = await import('semver');
if (semver.lt(process.version, '10.16.0')) {
setupGlobalTunnelNgProxy(settings);
} else {
// use global-agent instead of global-tunnel-ng
await setupGlobalAgentProxy(settings);
// `global-tunnel-ng` accepts lowercase variables with higher precedence
// than uppercase variables, but `global-agent` does not accept lowercase.
// Set uppercase versions for backwards compatibility.
const { env } = process;
if (env.http_proxy) {
env.HTTP_PROXY = env.http_proxy;
}
if (env.https_proxy) {
env.HTTPS_PROXY = env.https_proxy;
}
delete env.http_proxy;
delete env.https_proxy;
const proxy = settings.getCatch<ProxyConfig>('proxy');
if (proxy || env.HTTPS_PROXY || env.HTTP_PROXY) {
const semver = await import('semver');
if (semver.lt(process.version, '10.16.0')) {
setupGlobalTunnelNgProxy(proxy);
} else {
// use global-agent instead of global-tunnel-ng
await setupGlobalAgentProxy(settings, proxy);
}
}
}
@ -123,8 +141,7 @@ async function setupGlobalHttpProxy(settings: CliSettings) {
* `global-tunnel-ng` proxy setup.
* See docs for setupGlobalHttpProxy() above.
*/
function setupGlobalTunnelNgProxy(settings: CliSettings) {
const proxy = settings.getCatch<string | GlobalTunnelNgConfig>('proxy');
function setupGlobalTunnelNgProxy(proxy?: ProxyConfig) {
const globalTunnel = require('global-tunnel-ng');
// Init the tunnel even if BALENARC_PROXY is not defined, because
// other env vars may be defined. If no proxy configuration exists,
@ -138,8 +155,10 @@ function setupGlobalTunnelNgProxy(settings: CliSettings) {
* See docs for setupGlobalHttpProxy() above, and also the README file
* (Proxy Support section).
*/
async function setupGlobalAgentProxy(settings: CliSettings) {
const proxy = settings.getCatch<string | GlobalTunnelNgConfig>('proxy');
async function setupGlobalAgentProxy(
settings: CliSettings,
proxy?: ProxyConfig,
) {
const noProxy = settings.getCatch<string>('noProxy');
// Always exclude localhost, even if NO_PROXY is set
const requiredNoProxy = ['localhost', '127.0.0.1'];
@ -158,23 +177,11 @@ async function setupGlobalAgentProxy(settings: CliSettings) {
typeof proxy === 'string' ? proxy : makeUrlFromTunnelNgConfig(proxy);
env.HTTPS_PROXY = env.HTTP_PROXY = proxyUrl;
delete env.http_proxy;
delete env.https_proxy;
env.NO_PROXY = [
...requiredNoProxy,
...(noProxy ? _.filter((noProxy || '').split(',')) : privateNoProxy),
].join(',');
} else {
// `global-tunnel-ng` accepts lowercase variables with higher precedence
// than uppercase variables, but `global-agent` does not accept lowercase.
// Set uppercase versions for backwards compatibility.
if (env.http_proxy) {
env.HTTP_PROXY = env.http_proxy;
}
if (env.https_proxy) {
env.HTTPS_PROXY = env.https_proxy;
}
}
const { bootstrap } = require('global-agent');