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

feat(utils/cookie): Ability to set a priority to cookies in setCookie options #3762

Open
wants to merge 2 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
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 @@
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 @@
cookie += `; SameSite=${opt.sameSite.charAt(0).toUpperCase() + opt.sameSite.slice(1)}`
}

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

Check warning on line 211 in src/utils/cookie.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/cookie.ts#L211

Added line #L211 was not covered by tests
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
Loading