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

fix: parse max-age in set cookie #995

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/big-pumpkins-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@edge-runtime/cookies': patch
---

Fix parsing max-age in set cookie
9 changes: 7 additions & 2 deletions packages/cookies/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export function stringifyCookie(c: ResponseCookie | RequestCookie): string {
].filter(Boolean)

const stringified = `${c.name}=${encodeURIComponent(c.value ?? '')}`
return attrs.length === 0 ? stringified : `${stringified}; ${attrs.join('; ')}`
return attrs.length === 0
? stringified
: `${stringified}; ${attrs.join('; ')}`
}

/** Parse a `Cookie` header value */
Expand Down Expand Up @@ -68,7 +70,10 @@ export function parseSetCookie(setCookie: string): undefined | ResponseCookie {
partitioned,
priority,
} = Object.fromEntries(
attributes.map(([key, value]) => [key.toLowerCase(), value]),
attributes.map(([key, value]) => [
key.toLowerCase().replace(/-/g, ''),
value,
]),
)
const cookie: ResponseCookie = {
name,
Expand Down
9 changes: 9 additions & 0 deletions packages/cookies/test/response-cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,12 @@ test('splitting multiple set-cookie', () => {
expect(cookies2.get('foo')?.value).toBe(undefined)
expect(cookies2.get('fooz')?.value).toBe('barz')
})

test('parse max-age from set-cookie', () => {
const headers = new Headers()
headers.set('set-cookie', 'foo=bar; Max-Age=1000')

const cookies = new ResponseCookies(headers)
expect(cookies.get('foo')?.value).toBe('bar')
expect(cookies.get('foo')?.maxAge).toBe(1000)
})