-
Notifications
You must be signed in to change notification settings - Fork 523
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 proxy-agent #1070
feat: add proxy-agent #1070
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1070 +/- ##
==========================================
+ Coverage 94.93% 94.96% +0.03%
==========================================
Files 38 39 +1
Lines 3709 3734 +25
==========================================
+ Hits 3521 3546 +25
Misses 188 188
Continue to review full report at Codecov.
|
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.
Could you add another test with a path as well?
Looks good!
docs? |
I've improved the test coverage and included API documentation in this new class. There is a remaining check to do before making it ready for review which is the usage with HTTPS. I'll do it and share feedback ASAP. |
docs/best-practices/proxy.md
Outdated
@@ -9,6 +9,8 @@ the `path` should be `path: 'http:https://upstream.server:port/hello?foo=bar'`. | |||
|
|||
If you proxy requires basic authentication, you can send it via the `proxy-authorization` header. | |||
|
|||
> In latest versions of `undici` you can use [`ProxyAgent`](docs/api/ProxyAgent.md) to avoid some settings. |
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 would consider rewording this:
- after the first release where this will be available, the version in which it is available will not be the "latest" anymore. When this code is released this comment is basically redundant because the ProxyAgent is available and that's all users need to know. This mention should simply go in the release notes. I would also move it to the top as it's probably going to be the preferred way to set it up
- "avoid some settings" could be reworded as well
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.
Do you think that this entire document should be refactored to use ProxyAgent or a recommendation on top of the file:
Connecting through a proxy is possible by:
- Using [AgentProxy](docs/api/ProxyAgent.md).
- Configuring `Client` or `Pool` constructor.
should be enough?
@@ -0,0 +1,189 @@ | |||
'use strict' |
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.
in the agent implementation I see it's dealing with possibly multiple clients. is it something worth testing as well?
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.
If I'm not mistaken, when dealing with Proxy just one client is set.
Example:
setGlobalDispatcher(new ProxyAgent('http:https://localhost:8000/'))
async function main() {
const res1 = await request('http:https://localhost:3000/undici')
console.log('response received', res1.statusCode)
const res2 = await request('http:https://localhost:4000/undici')
console.log('response received', res2.statusCode)
}
main()
It will only set http:https://localhost:8000
as a client.
setting client http:https://localhost:8000/
response received 200
response received 201
This comment has been minimized.
This comment has been minimized.
Communication with origin HTTPS is now working. It wasn't working because I've missed the |
lib/proxy-agent.js
Outdated
headers: { | ||
...opts.headers, | ||
host, | ||
'User-Agent': 'undici' |
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.
Let's remove this user-agent if it's not strictly needed. If it's strictly required by proxies, let's make sure it's user-configurable (right now is not if I read the code correctly).
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.
It's not required by proxies and it's totally user-configurable:
const {
statusCode,
headers,
trailers,
body
// send the request via the http:https://localhost:8000/ HTTP proxy
} = await request('http:https://localhost:3000/undici', { headers: { 'User-Agent': 'Example/0.2.1' } })
Removed support to pre-defined UserAgent.
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.
lgtm
@@ -10,13 +10,14 @@ import MockClient = require('./types/mock-client') | |||
import MockPool = require('./types/mock-pool') | |||
import MockAgent = require('./types/mock-agent') | |||
import mockErrors = require('./types/mock-errors') | |||
import ProxyAgent from './types/proxy-agent' |
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 does not correspond with the actual import of ProxyAgent (index.js#L17).
Address: #569.
There is a remaining point to be discussed. I've implemented it and tested it against other HTTP libraries (request, axios) and here are a quick HTTP response summary:
Undici
Axios
Request
You will notice that other HTTP libraries answer the HTTP request with
Connection: close
through the proxy. Nonetheless,undici
keeps the connection open withkeep-alive
. I'm supposing that it's was designed by default, I'm sharing it just to align expectations.TODO: