Skip to content

Commit

Permalink
fix: parse proxy error (#216)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Lee <[email protected]>
  • Loading branch information
BlackHole1 and malept authored Mar 14, 2022
1 parent 4d303d3 commit c99de4b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as debug from 'debug';
import { getEnv } from './utils';
import { getEnv, setEnv } from './utils';

const d = debug('@electron/get:proxy');

Expand All @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,19 @@ 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()] ||
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;
}
}
23 changes: 23 additions & 0 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ensureIsTruthyString,
isOfficialLinuxIA32Download,
getEnv,
setEnv,
} from '../src/utils';

describe('utils', () => {
Expand Down Expand Up @@ -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');
});
});

0 comments on commit c99de4b

Please sign in to comment.