diff --git a/index.d.ts b/index.d.ts
index 48501a1..652c371 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -106,16 +106,32 @@ declare namespace CacheableResponse {
export type GetOpts = HttpContext & Props;
+ /**
+ * Stored when `compress` is enabled.
+ * Lifetimes sit beside the brotli payload so expiry does not depend on decompressing it.
+ * Entries written before that are still a raw `Buffer`.
+ */
+ export interface CompressedEntry {
+ compressed: Buffer;
+ ttl: number;
+ staleTtl: number | boolean;
+ }
+
export interface CacheProvider
{
- /** Returns the cached value, or the compressed buffer when compress option is set to true. */
- get(key: string): Promise & P) | undefined>;
+ /**
+ * Returns the cached value.
+ * With `compress` enabled this is a `CompressedEntry`, or a `Buffer` for older entries.
+ */
+ get(
+ key: string
+ ): Promise & P) | undefined>;
/**
* Set a value. You can implements an expiry TTL in milliseconds.
*/
set(
key: string,
- value: Buffer | (Cache & P),
+ value: Buffer | CompressedEntry | (Cache & P),
ttl: number
): Promise;
}
diff --git a/src/index.js b/src/index.js
index 280374f..36f1dc5 100644
--- a/src/index.js
+++ b/src/index.js
@@ -33,6 +33,32 @@ const cacheableResponse = ({
...compressOpts
})
+ // memoize calls ttl/staleTtl on the stored value. A brotli payload is a
+ // Buffer, so those fields would be invisible and every entry would keep the
+ // default lifetime. Keep them beside the payload; raw buffers still load.
+ const isCompressedEntry = value =>
+ enableCompression &&
+ value != null &&
+ typeof value === 'object' &&
+ Object.prototype.hasOwnProperty.call(value, 'compressed')
+
+ const storeValue = async data => {
+ const payload = await compress(data)
+ if (!enableCompression || data == null || typeof data !== 'object') { return payload }
+ return {
+ compressed: payload,
+ ttl: ttl(data),
+ staleTtl: staleTtl(data)
+ }
+ }
+
+ const loadValue = data =>
+ isCompressedEntry(data) ? decompress(data.compressed) : decompress(data)
+
+ const ttlOf = value => (isCompressedEntry(value) ? value.ttl : ttl(value))
+ const staleTtlOf = value =>
+ isCompressedEntry(value) ? value.staleTtl : staleTtl(value)
+
const getEtag = input => require('etag')(serialize(input))
const get = opts =>
@@ -55,9 +81,9 @@ const cacheableResponse = ({
const memoGet = memoize(get, cache, {
key: memoKey,
objectMode: true,
- staleTtl,
- ttl,
- value: compress
+ staleTtl: staleTtlOf,
+ ttl: ttlOf,
+ value: storeValue
})
const fn = async opts => {
@@ -67,7 +93,7 @@ const cacheableResponse = ({
if (res.writableEnded) return
- const result = (await decompress(raw)) || {}
+ const result = (await loadValue(raw)) || {}
const isHit = !forceExpiration && !isExpired && hasValue
const preventCaching = raw == null
@@ -75,11 +101,17 @@ const cacheableResponse = ({
createdAt = Date.now(),
data = null,
etag = getEtag(result),
- staleTtl = memoGet.staleTtl(result),
- ttl = memoGet.ttl(result),
+ staleTtl: resultStaleTtl,
+ ttl: resultTtl,
...props
} = result
+ // A cached value may itself have a `compressed` field. That is not the
+ // storage envelope, so don't read lifetimes with the envelope predicate.
+ const responseStaleTtl =
+ resultStaleTtl === undefined ? staleTtl(result) : resultStaleTtl
+ const responseTtl = resultTtl === undefined ? ttl(result) : resultTtl
+
const ifNoneMatch = req.headers['if-none-match']
const isModified = etag !== ifNoneMatch
@@ -103,8 +135,8 @@ const cacheableResponse = ({
isStale,
preventCaching,
res,
- staleTtl,
- ttl
+ staleTtl: responseStaleTtl,
+ ttl: responseTtl
})
if (hasValue && !forceExpiration && !isModified) {
diff --git a/test/index.js b/test/index.js
index 6b4a7d9..00a0bbb 100644
--- a/test/index.js
+++ b/test/index.js
@@ -37,6 +37,8 @@ test('compress support', async t => {
t,
cacheableResponse({
compress: true,
+ // Shorter than the default stale window; disable it so a fresh entry is a HIT.
+ staleTtl: false,
get: ({ req, res }) => {
return {
data: { foo: 'bar' },
@@ -57,6 +59,52 @@ test('compress support', async t => {
t.is(headersOne.etag, headersTwo.etag)
})
+test('compressed entries written as raw buffers still hit', async t => {
+ const Keyv = require('@keyvhq/core')
+ const createCompress = require('compress-brotli')
+ const cache = new Keyv({ namespace: 'ssr' })
+ const { compress } = createCompress({ enable: true })
+ await cache.set(
+ '/compat',
+ await compress({ data: { foo: 'from-raw' }, createdAt: Date.now() }),
+ 86400000
+ )
+
+ const url = await runServer(
+ t,
+ cacheableResponse({
+ cache,
+ compress: true,
+ staleTtl: false,
+ get: () => ({ data: { foo: 'fresh' } }),
+ send: ({ data, res }) => res.end(data.foo)
+ })
+ )
+
+ const { headers, body } = await got(`${url}/compat`)
+ t.is(headers['x-cache-status'], 'HIT')
+ t.is(body, 'from-raw')
+})
+
+test('uncompressed values keep their own compressed field', async t => {
+ const url = await runServer(
+ t,
+ cacheableResponse({
+ get: () => ({ data: { foo: 'bar' }, compressed: { algo: 'br' } }),
+ send: ({ data, compressed, res }) =>
+ res.end(`${data.foo}|${compressed.algo}`)
+ })
+ )
+
+ const miss = await got(`${url}/kikobeats`)
+ t.is(miss.headers['x-cache-status'], 'MISS')
+ t.is(miss.body, 'bar|br')
+
+ const hit = await got(`${url}/kikobeats`)
+ t.is(hit.headers['x-cache-status'], 'HIT')
+ t.is(hit.body, 'bar|br')
+})
+
test('exit early is response was written', async t => {
let isEnd = false
const end = (res, msg) => {
diff --git a/test/ttl.js b/test/ttl.js
index fda32ef..73b7fe4 100644
--- a/test/ttl.js
+++ b/test/ttl.js
@@ -97,3 +97,148 @@ test.serial(
t.is(parseCacheControl(hit)['max-age'], 3570)
}
)
+
+test.serial('compressed entries expire on their own ttl', async t => {
+ const realNow = Date.now
+ let now = 1_700_000_000_000
+ Date.now = () => now
+ t.teardown(() => {
+ Date.now = realNow
+ })
+
+ let calls = 0
+ const url = await runServer(
+ t,
+ cacheableResponse({
+ compress: true,
+ ttl: 86400000,
+ staleTtl: false,
+ get: () => {
+ calls += 1
+ return { data: { n: calls }, ttl: 1000 }
+ },
+ send: ({ data, res }) => res.end(String(data.n))
+ })
+ )
+
+ const miss = await got(`${url}/kikobeats`)
+ t.is(miss.headers['x-cache-status'], 'MISS')
+ t.is(miss.body, '1')
+
+ now += 5000
+
+ const expired = await got(`${url}/kikobeats`)
+ t.is(expired.headers['x-cache-status'], 'EXPIRED')
+ t.is(expired.body, '2')
+})
+
+test.serial('compressed entries honor a per-entry staleTtl', async t => {
+ const realNow = Date.now
+ let now = 1_700_000_000_000
+ Date.now = () => now
+ t.teardown(() => {
+ Date.now = realNow
+ })
+
+ let calls = 0
+ const url = await runServer(
+ t,
+ cacheableResponse({
+ compress: true,
+ ttl: 86400000,
+ staleTtl: false,
+ get: () => {
+ calls += 1
+ return { data: { n: calls }, ttl: 20000, staleTtl: 15000 }
+ },
+ send: ({ data, res }) => res.end(String(data.n))
+ })
+ )
+
+ const miss = await got(`${url}/kikobeats`)
+ t.is(miss.headers['x-cache-status'], 'MISS')
+ t.is(miss.body, '1')
+
+ const hit = await got(`${url}/kikobeats`)
+ t.is(hit.headers['x-cache-status'], 'HIT')
+ t.is(hit.body, '1')
+
+ now += 6000
+
+ const stale = await got(`${url}/kikobeats`)
+ t.is(stale.headers['x-cache-status'], 'STALE')
+ t.is(stale.body, '1')
+ await new Promise(resolve => setImmediate(resolve))
+ t.is(calls, 2)
+})
+
+test.serial('compressed entries honor ttl and staleTtl functions', async t => {
+ const realNow = Date.now
+ let now = 1_700_000_000_000
+ Date.now = () => now
+ t.teardown(() => {
+ Date.now = realNow
+ })
+
+ let calls = 0
+ const url = await runServer(
+ t,
+ cacheableResponse({
+ compress: true,
+ ttl: value => value.seconds * 1000,
+ staleTtl: value => value.staleSeconds * 1000,
+ get: () => {
+ calls += 1
+ return { data: { n: calls }, seconds: 20, staleSeconds: 15 }
+ },
+ send: ({ data, res }) => res.end(String(data.n))
+ })
+ )
+
+ const miss = await got(`${url}/kikobeats`)
+ t.is(miss.headers['x-cache-status'], 'MISS')
+ t.is(miss.body, '1')
+ t.is(parseCacheControl(miss.headers)['max-age'], 20)
+ t.is(parseCacheControl(miss.headers)['stale-while-revalidate'], 15)
+
+ const hit = await got(`${url}/kikobeats`)
+ t.is(hit.headers['x-cache-status'], 'HIT')
+ t.is(hit.body, '1')
+
+ now += 6000
+
+ const stale = await got(`${url}/kikobeats`)
+ t.is(stale.headers['x-cache-status'], 'STALE')
+ t.is(stale.body, '1')
+ await new Promise(resolve => setImmediate(resolve))
+ t.is(calls, 2)
+})
+
+test('a compressed field on the value is not the storage envelope', async t => {
+ const url = await runServer(
+ t,
+ cacheableResponse({
+ compress: true,
+ ttl: 3600000,
+ staleTtl: 120000,
+ get: () => ({ data: { foo: 'bar' }, compressed: { algo: 'br' } }),
+ send: ({ data, compressed, res }) =>
+ res.end(`${data.foo}|${compressed.algo}`)
+ })
+ )
+
+ const miss = await got(`${url}/kikobeats`)
+ t.is(miss.headers['x-cache-status'], 'MISS')
+ t.is(miss.body, 'bar|br')
+ const missCache = parseCacheControl(miss.headers)
+ t.true([3600, 3599].includes(missCache['max-age']))
+ t.true([120, 119].includes(missCache['stale-while-revalidate']))
+ t.true([120, 119].includes(missCache['stale-if-error']))
+
+ const hit = await got(`${url}/kikobeats`)
+ t.is(hit.headers['x-cache-status'], 'HIT')
+ t.is(hit.body, 'bar|br')
+ const hitCache = parseCacheControl(hit.headers)
+ t.true([3600, 3599].includes(hitCache['max-age']))
+ t.true([120, 119].includes(hitCache['stale-while-revalidate']))
+})