From 418b28b28c79a45e8c7e827df77f8bcdce0cbf27 Mon Sep 17 00:00:00 2001 From: Michael Kriese <michael.kriese@visualon.de> Date: Mon, 25 Nov 2019 22:02:13 +0100 Subject: [PATCH] refactor(typescript): convert proxy to typescript (#4878) --- lib/{proxy.js => proxy.ts} | 12 ++++++------ lib/types/global-agent.d.ts | 17 +++++++++++++++++ test/proxy.spec.ts | 11 +++++++++-- 3 files changed, 32 insertions(+), 8 deletions(-) rename lib/{proxy.js => proxy.ts} (64%) create mode 100644 lib/types/global-agent.d.ts diff --git a/lib/proxy.js b/lib/proxy.ts similarity index 64% rename from lib/proxy.js rename to lib/proxy.ts index 531aa26dc2..fb2266379e 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 0000000000..0d10188b2c --- /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 7caa957d2d..a03fe75fea 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); }); -- GitLab