-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add Proxy for Fetch in download.ts - Resolves issue 1280 #1291
Conversation
…are no types for this package
… standard UPPERCASE env convention
I feel that the Windows CI Build is failing across the board as noted with PR #1268 |
It would be mighty cool if this PR was checked out.... might help some us behind proxies use HardHat instead of the TruffleSuite. 😁 |
Hey @cdesch, thanks for submitting this PR. We'll review it in the next few days or early next week. |
Thank you. Surely it will make HardHat a little more accessible :-p |
@alcuadrado is there anything I can do to assist with getting this pull request merged? |
Hey @cdesch, sorry for not having reviewed this yet. I'll take a look this week. |
@fvictorio Thanks. Let me know if there is anything I can do to help. I'm happy to revise or making another PR if you have feedback. There can be some fun issues that crop up with proxies and it is kind of an edge case for some folks, which is why I included tests. I worked a little bit on adding custom certificates for the proxy but it seems that isn't needed currently. I used ENV variables in my commit as it is a pretty standard process for most proxies and the same as when using a proxy for |
That seems fine to me, there seems to be a convention around those two environment variables and as far as I can tell the right place to handle them in this scenario is in Hardhat (there's an issue about these variables in |
@cdesch Thanks a lot for this. I made some minor changes. I'll add some comments explaining them in case you are interested 🙂 |
|
||
const response = await fetch(url, { timeout: timeoutMillis }); | ||
if (process.env.HTTPS_PROXY !== undefined) { | ||
fetchOptions.agent = new HttpsProxyAgent(process.env.HTTPS_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.
There was some unnecessary casting here. The properties of process.env
have type string | undefined
, so if you check for that in the if
, typescript will know that the type is string
inside the block.
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.
Thanks! Yeah, I was struggling a bit on the TypeScript learning curve, but I'm slowly migrating a project to TypeScript currently.
@@ -1,5 +1,7 @@ | |||
import fs from "fs"; | |||
import fsExtra from "fs-extra"; | |||
import { HttpsProxyAgent } from "https-proxy-agent"; | |||
import type { RequestInit as FetchOptions } from "node-fetch"; |
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.
Manually defining FetchOptions
wasn't necessary, it is better to import it from the module.
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 think I was a bit confused on that because of the const { default: fetch } = await import("node-fetch");
which seemed strange as to where to put the import. Thank you!
// Setup Proxy Server | ||
proxy = new Proxy(); | ||
connections = 0; | ||
proxy.on("connection", () => { |
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 added this so we can assert that the proxy is really being used.
process.env.HTTPS_PROXY = oldHttpsProxyEnv; | ||
} else { | ||
delete process.env.HTTPS_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.
To save the current environment, you were doing something like this:
env = process.env
process.env.HTTP_PROXY = ...
But since process.env
is an object, any modifications you were doing to it were also done to env
, so nothing was restored afterwards.
I also have to explicitly use delete
here because it turns out that
process.env.FOO = undefined
doesn't remove the FOO
environment variable. Instead, it sets it to the string "undefined"
. Terrible behavior from node, but it is what it is.
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.
Nice catch. I think adapted that portion from the https-proxy-agent
package, and in fact I wasn't sure if the env
really needed to be changed back at the end of the test because I wasn't sure if changes to the process.env
were scoped to the test or not. It seems like they are.
Thanks for catching this.
Thank you @fvictorio for the comments and the feedback! I learned a lot in the process to get this working. |
@fvictorio Do you think this PR will make the next HH release? Possibly 2.2.x or minor like 2.1.3? Would you like me to add docs for this? |
Hey @cdesch, thanks for this PR. Unfortunately we have to focus on implementing Berlin right now, so we won't get merged this in the next couple of weeks. Once the new hardfork support is out, we'll review and release this. |
Thanks @alcuadrado, Can you recommend how I can install the NPM package from github in the meantime? I understand that typically you can do a My apologies for being persistent, this issue is a show stopper for my team using hardhat on a new project, but we understand that you have priorities too. |
I'm afraid it can't be installed like that. Check the contributing.md which explains how to build it locally. |
@alcuadrado Okay. Thank you. |
@alcuadrado and @fvictorio Happy Weekend! Any chance of looking at this PR this week? 😇 |
I updated this PR to the current Master branch and attempted to resolve the merge conflict with The test suite fails with:
but that seems to be from 0a575a38a and PR 1425, although I could be wrong. The error does not appear to be related to this PR. |
Checking in to see if this can be merged in the next version. Is there anything I can do to help merge this in? |
Hey @cdesch! I'm really sorry I took so long to review this. Thanks for submitting this PR, and for being patient. As I read that you were struggling a bit with ts, I pushed this commit improving a few tiny bits, which you may find interesting. I also added some docs about this. I wasn't sure where to place this info, so I added a warning box to the compilation guide for now. What do you think, @fvictorio? |
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.
Thank you
Thank you @alcuadrado! I think everything looks good. I will test as soon as the package is published. |
@fvictorio if you think the docs change is ok we can merge this. |
Thanks @cdesch and sorry for the huge delay! |
No worries, I completely understand! Thank you @fvictorio ! I'm ready to test it out! |
|
Added functionality to allow
HTTP_PROXY
andHTTPS_PROXY
environment variables for proxy settings.This addresses the issue#1280 to be able to download the compilers behind a corporate firewall via a proxy.
This pull request also adds test cases
packages/hardhat-core/test/internal/util/download.ts
for the existing functionality without using a proxy and using a proxy.