Skip to content

feat: add proxy-agent#1070

Merged
mcollina merged 11 commits intonodejs:mainfrom
RafaelGSS:feat/add-proxy-agent
Oct 24, 2021
Merged

feat: add proxy-agent#1070
mcollina merged 11 commits intonodejs:mainfrom
RafaelGSS:feat/add-proxy-agent

Conversation

@RafaelGSS
Copy link
Copy Markdown
Member

@RafaelGSS RafaelGSS commented Oct 21, 2021

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

image

Axios

image

Request

image

You will notice that other HTTP libraries answer the HTTP request with Connection: close through the proxy. Nonetheless, undici keeps the connection open with keep-alive. I'm supposing that it's was designed by default, I'm sharing it just to align expectations.


TODO:

  • Add ProxyAgent test
  • Https test
  • Add ProxyAgent types test
  • Docs

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Oct 21, 2021

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.96%. Comparing base (a821ba7) to head (f4e177a).
Report is 1515 commits behind head on main.

Additional details and impacted files
@@            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              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Copy Markdown
Member

@mcollina mcollina left a 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!

Comment thread examples/proxy-agent.js
@ronag
Copy link
Copy Markdown
Member

ronag commented Oct 21, 2021

docs?

@RafaelGSS
Copy link
Copy Markdown
Member Author

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.

Comment thread docs/best-practices/proxy.md Outdated

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.
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Member Author

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?

Comment thread test/proxy-agent.js
@@ -0,0 +1,189 @@
'use strict'
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Member Author

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://localhost:8000/'))

async function main() {
  const res1 = await request('http://localhost:3000/undici')
  console.log('response received', res1.statusCode)

  const res2 = await request('http://localhost:4000/undici')
  console.log('response received', res2.statusCode)

}
main()

It will only set http://localhost:8000 as a client.

setting client http://localhost:8000/
response received 200
response received 201

Comment thread docs/api/ProxyAgent.md
@RafaelGSS

This comment has been minimized.

@RafaelGSS
Copy link
Copy Markdown
Member Author

Communication with origin HTTPS is now working. It wasn't working because I've missed the Host header and the servers were answering the as bad requests.

@RafaelGSS RafaelGSS marked this pull request as ready for review October 22, 2021 21:14
Comment thread lib/proxy-agent.js Outdated
headers: {
...opts.headers,
host,
'User-Agent': 'undici'
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member Author

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://localhost:8000/ HTTP proxy
  } = await request('http://localhost:3000/undici', { headers: { 'User-Agent': 'Example/0.2.1' } })

Removed support to pre-defined UserAgent.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment thread index.d.ts
import MockPool = require('./types/mock-pool')
import MockAgent = require('./types/mock-agent')
import mockErrors = require('./types/mock-errors')
import ProxyAgent from './types/proxy-agent'
Copy link
Copy Markdown
Contributor

@RA80533 RA80533 Feb 1, 2022

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

@RA80533 RA80533 mentioned this pull request Feb 1, 2022
KhafraDev pushed a commit to KhafraDev/undici that referenced this pull request Jun 23, 2022
crysmags pushed a commit to crysmags/undici that referenced this pull request Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants