From d8de05557489a11eec87d20a30837d7db3258310 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 10 Sep 2026 04:07:28 +0000 Subject: [PATCH 1/2] fix: persist default createdAt so HIT max-age can shrink Cache-Control max-age is createdAt + ttl - now. The documented Date.now() default was applied at read time, so every HIT looked freshly generated and browsers/CDNs could keep serving the copy for a full extra TTL. --- src/index.js | 3 +++ test/ttl.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/index.js b/src/index.js index 954717f..a659bd1 100644 --- a/src/index.js +++ b/src/index.js @@ -39,6 +39,9 @@ const cacheableResponse = ({ Promise.resolve(rawGet(opts)).then(result => { if (result == null) return undefined if (typeof result !== 'object') return result + // Stamp here, not at read time: Cache-Control max-age is createdAt + ttl + // - now. A read-time Date.now() default resets freshness on every HIT. + if (result.createdAt == null) result.createdAt = Date.now() result.etag = getEtag(result) return result }) diff --git a/test/ttl.js b/test/ttl.js index 8d698a0..3db5adb 100644 --- a/test/ttl.js +++ b/test/ttl.js @@ -63,3 +63,34 @@ test('sets default ttl', async t => { t.true(cacheControl['must-revalidate']) t.true([86400, 86399].includes(cacheControl['max-age'])) }) + +test('default createdAt is persisted so HIT max-age shrinks', async t => { + const realNow = Date.now + let now = 1_700_000_000_000 + Date.now = () => now + t.teardown(() => { + Date.now = realNow + }) + + const url = await runServer( + t, + cacheableResponse({ + ttl: 3600000, + staleTtl: false, + get: () => ({ data: { foo: 'bar' } }), + send: ({ res }) => { + res.end('Hello World') + } + }) + ) + + const { headers: miss } = await got(`${url}/kikobeats`) + t.is(miss['x-cache-status'], 'MISS') + t.is(parseCacheControl(miss)['max-age'], 3600) + + now += 30_000 + + const { headers: hit } = await got(`${url}/kikobeats`) + t.is(hit['x-cache-status'], 'HIT') + t.is(parseCacheControl(hit)['max-age'], 3570) +}) From 7c03c4d7e76aaa36f9a8456d18b9ca3d3f0199f2 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Thu, 10 Sep 2026 06:42:26 +0000 Subject: [PATCH 2/2] fix: stamp createdAt after etag without mutating get() Hash the payload before the default timestamp so regeneration stays content-based, and run the Date.now TTL case serially so it cannot race sibling tests. Co-authored-by: Cursor --- src/index.js | 10 +++++----- test/ttl.js | 55 +++++++++++++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/index.js b/src/index.js index a659bd1..280374f 100644 --- a/src/index.js +++ b/src/index.js @@ -39,11 +39,11 @@ const cacheableResponse = ({ Promise.resolve(rawGet(opts)).then(result => { if (result == null) return undefined if (typeof result !== 'object') return result - // Stamp here, not at read time: Cache-Control max-age is createdAt + ttl - // - now. A read-time Date.now() default resets freshness on every HIT. - if (result.createdAt == null) result.createdAt = Date.now() - result.etag = getEtag(result) - return result + // Persist createdAt at write time so HIT max-age can count down. + const value = { ...result } + value.etag = getEtag(value) + if (value.createdAt == null) value.createdAt = Date.now() + return value }) // @keyvhq/memoize only treats forceExpiration === true as a forced refresh. diff --git a/test/ttl.js b/test/ttl.js index 3db5adb..fda32ef 100644 --- a/test/ttl.js +++ b/test/ttl.js @@ -64,33 +64,36 @@ test('sets default ttl', async t => { t.true([86400, 86399].includes(cacheControl['max-age'])) }) -test('default createdAt is persisted so HIT max-age shrinks', async t => { - const realNow = Date.now - let now = 1_700_000_000_000 - Date.now = () => now - t.teardown(() => { - Date.now = realNow - }) - - const url = await runServer( - t, - cacheableResponse({ - ttl: 3600000, - staleTtl: false, - get: () => ({ data: { foo: 'bar' } }), - send: ({ res }) => { - res.end('Hello World') - } +test.serial( + 'default createdAt is persisted so HIT max-age shrinks', + async t => { + const realNow = Date.now + let now = 1_700_000_000_000 + Date.now = () => now + t.teardown(() => { + Date.now = realNow }) - ) - const { headers: miss } = await got(`${url}/kikobeats`) - t.is(miss['x-cache-status'], 'MISS') - t.is(parseCacheControl(miss)['max-age'], 3600) + const url = await runServer( + t, + cacheableResponse({ + ttl: 3600000, + staleTtl: false, + get: () => ({ data: { foo: 'bar' } }), + send: ({ res }) => { + res.end('Hello World') + } + }) + ) - now += 30_000 + const { headers: miss } = await got(`${url}/kikobeats`) + t.is(miss['x-cache-status'], 'MISS') + t.is(parseCacheControl(miss)['max-age'], 3600) - const { headers: hit } = await got(`${url}/kikobeats`) - t.is(hit['x-cache-status'], 'HIT') - t.is(parseCacheControl(hit)['max-age'], 3570) -}) + now += 30_000 + + const { headers: hit } = await got(`${url}/kikobeats`) + t.is(hit['x-cache-status'], 'HIT') + t.is(parseCacheControl(hit)['max-age'], 3570) + } +)