diff --git a/lib/proxy.spec.ts b/lib/proxy.spec.ts index d2a8e832b25716a83172c08808cf374b52137e07..c999ed909f9d3d7d3836ada0a3e98e92a96ea19e 100644 --- a/lib/proxy.spec.ts +++ b/lib/proxy.spec.ts @@ -1,11 +1,11 @@ -import { bootstrap } from './proxy'; +import { bootstrap, hasProxy } from './proxy'; describe('proxy', () => { const httpProxy = 'http://example.org/http-proxy'; const httpsProxy = 'http://example.org/https-proxy'; const noProxy = 'http://example.org/no-proxy'; - beforeAll(() => { + beforeEach(() => { delete process.env.HTTP_PROXY; delete process.env.HTTPS_PROXY; delete process.env.NO_PROXY; @@ -14,17 +14,17 @@ describe('proxy', () => { it('respects HTTP_PROXY', () => { process.env.HTTP_PROXY = httpProxy; - const result = bootstrap(); - expect(result.HTTP_PROXY).toEqual(httpProxy); + bootstrap(); + expect(hasProxy()).toBeTrue(); }); it('respects HTTPS_PROXY', () => { process.env.HTTPS_PROXY = httpsProxy; - const result = bootstrap(); - expect(result.HTTPS_PROXY).toEqual(httpsProxy); + bootstrap(); + expect(hasProxy()).toBeTrue(); }); - it('respects no_proxy', () => { + it('does nothing', () => { process.env.no_proxy = noProxy; - const result = bootstrap(); - expect(result.NO_PROXY).toEqual(noProxy); + bootstrap(); + expect(hasProxy()).toBeFalse(); }); }); diff --git a/lib/proxy.ts b/lib/proxy.ts index 453206b5d1dd91a141f096ce3c9f55b979f0aa66..a1f7ca6d61a8a835877b97e0229509de0eaccf99 100644 --- a/lib/proxy.ts +++ b/lib/proxy.ts @@ -1,11 +1,11 @@ -import { - ProxyAgentConfigurationType, - createGlobalProxyAgent, -} from 'global-agent'; +import is from '@sindresorhus/is'; +import { createGlobalProxyAgent } from 'global-agent'; const envVars = ['HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY']; -export function bootstrap(): ProxyAgentConfigurationType { +let agent = false; + +export function bootstrap(): void { envVars.forEach((envVar) => { /* istanbul ignore if: env is case-insensitive on windows */ if ( @@ -15,7 +15,22 @@ export function bootstrap(): ProxyAgentConfigurationType { process.env[envVar] = process.env[envVar.toLowerCase()]; } }); - return createGlobalProxyAgent({ - environmentVariableNamespace: '', - }); + + if ( + is.nonEmptyString(process.env.HTTP_PROXY) || + is.nonEmptyString(process.env.HTTPS_PROXY) + ) { + createGlobalProxyAgent({ + environmentVariableNamespace: '', + }); + agent = true; + } else { + // for testing only, does not reset global agent + agent = false; + } +} + +// will be used by our http layer later +export function hasProxy(): boolean { + return agent === true; }