MCPcopy Create free account
hub / github.com/nodejs/undici / determineDeleteAt

Function determineDeleteAt

lib/handler/cache-handler.js:736–773  ·  view source on GitHub ↗

* @param {number} baseTime * @param {number} cachedAt * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives * @param {number} staleAt

(baseTime, cachedAt, cacheControlDirectives, staleAt)

Source from the content-addressed store, hash-verified

734 * @param {number} staleAt
735 */
736function determineDeleteAt (baseTime, cachedAt, cacheControlDirectives, staleAt) {
737 let staleWhileRevalidate = -Infinity
738 let staleIfError = -Infinity
739 let immutable = -Infinity
740
741 if (cacheControlDirectives['stale-while-revalidate']) {
742 staleWhileRevalidate = staleAt + (cacheControlDirectives['stale-while-revalidate'] * 1000)
743 }
744
745 if (cacheControlDirectives['stale-if-error']) {
746 staleIfError = staleAt + (cacheControlDirectives['stale-if-error'] * 1000)
747 }
748
749 if (cacheControlDirectives.immutable && staleWhileRevalidate === -Infinity && staleIfError === -Infinity) {
750 immutable = cachedAt + 31536000000
751 }
752
753 // When no stale directives or immutable flag, add a revalidation buffer
754 // equal to the freshness lifetime so the entry survives past staleAt long
755 // enough to be revalidated instead of silently disappearing.
756 //
757 // Response Date headers only have second precision, so baseTime can trail the
758 // actual cache insertion time by up to ~1s. Pad the buffer by that bounded
759 // skew so short-lived entries do not disappear exactly when they should be
760 // revalidated.
761 if (staleWhileRevalidate === -Infinity && staleIfError === -Infinity && immutable === -Infinity) {
762 const freshnessLifetime = staleAt - baseTime
763 if (freshnessLifetime <= 0) {
764 // Revalidation-only entry: no freshness lifetime to size the buffer on,
765 // so retain it for a bounded window instead.
766 return cachedAt + REVALIDATION_ONLY_RETENTION
767 }
768 const datePrecisionPadding = Math.min(Math.max(cachedAt - baseTime, 0), 1000)
769 return staleAt + freshnessLifetime + datePrecisionPadding
770 }
771
772 return Math.max(staleAt, staleWhileRevalidate, staleIfError, immutable)
773}
774
775/**
776 * Strips headers required to be removed in cached responses

Callers 1

onResponseStartMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected