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: cookies() .set() reflect priority attribute into set-cookie #640

Merged
merged 4 commits into from
Oct 15, 2023
Merged
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/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