@fastify/cookie 9.0.0 → 9.0.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/package.json +1 -1
- package/plugin.js +36 -19
- package/test/cookie.test.js +31 -0
package/package.json
CHANGED
package/plugin.js
CHANGED
|
@@ -8,6 +8,11 @@ const { Signer, sign, unsign } = require('./signer')
|
|
|
8
8
|
const kReplySetCookies = Symbol('fastify.reply.setCookies')
|
|
9
9
|
|
|
10
10
|
function fastifyCookieSetCookie (reply, name, value, options) {
|
|
11
|
+
let sendHeaders = false
|
|
12
|
+
if (reply[kReplySetCookies] === null) {
|
|
13
|
+
sendHeaders = true
|
|
14
|
+
reply[kReplySetCookies] = new Map()
|
|
15
|
+
}
|
|
11
16
|
const opts = Object.assign({}, options)
|
|
12
17
|
|
|
13
18
|
if (opts.expires && Number.isInteger(opts.expires)) {
|
|
@@ -29,6 +34,10 @@ function fastifyCookieSetCookie (reply, name, value, options) {
|
|
|
29
34
|
|
|
30
35
|
reply[kReplySetCookies].set(`${name};${opts.domain};${opts.path || '/'}`, { name, value, opts })
|
|
31
36
|
|
|
37
|
+
if (sendHeaders) {
|
|
38
|
+
setCookies(reply)
|
|
39
|
+
}
|
|
40
|
+
|
|
32
41
|
return reply
|
|
33
42
|
}
|
|
34
43
|
|
|
@@ -63,33 +72,41 @@ function onReqHandlerWrapper (fastify, hook) {
|
|
|
63
72
|
}
|
|
64
73
|
}
|
|
65
74
|
|
|
66
|
-
function
|
|
67
|
-
|
|
68
|
-
let setCookie = fastifyRes.getHeader('Set-Cookie')
|
|
69
|
-
|
|
70
|
-
/* istanbul ignore else */
|
|
71
|
-
if (setCookie === undefined) {
|
|
72
|
-
if (fastifyRes[kReplySetCookies].size === 1) {
|
|
73
|
-
for (const c of fastifyRes[kReplySetCookies].values()) {
|
|
74
|
-
fastifyRes.header('Set-Cookie', cookie.serialize(c.name, c.value, c.opts))
|
|
75
|
-
}
|
|
75
|
+
function setCookies (reply) {
|
|
76
|
+
let setCookie = reply.getHeader('Set-Cookie')
|
|
76
77
|
|
|
77
|
-
|
|
78
|
+
/* istanbul ignore else */
|
|
79
|
+
if (setCookie === undefined) {
|
|
80
|
+
if (reply[kReplySetCookies].size === 1) {
|
|
81
|
+
for (const c of reply[kReplySetCookies].values()) {
|
|
82
|
+
reply.header('Set-Cookie', cookie.serialize(c.name, c.value, c.opts))
|
|
78
83
|
}
|
|
79
84
|
|
|
80
|
-
|
|
81
|
-
} else if (typeof setCookie === 'string') {
|
|
82
|
-
setCookie = [setCookie]
|
|
85
|
+
return
|
|
83
86
|
}
|
|
84
87
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
setCookie = []
|
|
89
|
+
} else if (typeof setCookie === 'string') {
|
|
90
|
+
setCookie = [setCookie]
|
|
91
|
+
}
|
|
88
92
|
|
|
89
|
-
|
|
90
|
-
|
|
93
|
+
for (const c of reply[kReplySetCookies].values()) {
|
|
94
|
+
setCookie.push(cookie.serialize(c.name, c.value, c.opts))
|
|
91
95
|
}
|
|
92
96
|
|
|
97
|
+
reply.removeHeader('Set-Cookie')
|
|
98
|
+
reply.header('Set-Cookie', setCookie)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
|
|
102
|
+
if (fastifyRes[kReplySetCookies].size) {
|
|
103
|
+
setCookies(fastifyRes)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Explicitly set the property to null so that we can
|
|
107
|
+
// check if the header was already set
|
|
108
|
+
fastifyRes[kReplySetCookies] = null
|
|
109
|
+
|
|
93
110
|
done()
|
|
94
111
|
}
|
|
95
112
|
|
package/test/cookie.test.js
CHANGED
|
@@ -1156,3 +1156,34 @@ test('should update a cookie value when setCookie is called multiple times (non-
|
|
|
1156
1156
|
t.ok(new Date(cookies[1].expires) < new Date())
|
|
1157
1157
|
})
|
|
1158
1158
|
})
|
|
1159
|
+
|
|
1160
|
+
test('cookies get set correctly if set inside onSend', (t) => {
|
|
1161
|
+
t.plan(7)
|
|
1162
|
+
const fastify = Fastify()
|
|
1163
|
+
fastify.register(plugin)
|
|
1164
|
+
|
|
1165
|
+
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
1166
|
+
reply.setCookie('foo', 'foo', { path: '/' })
|
|
1167
|
+
return payload
|
|
1168
|
+
})
|
|
1169
|
+
|
|
1170
|
+
fastify.get('/test1', (req, reply) => {
|
|
1171
|
+
reply
|
|
1172
|
+
.send({ hello: 'world' })
|
|
1173
|
+
})
|
|
1174
|
+
|
|
1175
|
+
fastify.inject({
|
|
1176
|
+
method: 'GET',
|
|
1177
|
+
url: '/test1'
|
|
1178
|
+
}, (err, res) => {
|
|
1179
|
+
t.error(err)
|
|
1180
|
+
t.equal(res.statusCode, 200)
|
|
1181
|
+
t.same(JSON.parse(res.body), { hello: 'world' })
|
|
1182
|
+
|
|
1183
|
+
const cookies = res.cookies
|
|
1184
|
+
t.equal(cookies.length, 1)
|
|
1185
|
+
t.equal(cookies[0].name, 'foo')
|
|
1186
|
+
t.equal(cookies[0].value, 'foo')
|
|
1187
|
+
t.equal(cookies[0].path, '/')
|
|
1188
|
+
})
|
|
1189
|
+
})
|