From 5751fc1180ed6544602c681ffbd08ca66a0cb12c Mon Sep 17 00:00:00 2001 From: Ehden Sinai Date: Thu, 18 Oct 2018 14:11:37 -0400 Subject: [PATCH] fix: sockLen being miscalculated when removing sockets (#60) Order of operations issue with + and ternary statements: console.log(0 + '' ? 'a' : 'b') // a The code: freeLen + this.sockets[name] ? this.sockets[name].length : 0; Is equivalent to: (freeLen + this.sockets[name]) ? this.sockets[name].length : 0; When `this.sockets[name]` exists, it is an array, `(freeLen + this.sockets[name])` evaluates to a string, which evaluates to a string (true). When it does not, `(freeLen + this.sockets[name])` evaluates to `NaN` (false). Either way, `freeLen` is ignored. --- lib/_http_agent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 83f1d11..c324b7f 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -380,7 +380,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) { // [patch start] var freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0; - var sockLen = freeLen + this.sockets[name] ? this.sockets[name].length : 0; + var sockLen = freeLen + (this.sockets[name] ? this.sockets[name].length : 0); // [patch end] if (this.requests[name] && this.requests[name].length && sockLen < this.maxSockets) {