cacheable-response
Advanced tools
Comparing version 2.4.3 to 2.5.0
@@ -5,2 +5,9 @@ # Changelog | ||
## 2.5.0 (2021-08-16) | ||
### Features | ||
* be possible to set staleTtl from data ([026a27b](https://github.com/Kikobeats/cacheable-response/commit/026a27b5b9ef6985ccac462b0ad92d87a2798f39)) | ||
### 2.4.3 (2021-08-12) | ||
@@ -7,0 +14,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://nicedoc.io/Kikobeats/cacheable-response", | ||
"version": "2.4.3", | ||
"version": "2.5.0", | ||
"main": "src/index.js", | ||
@@ -59,3 +59,3 @@ "author": { | ||
"@keyvhq/core": "~1.1.1", | ||
"@keyvhq/memoize": "~1.2.0", | ||
"@keyvhq/memoize": "~1.2.2", | ||
"compress-brotli": "~1.3.0", | ||
@@ -62,0 +62,0 @@ "debug-logfmt": "~1.0.4", |
@@ -10,3 +10,3 @@ 'use strict' | ||
const { hasQueryParameter, createSetHeaders } = require('./util') | ||
const { hasQueryParameter, setHeaders } = require('./util') | ||
@@ -21,3 +21,3 @@ const cacheableResponse = ({ | ||
staleTtl: rawStaleTtl = 3600000, | ||
ttl: defaultTtl = 86400000, | ||
ttl: rawTtl = 86400000, | ||
...compressOpts | ||
@@ -28,4 +28,13 @@ } = {}) => { | ||
const staleTtl = typeof rawStaleTtl === 'number' ? rawStaleTtl : undefined | ||
const isStaleEnabled = rawStaleTtl !== false | ||
const staleTtl = isStaleEnabled | ||
? typeof rawStaleTtl === 'function' | ||
? rawStaleTtl | ||
: ({ staleTtl = rawStaleTtl } = {}) => staleTtl | ||
: undefined | ||
const ttl = | ||
typeof rawTtl === 'function' ? rawTtl : ({ ttl = rawTtl } = {}) => ttl | ||
const { serialize, compress, decompress } = createCompress({ | ||
@@ -37,3 +46,3 @@ enable: enableCompression, | ||
const memoGet = memoize(get, cache, { | ||
ttl: ({ ttl = defaultTtl } = {}) => ttl, | ||
ttl, | ||
staleTtl, | ||
@@ -45,4 +54,2 @@ objectMode: true, | ||
const setHeaders = createSetHeaders({ staleTtl }) | ||
return async opts => { | ||
@@ -58,6 +65,7 @@ const { req, res } = opts | ||
const { | ||
etag: cachedEtag, | ||
ttl = defaultTtl, | ||
createdAt = Date.now(), | ||
data = null, | ||
etag: cachedEtag, | ||
staleTtl = isStaleEnabled ? memoGet.staleTtl(result) : undefined, | ||
ttl = memoGet.ttl(result), | ||
...props | ||
@@ -88,2 +96,3 @@ } = result | ||
ttl, | ||
staleTtl, | ||
hasForce | ||
@@ -90,0 +99,0 @@ }) |
@@ -31,27 +31,34 @@ 'use strict' | ||
const createSetHeaders = ({ staleTtl }) => { | ||
return ({ res, createdAt, isHit, isStale, ttl, hasForce, etag }) => { | ||
// Specifies the maximum amount of time a resource | ||
// will be considered fresh in seconds | ||
const diff = hasForce ? 0 : createdAt + ttl - Date.now() | ||
const maxAge = toSeconds(diff) | ||
const revalidation = staleTtl ? toSeconds(staleTtl) : 0 | ||
const setHeaders = ({ | ||
createdAt, | ||
etag, | ||
hasForce, | ||
isHit, | ||
isStale, | ||
res, | ||
staleTtl, | ||
ttl | ||
}) => { | ||
// Specifies the maximum amount of time a resource | ||
// will be considered fresh in seconds | ||
const diff = hasForce ? 0 : createdAt + ttl - Date.now() | ||
const maxAge = toSeconds(diff) | ||
const revalidation = staleTtl ? toSeconds(staleTtl) : 0 | ||
let cacheControl = `public, must-revalidate, max-age=${maxAge}` | ||
let cacheControl = `public, must-revalidate, max-age=${maxAge}` | ||
if (revalidation) { | ||
cacheControl = `${cacheControl}, stale-while-revalidate=${revalidation}, stale-if-error=${revalidation}` | ||
} | ||
if (revalidation) { | ||
cacheControl = `${cacheControl}, stale-while-revalidate=${revalidation}, stale-if-error=${revalidation}` | ||
} | ||
res.setHeader('Cache-Control', cacheControl) | ||
res.setHeader('X-Cache-Status', getStatus({ isHit, isStale, hasForce })) | ||
res.setHeader('X-Cache-Expired-At', prettyMs(diff)) | ||
res.setHeader('ETag', etag) | ||
} | ||
res.setHeader('Cache-Control', cacheControl) | ||
res.setHeader('X-Cache-Status', getStatus({ isHit, isStale, hasForce })) | ||
res.setHeader('X-Cache-Expired-At', prettyMs(diff)) | ||
res.setHeader('ETag', etag) | ||
} | ||
module.exports = { | ||
getKey, | ||
hasQueryParameter, | ||
getKey, | ||
createSetHeaders | ||
setHeaders | ||
} |
37425
223
Updated@keyvhq/memoize@~1.2.2