* @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)
| 635 | * @returns {number | undefined} time that the value is stale at in seconds or undefined if it shouldn't be cached |
| 636 | */ |
| 637 | function determineStaleAt (cacheType, now, age, resHeaders, responseDate, cacheControlDirectives, hasValidator) { |
| 638 | if (cacheType === 'shared') { |
| 639 | // Prioritize s-maxage since we're a shared cache |
| 640 | // s-maxage > max-age > Expire |
| 641 | // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.2.10-3 |
| 642 | if (hasInvalidCacheControlDirective(cacheControlDirectives, 's-maxage')) { |
| 643 | return 0 |
| 644 | } |
| 645 | |
| 646 | const sMaxAge = cacheControlDirectives['s-maxage'] |
| 647 | if (sMaxAge !== undefined) { |
| 648 | if (sMaxAge > 0) { |
| 649 | return sMaxAge * 1000 |
| 650 | } |
| 651 | |
| 652 | // Immediately stale, but storable if we can revalidate it before reuse. |
| 653 | return 0 |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | if (hasInvalidCacheControlDirective(cacheControlDirectives, 'max-age')) { |
| 658 | return 0 |
| 659 | } |
| 660 | |
| 661 | const maxAge = cacheControlDirectives['max-age'] |
| 662 | if (maxAge !== undefined) { |
| 663 | if (maxAge > 0) { |
| 664 | return maxAge * 1000 |
| 665 | } |
| 666 | |
| 667 | // Immediately stale, but storable if we can revalidate it before reuse. |
| 668 | return 0 |
| 669 | } |
| 670 | |
| 671 | if (Object.hasOwn(resHeaders, 'expires')) { |
| 672 | // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.3 |
| 673 | if (typeof resHeaders.expires !== 'string') { |
| 674 | return 0 |
| 675 | } |
| 676 | |
| 677 | const expiresDate = parseHttpDate(resHeaders.expires) |
| 678 | if (!expiresDate) { |
| 679 | return 0 |
| 680 | } |
| 681 | |
| 682 | if (now >= expiresDate.getTime()) { |
| 683 | return 0 |
| 684 | } |
| 685 | |
| 686 | if (responseDate) { |
| 687 | if (responseDate >= expiresDate) { |
| 688 | return 0 |
| 689 | } |
| 690 | |
| 691 | const freshnessLifetime = expiresDate.getTime() - responseDate.getTime() |
| 692 | if (age !== undefined && age >= freshnessLifetime) { |
| 693 | return 0 |
| 694 | } |
no test coverage detected