@fastify/cookie 10.0.0 → 11.0.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/cookie",
3
- "version": "10.0.0",
3
+ "version": "11.0.0",
4
4
  "description": "Plugin for fastify to add support for cookies",
5
5
  "main": "plugin.js",
6
6
  "type": "commonjs",
@@ -43,16 +43,17 @@
43
43
  "@fastify/pre-commit": "^2.1.0",
44
44
  "@types/node": "^22.0.0",
45
45
  "benchmark": "^2.1.4",
46
- "fastify": "^5.0.0-alpha.4",
47
- "sinon": "^18.0.0",
46
+ "fastify": "^5.0.0",
47
+ "sinon": "^19.0.2",
48
48
  "snazzy": "^9.0.0",
49
49
  "standard": "^17.1.0",
50
50
  "tap": "^18.6.1",
51
51
  "tsd": "^0.31.1"
52
52
  },
53
53
  "dependencies": {
54
- "fastify-plugin": "^5.0.0",
55
- "cookie-signature": "^1.2.1"
54
+ "cookie": "^1.0.0",
55
+ "cookie-signature": "^1.2.1",
56
+ "fastify-plugin": "^5.0.0"
56
57
  },
57
58
  "tsd": {
58
59
  "directory": "test"
package/plugin.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const fp = require('fastify-plugin')
4
- const cookie = require('./cookie')
4
+ const cookie = require('cookie')
5
5
 
6
6
  const { Signer, sign, unsign } = require('./signer')
7
7
 
@@ -40,9 +40,9 @@ function fastifyCookieSetCookie (reply, name, value, options) {
40
40
 
41
41
  function fastifyCookieClearCookie (reply, name, options) {
42
42
  const opts = Object.assign({ path: '/' }, options, {
43
- expires: new Date(1),
44
- signed: undefined,
45
- maxAge: undefined
43
+ expires: new Date(0),
44
+ maxAge: 0,
45
+ signed: false
46
46
  })
47
47
 
48
48
  return fastifyCookieSetCookie(reply, name, '', opts)
@@ -224,7 +224,7 @@ test('share options for setCookie and clearCookie', (t) => {
224
224
  t.equal(cookies.length, 1)
225
225
  t.equal(cookies[0].name, 'foo')
226
226
  t.equal(cookies[0].value, '')
227
- t.equal(cookies[0].maxAge, undefined)
227
+ t.equal(cookies[0].maxAge, 0)
228
228
 
229
229
  t.ok(new Date(cookies[0].expires) < new Date())
230
230
  })
@@ -1054,7 +1054,7 @@ test('clearCookie should include parseOptions', (t) => {
1054
1054
  t.equal(cookies.length, 1)
1055
1055
  t.equal(cookies[0].name, 'foo')
1056
1056
  t.equal(cookies[0].value, '')
1057
- t.equal(cookies[0].maxAge, undefined)
1057
+ t.equal(cookies[0].maxAge, 0)
1058
1058
  t.equal(cookies[0].path, '/test')
1059
1059
  t.equal(cookies[0].domain, 'example.com')
1060
1060
 
package/cookie.js DELETED
@@ -1,247 +0,0 @@
1
- /*!
2
- * Adapted from https://github.com/jshttp/cookie
3
- *
4
- * (The MIT License)
5
- *
6
- * Copyright (c) 2012-2014 Roman Shtylman <shtylman@gmail.com>
7
- * Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
8
- *
9
- * Permission is hereby granted, free of charge, to any person obtaining
10
- * a copy of this software and associated documentation files (the
11
- * 'Software'), to deal in the Software without restriction, including
12
- * without limitation the rights to use, copy, modify, merge, publish,
13
- * distribute, sublicense, and/or sell copies of the Software, and to
14
- * permit persons to whom the Software is furnished to do so, subject to
15
- * the following conditions:
16
- *
17
- * The above copyright notice and this permission notice shall be
18
- * included in all copies or substantial portions of the Software.
19
- *
20
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
21
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
- */
28
-
29
- 'use strict'
30
-
31
- /**
32
- * RegExp to match field-content in RFC 7230 sec 3.2
33
- *
34
- * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
35
- * field-vchar = VCHAR / obs-text
36
- * obs-text = %x80-FF
37
- */
38
-
39
- const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/ // eslint-disable-line
40
-
41
- /**
42
- * Parse a cookie header.
43
- *
44
- * Parse the given cookie header string into an object
45
- * The object has the various cookies as keys(names) => values
46
- *
47
- * @param {string} str
48
- * @param {object} [opt]
49
- * @return {object}
50
- * @public
51
- */
52
-
53
- function parse (str, opt) {
54
- if (typeof str !== 'string') {
55
- throw new TypeError('argument str must be a string')
56
- }
57
-
58
- const dec = opt?.decode || decodeURIComponent
59
- const result = {}
60
-
61
- const strLen = str.length
62
- let pos = 0
63
- let terminatorPos = 0
64
- while (true) {
65
- if (terminatorPos === strLen) break
66
- terminatorPos = str.indexOf(';', pos)
67
- if (terminatorPos === -1) terminatorPos = strLen // This is the last pair
68
-
69
- let eqIdx = str.indexOf('=', pos)
70
- if (eqIdx === -1) break // No key-value pairs left
71
- if (eqIdx > terminatorPos) {
72
- // Malformed key-value pair
73
- pos = terminatorPos + 1
74
- continue
75
- }
76
-
77
- const key = str.substring(pos, eqIdx++).trim()
78
- if (result[key] === undefined) {
79
- const val = str.charCodeAt(eqIdx) === 0x22
80
- ? str.substring(eqIdx + 1, terminatorPos - 1).trim()
81
- : str.substring(eqIdx, terminatorPos).trim()
82
-
83
- result[key] = !(dec === decodeURIComponent && val.indexOf('%') === -1)
84
- ? tryDecode(val, dec)
85
- : val
86
- }
87
-
88
- pos = terminatorPos + 1
89
- }
90
-
91
- return result
92
- }
93
-
94
- /**
95
- * Serialize data into a cookie header.
96
- *
97
- * Serialize the a name value pair into a cookie string suitable for
98
- * http headers. An optional options object specified cookie parameters.
99
- *
100
- * serialize('foo', 'bar', { httpOnly: true })
101
- * => "foo=bar; httpOnly"
102
- *
103
- * @param {string} name
104
- * @param {string} val
105
- * @param {object} [opt]
106
- * @return {string}
107
- * @public
108
- */
109
-
110
- function serialize (name, val, opt) {
111
- const enc = opt?.encode || encodeURIComponent
112
- if (typeof enc !== 'function') {
113
- throw new TypeError('option encode is invalid')
114
- }
115
-
116
- if (name && !fieldContentRegExp.test(name)) {
117
- throw new TypeError('argument name is invalid')
118
- }
119
-
120
- const value = enc(val)
121
- if (value && !fieldContentRegExp.test(value)) {
122
- throw new TypeError('argument val is invalid')
123
- }
124
-
125
- let str = name + '=' + value
126
-
127
- if (opt == null) return str
128
-
129
- if (opt.maxAge != null) {
130
- const maxAge = +opt.maxAge
131
-
132
- if (!isFinite(maxAge)) {
133
- throw new TypeError('option maxAge is invalid')
134
- }
135
-
136
- str += '; Max-Age=' + Math.trunc(maxAge)
137
- }
138
-
139
- if (opt.domain) {
140
- if (!fieldContentRegExp.test(opt.domain)) {
141
- throw new TypeError('option domain is invalid')
142
- }
143
-
144
- str += '; Domain=' + opt.domain
145
- }
146
-
147
- if (opt.path) {
148
- if (!fieldContentRegExp.test(opt.path)) {
149
- throw new TypeError('option path is invalid')
150
- }
151
-
152
- str += '; Path=' + opt.path
153
- }
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
-
175
- if (opt.expires) {
176
- if (typeof opt.expires.toUTCString !== 'function') {
177
- throw new TypeError('option expires is invalid')
178
- }
179
-
180
- str += '; Expires=' + opt.expires.toUTCString()
181
- }
182
-
183
- if (opt.httpOnly) {
184
- str += '; HttpOnly'
185
- }
186
-
187
- if (opt.secure) {
188
- str += '; Secure'
189
- }
190
-
191
- // Draft implementation to support Chrome from 2024-Q1 forward.
192
- // See https://datatracker.ietf.org/doc/html/draft-cutler-httpbis-partitioned-cookies#section-2.1
193
- if (opt.partitioned) {
194
- str += '; Partitioned'
195
- }
196
-
197
- if (opt.sameSite) {
198
- const sameSite = typeof opt.sameSite === 'string'
199
- ? opt.sameSite.toLowerCase()
200
- : opt.sameSite
201
-
202
- switch (sameSite) {
203
- case true:
204
- str += '; SameSite=Strict'
205
- break
206
- case 'lax':
207
- str += '; SameSite=Lax'
208
- break
209
- case 'strict':
210
- str += '; SameSite=Strict'
211
- break
212
- case 'none':
213
- str += '; SameSite=None'
214
- break
215
- default:
216
- throw new TypeError('option sameSite is invalid')
217
- }
218
- }
219
-
220
- return str
221
- }
222
-
223
- /**
224
- * Try decoding a string using a decoding function.
225
- *
226
- * @param {string} str
227
- * @param {function} decode
228
- * @returns {string}
229
- * @private
230
- */
231
- function tryDecode (str, decode) {
232
- try {
233
- return decode(str)
234
- } catch {
235
- return str
236
- }
237
- }
238
-
239
- /**
240
- * Module exports.
241
- * @public
242
- */
243
-
244
- module.exports = {
245
- parse,
246
- serialize
247
- }
@@ -1,223 +0,0 @@
1
- 'use strict'
2
-
3
- const tap = require('tap')
4
- const test = tap.test
5
-
6
- const cookie = require('..')
7
-
8
- test('parse: argument validation', (t) => {
9
- t.plan(2)
10
- t.throws(cookie.parse.bind(), /argument str must be a string/)
11
- t.throws(cookie.parse.bind(null, 42), /argument str must be a string/)
12
- t.end()
13
- })
14
-
15
- test('parse: basic', (t) => {
16
- t.plan(2)
17
- t.same(cookie.parse('foo=bar'), { foo: 'bar' })
18
- t.same(cookie.parse('foo=123'), { foo: '123' })
19
- t.end()
20
- })
21
-
22
- test('parse: ignore spaces', (t) => {
23
- t.plan(1)
24
- t.same(cookie.parse('FOO = bar; baz = raz'), { FOO: 'bar', baz: 'raz' })
25
- t.end()
26
- })
27
-
28
- test('parse: escaping', (t) => {
29
- t.plan(2)
30
- t.same(cookie.parse('foo="bar=123456789&name=Magic+Mouse"'), { foo: 'bar=123456789&name=Magic+Mouse' })
31
- t.same(cookie.parse('email=%20%22%2c%3b%2f'), { email: ' ",;/' })
32
- t.end()
33
- })
34
-
35
- test('parse: ignore escaping error and return original value', (t) => {
36
- t.plan(1)
37
- t.same(cookie.parse('foo=%1;bar=bar'), { foo: '%1', bar: 'bar' })
38
- t.end()
39
- })
40
-
41
- test('parse: ignore non values', (t) => {
42
- t.plan(1)
43
- t.same(cookie.parse('foo=%1;bar=bar;HttpOnly;Secure'),
44
- { foo: '%1', bar: 'bar' })
45
- t.end()
46
- })
47
-
48
- test('parse: unencoded', (t) => {
49
- t.plan(2)
50
- t.same(cookie.parse('foo="bar=123456789&name=Magic+Mouse"', {
51
- decode: function (v) { return v }
52
- }), { foo: 'bar=123456789&name=Magic+Mouse' })
53
-
54
- t.same(cookie.parse('email=%20%22%2c%3b%2f', {
55
- decode: function (v) { return v }
56
- }), { email: '%20%22%2c%3b%2f' })
57
- t.end()
58
- })
59
-
60
- test('parse: dates', (t) => {
61
- t.plan(1)
62
- t.same(cookie.parse('priority=true; expires=Wed, 29 Jan 2014 17:43:25 GMT; Path=/', {
63
- decode: function (v) { return v }
64
- }), { priority: 'true', Path: '/', expires: 'Wed, 29 Jan 2014 17:43:25 GMT' })
65
- t.end()
66
- })
67
-
68
- test('parse: missing value', (t) => {
69
- t.plan(1)
70
- t.same(cookie.parse('foo; bar=1; fizz= ; buzz=2', {
71
- decode: function (v) { return v }
72
- }), { bar: '1', fizz: '', buzz: '2' })
73
- t.end()
74
- })
75
-
76
- test('parse: assign only once', (t) => {
77
- t.plan(3)
78
- t.same(cookie.parse('foo=%1;bar=bar;foo=boo'), { foo: '%1', bar: 'bar' })
79
- t.same(cookie.parse('foo=false;bar=bar;foo=true'), { foo: 'false', bar: 'bar' })
80
- t.same(cookie.parse('foo=;bar=bar;foo=boo'), { foo: '', bar: 'bar' })
81
- t.end()
82
- })
83
-
84
- test('serializer: basic', (t) => {
85
- t.plan(6)
86
- t.same(cookie.serialize('foo', 'bar'), 'foo=bar')
87
- t.same(cookie.serialize('foo', 'bar baz'), 'foo=bar%20baz')
88
- t.same(cookie.serialize('foo', ''), 'foo=')
89
- t.throws(cookie.serialize.bind(cookie, 'foo\n', 'bar'), /argument name is invalid/)
90
- t.throws(cookie.serialize.bind(cookie, 'foo\u280a', 'bar'), /argument name is invalid/)
91
- t.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { encode: 42 }), /option encode is invalid/)
92
- t.end()
93
- })
94
-
95
- test('serializer: path', (t) => {
96
- t.plan(2)
97
- t.same(cookie.serialize('foo', 'bar', { path: '/' }), 'foo=bar; Path=/')
98
- t.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
99
- path: '/\n'
100
- }), /option path is invalid/)
101
- t.end()
102
- })
103
-
104
- test('serializer: secure', (t) => {
105
- t.plan(2)
106
- t.same(cookie.serialize('foo', 'bar', { secure: true }), 'foo=bar; Secure')
107
- t.same(cookie.serialize('foo', 'bar', { secure: false }), 'foo=bar')
108
- t.end()
109
- })
110
-
111
- test('serializer: domain', (t) => {
112
- t.plan(2)
113
- t.same(cookie.serialize('foo', 'bar', { domain: 'example.com' }), 'foo=bar; Domain=example.com')
114
- t.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
115
- domain: 'example.com\n'
116
- }), /option domain is invalid/)
117
- t.end()
118
- })
119
-
120
- test('serializer: httpOnly', (t) => {
121
- t.plan(1)
122
- t.same(cookie.serialize('foo', 'bar', { httpOnly: true }), 'foo=bar; HttpOnly')
123
- t.end()
124
- })
125
-
126
- test('serializer: maxAge', (t) => {
127
- t.plan(9)
128
- t.throws(function () {
129
- cookie.serialize('foo', 'bar', {
130
- maxAge: 'buzz'
131
- })
132
- }, /option maxAge is invalid/)
133
-
134
- t.throws(function () {
135
- cookie.serialize('foo', 'bar', {
136
- maxAge: Infinity
137
- })
138
- }, /option maxAge is invalid/)
139
-
140
- t.same(cookie.serialize('foo', 'bar', { maxAge: 1000 }), 'foo=bar; Max-Age=1000')
141
- t.same(cookie.serialize('foo', 'bar', { maxAge: '1000' }), 'foo=bar; Max-Age=1000')
142
- t.same(cookie.serialize('foo', 'bar', { maxAge: 0 }), 'foo=bar; Max-Age=0')
143
- t.same(cookie.serialize('foo', 'bar', { maxAge: '0' }), 'foo=bar; Max-Age=0')
144
- t.same(cookie.serialize('foo', 'bar', { maxAge: null }), 'foo=bar')
145
- t.same(cookie.serialize('foo', 'bar', { maxAge: undefined }), 'foo=bar')
146
- t.same(cookie.serialize('foo', 'bar', { maxAge: 3.14 }), 'foo=bar; Max-Age=3')
147
- t.end()
148
- })
149
-
150
- test('serializer: expires', (t) => {
151
- t.plan(2)
152
- t.same(cookie.serialize('foo', 'bar', {
153
- expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900))
154
- }), 'foo=bar; Expires=Sun, 24 Dec 2000 10:30:59 GMT')
155
-
156
- t.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
157
- expires: Date.now()
158
- }), /option expires is invalid/)
159
- t.end()
160
- })
161
-
162
- test('sameSite', (t) => {
163
- t.plan(9)
164
- t.same(cookie.serialize('foo', 'bar', { sameSite: true }), 'foo=bar; SameSite=Strict')
165
- t.same(cookie.serialize('foo', 'bar', { sameSite: 'Strict' }), 'foo=bar; SameSite=Strict')
166
- t.same(cookie.serialize('foo', 'bar', { sameSite: 'strict' }), 'foo=bar; SameSite=Strict')
167
- t.same(cookie.serialize('foo', 'bar', { sameSite: 'Lax' }), 'foo=bar; SameSite=Lax')
168
- t.same(cookie.serialize('foo', 'bar', { sameSite: 'lax' }), 'foo=bar; SameSite=Lax')
169
- t.same(cookie.serialize('foo', 'bar', { sameSite: 'None' }), 'foo=bar; SameSite=None')
170
- t.same(cookie.serialize('foo', 'bar', { sameSite: 'none' }), 'foo=bar; SameSite=None')
171
- t.same(cookie.serialize('foo', 'bar', { sameSite: false }), 'foo=bar')
172
-
173
- t.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
174
- sameSite: 'foo'
175
- }), /option sameSite is invalid/)
176
- t.end()
177
- })
178
-
179
- test('escaping', (t) => {
180
- t.plan(1)
181
- t.same(cookie.serialize('cat', '+ '), 'cat=%2B%20')
182
- t.end()
183
- })
184
-
185
- test('parse->serialize', (t) => {
186
- t.plan(2)
187
- t.same(cookie.parse(cookie.serialize('cat', 'foo=123&name=baz five')),
188
- { cat: 'foo=123&name=baz five' })
189
-
190
- t.same(cookie.parse(cookie.serialize('cat', ' ";/')),
191
- { cat: ' ";/' })
192
- t.end()
193
- })
194
-
195
- test('unencoded', (t) => {
196
- t.plan(2)
197
- t.same(cookie.serialize('cat', '+ ', {
198
- encode: function (value) { return value }
199
- }), 'cat=+ ')
200
-
201
- t.throws(cookie.serialize.bind(cookie, 'cat', '+ \n', {
202
- encode: function (value) { return value }
203
- }), /argument val is invalid/)
204
- t.end()
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
- })