* @param {number} baseTime * @param {number} cachedAt * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives * @param {number} staleAt
(baseTime, cachedAt, cacheControlDirectives, staleAt)
| 492 | * @param {number} staleAt |
| 493 | */ |
| 494 | function 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 |
no outgoing calls
no test coverage detected
searching dependent graphs…