summaryrefslogtreecommitdiff
path: root/dist/workbox-v3.2.0/workbox-background-sync.dev.js
blob: fcb3e016ec7e55d7888758706489628bfc8795ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
this.workbox = this.workbox || {};
this.workbox.backgroundSync = (function (DBWrapper_mjs,WorkboxError_mjs,logger_mjs,assert_mjs,getFriendlyURL_mjs) {
'use strict';

try {
  self.workbox.v['workbox:background-sync:3.2.0'] = 1;
} catch (e) {} // eslint-disable-line

/*
 Copyright 2017 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

const serializableProperties = ['method', 'referrer', 'referrerPolicy', 'mode', 'credentials', 'cache', 'redirect', 'integrity', 'keepalive'];

/**
 * A class to make it easier to serialize and de-serialize requests so they
 * can be stored in IndexedDB.
 *
 * @private
 */
class StorableRequest {
  /**
   * Converts a Request object to a plain object that can be structured
   * cloned or JSON-stringified.
   *
   * @param {Request} request
   * @return {Promise<StorableRequest>}
   *
   * @private
   */
  static fromRequest(request) {
    return babelHelpers.asyncToGenerator(function* () {
      const requestInit = { headers: {} };

      // Set the body if present.
      if (request.method !== 'GET') {
        // Use blob to support non-text request bodies,
        // and clone first in case the caller still needs the request.
        requestInit.body = yield request.clone().blob();
      }

      // Convert the headers from an iterable to an object.
      for (const [key, value] of request.headers.entries()) {
        requestInit.headers[key] = value;
      }

      // Add all other serializable request properties
      for (const prop of serializableProperties) {
        if (request[prop] !== undefined) {
          requestInit[prop] = request[prop];
        }
      }

      return new StorableRequest({ url: request.url, requestInit });
    })();
  }

  /**
   * Accepts a URL and RequestInit dictionary that can be used to create a
   * new Request object. A timestamp is also generated so consumers can
   * reference when the object was created.
   *
   * @param {Object} param1
   * @param {string} param1.url
   * @param {Object} param1.requestInit
   *     See: https://fetch.spec.whatwg.org/#requestinit
   * @param {number} param1.timestamp The time the request was created,
   *     defaulting to the current time if not specified.
   *
   * @private
   */
  constructor({ url, requestInit, timestamp = Date.now() }) {
    this.url = url;
    this.requestInit = requestInit;

    // "Private"
    this._timestamp = timestamp;
  }

  /**
   * Gets the private _timestamp property.
   *
   * @return {number}
   *
   * @private
   */
  get timestamp() {
    return this._timestamp;
  }

  /**
   * Coverts this instance to a plain Object.
   *
   * @return {Object}
   *
   * @private
   */
  toObject() {
    return {
      url: this.url,
      timestamp: this.timestamp,
      requestInit: this.requestInit
    };
  }

  /**
   * Converts this instance to a Request.
   *
   * @return {Request}
   *
   * @private
   */
  toRequest() {
    return new Request(this.url, this.requestInit);
  }

  /**
   * Creates and returns a deep clone of the instance.
   *
   * @return {StorableRequest}
   *
   * @private
   */
  clone() {
    const requestInit = Object.assign({}, this.requestInit);
    requestInit.headers = Object.assign({}, this.requestInit.headers);
    if (this.requestInit.body) {
      requestInit.body = this.requestInit.body.slice();
    }

    return new StorableRequest({
      url: this.url,
      timestamp: this.timestamp,
      requestInit
    });
  }
}

/*
 Copyright 2017 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

const DB_NAME = 'workbox-background-sync';
const OBJECT_STORE_NAME = 'requests';
const INDEXED_PROP = 'queueName';
const TAG_PREFIX = 'workbox-background-sync';
const MAX_RETENTION_TIME = 60 * 24 * 7; // 7 days in minutes

/*
 Copyright 2017 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/**
 * A class to manage storing requests from a Queue in IndexedbDB,
 * indexed by their queue name for easier access.
 *
 * @private
 */
class QueueStore {
  /**
   * Associates this instance with a Queue instance, so entries added can be
   * identified by their queue name.
   *
   * @param {Queue} queue
   *
   * @private
   */
  constructor(queue) {
    this._queue = queue;
    this._db = new DBWrapper_mjs.DBWrapper(DB_NAME, 1, {
      onupgradeneeded: evt => evt.target.result.createObjectStore(OBJECT_STORE_NAME, { autoIncrement: true }).createIndex(INDEXED_PROP, INDEXED_PROP, { unique: false })
    });
  }

  /**
   * Takes a StorableRequest instance, converts it to an object and adds it
   * as an entry in the object store.
   *
   * @param {StorableRequest} storableRequest
   *
   * @private
   */
  addEntry(storableRequest) {
    var _this = this;

    return babelHelpers.asyncToGenerator(function* () {
      yield _this._db.add(OBJECT_STORE_NAME, {
        queueName: _this._queue.name,
        storableRequest: storableRequest.toObject()
      });
    })();
  }

  /**
   * Gets the oldest entry in the object store, removes it, and returns the
   * value as a StorableRequest instance. If no entry exists, it returns
   * undefined.
   *
   * @return {StorableRequest|undefined}
   *
   * @private
   */
  getAndRemoveOldestEntry() {
    var _this2 = this;

    return babelHelpers.asyncToGenerator(function* () {
      const [entry] = yield _this2._db.getAllMatching(OBJECT_STORE_NAME, {
        index: INDEXED_PROP,
        query: IDBKeyRange.only(_this2._queue.name),
        count: 1,
        includeKeys: true
      });

      if (entry) {
        yield _this2._db.delete(OBJECT_STORE_NAME, entry.primaryKey);
        return new StorableRequest(entry.value.storableRequest);
      }
    })();
  }
}

/*
 Copyright 2017 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

const queueNames = new Set();

/**
 * A class to manage storing failed requests in IndexedDB and retrying them
 * later. All parts of the storing and replaying process are observable via
 * callbacks.
 *
 * @memberof workbox.backgroundSync
 */
class Queue {
  /**
   * Creates an instance of Queue with the given options
   *
   * @param {string} name The unique name for this queue. This name must be
   *     unique as it's used to register sync events and store requests
   *     in IndexedDB specific to this instance. An error will be thrown if
   *     a duplicate name is detected.
   * @param {Object} [options]
   * @param {Object} [options.callbacks] Callbacks to observe the lifecycle of
   *     queued requests. Use these to respond to or modify the requests
   *     during the replay process.
   * @param {function(StorableRequest):undefined}
   *     [options.callbacks.requestWillEnqueue]
   *     Invoked immediately before the request is stored to IndexedDB. Use
   *     this callback to modify request data at store time.
   * @param {function(StorableRequest):undefined}
   *     [options.callbacks.requestWillReplay]
   *     Invoked immediately before the request is re-fetched. Use this
   *     callback to modify request data at fetch time.
   * @param {function(Array<StorableRequest>):undefined}
   *     [options.callbacks.queueDidReplay]
   *     Invoked after all requests in the queue have successfully replayed.
   * @param {number} [options.maxRetentionTime = 7 days] The amount of time (in
   *     minutes) a request may be retried. After this amount of time has
   *     passed, the request will be deleted from the queue.
   */
  constructor(name, {
    callbacks = {},
    maxRetentionTime = MAX_RETENTION_TIME
  } = {}) {
    // Ensure the store name is not already being used
    if (queueNames.has(name)) {
      throw new WorkboxError_mjs.WorkboxError('duplicate-queue-name', { name });
    } else {
      queueNames.add(name);
    }

    this._name = name;
    this._callbacks = callbacks;
    this._maxRetentionTime = maxRetentionTime;
    this._queueStore = new QueueStore(this);

    this._addSyncListener();
  }

  /**
   * @return {string}
   */
  get name() {
    return this._name;
  }

  /**
   * Stores the passed request into IndexedDB. The database used is
   * `workbox-background-sync` and the object store name is the same as
   * the name this instance was created with (to guarantee it's unique).
   *
   * @param {Request} request The request object to store.
   */
  addRequest(request) {
    var _this = this;

    return babelHelpers.asyncToGenerator(function* () {
      {
        assert_mjs.assert.isInstance(request, Request, {
          moduleName: 'workbox-background-sync',
          className: 'Queue',
          funcName: 'addRequest',
          paramName: 'request'
        });
      }

      const storableRequest = yield StorableRequest.fromRequest(request.clone());
      yield _this._runCallback('requestWillEnqueue', storableRequest);
      yield _this._queueStore.addEntry(storableRequest);
      yield _this._registerSync();
      {
        logger_mjs.logger.log(`Request for '${getFriendlyURL_mjs.getFriendlyURL(storableRequest.url)}' has been
          added to background sync queue '${_this._name}'.`);
      }
    })();
  }

  /**
   * Retrieves all stored requests in IndexedDB and retries them. If the
   * queue contained requests that were successfully replayed, the
   * `queueDidReplay` callback is invoked (which implies the queue is
   * now empty). If any of the requests fail, a new sync registration is
   * created to retry again later.
   */
  replayRequests() {
    var _this2 = this;

    return babelHelpers.asyncToGenerator(function* () {
      const now = Date.now();
      const replayedRequests = [];
      const failedRequests = [];

      let storableRequest;
      while (storableRequest = yield _this2._queueStore.getAndRemoveOldestEntry()) {
        // Make a copy so the unmodified request can be stored
        // in the event of a replay failure.
        const storableRequestClone = storableRequest.clone();

        // Ignore requests older than maxRetentionTime.
        const maxRetentionTimeInMs = _this2._maxRetentionTime * 60 * 1000;
        if (now - storableRequest.timestamp > maxRetentionTimeInMs) {
          continue;
        }

        yield _this2._runCallback('requestWillReplay', storableRequest);

        const replay = { request: storableRequest.toRequest() };

        try {
          // Clone the request before fetching so callbacks get an unused one.
          replay.response = yield fetch(replay.request.clone());
          {
            logger_mjs.logger.log(`Request for '${getFriendlyURL_mjs.getFriendlyURL(storableRequest.url)}'
             has been replayed`);
          }
        } catch (err) {
          {
            logger_mjs.logger.log(`Request for '${getFriendlyURL_mjs.getFriendlyURL(storableRequest.url)}'
             failed to replay`);
          }
          replay.error = err;
          failedRequests.push(storableRequestClone);
        }

        replayedRequests.push(replay);
      }

      yield _this2._runCallback('queueDidReplay', replayedRequests);

      // If any requests failed, put the failed requests back in the queue
      // and rethrow the failed requests count.
      if (failedRequests.length) {
        yield Promise.all(failedRequests.map(function (storableRequest) {
          return _this2._queueStore.addEntry(storableRequest);
        }));

        throw new WorkboxError_mjs.WorkboxError('queue-replay-failed', { name: _this2._name, count: failedRequests.length });
      }
    })();
  }

  /**
   * Runs the passed callback if it exists.
   *
   * @private
   * @param {string} name The name of the callback on this._callbacks.
   * @param {...*} args The arguments to invoke the callback with.
   */
  _runCallback(name, ...args) {
    var _this3 = this;

    return babelHelpers.asyncToGenerator(function* () {
      if (typeof _this3._callbacks[name] === 'function') {
        yield _this3._callbacks[name].apply(null, args);
      }
    })();
  }

  /**
   * In sync-supporting browsers, this adds a listener for the sync event.
   * In non-sync-supporting browsers, this will retry the queue on service
   * worker startup.
   *
   * @private
   */
  _addSyncListener() {
    if ('sync' in registration) {
      self.addEventListener('sync', event => {
        if (event.tag === `${TAG_PREFIX}:${this._name}`) {
          {
            logger_mjs.logger.log(`Background sync for tag '${event.tag}'
                has been received, starting replay now`);
          }
          event.waitUntil(this.replayRequests());
        }
      });
    } else {
      {
        logger_mjs.logger.log(`Background sync replaying without background sync event`);
      }
      // If the browser doesn't support background sync, retry
      // every time the service worker starts up as a fallback.
      this.replayRequests();
    }
  }

  /**
   * Registers a sync event with a tag unique to this instance.
   *
   * @private
   */
  _registerSync() {
    var _this4 = this;

    return babelHelpers.asyncToGenerator(function* () {
      if ('sync' in registration) {
        try {
          yield registration.sync.register(`${TAG_PREFIX}:${_this4._name}`);
        } catch (err) {
          // This means the registration failed for some reason, possibly due to
          // the user disabling it.
          {
            logger_mjs.logger.warn(`Unable to register sync event for '${_this4._name}'.`, err);
          }
        }
      }
    })();
  }

  /**
   * Returns the set of queue names. This is primarily used to reset the list
   * of queue names in tests.
   *
   * @return {Set}
   *
   * @private
   */
  static get _queueNames() {
    return queueNames;
  }
}

/*
 Copyright 2017 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/**
 * A class implementing the `fetchDidFail` lifecycle callback. This makes it
 * easier to add failed requests to a background sync Queue.
 *
 * @memberof workbox.backgroundSync
 */
class Plugin {
  /**
   * @param {...*} queueArgs Args to forward to the composed Queue instance.
   *    See the [Queue]{@link workbox.backgroundSync.Queue} documentation for
   *    parameter details.
   */
  constructor(...queueArgs) {
    this._queue = new Queue(...queueArgs);
    this.fetchDidFail = this.fetchDidFail.bind(this);
  }

  /**
   * @param {Object} options
   * @param {Request} options.request
   * @private
   */
  fetchDidFail({ request }) {
    var _this = this;

    return babelHelpers.asyncToGenerator(function* () {
      yield _this._queue.addRequest(request);
    })();
  }
}

/*
 Copyright 2017 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/


var publicAPI = Object.freeze({
	Queue: Queue,
	Plugin: Plugin
});

/*
 Copyright 2017 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

return publicAPI;

}(workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private));

//# sourceMappingURL=workbox-background-sync.dev.js.map