@fastify/cookie 9.0.2 → 9.0.4

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.2",
3
+ "version": "9.0.4",
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
@@ -9,6 +9,8 @@ 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.context.server, reply.request, reply)
13
+
12
14
  const opts = Object.assign({}, options)
13
15
 
14
16
  if (opts.expires && Number.isInteger(opts.expires)) {
@@ -46,24 +48,25 @@ function fastifyCookieClearCookie (reply, name, options) {
46
48
  return fastifyCookieSetCookie(reply, name, '', opts)
47
49
  }
48
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
+
49
62
  function onReqHandlerWrapper (fastify, hook) {
50
63
  return hook === 'preParsing'
51
64
  ? function fastifyCookieHandler (fastifyReq, fastifyRes, payload, done) {
52
- fastifyReq.cookies = {} // New container per request. Issue #53
53
- const cookieHeader = fastifyReq.raw.headers.cookie
54
- if (cookieHeader) {
55
- fastifyReq.cookies = fastify.parseCookie(cookieHeader)
56
- }
57
- fastifyRes[kReplySetCookies] = new Map()
65
+ parseCookies(fastify, fastifyReq, fastifyRes)
58
66
  done()
59
67
  }
60
68
  : function fastifyCookieHandler (fastifyReq, fastifyRes, done) {
61
- fastifyReq.cookies = {} // New container per request. Issue #53
62
- const cookieHeader = fastifyReq.raw.headers.cookie
63
- if (cookieHeader) {
64
- fastifyReq.cookies = fastify.parseCookie(cookieHeader)
65
- }
66
- fastifyRes[kReplySetCookies] = new Map()
69
+ parseCookies(fastify, fastifyReq, fastifyRes)
67
70
  done()
68
71
  }
69
72
  }
@@ -99,6 +102,11 @@ function setCookies (reply) {
99
102
  }
100
103
 
101
104
  function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
105
+ if (!fastifyRes[kReplySetCookies]) {
106
+ done()
107
+ return
108
+ }
109
+
102
110
  if (fastifyRes[kReplySetCookies].size) {
103
111
  setCookies(fastifyRes)
104
112
  }
@@ -1226,3 +1226,51 @@ test('cookies get set correctly if set inside multiple onSends', (t) => {
1226
1226
  t.equal(cookies[1].path, '/')
1227
1227
  })
1228
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
+ })
1255
+
1256
+ test('do not crash if the onRequest hook is not run', (t) => {
1257
+ t.plan(3)
1258
+ const fastify = Fastify()
1259
+ fastify.addHook('onRequest', async (req, reply) => {
1260
+ return reply.send({ hello: 'world' })
1261
+ })
1262
+
1263
+ fastify.register(plugin)
1264
+
1265
+ fastify.inject({
1266
+ method: 'GET',
1267
+ url: '/test1',
1268
+ headers: {
1269
+ cookie: 'foo=foo'
1270
+ }
1271
+ }, (err, res) => {
1272
+ t.error(err)
1273
+ t.equal(res.statusCode, 200)
1274
+ t.same(JSON.parse(res.body), { hello: 'world' })
1275
+ })
1276
+ })