@serwist/window 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,689 @@
1
+ import { Deferred, logger, dontWaitFor } from '@serwist/core/internal';
2
+
3
+ /*
4
+ Copyright 2019 Google LLC
5
+
6
+ Use of this source code is governed by an MIT-style
7
+ license that can be found in the LICENSE file or at
8
+ https://opensource.org/licenses/MIT.
9
+ */ /**
10
+ * Sends a data object to a service worker via `postMessage` and resolves with
11
+ * a response (if any).
12
+ *
13
+ * A response can be set in a message handler in the service worker by
14
+ * calling `event.ports[0].postMessage(...)`, which will resolve the promise
15
+ * returned by `messageSW()`. If no response is set, the promise will not
16
+ * resolve.
17
+ *
18
+ * @param sw The service worker to send the message to.
19
+ * @param data An object to send to the service worker.
20
+ * @returns
21
+ */ // Better not change type of data.
22
+ // eslint-disable-next-line @typescript-eslint/ban-types
23
+ function messageSW(sw, data) {
24
+ return new Promise((resolve)=>{
25
+ const messageChannel = new MessageChannel();
26
+ messageChannel.port1.onmessage = (event)=>{
27
+ resolve(event.data);
28
+ };
29
+ sw.postMessage(data, [
30
+ messageChannel.port2
31
+ ]);
32
+ });
33
+ }
34
+
35
+ /*
36
+ Copyright 2019 Google LLC
37
+
38
+ Use of this source code is governed by an MIT-style
39
+ license that can be found in the LICENSE file or at
40
+ https://opensource.org/licenses/MIT.
41
+ */ /**
42
+ * A minimal `Event` subclass shim.
43
+ * This doesn't *actually* subclass `Event` because not all browsers support
44
+ * constructable `EventTarget`, and using a real `Event` will error.
45
+ * @private
46
+ */ class SerwistEvent {
47
+ type;
48
+ target;
49
+ sw;
50
+ originalEvent;
51
+ isExternal;
52
+ constructor(type, props){
53
+ this.type = type;
54
+ Object.assign(this, props);
55
+ }
56
+ }
57
+
58
+ /*
59
+ Copyright 2019 Google LLC
60
+
61
+ Use of this source code is governed by an MIT-style
62
+ license that can be found in the LICENSE file or at
63
+ https://opensource.org/licenses/MIT.
64
+ */ /**
65
+ * A minimal `EventTarget` shim.
66
+ * This is necessary because not all browsers support constructable
67
+ * `EventTarget`, so using a real `EventTarget` will error.
68
+ * @private
69
+ */ class SerwistEventTarget {
70
+ _eventListenerRegistry = new Map();
71
+ /**
72
+ * @param type
73
+ * @param listener
74
+ * @private
75
+ */ addEventListener(type, listener) {
76
+ const foo = this._getEventListenersByType(type);
77
+ foo.add(listener);
78
+ }
79
+ /**
80
+ * @param type
81
+ * @param listener
82
+ * @private
83
+ */ removeEventListener(type, listener) {
84
+ this._getEventListenersByType(type).delete(listener);
85
+ }
86
+ /**
87
+ * @param event
88
+ * @private
89
+ */ dispatchEvent(event) {
90
+ event.target = this;
91
+ const listeners = this._getEventListenersByType(event.type);
92
+ for (const listener of listeners){
93
+ listener(event);
94
+ }
95
+ }
96
+ /**
97
+ * Returns a Set of listeners associated with the passed event type.
98
+ * If no handlers have been registered, an empty Set is returned.
99
+ *
100
+ * @param type The event type.
101
+ * @returns An array of handler functions.
102
+ * @private
103
+ */ _getEventListenersByType(type) {
104
+ if (!this._eventListenerRegistry.has(type)) {
105
+ this._eventListenerRegistry.set(type, new Set());
106
+ }
107
+ return this._eventListenerRegistry.get(type);
108
+ }
109
+ }
110
+
111
+ /*
112
+ Copyright 2019 Google LLC
113
+
114
+ Use of this source code is governed by an MIT-style
115
+ license that can be found in the LICENSE file or at
116
+ https://opensource.org/licenses/MIT.
117
+ */ /**
118
+ * Returns true if two URLs have the same `.href` property. The URLS can be
119
+ * relative, and if they are the current location href is used to resolve URLs.
120
+ *
121
+ * @private
122
+ * @param url1
123
+ * @param url2
124
+ * @returns
125
+ */ function urlsMatch(url1, url2) {
126
+ const { href } = location;
127
+ return new URL(url1, href).href === new URL(url2, href).href;
128
+ }
129
+
130
+ // The time a SW must be in the waiting phase before we can conclude
131
+ // `skipWaiting()` wasn't called. This 200 amount wasn't scientifically
132
+ // chosen, but it seems to avoid false positives in my testing.
133
+ const WAITING_TIMEOUT_DURATION = 200;
134
+ // The amount of time after a registration that we can reasonably conclude
135
+ // that the registration didn't trigger an update.
136
+ const REGISTRATION_TIMEOUT_DURATION = 60000;
137
+ // The de facto standard message that a service worker should be listening for
138
+ // to trigger a call to skipWaiting().
139
+ const SKIP_WAITING_MESSAGE = {
140
+ type: "SKIP_WAITING"
141
+ };
142
+ /**
143
+ * A class to aid in handling service worker registration, updates, and
144
+ * reacting to service worker lifecycle events.
145
+ *
146
+ * @fires `@serwist/window.Serwist.message`
147
+ * @fires `@serwist/window.Serwist.installed`
148
+ * @fires `@serwist/window.Serwist.waiting`
149
+ * @fires `@serwist/window.Serwist.controlling`
150
+ * @fires `@serwist/window.Serwist.activated`
151
+ * @fires `@serwist/window.Serwist.redundant`
152
+ */ class Serwist extends SerwistEventTarget {
153
+ _scriptURL;
154
+ _registerOptions = {};
155
+ _updateFoundCount = 0;
156
+ // Deferreds we can resolve later.
157
+ _swDeferred = new Deferred();
158
+ _activeDeferred = new Deferred();
159
+ _controllingDeferred = new Deferred();
160
+ _registrationTime = 0;
161
+ _isUpdate;
162
+ _compatibleControllingSW;
163
+ _registration;
164
+ _sw;
165
+ _ownSWs = new Set();
166
+ _externalSW;
167
+ _waitingTimeout;
168
+ /**
169
+ * Creates a new Serwist instance with a script URL and service worker
170
+ * options. The script URL and options are the same as those used when
171
+ * calling [navigator.serviceWorker.register(scriptURL, options)](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
172
+ *
173
+ * @param scriptURL The service worker script
174
+ * associated with this instance. Using a
175
+ * [`TrustedScriptURL`](https://web.dev/trusted-types/) is supported.
176
+ * @param registerOptions The service worker options associated
177
+ * with this instance.
178
+ */ // eslint-disable-next-line @typescript-eslint/ban-types
179
+ constructor(scriptURL, registerOptions = {}){
180
+ super();
181
+ this._scriptURL = scriptURL;
182
+ this._registerOptions = registerOptions;
183
+ // Add a message listener immediately since messages received during
184
+ // page load are buffered only until the DOMContentLoaded event:
185
+ // https://github.com/GoogleChrome/workbox/issues/2202
186
+ navigator.serviceWorker.addEventListener("message", this._onMessage);
187
+ }
188
+ /**
189
+ * Registers a service worker for this instances script URL and service
190
+ * worker options. By default this method delays registration until after
191
+ * the window has loaded.
192
+ *
193
+ * @param options
194
+ */ async register({ immediate = false } = {}) {
195
+ if (process.env.NODE_ENV !== "production") {
196
+ if (this._registrationTime) {
197
+ logger.error("Cannot re-register a Serwist instance after it has " + "been registered. Create a new instance instead.");
198
+ return;
199
+ }
200
+ }
201
+ if (!immediate && document.readyState !== "complete") {
202
+ await new Promise((res)=>window.addEventListener("load", res));
203
+ }
204
+ // Set this flag to true if any service worker was controlling the page
205
+ // at registration time.
206
+ this._isUpdate = Boolean(navigator.serviceWorker.controller);
207
+ // Before registering, attempt to determine if a SW is already controlling
208
+ // the page, and if that SW script (and version, if specified) matches this
209
+ // instance's script.
210
+ this._compatibleControllingSW = this._getControllingSWIfCompatible();
211
+ this._registration = await this._registerScript();
212
+ // If we have a compatible controller, store the controller as the "own"
213
+ // SW, resolve active/controlling deferreds and add necessary listeners.
214
+ if (this._compatibleControllingSW) {
215
+ this._sw = this._compatibleControllingSW;
216
+ this._activeDeferred.resolve(this._compatibleControllingSW);
217
+ this._controllingDeferred.resolve(this._compatibleControllingSW);
218
+ this._compatibleControllingSW.addEventListener("statechange", this._onStateChange, {
219
+ once: true
220
+ });
221
+ }
222
+ // If there's a waiting service worker with a matching URL before the
223
+ // `updatefound` event fires, it likely means that this site is open
224
+ // in another tab, or the user refreshed the page (and thus the previous
225
+ // page wasn't fully unloaded before this page started loading).
226
+ // https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#waiting
227
+ const waitingSW = this._registration.waiting;
228
+ if (waitingSW && urlsMatch(waitingSW.scriptURL, this._scriptURL.toString())) {
229
+ // Store the waiting SW as the "own" Sw, even if it means overwriting
230
+ // a compatible controller.
231
+ this._sw = waitingSW;
232
+ // Run this in the next microtask, so any code that adds an event
233
+ // listener after awaiting `register()` will get this event.
234
+ dontWaitFor(Promise.resolve().then(()=>{
235
+ this.dispatchEvent(new SerwistEvent("waiting", {
236
+ sw: waitingSW,
237
+ wasWaitingBeforeRegister: true
238
+ }));
239
+ if (process.env.NODE_ENV !== "production") {
240
+ logger.warn("A service worker was already waiting to activate " + "before this script was registered...");
241
+ }
242
+ }));
243
+ }
244
+ // If an "own" SW is already set, resolve the deferred.
245
+ if (this._sw) {
246
+ this._swDeferred.resolve(this._sw);
247
+ this._ownSWs.add(this._sw);
248
+ }
249
+ if (process.env.NODE_ENV !== "production") {
250
+ logger.log("Successfully registered service worker.", this._scriptURL.toString());
251
+ if (navigator.serviceWorker.controller) {
252
+ if (this._compatibleControllingSW) {
253
+ logger.debug("A service worker with the same script URL " + "is already controlling this page.");
254
+ } else {
255
+ logger.debug("A service worker with a different script URL is " + "currently controlling the page. The browser is now fetching " + "the new script now...");
256
+ }
257
+ }
258
+ const currentPageIsOutOfScope = ()=>{
259
+ const scopeURL = new URL(this._registerOptions.scope || this._scriptURL.toString(), document.baseURI);
260
+ const scopeURLBasePath = new URL("./", scopeURL.href).pathname;
261
+ return !location.pathname.startsWith(scopeURLBasePath);
262
+ };
263
+ if (currentPageIsOutOfScope()) {
264
+ logger.warn("The current page is not in scope for the registered " + "service worker. Was this a mistake?");
265
+ }
266
+ }
267
+ this._registration.addEventListener("updatefound", this._onUpdateFound);
268
+ navigator.serviceWorker.addEventListener("controllerchange", this._onControllerChange);
269
+ return this._registration;
270
+ }
271
+ /**
272
+ * Checks for updates of the registered service worker.
273
+ */ async update() {
274
+ if (!this._registration) {
275
+ if (process.env.NODE_ENV !== "production") {
276
+ logger.error("Cannot update a Serwist instance without " + "being registered. Register the Serwist instance first.");
277
+ }
278
+ return;
279
+ }
280
+ // Try to update registration
281
+ await this._registration.update();
282
+ }
283
+ /**
284
+ * Resolves to the service worker registered by this instance as soon as it
285
+ * is active. If a service worker was already controlling at registration
286
+ * time then it will resolve to that if the script URLs (and optionally
287
+ * script versions) match, otherwise it will wait until an update is found
288
+ * and activates.
289
+ *
290
+ * @returns
291
+ */ get active() {
292
+ return this._activeDeferred.promise;
293
+ }
294
+ /**
295
+ * Resolves to the service worker registered by this instance as soon as it
296
+ * is controlling the page. If a service worker was already controlling at
297
+ * registration time then it will resolve to that if the script URLs (and
298
+ * optionally script versions) match, otherwise it will wait until an update
299
+ * is found and starts controlling the page.
300
+ * Note: the first time a service worker is installed it will active but
301
+ * not start controlling the page unless `clients.claim()` is called in the
302
+ * service worker.
303
+ *
304
+ * @returns
305
+ */ get controlling() {
306
+ return this._controllingDeferred.promise;
307
+ }
308
+ /**
309
+ * Resolves with a reference to a service worker that matches the script URL
310
+ * of this instance, as soon as it's available.
311
+ *
312
+ * If, at registration time, there's already an active or waiting service
313
+ * worker with a matching script URL, it will be used (with the waiting
314
+ * service worker taking precedence over the active service worker if both
315
+ * match, since the waiting service worker would have been registered more
316
+ * recently).
317
+ * If there's no matching active or waiting service worker at registration
318
+ * time then the promise will not resolve until an update is found and starts
319
+ * installing, at which point the installing service worker is used.
320
+ *
321
+ * @returns
322
+ */ getSW() {
323
+ // If `this._sw` is set, resolve with that as we want `getSW()` to
324
+ // return the correct (new) service worker if an update is found.
325
+ return this._sw !== undefined ? Promise.resolve(this._sw) : this._swDeferred.promise;
326
+ }
327
+ /**
328
+ * Sends the passed data object to the service worker registered by this
329
+ * instance (via `@serwist/window.Serwist.getSW`) and resolves
330
+ * with a response (if any).
331
+ *
332
+ * A response can be set in a message handler in the service worker by
333
+ * calling `event.ports[0].postMessage(...)`, which will resolve the promise
334
+ * returned by `messageSW()`. If no response is set, the promise will never
335
+ * resolve.
336
+ *
337
+ * @param data An object to send to the service worker
338
+ * @returns
339
+ */ // We might be able to change the 'data' type to Record<string, unknown> in the future.
340
+ // eslint-disable-next-line @typescript-eslint/ban-types
341
+ async messageSW(data) {
342
+ const sw = await this.getSW();
343
+ return messageSW(sw, data);
344
+ }
345
+ /**
346
+ * Sends a `{type: 'SKIP_WAITING'}` message to the service worker that's
347
+ * currently in the `waiting` state associated with the current registration.
348
+ *
349
+ * If there is no current registration or no service worker is `waiting`,
350
+ * calling this will have no effect.
351
+ */ messageSkipWaiting() {
352
+ if (this._registration && this._registration.waiting) {
353
+ void messageSW(this._registration.waiting, SKIP_WAITING_MESSAGE);
354
+ }
355
+ }
356
+ /**
357
+ * Checks for a service worker already controlling the page and returns
358
+ * it if its script URL matches.
359
+ *
360
+ * @private
361
+ * @returns
362
+ */ _getControllingSWIfCompatible() {
363
+ const controller = navigator.serviceWorker.controller;
364
+ if (controller && urlsMatch(controller.scriptURL, this._scriptURL.toString())) {
365
+ return controller;
366
+ } else {
367
+ return undefined;
368
+ }
369
+ }
370
+ /**
371
+ * Registers a service worker for this instances script URL and register
372
+ * options and tracks the time registration was complete.
373
+ *
374
+ * @private
375
+ */ async _registerScript() {
376
+ try {
377
+ // this._scriptURL may be a TrustedScriptURL, but there's no support for
378
+ // passing that to register() in lib.dom right now.
379
+ // https://github.com/GoogleChrome/workbox/issues/2855
380
+ const reg = await navigator.serviceWorker.register(this._scriptURL, this._registerOptions);
381
+ // Keep track of when registration happened, so it can be used in the
382
+ // `this._onUpdateFound` heuristic. Also use the presence of this
383
+ // property as a way to see if `.register()` has been called.
384
+ this._registrationTime = performance.now();
385
+ return reg;
386
+ } catch (error) {
387
+ if (process.env.NODE_ENV !== "production") {
388
+ logger.error(error);
389
+ }
390
+ // Re-throw the error.
391
+ throw error;
392
+ }
393
+ }
394
+ /**
395
+ * @private
396
+ */ _onUpdateFound = ()=>{
397
+ // `this._registration` will never be `undefined` after an update is found.
398
+ const registration = this._registration;
399
+ const installingSW = registration.installing;
400
+ // If the script URL passed to `navigator.serviceWorker.register()` is
401
+ // different from the current controlling SW's script URL, we know any
402
+ // successful registration calls will trigger an `updatefound` event.
403
+ // But if the registered script URL is the same as the current controlling
404
+ // SW's script URL, we'll only get an `updatefound` event if the file
405
+ // changed since it was last registered. This can be a problem if the user
406
+ // opens up the same page in a different tab, and that page registers
407
+ // a SW that triggers an update. It's a problem because this page has no
408
+ // good way of knowing whether the `updatefound` event came from the SW
409
+ // script it registered or from a registration attempt made by a newer
410
+ // version of the page running in another tab.
411
+ // To minimize the possibility of a false positive, we use the logic here:
412
+ const updateLikelyTriggeredExternally = // Since we enforce only calling `register()` once, and since we don't
413
+ // add the `updatefound` event listener until the `register()` call, if
414
+ // `_updateFoundCount` is > 0 then it means this method has already
415
+ // been called, thus this SW must be external
416
+ this._updateFoundCount > 0 || // If the script URL of the installing SW is different from this
417
+ // instance's script URL, we know it's definitely not from our
418
+ // registration.
419
+ !urlsMatch(installingSW.scriptURL, this._scriptURL.toString()) || // If all of the above are false, then we use a time-based heuristic:
420
+ // Any `updatefound` event that occurs long after our registration is
421
+ // assumed to be external.
422
+ performance.now() > this._registrationTime + REGISTRATION_TIMEOUT_DURATION ? // triggered by this instance.
423
+ true : false;
424
+ if (updateLikelyTriggeredExternally) {
425
+ this._externalSW = installingSW;
426
+ registration.removeEventListener("updatefound", this._onUpdateFound);
427
+ } else {
428
+ // If the update was not triggered externally we know the installing
429
+ // SW is the one we registered, so we set it.
430
+ this._sw = installingSW;
431
+ this._ownSWs.add(installingSW);
432
+ this._swDeferred.resolve(installingSW);
433
+ // The `installing` state isn't something we have a dedicated
434
+ // callback for, but we do log messages for it in development.
435
+ if (process.env.NODE_ENV !== "production") {
436
+ if (navigator.serviceWorker.controller) {
437
+ logger.log("Updated service worker found. Installing now...");
438
+ } else {
439
+ logger.log("Service worker is installing...");
440
+ }
441
+ }
442
+ }
443
+ // Increment the `updatefound` count, so future invocations of this
444
+ // method can be sure they were triggered externally.
445
+ ++this._updateFoundCount;
446
+ // Add a `statechange` listener regardless of whether this update was
447
+ // triggered externally, since we have callbacks for both.
448
+ installingSW.addEventListener("statechange", this._onStateChange);
449
+ };
450
+ /**
451
+ * @private
452
+ * @param originalEvent
453
+ */ _onStateChange = (originalEvent)=>{
454
+ // `this._registration` will never be `undefined` after an update is found.
455
+ const registration = this._registration;
456
+ const sw = originalEvent.target;
457
+ const { state } = sw;
458
+ const isExternal = sw === this._externalSW;
459
+ const eventProps = {
460
+ sw,
461
+ isExternal,
462
+ originalEvent
463
+ };
464
+ if (!isExternal && this._isUpdate) {
465
+ eventProps.isUpdate = true;
466
+ }
467
+ this.dispatchEvent(new SerwistEvent(state, eventProps));
468
+ if (state === "installed") {
469
+ // This timeout is used to ignore cases where the service worker calls
470
+ // `skipWaiting()` in the install event, thus moving it directly in the
471
+ // activating state. (Since all service workers *must* go through the
472
+ // waiting phase, the only way to detect `skipWaiting()` called in the
473
+ // install event is to observe that the time spent in the waiting phase
474
+ // is very short.)
475
+ // NOTE: we don't need separate timeouts for the own and external SWs
476
+ // since they can't go through these phases at the same time.
477
+ this._waitingTimeout = self.setTimeout(()=>{
478
+ // Ensure the SW is still waiting (it may now be redundant).
479
+ if (state === "installed" && registration.waiting === sw) {
480
+ this.dispatchEvent(new SerwistEvent("waiting", eventProps));
481
+ if (process.env.NODE_ENV !== "production") {
482
+ if (isExternal) {
483
+ logger.warn("An external service worker has installed but is " + "waiting for this client to close before activating...");
484
+ } else {
485
+ logger.warn("The service worker has installed but is waiting " + "for existing clients to close before activating...");
486
+ }
487
+ }
488
+ }
489
+ }, WAITING_TIMEOUT_DURATION);
490
+ } else if (state === "activating") {
491
+ clearTimeout(this._waitingTimeout);
492
+ if (!isExternal) {
493
+ this._activeDeferred.resolve(sw);
494
+ }
495
+ }
496
+ if (process.env.NODE_ENV !== "production") {
497
+ switch(state){
498
+ case "installed":
499
+ if (isExternal) {
500
+ logger.warn("An external service worker has installed. " + "You may want to suggest users reload this page.");
501
+ } else {
502
+ logger.log("Registered service worker installed.");
503
+ }
504
+ break;
505
+ case "activated":
506
+ if (isExternal) {
507
+ logger.warn("An external service worker has activated.");
508
+ } else {
509
+ logger.log("Registered service worker activated.");
510
+ if (sw !== navigator.serviceWorker.controller) {
511
+ logger.warn("The registered service worker is active but " + "not yet controlling the page. Reload or run " + "`clients.claim()` in the service worker.");
512
+ }
513
+ }
514
+ break;
515
+ case "redundant":
516
+ if (sw === this._compatibleControllingSW) {
517
+ logger.log("Previously controlling service worker now redundant!");
518
+ } else if (!isExternal) {
519
+ logger.log("Registered service worker now redundant!");
520
+ }
521
+ break;
522
+ }
523
+ }
524
+ };
525
+ /**
526
+ * @private
527
+ * @param originalEvent
528
+ */ _onControllerChange = (originalEvent)=>{
529
+ const sw = this._sw;
530
+ const isExternal = sw !== navigator.serviceWorker.controller;
531
+ // Unconditionally dispatch the controlling event, with isExternal set
532
+ // to distinguish between controller changes due to the initial registration
533
+ // vs. an update-check or other tab's registration.
534
+ // See https://github.com/GoogleChrome/workbox/issues/2786
535
+ this.dispatchEvent(new SerwistEvent("controlling", {
536
+ isExternal,
537
+ originalEvent,
538
+ sw,
539
+ isUpdate: this._isUpdate
540
+ }));
541
+ if (!isExternal) {
542
+ if (process.env.NODE_ENV !== "production") {
543
+ logger.log("Registered service worker now controlling this page.");
544
+ }
545
+ this._controllingDeferred.resolve(sw);
546
+ }
547
+ };
548
+ /**
549
+ * @private
550
+ * @param originalEvent
551
+ */ _onMessage = async (originalEvent)=>{
552
+ // Can't change type 'any' of data.
553
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
554
+ const { data, ports, source } = originalEvent;
555
+ // Wait until there's an "own" service worker. This is used to buffer
556
+ // `message` events that may be received prior to calling `register()`.
557
+ await this.getSW();
558
+ // If the service worker that sent the message is in the list of own
559
+ // service workers for this instance, dispatch a `message` event.
560
+ // NOTE: we check for all previously owned service workers rather than
561
+ // just the current one because some messages (e.g. cache updates) use
562
+ // a timeout when sent and may be delayed long enough for a service worker
563
+ // update to be found.
564
+ if (this._ownSWs.has(source)) {
565
+ this.dispatchEvent(new SerwistEvent("message", {
566
+ // Can't change type 'any' of data.
567
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
568
+ data,
569
+ originalEvent,
570
+ ports,
571
+ sw: source
572
+ }));
573
+ }
574
+ };
575
+ } // The jsdoc comments below outline the events this instance may dispatch:
576
+ // -----------------------------------------------------------------------
577
+ /**
578
+ * The `message` event is dispatched any time a `postMessage` is received.
579
+ *
580
+ * @event workbox-window.Workbox#message
581
+ * @type {SerwistEvent}
582
+ * @property {*} data The `data` property from the original `message` event.
583
+ * @property {Event} originalEvent The original [`message`]{@link https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent}
584
+ * event.
585
+ * @property {string} type `message`.
586
+ * @property {MessagePort[]} ports The `ports` value from `originalEvent`.
587
+ * @property {Workbox} target The `Workbox` instance.
588
+ */ /**
589
+ * The `installed` event is dispatched if the state of a
590
+ * {@link workbox-window.Workbox} instance's
591
+ * {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
592
+ * changes to `installed`.
593
+ *
594
+ * Then can happen either the very first time a service worker is installed,
595
+ * or after an update to the current service worker is found. In the case
596
+ * of an update being found, the event's `isUpdate` property will be `true`.
597
+ *
598
+ * @event workbox-window.Workbox#installed
599
+ * @type {SerwistEvent}
600
+ * @property {ServiceWorker} sw The service worker instance.
601
+ * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
602
+ * event.
603
+ * @property {boolean|undefined} isUpdate True if a service worker was already
604
+ * controlling when this `Workbox` instance called `register()`.
605
+ * @property {boolean|undefined} isExternal True if this event is associated
606
+ * with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
607
+ * @property {string} type `installed`.
608
+ * @property {Workbox} target The `Workbox` instance.
609
+ */ /**
610
+ * The `waiting` event is dispatched if the state of a
611
+ * {@link workbox-window.Workbox} instance's
612
+ * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
613
+ * changes to `installed` and then doesn't immediately change to `activating`.
614
+ * It may also be dispatched if a service worker with the same
615
+ * [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
616
+ * was already waiting when the {@link workbox-window.Workbox#register}
617
+ * method was called.
618
+ *
619
+ * @event workbox-window.Workbox#waiting
620
+ * @type {SerwistEvent}
621
+ * @property {ServiceWorker} sw The service worker instance.
622
+ * @property {Event|undefined} originalEvent The original
623
+ * [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
624
+ * event, or `undefined` in the case where the service worker was waiting
625
+ * to before `.register()` was called.
626
+ * @property {boolean|undefined} isUpdate True if a service worker was already
627
+ * controlling when this `Workbox` instance called `register()`.
628
+ * @property {boolean|undefined} isExternal True if this event is associated
629
+ * with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
630
+ * @property {boolean|undefined} wasWaitingBeforeRegister True if a service worker with
631
+ * a matching `scriptURL` was already waiting when this `Workbox`
632
+ * instance called `register()`.
633
+ * @property {string} type `waiting`.
634
+ * @property {Workbox} target The `Workbox` instance.
635
+ */ /**
636
+ * The `controlling` event is dispatched if a
637
+ * [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
638
+ * fires on the service worker [container]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer}
639
+ * and the [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
640
+ * of the new [controller]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/controller}
641
+ * matches the `scriptURL` of the `Workbox` instance's
642
+ * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}.
643
+ *
644
+ * @event workbox-window.Workbox#controlling
645
+ * @type {SerwistEvent}
646
+ * @property {ServiceWorker} sw The service worker instance.
647
+ * @property {Event} originalEvent The original [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
648
+ * event.
649
+ * @property {boolean|undefined} isUpdate True if a service worker was already
650
+ * controlling when this service worker was registered.
651
+ * @property {boolean|undefined} isExternal True if this event is associated
652
+ * with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
653
+ * @property {string} type `controlling`.
654
+ * @property {Workbox} target The `Workbox` instance.
655
+ */ /**
656
+ * The `activated` event is dispatched if the state of a
657
+ * {@link workbox-window.Workbox} instance's
658
+ * {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
659
+ * changes to `activated`.
660
+ *
661
+ * @event workbox-window.Workbox#activated
662
+ * @type {SerwistEvent}
663
+ * @property {ServiceWorker} sw The service worker instance.
664
+ * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
665
+ * event.
666
+ * @property {boolean|undefined} isUpdate True if a service worker was already
667
+ * controlling when this `Workbox` instance called `register()`.
668
+ * @property {boolean|undefined} isExternal True if this event is associated
669
+ * with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
670
+ * @property {string} type `activated`.
671
+ * @property {Workbox} target The `Workbox` instance.
672
+ */ /**
673
+ * The `redundant` event is dispatched if the state of a
674
+ * {@link workbox-window.Workbox} instance's
675
+ * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
676
+ * changes to `redundant`.
677
+ *
678
+ * @event workbox-window.Workbox#redundant
679
+ * @type {SerwistEvent}
680
+ * @property {ServiceWorker} sw The service worker instance.
681
+ * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
682
+ * event.
683
+ * @property {boolean|undefined} isUpdate True if a service worker was already
684
+ * controlling when this `Workbox` instance called `register()`.
685
+ * @property {string} type `redundant`.
686
+ * @property {Workbox} target The `Workbox` instance.
687
+ */
688
+
689
+ export { Serwist, SerwistEvent, messageSW };