Skip to content

Commit 87fa734

Browse files
authored
fix(headers): clone getSetCookie list & add getSetCookie type (#1917)
1 parent ba5ef44 commit 87fa734

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

lib/fetch/headers.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,13 @@ class Headers {
392392
// 2. Return the values of all headers in this’s header list whose name is
393393
// a byte-case-insensitive match for `Set-Cookie`, in order.
394394

395-
return this[kHeadersList].cookies ?? []
395+
const list = this[kHeadersList].cookies
396+
397+
if (list) {
398+
return [...list]
399+
}
400+
401+
return []
396402
}
397403

398404
// https://fetch.spec.whatwg.org/#concept-header-list-sort-and-combine

test/fetch/headers.js

+18
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,21 @@ tap.test('invalid headers', (t) => {
665665

666666
t.end()
667667
})
668+
669+
tap.test('Headers.prototype.getSetCookie', (t) => {
670+
t.test('Mutating the returned list does not affect the set-cookie list', (t) => {
671+
const h = new Headers([
672+
['set-cookie', 'a=b'],
673+
['set-cookie', 'c=d']
674+
])
675+
676+
const old = h.getSetCookie()
677+
h.getSetCookie().push('oh=no')
678+
const now = h.getSetCookie()
679+
680+
t.same(old, now)
681+
t.end()
682+
})
683+
684+
t.end()
685+
})

test/types/fetch.test-d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,6 @@ expectType<Response>(response.clone())
168168

169169
expectType<Request>(new Request('https://example.com', { body: 'Hello, world', duplex: 'half' }))
170170
expectAssignable<RequestInit>({ duplex: 'half' })
171-
expectNotAssignable<RequestInit>({ duplex: 'not valid' })
171+
expectNotAssignable<RequestInit>({ duplex: 'not valid' })
172+
173+
expectType<string[]>(headers.getSetCookie())

types/fetch.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export declare class Headers implements SpecIterable<[string, string]> {
5959
readonly get: (name: string) => string | null
6060
readonly has: (name: string) => boolean
6161
readonly set: (name: string, value: string) => void
62+
readonly getSetCookie: () => string[]
6263
readonly forEach: (
6364
callbackfn: (value: string, key: string, iterable: Headers) => void,
6465
thisArg?: unknown

0 commit comments

Comments
 (0)