New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add no_proxy env variable #361
Conversation
@@ -55,7 +74,6 @@ function loadProxy() { | |||
} | |||
return proxy; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bcoe, I want to ask about the default behavior. If a URL comes in that is not listed under no_proxy or proxy, should it be proxied? Currently that's the way it is with the code as it stands now, but I feel like the default should be the other way around. In other words, we should maybe do some kind of checking to see if the URL equals any of the HTTPS_PROXY env variables.
Codecov Report
@@ Coverage Diff @@
## master #361 +/- ##
==========================================
- Coverage 96.31% 96.31% -0.01%
==========================================
Files 6 6
Lines 705 732 +27
Branches 99 106 +7
==========================================
+ Hits 679 705 +26
- Misses 26 27 +1
Continue to review full report at Codecov.
|
src/gaxios.ts
Outdated
@@ -44,7 +44,26 @@ let HttpsProxyAgent: any; | |||
|
|||
// Figure out if we should be using a proxy. Only if it's required, load | |||
// the https-proxy-agent module as it adds startup cost. | |||
function loadProxy() { | |||
function loadProxy(url: string) { | |||
const noProxy = (process.env.no_proxy ?? process.env.NO_PROXY)?.split(','); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line of code is hurting my brain. Using the ??
means if process.env.no_proxy
is an empty string, we would accept that value. Is that our intent? I would imagine ||
is more appropriate here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't realize it would accept empty strings. Changed!
src/gaxios.ts
Outdated
function loadProxy() { | ||
function loadProxy(url: string) { | ||
const noProxy = (process.env.no_proxy ?? process.env.NO_PROXY)?.split(','); | ||
if (noProxy) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also check that noProxy.length > 0
?
@@ -55,7 +74,6 @@ function loadProxy() { | |||
} | |||
return proxy; | |||
} | |||
loadProxy(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So thinking back through this - I think I was front-loading this call so that the require
call would always happen on app start, instead of potentially happening during a request and synchronously blocking the thread. By removing this, we could introduce a slower HTTP call here, and kind of degrade performance at a random point in app execution, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(sorry @bcoe I know you asked earlier, but it didn't make sense until I re-looked at the codes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JustinBeckwith, given that the function now depends on checking against a URL, wouldn't we have to wait for it to be called during a request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just had a discussion with @sofisl about a potential refactor here, to get the best of both worlds.
test/test.getch.ts
Outdated
const res = await request({url}); | ||
scope.done(); | ||
assert.deepStrictEqual(res.data, body); | ||
assert.ok(res.config.agent instanceof HttpsProxyAgent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I simply love the number of tests you wrote to cover this
@JustinBeckwith @bcoe, refactored the code to separate out loadProxy() and getProxy(). The default behavior will be to load proxy if there's a proxy env variable, but will only use it if there is a match for proxy variable. |
src/gaxios.ts
Outdated
if (proxy) { | ||
loadProxy(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't need this line anymore
src/gaxios.ts
Outdated
return matchingProxyStrings( | ||
process.env.HTTPS_PROXY, | ||
process.env.https_proxy, | ||
process.env.HTTP_PROXY, | ||
process.env.http_proxy, | ||
url | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should just return the proxy here, the logic is different for http_proxy
, vs., no_proxy
(one contains a list of URLs, the other is just a single URL representing the proxy).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticed a slight logic hiccup.
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
test/test.getch.ts
Outdated
assert.strictEqual(res.config.agent, undefined); | ||
}); | ||
|
||
it('should proxy if url is an exact match of proxy, but not of no_proxy', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should also update this test to reflect the fact that there's no correlation between the URL and the https_proxy
variable.
It's no_proxy
that matches a list of URLs.
test/test.getch.ts
Outdated
}); | ||
|
||
it('should allow comma-separated lists for proxy env variables', async () => { | ||
const url = 'https://api.google.com'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test can be dropped.
Fixes #69