Socket
Socket
Sign inDemoInstall

workbox-background-sync

Package Overview
Dependencies
Maintainers
4
Versions
97
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workbox-background-sync - npm Package Compare versions

Comparing version 3.2.0 to 3.3.0

README.md

973

build/workbox-background-sync.dev.js
this.workbox = this.workbox || {};
this.workbox.backgroundSync = (function (DBWrapper_mjs,WorkboxError_mjs,logger_mjs,assert_mjs,getFriendlyURL_mjs) {
'use strict';
'use strict';
try {
self.workbox.v['workbox:background-sync:3.2.0'] = 1;
} catch (e) {} // eslint-disable-line
try {
self.workbox.v['workbox:background-sync:3.3.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
/*
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
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.
*/
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'];
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.
* A class to make it easier to serialize and de-serialize requests so they
* can be stored in IndexedDB.
*
* @param {Request} request
* @return {Promise<StorableRequest>}
*
* @private
*/
static fromRequest(request) {
return babelHelpers.asyncToGenerator(function* () {
const requestInit = { headers: {} };
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();
}
// 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;
}
// 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];
// 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 });
})();
}
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;
/**
* 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;
}
// "Private"
this._timestamp = timestamp;
}
/**
* Gets the private _timestamp property.
*
* @return {number}
*
* @private
*/
get timestamp() {
return this._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
};
}
/**
* 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);
}
/**
* 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();
/**
* 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
});
}
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
/*
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
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.
*/
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
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
/*
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
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.
*/
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.
* A class to manage storing requests from a Queue in IndexedbDB,
* indexed by their queue name for easier access.
*
* @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 })
});
}
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;
/**
* 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()
});
})();
}
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;
/**
* 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
});
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);
}
})();
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
/*
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
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.
*/
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();
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
* 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.
*
* @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.
* @memberof workbox.backgroundSync
*/
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);
}
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._name = name;
this._callbacks = callbacks;
this._maxRetentionTime = maxRetentionTime;
this._queueStore = new QueueStore(this);
this._addSyncListener();
}
this._addSyncListener();
}
/**
* @return {string}
*/
get name() {
return this._name;
}
/**
* @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;
/**
* 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'
});
}
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
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;
/**
* 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 = [];
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();
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;
}
// Ignore requests older than maxRetentionTime.
const maxRetentionTimeInMs = _this2._maxRetentionTime * 60 * 1000;
if (now - storableRequest.timestamp > maxRetentionTimeInMs) {
continue;
}
yield _this2._runCallback('requestWillReplay', storableRequest);
yield _this2._runCallback('requestWillReplay', storableRequest);
const replay = { request: storableRequest.toRequest() };
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)}'
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)}'
}
} catch (err) {
{
logger_mjs.logger.log(`Request for '${getFriendlyURL_mjs.getFriendlyURL(storableRequest.url)}'
failed to replay`);
}
replay.error = err;
failedRequests.push(storableRequestClone);
}
replay.error = err;
failedRequests.push(storableRequestClone);
replayedRequests.push(replay);
}
replayedRequests.push(replay);
}
yield _this2._runCallback('queueDidReplay', replayedRequests);
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);
}));
// 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 });
}
})();
}
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;
/**
* 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);
}
})();
}
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}'
/**
* 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());
}
event.waitUntil(this.replayRequests());
});
} else {
{
logger_mjs.logger.log(`Background sync replaying without background sync event`);
}
});
} 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();
}
// 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;
/**
* 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);
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;
/**
* 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
/*
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
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.
*/
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.
* A class implementing the `fetchDidFail` lifecycle callback. This makes it
* easier to add failed requests to a background sync Queue.
*
* @memberof workbox.backgroundSync
*/
constructor(...queueArgs) {
this._queue = new Queue(...queueArgs);
this.fetchDidFail = this.fetchDidFail.bind(this);
}
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;
/**
* @param {Object} options
* @param {Request} options.request
* @private
*/
fetchDidFail({ request }) {
var _this = this;
return babelHelpers.asyncToGenerator(function* () {
yield _this._queue.addRequest(request);
})();
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
/*
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
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.
*/
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 = /*#__PURE__*/Object.freeze({
Queue: Queue,
Plugin: Plugin
});
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
/*
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
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.
*/
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;
return publicAPI;
}(workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private));
//# sourceMappingURL=workbox-background-sync.dev.js.map

@@ -1,3 +0,3 @@

this.workbox=this.workbox||{},this.workbox.backgroundSync=function(e,t){"use strict";try{self.workbox.v["workbox:background-sync:3.2.0"]=1}catch(e){}const r=["method","referrer","referrerPolicy","mode","credentials","cache","redirect","integrity","keepalive"];class s{static fromRequest(e){return babelHelpers.asyncToGenerator(function*(){const t={headers:{}};"GET"!==e.method&&(t.body=yield e.clone().blob());for(const[r,s]of e.headers.entries())t.headers[r]=s;for(const s of r)void 0!==e[s]&&(t[s]=e[s]);return new s({url:e.url,requestInit:t})})()}constructor({url:e,requestInit:t,timestamp:r=Date.now()}){this.url=e,this.requestInit=t,this.e=r}get timestamp(){return this.e}toObject(){return{url:this.url,timestamp:this.timestamp,requestInit:this.requestInit}}toRequest(){return new Request(this.url,this.requestInit)}clone(){const e=Object.assign({},this.requestInit);return e.headers=Object.assign({},this.requestInit.headers),this.requestInit.body&&(e.body=this.requestInit.body.slice()),new s({url:this.url,timestamp:this.timestamp,requestInit:e})}}const i="workbox-background-sync",n="requests",u="queueName",c="workbox-background-sync",o=10080;class l{constructor(t){this.t=t,this.r=new e.DBWrapper(i,1,{onupgradeneeded:e=>e.target.result.createObjectStore(n,{autoIncrement:!0}).createIndex(u,u,{unique:!1})})}addEntry(e){var t=this;return babelHelpers.asyncToGenerator(function*(){yield t.r.add(n,{queueName:t.t.name,storableRequest:e.toObject()})})()}getAndRemoveOldestEntry(){var e=this;return babelHelpers.asyncToGenerator(function*(){const[t]=yield e.r.getAllMatching(n,{index:u,query:IDBKeyRange.only(e.t.name),count:1,includeKeys:!0});if(t)return yield e.r.delete(n,t.primaryKey),new s(t.value.storableRequest)})()}}const a=new Set;class h{constructor(e,{callbacks:r={},maxRetentionTime:s=o}={}){if(a.has(e))throw new t.WorkboxError("duplicate-queue-name",{name:e});a.add(e),this.s=e,this.i=r,this.n=s,this.u=new l(this),this.c()}get name(){return this.s}addRequest(e){var t=this;return babelHelpers.asyncToGenerator(function*(){const r=yield s.fromRequest(e.clone());yield t.o("requestWillEnqueue",r),yield t.u.addEntry(r),yield t.l()})()}replayRequests(){var e=this;return babelHelpers.asyncToGenerator(function*(){const r=Date.now(),s=[],i=[];let n;for(;n=yield e.u.getAndRemoveOldestEntry();){const t=n.clone(),u=60*e.n*1e3;if(r-n.timestamp>u)continue;yield e.o("requestWillReplay",n);const c={request:n.toRequest()};try{c.response=yield fetch(c.request.clone())}catch(e){c.error=e,i.push(t)}s.push(c)}if(yield e.o("queueDidReplay",s),i.length)throw yield Promise.all(i.map(function(t){return e.u.addEntry(t)})),new t.WorkboxError("queue-replay-failed",{name:e.s,count:i.length})})()}o(e,...t){var r=this;return babelHelpers.asyncToGenerator(function*(){"function"==typeof r.i[e]&&(yield r.i[e].apply(null,t))})()}c(){"sync"in registration?self.addEventListener("sync",e=>{e.tag===`${c}:${this.s}`&&e.waitUntil(this.replayRequests())}):this.replayRequests()}l(){var e=this;return babelHelpers.asyncToGenerator(function*(){if("sync"in registration)try{yield registration.sync.register(`${c}:${e.s}`)}catch(e){}})()}static get a(){return a}}return Object.freeze({Queue:h,Plugin:class{constructor(...e){this.t=new h(...e),this.fetchDidFail=this.fetchDidFail.bind(this)}fetchDidFail({request:e}){var t=this;return babelHelpers.asyncToGenerator(function*(){yield t.t.addRequest(e)})()}}})}(workbox.core._private,workbox.core._private);
this.workbox=this.workbox||{},this.workbox.backgroundSync=function(e,t){"use strict";try{self.workbox.v["workbox:background-sync:3.3.0"]=1}catch(e){}const r=["method","referrer","referrerPolicy","mode","credentials","cache","redirect","integrity","keepalive"];class s{static fromRequest(e){return babelHelpers.asyncToGenerator(function*(){const t={headers:{}};"GET"!==e.method&&(t.body=yield e.clone().blob());for(const[r,s]of e.headers.entries())t.headers[r]=s;for(const s of r)void 0!==e[s]&&(t[s]=e[s]);return new s({url:e.url,requestInit:t})})()}constructor({url:e,requestInit:t,timestamp:r=Date.now()}){this.url=e,this.requestInit=t,this.e=r}get timestamp(){return this.e}toObject(){return{url:this.url,timestamp:this.timestamp,requestInit:this.requestInit}}toRequest(){return new Request(this.url,this.requestInit)}clone(){const e=Object.assign({},this.requestInit);return e.headers=Object.assign({},this.requestInit.headers),this.requestInit.body&&(e.body=this.requestInit.body.slice()),new s({url:this.url,timestamp:this.timestamp,requestInit:e})}}const i="workbox-background-sync",n="requests",u="queueName",c="workbox-background-sync",o=10080;class l{constructor(t){this.t=t,this.r=new e.DBWrapper(i,1,{onupgradeneeded:e=>e.target.result.createObjectStore(n,{autoIncrement:!0}).createIndex(u,u,{unique:!1})})}addEntry(e){var t=this;return babelHelpers.asyncToGenerator(function*(){yield t.r.add(n,{queueName:t.t.name,storableRequest:e.toObject()})})()}getAndRemoveOldestEntry(){var e=this;return babelHelpers.asyncToGenerator(function*(){const[t]=yield e.r.getAllMatching(n,{index:u,query:IDBKeyRange.only(e.t.name),count:1,includeKeys:!0});if(t)return yield e.r.delete(n,t.primaryKey),new s(t.value.storableRequest)})()}}const a=new Set;class h{constructor(e,{callbacks:r={},maxRetentionTime:s=o}={}){if(a.has(e))throw new t.WorkboxError("duplicate-queue-name",{name:e});a.add(e),this.s=e,this.i=r,this.n=s,this.u=new l(this),this.c()}get name(){return this.s}addRequest(e){var t=this;return babelHelpers.asyncToGenerator(function*(){const r=yield s.fromRequest(e.clone());yield t.o("requestWillEnqueue",r),yield t.u.addEntry(r),yield t.l()})()}replayRequests(){var e=this;return babelHelpers.asyncToGenerator(function*(){const r=Date.now(),s=[],i=[];let n;for(;n=yield e.u.getAndRemoveOldestEntry();){const t=n.clone(),u=60*e.n*1e3;if(r-n.timestamp>u)continue;yield e.o("requestWillReplay",n);const c={request:n.toRequest()};try{c.response=yield fetch(c.request.clone())}catch(e){c.error=e,i.push(t)}s.push(c)}if(yield e.o("queueDidReplay",s),i.length)throw yield Promise.all(i.map(function(t){return e.u.addEntry(t)})),new t.WorkboxError("queue-replay-failed",{name:e.s,count:i.length})})()}o(e,...t){var r=this;return babelHelpers.asyncToGenerator(function*(){"function"==typeof r.i[e]&&(yield r.i[e].apply(null,t))})()}c(){"sync"in registration?self.addEventListener("sync",e=>{e.tag===`${c}:${this.s}`&&e.waitUntil(this.replayRequests())}):this.replayRequests()}l(){var e=this;return babelHelpers.asyncToGenerator(function*(){if("sync"in registration)try{yield registration.sync.register(`${c}:${e.s}`)}catch(e){}})()}static get a(){return a}}return Object.freeze({Queue:h,Plugin:class{constructor(...e){this.t=new h(...e),this.fetchDidFail=this.fetchDidFail.bind(this)}fetchDidFail({request:e}){var t=this;return babelHelpers.asyncToGenerator(function*(){yield t.t.addRequest(e)})()}}})}(workbox.core._private,workbox.core._private);
//# sourceMappingURL=workbox-background-sync.prod.js.map
{
"name": "workbox-background-sync",
"version": "3.2.0",
"version": "3.3.0",
"license": "Apache-2.0",

@@ -30,4 +30,4 @@ "author": "Google's Web DevRel Team",

"dependencies": {
"workbox-core": "^3.2.0"
"workbox-core": "^3.3.0"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc