From b22da5734dace60693da2245001fb2311c6604ff Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sat, 2 Mar 2024 16:33:53 +0900 Subject: [PATCH 1/2] perf(headers): a single set-cookie --- benchmarks/fetch/headers-length32.mjs | 2 +- lib/web/fetch/headers.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmarks/fetch/headers-length32.mjs b/benchmarks/fetch/headers-length32.mjs index 744263f2569..a9b7a52a40e 100644 --- a/benchmarks/fetch/headers-length32.mjs +++ b/benchmarks/fetch/headers-length32.mjs @@ -19,7 +19,7 @@ const headers = new Headers( 'Width', 'Accept-CH', 'Via', - 'Refresh', + 'Set-Cookie', 'Server', 'Sec-Fetch-Dest', 'Sec-CH-UA-Model', diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index f21c33b438d..b3ec5a70711 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -536,7 +536,7 @@ class Headers { const cookies = this[kHeadersList].cookies // fast-path - if (cookies === null) { + if (cookies === null || cookies.length === 1) { // Note: The non-null assertion of value has already been done by `HeadersList#toSortedArray` return (this[kHeadersList][kHeadersSortedMap] = names) } From a98a09a771edb18a91ba822e0ae9d9bdd0f94bf5 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Sun, 3 Mar 2024 09:35:38 +0100 Subject: [PATCH 2/2] add unit test --- test/fetch/headers.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/test/fetch/headers.js b/test/fetch/headers.js index acdf5ab611e..00798559e90 100644 --- a/test/fetch/headers.js +++ b/test/fetch/headers.js @@ -694,7 +694,7 @@ test('Headers.prototype.getSetCookie', async (t) => { }) // https://github.com/nodejs/undici/issues/1935 - await t.test('When Headers are cloned, so are the cookies', async (t) => { + await t.test('When Headers are cloned, so are the cookies (single entry)', async (t) => { const server = createServer((req, res) => { res.setHeader('Set-Cookie', 'test=onetwo') res.end('Hello World!') @@ -709,6 +709,22 @@ test('Headers.prototype.getSetCookie', async (t) => { assert.deepStrictEqual(res.headers.getSetCookie(), ['test=onetwo']) assert.ok('set-cookie' in entries) }) + + await t.test('When Headers are cloned, so are the cookies (multiple entries)', async (t) => { + const server = createServer((req, res) => { + res.setHeader('Set-Cookie', ['test=onetwo', 'test=onetwothree']) + res.end('Hello World!') + }).listen(0) + + await once(server, 'listening') + t.after(closeServerAsPromise(server)) + + const res = await fetch(`http://localhost:${server.address().port}`) + const entries = Object.fromEntries(res.headers.entries()) + + assert.deepStrictEqual(res.headers.getSetCookie(), ['test=onetwo', 'test=onetwothree']) + assert.ok('set-cookie' in entries) + }) }) test('When the value is updated, update the cache', (t) => {