@serwist/strategies 9.0.0-preview.9 → 9.0.1

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.
@@ -1,544 +0,0 @@
1
- /*
2
- Copyright 2020 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import type { HandlerCallbackOptions, MapLikeObject, SerwistPlugin, SerwistPluginCallbackParam } from "@serwist/core";
10
- import {
11
- assert,
12
- Deferred,
13
- SerwistError,
14
- cacheMatchIgnoreParams,
15
- executeQuotaErrorCallbacks,
16
- getFriendlyURL,
17
- logger,
18
- timeout,
19
- } from "@serwist/core/internal";
20
-
21
- import type { Strategy } from "./Strategy.js";
22
-
23
- function toRequest(input: RequestInfo) {
24
- return typeof input === "string" ? new Request(input) : input;
25
- }
26
-
27
- /**
28
- * A class created every time a Strategy instance instance calls `Strategy.handle` or
29
- * `Strategy.handleAll` that wraps all fetch and cache actions around plugin callbacks
30
- * and keeps track of when the strategy is "done" (i.e. all added `event.waitUntil()` promises
31
- * have resolved).
32
- */
33
- export class StrategyHandler {
34
- /**
35
- * The request the strategy is performing (passed to the strategy's
36
- * `handle()` or `handleAll()` method).
37
- */
38
- public request!: Request;
39
- /**
40
- * A `URL` instance of `request.url` (if passed to the strategy's
41
- * `handle()` or `handleAll()` method).
42
- * Note: the `url` param will be present if the strategy was invoked
43
- * from a `@serwist/routing.Route` object.
44
- */
45
- public url?: URL;
46
- /**
47
- * The event associated with this request.
48
- */
49
- public event: ExtendableEvent;
50
- /**
51
- * A `param` value (if passed to the strategy's
52
- * `handle()` or `handleAll()` method).
53
- * Note: the `param` param will be present if the strategy was invoked
54
- * from a `@serwist/routing.Route` object and the `@serwist/strategies.matchCallback`
55
- * returned a truthy value (it will be that value).
56
- */
57
- public params?: any;
58
-
59
- private _cacheKeys: Record<string, Request> = {};
60
-
61
- private readonly _strategy: Strategy;
62
- private readonly _extendLifetimePromises: Promise<any>[];
63
- private readonly _handlerDeferred: Deferred<any>;
64
- private readonly _plugins: SerwistPlugin[];
65
- private readonly _pluginStateMap: Map<SerwistPlugin, MapLikeObject>;
66
-
67
- /**
68
- * Creates a new instance associated with the passed strategy and event
69
- * that's handling the request.
70
- *
71
- * The constructor also initializes the state that will be passed to each of
72
- * the plugins handling this request.
73
- *
74
- * @param strategy
75
- * @param options
76
- */
77
- constructor(strategy: Strategy, options: HandlerCallbackOptions) {
78
- if (process.env.NODE_ENV !== "production") {
79
- assert!.isInstance(options.event, ExtendableEvent, {
80
- moduleName: "@serwist/strategies",
81
- className: "StrategyHandler",
82
- funcName: "constructor",
83
- paramName: "options.event",
84
- });
85
- }
86
-
87
- Object.assign(this, options);
88
-
89
- this.event = options.event;
90
- this._strategy = strategy;
91
- this._handlerDeferred = new Deferred();
92
- this._extendLifetimePromises = [];
93
-
94
- // Copy the plugins list (since it's mutable on the strategy),
95
- // so any mutations don't affect this handler instance.
96
- this._plugins = [...strategy.plugins];
97
- this._pluginStateMap = new Map();
98
- for (const plugin of this._plugins) {
99
- this._pluginStateMap.set(plugin, {});
100
- }
101
-
102
- this.event.waitUntil(this._handlerDeferred.promise);
103
- }
104
-
105
- /**
106
- * Fetches a given request (and invokes any applicable plugin callback
107
- * methods) using the `fetchOptions` (for non-navigation requests) and
108
- * `plugins` defined on the `Strategy` object.
109
- *
110
- * The following plugin lifecycle methods are invoked when using this method:
111
- * - `requestWillFetch()`
112
- * - `fetchDidSucceed()`
113
- * - `fetchDidFail()`
114
- *
115
- * @param input The URL or request to fetch.
116
- * @returns
117
- */
118
- async fetch(input: RequestInfo): Promise<Response> {
119
- const { event } = this;
120
- let request: Request = toRequest(input);
121
-
122
- if (request.mode === "navigate" && event instanceof FetchEvent && event.preloadResponse) {
123
- const possiblePreloadResponse = (await event.preloadResponse) as Response | undefined;
124
- if (possiblePreloadResponse) {
125
- if (process.env.NODE_ENV !== "production") {
126
- logger.log(`Using a preloaded navigation response for '${getFriendlyURL(request.url)}'`);
127
- }
128
- return possiblePreloadResponse;
129
- }
130
- }
131
-
132
- // If there is a fetchDidFail plugin, we need to save a clone of the
133
- // original request before it's either modified by a requestWillFetch
134
- // plugin or before the original request's body is consumed via fetch().
135
- const originalRequest = this.hasCallback("fetchDidFail") ? request.clone() : null;
136
-
137
- try {
138
- for (const cb of this.iterateCallbacks("requestWillFetch")) {
139
- request = await cb({ request: request.clone(), event });
140
- }
141
- } catch (err) {
142
- if (err instanceof Error) {
143
- throw new SerwistError("plugin-error-request-will-fetch", {
144
- thrownErrorMessage: err.message,
145
- });
146
- }
147
- }
148
-
149
- // The request can be altered by plugins with `requestWillFetch` making
150
- // the original request (most likely from a `fetch` event) different
151
- // from the Request we make. Pass both to `fetchDidFail` to aid debugging.
152
- const pluginFilteredRequest: Request = request.clone();
153
-
154
- try {
155
- let fetchResponse: Response;
156
-
157
- // See https://github.com/GoogleChrome/workbox/issues/1796
158
- fetchResponse = await fetch(request, request.mode === "navigate" ? undefined : this._strategy.fetchOptions);
159
-
160
- if (process.env.NODE_ENV !== "production") {
161
- logger.debug(`Network request for '${getFriendlyURL(request.url)}' returned a response with status '${fetchResponse.status}'.`);
162
- }
163
-
164
- for (const callback of this.iterateCallbacks("fetchDidSucceed")) {
165
- fetchResponse = await callback({
166
- event,
167
- request: pluginFilteredRequest,
168
- response: fetchResponse,
169
- });
170
- }
171
- return fetchResponse;
172
- } catch (error) {
173
- if (process.env.NODE_ENV !== "production") {
174
- logger.log(`Network request for '${getFriendlyURL(request.url)}' threw an error.`, error);
175
- }
176
-
177
- // `originalRequest` will only exist if a `fetchDidFail` callback
178
- // is being used (see above).
179
- if (originalRequest) {
180
- await this.runCallbacks("fetchDidFail", {
181
- error: error as Error,
182
- event,
183
- originalRequest: originalRequest.clone(),
184
- request: pluginFilteredRequest.clone(),
185
- });
186
- }
187
- throw error;
188
- }
189
- }
190
-
191
- /**
192
- * Calls `this.fetch()` and (in the background) runs `this.cachePut()` on
193
- * the response generated by `this.fetch()`.
194
- *
195
- * The call to `this.cachePut()` automatically invokes `this.waitUntil()`,
196
- * so you do not have to manually call `waitUntil()` on the event.
197
- *
198
- * @param input The request or URL to fetch and cache.
199
- * @returns
200
- */
201
- async fetchAndCachePut(input: RequestInfo): Promise<Response> {
202
- const response = await this.fetch(input);
203
- const responseClone = response.clone();
204
-
205
- void this.waitUntil(this.cachePut(input, responseClone));
206
-
207
- return response;
208
- }
209
-
210
- /**
211
- * Matches a request from the cache (and invokes any applicable plugin
212
- * callback methods) using the `cacheName`, `matchOptions`, and `plugins`
213
- * defined on the strategy object.
214
- *
215
- * The following plugin lifecycle methods are invoked when using this method:
216
- * - cacheKeyWillByUsed()
217
- * - cachedResponseWillByUsed()
218
- *
219
- * @param key The Request or URL to use as the cache key.
220
- * @returns A matching response, if found.
221
- */
222
- async cacheMatch(key: RequestInfo): Promise<Response | undefined> {
223
- const request: Request = toRequest(key);
224
- let cachedResponse: Response | undefined;
225
- const { cacheName, matchOptions } = this._strategy;
226
-
227
- const effectiveRequest = await this.getCacheKey(request, "read");
228
- const multiMatchOptions = { ...matchOptions, ...{ cacheName } };
229
-
230
- cachedResponse = await caches.match(effectiveRequest, multiMatchOptions);
231
-
232
- if (process.env.NODE_ENV !== "production") {
233
- if (cachedResponse) {
234
- logger.debug(`Found a cached response in '${cacheName}'.`);
235
- } else {
236
- logger.debug(`No cached response found in '${cacheName}'.`);
237
- }
238
- }
239
-
240
- for (const callback of this.iterateCallbacks("cachedResponseWillBeUsed")) {
241
- cachedResponse =
242
- (await callback({
243
- cacheName,
244
- matchOptions,
245
- cachedResponse,
246
- request: effectiveRequest,
247
- event: this.event,
248
- })) || undefined;
249
- }
250
- return cachedResponse;
251
- }
252
-
253
- /**
254
- * Puts a request/response pair in the cache (and invokes any applicable
255
- * plugin callback methods) using the `cacheName` and `plugins` defined on
256
- * the strategy object.
257
- *
258
- * The following plugin lifecycle methods are invoked when using this method:
259
- * - cacheKeyWillByUsed()
260
- * - cacheWillUpdate()
261
- * - cacheDidUpdate()
262
- *
263
- * @param key The request or URL to use as the cache key.
264
- * @param response The response to cache.
265
- * @returns `false` if a cacheWillUpdate caused the response
266
- * not be cached, and `true` otherwise.
267
- */
268
- async cachePut(key: RequestInfo, response: Response): Promise<boolean> {
269
- const request: Request = toRequest(key);
270
-
271
- // Run in the next task to avoid blocking other cache reads.
272
- // https://github.com/w3c/ServiceWorker/issues/1397
273
- await timeout(0);
274
-
275
- const effectiveRequest = await this.getCacheKey(request, "write");
276
-
277
- if (process.env.NODE_ENV !== "production") {
278
- if (effectiveRequest.method && effectiveRequest.method !== "GET") {
279
- throw new SerwistError("attempt-to-cache-non-get-request", {
280
- url: getFriendlyURL(effectiveRequest.url),
281
- method: effectiveRequest.method,
282
- });
283
- }
284
- }
285
-
286
- if (!response) {
287
- if (process.env.NODE_ENV !== "production") {
288
- logger.error(`Cannot cache non-existent response for '${getFriendlyURL(effectiveRequest.url)}'.`);
289
- }
290
-
291
- throw new SerwistError("cache-put-with-no-response", {
292
- url: getFriendlyURL(effectiveRequest.url),
293
- });
294
- }
295
-
296
- const responseToCache = await this._ensureResponseSafeToCache(response);
297
-
298
- if (!responseToCache) {
299
- if (process.env.NODE_ENV !== "production") {
300
- logger.debug(`Response '${getFriendlyURL(effectiveRequest.url)}' will not be cached.`, responseToCache);
301
- }
302
- return false;
303
- }
304
-
305
- const { cacheName, matchOptions } = this._strategy;
306
- const cache = await self.caches.open(cacheName);
307
-
308
- if (process.env.NODE_ENV !== "production") {
309
- // See https://github.com/GoogleChrome/workbox/issues/2818
310
- const vary = response.headers.get("Vary");
311
- if (vary && matchOptions?.ignoreVary !== true) {
312
- logger.debug(
313
- `The response for ${getFriendlyURL(
314
- effectiveRequest.url,
315
- )} has a 'Vary: ${vary}' header. Consider setting the {ignoreVary: true} option on your strategy to ensure cache matching and deletion works as expected.`,
316
- );
317
- }
318
- }
319
-
320
- const hasCacheUpdateCallback = this.hasCallback("cacheDidUpdate");
321
- const oldResponse = hasCacheUpdateCallback
322
- ? await cacheMatchIgnoreParams(
323
- // TODO(philipwalton): the `__WB_REVISION__` param is a precaching
324
- // feature. Consider into ways to only add this behavior if using
325
- // precaching.
326
- cache,
327
- effectiveRequest.clone(),
328
- ["__WB_REVISION__"],
329
- matchOptions,
330
- )
331
- : null;
332
-
333
- if (process.env.NODE_ENV !== "production") {
334
- logger.debug(`Updating the '${cacheName}' cache with a new Response for ${getFriendlyURL(effectiveRequest.url)}.`);
335
- }
336
-
337
- try {
338
- await cache.put(effectiveRequest, hasCacheUpdateCallback ? responseToCache.clone() : responseToCache);
339
- } catch (error) {
340
- if (error instanceof Error) {
341
- // See https://developer.mozilla.org/en-US/docs/Web/API/DOMException#exception-QuotaExceededError
342
- if (error.name === "QuotaExceededError") {
343
- await executeQuotaErrorCallbacks();
344
- }
345
- throw error;
346
- }
347
- }
348
-
349
- for (const callback of this.iterateCallbacks("cacheDidUpdate")) {
350
- await callback({
351
- cacheName,
352
- oldResponse,
353
- newResponse: responseToCache.clone(),
354
- request: effectiveRequest,
355
- event: this.event,
356
- });
357
- }
358
-
359
- return true;
360
- }
361
-
362
- /**
363
- * Checks the list of plugins for the `cacheKeyWillBeUsed` callback, and
364
- * executes any of those callbacks found in sequence. The final `Request`
365
- * object returned by the last plugin is treated as the cache key for cache
366
- * reads and/or writes. If no `cacheKeyWillBeUsed` plugin callbacks have
367
- * been registered, the passed request is returned unmodified
368
- *
369
- * @param request
370
- * @param mode
371
- * @returns
372
- */
373
- async getCacheKey(request: Request, mode: "read" | "write"): Promise<Request> {
374
- const key = `${request.url} | ${mode}`;
375
- if (!this._cacheKeys[key]) {
376
- let effectiveRequest = request;
377
-
378
- for (const callback of this.iterateCallbacks("cacheKeyWillBeUsed")) {
379
- effectiveRequest = toRequest(
380
- await callback({
381
- mode,
382
- request: effectiveRequest,
383
- event: this.event,
384
- params: this.params,
385
- }),
386
- );
387
- }
388
-
389
- this._cacheKeys[key] = effectiveRequest;
390
- }
391
- return this._cacheKeys[key];
392
- }
393
-
394
- /**
395
- * Returns true if the strategy has at least one plugin with the given
396
- * callback.
397
- *
398
- * @param name The name of the callback to check for.
399
- * @returns
400
- */
401
- hasCallback<C extends keyof SerwistPlugin>(name: C): boolean {
402
- for (const plugin of this._strategy.plugins) {
403
- if (name in plugin) {
404
- return true;
405
- }
406
- }
407
- return false;
408
- }
409
-
410
- /**
411
- * Runs all plugin callbacks matching the given name, in order, passing the
412
- * given param object (merged ith the current plugin state) as the only
413
- * argument.
414
- *
415
- * Note: since this method runs all plugins, it's not suitable for cases
416
- * where the return value of a callback needs to be applied prior to calling
417
- * the next callback. See `@serwist/strategies.iterateCallbacks` for how to handle that case.
418
- *
419
- * @param name The name of the callback to run within each plugin.
420
- * @param param The object to pass as the first (and only) param when executing each callback. This object will be merged with the
421
- * current plugin state prior to callback execution.
422
- */
423
- async runCallbacks<C extends keyof NonNullable<SerwistPlugin>>(name: C, param: Omit<SerwistPluginCallbackParam[C], "state">): Promise<void> {
424
- for (const callback of this.iterateCallbacks(name)) {
425
- // TODO(philipwalton): not sure why `any` is needed. It seems like
426
- // this should work with `as SerwistPluginCallbackParam[C]`.
427
- await callback(param as any);
428
- }
429
- }
430
-
431
- /**
432
- * Accepts a callback and returns an iterable of matching plugin callbacks,
433
- * where each callback is wrapped with the current handler state (i.e. when
434
- * you call each callback, whatever object parameter you pass it will
435
- * be merged with the plugin's current state).
436
- *
437
- * @param name The name fo the callback to run
438
- * @returns
439
- */
440
- *iterateCallbacks<C extends keyof SerwistPlugin>(name: C): Generator<NonNullable<SerwistPlugin[C]>> {
441
- for (const plugin of this._strategy.plugins) {
442
- if (typeof plugin[name] === "function") {
443
- const state = this._pluginStateMap.get(plugin);
444
- const statefulCallback = (param: Omit<SerwistPluginCallbackParam[C], "state">) => {
445
- const statefulParam = { ...param, state };
446
-
447
- // TODO(philipwalton): not sure why `any` is needed. It seems like
448
- // this should work with `as WorkboxPluginCallbackParam[C]`.
449
- return plugin[name]!(statefulParam as any);
450
- };
451
- yield statefulCallback as NonNullable<SerwistPlugin[C]>;
452
- }
453
- }
454
- }
455
-
456
- /**
457
- * Adds a promise to the
458
- * [extend lifetime promises](https://w3c.github.io/ServiceWorker/#extendableevent-extend-lifetime-promises)
459
- * of the event event associated with the request being handled (usually a `FetchEvent`).
460
- *
461
- * Note: you can await
462
- * `@serwist/strategies.StrategyHandler.doneWaiting`
463
- * to know when all added promises have settled.
464
- *
465
- * @param promise A promise to add to the extend lifetime promises of
466
- * the event that triggered the request.
467
- */
468
- waitUntil<T>(promise: Promise<T>): Promise<T> {
469
- this._extendLifetimePromises.push(promise);
470
- return promise;
471
- }
472
-
473
- /**
474
- * Returns a promise that resolves once all promises passed to
475
- * `@serwist/strategies.StrategyHandler.waitUntil` have settled.
476
- *
477
- * Note: any work done after `doneWaiting()` settles should be manually
478
- * passed to an event's `waitUntil()` method (not this handler's
479
- * `waitUntil()` method), otherwise the service worker thread my be killed
480
- * prior to your work completing.
481
- */
482
- async doneWaiting(): Promise<void> {
483
- let promise: Promise<any> | undefined = undefined;
484
- while ((promise = this._extendLifetimePromises.shift())) {
485
- await promise;
486
- }
487
- }
488
-
489
- /**
490
- * Stops running the strategy and immediately resolves any pending
491
- * `waitUntil()` promises.
492
- */
493
- destroy(): void {
494
- this._handlerDeferred.resolve(null);
495
- }
496
-
497
- /**
498
- * This method will call `cacheWillUpdate` on the available plugins (or use
499
- * status === 200) to determine if the response is safe and valid to cache.
500
- *
501
- * @param response
502
- * @returns
503
- * @private
504
- */
505
- async _ensureResponseSafeToCache(response: Response): Promise<Response | undefined> {
506
- let responseToCache: Response | undefined = response;
507
- let pluginsUsed = false;
508
-
509
- for (const callback of this.iterateCallbacks("cacheWillUpdate")) {
510
- responseToCache =
511
- (await callback({
512
- request: this.request,
513
- response: responseToCache,
514
- event: this.event,
515
- })) || undefined;
516
- pluginsUsed = true;
517
-
518
- if (!responseToCache) {
519
- break;
520
- }
521
- }
522
-
523
- if (!pluginsUsed) {
524
- if (responseToCache && responseToCache.status !== 200) {
525
- responseToCache = undefined;
526
- }
527
- if (process.env.NODE_ENV !== "production") {
528
- if (responseToCache) {
529
- if (responseToCache.status !== 200) {
530
- if (responseToCache.status === 0) {
531
- logger.warn(
532
- `The response for '${this.request.url}' is an opaque response. The caching strategy that you're using will not cache opaque responses by default.`,
533
- );
534
- } else {
535
- logger.debug(`The response for '${this.request.url}' returned a status code of '${response.status}' and won't be cached as a result.`);
536
- }
537
- }
538
- }
539
- }
540
- }
541
-
542
- return responseToCache;
543
- }
544
- }
@@ -1,26 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import type { SerwistPlugin } from "@serwist/core";
10
-
11
- export const cacheOkAndOpaquePlugin: SerwistPlugin = {
12
- /**
13
- * Returns a valid response (to allow caching) if the status is 200 (OK) or
14
- * 0 (opaque).
15
- *
16
- * @param options
17
- * @returns
18
- * @private
19
- */
20
- cacheWillUpdate: async ({ response }) => {
21
- if (response.status === 200 || response.status === 0) {
22
- return response;
23
- }
24
- return null;
25
- },
26
- };
@@ -1,20 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { getFriendlyURL, logger } from "@serwist/core/internal";
10
-
11
- export const messages = {
12
- strategyStart: (strategyName: string, request: Request): string => `Using ${strategyName} to respond to '${getFriendlyURL(request.url)}'`,
13
- printFinalResponse: (response?: Response): void => {
14
- if (response) {
15
- logger.groupCollapsed("View the final response here.");
16
- logger.log(response || "[No response returned]");
17
- logger.groupEnd();
18
- }
19
- },
20
- };