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

Function determineDeleteAt

lib/handler/cache-handler.js:712–749  ·  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

710 * @param {number} staleAt
711 */
712function determineDeleteAt (baseTime, cachedAt, cacheControlDirectives, staleAt) {
713 let staleWhileRevalidate = -Infinity
714 let staleIfError = -Infinity
715 let immutable = -Infinity
716
717 if (cacheControlDirectives['stale-while-revalidate']) {
718 staleWhileRevalidate = staleAt + (cacheControlDirectives['stale-while-revalidate'] * 1000)
719 }
720
721 if (cacheControlDirectives['stale-if-error']) {
722 staleIfError = staleAt + (cacheControlDirectives['stale-if-error'] * 1000)
723 }
724
725 if (cacheControlDirectives.immutable && staleWhileRevalidate === -Infinity && staleIfError === -Infinity) {
726 immutable = cachedAt + 31536000000
727 }
728
729 // When no stale directives or immutable flag, add a revalidation buffer
730 // equal to the freshness lifetime so the entry survives past staleAt long
731 // enough to be revalidated instead of silently disappearing.
732 //
733 // Response Date headers only have second precision, so baseTime can trail the
734 // actual cache insertion time by up to ~1s. Pad the buffer by that bounded
735 // skew so short-lived entries do not disappear exactly when they should be
736 // revalidated.
737 if (staleWhileRevalidate === -Infinity && staleIfError === -Infinity && immutable === -Infinity) {
738 const freshnessLifetime = staleAt - baseTime
739 if (freshnessLifetime <= 0) {
740 // Revalidation-only entry: no freshness lifetime to size the buffer on,
741 // so retain it for a bounded window instead.
742 return cachedAt + REVALIDATION_ONLY_RETENTION
743 }
744 const datePrecisionPadding = Math.min(Math.max(cachedAt - baseTime, 0), 1000)
745 return staleAt + freshnessLifetime + datePrecisionPadding
746 }
747
748 return Math.max(staleAt, staleWhileRevalidate, staleIfError, immutable)
749}
750
751/**
752 * Strips headers required to be removed in cached responses

Callers 1

onResponseStartMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected