Skip to content

Commit c43adac

Browse files
authored
fix: respect npm proxy settings (#1137)
1 parent b6c6c41 commit c43adac

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

packages/cubejs-server-core/dev/PackageFetcher.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const fs = require('fs-extra');
2-
const fetch = require('node-fetch').default;
32
const decompress = require('decompress');
43
const decompressTargz = require('decompress-targz');
54
const path = require('path');
65

7-
const { executeCommand } = require('./utils');
6+
const { executeCommand, proxyFetch } = require('./utils');
87

98
class PackageFetcher {
109
constructor(repo) {
@@ -26,7 +25,7 @@ class PackageFetcher {
2625
}
2726

2827
async manifestJSON() {
29-
const response = await fetch(
28+
const response = await proxyFetch(
3029
`https://api.github.com/repos/${this.repo.owner}/${this.repo.name}/contents/manifest.json`
3130
);
3231

@@ -37,7 +36,7 @@ class PackageFetcher {
3736
const url = `https://github.com/${this.repo.owner}/${this.repo.name}/archive/master.tar.gz`;
3837
const writer = fs.createWriteStream(this.repoArchivePath);
3938

40-
(await fetch(url)).body.pipe(writer);
39+
(await proxyFetch(url)).body.pipe(writer);
4140

4241
return new Promise((resolve, reject) => {
4342
writer.on('finish', resolve);

packages/cubejs-server-core/dev/utils.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const fs = require('fs-extra');
22
const path = require('path');
33
const spawn = require('cross-spawn');
4+
const fetch = require('node-fetch');
5+
const HttpsProxyAgent = require('http-proxy-agent');
6+
const { exec } = require('child_process');
47

58
async function fileContentsRecursive(dir, rootPath, includeNodeModules) {
69
if (!rootPath) {
@@ -54,7 +57,38 @@ function executeCommand(command, args, options = {}) {
5457
});
5558
}
5659

60+
function getCommandOutput(command) {
61+
return new Promise((resolve, reject) => {
62+
exec(command, (error, stdout) => {
63+
if (error) {
64+
reject(error.message);
65+
return;
66+
}
67+
resolve(stdout);
68+
});
69+
});
70+
}
71+
72+
async function proxyFetch(url) {
73+
const [proxy] = (await Promise.all([
74+
getCommandOutput('npm config get https-proxy'),
75+
getCommandOutput('npm config get proxy'),
76+
]))
77+
.map((s) => s.trim())
78+
.filter((s) => !['null', 'undefined', ''].includes(s));
79+
80+
return fetch(
81+
url,
82+
proxy
83+
? {
84+
agent: new HttpsProxyAgent(proxy),
85+
}
86+
: {}
87+
);
88+
}
89+
5790
module.exports = {
5891
fileContentsRecursive,
5992
executeCommand,
93+
proxyFetch
6094
};

packages/cubejs-server-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"decompress": "^4.2.1",
3535
"decompress-targz": "^4.1.1",
3636
"fs-extra": "^8.1.0",
37+
"http-proxy-agent": "^4.0.1",
3738
"jsonwebtoken": "^8.4.0",
3839
"lru-cache": "^5.1.1",
3940
"node-fetch": "^2.6.0",

packages/cubejs-server-core/yarn.lock

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,11 @@
10641064
"@types/istanbul-reports" "^1.1.1"
10651065
"@types/yargs" "^13.0.0"
10661066

1067+
"@tootallnate/once@1":
1068+
version "1.1.2"
1069+
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
1070+
integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==
1071+
10671072
"@types/babel__core@^7.1.0":
10681073
version "7.1.3"
10691074
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.3.tgz#e441ea7df63cd080dfcd02ab199e6d16a735fc30"
@@ -1207,6 +1212,13 @@ acorn@^7.1.0:
12071212
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.1.1.tgz#e35668de0b402f359de515c5482a1ab9f89a69bf"
12081213
integrity sha512-add7dgA5ppRPxCFJoAGfMDi7PIBXq1RtGo7BhbLaxwrXPOmw8gq48Y9ozT01hUKy9byMjlR20EJhu5zlkErEkg==
12091214

1215+
agent-base@6:
1216+
version "6.0.1"
1217+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.1.tgz#808007e4e5867decb0ab6ab2f928fbdb5a596db4"
1218+
integrity sha512-01q25QQDwLSsyfhrKbn8yuur+JNw0H+0Y4JiGIKd3z9aYk/w/2kxD/Upc+t2ZBBSUNff50VjPsSW2YxM8QYKVg==
1219+
dependencies:
1220+
debug "4"
1221+
12101222
ajv@^6.10.0, ajv@^6.10.2, ajv@^6.5.5:
12111223
version "6.12.0"
12121224
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.0.tgz#06d60b96d87b8454a5adaba86e7854da629db4b7"
@@ -1968,6 +1980,13 @@ debug@3.2.6, debug@^3.2.6:
19681980
dependencies:
19691981
ms "^2.1.1"
19701982

1983+
debug@4:
1984+
version "4.2.0"
1985+
resolved "https://registry.yarnpkg.com/debug/-/debug-4.2.0.tgz#7f150f93920e94c58f5574c2fd01a3110effe7f1"
1986+
integrity sha512-IX2ncY78vDTjZMFUdmsvIRFY2Cf4FnD0wRs+nQwJU8Lu99/tPFdb0VybiiMTPe3I6rQmwsqQqRBvxU+bZ/I8sg==
1987+
dependencies:
1988+
ms "2.1.2"
1989+
19711990
debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
19721991
version "4.1.1"
19731992
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
@@ -3075,6 +3094,15 @@ http-errors@~1.6.2:
30753094
setprototypeof "1.1.0"
30763095
statuses ">= 1.4.0 < 2"
30773096

3097+
http-proxy-agent@^4.0.1:
3098+
version "4.0.1"
3099+
resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-4.0.1.tgz#8a8c8ef7f5932ccf953c296ca8291b95aa74aa3a"
3100+
integrity sha512-k0zdNgqWTGA6aeIRVpvfVob4fL52dTfaehylg0Y4UvSySvOq/Y+BOyPrgpUrA7HylqvU8vIZGsRuXmspskV0Tg==
3101+
dependencies:
3102+
"@tootallnate/once" "1"
3103+
agent-base "6"
3104+
debug "4"
3105+
30783106
http-signature@~1.2.0:
30793107
version "1.2.0"
30803108
resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.2.0.tgz#9aecd925114772f3d95b65a60abb8f7c18fbace1"

0 commit comments

Comments
 (0)