MCPcopy Create free account
hub / github.com/nodejs/node / isStale

Function isStale

deps/undici/src/lib/interceptor/cache.js:65–91  ·  view source on GitHub ↗

* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives | undefined} cacheControlDirectives * @returns {boolean}

(result, cacheControlDirectives)

Source from the content-addressed store, hash-verified

63 * @returns {boolean}
64 */
65function isStale (result, cacheControlDirectives) {
66 const now = Date.now()
67 if (now > result.staleAt) {
68 // Response is stale
69 if (cacheControlDirectives?.['max-stale']) {
70 // There's a threshold where we can serve stale responses, let's see if
71 // we're in it
72 // https://www.rfc-editor.org/rfc/rfc9111.html#name-max-stale
73 const gracePeriod = result.staleAt + (cacheControlDirectives['max-stale'] * 1000)
74 return now > gracePeriod
75 }
76
77 return true
78 }
79
80 if (cacheControlDirectives?.['min-fresh']) {
81 // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.1.3
82
83 // At this point, staleAt is always > now
84 const timeLeftTillStale = result.staleAt - now
85 const threshold = cacheControlDirectives['min-fresh'] * 1000
86
87 return timeLeftTillStale <= threshold
88 }
89
90 return false
91}
92
93/**
94 * Check if we're within the stale-while-revalidate window for a stale response

Callers 1

handleResultFunction · 0.85

Calls 1

nowMethod · 0.80

Tested by

no test coverage detected