From 4d303d301a4c532b3341c2631117ecdd8a93acb2 Mon Sep 17 00:00:00 2001 From: Black-Hole <158blackhole@gmail.com> Date: Sat, 12 Mar 2022 07:22:13 +0800 Subject: [PATCH] feat(proxy): support http(s)_proxy sans prefix on nodejs@>=10 (#214) --- src/proxy.ts | 7 +++++++ src/utils.ts | 20 ++++++++++++++++++++ test/utils.spec.ts | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/src/proxy.ts b/src/proxy.ts index b9dc1f8fc..368341ce2 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -1,4 +1,5 @@ import * as debug from 'debug'; +import { getEnv } from './utils'; const d = debug('@electron/get:proxy'); @@ -11,6 +12,12 @@ export function initializeProxy(): void { const MAJOR_NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10); if (MAJOR_NODEJS_VERSION >= 10) { + // See: https://github.com/electron/get/pull/214#discussion_r798845713 + const env = getEnv('GLOBAL_AGENT_'); + process.env.GLOBAL_AGENT_HTTP_PROXY = env('HTTP_PROXY'); + process.env.GLOBAL_AGENT_HTTPS_PROXY = env('HTTPS_PROXY'); + process.env.GLOBAL_AGENT_NO_PROXY = env('NO_PROXY'); + // `global-agent` works with Node.js v10 and above. require('global-agent').bootstrap(); } else { diff --git a/src/utils.ts b/src/utils.ts index 3b3445c84..1e97077c7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -92,3 +92,23 @@ export function isOfficialLinuxIA32Download( typeof mirrorOptions === 'undefined' ); } + +/** + * Find the value of a environment variable which may or may not have the + * prefix, in a case-insensitive manner. + */ +export function getEnv(prefix = ''): (name: string) => string | undefined { + const envsLowerCase: NodeJS.ProcessEnv = {}; + + for (const envKey in process.env) { + envsLowerCase[envKey.toLowerCase()] = process.env[envKey]; + } + + return (name: string) => { + return ( + envsLowerCase[`${prefix}${name}`.toLowerCase()] || + envsLowerCase[name.toLowerCase()] || + undefined + ); + }; +} diff --git a/test/utils.spec.ts b/test/utils.spec.ts index ee5ef5cdd..6ce8e8617 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -7,6 +7,7 @@ import { getHostArch, ensureIsTruthyString, isOfficialLinuxIA32Download, + getEnv, } from '../src/utils'; describe('utils', () => { @@ -155,4 +156,40 @@ describe('utils', () => { expect(isOfficialLinuxIA32Download('linux', 'x64', 'v4.0.0')).toEqual(false); }); }); + + describe('getEnv()', () => { + const [prefix, envName] = ['TeSt_EnV_vAr_', 'eNv_Key']; + const prefixEnvName = `${prefix}${envName}`; + const [hasPrefixValue, noPrefixValue] = ['yes_prefix', 'no_prefix']; + + beforeAll(() => { + process.env[prefixEnvName] = hasPrefixValue; + process.env[envName] = noPrefixValue; + }); + + afterAll(() => { + delete process.env[prefixEnvName]; + delete process.env[envName]; + }); + + it('should return prefixed environment variable if prefixed variable found', () => { + const env = getEnv(prefix); + expect(env(envName)).toEqual(hasPrefixValue); + expect(env(envName.toLowerCase())).toEqual(hasPrefixValue); + expect(env(envName.toUpperCase())).toEqual(hasPrefixValue); + }); + + it('should return non-prefixed environment variable if no prefixed variable found', () => { + expect(getEnv()(envName)).toEqual(noPrefixValue); + expect(getEnv()(envName.toLowerCase())).toEqual(noPrefixValue); + expect(getEnv()(envName.toUpperCase())).toEqual(noPrefixValue); + }); + + it('should return undefined if no match', () => { + const randomStr = 'AAAAA_electron_'; + expect(getEnv()(randomStr)).toEqual(undefined); + expect(getEnv()(randomStr.toLowerCase())).toEqual(undefined); + expect(getEnv()(randomStr.toUpperCase())).toEqual(undefined); + }); + }); });