From c99de4bd1f8c21e4da98a4b8ac37012530e99c50 Mon Sep 17 00:00:00 2001 From: Black-Hole <158blackhole@gmail.com> Date: Tue, 15 Mar 2022 05:51:28 +0800 Subject: [PATCH] fix: parse proxy error (#216) Co-authored-by: Mark Lee --- src/proxy.ts | 9 +++++---- src/utils.ts | 10 +++++++++- test/utils.spec.ts | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/proxy.ts b/src/proxy.ts index 368341ce2..a2b6037d1 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -1,5 +1,5 @@ import * as debug from 'debug'; -import { getEnv } from './utils'; +import { getEnv, setEnv } from './utils'; const d = debug('@electron/get:proxy'); @@ -14,9 +14,10 @@ export function initializeProxy(): void { 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'); + + setEnv('GLOBAL_AGENT_HTTP_PROXY', env('HTTP_PROXY')); + setEnv('GLOBAL_AGENT_HTTPS_PROXY', env('HTTPS_PROXY')); + setEnv('GLOBAL_AGENT_NO_PROXY', env('NO_PROXY')); // `global-agent` works with Node.js v10 and above. require('global-agent').bootstrap(); diff --git a/src/utils.ts b/src/utils.ts index 1e97077c7..fbb4684ae 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -104,7 +104,7 @@ export function getEnv(prefix = ''): (name: string) => string | undefined { envsLowerCase[envKey.toLowerCase()] = process.env[envKey]; } - return (name: string) => { + return (name: string): string | undefined => { return ( envsLowerCase[`${prefix}${name}`.toLowerCase()] || envsLowerCase[name.toLowerCase()] || @@ -112,3 +112,11 @@ export function getEnv(prefix = ''): (name: string) => string | undefined { ); }; } + +export function setEnv(key: string, value: string | undefined): void { + // The `void` operator always returns `undefined`. + // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void + if (value !== void 0) { + process.env[key] = value; + } +} diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 6ce8e8617..462b0fe25 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -8,6 +8,7 @@ import { ensureIsTruthyString, isOfficialLinuxIA32Download, getEnv, + setEnv, } from '../src/utils'; describe('utils', () => { @@ -193,3 +194,25 @@ describe('utils', () => { }); }); }); + +describe('setEnv()', () => { + it("doesn't set the environment variable if the value is undefined", () => { + const [key, value] = ['Set_AAA_electron', undefined]; + setEnv(key, value); + expect(process.env[key]).toEqual(void 0); + }); + + it('successfully sets the environment variable when the value is defined', () => { + const [key, value] = ['Set_BBB_electron', 'Test']; + setEnv(key, value); + expect(process.env[key]).toEqual(value); + }); + + it('successfully sets the environment variable when the value is falsey', () => { + const [key, value] = ['Set_AAA_electron', false]; + // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // @ts-ignore + setEnv(key, value); + expect(process.env[key]).toEqual('false'); + }); +});