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

Function parseCacheControlHeader

lib/util/cache.js:314–530  ·  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

312 * @returns {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives}
313 */
314function parseCacheControlHeader (header) {
315 /**
316 * @type {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives}
317 */
318 const output = {}
319 const invalidNumericDirectives = new Set()
320 const invalidNoArgumentDirectives = new Set()
321
322 const directives = splitCacheControlHeaderValue(Array.isArray(header) ? header.join(',') : header)
323
324 for (let i = 0; i < directives.length; i++) {
325 const directiveRecord = directives[i]
326 const directive = directiveRecord.value.toLowerCase()
327 const fromMalformedQuote = directiveRecord.fromMalformedQuote
328 const keyValueDelimiter = directive.indexOf('=')
329
330 let key
331 let value
332 let keyHasTrailingWhitespace = false
333 let valueHasLeadingWhitespace = false
334 if (keyValueDelimiter !== -1) {
335 const rawKey = directive.substring(0, keyValueDelimiter)
336 const rawValue = directive.substring(keyValueDelimiter + 1)
337
338 keyHasTrailingWhitespace = trimOWSEnd(rawKey) !== rawKey
339 valueHasLeadingWhitespace = trimOWSStart(rawValue) !== rawValue
340 key = trimOWS(rawKey)
341 value = trimOWSStart(rawValue)
342 } else {
343 key = trimOWS(directive)
344 }
345
346 const malformedRestrictiveDirectiveName = getMalformedRestrictiveDirectiveName(key)
347 if (malformedRestrictiveDirectiveName !== undefined) {
348 output[malformedRestrictiveDirectiveName] = true
349 continue
350 }
351
352 switch (key) {
353 case 'min-fresh':
354 case 'max-stale':
355 case 'max-age':
356 case 's-maxage':
357 case 'stale-while-revalidate':
358 case 'stale-if-error': {
359 if (fromMalformedQuote || invalidNumericDirectives.has(key)) {
360 continue
361 }
362
363 if (value === undefined || keyHasTrailingWhitespace || valueHasLeadingWhitespace) {
364 delete output[key]
365 invalidNumericDirectives.add(key)
366 markInvalidCacheControlDirective(output, key)
367 continue
368 }
369
370 if (
371 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