From 742aec441b7726611581981f8a2364e201191ae5 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Wed, 25 Sep 2024 14:50:34 +0200 Subject: [PATCH] chore: named hook handlers (#152) --- index.js | 10 +++++----- test/cache.test.js | 8 ++++---- test/headers.test.js | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 009beb6..531cd68 100644 --- a/index.js +++ b/index.js @@ -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) => { @@ -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( @@ -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) } @@ -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() diff --git a/test/cache.test.js b/test/cache.test.js index 97c3a5a..8441b4e 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -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) }) @@ -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) }) diff --git a/test/headers.test.js b/test/headers.test.js index 6c1e3b8..015a2eb 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -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)