Skip to content
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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function cachingLoadByEtag (req, res, next) {
})
}

function cachingStoreByEtag (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', function cachingSetCacheControlHeader (req, res, next) {
instance.addHook('onRequest', function cachingSetCacheControlHeader (_req, res, next) {
if (!res.hasHeader('Cache-control')) {
res.header('Cache-control', value)
}
Expand Down
34 changes: 17 additions & 17 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ test('cache is usable', async (t) => {
t.plan(4)

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

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

fastify.get('/one', (req, reply) => {
fastify.get('/one', (_req, reply) => {
fastify.cache.set('one', { one: true }, 1000, (err) => {
if (err) return reply.send(err)
reply.redirect('/two')
})
})

fastify.get('/two', (req, reply) => {
fastify.get('/two', (_req, reply) => {
fastify.cache.get('one', (err, obj) => {
t.assert.ifError(err)
t.assert.strictEqual(obj.item, { one: true })
Expand Down Expand Up @@ -70,25 +70,25 @@ test('cache is usable with function as plugin default options input', async (t)
t.plan(4)

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

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

fastify.get('/one', (req, reply) => {
fastify.get('/one', (_req, reply) => {
fastify.cache.set('one', { one: true }, 1000, (err) => {
if (err) return reply.send(err)
reply.redirect('/two')
})
})

fastify.get('/two', (req, reply) => {
fastify.get('/two', (_req, reply) => {
fastify.cache.get('one', (err, obj) => {
t.assert.ifError(err)
t.assert.strictEqual(obj.item, { one: true })
Expand Down Expand Up @@ -120,21 +120,21 @@ test('getting cache item with error returns error', async (t) => {
t.plan(1)

const mockCache = {
get: (info, callback) => callback(new Error('cache.get always errors')),
set: (key, value, ttl, callback) => callback()
get: (_info, callback) => callback(new Error('cache.get always errors')),
set: (_key, _value, _ttl, callback) => callback()
}

const fastify = Fastify()
await fastify.register(plugin, { cache: mockCache })

fastify.get('/one', (req, reply) => {
fastify.get('/one', (_req, reply) => {
fastify.cache.set('one', { one: true }, 1000, (err) => {
if (err) return reply.send(err)
return reply.etag('123456').send({ hello: 'world' })
})
})

fastify.get('/two', (req, reply) => {
fastify.get('/two', () => {
fastify.cache.get('one', (err, obj) => {
t.assert.failure(err)
t.assert.failure(obj)
Expand Down Expand Up @@ -164,7 +164,7 @@ test('etags get stored in cache', async (t) => {
const fastify = Fastify()
await fastify.register(plugin)

fastify.get('/one', (req, reply) => {
fastify.get('/one', (_req, reply) => {
reply.etag('123456').send({ hello: 'world' })
})

Expand All @@ -191,7 +191,7 @@ test('etag cache life is customizable', async (t) => {
const fastify = Fastify()
await fastify.register(plugin)

fastify.get('/one', function (req, reply) {
fastify.get('/one', function (_req, reply) {
reply
// We set a cache lifetime of 50 milliseconds
.etag('123456', 50)
Expand Down Expand Up @@ -237,7 +237,7 @@ test('returns response payload', async (t) => {
const fastify = Fastify()
await fastify.register(plugin)

fastify.get('/one', (req, reply) => {
fastify.get('/one', (_req, reply) => {
reply.etag('123456', 300).send({ hello: 'world' })
})

Expand Down
28 changes: 14 additions & 14 deletions test/headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('decorators get added', async (t) => {
const fastify = Fastify()
await fastify.register(plugin)

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
t.assert.ok(reply.etag)
reply.send()
})
Expand All @@ -31,7 +31,7 @@ test('decorators add headers', async (t) => {
const fastify = Fastify()
await fastify.register(plugin)

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.etag(tag).send()
})

Expand All @@ -51,7 +51,7 @@ test('sets etag header for falsy argument', async (t) => {
const fastify = Fastify()
await fastify.register(plugin)

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.etag().send()
})

Expand All @@ -70,7 +70,7 @@ test('sets no-cache header', async (t) => {
const fastify = Fastify()
await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send({ hello: 'world' })
})

Expand All @@ -95,7 +95,7 @@ test('sets private with max-age header', async (t) => {
const fastify = Fastify()
await fastify.register(plugin, opts)

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send({ hello: 'world' })
})

Expand Down Expand Up @@ -124,7 +124,7 @@ test('sets public with max-age and s-maxage header', async (t) => {
const fastify = Fastify()
await fastify.register(plugin, opts)

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send({ hello: 'world' })
})

Expand All @@ -151,12 +151,12 @@ test('do not set headers if another upstream plugin already sets it', async (t)
}

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

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send({ hello: 'world' })
})

Expand All @@ -182,7 +182,7 @@ test('only sets max-age and ignores s-maxage with private header', async (t) =>
const fastify = Fastify()
await fastify.register(plugin, opts)

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send({ hello: 'world' })
})

Expand Down Expand Up @@ -210,7 +210,7 @@ test('s-maxage is optional with public header', async (t) => {
const fastify = Fastify()
await fastify.register(plugin, opts)

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send({ hello: 'world' })
})

Expand All @@ -230,7 +230,7 @@ test('sets no-store with max-age header', async (t) => {
const fastify = Fastify()
await fastify.register(plugin, { privacy: 'no-store', expiresIn: 300 })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send({ hello: 'world' })
})

Expand All @@ -255,7 +255,7 @@ test('sets the expires header', async (t) => {
const fastify = Fastify()
await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.expires(now).send({ hello: 'world' })
})

Expand All @@ -275,7 +275,7 @@ test('sets the expires header to a falsy value', async (t) => {
const fastify = Fastify()
await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.expires().send({ hello: 'world' })
})

Expand All @@ -294,7 +294,7 @@ test('sets the expires header to a custom value', async (t) => {
const fastify = Fastify()
await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.expires('foobar').send({ hello: 'world' })
})

Expand Down
6 changes: 3 additions & 3 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ expectType<AbstractCacheCompliantObject['set']>(fastify.cache.set)
expectType<string>(fastify.cacheSegment)
// expectType<number>(fastify.etagMaxLife);

fastify.get('/one', async (request, reply) => {
fastify.get('/one', async (_request, reply) => {
expectType<(tag?: string, timeToLive?: number) => FastifyReply>(reply.etag)
expectType<(date?: Date) => FastifyReply>(reply.expires)

Expand All @@ -32,7 +32,7 @@ fastify.get('/one', async (request, reply) => {
return { message: 'one' }
})

fastify.get('/two', async (request, reply) => {
fastify.get('/two', async (_request, reply) => {
expectType<FastifyReply>(
reply.etag('hello', 6000).expires(new Date(Date.now() + 6000))
)
Expand All @@ -51,7 +51,7 @@ const badCachingOptions = {

expectError(shouldErrorApp.register(fastifyCaching, badCachingOptions))

fastify.get('/three', async (request, reply) => {
fastify.get('/three', async () => {
expectAssignable<Promise<unknown>>(
fastify.cache.get('well-known')
)
Expand Down
Loading