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

Function parseCacheControlHeader

lib/util/cache.js:354–570  ·  view source on GitHub ↗

* @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)

Source from the content-addressed store, hash-verified

352 * @returns {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives}
353 */
354function parseCacheControlHeader (header) {
355 /**
356 * @type {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives}
357 */
358 const output = {}
359 const invalidNumericDirectives = new Set()
360 const invalidNoArgumentDirectives = new Set()
361
362 const directives = splitCacheControlHeaderValue(Array.isArray(header) ? header.join(',') : header)
363
364 for (let i = 0; i < directives.length; i++) {
365 const directiveRecord = directives[i]
366 const directive = directiveRecord.value.toLowerCase()
367 const fromMalformedQuote = directiveRecord.fromMalformedQuote
368 const keyValueDelimiter = directive.indexOf('=')
369
370 let key
371 let value
372 let keyHasTrailingWhitespace = false
373 let valueHasLeadingWhitespace = false
374 if (keyValueDelimiter !== -1) {
375 const rawKey = directive.substring(0, keyValueDelimiter)
376 const rawValue = directive.substring(keyValueDelimiter + 1)
377
378 keyHasTrailingWhitespace = trimOWSEnd(rawKey) !== rawKey
379 valueHasLeadingWhitespace = trimOWSStart(rawValue) !== rawValue
380 key = trimOWS(rawKey)
381 value = trimOWSStart(rawValue)
382 } else {
383 key = trimOWS(directive)
384 }
385
386 const malformedRestrictiveDirectiveName = getMalformedRestrictiveDirectiveName(key)
387 if (malformedRestrictiveDirectiveName !== undefined) {
388 output[malformedRestrictiveDirectiveName] = true
389 continue
390 }
391
392 switch (key) {
393 case 'min-fresh':
394 case 'max-stale':
395 case 'max-age':
396 case 's-maxage':
397 case 'stale-while-revalidate':
398 case 'stale-if-error': {
399 if (fromMalformedQuote || invalidNumericDirectives.has(key)) {
400 continue
401 }
402
403 if (value === undefined || keyHasTrailingWhitespace || valueHasLeadingWhitespace) {
404 delete output[key]
405 invalidNumericDirectives.add(key)
406 markInvalidCacheControlDirective(output, key)
407 continue
408 }
409
410 if (
411 value.length >= 2 &&

Callers 4

cache.jsFile · 0.85
onResponseStartMethod · 0.85
utils.jsFile · 0.85

Calls 11

trimOWSEndFunction · 0.85
trimOWSStartFunction · 0.85
findUnescapedQuoteFunction · 0.85
isValidHTTPTokenFunction · 0.85
trimOWSFunction · 0.70
hasMethod · 0.65
addMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected