Skip to content

Commit

Permalink
fix: cookies() .set() reflect priority attribute into set-cookie (#640)
Browse files Browse the repository at this point in the history
Co-authored-by: Balázs Orbán <info@balazsorban.com>
  • Loading branch information
renchris and balazsorban44 authored Oct 15, 2023
1 parent 4dab7cd commit 5a8f35a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-eagles-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@edge-runtime/cookies': patch
---

fix cookies() .set() to reflect the priority attribute into set-cookie
27 changes: 23 additions & 4 deletions packages/cookies/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function stringifyCookie(c: ResponseCookie | RequestCookie): string {
'secure' in c && c.secure && 'Secure',
'httpOnly' in c && c.httpOnly && 'HttpOnly',
'sameSite' in c && c.sameSite && `SameSite=${c.sameSite}`,
'priority' in c && c.priority && `Priority=${c.priority}`,
].filter(Boolean)

return `${c.name}=${encodeURIComponent(c.value ?? '')}; ${attrs.join('; ')}`
Expand Down Expand Up @@ -54,10 +55,18 @@ export function parseSetCookie(setCookie: string): undefined | ResponseCookie {
}

const [[name, value], ...attributes] = parseCookie(setCookie)
const { domain, expires, httponly, maxage, path, samesite, secure } =
Object.fromEntries(
attributes.map(([key, value]) => [key.toLowerCase(), value]),
)
const {
domain,
expires,
httponly,
maxage,
path,
samesite,
secure,
priority,
} = Object.fromEntries(
attributes.map(([key, value]) => [key.toLowerCase(), value]),
)
const cookie: ResponseCookie = {
name,
value: decodeURIComponent(value),
Expand All @@ -68,6 +77,7 @@ export function parseSetCookie(setCookie: string): undefined | ResponseCookie {
path,
...(samesite && { sameSite: parseSameSite(samesite) }),
...(secure && { secure: true }),
...(priority && { priority: parsePriority(priority) }),
}

return compact(cookie)
Expand All @@ -91,3 +101,12 @@ function parseSameSite(string: string): ResponseCookie['sameSite'] {
? (string as ResponseCookie['sameSite'])
: undefined
}

const PRIORITY: ResponseCookie['priority'][] = ['low', 'medium', 'high']

function parsePriority(string: string): ResponseCookie['priority'] {
string = string.toLowerCase()
return PRIORITY.includes(string as any)
? (string as ResponseCookie['priority'])
: undefined
}
19 changes: 19 additions & 0 deletions packages/cookies/test/response-cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ test('reflect .set into `set-cookie`', async () => {
)
})

it('reflect .set all options attributes into `set-cookie`', async () => {
const headers = new Headers()
const cookies = new ResponseCookies(headers)
cookies.set('first-name', 'first-value', {
domain: 'custom-domain',
path: 'custom-path',
secure: true,
sameSite: 'strict',
expires: new Date(2100, 0, 1, 12, 0, 0),
httpOnly: true,
maxAge: 0,
priority: 'high',
})
const cookiesInHeaders = Object.fromEntries(headers.entries())['set-cookie']
expect(cookiesInHeaders).toBe(
'first-name=first-value; Path=custom-path; Expires=Fri, 01 Jan 2100 12:00:00 GMT; Max-Age=0; Domain=custom-domain; Secure; HttpOnly; SameSite=strict; Priority=high',
)
})

describe('`set-cookie` into .get and .getAll', () => {
test.each([
'name=value; Secure; HttpOnly',
Expand Down

1 comment on commit 5a8f35a

@vercel
Copy link

@vercel vercel bot commented on 5a8f35a Oct 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

edge-runtime – ./

edge-runtime.vercel.sh
edge-runtime-git-main.vercel.sh
edge-runtime.vercel.app

Please sign in to comment.