Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ disable, set the `ELECTRON_GET_NO_PROGRESS` environment variable to any non-empt

### Proxies

Downstream packages should utilize the `initializeProxy` function to add HTTP(S) proxy support. If
the environment variable `ELECTRON_GET_USE_PROXY` is set, it is called automatically.
Proxy support is built-in via [`proxy-agent`](https://github.com/TooTallNate/proxy-agents). The standard
`HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` environment variables are automatically detected
for all downloads. No additional configuration is required.

### Debug

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"got": "^14.4.5",
"graceful-fs": "^4.2.11",
"progress": "^2.0.3",
"proxy-agent": "^6.5.0",
"semver": "^7.6.3",
"sumchecker": "^3.0.1"
},
Expand Down Expand Up @@ -78,8 +79,5 @@
"artifact",
"release"
],
"optionalDependencies": {
"global-agent": "^3.0.0"
},
"packageManager": "yarn@4.10.3+sha512.c38cafb5c7bb273f3926d04e55e1d8c9dfa7d9c3ea1f36a4868fa028b9e5f72298f0b7f401ad5eb921749eb012eb1c3bb74bf7503df3ee43fd600d14a018266f"
}
16 changes: 15 additions & 1 deletion src/GotDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ import fs from 'graceful-fs';

import path from 'node:path';
import ProgressBar from 'progress';
import { ProxyAgent } from 'proxy-agent';

import { Downloader } from './Downloader.js';
import { pipeline } from 'node:stream/promises';

const PROGRESS_BAR_DELAY_IN_SECONDS = 30;

const proxyAgent = new ProxyAgent();

/**
* Default `got` options with proxy support.
* Proxy is automatically detected from `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` environment variables.
*/
const defaultGotOptions = {
agent: {
http: proxyAgent,
https: proxyAgent,
},
};

/**
* Options for the default [`got`](https://github.com/sindresorhus/got) Downloader implementation.
*
Expand Down Expand Up @@ -66,7 +80,7 @@ export class GotDownloader implements Downloader<GotDownloaderOptions> {
}
}, PROGRESS_BAR_DELAY_IN_SECONDS * 1000);
}
const downloadStream = got.stream(url, gotOptions);
const downloadStream = got.stream(url, { ...defaultGotOptions, ...gotOptions });
downloadStream.on('downloadProgress', async (progress: Progress) => {
progressPercent = progress.percent;
if (bar) {
Expand Down
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from './types.js';
import { Cache } from './Cache.js';
import { getDownloaderForSystem } from './downloader-resolver.js';
import { initializeProxy } from './proxy.js';
import {
withTempDirectoryIn,
getHostArch,
Expand All @@ -31,14 +30,17 @@ import {
} from './utils.js';

export { getHostArch } from './utils.js';
export { initializeProxy } from './proxy.js';
export * from './types.js';

const d = debug('@electron/get:index');
/**
* @deprecated Proxy support is now built-in and enabled automatically.
* This function is a no-op and will be removed in a future major release.
* Standard `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` environment variables
* are detected automatically for all downloads.
*/
export function initializeProxy(): void {}

if (process.env.ELECTRON_GET_USE_PROXY) {
initializeProxy();
}
const d = debug('@electron/get:index');

type ArtifactDownloader = (
_artifactDetails: ElectronPlatformArtifactDetailsWithDefaults | ElectronGenericArtifactDetails,
Expand Down
40 changes: 0 additions & 40 deletions src/proxy.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,6 @@ 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): 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;
}
}

export function effectiveCacheMode(
artifactDetails: ElectronPlatformArtifactDetailsWithDefaults | ElectronGenericArtifactDetails,
): ElectronDownloadCacheMode {
Expand Down
61 changes: 1 addition & 60 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'graceful-fs';

import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
import { describe, expect, it, vi } from 'vitest';

import {
normalizeVersion,
Expand All @@ -9,8 +9,6 @@ import {
getHostArch,
ensureIsTruthyString,
isOfficialLinuxIA32Download,
getEnv,
setEnv,
TempDirCleanUpMode,
} from '../src/utils';

Expand Down Expand Up @@ -154,61 +152,4 @@ 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);
});
});
});

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];
// @ts-expect-error - we want to ensure that the boolean gets converted to string accordingly
setEnv(key, value);
expect(process.env[key]).toEqual('false');
});
});
Loading
Loading