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: add priority to serializer #275

Merged
merged 3 commits into from
Jan 17, 2024
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not.

More information about can be found in [the proposal](https://github.com/privacycg/CHIPS).


##### priority

Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).

- `'low'` will set the `Priority` attribute to `Low`.
- `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
- `'high'` will set the `Priority` attribute to `High`.

More information about the different priority levels can be found in
[the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).

⚠️ **Warning:** This is an attribute that has not yet been fully standardized, and may change in the future without reflecting the semver versioning. This also means many clients may ignore the attribute until they understand it.

##### path

Specifies the value for the [`Path` `Set-Cookie` attribute](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.4). By default, the path
Expand Down
20 changes: 20 additions & 0 deletions cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ function serialize (name, val, opt) {
str += '; Path=' + opt.path
}

if (opt.priority) {
const priority = typeof opt.priority === 'string'
? opt.priority.toLowerCase()
: opt.priority

switch (priority) {
case 'low':
str += '; Priority=Low'
break
case 'medium':
str += '; Priority=Medium'
break
case 'high':
str += '; Priority=High'
break
default:
throw new TypeError('option priority is invalid')
}
}

if (opt.expires) {
if (typeof opt.expires.toUTCString !== 'function') {
throw new TypeError('option expires is invalid')
Expand Down
18 changes: 18 additions & 0 deletions test/cookie-module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,21 @@ test('unencoded', (t) => {
}), /argument val is invalid/)
t.end()
})

test('serializer: priority', (t) => {
t.plan(8)
t.same(cookie.serialize('foo', 'bar', { priority: 'Low' }), 'foo=bar; Priority=Low')
t.same(cookie.serialize('foo', 'bar', { priority: 'low' }), 'foo=bar; Priority=Low')
t.same(cookie.serialize('foo', 'bar', { priority: 'Medium' }), 'foo=bar; Priority=Medium')
t.same(cookie.serialize('foo', 'bar', { priority: 'medium' }), 'foo=bar; Priority=Medium')
t.same(cookie.serialize('foo', 'bar', { priority: 'High' }), 'foo=bar; Priority=High')
t.same(cookie.serialize('foo', 'bar', { priority: 'high' }), 'foo=bar; Priority=High')

t.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
priority: 'foo'
}), /option priority is invalid/)
t.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
priority: true
}), /option priority is invalid/)
t.end()
})
13 changes: 8 additions & 5 deletions types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,21 @@ declare namespace fastifyCookie {
domain?: string;
/** Specifies a function that will be used to encode a cookie's value. Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode a value into a string suited for a cookie's value. */
encode?(val: string): string;
/** The expiration `date` used for the `Expires` attribute. If both `expires` and `maxAge` are set, then `expires` is used. */
/** The expiration `date` used for the `Expires` attribute. If both `expires` and `maxAge` are set, then `expires` is used. */
expires?: Date;
/** The `boolean` value of the `HttpOnly` attribute. Defaults to true. */
/** The `boolean` value of the `HttpOnly` attribute. Defaults to true. */
httpOnly?: boolean;
/** A `number` in seconds that specifies the `Expires` attribute by adding the specified seconds to the current date. If both `expires` and `maxAge` are set, then `expires` is used. */
/** A `number` in seconds that specifies the `Expires` attribute by adding the specified seconds to the current date. If both `expires` and `maxAge` are set, then `expires` is used. */
maxAge?: number;
/** A `boolean` indicating whether the cookie is tied to the top-level site where it's initially set and cannot be accessed from elsewhere. */
partitioned?: boolean;
/** The `Path` attribute. Defaults to `/` (the root path). */
/** The `Path` attribute. Defaults to `/` (the root path). */
path?: string;
/** A `boolean` or one of the `SameSite` string attributes. E.g.: `lax`, `none` or `strict`. */
sameSite?: 'lax' | 'none' | 'strict' | boolean;
/** The `boolean` value of the `Secure` attribute. Set this option to false when communicating over an unencrypted (HTTP) connection. Value can be set to `auto`; in this case the `Secure` attribute will be set to false for HTTP request, in case of HTTPS it will be set to true. Defaults to true. */
/** One of the `Priority` string attributes (`low`, `medium` or `high`) specifying a retention priority for HTTP cookies that will be respected by user agents during cookie eviction. */
priority?: 'low' | 'medium' | 'high';
/** The `boolean` value of the `Secure` attribute. Set this option to false when communicating over an unencrypted (HTTP) connection. Value can be set to `auto`; in this case the `Secure` attribute will be set to false for HTTP request, in case of HTTPS it will be set to true. Defaults to true. */
secure?: boolean;
}

Expand Down
Loading