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

lib: support overriding http\s.globalAgent #25170

Conversation

illBeRoy
Copy link
Contributor

Overriding require('http').globalAgent and require('https').globalAgent is now respected by consequent requests.

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

  1. http: module.exports.globalAgent is now defined through Object.defineProperty. Its getter and setter return \ set require('_http_agent').globalAgent.
  2. 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. test/parallel: 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

On a side note, this is my first contribution to the project. Would appreciate feedback :)

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added http Issues or PRs related to the http subsystem. https Issues or PRs related to the https subsystem. labels 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
@Trott
Copy link
Member

Trott commented Dec 23, 2018

@Trott
Copy link
Member

Trott commented Dec 23, 2018

Will this require updates to the docs?

@illBeRoy
Copy link
Contributor Author

illBeRoy commented Dec 23, 2018

Hey @Trott,

I believe that the way it worked up until now was a bug, because one could expect reassigning to http[s] globalAgent would work much like mutating it, which currently works. The inconsistent behavior, where mutating is possible but overriding is not, was the result of destructuring in both modules, and that is what I attended to :)

That's why I don't think a change to the docs is required - as far as I can see, they simply state that the globalAgent is the default agent for all requests (in both HTTP and HTTPS).

Then again - if you deem it required, I'll be more than happy update my PR :)

@Trott
Copy link
Member

Trott commented Dec 23, 2018

@Trott
Copy link
Member

Trott commented Dec 23, 2018

Resume Build CI again: https://ci.nodejs.org/job/node-test-pull-request/19768/ ✔️

@Trott
Copy link
Member

Trott commented Dec 23, 2018

@nodejs/http

@jasnell jasnell added the semver-minor PRs that contain new features and should be released in the next minor version. label Dec 27, 2018
@illBeRoy
Copy link
Contributor Author

illBeRoy commented Jan 7, 2019

Hey again :) I'm not quite sure about the process, but since it's approved, is there anything else for me to do? how do I know if it was merged? Thanks!

@addaleax
Copy link
Member

addaleax commented Jan 7, 2019

@illBeRoy Pinging us is the exact right thing to do. :)

Landed in 5774688, and congratulations to your first contribution! 🎉

@addaleax addaleax closed this Jan 7, 2019
addaleax pushed a commit that referenced this pull request Jan 7, 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 <[email protected]>
addaleax pushed a commit that referenced this pull request 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 <[email protected]>
refack pushed a commit to refack/node that referenced this pull request 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 <[email protected]>
@BridgeAR BridgeAR mentioned this pull request Jan 16, 2019
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request 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 <[email protected]>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Jan 17, 2019
Notable Changes

* compression / zlib:
  * Added brotli support (Anna Henningsen and Zach Vacura)
    nodejs#24938
* console:
  * Added `inspectOptions` option (Ruben Bridgewater)
    nodejs#24978
* crypto:
  * Always accept private keys as public keys (Tobias Nießen)
    nodejs#25217
* deps:
  * Upgrade npm to v6.5.0 (Jordan Harband)
    nodejs#25234
* fs:
  * Use internalBinding('fs') internally instead of
    process.binding('fs') (Masashi Hirano)
    nodejs#22478
* http(s):
  * Support overriding http\\s.globalAgent (Roy Sommer)
    nodejs#25170
* util:
  * Inspect ArrayBuffers contents closely (Ruben Bridgewater)
    nodejs#25006
* worker:
  * Expose workers by default and remove `--experimental-worker` flag
    (Anna Henningsen) nodejs#25361

PR-URL: nodejs#25537
BridgeAR added a commit that referenced this pull request Jan 18, 2019
Notable Changes

* compression / zlib:
  * Added brotli support (Anna Henningsen and Zach Vacura)
    #24938
* console:
  * Added `inspectOptions` option (Ruben Bridgewater)
    #24978
* crypto:
  * Always accept private keys as public keys (Tobias Nießen)
    #25217
* deps:
  * Upgrade npm to v6.5.0 (Jordan Harband)
    #25234
* fs:
  * Use internalBinding('fs') internally instead of
    process.binding('fs') (Masashi Hirano)
    #22478
* http(s):
  * Support overriding http\\s.globalAgent (Roy Sommer)
    #25170
* util:
  * Inspect ArrayBuffers contents closely (Ruben Bridgewater)
    #25006
* worker:
  * Expose workers by default and remove `--experimental-worker` flag
    (Anna Henningsen) #25361

PR-URL: #25537
@MylesBorins MylesBorins mentioned this pull request Jan 24, 2019
BethGriggs pushed a commit that referenced this pull request 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 <[email protected]>
@BethGriggs BethGriggs mentioned this pull request May 1, 2019
BethGriggs pushed a commit that referenced this pull request 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 <[email protected]>
MylesBorins pushed a commit that referenced this pull request 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 <[email protected]>
whut added a commit to whut/node that referenced this pull request Jul 17, 2023
Under ECMAScript modules when you do "import * as https from 'https'"
you get a new object with properties copied from https module exports.
So if this is a regular data property, you will just override a copy,
but if this would be a accessor property, we can still access the actual
https.globalAgent.

Refs: nodejs#25170, nodejs#9386
whut added a commit to whut/node that referenced this pull request Jul 17, 2023
Under ECMAScript modules when you do "import * as https from 'https'"
you get a new object with properties copied from https module exports.
So if this is a regular data property, you will just override a copy,
but if this would be a accessor property, we can still access the actual
https.globalAgent.

Refs: nodejs#25170, nodejs#9386
whut added a commit to whut/node that referenced this pull request Jul 17, 2023
Under ECMAScript modules when you do "import * as https from 'https'"
you get a new object with properties copied from https module exports.
So if this is a regular data property, you will just override a copy,
but if this would be a accessor property, we can still access the actual
https.globalAgent.

Refs: nodejs#25170, nodejs#9386
whut added a commit to whut/node that referenced this pull request Jul 17, 2023
Under ECMAScript modules when you do "import * as https from 'https'"
you get a new object with properties copied from https module exports.
So if this is a regular data property, you will just override a copy,
but if this would be a accessor property, we can still access the actual
https.globalAgent.

Refs: nodejs#25170, nodejs#9386
whut added a commit to whut/node that referenced this pull request Jul 19, 2023
Under ECMAScript modules when you do "import * as https from 'https'"
you get a new object with properties copied from https module exports.
So if this is a regular data property, you will just override a copy,
but if this would be a accessor property, we can still access the actual
https.globalAgent.

Refs: nodejs#25170, nodejs#9386
whut added a commit to whut/node that referenced this pull request Jul 20, 2023
Under ECMAScript modules when you do "import * as https from 'https'"
you get a new object with properties copied from https module exports.
So if this is a regular data property, you will just override a copy,
but if this would be a accessor property, we can still access the actual
https.globalAgent.

Refs: nodejs#25170, nodejs#9386
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem. https Issues or PRs related to the https subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can we restart the work on overridable globalAgent?
5 participants