@fastify/cookie 9.0.3 → 9.1.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 +3 -3
- package/plugin.js +22 -25
- package/signer.js +1 -1
- package/test/cookie.test.js +23 -2
- package/test/signer.test.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/cookie",
|
|
3
|
-
"version": "9.0
|
|
3
|
+
"version": "9.1.0",
|
|
4
4
|
"description": "Plugin for fastify to add support for cookies",
|
|
5
5
|
"main": "plugin.js",
|
|
6
6
|
"types": "types/plugin.d.ts",
|
|
@@ -43,11 +43,11 @@
|
|
|
43
43
|
"@types/node": "^20.1.0",
|
|
44
44
|
"benchmark": "^2.1.4",
|
|
45
45
|
"fastify": "^4.0.0",
|
|
46
|
-
"sinon": "^
|
|
46
|
+
"sinon": "^16.0.0",
|
|
47
47
|
"snazzy": "^9.0.0",
|
|
48
48
|
"standard": "^17.0.0",
|
|
49
49
|
"tap": "^16.0.0",
|
|
50
|
-
"tsd": "^0.
|
|
50
|
+
"tsd": "^0.29.0"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"cookie": "^0.5.0",
|
package/plugin.js
CHANGED
|
@@ -9,7 +9,7 @@ const kReplySetCookies = Symbol('fastify.reply.setCookies')
|
|
|
9
9
|
const kReplySetCookiesHookRan = Symbol('fastify.reply.setCookiesHookRan')
|
|
10
10
|
|
|
11
11
|
function fastifyCookieSetCookie (reply, name, value, options) {
|
|
12
|
-
parseCookies(reply.
|
|
12
|
+
parseCookies(reply.server, reply.request, reply)
|
|
13
13
|
|
|
14
14
|
const opts = Object.assign({}, options)
|
|
15
15
|
|
|
@@ -51,11 +51,9 @@ function fastifyCookieClearCookie (reply, name, options) {
|
|
|
51
51
|
function parseCookies (fastify, request, reply) {
|
|
52
52
|
if (reply[kReplySetCookies]) return
|
|
53
53
|
|
|
54
|
-
request.cookies = {} // New container per request. Issue #53
|
|
55
54
|
const cookieHeader = request.raw.headers.cookie
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
55
|
+
|
|
56
|
+
request.cookies = cookieHeader ? fastify.parseCookie(cookieHeader) : {} // New container per request. Issue #53
|
|
59
57
|
reply[kReplySetCookies] = new Map()
|
|
60
58
|
}
|
|
61
59
|
|
|
@@ -102,6 +100,11 @@ function setCookies (reply) {
|
|
|
102
100
|
}
|
|
103
101
|
|
|
104
102
|
function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
|
|
103
|
+
if (!fastifyRes[kReplySetCookies]) {
|
|
104
|
+
done()
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
105
108
|
if (fastifyRes[kReplySetCookies].size) {
|
|
106
109
|
setCookies(fastifyRes)
|
|
107
110
|
}
|
|
@@ -111,18 +114,6 @@ function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
|
|
|
111
114
|
done()
|
|
112
115
|
}
|
|
113
116
|
|
|
114
|
-
function getHook (hook = 'onRequest') {
|
|
115
|
-
const hooks = {
|
|
116
|
-
onRequest: 'onRequest',
|
|
117
|
-
preParsing: 'preParsing',
|
|
118
|
-
preValidation: 'preValidation',
|
|
119
|
-
preHandler: 'preHandler',
|
|
120
|
-
[false]: false
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return hooks[hook]
|
|
124
|
-
}
|
|
125
|
-
|
|
126
117
|
function plugin (fastify, options, next) {
|
|
127
118
|
const secret = options.secret
|
|
128
119
|
const hook = getHook(options.hook)
|
|
@@ -185,6 +176,18 @@ function plugin (fastify, options, next) {
|
|
|
185
176
|
}
|
|
186
177
|
}
|
|
187
178
|
|
|
179
|
+
function getHook (hook = 'onRequest') {
|
|
180
|
+
const hooks = {
|
|
181
|
+
onRequest: 'onRequest',
|
|
182
|
+
preParsing: 'preParsing',
|
|
183
|
+
preValidation: 'preValidation',
|
|
184
|
+
preHandler: 'preHandler',
|
|
185
|
+
[false]: false
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return hooks[hook]
|
|
189
|
+
}
|
|
190
|
+
|
|
188
191
|
function isConnectionSecure (request) {
|
|
189
192
|
return (
|
|
190
193
|
request.raw.socket?.encrypted === true ||
|
|
@@ -207,15 +210,9 @@ const fastifyCookie = fp(plugin, {
|
|
|
207
210
|
* - `import { fastifyCookie } from 'fastify-cookie'`
|
|
208
211
|
* - `import fastifyCookie from 'fastify-cookie'`
|
|
209
212
|
*/
|
|
210
|
-
fastifyCookie.signerFactory = Signer
|
|
211
|
-
fastifyCookie.fastifyCookie = fastifyCookie
|
|
212
|
-
fastifyCookie.default = fastifyCookie
|
|
213
213
|
module.exports = fastifyCookie
|
|
214
|
-
|
|
215
|
-
fastifyCookie.fastifyCookie
|
|
216
|
-
fastifyCookie.fastifyCookie.Signer = Signer
|
|
217
|
-
fastifyCookie.fastifyCookie.sign = sign
|
|
218
|
-
fastifyCookie.fastifyCookie.unsign = unsign
|
|
214
|
+
module.exports.default = fastifyCookie // supersedes fastifyCookie.default = fastifyCookie
|
|
215
|
+
module.exports.fastifyCookie = fastifyCookie // supersedes fastifyCookie.fastifyCookie = fastifyCookie
|
|
219
216
|
|
|
220
217
|
module.exports.signerFactory = Signer
|
|
221
218
|
module.exports.Signer = Signer
|
package/signer.js
CHANGED
package/test/cookie.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = tap.test
|
|
3
|
+
const { test } = require('tap')
|
|
5
4
|
const Fastify = require('fastify')
|
|
6
5
|
const sinon = require('sinon')
|
|
7
6
|
const { sign, unsign } = require('../signer')
|
|
@@ -1252,3 +1251,25 @@ test('cookies get set correctly if set inside onRequest', (t) => {
|
|
|
1252
1251
|
t.equal(cookies[0].path, '/')
|
|
1253
1252
|
})
|
|
1254
1253
|
})
|
|
1254
|
+
|
|
1255
|
+
test('do not crash if the onRequest hook is not run', (t) => {
|
|
1256
|
+
t.plan(3)
|
|
1257
|
+
const fastify = Fastify()
|
|
1258
|
+
fastify.addHook('onRequest', async (req, reply) => {
|
|
1259
|
+
return reply.send({ hello: 'world' })
|
|
1260
|
+
})
|
|
1261
|
+
|
|
1262
|
+
fastify.register(plugin)
|
|
1263
|
+
|
|
1264
|
+
fastify.inject({
|
|
1265
|
+
method: 'GET',
|
|
1266
|
+
url: '/test1',
|
|
1267
|
+
headers: {
|
|
1268
|
+
cookie: 'foo=foo'
|
|
1269
|
+
}
|
|
1270
|
+
}, (err, res) => {
|
|
1271
|
+
t.error(err)
|
|
1272
|
+
t.equal(res.statusCode, 200)
|
|
1273
|
+
t.same(JSON.parse(res.body), { hello: 'world' })
|
|
1274
|
+
})
|
|
1275
|
+
})
|