From 87fa73498d6014a33989179cfaa4347dcb29600f Mon Sep 17 00:00:00 2001 From: Khafra Date: Thu, 9 Feb 2023 00:15:58 -0500 Subject: [PATCH] fix(headers): clone getSetCookie list & add getSetCookie type (#1917) --- lib/fetch/headers.js | 8 +++++++- test/fetch/headers.js | 18 ++++++++++++++++++ test/types/fetch.test-d.ts | 4 +++- types/fetch.d.ts | 1 + 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/fetch/headers.js b/lib/fetch/headers.js index af2c64bad8d..634b81fc74a 100644 --- a/lib/fetch/headers.js +++ b/lib/fetch/headers.js @@ -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 diff --git a/test/fetch/headers.js b/test/fetch/headers.js index f4508d01fd9..7ae85b815b5 100644 --- a/test/fetch/headers.js +++ b/test/fetch/headers.js @@ -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() +}) diff --git a/test/types/fetch.test-d.ts b/test/types/fetch.test-d.ts index a76d6d7043c..e11296aa85c 100644 --- a/test/types/fetch.test-d.ts +++ b/test/types/fetch.test-d.ts @@ -168,4 +168,6 @@ expectType(response.clone()) expectType(new Request('https://example.com', { body: 'Hello, world', duplex: 'half' })) expectAssignable({ duplex: 'half' }) -expectNotAssignable({ duplex: 'not valid' }) \ No newline at end of file +expectNotAssignable({ duplex: 'not valid' }) + +expectType(headers.getSetCookie()) diff --git a/types/fetch.d.ts b/types/fetch.d.ts index 58bd4d2a769..fa4619c9182 100644 --- a/types/fetch.d.ts +++ b/types/fetch.d.ts @@ -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