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

Function determineStaleAt

lib/handler/cache-handler.js:613–704  ·  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, hasValidator)

Source from the content-addressed store, hash-verified

611 * @returns {number | undefined} time that the value is stale at in seconds or undefined if it shouldn't be cached
612 */
613function determineStaleAt (cacheType, now, age, resHeaders, responseDate, cacheControlDirectives, hasValidator) {
614 if (cacheType === 'shared') {
615 // Prioritize s-maxage since we're a shared cache
616 // s-maxage > max-age > Expire
617 // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.2.10-3
618 if (hasInvalidCacheControlDirective(cacheControlDirectives, 's-maxage')) {
619 return 0
620 }
621
622 const sMaxAge = cacheControlDirectives['s-maxage']
623 if (sMaxAge !== undefined) {
624 if (sMaxAge > 0) {
625 return sMaxAge * 1000
626 }
627
628 // Immediately stale, but storable if we can revalidate it before reuse.
629 return 0
630 }
631 }
632
633 if (hasInvalidCacheControlDirective(cacheControlDirectives, 'max-age')) {
634 return 0
635 }
636
637 const maxAge = cacheControlDirectives['max-age']
638 if (maxAge !== undefined) {
639 if (maxAge > 0) {
640 return maxAge * 1000
641 }
642
643 // Immediately stale, but storable if we can revalidate it before reuse.
644 return 0
645 }
646
647 if (Object.hasOwn(resHeaders, 'expires')) {
648 // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.3
649 if (typeof resHeaders.expires !== 'string') {
650 return 0
651 }
652
653 const expiresDate = parseHttpDate(resHeaders.expires)
654 if (!expiresDate) {
655 return 0
656 }
657
658 if (now >= expiresDate.getTime()) {
659 return 0
660 }
661
662 if (responseDate) {
663 if (responseDate >= expiresDate) {
664 return 0
665 }
666
667 const freshnessLifetime = expiresDate.getTime() - responseDate.getTime()
668 if (age !== undefined && age >= freshnessLifetime) {
669 return 0
670 }

Callers 1

onResponseStartMethod · 0.85

Calls 2

parseHttpDateFunction · 0.85

Tested by

no test coverage detected