Skip to content

Commit 0bce450

Browse files
authored
Merge pull request #3028 from notsapinho/master
feat: support "http_proxy" channel option
2 parents 8e68881 + 144c2db commit 0bce450

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

packages/grpc-js/src/channel-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface ChannelOptions {
3636
'grpc.max_send_message_length'?: number;
3737
'grpc.max_receive_message_length'?: number;
3838
'grpc.enable_http_proxy'?: number;
39+
'grpc.http_proxy'?: string;
3940
/* http_connect_target and http_connect_creds are used for passing data
4041
* around internally, and should not be documented as public-facing options
4142
*/

packages/grpc-js/src/http_proxy.ts

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

44-
function getProxyInfo(): 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;
61+
proxyUrlString = process.env.http_proxy;
6062
} else {
6163
return {};
6264
}
6365
let proxyUrl: URL;
6466
try {
65-
proxyUrl = new URL(proxyEnv);
67+
proxyUrl = new URL(proxyUrlString);
6668
} catch (e) {
67-
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+
}
6874
return {};
6975
}
7076
if (proxyUrl.protocol !== 'http:') {
@@ -97,9 +103,15 @@ function getProxyInfo(): ProxyInfo {
97103
if (userCred) {
98104
result.creds = userCred;
99105
}
100-
trace(
101-
'Proxy server ' + result.address + ' set by environment variable ' + envVar
102-
);
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+
}
103115
return result;
104116
}
105117

@@ -190,7 +202,7 @@ export function mapProxyName(
190202
if (target.scheme === 'unix') {
191203
return noProxyResult;
192204
}
193-
const proxyInfo = getProxyInfo();
205+
const proxyInfo = getProxyInfo(options);
194206
if (!proxyInfo.address) {
195207
return noProxyResult;
196208
}

0 commit comments

Comments
 (0)