Skip to content

Commit

Permalink
perf(headers): a single set-cookie (#2903)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx authored Mar 3, 2024
1 parent b4ebda2 commit 1216ba0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion benchmarks/fetch/headers-length32.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const headers = new Headers(
'Width',
'Accept-CH',
'Via',
'Refresh',
'Set-Cookie',
'Server',
'Sec-Fetch-Dest',
'Sec-CH-UA-Model',
Expand Down
2 changes: 1 addition & 1 deletion lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 17 additions & 1 deletion test/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
Expand All @@ -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) => {
Expand Down

0 comments on commit 1216ba0

Please sign in to comment.