Skip to content
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

Merged
merged 17 commits into from Dec 8, 2020
Merged

feat: add no_proxy env variable #361

merged 17 commits into from Dec 8, 2020

Conversation

sofisl
Copy link
Contributor

@sofisl sofisl commented Nov 6, 2020

Fixes #69

@sofisl sofisl requested a review from a team as a code owner November 6, 2020 04:05
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Nov 6, 2020
@sofisl sofisl requested a review from bcoe November 6, 2020 04:05
@@ -55,7 +74,6 @@ function loadProxy() {
}
return proxy;
}
Copy link
Contributor Author

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
Copy link

codecov bot commented Nov 6, 2020

Codecov Report

Merging #361 (2e9d695) into master (132568f) will decrease coverage by 0.00%.
The diff coverage is 96.66%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
src/gaxios.ts 98.67% <96.66%> (-0.24%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 132568f...2e9d695. Read the comment docs.

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(',');
Copy link
Contributor

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.

Copy link
Contributor Author

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) {
Copy link
Contributor

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();
Copy link
Contributor

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?

Copy link
Contributor

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)

Copy link
Contributor Author

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?

Copy link
Contributor

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.

const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.ok(res.config.agent instanceof HttpsProxyAgent);
Copy link
Contributor

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

@sofisl
Copy link
Contributor Author

sofisl commented Nov 10, 2020

@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();
Copy link
Contributor

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 Show resolved Hide resolved
@sofisl sofisl requested a review from bcoe November 24, 2020 22:23
src/gaxios.ts Outdated
Comment on lines 101 to 107
return matchingProxyStrings(
process.env.HTTPS_PROXY,
process.env.https_proxy,
process.env.HTTP_PROXY,
process.env.http_proxy,
url
);
Copy link
Contributor

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).

Copy link
Contributor

@bcoe bcoe left a 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.

@sofisl sofisl requested a review from bcoe November 30, 2020 21:48
src/gaxios.ts Show resolved Hide resolved
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
assert.strictEqual(res.config.agent, undefined);
});

it('should proxy if url is an exact match of proxy, but not of no_proxy', async () => {
Copy link
Contributor

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.

});

it('should allow comma-separated lists for proxy env variables', async () => {
const url = 'https://api.google.com';
Copy link
Contributor

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.

@sofisl sofisl requested a review from bcoe December 5, 2020 08:58
@sofisl sofisl merged commit efe72a7 into master Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

adding support for no_proxy environment variable
3 participants