Skip to content

Commit 144c2db

Browse files
committed
fix: proper error handling
fix: if priority
1 parent 276321d commit 144c2db

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

packages/grpc-js/src/http_proxy.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,36 @@ interface ProxyInfo {
4141
creds?: string;
4242
}
4343

44-
function getProxyInfo(proxy?: string): ProxyInfo {
45-
let proxyEnv = '';
44+
function getProxyInfo(options: ChannelOptions): ProxyInfo {
45+
let proxyUrlString = '';
4646
let envVar = '';
47-
/* Prefer using 'grpc_proxy'. Fallback on 'http_proxy' if it is not set.
47+
/* Prefer using 'grpc.http_proxy' option. Fallback on 'grpc_proxy' env var if it is not set.
4848
* Also prefer using 'https_proxy' with fallback on 'http_proxy'. The
4949
* fallback behavior can be removed if there's a demand for it.
5050
*/
51-
if (process.env.grpc_proxy) {
51+
if (options['grpc.http_proxy']) {
52+
proxyUrlString = options['grpc.http_proxy'];
53+
} else if (process.env.grpc_proxy) {
5254
envVar = 'grpc_proxy';
53-
proxyEnv = process.env.grpc_proxy;
55+
proxyUrlString = process.env.grpc_proxy;
5456
} else if (process.env.https_proxy) {
5557
envVar = 'https_proxy';
56-
proxyEnv = process.env.https_proxy;
58+
proxyUrlString = process.env.https_proxy;
5759
} else if (process.env.http_proxy) {
5860
envVar = 'http_proxy';
59-
proxyEnv = process.env.http_proxy;
60-
} else if(proxy) {
61-
envVar = 'proxy';
62-
proxyEnv = proxy
61+
proxyUrlString = process.env.http_proxy;
6362
} else {
6463
return {};
6564
}
6665
let proxyUrl: URL;
6766
try {
68-
proxyUrl = new URL(proxyEnv);
67+
proxyUrl = new URL(proxyUrlString);
6968
} catch (e) {
70-
log(LogVerbosity.ERROR, `cannot parse value of "${envVar}" env var`);
69+
if (envVar) {
70+
log(LogVerbosity.ERROR, `cannot parse value of "${envVar}" env var`);
71+
} else {
72+
log(LogVerbosity.ERROR, `cannot parse value of "grpc.http_proxy" channel option`);
73+
}
7174
return {};
7275
}
7376
if (proxyUrl.protocol !== 'http:') {
@@ -100,9 +103,15 @@ function getProxyInfo(proxy?: string): ProxyInfo {
100103
if (userCred) {
101104
result.creds = userCred;
102105
}
103-
trace(
104-
'Proxy server ' + result.address + ' set by environment variable ' + envVar
105-
);
106+
if (envVar) {
107+
trace(
108+
'Proxy server ' + result.address + ' set by environment variable ' + envVar
109+
);
110+
} else {
111+
trace(
112+
'Proxy server ' + result.address + ' set by channel option grpc.http_proxy'
113+
);
114+
}
106115
return result;
107116
}
108117

@@ -193,7 +202,7 @@ export function mapProxyName(
193202
if (target.scheme === 'unix') {
194203
return noProxyResult;
195204
}
196-
const proxyInfo = getProxyInfo(options['grpc.http_proxy']);
205+
const proxyInfo = getProxyInfo(options);
197206
if (!proxyInfo.address) {
198207
return noProxyResult;
199208
}

0 commit comments

Comments
 (0)