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

Connecting through a proxy #568

Merged
merged 4 commits into from
Feb 26, 2021
Merged

Connecting through a proxy #568

merged 4 commits into from
Feb 26, 2021

Conversation

delvedor
Copy link
Member

This pr adds a test and the documentation to show how to connect through a proxy :)
I had to remove the super complex regex as it was not validating all absolute urls (eg: http://localhost:8080 didn't pass the check). I think that lazily checking the protocol is enough, as trying to save all users from themselves is impossible.

/cc @szmarczak
Closes #427

@delvedor delvedor requested review from mcollina and ronag February 26, 2021 10:30
@delvedor delvedor added Docs Changes related to the documentation enhancement New feature or request labels Feb 26, 2021
Copy link
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.

Would you mind to show how to use it with Pool?

It would really be nice to also include some ways of having a ProxyAgent.

docs/proxy.md Outdated Show resolved Hide resolved
@delvedor
Copy link
Member Author

It works in the exact same way with both Client and Pool, so I've added an additional test and a note in the docs :)

@mcollina
Copy link
Member

mcollina commented Feb 26, 2021

Here is an example for the Agent API:

import { request, setGlobalAgent, ProxyAgent } from 'undici'

setGlobalAgent(new ProxyAgent('https://my.proxy.agent/'))

const {
  statusCode,
  headers,
  trailers,
  body
  // send the request via the proxy
} = await request('http://localhost:3000/foo')

console.log('response received', statusCode)
console.log('headers', headers)

for await (const data of body) {
  console.log('data', data)
}

console.log('trailers', trailers)

@codecov-io
Copy link

codecov-io commented Feb 26, 2021

Codecov Report

Merging #568 (f9603f3) into master (1cae429) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #568      +/-   ##
==========================================
- Coverage   99.57%   99.57%   -0.01%     
==========================================
  Files          16       16              
  Lines        1400     1399       -1     
==========================================
- Hits         1394     1393       -1     
  Misses          6        6              
Impacted Files Coverage Δ
lib/core/request.js 100.00% <100.00%> (ø)

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 1cae429...f9603f3. Read the comment docs.

@mcollina
Copy link
Member

FYI, you don't need to add the ProxyAgent right now. It can be another issue.

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

@budarin
Copy link

budarin commented Sep 24, 2021

is there an example with proxying a POST request?

@mcollina
Copy link
Member

No but it would be a good PR.

@Uzlopak Uzlopak deleted the delvedor/proxy branch February 21, 2024 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Changes related to the documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connect through a proxy
5 participants