forked from fastify/fastify-cookie
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.js
70 lines (59 loc) · 2.07 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict'
const fp = require('fastify-plugin')
const cookie = require('cookie')
const cookieSignature = require('cookie-signature')
function fastifyCookieSetCookie (reply, name, value, options, secret) {
const opts = Object.assign({}, options || {})
if (opts.expires && Number.isInteger(opts.expires)) {
opts.expires = new Date(opts.expires)
}
if (opts.signed) {
value = cookieSignature.sign(value, secret)
}
const serialized = cookie.serialize(name, value, opts)
let setCookie = reply.getHeader('Set-Cookie')
if (!setCookie) {
reply.header('Set-Cookie', serialized)
return reply
}
if (typeof setCookie === 'string') {
setCookie = [setCookie]
}
setCookie.push(serialized)
reply.removeHeader('Set-Cookie')
reply.header('Set-Cookie', setCookie)
return reply
}
function fastifyCookieClearCookie (reply, name, options) {
const opts = Object.assign({ expires: new Date(1), path: '/' }, options || {})
return fastifyCookieSetCookie(reply, name, '', opts)
}
function onReqHandlerWrapper (options) {
return function fastifyCookieOnReqHandler (fastifyReq, fastifyRes, done) {
fastifyReq.cookies = {} // New container per request. Issue #53
const cookieHeader = fastifyReq.req.headers.cookie
if (cookieHeader) {
fastifyReq.cookies = cookie.parse(cookieHeader, options)
}
done()
}
}
function plugin (fastify, options, next) {
const secret = options ? options.secret || '' : ''
fastify.decorateRequest('cookies', {})
fastify.decorateReply('setCookie', function setCookieWrapper (name, value, options) {
return fastifyCookieSetCookie(this, name, value, options, secret)
})
fastify.decorateReply('clearCookie', function clearCookieWrapper (name, options) {
return fastifyCookieClearCookie(this, name, options)
})
fastify.decorateReply('unsignCookie', function unsignCookieWrapper (value) {
return cookieSignature.unsign(value, secret)
})
fastify.addHook('onRequest', onReqHandlerWrapper(options.parseOptions))
next()
}
module.exports = fp(plugin, {
fastify: '>=2.0.0',
name: 'fastify-cookie'
})