diff --git a/lib/proxy.js b/lib/proxy.ts similarity index 64% rename from lib/proxy.js rename to lib/proxy.ts index 531aa26dc202252ebfa845e37f547312c4323a62..fb2266379eec755faf78800e962e928750835fbe 100644 --- a/lib/proxy.js +++ b/lib/proxy.ts @@ -1,13 +1,13 @@ -const { createGlobalProxyAgent } = require('global-agent'); - -module.exports = { - bootstrap, -}; +import { + createGlobalProxyAgent, + ProxyAgentConfigurationType, +} from 'global-agent'; const envVars = ['HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY']; -function bootstrap() { +export function bootstrap(): ProxyAgentConfigurationType { envVars.forEach(envVar => { + /* istanbul ignore if: env is case-insensitive on windows */ if ( typeof process.env[envVar] === 'undefined' && typeof process.env[envVar.toLowerCase()] !== 'undefined' diff --git a/lib/types/global-agent.d.ts b/lib/types/global-agent.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..0d10188b2c9b717f1980cd270320c04757bbf583 --- /dev/null +++ b/lib/types/global-agent.d.ts @@ -0,0 +1,17 @@ +declare module 'global-agent' { + export interface ProxyAgentConfigurationInputType { + environmentVariableNamespace?: string; + forceGlobalAgent?: boolean; + socketConnectionTimeout?: number; + } + + export interface ProxyAgentConfigurationType { + readonly HTTP_PROXY: string; + readonly HTTPS_PROXY: string; + readonly NO_PROXY: string; + } + + export function createGlobalProxyAgent( + opts: ProxyAgentConfigurationInputType + ): ProxyAgentConfigurationType; +} diff --git a/test/proxy.spec.ts b/test/proxy.spec.ts index 7caa957d2d93059ef97e037009962f664f07dc76..a03fe75fea7d72775a69cdff41c1cce0e143a32b 100644 --- a/test/proxy.spec.ts +++ b/test/proxy.spec.ts @@ -5,6 +5,13 @@ describe('proxy', () => { const httpsProxy = 'http://example.org/https-proxy'; const noProxy = 'http://example.org/no-proxy'; + beforeAll(() => { + delete process.env.HTTP_PROXY; + delete process.env.HTTPS_PROXY; + delete process.env.NO_PROXY; + delete process.env.no_proxy; + }); + it('respects HTTP_PROXY', () => { process.env.HTTP_PROXY = httpProxy; const result = bootstrap(); @@ -15,8 +22,8 @@ describe('proxy', () => { const result = bootstrap(); expect(result.HTTPS_PROXY).toEqual(httpsProxy); }); - it('respects NO_PROXY', () => { - process.env.NO_PROXY = noProxy; + it('respects no_proxy', () => { + process.env.no_proxy = noProxy; const result = bootstrap(); expect(result.NO_PROXY).toEqual(noProxy); });