Skip to content

Commit

Permalink
Disable keep-alive on HTTP and HTTPS global agents on Node v19+.
Browse files Browse the repository at this point in the history
  • Loading branch information
moll committed Aug 22, 2024
1 parent 4d831f3 commit 11f789a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased
- Removes the [Underscore.js](https://underscorejs.org) dependency in favor of just inlining two rewritten helper functions.
- Fixes possible double emitting on Node v19+ due to its global HTTP agent enabling keep-alive.

## 1.7.2 (May 1, 2021)
- Increases the upper-bound on [Underscore.js](https://underscorejs.org) dependency to v1.13 (inclusive).
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var slice = Function.call.bind(Array.prototype.slice)
var normalizeConnectArgs = Net._normalizeConnectArgs || Net._normalizeArgs
var createRequestAndResponse = Http._connectionListener
var NODE_0_10 = Semver.satisfies(process.version, ">= 0.10 < 0.11")
var NODE_GTE_19 = Semver.satisfies(process.version, ">= 19")
module.exports = Mitm

function Mitm() {
Expand Down Expand Up @@ -61,6 +62,11 @@ Mitm.prototype.enable = function() {
this.stubs.stub(Http.globalAgent, "maxSockets", Infinity)
this.stubs.stub(Https.globalAgent, "maxSockets", Infinity)
}
else if (NODE_GTE_19) {
// Note v19 enables keep-alive for both the Http and Https globalAgents.
this.stubs.stub(Http.globalAgent, "keepAlive", false)
this.stubs.stub(Https.globalAgent, "keepAlive", false)
}

// ClientRequest.prototype.onSocket is called synchronously from
// ClientRequest's constructor and is a convenient place to hook into new
Expand Down

0 comments on commit 11f789a

Please sign in to comment.