Skip to content

Commit 5e3da0c

Browse files
committed
[FIX] ui5Framework: Respect npm proxy configuration to fetch libraries
This commit fixes a regression caused via #579 The proxy functionality relies on the agent option so it must not be disabled when using a proxy. This again enables proxy support but doesn't solve potential "Socket Timeout" issues that might also appear when using a proxy (see #579). The https-proxy option is now also taken into account, which didn't work before due to missing normalization from https-proxy to httpsProxy. Fixes: SAP/ui5-tooling#822
1 parent 1f07fbe commit 5e3da0c

File tree

2 files changed

+101
-9
lines changed

2 files changed

+101
-9
lines changed

lib/ui5Framework/npm/Registry.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,35 @@ class Registry {
5252
const {default: libnpmconfig} = await import("libnpmconfig");
5353
const opts = {
5454
cache: this._cacheDir,
55-
// Disable usage of shared keep-alive agents.
56-
// make-fetch-happen uses a hard-coded 15 seconds freeSocketTimeout
57-
// that can be easily reached (Error: Socket timeout) and there doesn't
58-
// seem to be another way to disable or increase it.
59-
// Also see: https://github.com/node-modules/agentkeepalive/issues/106
60-
agent: false,
6155
};
6256
const config = libnpmconfig.read(opts, {
6357
cwd: this._cwd
6458
}).toJSON();
6559

60+
// Rename https-proxy to httpsProxy so that it is picked up by npm-registry-fetch (via pacote)
61+
if (config["https-proxy"]) {
62+
config.httpsProxy = config["https-proxy"];
63+
delete config["https-proxy"];
64+
}
65+
66+
if (!config.proxy && !config.httpsProxy) {
67+
// Disable usage of shared keep-alive agents unless a proxy is configured
68+
// which only works with agents.
69+
70+
// make-fetch-happen uses a hard-coded 15 seconds freeSocketTimeout
71+
// that can be easily reached (Error: Socket timeout) and there doesn't
72+
// seem to be another way to disable or increase it.
73+
// Also see: https://github.com/node-modules/agentkeepalive/issues/106
74+
config.agent = false;
75+
}
76+
6677
log.verbose(`Using npm configuration (extract):`);
6778
// Do not log full configuration as it may contain authentication tokens
6879
logConfig(config, "registry");
6980
logConfig(config, "@sapui5:registry");
7081
logConfig(config, "@openui5:registry");
7182
logConfig(config, "proxy");
83+
logConfig(config, "httpsProxy");
7284
logConfig(config, "globalconfig");
7385
logConfig(config, "userconfig");
7486
logConfig(config, "cache");

test/lib/ui5framework/npm/Registry.js

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,87 @@ test.serial("_getPacoteOptions", async (t) => {
5454
"fake": "config"
5555
};
5656

57+
const expectedPacoteOptions = {
58+
fake: "config",
59+
agent: false
60+
};
61+
62+
libnpmconfigReadToJSON.returns(npmConfig);
63+
64+
const pacoteOptions = await registry._getPacoteOptions();
65+
66+
t.is(libnpmconfigReadToJSON.callCount, 1);
67+
t.is(libnpmconfig.read.callCount, 1);
68+
t.deepEqual(libnpmconfig.read.getCall(0).args, [{
69+
cache: "cacheDir",
70+
}, {
71+
cwd: "cwd"
72+
}]);
73+
74+
t.deepEqual(pacoteOptions, expectedPacoteOptions);
75+
76+
const cachedPacoteOptions = await registry._getPacoteOptions();
77+
78+
t.is(libnpmconfigReadToJSON.callCount, 1);
79+
t.is(libnpmconfig.read.callCount, 1);
80+
81+
t.deepEqual(cachedPacoteOptions, expectedPacoteOptions);
82+
});
83+
84+
test.serial("_getPacoteOptions (proxy config set)", async (t) => {
85+
const {Registry, libnpmconfig, libnpmconfigReadToJSON} = t.context;
86+
87+
const registry = new Registry({
88+
cwd: "cwd",
89+
cacheDir: "cacheDir"
90+
});
91+
92+
const npmConfig = {
93+
"proxy": "http://localhost:9999"
94+
};
95+
96+
const expectedPacoteOptions = {
97+
proxy: "http://localhost:9999"
98+
};
99+
100+
libnpmconfigReadToJSON.returns(npmConfig);
101+
102+
const pacoteOptions = await registry._getPacoteOptions();
103+
104+
t.is(libnpmconfigReadToJSON.callCount, 1);
105+
t.is(libnpmconfig.read.callCount, 1);
106+
t.deepEqual(libnpmconfig.read.getCall(0).args, [{
107+
cache: "cacheDir",
108+
}, {
109+
cwd: "cwd"
110+
}]);
111+
112+
t.deepEqual(pacoteOptions, expectedPacoteOptions);
113+
114+
const cachedPacoteOptions = await registry._getPacoteOptions();
115+
116+
t.is(libnpmconfigReadToJSON.callCount, 1);
117+
t.is(libnpmconfig.read.callCount, 1);
118+
119+
t.deepEqual(cachedPacoteOptions, expectedPacoteOptions);
120+
});
121+
122+
test.serial("_getPacoteOptions (https-proxy config set)", async (t) => {
123+
const {Registry, libnpmconfig, libnpmconfigReadToJSON} = t.context;
124+
125+
const registry = new Registry({
126+
cwd: "cwd",
127+
cacheDir: "cacheDir"
128+
});
129+
130+
const npmConfig = {
131+
"https-proxy": "http://localhost:9999"
132+
};
133+
134+
const expectedPacoteOptions = {
135+
httpsProxy: "http://localhost:9999"
136+
};
137+
57138
libnpmconfigReadToJSON.returns(npmConfig);
58139

59140
const pacoteOptions = await registry._getPacoteOptions();
@@ -62,19 +143,18 @@ test.serial("_getPacoteOptions", async (t) => {
62143
t.is(libnpmconfig.read.callCount, 1);
63144
t.deepEqual(libnpmconfig.read.getCall(0).args, [{
64145
cache: "cacheDir",
65-
agent: false
66146
}, {
67147
cwd: "cwd"
68148
}]);
69149

70-
t.is(pacoteOptions, npmConfig);
150+
t.deepEqual(pacoteOptions, expectedPacoteOptions);
71151

72152
const cachedPacoteOptions = await registry._getPacoteOptions();
73153

74154
t.is(libnpmconfigReadToJSON.callCount, 1);
75155
t.is(libnpmconfig.read.callCount, 1);
76156

77-
t.is(cachedPacoteOptions, npmConfig);
157+
t.deepEqual(cachedPacoteOptions, expectedPacoteOptions);
78158
});
79159

80160
test.serial("_getPacote", async (t) => {

0 commit comments

Comments
 (0)