Skip to content

Commit 77e704f

Browse files
committed
Cache npm proxy
1 parent 99959ee commit 77e704f

File tree

1 file changed

+11
-4
lines changed
  • packages/cubejs-backend-shared/src

1 file changed

+11
-4
lines changed

packages/cubejs-backend-shared/src/proxy.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { exec } from 'child_process';
22
import { ProxyAgent } from 'proxy-agent';
33
import { HttpsProxyAgent } from 'https-proxy-agent';
44

5+
let npmProxy: string;
6+
let npmProxyInitialized = false;
7+
58
function getCommandOutput(command: string) {
69
return new Promise<string>((resolve, reject) => {
710
exec(command, (error, stdout) => {
@@ -19,22 +22,26 @@ function getCommandOutput(command: string) {
1922
* @deprecated
2023
* use ProxyAgent instead
2124
*/
22-
export async function getProxySettings() {
25+
export async function getProxySettings(): Promise<string> {
2326
const [proxy] = (
2427
await Promise.all([getCommandOutput('npm config -g get https-proxy'), getCommandOutput('npm config -g get proxy')])
2528
)
2629
.map((s) => s.trim())
2730
.filter((s) => !['null', 'undefined', ''].includes(s));
2831

32+
npmProxyInitialized = true;
33+
2934
return proxy;
3035
}
3136

3237
export async function getHttpAgentForProxySettings() {
33-
const proxy = await getProxySettings();
38+
if (!npmProxyInitialized) {
39+
npmProxy = await getProxySettings();
40+
}
3441

35-
if (proxy) {
42+
if (npmProxy) {
3643
console.warn('Npm proxy settings are deprecated. Please use HTTP_PROXY, HTTPS_PROXY environment variables instead.');
37-
return new HttpsProxyAgent(proxy);
44+
return new HttpsProxyAgent(npmProxy);
3845
}
3946

4047
return new ProxyAgent();

0 commit comments

Comments
 (0)