diff --git a/CHANGELOG.md b/CHANGELOG.md index 27d1aa9..6536e64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/index.js b/index.js index 50930c9..fbc913f 100644 --- a/index.js +++ b/index.js @@ -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() { @@ -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