@fastify/cookie
Advanced tools
Comparing version 9.0.0 to 9.0.1
{ | ||
"name": "@fastify/cookie", | ||
"version": "9.0.0", | ||
"version": "9.0.1", | ||
"description": "Plugin for fastify to add support for cookies", | ||
@@ -5,0 +5,0 @@ "main": "plugin.js", |
@@ -11,2 +11,7 @@ 'use strict' | ||
function fastifyCookieSetCookie (reply, name, value, options) { | ||
let sendHeaders = false | ||
if (reply[kReplySetCookies] === null) { | ||
sendHeaders = true | ||
reply[kReplySetCookies] = new Map() | ||
} | ||
const opts = Object.assign({}, options) | ||
@@ -33,2 +38,6 @@ | ||
if (sendHeaders) { | ||
setCookies(reply) | ||
} | ||
return reply | ||
@@ -68,29 +77,37 @@ } | ||
function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) { | ||
if (fastifyRes[kReplySetCookies].size) { | ||
let setCookie = fastifyRes.getHeader('Set-Cookie') | ||
function setCookies (reply) { | ||
let setCookie = reply.getHeader('Set-Cookie') | ||
/* istanbul ignore else */ | ||
if (setCookie === undefined) { | ||
if (fastifyRes[kReplySetCookies].size === 1) { | ||
for (const c of fastifyRes[kReplySetCookies].values()) { | ||
fastifyRes.header('Set-Cookie', cookie.serialize(c.name, c.value, c.opts)) | ||
} | ||
return done() | ||
/* istanbul ignore else */ | ||
if (setCookie === undefined) { | ||
if (reply[kReplySetCookies].size === 1) { | ||
for (const c of reply[kReplySetCookies].values()) { | ||
reply.header('Set-Cookie', cookie.serialize(c.name, c.value, c.opts)) | ||
} | ||
setCookie = [] | ||
} else if (typeof setCookie === 'string') { | ||
setCookie = [setCookie] | ||
return | ||
} | ||
for (const c of fastifyRes[kReplySetCookies].values()) { | ||
setCookie.push(cookie.serialize(c.name, c.value, c.opts)) | ||
} | ||
setCookie = [] | ||
} else if (typeof setCookie === 'string') { | ||
setCookie = [setCookie] | ||
} | ||
fastifyRes.removeHeader('Set-Cookie') | ||
fastifyRes.header('Set-Cookie', setCookie) | ||
for (const c of reply[kReplySetCookies].values()) { | ||
setCookie.push(cookie.serialize(c.name, c.value, c.opts)) | ||
} | ||
reply.removeHeader('Set-Cookie') | ||
reply.header('Set-Cookie', setCookie) | ||
} | ||
function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) { | ||
if (fastifyRes[kReplySetCookies].size) { | ||
setCookies(fastifyRes) | ||
} | ||
// Explicitly set the property to null so that we can | ||
// check if the header was already set | ||
fastifyRes[kReplySetCookies] = null | ||
done() | ||
@@ -97,0 +114,0 @@ } |
@@ -1159,1 +1159,32 @@ 'use strict' | ||
}) | ||
test('cookies get set correctly if set inside onSend', (t) => { | ||
t.plan(7) | ||
const fastify = Fastify() | ||
fastify.register(plugin) | ||
fastify.addHook('onSend', async (req, reply, payload) => { | ||
reply.setCookie('foo', 'foo', { path: '/' }) | ||
return payload | ||
}) | ||
fastify.get('/test1', (req, reply) => { | ||
reply | ||
.send({ hello: 'world' }) | ||
}) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/test1' | ||
}, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same(JSON.parse(res.body), { hello: 'world' }) | ||
const cookies = res.cookies | ||
t.equal(cookies.length, 1) | ||
t.equal(cookies[0].name, 'foo') | ||
t.equal(cookies[0].value, 'foo') | ||
t.equal(cookies[0].path, '/') | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70453
1804