Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(headers): a single set-cookie #2903

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
tsctx marked this conversation as resolved.
Show resolved Hide resolved
'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
Loading