@fastify/cookie
Advanced tools
Comparing version 8.0.0 to 8.1.0
{ | ||
"name": "@fastify/cookie", | ||
"version": "8.0.0", | ||
"version": "8.1.0", | ||
"description": "Plugin for fastify to add support for cookies", | ||
@@ -5,0 +5,0 @@ "main": "plugin.js", |
@@ -53,11 +53,32 @@ 'use strict' | ||
function onReqHandlerWrapper (fastify) { | ||
return function fastifyCookieOnReqHandler (fastifyReq, fastifyRes, done) { | ||
fastifyReq.cookies = {} // New container per request. Issue #53 | ||
const cookieHeader = fastifyReq.raw.headers.cookie | ||
if (cookieHeader) { | ||
fastifyReq.cookies = fastify.parseCookie(cookieHeader) | ||
function onReqHandlerWrapper (fastify, hook) { | ||
return hook === 'preParsing' | ||
? function fastifyCookieHandler (fastifyReq, fastifyRes, payload, done) { | ||
fastifyReq.cookies = {} // New container per request. Issue #53 | ||
const cookieHeader = fastifyReq.raw.headers.cookie | ||
if (cookieHeader) { | ||
fastifyReq.cookies = fastify.parseCookie(cookieHeader) | ||
} | ||
done() | ||
} | ||
done() | ||
: function fastifyCookieHandler (fastifyReq, fastifyRes, done) { | ||
fastifyReq.cookies = {} // New container per request. Issue #53 | ||
const cookieHeader = fastifyReq.raw.headers.cookie | ||
if (cookieHeader) { | ||
fastifyReq.cookies = fastify.parseCookie(cookieHeader) | ||
} | ||
done() | ||
} | ||
} | ||
function getHook (hook = 'onRequest') { | ||
const hooks = { | ||
onRequest: 'onRequest', | ||
preParsing: 'preParsing', | ||
preValidation: 'preValidation', | ||
preHandler: 'preHandler', | ||
[false]: false | ||
} | ||
return hooks[hook] | ||
} | ||
@@ -67,2 +88,6 @@ | ||
const secret = options.secret | ||
const hook = getHook(options.hook) | ||
if (hook === undefined) { | ||
return next(new Error('@fastify/cookie: Invalid value provided for the hook-option. You can set the hook-option only to false, \'onRequest\' , \'preParsing\' , \'preValidation\' or \'preHandler\'')) | ||
} | ||
const enableRotation = Array.isArray(secret) | ||
@@ -91,3 +116,5 @@ const algorithm = options.algorithm || 'sha256' | ||
fastify.addHook('onRequest', onReqHandlerWrapper(fastify)) | ||
if (hook) { | ||
fastify.addHook(hook, onReqHandlerWrapper(fastify, hook)) | ||
} | ||
@@ -94,0 +121,0 @@ next() |
@@ -36,3 +36,4 @@ # @fastify/cookie | ||
secret: "my-secret", // for cookies signature | ||
parseOptions: {} // options for parsing cookies | ||
hook: 'onRequest', // set to false to disable cookie autoparsing or set autoparsing on any of the following hooks: 'onRequest', 'preParsing', 'preHandler', 'preValidation'. default: 'onRequest' | ||
parseOptions: {} // options for parsing cookies | ||
}) | ||
@@ -39,0 +40,0 @@ |
@@ -37,3 +37,3 @@ 'use strict' | ||
try { | ||
crypto.createHmac(algorithm, 'dummyHmac') | ||
crypto.createHmac(algorithm, crypto.randomBytes(16)) | ||
} catch (e) { | ||
@@ -40,0 +40,0 @@ throw new TypeError(`Algorithm ${algorithm} not supported.`) |
@@ -850,1 +850,96 @@ 'use strict' | ||
}) | ||
test('dont add auto cookie parsing to onRequest-hook if hook-option is set to false', (t) => { | ||
t.plan(6) | ||
const fastify = Fastify() | ||
fastify.register(plugin, { hook: false }) | ||
for (const hook of ['preValidation', 'preHandler', 'preParsing']) { | ||
fastify.addHook(hook, async (req) => { | ||
t.equal(req.cookies, null) | ||
}) | ||
} | ||
fastify.get('/disable', (req, reply) => { | ||
t.equal(req.cookies, null) | ||
reply.send() | ||
}) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/disable', | ||
headers: { | ||
cookie: 'bar=bar' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
}) | ||
}) | ||
test('result in an error if hook-option is set to an invalid value', (t) => { | ||
t.plan(1) | ||
const fastify = Fastify() | ||
t.rejects( | ||
() => fastify.register(plugin, { hook: true }), | ||
new Error("@fastify/cookie: Invalid value provided for the hook-option. You can set the hook-option only to false, 'onRequest' , 'preParsing' , 'preValidation' or 'preHandler'") | ||
) | ||
}) | ||
test('correct working plugin if hook-option to preParsing', (t) => { | ||
t.plan(5) | ||
const fastify = Fastify() | ||
fastify.register(plugin, { hook: 'preParsing' }) | ||
fastify.addHook('onRequest', async (req) => { | ||
t.equal(req.cookies, null) | ||
}) | ||
fastify.addHook('preValidation', async (req) => { | ||
t.equal(req.cookies.bar, 'bar') | ||
}) | ||
fastify.get('/preparsing', (req, reply) => { | ||
t.equal(req.cookies.bar, 'bar') | ||
reply.send() | ||
}) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/preparsing', | ||
headers: { | ||
cookie: 'bar=bar' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
}) | ||
}) | ||
test('if cookies are not set, then the handler creates an empty req.cookies object', (t) => { | ||
t.plan(5) | ||
const fastify = Fastify() | ||
fastify.register(plugin, { hook: 'preParsing' }) | ||
fastify.addHook('onRequest', async (req) => { | ||
t.equal(req.cookies, null) | ||
}) | ||
fastify.addHook('preValidation', async (req) => { | ||
t.ok(req.cookies) | ||
}) | ||
fastify.get('/preparsing', (req, reply) => { | ||
t.ok(req.cookies) | ||
reply.send() | ||
}) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/preparsing' | ||
}, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
}) | ||
}) |
@@ -128,4 +128,7 @@ /// <reference types='node' /> | ||
type HookType = 'onRequest' | 'preParsing' | 'preValidation' | 'preHandler' | 'preSerialization'; | ||
export interface FastifyCookieOptions { | ||
secret?: string | string[] | Signer; | ||
hook?: HookType | false; | ||
parseOptions?: fastifyCookie.CookieSerializeOptions; | ||
@@ -132,0 +135,0 @@ } |
import cookie from '..'; | ||
import { expectType } from 'tsd'; | ||
import { expectError, expectType } from 'tsd'; | ||
import * as fastifyCookieStar from '..'; | ||
import fastifyCookieCjsImport = require('..'); | ||
import fastifyCookieDefault, { fastifyCookie as fastifyCookieNamed } from '..'; | ||
import fastify, { FastifyInstance, FastifyReply, setCookieWrapper } from 'fastify'; | ||
import fastify, { FastifyInstance, FastifyPluginCallback, FastifyReply, setCookieWrapper } from 'fastify'; | ||
import { Server } from 'http'; | ||
@@ -216,2 +217,13 @@ const fastifyCookieCjs = require('..'); | ||
signer.sign('Lorem Ipsum') | ||
signer.unsign('Lorem Ipsum') | ||
signer.unsign('Lorem Ipsum') | ||
const appWithHook: FastifyInstance = fastify(); | ||
appWithHook.register(cookie, { hook: false }); | ||
appWithHook.register(cookie, { hook: 'onRequest' }); | ||
appWithHook.register(cookie, { hook: 'preHandler' }); | ||
appWithHook.register(cookie, { hook: 'preParsing' }); | ||
appWithHook.register(cookie, { hook: 'preSerialization' }); | ||
appWithHook.register(cookie, { hook: 'preValidation' }); | ||
expectError(appWithHook.register(cookie, { hook: true })); | ||
expectError(appWithHook.register(cookie, { hook: 'false' })); |
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
60933
1515
263