@fastify/cookie 9.0.1 → 9.0.3

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": "9.0.1",
3
+ "version": "9.0.3",
4
4
  "description": "Plugin for fastify to add support for cookies",
5
5
  "main": "plugin.js",
6
6
  "types": "types/plugin.d.ts",
package/plugin.js CHANGED
@@ -6,13 +6,11 @@ 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
- }
12
+ parseCookies(reply.context.server, reply.request, reply)
13
+
16
14
  const opts = Object.assign({}, options)
17
15
 
18
16
  if (opts.expires && Number.isInteger(opts.expires)) {
@@ -34,7 +32,7 @@ function fastifyCookieSetCookie (reply, name, value, options) {
34
32
 
35
33
  reply[kReplySetCookies].set(`${name};${opts.domain};${opts.path || '/'}`, { name, value, opts })
36
34
 
37
- if (sendHeaders) {
35
+ if (reply[kReplySetCookiesHookRan]) {
38
36
  setCookies(reply)
39
37
  }
40
38
 
@@ -50,38 +48,42 @@ function fastifyCookieClearCookie (reply, name, options) {
50
48
  return fastifyCookieSetCookie(reply, name, '', opts)
51
49
  }
52
50
 
51
+ function parseCookies (fastify, request, reply) {
52
+ if (reply[kReplySetCookies]) return
53
+
54
+ request.cookies = {} // New container per request. Issue #53
55
+ const cookieHeader = request.raw.headers.cookie
56
+ if (cookieHeader) {
57
+ request.cookies = fastify.parseCookie(cookieHeader)
58
+ }
59
+ reply[kReplySetCookies] = new Map()
60
+ }
61
+
53
62
  function onReqHandlerWrapper (fastify, hook) {
54
63
  return hook === 'preParsing'
55
64
  ? function fastifyCookieHandler (fastifyReq, fastifyRes, payload, done) {
56
- fastifyReq.cookies = {} // New container per request. Issue #53
57
- const cookieHeader = fastifyReq.raw.headers.cookie
58
- if (cookieHeader) {
59
- fastifyReq.cookies = fastify.parseCookie(cookieHeader)
60
- }
61
- fastifyRes[kReplySetCookies] = new Map()
65
+ parseCookies(fastify, fastifyReq, fastifyRes)
62
66
  done()
63
67
  }
64
68
  : function fastifyCookieHandler (fastifyReq, fastifyRes, done) {
65
- fastifyReq.cookies = {} // New container per request. Issue #53
66
- const cookieHeader = fastifyReq.raw.headers.cookie
67
- if (cookieHeader) {
68
- fastifyReq.cookies = fastify.parseCookie(cookieHeader)
69
- }
70
- fastifyRes[kReplySetCookies] = new Map()
69
+ parseCookies(fastify, fastifyReq, fastifyRes)
71
70
  done()
72
71
  }
73
72
  }
74
73
 
75
74
  function setCookies (reply) {
76
75
  let setCookie = reply.getHeader('Set-Cookie')
76
+ const setCookieIsUndefined = setCookie === undefined
77
77
 
78
78
  /* istanbul ignore else */
79
- if (setCookie === undefined) {
79
+ if (setCookieIsUndefined) {
80
80
  if (reply[kReplySetCookies].size === 1) {
81
81
  for (const c of reply[kReplySetCookies].values()) {
82
82
  reply.header('Set-Cookie', cookie.serialize(c.name, c.value, c.opts))
83
83
  }
84
84
 
85
+ reply[kReplySetCookies].clear()
86
+
85
87
  return
86
88
  }
87
89
 
@@ -94,8 +96,9 @@ function setCookies (reply) {
94
96
  setCookie.push(cookie.serialize(c.name, c.value, c.opts))
95
97
  }
96
98
 
97
- reply.removeHeader('Set-Cookie')
99
+ if (!setCookieIsUndefined) reply.removeHeader('Set-Cookie')
98
100
  reply.header('Set-Cookie', setCookie)
101
+ reply[kReplySetCookies].clear()
99
102
  }
100
103
 
101
104
  function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
@@ -103,9 +106,7 @@ function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
103
106
  setCookies(fastifyRes)
104
107
  }
105
108
 
106
- // Explicitly set the property to null so that we can
107
- // check if the header was already set
108
- fastifyRes[kReplySetCookies] = null
109
+ fastifyRes[kReplySetCookiesHookRan] = true
109
110
 
110
111
  done()
111
112
  }
@@ -147,6 +148,7 @@ function plugin (fastify, options, next) {
147
148
 
148
149
  fastify.decorateRequest('cookies', null)
149
150
  fastify.decorateReply(kReplySetCookies, null)
151
+ fastify.decorateReply(kReplySetCookiesHookRan, false)
150
152
 
151
153
  fastify.decorateReply('cookie', setCookie)
152
154
  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 (const secret of secrets) {
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 (const secret of secrets) {
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)
@@ -1187,3 +1187,68 @@ 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
+ })
1229
+
1230
+ test('cookies get set correctly if set inside onRequest', (t) => {
1231
+ t.plan(7)
1232
+ const fastify = Fastify()
1233
+ fastify.addHook('onRequest', async (req, reply) => {
1234
+ reply.setCookie('foo', 'foo', { path: '/' })
1235
+ return reply.send({ hello: 'world' })
1236
+ })
1237
+
1238
+ fastify.register(plugin)
1239
+
1240
+ fastify.inject({
1241
+ method: 'GET',
1242
+ url: '/test1'
1243
+ }, (err, res) => {
1244
+ t.error(err)
1245
+ t.equal(res.statusCode, 200)
1246
+ t.same(JSON.parse(res.body), { hello: 'world' })
1247
+
1248
+ const cookies = res.cookies
1249
+ t.equal(cookies.length, 1)
1250
+ t.equal(cookies[0].name, 'foo')
1251
+ t.equal(cookies[0].value, 'foo')
1252
+ t.equal(cookies[0].path, '/')
1253
+ })
1254
+ })