MCPcopy Index your code
hub / github.com/angular/angular / retrieveStateFromCache

Function retrieveStateFromCache

packages/common/http/src/transfer_cache.ts:173–250  ·  view source on GitHub ↗
(
  req: HttpRequest<unknown>,
  options: CacheOptions,
  transferState: TransferState,
  originMap: Record<string, string> | null,
  storeKey?: StateKey<TransferHttpResponse>,
  skipUseCacheChecks = false,
)

Source from the content-addressed store, hash-verified

171 * @param skipUseCacheChecks Whether to skip the use cache checks. (Only disable when the checks have been performed beforehand).
172 */
173export function retrieveStateFromCache(
174 req: HttpRequest<unknown>,
175 options: CacheOptions,
176 transferState: TransferState,
177 originMap: Record<string, string> | null,
178 storeKey?: StateKey<TransferHttpResponse>,
179 skipUseCacheChecks = false,
180): HttpResponse<unknown> | null {
181 if (!skipUseCacheChecks && !canUseOrCacheRequest(req, options)) {
182 return null;
183 }
184
185 if (typeof ngServerMode !== 'undefined' && !ngServerMode && originMap) {
186 throw new RuntimeError(
187 RuntimeErrorCode.HTTP_ORIGIN_MAP_USED_IN_CLIENT,
188 ngDevMode &&
189 'Angular detected that the `HTTP_TRANSFER_CACHE_ORIGIN_MAP` token is configured and ' +
190 'present in the client side code. Please ensure that this token is only provided in the ' +
191 'server code of the application.',
192 );
193 }
194
195 if (!storeKey) {
196 const requestUrl =
197 typeof ngServerMode !== 'undefined' && ngServerMode && originMap
198 ? mapRequestOriginUrl(req.url, originMap)
199 : req.url;
200
201 storeKey = makeCacheKey(req, requestUrl);
202 }
203
204 const response = transferState.get(storeKey, null);
205
206 if (!response) {
207 return null;
208 }
209
210 const {
211 [BODY]: undecodedBody,
212 [RESPONSE_TYPE]: responseType,
213 [HEADERS]: httpHeaders,
214 [STATUS]: status,
215 [STATUS_TEXT]: statusText,
216 [REQ_URL]: url,
217 } = response;
218 // Request found in cache. Respond using it.
219 let body: ArrayBuffer | Blob | string | undefined = undecodedBody;
220
221 switch (responseType) {
222 case 'arraybuffer':
223 body = fromBase64(undecodedBody);
224 break;
225 case 'blob':
226 body = new Blob([fromBase64(undecodedBody)]);
227 break;
228 }
229
230 // We want to warn users accessing a header provided from the cache

Callers 2

getInitialStreamFunction · 0.90

Calls 7

canUseOrCacheRequestFunction · 0.85
mapRequestOriginUrlFunction · 0.85
makeCacheKeyFunction · 0.85
fromBase64Function · 0.85
getHeadersToIncludeFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…