Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: don't initialize req.cookies before checking the header #248

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const kReplySetCookies = Symbol('fastify.reply.setCookies')
const kReplySetCookiesHookRan = Symbol('fastify.reply.setCookiesHookRan')

function fastifyCookieSetCookie (reply, name, value, options) {
parseCookies(reply.context.server, reply.request, reply)
parseCookies(reply.server, reply.request, reply)

const opts = Object.assign({}, options)

Expand Down Expand Up @@ -51,11 +51,9 @@ function fastifyCookieClearCookie (reply, name, options) {
function parseCookies (fastify, request, reply) {
if (reply[kReplySetCookies]) return

request.cookies = {} // New container per request. Issue #53
const cookieHeader = request.raw.headers.cookie
if (cookieHeader) {
request.cookies = fastify.parseCookie(cookieHeader)
}

request.cookies = cookieHeader ? fastify.parseCookie(cookieHeader) : {} // New container per request. Issue #53
reply[kReplySetCookies] = new Map()
}

Expand Down Expand Up @@ -116,18 +114,6 @@ function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
done()
}

function getHook (hook = 'onRequest') {
const hooks = {
onRequest: 'onRequest',
preParsing: 'preParsing',
preValidation: 'preValidation',
preHandler: 'preHandler',
[false]: false
}

return hooks[hook]
}

function plugin (fastify, options, next) {
const secret = options.secret
const hook = getHook(options.hook)
Expand Down Expand Up @@ -190,6 +176,18 @@ function plugin (fastify, options, next) {
}
}

function getHook (hook = 'onRequest') {
const hooks = {
onRequest: 'onRequest',
preParsing: 'preParsing',
preValidation: 'preValidation',
preHandler: 'preHandler',
[false]: false
}

return hooks[hook]
}

function isConnectionSecure (request) {
return (
request.raw.socket?.encrypted === true ||
Expand Down