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

Function determineStaleAt

lib/handler/cache-handler.js:428–486  ·  view source on GitHub ↗

* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType * @param {number} now * @param {number | undefined} age * @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders * @param {Date | undefined} responseDate * @param {import('../../typ

(cacheType, now, age, resHeaders, responseDate, cacheControlDirectives)

Source from the content-addressed store, hash-verified

426 * @returns {number | undefined} time that the value is stale at in seconds or undefined if it shouldn't be cached
427 */
428function determineStaleAt (cacheType, now, age, resHeaders, responseDate, cacheControlDirectives) {
429 if (cacheType === 'shared') {
430 // Prioritize s-maxage since we're a shared cache
431 // s-maxage > max-age > Expire
432 // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.2.10-3
433 const sMaxAge = cacheControlDirectives['s-maxage']
434 if (sMaxAge !== undefined) {
435 return sMaxAge > 0 ? sMaxAge * 1000 : undefined
436 }
437 }
438
439 const maxAge = cacheControlDirectives['max-age']
440 if (maxAge !== undefined) {
441 return maxAge > 0 ? maxAge * 1000 : undefined
442 }
443
444 if (typeof resHeaders.expires === 'string') {
445 // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.3
446 const expiresDate = parseHttpDate(resHeaders.expires)
447 if (expiresDate) {
448 if (now >= expiresDate.getTime()) {
449 return undefined
450 }
451
452 if (responseDate) {
453 if (responseDate >= expiresDate) {
454 return undefined
455 }
456
457 if (age !== undefined && age > (expiresDate - responseDate)) {
458 return undefined
459 }
460 }
461
462 return expiresDate.getTime() - now
463 }
464 }
465
466 if (typeof resHeaders['last-modified'] === 'string') {
467 // https://www.rfc-editor.org/rfc/rfc9111.html#name-calculating-heuristic-fresh
468 const lastModified = new Date(resHeaders['last-modified'])
469 if (isValidDate(lastModified)) {
470 if (lastModified.getTime() >= now) {
471 return undefined
472 }
473
474 const responseAge = now - lastModified.getTime()
475
476 return responseAge * 0.1
477 }
478 }
479
480 if (cacheControlDirectives.immutable) {
481 // https://www.rfc-editor.org/rfc/rfc8246.html#section-2.2
482 return 31536000000
483 }
484
485 return undefined

Callers 1

onResponseStartMethod · 0.85

Calls 2

parseHttpDateFunction · 0.85
isValidDateFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…