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

Function appendMissingHeadersDetection

packages/common/http/src/transfer_cache.ts:477–518  ·  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

475 * and log a warning if the header entry requested has been removed
476 */
477function appendMissingHeadersDetection(
478 url: string,
479 headers: HttpHeaders,
480 headersToInclude: string[],
481): HttpHeaders {
482 const warningProduced = new Set();
483 return new Proxy<HttpHeaders>(headers, {
484 get(target: HttpHeaders, prop: keyof HttpHeaders): unknown {
485 const value = Reflect.get(target, prop);
486 const methods: Set<keyof HttpHeaders> = new Set(['get', 'has', 'getAll']);
487
488 if (typeof value !== 'function' || !methods.has(prop)) {
489 return value;
490 }
491
492 return (headerName: string) => {
493 // We log when the key has been removed and a warning hasn't been produced for the header
494 const key = (prop + ':' + headerName).toLowerCase(); // e.g. `get:cache-control`
495 if (!headersToInclude.includes(headerName) && !warningProduced.has(key)) {
496 warningProduced.add(key);
497 const truncatedUrl = truncateMiddle(url);
498
499 console.warn(
500 formatRuntimeError(
501 RuntimeErrorCode.HEADERS_ALTERED_BY_TRANSFER_CACHE,
502 `Angular detected that the \`${headerName}\` header is accessed, but the value of the header ` +
503 `was not transferred from the server to the client by the HttpTransferCache. ` +
504 `To include the value of the \`${headerName}\` header for the \`${truncatedUrl}\` request, ` +
505 `use the \`includeHeaders\` list. The \`includeHeaders\` can be defined either ` +
506 `on a request level by adding the \`transferCache\` parameter, or on an application ` +
507 `level by adding the \`httpCacheTransfer.includeHeaders\` argument to the ` +
508 `\`provideClientHydration()\` call. `,
509 ),
510 );
511 }
512
513 // invoking the original method
514 return (value as Function).apply(target, [headerName]);
515 };
516 },
517 });
518}
519
520function mapRequestOriginUrl(url: string, originMap: Record<string, string>): string {
521 const origin = new URL(url, 'resolve://').origin;

Callers 1

retrieveStateFromCacheFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected