Skip to content

Commit

Permalink
fix(headers): clone getSetCookie list & add getSetCookie type (#1917)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored Feb 9, 2023
1 parent ba5ef44 commit 87fa734
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,13 @@ class Headers {
// 2. Return the values of all headers in this’s header list whose name is
// a byte-case-insensitive match for `Set-Cookie`, in order.

return this[kHeadersList].cookies ?? []
const list = this[kHeadersList].cookies

if (list) {
return [...list]
}

return []
}

// https://fetch.spec.whatwg.org/#concept-header-list-sort-and-combine
Expand Down
18 changes: 18 additions & 0 deletions test/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,21 @@ tap.test('invalid headers', (t) => {

t.end()
})

tap.test('Headers.prototype.getSetCookie', (t) => {
t.test('Mutating the returned list does not affect the set-cookie list', (t) => {
const h = new Headers([
['set-cookie', 'a=b'],
['set-cookie', 'c=d']
])

const old = h.getSetCookie()
h.getSetCookie().push('oh=no')
const now = h.getSetCookie()

t.same(old, now)
t.end()
})

t.end()
})
4 changes: 3 additions & 1 deletion test/types/fetch.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,6 @@ expectType<Response>(response.clone())

expectType<Request>(new Request('https://example.com', { body: 'Hello, world', duplex: 'half' }))
expectAssignable<RequestInit>({ duplex: 'half' })
expectNotAssignable<RequestInit>({ duplex: 'not valid' })
expectNotAssignable<RequestInit>({ duplex: 'not valid' })

expectType<string[]>(headers.getSetCookie())
1 change: 1 addition & 0 deletions types/fetch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export declare class Headers implements SpecIterable<[string, string]> {
readonly get: (name: string) => string | null
readonly has: (name: string) => boolean
readonly set: (name: string, value: string) => void
readonly getSetCookie: () => string[]
readonly forEach: (
callbackfn: (value: string, key: string, iterable: Headers) => void,
thisArg?: unknown
Expand Down

0 comments on commit 87fa734

Please sign in to comment.