@fastify/cookie 9.3.0 → 9.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -151,6 +151,20 @@ attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not.
151
151
 
152
152
  More information about can be found in [the proposal](https://github.com/privacycg/CHIPS).
153
153
 
154
+
155
+ ##### priority
156
+
157
+ 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).
158
+
159
+ - `'low'` will set the `Priority` attribute to `Low`.
160
+ - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
161
+ - `'high'` will set the `Priority` attribute to `High`.
162
+
163
+ More information about the different priority levels can be found in
164
+ [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
165
+
166
+ ⚠️ **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.
167
+
154
168
  ##### path
155
169
 
156
170
  Specifies the value for the [`Path` `Set-Cookie` attribute](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.4). By default, the path
package/cookie.js CHANGED
@@ -152,6 +152,26 @@ function serialize (name, val, opt) {
152
152
  str += '; Path=' + opt.path
153
153
  }
154
154
 
155
+ if (opt.priority) {
156
+ const priority = typeof opt.priority === 'string'
157
+ ? opt.priority.toLowerCase()
158
+ : opt.priority
159
+
160
+ switch (priority) {
161
+ case 'low':
162
+ str += '; Priority=Low'
163
+ break
164
+ case 'medium':
165
+ str += '; Priority=Medium'
166
+ break
167
+ case 'high':
168
+ str += '; Priority=High'
169
+ break
170
+ default:
171
+ throw new TypeError('option priority is invalid')
172
+ }
173
+ }
174
+
155
175
  if (opt.expires) {
156
176
  if (typeof opt.expires.toUTCString !== 'function') {
157
177
  throw new TypeError('option expires is invalid')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/cookie",
3
- "version": "9.3.0",
3
+ "version": "9.3.1",
4
4
  "description": "Plugin for fastify to add support for cookies",
5
5
  "main": "plugin.js",
6
6
  "type": "commonjs",
@@ -203,3 +203,21 @@ test('unencoded', (t) => {
203
203
  }), /argument val is invalid/)
204
204
  t.end()
205
205
  })
206
+
207
+ test('serializer: priority', (t) => {
208
+ t.plan(8)
209
+ t.same(cookie.serialize('foo', 'bar', { priority: 'Low' }), 'foo=bar; Priority=Low')
210
+ t.same(cookie.serialize('foo', 'bar', { priority: 'low' }), 'foo=bar; Priority=Low')
211
+ t.same(cookie.serialize('foo', 'bar', { priority: 'Medium' }), 'foo=bar; Priority=Medium')
212
+ t.same(cookie.serialize('foo', 'bar', { priority: 'medium' }), 'foo=bar; Priority=Medium')
213
+ t.same(cookie.serialize('foo', 'bar', { priority: 'High' }), 'foo=bar; Priority=High')
214
+ t.same(cookie.serialize('foo', 'bar', { priority: 'high' }), 'foo=bar; Priority=High')
215
+
216
+ t.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
217
+ priority: 'foo'
218
+ }), /option priority is invalid/)
219
+ t.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
220
+ priority: true
221
+ }), /option priority is invalid/)
222
+ t.end()
223
+ })
package/types/plugin.d.ts CHANGED
@@ -115,18 +115,21 @@ declare namespace fastifyCookie {
115
115
  domain?: string;
116
116
  /** 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. */
117
117
  encode?(val: string): string;
118
- /** The expiration `date` used for the `Expires` attribute. If both `expires` and `maxAge` are set, then `expires` is used. */
118
+ /** The expiration `date` used for the `Expires` attribute. If both `expires` and `maxAge` are set, then `expires` is used. */
119
119
  expires?: Date;
120
- /** The `boolean` value of the `HttpOnly` attribute. Defaults to true. */
120
+ /** The `boolean` value of the `HttpOnly` attribute. Defaults to true. */
121
121
  httpOnly?: boolean;
122
- /** 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. */
122
+ /** 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. */
123
123
  maxAge?: number;
124
+ /** A `boolean` indicating whether the cookie is tied to the top-level site where it's initially set and cannot be accessed from elsewhere. */
124
125
  partitioned?: boolean;
125
- /** The `Path` attribute. Defaults to `/` (the root path). */
126
+ /** The `Path` attribute. Defaults to `/` (the root path). */
126
127
  path?: string;
127
128
  /** A `boolean` or one of the `SameSite` string attributes. E.g.: `lax`, `none` or `strict`. */
128
129
  sameSite?: 'lax' | 'none' | 'strict' | boolean;
129
- /** 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. */
130
+ /** 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. */
131
+ priority?: 'low' | 'medium' | 'high';
132
+ /** 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. */
130
133
  secure?: boolean;
131
134
  }
132
135