MCPcopy
hub / github.com/nodejs/undici / determineDeleteAt

Function determineDeleteAt

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

492 * @param {number} staleAt
493 */
494function determineDeleteAt (baseTime, cachedAt, cacheControlDirectives, staleAt) {
495 let staleWhileRevalidate = -Infinity
496 let staleIfError = -Infinity
497 let immutable = -Infinity
498
499 if (cacheControlDirectives['stale-while-revalidate']) {
500 staleWhileRevalidate = staleAt + (cacheControlDirectives['stale-while-revalidate'] * 1000)
501 }
502
503 if (cacheControlDirectives['stale-if-error']) {
504 staleIfError = staleAt + (cacheControlDirectives['stale-if-error'] * 1000)
505 }
506
507 if (cacheControlDirectives.immutable && staleWhileRevalidate === -Infinity && staleIfError === -Infinity) {
508 immutable = cachedAt + 31536000000
509 }
510
511 // When no stale directives or immutable flag, add a revalidation buffer
512 // equal to the freshness lifetime so the entry survives past staleAt long
513 // enough to be revalidated instead of silently disappearing.
514 //
515 // Response Date headers only have second precision, so baseTime can trail the
516 // actual cache insertion time by up to ~1s. Pad the buffer by that bounded
517 // skew so short-lived entries do not disappear exactly when they should be
518 // revalidated.
519 if (staleWhileRevalidate === -Infinity && staleIfError === -Infinity && immutable === -Infinity) {
520 const freshnessLifetime = staleAt - baseTime
521 const datePrecisionPadding = Math.min(Math.max(cachedAt - baseTime, 0), 1000)
522 return staleAt + freshnessLifetime + datePrecisionPadding
523 }
524
525 return Math.max(staleAt, staleWhileRevalidate, staleIfError, immutable)
526}
527
528/**
529 * Strips headers required to be removed in cached responses

Callers 1

onResponseStartMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…