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

Can we restart the work on overridable globalAgent? #23281

Closed
bitinn opened this issue Oct 5, 2018 · 7 comments
Closed

Can we restart the work on overridable globalAgent? #23281

bitinn opened this issue Oct 5, 2018 · 7 comments
Labels
feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors. http Issues or PRs related to the http subsystem.

Comments

@bitinn
Copy link

bitinn commented Oct 5, 2018

Is your feature request related to a problem? Please describe.

There has been quite a few discussions here (#15620, #8381, #1490) on whether nodejs core should support environment variable for http proxy, and so far the consensus has been that proxy support in core is a slippery slope. Which I agree.

But we do need a fix for this headache: when a userland module use a http library, they often don't expose agent option, thus blocking users from using available userland proxy module like node-proxy-agent.

Describe the solution you'd like

I personally believe making http(s).globalAgent overridable is the best way forward:

  1. userland module can provide whatever proxy support they want, be it http/https/socks/pac.
  2. user can override http(s).globalAgent and be confident that ALL http library will respect it.
  3. it has been worked on before (http: make globalAgent of http and https overridable #11249, Modify https.globalAgent doesn't effect #9057), we should follow it through.
  4. after working on node-fetch for 3 years I realize it's simply too much to ask userland module to expose agent or support environment variable: it's often too niche a requirement (eg. why should an oauth lib expose agent?), and many strongly believe it to be nodejs core problem.

In short: overridable globalAgent has the lowest impact of all solutions, allow for highest flexibility when it comes to different proxy types, and has userland code ready to take advantage of it.

Describe alternatives you've considered

See linked discussions.

@Trott Trott added feature request Issues that request new features to be added to Node.js. http Issues or PRs related to the http subsystem. labels Nov 4, 2018
@Trott
Copy link
Member

Trott commented Nov 4, 2018

@nodejs/http

@bnoordhuis bnoordhuis added the good first issue Issues that are suitable for first-time contributors. label Dec 4, 2018
@bnoordhuis
Copy link
Member

@bitinn If you're interested in working on this, you're welcome to. I'll add the good-first-issue label.

@bitinn
Copy link
Author

bitinn commented Dec 8, 2018

@bnoordhuis sorry I didn't get around to do this due to my own schedule, I hope someone with same need could pick this up.

illBeRoy added a commit to illBeRoy/node that referenced this issue Dec 21, 2018
Overriding `require('http[s]').globalAgent` is now respected by
consequent requests.

In order to achieve that, the following changes were made:

1. Implmentation in `http`: `module.exports.globalAgent` is now defined
through `Object.defineProperty`. Its getter and setter return \ set
`require('_http_agent').globalAgent`.

2. Implementation in `https`: the https `globalAgent` is not the same
as `_http_agent`, and is defined in `https` module itself. Therefore,
the fix here was to simply use `module.exports.globalAgent` to support
mutation.

3. According tests were added for both `http` and `https`, where in
both we create a server, set the default agent to a newly created
instance and make a request to that server. We then assert that the
given instance was actually used by inspecting its sockets property.

Fixes: nodejs#23281
@illBeRoy
Copy link
Contributor

Hey :) this issue was labelled as a "good first issue" so I've taken the liberty to attempt and fix it. This is the relevant PR.

addaleax pushed a commit that referenced this issue Jan 9, 2019
Overriding `require('http[s]').globalAgent` is now respected by
consequent requests.

In order to achieve that, the following changes were made:

1. Implmentation in `http`: `module.exports.globalAgent` is now defined
through `Object.defineProperty`. Its getter and setter return \ set
`require('_http_agent').globalAgent`.

2. Implementation in `https`: the https `globalAgent` is not the same
as `_http_agent`, and is defined in `https` module itself. Therefore,
the fix here was to simply use `module.exports.globalAgent` to support
mutation.

3. According tests were added for both `http` and `https`, where in
both we create a server, set the default agent to a newly created
instance and make a request to that server. We then assert that the
given instance was actually used by inspecting its sockets property.

Fixes: #23281

PR-URL: #25170
Reviewed-By: James M Snell <jasnell@gmail.com>
refack pushed a commit to refack/node that referenced this issue Jan 14, 2019
Overriding `require('http[s]').globalAgent` is now respected by
consequent requests.

In order to achieve that, the following changes were made:

1. Implmentation in `http`: `module.exports.globalAgent` is now defined
through `Object.defineProperty`. Its getter and setter return \ set
`require('_http_agent').globalAgent`.

2. Implementation in `https`: the https `globalAgent` is not the same
as `_http_agent`, and is defined in `https` module itself. Therefore,
the fix here was to simply use `module.exports.globalAgent` to support
mutation.

3. According tests were added for both `http` and `https`, where in
both we create a server, set the default agent to a newly created
instance and make a request to that server. We then assert that the
given instance was actually used by inspecting its sockets property.

Fixes: nodejs#23281

PR-URL: nodejs#25170
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR pushed a commit to BridgeAR/node that referenced this issue Jan 16, 2019
Overriding `require('http[s]').globalAgent` is now respected by
consequent requests.

In order to achieve that, the following changes were made:

1. Implmentation in `http`: `module.exports.globalAgent` is now defined
through `Object.defineProperty`. Its getter and setter return \ set
`require('_http_agent').globalAgent`.

2. Implementation in `https`: the https `globalAgent` is not the same
as `_http_agent`, and is defined in `https` module itself. Therefore,
the fix here was to simply use `module.exports.globalAgent` to support
mutation.

3. According tests were added for both `http` and `https`, where in
both we create a server, set the default agent to a newly created
instance and make a request to that server. We then assert that the
given instance was actually used by inspecting its sockets property.

Fixes: nodejs#23281

PR-URL: nodejs#25170
Reviewed-By: James M Snell <jasnell@gmail.com>
@gajus
Copy link

gajus commented Apr 24, 2019

@illBeRoy Has this been released?

BethGriggs pushed a commit that referenced this issue Apr 28, 2019
Overriding `require('http[s]').globalAgent` is now respected by
consequent requests.

In order to achieve that, the following changes were made:

1. Implmentation in `http`: `module.exports.globalAgent` is now defined
through `Object.defineProperty`. Its getter and setter return \ set
`require('_http_agent').globalAgent`.

2. Implementation in `https`: the https `globalAgent` is not the same
as `_http_agent`, and is defined in `https` module itself. Therefore,
the fix here was to simply use `module.exports.globalAgent` to support
mutation.

3. According tests were added for both `http` and `https`, where in
both we create a server, set the default agent to a newly created
instance and make a request to that server. We then assert that the
given instance was actually used by inspecting its sockets property.

Fixes: #23281

PR-URL: #25170
Reviewed-By: James M Snell <jasnell@gmail.com>
@illBeRoy
Copy link
Contributor

illBeRoy commented May 1, 2019

I think this was merged to 11 already.
Seems like it's on 10 staging according to 43dd99c :)

@gajus
Copy link

gajus commented May 1, 2019

I have since released https://github.com/gajus/global-agent.

BethGriggs pushed a commit that referenced this issue May 10, 2019
Overriding `require('http[s]').globalAgent` is now respected by
consequent requests.

In order to achieve that, the following changes were made:

1. Implmentation in `http`: `module.exports.globalAgent` is now defined
through `Object.defineProperty`. Its getter and setter return \ set
`require('_http_agent').globalAgent`.

2. Implementation in `https`: the https `globalAgent` is not the same
as `_http_agent`, and is defined in `https` module itself. Therefore,
the fix here was to simply use `module.exports.globalAgent` to support
mutation.

3. According tests were added for both `http` and `https`, where in
both we create a server, set the default agent to a newly created
instance and make a request to that server. We then assert that the
given instance was actually used by inspecting its sockets property.

Fixes: #23281

PR-URL: #25170
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this issue May 16, 2019
Overriding `require('http[s]').globalAgent` is now respected by
consequent requests.

In order to achieve that, the following changes were made:

1. Implmentation in `http`: `module.exports.globalAgent` is now defined
through `Object.defineProperty`. Its getter and setter return \ set
`require('_http_agent').globalAgent`.

2. Implementation in `https`: the https `globalAgent` is not the same
as `_http_agent`, and is defined in `https` module itself. Therefore,
the fix here was to simply use `module.exports.globalAgent` to support
mutation.

3. According tests were added for both `http` and `https`, where in
both we create a server, set the default agent to a newly created
instance and make a request to that server. We then assert that the
given instance was actually used by inspecting its sockets property.

Fixes: #23281

PR-URL: #25170
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors. http Issues or PRs related to the http subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants