@fastify/cookie 9.0.1 → 9.0.2
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 +1 -1
- package/plugin.js +10 -11
- package/signer.js +4 -2
- package/test/cookie.test.js +39 -0
package/package.json
CHANGED
package/plugin.js
CHANGED
|
@@ -6,13 +6,9 @@ const cookie = require('cookie')
|
|
|
6
6
|
const { Signer, sign, unsign } = require('./signer')
|
|
7
7
|
|
|
8
8
|
const kReplySetCookies = Symbol('fastify.reply.setCookies')
|
|
9
|
+
const kReplySetCookiesHookRan = Symbol('fastify.reply.setCookiesHookRan')
|
|
9
10
|
|
|
10
11
|
function fastifyCookieSetCookie (reply, name, value, options) {
|
|
11
|
-
let sendHeaders = false
|
|
12
|
-
if (reply[kReplySetCookies] === null) {
|
|
13
|
-
sendHeaders = true
|
|
14
|
-
reply[kReplySetCookies] = new Map()
|
|
15
|
-
}
|
|
16
12
|
const opts = Object.assign({}, options)
|
|
17
13
|
|
|
18
14
|
if (opts.expires && Number.isInteger(opts.expires)) {
|
|
@@ -34,7 +30,7 @@ function fastifyCookieSetCookie (reply, name, value, options) {
|
|
|
34
30
|
|
|
35
31
|
reply[kReplySetCookies].set(`${name};${opts.domain};${opts.path || '/'}`, { name, value, opts })
|
|
36
32
|
|
|
37
|
-
if (
|
|
33
|
+
if (reply[kReplySetCookiesHookRan]) {
|
|
38
34
|
setCookies(reply)
|
|
39
35
|
}
|
|
40
36
|
|
|
@@ -74,14 +70,17 @@ function onReqHandlerWrapper (fastify, hook) {
|
|
|
74
70
|
|
|
75
71
|
function setCookies (reply) {
|
|
76
72
|
let setCookie = reply.getHeader('Set-Cookie')
|
|
73
|
+
const setCookieIsUndefined = setCookie === undefined
|
|
77
74
|
|
|
78
75
|
/* istanbul ignore else */
|
|
79
|
-
if (
|
|
76
|
+
if (setCookieIsUndefined) {
|
|
80
77
|
if (reply[kReplySetCookies].size === 1) {
|
|
81
78
|
for (const c of reply[kReplySetCookies].values()) {
|
|
82
79
|
reply.header('Set-Cookie', cookie.serialize(c.name, c.value, c.opts))
|
|
83
80
|
}
|
|
84
81
|
|
|
82
|
+
reply[kReplySetCookies].clear()
|
|
83
|
+
|
|
85
84
|
return
|
|
86
85
|
}
|
|
87
86
|
|
|
@@ -94,8 +93,9 @@ function setCookies (reply) {
|
|
|
94
93
|
setCookie.push(cookie.serialize(c.name, c.value, c.opts))
|
|
95
94
|
}
|
|
96
95
|
|
|
97
|
-
reply.removeHeader('Set-Cookie')
|
|
96
|
+
if (!setCookieIsUndefined) reply.removeHeader('Set-Cookie')
|
|
98
97
|
reply.header('Set-Cookie', setCookie)
|
|
98
|
+
reply[kReplySetCookies].clear()
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
|
|
@@ -103,9 +103,7 @@ function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
|
|
|
103
103
|
setCookies(fastifyRes)
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
// check if the header was already set
|
|
108
|
-
fastifyRes[kReplySetCookies] = null
|
|
106
|
+
fastifyRes[kReplySetCookiesHookRan] = true
|
|
109
107
|
|
|
110
108
|
done()
|
|
111
109
|
}
|
|
@@ -147,6 +145,7 @@ function plugin (fastify, options, next) {
|
|
|
147
145
|
|
|
148
146
|
fastify.decorateRequest('cookies', null)
|
|
149
147
|
fastify.decorateReply(kReplySetCookies, null)
|
|
148
|
+
fastify.decorateReply(kReplySetCookiesHookRan, false)
|
|
150
149
|
|
|
151
150
|
fastify.decorateReply('cookie', setCookie)
|
|
152
151
|
fastify.decorateReply('setCookie', setCookie)
|
package/signer.js
CHANGED
|
@@ -24,7 +24,8 @@ function Signer (secrets, algorithm = 'sha256') {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
function validateSecrets (secrets) {
|
|
27
|
-
for (
|
|
27
|
+
for (let i = 0; i < secrets.length; ++i) {
|
|
28
|
+
const secret = secrets[i]
|
|
28
29
|
if (typeof secret !== 'string' && Buffer.isBuffer(secret) === false) {
|
|
29
30
|
throw new TypeError('Secret key must be a string or Buffer.')
|
|
30
31
|
}
|
|
@@ -59,7 +60,8 @@ function _unsign (signedValue, secrets, algorithm) {
|
|
|
59
60
|
const value = signedValue.slice(0, signedValue.lastIndexOf('.'))
|
|
60
61
|
const actual = Buffer.from(signedValue.slice(signedValue.lastIndexOf('.') + 1))
|
|
61
62
|
|
|
62
|
-
for (
|
|
63
|
+
for (let i = 0; i < secrets.length; ++i) {
|
|
64
|
+
const secret = secrets[i]
|
|
63
65
|
const expected = Buffer.from(crypto
|
|
64
66
|
.createHmac(algorithm, secret)
|
|
65
67
|
.update(value)
|
package/test/cookie.test.js
CHANGED
|
@@ -1187,3 +1187,42 @@ test('cookies get set correctly if set inside onSend', (t) => {
|
|
|
1187
1187
|
t.equal(cookies[0].path, '/')
|
|
1188
1188
|
})
|
|
1189
1189
|
})
|
|
1190
|
+
|
|
1191
|
+
test('cookies get set correctly if set inside multiple onSends', (t) => {
|
|
1192
|
+
t.plan(10)
|
|
1193
|
+
const fastify = Fastify()
|
|
1194
|
+
fastify.register(plugin)
|
|
1195
|
+
|
|
1196
|
+
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
1197
|
+
reply.setCookie('foo', 'foo', { path: '/' })
|
|
1198
|
+
})
|
|
1199
|
+
|
|
1200
|
+
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
1201
|
+
reply.setCookie('foo', 'foos', { path: '/' })
|
|
1202
|
+
return payload
|
|
1203
|
+
})
|
|
1204
|
+
|
|
1205
|
+
fastify.get('/test1', (req, reply) => {
|
|
1206
|
+
reply
|
|
1207
|
+
.send({ hello: 'world' })
|
|
1208
|
+
})
|
|
1209
|
+
|
|
1210
|
+
fastify.inject({
|
|
1211
|
+
method: 'GET',
|
|
1212
|
+
url: '/test1'
|
|
1213
|
+
}, (err, res) => {
|
|
1214
|
+
t.error(err)
|
|
1215
|
+
t.equal(res.statusCode, 200)
|
|
1216
|
+
t.same(JSON.parse(res.body), { hello: 'world' })
|
|
1217
|
+
|
|
1218
|
+
const cookies = res.cookies
|
|
1219
|
+
t.equal(cookies.length, 2)
|
|
1220
|
+
t.equal(cookies[0].name, 'foo')
|
|
1221
|
+
t.equal(cookies[0].value, 'foo')
|
|
1222
|
+
t.equal(cookies[0].path, '/')
|
|
1223
|
+
|
|
1224
|
+
t.equal(cookies[1].name, 'foo')
|
|
1225
|
+
t.equal(cookies[1].value, 'foos')
|
|
1226
|
+
t.equal(cookies[1].path, '/')
|
|
1227
|
+
})
|
|
1228
|
+
})
|