From 9baa7eabd460b086040a1519cf6038a7e856fe73 Mon Sep 17 00:00:00 2001 From: Roberto Bianchi Date: Mon, 30 Nov 2026 11:11:20 +0100 Subject: [PATCH] http: avoid mutable push on private arrays The server's per-connection request and response queues, the uncork() and end() callback lists in OutgoingMessage, and the socket sets in Agent.prototype.removeSocket() used .push() on private arrays, which looks up Array.prototype.push at call time. These files do not use prototype primordials, so replacing that method from user code changed how they ran. Use indexed assignments instead. Signed-off-by: Roberto Bianchi --- lib/internal/http/agent.js | 2 +- lib/internal/http/outgoing.js | 6 +++--- lib/internal/http/server.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/internal/http/agent.js b/lib/internal/http/agent.js index 20aef9f83760..cd17592d06c9 100644 --- a/lib/internal/http/agent.js +++ b/lib/internal/http/agent.js @@ -617,7 +617,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) { // If the socket was destroyed, remove it from the free buffers too. if (!s.writable) - sets.push(this.freeSockets); + sets[sets.length] = this.freeSockets; for (let sk = 0; sk < sets.length; sk++) { const sockets = sets[sk]; diff --git a/lib/internal/http/outgoing.js b/lib/internal/http/outgoing.js index 6dfdbb244417..5c2efbfb7d9d 100644 --- a/lib/internal/http/outgoing.js +++ b/lib/internal/http/outgoing.js @@ -370,7 +370,7 @@ OutgoingMessage.prototype.uncork = function uncork() { this._send(crlf_buf, null, null); for (let n = 0; n < buf.length; n += 3) { this._send(buf[n + 0], buf[n + 1], null); - callbacks.push(buf[n + 2]); + callbacks[callbacks.length] = buf[n + 2]; } this._send(crlf_buf, null, (err) => { for (const callback of callbacks) { @@ -1300,8 +1300,8 @@ function queueEndCallback(msg, callback) { process.nextTick(callback, msg[kFlushError]); return; } - msg[kEndCallbacks] ??= []; - msg[kEndCallbacks].push(callback); + const callbacks = msg[kEndCallbacks] ??= []; + callbacks[callbacks.length] = callback; } function onFinish(outmsg, err) { diff --git a/lib/internal/http/server.js b/lib/internal/http/server.js index f6031f98a0aa..4f58399e187a 100644 --- a/lib/internal/http/server.js +++ b/lib/internal/http/server.js @@ -1417,7 +1417,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) { } } - state.incoming.push(req); + state.incoming[state.incoming.length] = req; // If the writable end isn't consuming, then stop reading // so that we don't become overwhelmed by a flood of @@ -1466,7 +1466,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) { if (socket._httpMessage) { // There are already pending outgoing res, append. - state.outgoing.push(res); + state.outgoing[state.outgoing.length] = res; } else { res.assignSocket(socket); }