Skip to content

Commit ee90121

Browse files
committed
fix!: Use Headers.prototype.getSetCookie to avoid comma-splitting
Since Node.js now has `getSetCookie` available, we can use it instead of splitting the header on comma, which was very error-prone. Ref: nodejs/undici#1915 The linked PR was released as Undici v5.19.0, which is first available in Node.js 18.14.1. Since Node.js 18.14.2 has an even more up-tp-date version of Undici (5.20.0), I figured it'd be sensible to raise our requirement to that. BREAKING CHANGE: Engine version requirement raised to Node `>=18.14.2`.
1 parent 286ba27 commit ee90121

File tree

4 files changed

+7
-9
lines changed

4 files changed

+7
-9
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ A stateful HTTP client for NodeJS. Test your routes even when mocking is infeasi
88

99
## Usage
1010

11-
This package requires Node version 18 or later because it makes use of native `fetch()`.
11+
This package requires Node version 18.14.2 or later because it makes use of native `fetch()`,
12+
as well as `Headers.prototype.getSetCookie`.
13+
1214
Additionally, it is packaged as an ES module only, so your tests have to be in ESM format, too.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"prepack": "npm run build"
1818
},
1919
"engines": {
20-
"node": ">=18"
20+
"node": ">=18.14.2"
2121
},
2222
"repository": {
2323
"type": "git",

src/cookies.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ export function cookieMiddleware (store: CookieStore): Middleware {
1919
}
2020

2121
function parseCookies (headers: Headers): Cookie[] {
22-
// Node's Headers implementation returns all Set-Cookie headers joined by ', '
23-
// TODO: Since dates are represented like "Wed, 12-Jul-2023 19:59:03 GMT", this will split too often!
24-
const cookieDefinitions = headers.get('set-cookie')?.trim().split(/\s*,\s*/) ?? []
25-
2622
const cookies = []
27-
for (const cookieDefinition of cookieDefinitions) {
23+
// Note: Headers.prototype.getSetCookie() is available since Node.js 18.14.1, but types aren't updated yet.
24+
for (const cookieDefinition of (headers as any).getSetCookie()) {
2825
// TODO: Handle malformed strings, quoted values, escaping.
2926
// TODO: Parse expiration date and flags.
3027
const key = cookieDefinition.match(/^([^=\s]+)/)?.[1]
@@ -33,6 +30,5 @@ function parseCookies (headers: Headers): Cookie[] {
3330
cookies.push({ key, value })
3431
}
3532
}
36-
3733
return cookies
3834
}

0 commit comments

Comments
 (0)