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

Function retrieveStateFromCache

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

197 * @param skipUseCacheChecks Whether to skip the use cache checks. (Only disable when the checks have been performed beforehand).
198 */
199export function retrieveStateFromCache(
200 req: HttpRequest<unknown>,
201 options: CacheOptions,
202 transferState: TransferState,
203 originMap: Record<string, string> | null,
204 storeKey?: StateKey<TransferHttpResponse>,
205 skipUseCacheChecks = false,
206): HttpResponse<unknown> | null {
207 if (!skipUseCacheChecks && !canUseOrCacheRequest(req, options)) {
208 return null;
209 }
210
211 if (typeof ngServerMode !== 'undefined' && !ngServerMode && originMap) {
212 throw new RuntimeError(
213 RuntimeErrorCode.HTTP_ORIGIN_MAP_USED_IN_CLIENT,
214 ngDevMode &&
215 'Angular detected that the `HTTP_TRANSFER_CACHE_ORIGIN_MAP` token is configured and ' +
216 'present in the client side code. Please ensure that this token is only provided in the ' +
217 'server code of the application.',
218 );
219 }
220
221 if (!storeKey) {
222 const requestUrl =
223 typeof ngServerMode !== 'undefined' && ngServerMode && originMap
224 ? mapRequestOriginUrl(req.url, originMap)
225 : req.url;
226
227 storeKey = makeCacheKey(req, requestUrl);
228 }
229
230 const response = transferState.get(storeKey, null);
231
232 if (!response) {
233 return null;
234 }
235
236 const {
237 [BODY]: undecodedBody,
238 [RESPONSE_TYPE]: responseType,
239 [HEADERS]: httpHeaders,
240 [STATUS]: status,
241 [STATUS_TEXT]: statusText,
242 [REQ_URL]: url,
243 } = response;
244 // Request found in cache. Respond using it.
245 let body: ArrayBuffer | Blob | string | undefined = undecodedBody;
246
247 switch (responseType) {
248 case 'arraybuffer':
249 body = fromBase64(undecodedBody);
250 break;
251 case 'blob':
252 body = new Blob([fromBase64(undecodedBody)]);
253 break;
254 }
255
256 // 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