Skip to content

Commit 742aec4

Browse files
authored
chore: named hook handlers (#152)
1 parent ded25e2 commit 742aec4

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function etag (value, lifetime) {
2626
return this
2727
}
2828

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

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

73-
instance.addHook('onRequest', (req, res, next) => {
73+
instance.addHook('onRequest', function cachingSetCacheControlHeader (req, res, next) {
7474
if (!res.hasHeader('Cache-control')) {
7575
res.header('Cache-control', value)
7676
}
@@ -83,8 +83,8 @@ function fastifyCaching (instance, options, next) {
8383
instance.decorate('etagMaxLife', _options.etagMaxLife)
8484
instance.decorateReply('etag', etag)
8585
instance.decorateReply('expires', cachingExpires)
86-
instance.addHook('onRequest', etagHandleRequest)
87-
instance.addHook('onSend', etagOnSend)
86+
instance.addHook('onRequest', cachingLoadByEtag)
87+
instance.addHook('onSend', cachingStoreByEtag)
8888

8989
instance[Symbol.for('fastify-caching.registered')] = true
9090
next()

test/cache.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ test('cache is usable', async (t) => {
2222

2323
const fastify = Fastify()
2424
await fastify.register(async (instance, options) => {
25-
instance.addHook('onRequest', async function (req, reply) {
25+
instance.addHook('onRequest', async function checkCachingRegistered (req, reply) {
2626
t.assert.ifError(instance[Symbol.for('fastify-caching.registered')])
2727
})
2828
})
2929
await fastify.register(plugin)
3030

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

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

7272
const fastify = Fastify()
7373
await fastify.register(async (instance, options) => {
74-
instance.addHook('onRequest', async function (req, reply) {
74+
instance.addHook('onRequest', async function checkCachingNotRegistered (req, reply) {
7575
t.assert.failure(instance[Symbol.for('fastify-caching.registered')])
7676
})
7777
})
7878
await fastify.register(plugin, () => () => {})
7979

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

test/headers.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ test('do not set headers if another upstream plugin already sets it', async (t)
151151
}
152152

153153
const fastify = Fastify()
154-
fastify.addHook('onRequest', async (req, reply) => {
154+
fastify.addHook('onRequest', async function checkCachingDoesNotOverrideCacheControlHeader (req, reply) {
155155
reply.header('cache-control', 'do not override')
156156
})
157157
await fastify.register(plugin, opts)

0 commit comments

Comments
 (0)