From d86a118bbdf98a79f2e7066caa4b577391a8eb3d Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 27 Oct 2025 15:01:06 +0100 Subject: [PATCH] http: lazy allocate cookies array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/59734 Reviewed-By: Juan José Arboleda Reviewed-By: Ethan Arrowood Reviewed-By: Tim Perry Reviewed-By: Marco Ippolito Reviewed-By: Jake Yuesong Li Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca --- lib/_http_outgoing.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index fdd0f2f77ea..24aae1caca6 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -673,20 +673,22 @@ OutgoingMessage.prototype.setHeaders = function setHeaders(headers) { // We also cannot safely split by comma. // To avoid setHeader overwriting the previous value we push // set-cookie values in array and set them all at once. - const cookies = []; + let cookies = null; for (const { 0: key, 1: value } of headers) { if (key === 'set-cookie') { if (ArrayIsArray(value)) { + cookies ??= []; cookies.push(...value); } else { + cookies ??= []; cookies.push(value); } continue; } this.setHeader(key, value); } - if (cookies.length) { + if (cookies != null) { this.setHeader('set-cookie', cookies); }