@serwist/strategies 8.0.0

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.
package/dist/index.js ADDED
@@ -0,0 +1,1065 @@
1
+ import { assert, Deferred, logger, getFriendlyURL, SerwistError, timeout, cacheMatchIgnoreParams, executeQuotaErrorCallbacks, privateCacheNames } from '@serwist/core/internal';
2
+
3
+ function toRequest(input) {
4
+ return typeof input === "string" ? new Request(input) : input;
5
+ }
6
+ /**
7
+ * A class created every time a Strategy instance instance calls `Strategy.handle` or
8
+ * `Strategy.handleAll` that wraps all fetch and cache actions around plugin callbacks
9
+ * and keeps track of when the strategy is "done" (i.e. all added `event.waitUntil()` promises
10
+ * have resolved).
11
+ */ class StrategyHandler {
12
+ /**
13
+ * The request the strategy is performing (passed to the strategy's
14
+ * `handle()` or `handleAll()` method).
15
+ */ request;
16
+ /**
17
+ * A `URL` instance of `request.url` (if passed to the strategy's
18
+ * `handle()` or `handleAll()` method).
19
+ * Note: the `url` param will be present if the strategy was invoked
20
+ * from a `@serwist/routing.Route` object.
21
+ */ url;
22
+ /**
23
+ * The event associated with this request.
24
+ */ event;
25
+ /**
26
+ * A `param` value (if passed to the strategy's
27
+ * `handle()` or `handleAll()` method).
28
+ * Note: the `param` param will be present if the strategy was invoked
29
+ * from a `@serwist/routing.Route` object and the `@serwist/strategies.matchCallback`
30
+ * returned a truthy value (it will be that value).
31
+ */ params;
32
+ _cacheKeys = {};
33
+ _strategy;
34
+ _extendLifetimePromises;
35
+ _handlerDeferred;
36
+ _plugins;
37
+ _pluginStateMap;
38
+ /**
39
+ * Creates a new instance associated with the passed strategy and event
40
+ * that's handling the request.
41
+ *
42
+ * The constructor also initializes the state that will be passed to each of
43
+ * the plugins handling this request.
44
+ *
45
+ * @param strategy
46
+ * @param options
47
+ */ constructor(strategy, options){
48
+ if (process.env.NODE_ENV !== "production") {
49
+ assert.isInstance(options.event, ExtendableEvent, {
50
+ moduleName: "@serwist/strategies",
51
+ className: "StrategyHandler",
52
+ funcName: "constructor",
53
+ paramName: "options.event"
54
+ });
55
+ }
56
+ Object.assign(this, options);
57
+ this.event = options.event;
58
+ this._strategy = strategy;
59
+ this._handlerDeferred = new Deferred();
60
+ this._extendLifetimePromises = [];
61
+ // Copy the plugins list (since it's mutable on the strategy),
62
+ // so any mutations don't affect this handler instance.
63
+ this._plugins = [
64
+ ...strategy.plugins
65
+ ];
66
+ this._pluginStateMap = new Map();
67
+ for (const plugin of this._plugins){
68
+ this._pluginStateMap.set(plugin, {});
69
+ }
70
+ this.event.waitUntil(this._handlerDeferred.promise);
71
+ }
72
+ /**
73
+ * Fetches a given request (and invokes any applicable plugin callback
74
+ * methods) using the `fetchOptions` (for non-navigation requests) and
75
+ * `plugins` defined on the `Strategy` object.
76
+ *
77
+ * The following plugin lifecycle methods are invoked when using this method:
78
+ * - `requestWillFetch()`
79
+ * - `fetchDidSucceed()`
80
+ * - `fetchDidFail()`
81
+ *
82
+ * @param input The URL or request to fetch.
83
+ * @returns
84
+ */ async fetch(input) {
85
+ const { event } = this;
86
+ let request = toRequest(input);
87
+ if (request.mode === "navigate" && event instanceof FetchEvent && event.preloadResponse) {
88
+ const possiblePreloadResponse = await event.preloadResponse;
89
+ if (possiblePreloadResponse) {
90
+ if (process.env.NODE_ENV !== "production") {
91
+ logger.log(`Using a preloaded navigation response for ` + `'${getFriendlyURL(request.url)}'`);
92
+ }
93
+ return possiblePreloadResponse;
94
+ }
95
+ }
96
+ // If there is a fetchDidFail plugin, we need to save a clone of the
97
+ // original request before it's either modified by a requestWillFetch
98
+ // plugin or before the original request's body is consumed via fetch().
99
+ const originalRequest = this.hasCallback("fetchDidFail") ? request.clone() : null;
100
+ try {
101
+ for (const cb of this.iterateCallbacks("requestWillFetch")){
102
+ request = await cb({
103
+ request: request.clone(),
104
+ event
105
+ });
106
+ }
107
+ } catch (err) {
108
+ if (err instanceof Error) {
109
+ throw new SerwistError("plugin-error-request-will-fetch", {
110
+ thrownErrorMessage: err.message
111
+ });
112
+ }
113
+ }
114
+ // The request can be altered by plugins with `requestWillFetch` making
115
+ // the original request (most likely from a `fetch` event) different
116
+ // from the Request we make. Pass both to `fetchDidFail` to aid debugging.
117
+ const pluginFilteredRequest = request.clone();
118
+ try {
119
+ let fetchResponse;
120
+ // See https://github.com/GoogleChrome/workbox/issues/1796
121
+ fetchResponse = await fetch(request, request.mode === "navigate" ? undefined : this._strategy.fetchOptions);
122
+ if (process.env.NODE_ENV !== "production") {
123
+ logger.debug(`Network request for ` + `'${getFriendlyURL(request.url)}' returned a response with ` + `status '${fetchResponse.status}'.`);
124
+ }
125
+ for (const callback of this.iterateCallbacks("fetchDidSucceed")){
126
+ fetchResponse = await callback({
127
+ event,
128
+ request: pluginFilteredRequest,
129
+ response: fetchResponse
130
+ });
131
+ }
132
+ return fetchResponse;
133
+ } catch (error) {
134
+ if (process.env.NODE_ENV !== "production") {
135
+ logger.log(`Network request for ` + `'${getFriendlyURL(request.url)}' threw an error.`, error);
136
+ }
137
+ // `originalRequest` will only exist if a `fetchDidFail` callback
138
+ // is being used (see above).
139
+ if (originalRequest) {
140
+ await this.runCallbacks("fetchDidFail", {
141
+ error: error,
142
+ event,
143
+ originalRequest: originalRequest.clone(),
144
+ request: pluginFilteredRequest.clone()
145
+ });
146
+ }
147
+ throw error;
148
+ }
149
+ }
150
+ /**
151
+ * Calls `this.fetch()` and (in the background) runs `this.cachePut()` on
152
+ * the response generated by `this.fetch()`.
153
+ *
154
+ * The call to `this.cachePut()` automatically invokes `this.waitUntil()`,
155
+ * so you do not have to manually call `waitUntil()` on the event.
156
+ *
157
+ * @param input The request or URL to fetch and cache.
158
+ * @returns
159
+ */ async fetchAndCachePut(input) {
160
+ const response = await this.fetch(input);
161
+ const responseClone = response.clone();
162
+ void this.waitUntil(this.cachePut(input, responseClone));
163
+ return response;
164
+ }
165
+ /**
166
+ * Matches a request from the cache (and invokes any applicable plugin
167
+ * callback methods) using the `cacheName`, `matchOptions`, and `plugins`
168
+ * defined on the strategy object.
169
+ *
170
+ * The following plugin lifecycle methods are invoked when using this method:
171
+ * - cacheKeyWillByUsed()
172
+ * - cachedResponseWillByUsed()
173
+ *
174
+ * @param key The Request or URL to use as the cache key.
175
+ * @returns A matching response, if found.
176
+ */ async cacheMatch(key) {
177
+ const request = toRequest(key);
178
+ let cachedResponse;
179
+ const { cacheName, matchOptions } = this._strategy;
180
+ const effectiveRequest = await this.getCacheKey(request, "read");
181
+ const multiMatchOptions = {
182
+ ...matchOptions,
183
+ ...{
184
+ cacheName
185
+ }
186
+ };
187
+ cachedResponse = await caches.match(effectiveRequest, multiMatchOptions);
188
+ if (process.env.NODE_ENV !== "production") {
189
+ if (cachedResponse) {
190
+ logger.debug(`Found a cached response in '${cacheName}'.`);
191
+ } else {
192
+ logger.debug(`No cached response found in '${cacheName}'.`);
193
+ }
194
+ }
195
+ for (const callback of this.iterateCallbacks("cachedResponseWillBeUsed")){
196
+ cachedResponse = await callback({
197
+ cacheName,
198
+ matchOptions,
199
+ cachedResponse,
200
+ request: effectiveRequest,
201
+ event: this.event
202
+ }) || undefined;
203
+ }
204
+ return cachedResponse;
205
+ }
206
+ /**
207
+ * Puts a request/response pair in the cache (and invokes any applicable
208
+ * plugin callback methods) using the `cacheName` and `plugins` defined on
209
+ * the strategy object.
210
+ *
211
+ * The following plugin lifecycle methods are invoked when using this method:
212
+ * - cacheKeyWillByUsed()
213
+ * - cacheWillUpdate()
214
+ * - cacheDidUpdate()
215
+ *
216
+ * @param key The request or URL to use as the cache key.
217
+ * @param response The response to cache.
218
+ * @returns `false` if a cacheWillUpdate caused the response
219
+ * not be cached, and `true` otherwise.
220
+ */ async cachePut(key, response) {
221
+ const request = toRequest(key);
222
+ // Run in the next task to avoid blocking other cache reads.
223
+ // https://github.com/w3c/ServiceWorker/issues/1397
224
+ await timeout(0);
225
+ const effectiveRequest = await this.getCacheKey(request, "write");
226
+ if (process.env.NODE_ENV !== "production") {
227
+ if (effectiveRequest.method && effectiveRequest.method !== "GET") {
228
+ throw new SerwistError("attempt-to-cache-non-get-request", {
229
+ url: getFriendlyURL(effectiveRequest.url),
230
+ method: effectiveRequest.method
231
+ });
232
+ }
233
+ // See https://github.com/GoogleChrome/workbox/issues/2818
234
+ const vary = response.headers.get("Vary");
235
+ if (vary) {
236
+ logger.debug(`The response for ${getFriendlyURL(effectiveRequest.url)} ` + `has a 'Vary: ${vary}' header. ` + `Consider setting the {ignoreVary: true} option on your strategy ` + `to ensure cache matching and deletion works as expected.`);
237
+ }
238
+ }
239
+ if (!response) {
240
+ if (process.env.NODE_ENV !== "production") {
241
+ logger.error(`Cannot cache non-existent response for ` + `'${getFriendlyURL(effectiveRequest.url)}'.`);
242
+ }
243
+ throw new SerwistError("cache-put-with-no-response", {
244
+ url: getFriendlyURL(effectiveRequest.url)
245
+ });
246
+ }
247
+ const responseToCache = await this._ensureResponseSafeToCache(response);
248
+ if (!responseToCache) {
249
+ if (process.env.NODE_ENV !== "production") {
250
+ logger.debug(`Response '${getFriendlyURL(effectiveRequest.url)}' ` + `will not be cached.`, responseToCache);
251
+ }
252
+ return false;
253
+ }
254
+ const { cacheName, matchOptions } = this._strategy;
255
+ const cache = await self.caches.open(cacheName);
256
+ const hasCacheUpdateCallback = this.hasCallback("cacheDidUpdate");
257
+ const oldResponse = hasCacheUpdateCallback ? await cacheMatchIgnoreParams(// TODO(philipwalton): the `__WB_REVISION__` param is a precaching
258
+ // feature. Consider into ways to only add this behavior if using
259
+ // precaching.
260
+ cache, effectiveRequest.clone(), [
261
+ "__WB_REVISION__"
262
+ ], matchOptions) : null;
263
+ if (process.env.NODE_ENV !== "production") {
264
+ logger.debug(`Updating the '${cacheName}' cache with a new Response ` + `for ${getFriendlyURL(effectiveRequest.url)}.`);
265
+ }
266
+ try {
267
+ await cache.put(effectiveRequest, hasCacheUpdateCallback ? responseToCache.clone() : responseToCache);
268
+ } catch (error) {
269
+ if (error instanceof Error) {
270
+ // See https://developer.mozilla.org/en-US/docs/Web/API/DOMException#exception-QuotaExceededError
271
+ if (error.name === "QuotaExceededError") {
272
+ await executeQuotaErrorCallbacks();
273
+ }
274
+ throw error;
275
+ }
276
+ }
277
+ for (const callback of this.iterateCallbacks("cacheDidUpdate")){
278
+ await callback({
279
+ cacheName,
280
+ oldResponse,
281
+ newResponse: responseToCache.clone(),
282
+ request: effectiveRequest,
283
+ event: this.event
284
+ });
285
+ }
286
+ return true;
287
+ }
288
+ /**
289
+ * Checks the list of plugins for the `cacheKeyWillBeUsed` callback, and
290
+ * executes any of those callbacks found in sequence. The final `Request`
291
+ * object returned by the last plugin is treated as the cache key for cache
292
+ * reads and/or writes. If no `cacheKeyWillBeUsed` plugin callbacks have
293
+ * been registered, the passed request is returned unmodified
294
+ *
295
+ * @param request
296
+ * @param mode
297
+ * @returns
298
+ */ async getCacheKey(request, mode) {
299
+ const key = `${request.url} | ${mode}`;
300
+ if (!this._cacheKeys[key]) {
301
+ let effectiveRequest = request;
302
+ for (const callback of this.iterateCallbacks("cacheKeyWillBeUsed")){
303
+ effectiveRequest = toRequest(await callback({
304
+ mode,
305
+ request: effectiveRequest,
306
+ event: this.event,
307
+ // params has a type any can't change right now.
308
+ params: this.params
309
+ }));
310
+ }
311
+ this._cacheKeys[key] = effectiveRequest;
312
+ }
313
+ return this._cacheKeys[key];
314
+ }
315
+ /**
316
+ * Returns true if the strategy has at least one plugin with the given
317
+ * callback.
318
+ *
319
+ * @param name The name of the callback to check for.
320
+ * @returns
321
+ */ hasCallback(name) {
322
+ for (const plugin of this._strategy.plugins){
323
+ if (name in plugin) {
324
+ return true;
325
+ }
326
+ }
327
+ return false;
328
+ }
329
+ /**
330
+ * Runs all plugin callbacks matching the given name, in order, passing the
331
+ * given param object (merged ith the current plugin state) as the only
332
+ * argument.
333
+ *
334
+ * Note: since this method runs all plugins, it's not suitable for cases
335
+ * where the return value of a callback needs to be applied prior to calling
336
+ * the next callback. See `@serwist/strategies.iterateCallbacks` for how to handle that case.
337
+ *
338
+ * @param name The name of the callback to run within each plugin.
339
+ * @param param The object to pass as the first (and only) param when executing each callback. This object will be merged with the
340
+ * current plugin state prior to callback execution.
341
+ */ async runCallbacks(name, param) {
342
+ for (const callback of this.iterateCallbacks(name)){
343
+ // TODO(philipwalton): not sure why `any` is needed. It seems like
344
+ // this should work with `as SerwistPluginCallbackParam[C]`.
345
+ await callback(param);
346
+ }
347
+ }
348
+ /**
349
+ * Accepts a callback and returns an iterable of matching plugin callbacks,
350
+ * where each callback is wrapped with the current handler state (i.e. when
351
+ * you call each callback, whatever object parameter you pass it will
352
+ * be merged with the plugin's current state).
353
+ *
354
+ * @param name The name fo the callback to run
355
+ * @returns
356
+ */ *iterateCallbacks(name) {
357
+ for (const plugin of this._strategy.plugins){
358
+ if (typeof plugin[name] === "function") {
359
+ const state = this._pluginStateMap.get(plugin);
360
+ const statefulCallback = (param)=>{
361
+ const statefulParam = {
362
+ ...param,
363
+ state
364
+ };
365
+ // TODO(philipwalton): not sure why `any` is needed. It seems like
366
+ // this should work with `as WorkboxPluginCallbackParam[C]`.
367
+ return plugin[name](statefulParam);
368
+ };
369
+ yield statefulCallback;
370
+ }
371
+ }
372
+ }
373
+ /**
374
+ * Adds a promise to the
375
+ * [extend lifetime promises](https://w3c.github.io/ServiceWorker/#extendableevent-extend-lifetime-promises)
376
+ * of the event event associated with the request being handled (usually a `FetchEvent`).
377
+ *
378
+ * Note: you can await
379
+ * `@serwist/strategies.StrategyHandler.doneWaiting`
380
+ * to know when all added promises have settled.
381
+ *
382
+ * @param promise A promise to add to the extend lifetime promises of
383
+ * the event that triggered the request.
384
+ */ waitUntil(promise) {
385
+ this._extendLifetimePromises.push(promise);
386
+ return promise;
387
+ }
388
+ /**
389
+ * Returns a promise that resolves once all promises passed to
390
+ * `@serwist/strategies.StrategyHandler.waitUntil` have settled.
391
+ *
392
+ * Note: any work done after `doneWaiting()` settles should be manually
393
+ * passed to an event's `waitUntil()` method (not this handler's
394
+ * `waitUntil()` method), otherwise the service worker thread my be killed
395
+ * prior to your work completing.
396
+ */ async doneWaiting() {
397
+ let promise;
398
+ while(promise = this._extendLifetimePromises.shift()){
399
+ await promise;
400
+ }
401
+ }
402
+ /**
403
+ * Stops running the strategy and immediately resolves any pending
404
+ * `waitUntil()` promises.
405
+ */ destroy() {
406
+ this._handlerDeferred.resolve(null);
407
+ }
408
+ /**
409
+ * This method will call `cacheWillUpdate` on the available plugins (or use
410
+ * status === 200) to determine if the response is safe and valid to cache.
411
+ *
412
+ * @param response
413
+ * @returns
414
+ * @private
415
+ */ async _ensureResponseSafeToCache(response) {
416
+ let responseToCache = response;
417
+ let pluginsUsed = false;
418
+ for (const callback of this.iterateCallbacks("cacheWillUpdate")){
419
+ responseToCache = await callback({
420
+ request: this.request,
421
+ response: responseToCache,
422
+ event: this.event
423
+ }) || undefined;
424
+ pluginsUsed = true;
425
+ if (!responseToCache) {
426
+ break;
427
+ }
428
+ }
429
+ if (!pluginsUsed) {
430
+ if (responseToCache && responseToCache.status !== 200) {
431
+ responseToCache = undefined;
432
+ }
433
+ if (process.env.NODE_ENV !== "production") {
434
+ if (responseToCache) {
435
+ if (responseToCache.status !== 200) {
436
+ if (responseToCache.status === 0) {
437
+ logger.warn(`The response for '${this.request.url}' ` + `is an opaque response. The caching strategy that you're ` + `using will not cache opaque responses by default.`);
438
+ } else {
439
+ logger.debug(`The response for '${this.request.url}' ` + `returned a status code of '${response.status}' and won't ` + `be cached as a result.`);
440
+ }
441
+ }
442
+ }
443
+ }
444
+ }
445
+ return responseToCache;
446
+ }
447
+ }
448
+
449
+ /**
450
+ * Classes extending the `Strategy` based class should implement this method,
451
+ * and leverage `@serwist/strategies`'s `StrategyHandler` arg to perform all
452
+ * fetching and cache logic, which will ensure all relevant cache, cache options,
453
+ * fetch options and plugins are used (per the current strategy instance).
454
+ */ class Strategy {
455
+ cacheName;
456
+ plugins;
457
+ fetchOptions;
458
+ matchOptions;
459
+ /**
460
+ * Creates a new instance of the strategy and sets all documented option
461
+ * properties as public instance properties.
462
+ *
463
+ * Note: if a custom strategy class extends the base Strategy class and does
464
+ * not need more than these properties, it does not need to define its own
465
+ * constructor.
466
+ *
467
+ * @param options
468
+ */ constructor(options = {}){
469
+ this.cacheName = privateCacheNames.getRuntimeName(options.cacheName);
470
+ this.plugins = options.plugins || [];
471
+ this.fetchOptions = options.fetchOptions;
472
+ this.matchOptions = options.matchOptions;
473
+ }
474
+ /**
475
+ * Perform a request strategy and returns a `Promise` that will resolve with
476
+ * a `Response`, invoking all relevant plugin callbacks.
477
+ *
478
+ * When a strategy instance is registered with a `@serwist/routing` Route, this method is automatically
479
+ * called when the route matches.
480
+ *
481
+ * Alternatively, this method can be used in a standalone `FetchEvent`
482
+ * listener by passing it to `event.respondWith()`.
483
+ *
484
+ * @param options A `FetchEvent` or an object with the properties listed below.
485
+ * @param options.request A request to run this strategy for.
486
+ * @param options.event The event associated with the request.
487
+ * @param options.url
488
+ * @param options.params
489
+ */ handle(options) {
490
+ const [responseDone] = this.handleAll(options);
491
+ return responseDone;
492
+ }
493
+ /**
494
+ * Similar to `@serwist/strategies`'s `Strategy.handle`, but
495
+ * instead of just returning a `Promise` that resolves to a `Response` it
496
+ * it will return an tuple of `[response, done]` promises, where the former
497
+ * (`response`) is equivalent to what `handle()` returns, and the latter is a
498
+ * Promise that will resolve once any promises that were added to
499
+ * `event.waitUntil()` as part of performing the strategy have completed.
500
+ *
501
+ * You can await the `done` promise to ensure any extra work performed by
502
+ * the strategy (usually caching responses) completes successfully.
503
+ *
504
+ * @param options A `FetchEvent` or `HandlerCallbackOptions` object.
505
+ * @returns A tuple of [response, done] promises that can be used to determine when the response resolves as
506
+ * well as when the handler has completed all its work.
507
+ */ handleAll(options) {
508
+ // Allow for flexible options to be passed.
509
+ if (options instanceof FetchEvent) {
510
+ options = {
511
+ event: options,
512
+ request: options.request
513
+ };
514
+ }
515
+ const event = options.event;
516
+ const request = typeof options.request === "string" ? new Request(options.request) : options.request;
517
+ const params = "params" in options ? options.params : undefined;
518
+ const handler = new StrategyHandler(this, {
519
+ event,
520
+ request,
521
+ params
522
+ });
523
+ const responseDone = this._getResponse(handler, request, event);
524
+ const handlerDone = this._awaitComplete(responseDone, handler, request, event);
525
+ // Return an array of promises, suitable for use with Promise.all().
526
+ return [
527
+ responseDone,
528
+ handlerDone
529
+ ];
530
+ }
531
+ async _getResponse(handler, request, event) {
532
+ await handler.runCallbacks("handlerWillStart", {
533
+ event,
534
+ request
535
+ });
536
+ let response = undefined;
537
+ try {
538
+ response = await this._handle(request, handler);
539
+ // The "official" Strategy subclasses all throw this error automatically,
540
+ // but in case a third-party Strategy doesn't, ensure that we have a
541
+ // consistent failure when there's no response or an error response.
542
+ if (response === undefined || response.type === "error") {
543
+ throw new SerwistError("no-response", {
544
+ url: request.url
545
+ });
546
+ }
547
+ } catch (error) {
548
+ if (error instanceof Error) {
549
+ for (const callback of handler.iterateCallbacks("handlerDidError")){
550
+ response = await callback({
551
+ error,
552
+ event,
553
+ request
554
+ });
555
+ if (response !== undefined) {
556
+ break;
557
+ }
558
+ }
559
+ }
560
+ if (!response) {
561
+ throw error;
562
+ } else if (process.env.NODE_ENV !== "production") {
563
+ throw logger.log(`While responding to '${getFriendlyURL(request.url)}', ` + `an ${error instanceof Error ? error.toString() : ""} error occurred. Using a fallback response provided by ` + `a handlerDidError plugin.`);
564
+ }
565
+ }
566
+ for (const callback of handler.iterateCallbacks("handlerWillRespond")){
567
+ response = await callback({
568
+ event,
569
+ request,
570
+ response
571
+ });
572
+ }
573
+ return response;
574
+ }
575
+ async _awaitComplete(responseDone, handler, request, event) {
576
+ let response;
577
+ let error;
578
+ try {
579
+ response = await responseDone;
580
+ } catch (error) {
581
+ // Ignore errors, as response errors should be caught via the `response`
582
+ // promise above. The `done` promise will only throw for errors in
583
+ // promises passed to `handler.waitUntil()`.
584
+ }
585
+ try {
586
+ await handler.runCallbacks("handlerDidRespond", {
587
+ event,
588
+ request,
589
+ response
590
+ });
591
+ await handler.doneWaiting();
592
+ } catch (waitUntilError) {
593
+ if (waitUntilError instanceof Error) {
594
+ error = waitUntilError;
595
+ }
596
+ }
597
+ await handler.runCallbacks("handlerDidComplete", {
598
+ event,
599
+ request,
600
+ response,
601
+ error: error
602
+ });
603
+ handler.destroy();
604
+ if (error) {
605
+ throw error;
606
+ }
607
+ }
608
+ }
609
+
610
+ const messages = {
611
+ strategyStart: (strategyName, request)=>`Using ${strategyName} to respond to '${getFriendlyURL(request.url)}'`,
612
+ printFinalResponse: (response)=>{
613
+ if (response) {
614
+ logger.groupCollapsed(`View the final response here.`);
615
+ logger.log(response || "[No response returned]");
616
+ logger.groupEnd();
617
+ }
618
+ }
619
+ };
620
+
621
+ /**
622
+ * An implementation of a [cache first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache-first-falling-back-to-network)
623
+ * request strategy.
624
+ *
625
+ * A cache first strategy is useful for assets that have been revisioned,
626
+ * such as URLs like `/styles/example.a8f5f1.css`, since they
627
+ * can be cached for long periods of time.
628
+ *
629
+ * If the network request fails, and there is no cache match, this will throw
630
+ * a `SerwistError` exception.
631
+ */ class CacheFirst extends Strategy {
632
+ /**
633
+ * @private
634
+ * @param request A request to run this strategy for.
635
+ * @param handler The event that triggered the request.
636
+ * @returns
637
+ */ async _handle(request, handler) {
638
+ const logs = [];
639
+ if (process.env.NODE_ENV !== "production") {
640
+ assert.isInstance(request, Request, {
641
+ moduleName: "@serwist/strategies",
642
+ className: this.constructor.name,
643
+ funcName: "makeRequest",
644
+ paramName: "request"
645
+ });
646
+ }
647
+ let response = await handler.cacheMatch(request);
648
+ let error = undefined;
649
+ if (!response) {
650
+ if (process.env.NODE_ENV !== "production") {
651
+ logs.push(`No response found in the '${this.cacheName}' cache. ` + `Will respond with a network request.`);
652
+ }
653
+ try {
654
+ response = await handler.fetchAndCachePut(request);
655
+ } catch (err) {
656
+ if (err instanceof Error) {
657
+ error = err;
658
+ }
659
+ }
660
+ if (process.env.NODE_ENV !== "production") {
661
+ if (response) {
662
+ logs.push(`Got response from network.`);
663
+ } else {
664
+ logs.push(`Unable to get a response from the network.`);
665
+ }
666
+ }
667
+ } else {
668
+ if (process.env.NODE_ENV !== "production") {
669
+ logs.push(`Found a cached response in the '${this.cacheName}' cache.`);
670
+ }
671
+ }
672
+ if (process.env.NODE_ENV !== "production") {
673
+ logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
674
+ for (const log of logs){
675
+ logger.log(log);
676
+ }
677
+ messages.printFinalResponse(response);
678
+ logger.groupEnd();
679
+ }
680
+ if (!response) {
681
+ throw new SerwistError("no-response", {
682
+ url: request.url,
683
+ error
684
+ });
685
+ }
686
+ return response;
687
+ }
688
+ }
689
+
690
+ /**
691
+ * An implementation of a [cache only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache-only)
692
+ * request strategy.
693
+ *
694
+ * This class is useful if you want to take advantage of any Serwist plugin.
695
+ *
696
+ * If there is no cache match, this will throw a `SerwistError` exception.
697
+ */ class CacheOnly extends Strategy {
698
+ /**
699
+ * @private
700
+ * @param request A request to run this strategy for.
701
+ * @param handler The event that triggered the request.
702
+ * @returns
703
+ */ async _handle(request, handler) {
704
+ if (process.env.NODE_ENV !== "production") {
705
+ assert.isInstance(request, Request, {
706
+ moduleName: "@serwist/strategies",
707
+ className: this.constructor.name,
708
+ funcName: "makeRequest",
709
+ paramName: "request"
710
+ });
711
+ }
712
+ const response = await handler.cacheMatch(request);
713
+ if (process.env.NODE_ENV !== "production") {
714
+ logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
715
+ if (response) {
716
+ logger.log(`Found a cached response in the '${this.cacheName}' ` + `cache.`);
717
+ messages.printFinalResponse(response);
718
+ } else {
719
+ logger.log(`No response found in the '${this.cacheName}' cache.`);
720
+ }
721
+ logger.groupEnd();
722
+ }
723
+ if (!response) {
724
+ throw new SerwistError("no-response", {
725
+ url: request.url
726
+ });
727
+ }
728
+ return response;
729
+ }
730
+ }
731
+
732
+ /*
733
+ Copyright 2018 Google LLC
734
+
735
+ Use of this source code is governed by an MIT-style
736
+ license that can be found in the LICENSE file or at
737
+ https://opensource.org/licenses/MIT.
738
+ */ const cacheOkAndOpaquePlugin = {
739
+ /**
740
+ * Returns a valid response (to allow caching) if the status is 200 (OK) or
741
+ * 0 (opaque).
742
+ *
743
+ * @param options
744
+ * @returns
745
+ * @private
746
+ */ cacheWillUpdate: async ({ response })=>{
747
+ if (response.status === 200 || response.status === 0) {
748
+ return response;
749
+ }
750
+ return null;
751
+ }
752
+ };
753
+
754
+ /**
755
+ * An implementation of a [network first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-first-falling-back-to-cache)
756
+ * request strategy.
757
+ *
758
+ * By default, this strategy will cache responses with a 200 status code as
759
+ * well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
760
+ * Opaque responses are are cross-origin requests where the response doesn't
761
+ * support [CORS](https://enable-cors.org/).
762
+ *
763
+ * If the network request fails, and there is no cache match, this will throw
764
+ * a `SerwistError` exception.
765
+ */ class NetworkFirst extends Strategy {
766
+ _networkTimeoutSeconds;
767
+ /**
768
+ * @param options
769
+ * This option can be used to combat
770
+ * "[lie-fi](https://developers.google.com/web/fundamentals/performance/poor-connectivity/#lie-fi)"
771
+ * scenarios.
772
+ */ constructor(options = {}){
773
+ super(options);
774
+ // If this instance contains no plugins with a 'cacheWillUpdate' callback,
775
+ // prepend the `cacheOkAndOpaquePlugin` plugin to the plugins list.
776
+ if (!this.plugins.some((p)=>"cacheWillUpdate" in p)) {
777
+ this.plugins.unshift(cacheOkAndOpaquePlugin);
778
+ }
779
+ this._networkTimeoutSeconds = options.networkTimeoutSeconds || 0;
780
+ if (process.env.NODE_ENV !== "production") {
781
+ if (this._networkTimeoutSeconds) {
782
+ assert.isType(this._networkTimeoutSeconds, "number", {
783
+ moduleName: "@serwist/strategies",
784
+ className: this.constructor.name,
785
+ funcName: "constructor",
786
+ paramName: "networkTimeoutSeconds"
787
+ });
788
+ }
789
+ }
790
+ }
791
+ /**
792
+ * @private
793
+ * @param request A request to run this strategy for.
794
+ * @param handler The event that triggered the request.
795
+ * @returns
796
+ */ async _handle(request, handler) {
797
+ const logs = [];
798
+ if (process.env.NODE_ENV !== "production") {
799
+ assert.isInstance(request, Request, {
800
+ moduleName: "@serwist/strategies",
801
+ className: this.constructor.name,
802
+ funcName: "handle",
803
+ paramName: "makeRequest"
804
+ });
805
+ }
806
+ const promises = [];
807
+ let timeoutId;
808
+ if (this._networkTimeoutSeconds) {
809
+ const { id, promise } = this._getTimeoutPromise({
810
+ request,
811
+ logs,
812
+ handler
813
+ });
814
+ timeoutId = id;
815
+ promises.push(promise);
816
+ }
817
+ const networkPromise = this._getNetworkPromise({
818
+ timeoutId,
819
+ request,
820
+ logs,
821
+ handler
822
+ });
823
+ promises.push(networkPromise);
824
+ const response = await handler.waitUntil((async ()=>{
825
+ // Promise.race() will resolve as soon as the first promise resolves.
826
+ return await handler.waitUntil(Promise.race(promises)) || // If Promise.race() resolved with null, it might be due to a network
827
+ // timeout + a cache miss. If that were to happen, we'd rather wait until
828
+ // the networkPromise resolves instead of returning null.
829
+ // Note that it's fine to await an already-resolved promise, so we don't
830
+ // have to check to see if it's still "in flight".
831
+ await networkPromise;
832
+ })());
833
+ if (process.env.NODE_ENV !== "production") {
834
+ logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
835
+ for (const log of logs){
836
+ logger.log(log);
837
+ }
838
+ messages.printFinalResponse(response);
839
+ logger.groupEnd();
840
+ }
841
+ if (!response) {
842
+ throw new SerwistError("no-response", {
843
+ url: request.url
844
+ });
845
+ }
846
+ return response;
847
+ }
848
+ /**
849
+ * @param options
850
+ * @returns
851
+ * @private
852
+ */ _getTimeoutPromise({ request, logs, handler }) {
853
+ let timeoutId;
854
+ const timeoutPromise = new Promise((resolve)=>{
855
+ const onNetworkTimeout = async ()=>{
856
+ if (process.env.NODE_ENV !== "production") {
857
+ logs.push(`Timing out the network response at ` + `${this._networkTimeoutSeconds} seconds.`);
858
+ }
859
+ resolve(await handler.cacheMatch(request));
860
+ };
861
+ timeoutId = setTimeout(onNetworkTimeout, this._networkTimeoutSeconds * 1000);
862
+ });
863
+ return {
864
+ promise: timeoutPromise,
865
+ id: timeoutId
866
+ };
867
+ }
868
+ /**
869
+ * @param options
870
+ * @param options.timeoutId
871
+ * @param options.request
872
+ * @param options.logs A reference to the logs Array.
873
+ * @param options.event
874
+ * @returns
875
+ *
876
+ * @private
877
+ */ async _getNetworkPromise({ timeoutId, request, logs, handler }) {
878
+ let error;
879
+ let response;
880
+ try {
881
+ response = await handler.fetchAndCachePut(request);
882
+ } catch (fetchError) {
883
+ if (fetchError instanceof Error) {
884
+ error = fetchError;
885
+ }
886
+ }
887
+ if (timeoutId) {
888
+ clearTimeout(timeoutId);
889
+ }
890
+ if (process.env.NODE_ENV !== "production") {
891
+ if (response) {
892
+ logs.push(`Got response from network.`);
893
+ } else {
894
+ logs.push(`Unable to get a response from the network. Will respond ` + `with a cached response.`);
895
+ }
896
+ }
897
+ if (error || !response) {
898
+ response = await handler.cacheMatch(request);
899
+ if (process.env.NODE_ENV !== "production") {
900
+ if (response) {
901
+ logs.push(`Found a cached response in the '${this.cacheName}'` + ` cache.`);
902
+ } else {
903
+ logs.push(`No response found in the '${this.cacheName}' cache.`);
904
+ }
905
+ }
906
+ }
907
+ return response;
908
+ }
909
+ }
910
+
911
+ /**
912
+ * An implementation of a [network only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-only)
913
+ * request strategy.
914
+ *
915
+ * This class is useful if you want to take advantage of any Serwist plugin.
916
+ *
917
+ * If the network request fails, this will throw a `SerwistError` exception.
918
+ */ class NetworkOnly extends Strategy {
919
+ _networkTimeoutSeconds;
920
+ /**
921
+ * @param options
922
+ */ constructor(options = {}){
923
+ super(options);
924
+ this._networkTimeoutSeconds = options.networkTimeoutSeconds || 0;
925
+ }
926
+ /**
927
+ * @private
928
+ * @param request A request to run this strategy for.
929
+ * @param handler The event that triggered the request.
930
+ * @returns
931
+ */ async _handle(request, handler) {
932
+ if (process.env.NODE_ENV !== "production") {
933
+ assert.isInstance(request, Request, {
934
+ moduleName: "@serwist/strategies",
935
+ className: this.constructor.name,
936
+ funcName: "_handle",
937
+ paramName: "request"
938
+ });
939
+ }
940
+ let error = undefined;
941
+ let response;
942
+ try {
943
+ const promises = [
944
+ handler.fetch(request)
945
+ ];
946
+ if (this._networkTimeoutSeconds) {
947
+ const timeoutPromise = timeout(this._networkTimeoutSeconds * 1000);
948
+ promises.push(timeoutPromise);
949
+ }
950
+ response = await Promise.race(promises);
951
+ if (!response) {
952
+ throw new Error(`Timed out the network response after ` + `${this._networkTimeoutSeconds} seconds.`);
953
+ }
954
+ } catch (err) {
955
+ if (err instanceof Error) {
956
+ error = err;
957
+ }
958
+ }
959
+ if (process.env.NODE_ENV !== "production") {
960
+ logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
961
+ if (response) {
962
+ logger.log(`Got response from network.`);
963
+ } else {
964
+ logger.log(`Unable to get a response from the network.`);
965
+ }
966
+ messages.printFinalResponse(response);
967
+ logger.groupEnd();
968
+ }
969
+ if (!response) {
970
+ throw new SerwistError("no-response", {
971
+ url: request.url,
972
+ error
973
+ });
974
+ }
975
+ return response;
976
+ }
977
+ }
978
+
979
+ /**
980
+ * An implementation of a
981
+ * [stale-while-revalidate](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#stale-while-revalidate)
982
+ * request strategy.
983
+ *
984
+ * Resources are requested from both the cache and the network in parallel.
985
+ * The strategy will respond with the cached version if available, otherwise
986
+ * wait for the network response. The cache is updated with the network response
987
+ * with each successful request.
988
+ *
989
+ * By default, this strategy will cache responses with a 200 status code as
990
+ * well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
991
+ * Opaque responses are cross-origin requests where the response doesn't
992
+ * support [CORS](https://enable-cors.org/).
993
+ *
994
+ * If the network request fails, and there is no cache match, this will throw
995
+ * a `SerwistError` exception.
996
+ */ class StaleWhileRevalidate extends Strategy {
997
+ /**
998
+ * @param options
999
+ */ constructor(options = {}){
1000
+ super(options);
1001
+ // If this instance contains no plugins with a 'cacheWillUpdate' callback,
1002
+ // prepend the `cacheOkAndOpaquePlugin` plugin to the plugins list.
1003
+ if (!this.plugins.some((p)=>"cacheWillUpdate" in p)) {
1004
+ this.plugins.unshift(cacheOkAndOpaquePlugin);
1005
+ }
1006
+ }
1007
+ /**
1008
+ * @private
1009
+ * @param request A request to run this strategy for.
1010
+ * @param handler The event that triggered the request.
1011
+ * @returns
1012
+ */ async _handle(request, handler) {
1013
+ const logs = [];
1014
+ if (process.env.NODE_ENV !== "production") {
1015
+ assert.isInstance(request, Request, {
1016
+ moduleName: "@serwist/strategies",
1017
+ className: this.constructor.name,
1018
+ funcName: "handle",
1019
+ paramName: "request"
1020
+ });
1021
+ }
1022
+ const fetchAndCachePromise = handler.fetchAndCachePut(request).catch(()=>{
1023
+ // Swallow this error because a 'no-response' error will be thrown in
1024
+ // main handler return flow. This will be in the `waitUntil()` flow.
1025
+ });
1026
+ void handler.waitUntil(fetchAndCachePromise);
1027
+ let response = await handler.cacheMatch(request);
1028
+ let error;
1029
+ if (response) {
1030
+ if (process.env.NODE_ENV !== "production") {
1031
+ logs.push(`Found a cached response in the '${this.cacheName}'` + ` cache. Will update with the network response in the background.`);
1032
+ }
1033
+ } else {
1034
+ if (process.env.NODE_ENV !== "production") {
1035
+ logs.push(`No response found in the '${this.cacheName}' cache. ` + `Will wait for the network response.`);
1036
+ }
1037
+ try {
1038
+ // NOTE(philipwalton): Really annoying that we have to type cast here.
1039
+ // https://github.com/microsoft/TypeScript/issues/20006
1040
+ response = await fetchAndCachePromise;
1041
+ } catch (err) {
1042
+ if (err instanceof Error) {
1043
+ error = err;
1044
+ }
1045
+ }
1046
+ }
1047
+ if (process.env.NODE_ENV !== "production") {
1048
+ logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
1049
+ for (const log of logs){
1050
+ logger.log(log);
1051
+ }
1052
+ messages.printFinalResponse(response);
1053
+ logger.groupEnd();
1054
+ }
1055
+ if (!response) {
1056
+ throw new SerwistError("no-response", {
1057
+ url: request.url,
1058
+ error
1059
+ });
1060
+ }
1061
+ return response;
1062
+ }
1063
+ }
1064
+
1065
+ export { CacheFirst, CacheOnly, NetworkFirst, NetworkOnly, StaleWhileRevalidate, Strategy, StrategyHandler };