@mmstack/resource 20.0.3 → 20.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -322,30 +322,25 @@ function parseCacheControlHeader(req) {
322
322
  };
323
323
  return directives;
324
324
  }
325
- function resolveTimings(cacheControl, staleTime, ttl) {
326
- const timings = {
327
- staleTime,
328
- ttl,
329
- };
325
+ function resolveTimings(cacheControl, optStaleTime, optTTL) {
326
+ let staleTime = optStaleTime;
327
+ let ttl = optTTL;
330
328
  if (cacheControl.immutable)
331
329
  return {
332
330
  staleTime: Infinity,
333
331
  ttl: Infinity,
334
332
  };
333
+ if (cacheControl.maxAge !== null)
334
+ ttl = cacheControl.maxAge * 1000;
335
+ if (cacheControl.staleWhileRevalidate !== null)
336
+ staleTime = cacheControl.staleWhileRevalidate * 1000;
335
337
  // if no-cache is set, we must always revalidate
336
338
  if (cacheControl.noCache || cacheControl.mustRevalidate)
337
- timings.staleTime = 0;
338
- if (cacheControl.staleWhileRevalidate !== null)
339
- timings.staleTime = cacheControl.staleWhileRevalidate;
340
- if (cacheControl.maxAge !== null)
341
- timings.ttl = cacheControl.maxAge * 1000;
342
- // if stale-while-revalidate is set, we must revalidate after that time at the latest, but we can still serve the stale data
343
- if (cacheControl.staleWhileRevalidate !== null) {
344
- const ms = cacheControl.staleWhileRevalidate * 1000;
345
- if (timings.staleTime === undefined || timings.staleTime > ms)
346
- timings.staleTime = ms;
339
+ staleTime = 0;
340
+ if (ttl !== undefined && staleTime !== undefined && ttl < staleTime) {
341
+ staleTime = ttl;
347
342
  }
348
- return timings;
343
+ return { staleTime, ttl };
349
344
  }
350
345
  /**
351
346
  * Creates an `HttpInterceptorFn` that implements caching for HTTP requests. This interceptor
@@ -403,6 +398,8 @@ function createCacheInterceptor(allowedMethods = ['GET', 'HEAD', 'OPTIONS']) {
403
398
  if (cacheControl.noStore)
404
399
  return;
405
400
  const { staleTime, ttl } = resolveTimings(cacheControl, opt.staleTime, opt.ttl);
401
+ if (opt.ttl === 0)
402
+ return; // no point
406
403
  cache.store(key, event, staleTime, ttl);
407
404
  }
408
405
  }), map((event) => {