Skip to content

Commit

Permalink
feat(proxy): support http(s)_proxy sans prefix on nodejs@>=10 (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackHole1 authored Mar 11, 2022
1 parent 8cdccc0 commit 4d303d3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as debug from 'debug';
import { getEnv } from './utils';

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

Expand All @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
};
}
37 changes: 37 additions & 0 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getHostArch,
ensureIsTruthyString,
isOfficialLinuxIA32Download,
getEnv,
} from '../src/utils';

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

0 comments on commit 4d303d3

Please sign in to comment.