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

chore: named hook handlers #152

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function etag (value, lifetime) {
return this
}

function etagHandleRequest (req, res, next) {
function cachingLoadByEtag (req, res, next) {
if (!req.headers['if-none-match']) return next()
const etag = req.headers['if-none-match']
this.cache.get({ id: etag, segment: this.cacheSegment }, (err, cached) => {
Expand All @@ -38,7 +38,7 @@ function etagHandleRequest (req, res, next) {
})
}

function etagOnSend (req, res, payload, next) {
function cachingStoreByEtag (req, res, payload, next) {
const etag = res.getHeader('etag')
if (!etag || !res._etagLife) return next()
this.cache.set(
Expand Down Expand Up @@ -70,7 +70,7 @@ function fastifyCaching (instance, options, next) {
value += `, s-maxage=${_options.serverExpiresIn}`
}

instance.addHook('onRequest', (req, res, next) => {
instance.addHook('onRequest', function cachingSetCacheControlHeader (req, res, next) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really the only change needed in this PR. The other names are fine; the fact that they are named is enough. And we don't need names in the tests. The names provide information when reviewing stack traces, stepping through with a debugger, and reviewing observability data.

if (!res.hasHeader('Cache-control')) {
res.header('Cache-control', value)
}
Expand All @@ -83,8 +83,8 @@ function fastifyCaching (instance, options, next) {
instance.decorate('etagMaxLife', _options.etagMaxLife)
instance.decorateReply('etag', etag)
instance.decorateReply('expires', cachingExpires)
instance.addHook('onRequest', etagHandleRequest)
instance.addHook('onSend', etagOnSend)
instance.addHook('onRequest', cachingLoadByEtag)
instance.addHook('onSend', cachingStoreByEtag)

instance[Symbol.for('fastify-caching.registered')] = true
next()
Expand Down
8 changes: 4 additions & 4 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ test('cache is usable', async (t) => {

const fastify = Fastify()
await fastify.register(async (instance, options) => {
instance.addHook('onRequest', async function (req, reply) {
instance.addHook('onRequest', async function checkCachingRegistered (req, reply) {
t.assert.ifError(instance[Symbol.for('fastify-caching.registered')])
})
})
await fastify.register(plugin)

fastify.addHook('onRequest', async function (req, reply) {
fastify.addHook('onRequest', async function checkCachingRegistered (req, reply) {
t.assert.strictEqual(this[Symbol.for('fastify-caching.registered')], true)
})

Expand Down Expand Up @@ -71,13 +71,13 @@ test('cache is usable with function as plugin default options input', async (t)

const fastify = Fastify()
await fastify.register(async (instance, options) => {
instance.addHook('onRequest', async function (req, reply) {
instance.addHook('onRequest', async function checkCachingNotRegistered (req, reply) {
t.assert.failure(instance[Symbol.for('fastify-caching.registered')])
})
})
await fastify.register(plugin, () => () => {})

fastify.addHook('onRequest', async function (req, reply) {
fastify.addHook('onRequest', async function checkCachingRegistered (req, reply) {
t.assert.strictEqual(this[Symbol.for('fastify-caching.registered')], true)
})

Expand Down
2 changes: 1 addition & 1 deletion test/headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ test('do not set headers if another upstream plugin already sets it', async (t)
}

const fastify = Fastify()
fastify.addHook('onRequest', async (req, reply) => {
fastify.addHook('onRequest', async function checkCachingDoesNotOverrideCacheControlHeader (req, reply) {
reply.header('cache-control', 'do not override')
})
await fastify.register(plugin, opts)
Expand Down