* @see https://www.rfc-editor.org/rfc/rfc9111.html#name-cache-control * @see https://www.iana.org/assignments/http-cache-directives/http-cache-directives.xhtml * @param {string | string[]} header * @returns {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives}
(header)
| 122 | * @returns {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} |
| 123 | */ |
| 124 | function parseCacheControlHeader (header) { |
| 125 | /** |
| 126 | * @type {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} |
| 127 | */ |
| 128 | const output = {} |
| 129 | |
| 130 | let directives |
| 131 | if (Array.isArray(header)) { |
| 132 | directives = [] |
| 133 | |
| 134 | for (const directive of header) { |
| 135 | directives.push(...directive.split(',')) |
| 136 | } |
| 137 | } else { |
| 138 | directives = header.split(',') |
| 139 | } |
| 140 | |
| 141 | for (let i = 0; i < directives.length; i++) { |
| 142 | const directive = directives[i].toLowerCase() |
| 143 | const keyValueDelimiter = directive.indexOf('=') |
| 144 | |
| 145 | let key |
| 146 | let value |
| 147 | if (keyValueDelimiter !== -1) { |
| 148 | key = directive.substring(0, keyValueDelimiter).trimStart() |
| 149 | value = directive.substring(keyValueDelimiter + 1) |
| 150 | } else { |
| 151 | key = directive.trim() |
| 152 | } |
| 153 | |
| 154 | switch (key) { |
| 155 | case 'min-fresh': |
| 156 | case 'max-stale': |
| 157 | case 'max-age': |
| 158 | case 's-maxage': |
| 159 | case 'stale-while-revalidate': |
| 160 | case 'stale-if-error': { |
| 161 | if (value === undefined || value[0] === ' ') { |
| 162 | continue |
| 163 | } |
| 164 | |
| 165 | if ( |
| 166 | value.length >= 2 && |
| 167 | value[0] === '"' && |
| 168 | value[value.length - 1] === '"' |
| 169 | ) { |
| 170 | value = value.substring(1, value.length - 1) |
| 171 | } |
| 172 | |
| 173 | const parsedValue = parseInt(value, 10) |
| 174 | // eslint-disable-next-line no-self-compare |
| 175 | if (parsedValue !== parsedValue) { |
| 176 | continue |
| 177 | } |
| 178 | |
| 179 | if (key === 'max-age' && key in output && output[key] >= parsedValue) { |
| 180 | continue |
| 181 | } |
no test coverage detected
searching dependent graphs…