Skip to content

Commit

Permalink
feat(utils/cookie): Ability to set a priority to cookies in setCookie…
Browse files Browse the repository at this point in the history
… options (#3762)

* Update cookie.ts

* Integrated `priority` option into setCookie serialization tests
  • Loading branch information
Beyondo authored Dec 28, 2024
1 parent efc8a46 commit fd6e5bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/utils/cookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ describe('Set cookie', () => {
maxAge: 1000,
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),
sameSite: 'Strict',
priority: 'High',
partitioned: true,
})
expect(serialized).toBe(
'__Secure-great_cookie=banana; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Partitioned'
'__Secure-great_cookie=banana; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Priority=High; Partitioned'
)
})

Expand All @@ -185,10 +186,11 @@ describe('Set cookie', () => {
maxAge: 1000,
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),
sameSite: 'Strict',
priority: 'High',
partitioned: true,
})
expect(serialized).toBe(
'__Host-great_cookie=banana; Max-Age=1000; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Partitioned'
'__Host-great_cookie=banana; Max-Age=1000; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Priority=High; Partitioned'
)
})

Expand All @@ -210,10 +212,11 @@ describe('Set cookie', () => {
maxAge: 1000,
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),
sameSite: 'Strict',
priority: 'High',
partitioned: true,
})
expect(serialized).toBe(
'great_cookie=banana.hSo6gB7YT2db0WBiEAakEmh7dtwEL0DSp76G23WvHuQ%3D; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Partitioned'
'great_cookie=banana.hSo6gB7YT2db0WBiEAakEmh7dtwEL0DSp76G23WvHuQ%3D; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Priority=High; Partitioned'
)
})

Expand Down
5 changes: 5 additions & 0 deletions src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type CookieOptions = {
signingSecret?: string
sameSite?: 'Strict' | 'Lax' | 'None' | 'strict' | 'lax' | 'none'
partitioned?: boolean
priority?: 'Low' | 'Medium' | 'High'
prefix?: CookiePrefixOptions
} & PartitionedCookieConstraint
export type CookiePrefixOptions = 'host' | 'secure'
Expand Down Expand Up @@ -204,6 +205,10 @@ const _serialize = (name: string, value: string, opt: CookieOptions = {}): strin
cookie += `; SameSite=${opt.sameSite.charAt(0).toUpperCase() + opt.sameSite.slice(1)}`
}

if (opt.priority) {
cookie += `; Priority=${opt.priority}`
}

if (opt.partitioned) {
// FIXME: replace link to RFC
// https://www.ietf.org/archive/id/draft-cutler-httpbis-partitioned-cookies-01.html#section-2.3
Expand Down

0 comments on commit fd6e5bd

Please sign in to comment.