MCPcopy Create free account
hub / github.com/angular/angular / appendMissingHeadersDetection

Function appendMissingHeadersDetection

packages/common/http/src/transfer_cache.ts:503–544  ·  view source on GitHub ↗

* This function will add a proxy to an HttpHeader to intercept calls to get/has * and log a warning if the header entry requested has been removed

(
  url: string,
  headers: HttpHeaders,
  headersToInclude: string[],
)

Source from the content-addressed store, hash-verified

501 * and log a warning if the header entry requested has been removed
502 */
503function appendMissingHeadersDetection(
504 url: string,
505 headers: HttpHeaders,
506 headersToInclude: string[],
507): HttpHeaders {
508 const warningProduced = new Set();
509 return new Proxy<HttpHeaders>(headers, {
510 get(target: HttpHeaders, prop: keyof HttpHeaders): unknown {
511 const value = Reflect.get(target, prop);
512 const methods: Set<keyof HttpHeaders> = new Set(['get', 'has', 'getAll']);
513
514 if (typeof value !== 'function' || !methods.has(prop)) {
515 return value;
516 }
517
518 return (headerName: string) => {
519 // We log when the key has been removed and a warning hasn't been produced for the header
520 const key = (prop + ':' + headerName).toLowerCase(); // e.g. `get:cache-control`
521 if (!headersToInclude.includes(headerName) && !warningProduced.has(key)) {
522 warningProduced.add(key);
523 const truncatedUrl = truncateMiddle(url);
524
525 console.warn(
526 formatRuntimeError(
527 RuntimeErrorCode.HEADERS_ALTERED_BY_TRANSFER_CACHE,
528 `Angular detected that the \`${headerName}\` header is accessed, but the value of the header ` +
529 `was not transferred from the server to the client by the HttpTransferCache. ` +
530 `To include the value of the \`${headerName}\` header for the \`${truncatedUrl}\` request, ` +
531 `use the \`includeHeaders\` list. The \`includeHeaders\` can be defined either ` +
532 `on a request level by adding the \`transferCache\` parameter, or on an application ` +
533 `level by adding the \`httpCacheTransfer.includeHeaders\` argument to the ` +
534 `\`provideClientHydration()\` call. `,
535 ),
536 );
537 }
538
539 // invoking the original method
540 return (value as Function).apply(target, [headerName]);
541 };
542 },
543 });
544}
545
546function mapRequestOriginUrl(url: string, originMap: Record<string, string>): string {
547 const origin = new URL(url, 'resolve://').origin;

Callers 1

retrieveStateFromCacheFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected