Skip to content

Commit

Permalink
chore: named hook handlers (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Sep 25, 2024
1 parent ded25e2 commit 742aec4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
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) {
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

0 comments on commit 742aec4

Please sign in to comment.