This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import { WorkboxPlugin } from 'workbox-core/types.js';
import { QueueOptions } from './Queue.js';
import './_version.js';
/**
* A class implementing the `fetchDidFail` lifecycle callback. This makes it
* easier to add failed requests to a background sync Queue.
*
* @memberof workbox-background-sync
*/
declare class BackgroundSyncPlugin implements WorkboxPlugin {
private readonly _queue;
/**
* @param {string} name See the {@link workbox-background-sync.Queue}
* documentation for parameter details.
* @param {Object} [options] See the
* {@link workbox-background-sync.Queue} documentation for
* parameter details.
*/
constructor(name: string, options?: QueueOptions);
/**
* @param {Object} options
* @param {Request} options.request
* @private
*/
fetchDidFail: WorkboxPlugin['fetchDidFail'];
}
export { BackgroundSyncPlugin };

View File

@@ -0,0 +1,36 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { Queue } from './Queue.js';
import './_version.js';
/**
* A class implementing the `fetchDidFail` lifecycle callback. This makes it
* easier to add failed requests to a background sync Queue.
*
* @memberof workbox-background-sync
*/
class BackgroundSyncPlugin {
/**
* @param {string} name See the {@link workbox-background-sync.Queue}
* documentation for parameter details.
* @param {Object} [options] See the
* {@link workbox-background-sync.Queue} documentation for
* parameter details.
*/
constructor(name, options) {
/**
* @param {Object} options
* @param {Request} options.request
* @private
*/
this.fetchDidFail = async ({ request }) => {
await this._queue.pushRequest({ request });
};
this._queue = new Queue(name, options);
}
}
export { BackgroundSyncPlugin };

View File

@@ -0,0 +1 @@
export * from './BackgroundSyncPlugin.js';

19
frontend/node_modules/workbox-background-sync/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,19 @@
Copyright 2018 Google LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,175 @@
import './_version.js';
interface OnSyncCallbackOptions {
queue: Queue;
}
interface OnSyncCallback {
(options: OnSyncCallbackOptions): void | Promise<void>;
}
export interface QueueOptions {
forceSyncFallback?: boolean;
maxRetentionTime?: number;
onSync?: OnSyncCallback;
}
interface QueueEntry {
request: Request;
timestamp?: number;
metadata?: object;
}
/**
* 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-background-sync
*/
declare class Queue {
private readonly _name;
private readonly _onSync;
private readonly _maxRetentionTime;
private readonly _queueStore;
private readonly _forceSyncFallback;
private _syncInProgress;
private _requestsAddedDuringSync;
/**
* 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 {Function} [options.onSync] A function that gets invoked whenever
* the 'sync' event fires. The function is invoked with an object
* containing the `queue` property (referencing this instance), and you
* can use the callback to customize the replay behavior of the queue.
* When not set the `replayRequests()` method is called.
* Note: if the replay fails after a sync event, make sure you throw an
* error, so the browser knows to retry the sync event later.
* @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.
* @param {boolean} [options.forceSyncFallback=false] If `true`, instead
* of attempting to use background sync events, always attempt to replay
* queued request at service worker startup. Most folks will not need
* this, unless you explicitly target a runtime like Electron that
* exposes the interfaces for background sync, but does not have a working
* implementation.
*/
constructor(name: string, { forceSyncFallback, onSync, maxRetentionTime }?: QueueOptions);
/**
* @return {string}
*/
get name(): string;
/**
* Stores the passed request in IndexedDB (with its timestamp and any
* metadata) at the end of the queue.
*
* @param {QueueEntry} entry
* @param {Request} entry.request The request to store in the queue.
* @param {Object} [entry.metadata] Any metadata you want associated with the
* stored request. When requests are replayed you'll have access to this
* metadata object in case you need to modify the request beforehand.
* @param {number} [entry.timestamp] The timestamp (Epoch time in
* milliseconds) when the request was first added to the queue. This is
* used along with `maxRetentionTime` to remove outdated requests. In
* general you don't need to set this value, as it's automatically set
* for you (defaulting to `Date.now()`), but you can update it if you
* don't want particular requests to expire.
*/
pushRequest(entry: QueueEntry): Promise<void>;
/**
* Stores the passed request in IndexedDB (with its timestamp and any
* metadata) at the beginning of the queue.
*
* @param {QueueEntry} entry
* @param {Request} entry.request The request to store in the queue.
* @param {Object} [entry.metadata] Any metadata you want associated with the
* stored request. When requests are replayed you'll have access to this
* metadata object in case you need to modify the request beforehand.
* @param {number} [entry.timestamp] The timestamp (Epoch time in
* milliseconds) when the request was first added to the queue. This is
* used along with `maxRetentionTime` to remove outdated requests. In
* general you don't need to set this value, as it's automatically set
* for you (defaulting to `Date.now()`), but you can update it if you
* don't want particular requests to expire.
*/
unshiftRequest(entry: QueueEntry): Promise<void>;
/**
* Removes and returns the last request in the queue (along with its
* timestamp and any metadata). The returned object takes the form:
* `{request, timestamp, metadata}`.
*
* @return {Promise<QueueEntry | undefined>}
*/
popRequest(): Promise<QueueEntry | undefined>;
/**
* Removes and returns the first request in the queue (along with its
* timestamp and any metadata). The returned object takes the form:
* `{request, timestamp, metadata}`.
*
* @return {Promise<QueueEntry | undefined>}
*/
shiftRequest(): Promise<QueueEntry | undefined>;
/**
* Returns all the entries that have not expired (per `maxRetentionTime`).
* Any expired entries are removed from the queue.
*
* @return {Promise<Array<QueueEntry>>}
*/
getAll(): Promise<Array<QueueEntry>>;
/**
* Returns the number of entries present in the queue.
* Note that expired entries (per `maxRetentionTime`) are also included in this count.
*
* @return {Promise<number>}
*/
size(): Promise<number>;
/**
* Adds the entry to the QueueStore and registers for a sync event.
*
* @param {Object} entry
* @param {Request} entry.request
* @param {Object} [entry.metadata]
* @param {number} [entry.timestamp=Date.now()]
* @param {string} operation ('push' or 'unshift')
* @private
*/
_addRequest({ request, metadata, timestamp }: QueueEntry, operation: 'push' | 'unshift'): Promise<void>;
/**
* Removes and returns the first or last (depending on `operation`) entry
* from the QueueStore that's not older than the `maxRetentionTime`.
*
* @param {string} operation ('pop' or 'shift')
* @return {Object|undefined}
* @private
*/
_removeRequest(operation: 'pop' | 'shift'): Promise<QueueEntry | undefined>;
/**
* Loops through each request in the queue and attempts to re-fetch it.
* If any request fails to re-fetch, it's put back in the same position in
* the queue (which registers a retry for the next sync event).
*/
replayRequests(): Promise<void>;
/**
* Registers a sync event with a tag unique to this instance.
*/
registerSync(): Promise<void>;
/**
* In sync-supporting browsers, this adds a listener for the sync event.
* In non-sync-supporting browsers, or if _forceSyncFallback is true, this
* will retry the queue on service worker startup.
*
* @private
*/
private _addSyncListener;
/**
* Returns the set of queue names. This is primarily used to reset the list
* of queue names in tests.
*
* @return {Set<string>}
*
* @private
*/
static get _queueNames(): Set<string>;
}
export { Queue };

401
frontend/node_modules/workbox-background-sync/Queue.js generated vendored Normal file
View File

@@ -0,0 +1,401 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
import { logger } from 'workbox-core/_private/logger.js';
import { assert } from 'workbox-core/_private/assert.js';
import { getFriendlyURL } from 'workbox-core/_private/getFriendlyURL.js';
import { QueueStore } from './lib/QueueStore.js';
import { StorableRequest } from './lib/StorableRequest.js';
import './_version.js';
const TAG_PREFIX = 'workbox-background-sync';
const MAX_RETENTION_TIME = 60 * 24 * 7; // 7 days in minutes
const queueNames = new Set();
/**
* Converts a QueueStore entry into the format exposed by Queue. This entails
* converting the request data into a real request and omitting the `id` and
* `queueName` properties.
*
* @param {UnidentifiedQueueStoreEntry} queueStoreEntry
* @return {Queue}
* @private
*/
const convertEntry = (queueStoreEntry) => {
const queueEntry = {
request: new StorableRequest(queueStoreEntry.requestData).toRequest(),
timestamp: queueStoreEntry.timestamp,
};
if (queueStoreEntry.metadata) {
queueEntry.metadata = queueStoreEntry.metadata;
}
return queueEntry;
};
/**
* 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-background-sync
*/
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 {Function} [options.onSync] A function that gets invoked whenever
* the 'sync' event fires. The function is invoked with an object
* containing the `queue` property (referencing this instance), and you
* can use the callback to customize the replay behavior of the queue.
* When not set the `replayRequests()` method is called.
* Note: if the replay fails after a sync event, make sure you throw an
* error, so the browser knows to retry the sync event later.
* @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.
* @param {boolean} [options.forceSyncFallback=false] If `true`, instead
* of attempting to use background sync events, always attempt to replay
* queued request at service worker startup. Most folks will not need
* this, unless you explicitly target a runtime like Electron that
* exposes the interfaces for background sync, but does not have a working
* implementation.
*/
constructor(name, { forceSyncFallback, onSync, maxRetentionTime } = {}) {
this._syncInProgress = false;
this._requestsAddedDuringSync = false;
// Ensure the store name is not already being used
if (queueNames.has(name)) {
throw new WorkboxError('duplicate-queue-name', { name });
}
else {
queueNames.add(name);
}
this._name = name;
this._onSync = onSync || this.replayRequests;
this._maxRetentionTime = maxRetentionTime || MAX_RETENTION_TIME;
this._forceSyncFallback = Boolean(forceSyncFallback);
this._queueStore = new QueueStore(this._name);
this._addSyncListener();
}
/**
* @return {string}
*/
get name() {
return this._name;
}
/**
* Stores the passed request in IndexedDB (with its timestamp and any
* metadata) at the end of the queue.
*
* @param {QueueEntry} entry
* @param {Request} entry.request The request to store in the queue.
* @param {Object} [entry.metadata] Any metadata you want associated with the
* stored request. When requests are replayed you'll have access to this
* metadata object in case you need to modify the request beforehand.
* @param {number} [entry.timestamp] The timestamp (Epoch time in
* milliseconds) when the request was first added to the queue. This is
* used along with `maxRetentionTime` to remove outdated requests. In
* general you don't need to set this value, as it's automatically set
* for you (defaulting to `Date.now()`), but you can update it if you
* don't want particular requests to expire.
*/
async pushRequest(entry) {
if (process.env.NODE_ENV !== 'production') {
assert.isType(entry, 'object', {
moduleName: 'workbox-background-sync',
className: 'Queue',
funcName: 'pushRequest',
paramName: 'entry',
});
assert.isInstance(entry.request, Request, {
moduleName: 'workbox-background-sync',
className: 'Queue',
funcName: 'pushRequest',
paramName: 'entry.request',
});
}
await this._addRequest(entry, 'push');
}
/**
* Stores the passed request in IndexedDB (with its timestamp and any
* metadata) at the beginning of the queue.
*
* @param {QueueEntry} entry
* @param {Request} entry.request The request to store in the queue.
* @param {Object} [entry.metadata] Any metadata you want associated with the
* stored request. When requests are replayed you'll have access to this
* metadata object in case you need to modify the request beforehand.
* @param {number} [entry.timestamp] The timestamp (Epoch time in
* milliseconds) when the request was first added to the queue. This is
* used along with `maxRetentionTime` to remove outdated requests. In
* general you don't need to set this value, as it's automatically set
* for you (defaulting to `Date.now()`), but you can update it if you
* don't want particular requests to expire.
*/
async unshiftRequest(entry) {
if (process.env.NODE_ENV !== 'production') {
assert.isType(entry, 'object', {
moduleName: 'workbox-background-sync',
className: 'Queue',
funcName: 'unshiftRequest',
paramName: 'entry',
});
assert.isInstance(entry.request, Request, {
moduleName: 'workbox-background-sync',
className: 'Queue',
funcName: 'unshiftRequest',
paramName: 'entry.request',
});
}
await this._addRequest(entry, 'unshift');
}
/**
* Removes and returns the last request in the queue (along with its
* timestamp and any metadata). The returned object takes the form:
* `{request, timestamp, metadata}`.
*
* @return {Promise<QueueEntry | undefined>}
*/
async popRequest() {
return this._removeRequest('pop');
}
/**
* Removes and returns the first request in the queue (along with its
* timestamp and any metadata). The returned object takes the form:
* `{request, timestamp, metadata}`.
*
* @return {Promise<QueueEntry | undefined>}
*/
async shiftRequest() {
return this._removeRequest('shift');
}
/**
* Returns all the entries that have not expired (per `maxRetentionTime`).
* Any expired entries are removed from the queue.
*
* @return {Promise<Array<QueueEntry>>}
*/
async getAll() {
const allEntries = await this._queueStore.getAll();
const now = Date.now();
const unexpiredEntries = [];
for (const entry of allEntries) {
// Ignore requests older than maxRetentionTime. Call this function
// recursively until an unexpired request is found.
const maxRetentionTimeInMs = this._maxRetentionTime * 60 * 1000;
if (now - entry.timestamp > maxRetentionTimeInMs) {
await this._queueStore.deleteEntry(entry.id);
}
else {
unexpiredEntries.push(convertEntry(entry));
}
}
return unexpiredEntries;
}
/**
* Returns the number of entries present in the queue.
* Note that expired entries (per `maxRetentionTime`) are also included in this count.
*
* @return {Promise<number>}
*/
async size() {
return await this._queueStore.size();
}
/**
* Adds the entry to the QueueStore and registers for a sync event.
*
* @param {Object} entry
* @param {Request} entry.request
* @param {Object} [entry.metadata]
* @param {number} [entry.timestamp=Date.now()]
* @param {string} operation ('push' or 'unshift')
* @private
*/
async _addRequest({ request, metadata, timestamp = Date.now() }, operation) {
const storableRequest = await StorableRequest.fromRequest(request.clone());
const entry = {
requestData: storableRequest.toObject(),
timestamp,
};
// Only include metadata if it's present.
if (metadata) {
entry.metadata = metadata;
}
switch (operation) {
case 'push':
await this._queueStore.pushEntry(entry);
break;
case 'unshift':
await this._queueStore.unshiftEntry(entry);
break;
}
if (process.env.NODE_ENV !== 'production') {
logger.log(`Request for '${getFriendlyURL(request.url)}' has ` +
`been added to background sync queue '${this._name}'.`);
}
// Don't register for a sync if we're in the middle of a sync. Instead,
// we wait until the sync is complete and call register if
// `this._requestsAddedDuringSync` is true.
if (this._syncInProgress) {
this._requestsAddedDuringSync = true;
}
else {
await this.registerSync();
}
}
/**
* Removes and returns the first or last (depending on `operation`) entry
* from the QueueStore that's not older than the `maxRetentionTime`.
*
* @param {string} operation ('pop' or 'shift')
* @return {Object|undefined}
* @private
*/
async _removeRequest(operation) {
const now = Date.now();
let entry;
switch (operation) {
case 'pop':
entry = await this._queueStore.popEntry();
break;
case 'shift':
entry = await this._queueStore.shiftEntry();
break;
}
if (entry) {
// Ignore requests older than maxRetentionTime. Call this function
// recursively until an unexpired request is found.
const maxRetentionTimeInMs = this._maxRetentionTime * 60 * 1000;
if (now - entry.timestamp > maxRetentionTimeInMs) {
return this._removeRequest(operation);
}
return convertEntry(entry);
}
else {
return undefined;
}
}
/**
* Loops through each request in the queue and attempts to re-fetch it.
* If any request fails to re-fetch, it's put back in the same position in
* the queue (which registers a retry for the next sync event).
*/
async replayRequests() {
let entry;
while ((entry = await this.shiftRequest())) {
try {
await fetch(entry.request.clone());
if (process.env.NODE_ENV !== 'production') {
logger.log(`Request for '${getFriendlyURL(entry.request.url)}' ` +
`has been replayed in queue '${this._name}'`);
}
}
catch (error) {
await this.unshiftRequest(entry);
if (process.env.NODE_ENV !== 'production') {
logger.log(`Request for '${getFriendlyURL(entry.request.url)}' ` +
`failed to replay, putting it back in queue '${this._name}'`);
}
throw new WorkboxError('queue-replay-failed', { name: this._name });
}
}
if (process.env.NODE_ENV !== 'production') {
logger.log(`All requests in queue '${this.name}' have successfully ` +
`replayed; the queue is now empty!`);
}
}
/**
* Registers a sync event with a tag unique to this instance.
*/
async registerSync() {
// See https://github.com/GoogleChrome/workbox/issues/2393
if ('sync' in self.registration && !this._forceSyncFallback) {
try {
await self.registration.sync.register(`${TAG_PREFIX}:${this._name}`);
}
catch (err) {
// This means the registration failed for some reason, possibly due to
// the user disabling it.
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to register sync event for '${this._name}'.`, err);
}
}
}
}
/**
* In sync-supporting browsers, this adds a listener for the sync event.
* In non-sync-supporting browsers, or if _forceSyncFallback is true, this
* will retry the queue on service worker startup.
*
* @private
*/
_addSyncListener() {
// See https://github.com/GoogleChrome/workbox/issues/2393
if ('sync' in self.registration && !this._forceSyncFallback) {
self.addEventListener('sync', (event) => {
if (event.tag === `${TAG_PREFIX}:${this._name}`) {
if (process.env.NODE_ENV !== 'production') {
logger.log(`Background sync for tag '${event.tag}' ` + `has been received`);
}
const syncComplete = async () => {
this._syncInProgress = true;
let syncError;
try {
await this._onSync({ queue: this });
}
catch (error) {
if (error instanceof Error) {
syncError = error;
// Rethrow the error. Note: the logic in the finally clause
// will run before this gets rethrown.
throw syncError;
}
}
finally {
// New items may have been added to the queue during the sync,
// so we need to register for a new sync if that's happened...
// Unless there was an error during the sync, in which
// case the browser will automatically retry later, as long
// as `event.lastChance` is not true.
if (this._requestsAddedDuringSync &&
!(syncError && !event.lastChance)) {
await this.registerSync();
}
this._syncInProgress = false;
this._requestsAddedDuringSync = false;
}
};
event.waitUntil(syncComplete());
}
});
}
else {
if (process.env.NODE_ENV !== 'production') {
logger.log(`Background sync replaying without background sync event`);
}
// If the browser doesn't support background sync, or the developer has
// opted-in to not using it, retry every time the service worker starts up
// as a fallback.
void this._onSync({ queue: this });
}
}
/**
* Returns the set of queue names. This is primarily used to reset the list
* of queue names in tests.
*
* @return {Set<string>}
*
* @private
*/
static get _queueNames() {
return queueNames;
}
}
export { Queue };

View File

@@ -0,0 +1 @@
export * from './Queue.js';

View File

@@ -0,0 +1,2 @@
import './_version.js';
export { QueueStore } from './lib/QueueStore';

View File

@@ -0,0 +1,12 @@
/*
Copyright 2021 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import './_version.js';
// This is a temporary workaround to expose something from ./lib/ via our
// top-level public API.
// TODO: In Workbox v7, move the actual code from ./lib/ to this file.
export { QueueStore } from './lib/QueueStore';

View File

@@ -0,0 +1 @@
export * from './QueueStore.js';

View File

@@ -0,0 +1 @@
This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-background-sync

View File

@@ -0,0 +1,2 @@
import './_version.js';
export { StorableRequest } from './lib/StorableRequest';

View File

@@ -0,0 +1,12 @@
/*
Copyright 2021 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import './_version.js';
// This is a temporary workaround to expose something from ./lib/ via our
// top-level public API.
// TODO: In Workbox v7, move the actual code from ./lib/ to this file.
export { StorableRequest } from './lib/StorableRequest';

View File

@@ -0,0 +1 @@
export * from './StorableRequest.js';

View File

View File

@@ -0,0 +1,6 @@
"use strict";
// @ts-ignore
try {
self['workbox:background-sync:6.5.4'] && _();
}
catch (e) { }

View File

@@ -0,0 +1 @@
try{self['workbox:background-sync:6.6.0']&&_()}catch(e){}// eslint-disable-line

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,25 @@
import { BackgroundSyncPlugin } from './BackgroundSyncPlugin.js';
import { Queue, QueueOptions } from './Queue.js';
import { QueueStore } from './QueueStore.js';
import { StorableRequest } from './StorableRequest.js';
import './_version.js';
interface SyncManager {
getTags(): Promise<string[]>;
register(tag: string): Promise<void>;
}
declare global {
interface ServiceWorkerRegistration {
readonly sync: SyncManager;
}
interface SyncEvent extends ExtendableEvent {
readonly lastChance: boolean;
readonly tag: string;
}
interface ServiceWorkerGlobalScopeEventMap {
sync: SyncEvent;
}
}
/**
* @module workbox-background-sync
*/
export { BackgroundSyncPlugin, Queue, QueueOptions, QueueStore, StorableRequest };

16
frontend/node_modules/workbox-background-sync/index.js generated vendored Normal file
View File

@@ -0,0 +1,16 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { BackgroundSyncPlugin } from './BackgroundSyncPlugin.js';
import { Queue } from './Queue.js';
import { QueueStore } from './QueueStore.js';
import { StorableRequest } from './StorableRequest.js';
import './_version.js';
/**
* @module workbox-background-sync
*/
export { BackgroundSyncPlugin, Queue, QueueStore, StorableRequest };

View File

@@ -0,0 +1 @@
export * from './index.js';

View File

@@ -0,0 +1,90 @@
import { RequestData } from './StorableRequest.js';
import '../_version.js';
export interface UnidentifiedQueueStoreEntry {
requestData: RequestData;
timestamp: number;
id?: number;
queueName?: string;
metadata?: object;
}
export interface QueueStoreEntry extends UnidentifiedQueueStoreEntry {
id: number;
}
/**
* A class to interact directly an IndexedDB created specifically to save and
* retrieve QueueStoreEntries. This class encapsulates all the schema details
* to store the representation of a Queue.
*
* @private
*/
export declare class QueueDb {
private _db;
/**
* Add QueueStoreEntry to underlying db.
*
* @param {UnidentifiedQueueStoreEntry} entry
*/
addEntry(entry: UnidentifiedQueueStoreEntry): Promise<void>;
/**
* Returns the first entry id in the ObjectStore.
*
* @return {number | undefined}
*/
getFirstEntryId(): Promise<number | undefined>;
/**
* Get all the entries filtered by index
*
* @param queueName
* @return {Promise<QueueStoreEntry[]>}
*/
getAllEntriesByQueueName(queueName: string): Promise<QueueStoreEntry[]>;
/**
* Returns the number of entries filtered by index
*
* @param queueName
* @return {Promise<number>}
*/
getEntryCountByQueueName(queueName: string): Promise<number>;
/**
* Deletes a single entry by id.
*
* @param {number} id the id of the entry to be deleted
*/
deleteEntry(id: number): Promise<void>;
/**
*
* @param queueName
* @returns {Promise<QueueStoreEntry | undefined>}
*/
getFirstEntryByQueueName(queueName: string): Promise<QueueStoreEntry | undefined>;
/**
*
* @param queueName
* @returns {Promise<QueueStoreEntry | undefined>}
*/
getLastEntryByQueueName(queueName: string): Promise<QueueStoreEntry | undefined>;
/**
* Returns either the first or the last entries, depending on direction.
* Filtered by index.
*
* @param {IDBCursorDirection} direction
* @param {IDBKeyRange} query
* @return {Promise<QueueStoreEntry | undefined>}
* @private
*/
getEndEntryFromIndex(query: IDBKeyRange, direction: IDBCursorDirection): Promise<QueueStoreEntry | undefined>;
/**
* Returns an open connection to the database.
*
* @private
*/
private getDb;
/**
* Upgrades QueueDB
*
* @param {IDBPDatabase<QueueDBSchema>} db
* @param {number} oldVersion
* @private
*/
private _upgradeDb;
}

View File

@@ -0,0 +1,145 @@
/*
Copyright 2021 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { openDB } from 'idb';
import '../_version.js';
const DB_VERSION = 3;
const DB_NAME = 'workbox-background-sync';
const REQUEST_OBJECT_STORE_NAME = 'requests';
const QUEUE_NAME_INDEX = 'queueName';
/**
* A class to interact directly an IndexedDB created specifically to save and
* retrieve QueueStoreEntries. This class encapsulates all the schema details
* to store the representation of a Queue.
*
* @private
*/
export class QueueDb {
constructor() {
this._db = null;
}
/**
* Add QueueStoreEntry to underlying db.
*
* @param {UnidentifiedQueueStoreEntry} entry
*/
async addEntry(entry) {
const db = await this.getDb();
const tx = db.transaction(REQUEST_OBJECT_STORE_NAME, 'readwrite', {
durability: 'relaxed',
});
await tx.store.add(entry);
await tx.done;
}
/**
* Returns the first entry id in the ObjectStore.
*
* @return {number | undefined}
*/
async getFirstEntryId() {
const db = await this.getDb();
const cursor = await db
.transaction(REQUEST_OBJECT_STORE_NAME)
.store.openCursor();
return cursor === null || cursor === void 0 ? void 0 : cursor.value.id;
}
/**
* Get all the entries filtered by index
*
* @param queueName
* @return {Promise<QueueStoreEntry[]>}
*/
async getAllEntriesByQueueName(queueName) {
const db = await this.getDb();
const results = await db.getAllFromIndex(REQUEST_OBJECT_STORE_NAME, QUEUE_NAME_INDEX, IDBKeyRange.only(queueName));
return results ? results : new Array();
}
/**
* Returns the number of entries filtered by index
*
* @param queueName
* @return {Promise<number>}
*/
async getEntryCountByQueueName(queueName) {
const db = await this.getDb();
return db.countFromIndex(REQUEST_OBJECT_STORE_NAME, QUEUE_NAME_INDEX, IDBKeyRange.only(queueName));
}
/**
* Deletes a single entry by id.
*
* @param {number} id the id of the entry to be deleted
*/
async deleteEntry(id) {
const db = await this.getDb();
await db.delete(REQUEST_OBJECT_STORE_NAME, id);
}
/**
*
* @param queueName
* @returns {Promise<QueueStoreEntry | undefined>}
*/
async getFirstEntryByQueueName(queueName) {
return await this.getEndEntryFromIndex(IDBKeyRange.only(queueName), 'next');
}
/**
*
* @param queueName
* @returns {Promise<QueueStoreEntry | undefined>}
*/
async getLastEntryByQueueName(queueName) {
return await this.getEndEntryFromIndex(IDBKeyRange.only(queueName), 'prev');
}
/**
* Returns either the first or the last entries, depending on direction.
* Filtered by index.
*
* @param {IDBCursorDirection} direction
* @param {IDBKeyRange} query
* @return {Promise<QueueStoreEntry | undefined>}
* @private
*/
async getEndEntryFromIndex(query, direction) {
const db = await this.getDb();
const cursor = await db
.transaction(REQUEST_OBJECT_STORE_NAME)
.store.index(QUEUE_NAME_INDEX)
.openCursor(query, direction);
return cursor === null || cursor === void 0 ? void 0 : cursor.value;
}
/**
* Returns an open connection to the database.
*
* @private
*/
async getDb() {
if (!this._db) {
this._db = await openDB(DB_NAME, DB_VERSION, {
upgrade: this._upgradeDb,
});
}
return this._db;
}
/**
* Upgrades QueueDB
*
* @param {IDBPDatabase<QueueDBSchema>} db
* @param {number} oldVersion
* @private
*/
_upgradeDb(db, oldVersion) {
if (oldVersion > 0 && oldVersion < DB_VERSION) {
if (db.objectStoreNames.contains(REQUEST_OBJECT_STORE_NAME)) {
db.deleteObjectStore(REQUEST_OBJECT_STORE_NAME);
}
}
const objStore = db.createObjectStore(REQUEST_OBJECT_STORE_NAME, {
autoIncrement: true,
keyPath: 'id',
});
objStore.createIndex(QUEUE_NAME_INDEX, QUEUE_NAME_INDEX, { unique: false });
}
}

View File

@@ -0,0 +1 @@
export * from './QueueDb.js';

View File

@@ -0,0 +1,83 @@
import { UnidentifiedQueueStoreEntry, QueueStoreEntry } from './QueueDb.js';
import '../_version.js';
/**
* A class to manage storing requests from a Queue in IndexedDB,
* indexed by their queue name for easier access.
*
* Most developers will not need to access this class directly;
* it is exposed for advanced use cases.
*/
export declare class QueueStore {
private readonly _queueName;
private readonly _queueDb;
/**
* Associates this instance with a Queue instance, so entries added can be
* identified by their queue name.
*
* @param {string} queueName
*/
constructor(queueName: string);
/**
* Append an entry last in the queue.
*
* @param {Object} entry
* @param {Object} entry.requestData
* @param {number} [entry.timestamp]
* @param {Object} [entry.metadata]
*/
pushEntry(entry: UnidentifiedQueueStoreEntry): Promise<void>;
/**
* Prepend an entry first in the queue.
*
* @param {Object} entry
* @param {Object} entry.requestData
* @param {number} [entry.timestamp]
* @param {Object} [entry.metadata]
*/
unshiftEntry(entry: UnidentifiedQueueStoreEntry): Promise<void>;
/**
* Removes and returns the last entry in the queue matching the `queueName`.
*
* @return {Promise<QueueStoreEntry|undefined>}
*/
popEntry(): Promise<QueueStoreEntry | undefined>;
/**
* Removes and returns the first entry in the queue matching the `queueName`.
*
* @return {Promise<QueueStoreEntry|undefined>}
*/
shiftEntry(): Promise<QueueStoreEntry | undefined>;
/**
* Returns all entries in the store matching the `queueName`.
*
* @param {Object} options See {@link workbox-background-sync.Queue~getAll}
* @return {Promise<Array<Object>>}
*/
getAll(): Promise<QueueStoreEntry[]>;
/**
* Returns the number of entries in the store matching the `queueName`.
*
* @param {Object} options See {@link workbox-background-sync.Queue~size}
* @return {Promise<number>}
*/
size(): Promise<number>;
/**
* Deletes the entry for the given ID.
*
* WARNING: this method does not ensure the deleted entry belongs to this
* queue (i.e. matches the `queueName`). But this limitation is acceptable
* as this class is not publicly exposed. An additional check would make
* this method slower than it needs to be.
*
* @param {number} id
*/
deleteEntry(id: number): Promise<void>;
/**
* Removes and returns the first or last entry in the queue (based on the
* `direction` argument) matching the `queueName`.
*
* @return {Promise<QueueStoreEntry|undefined>}
* @private
*/
_removeEntry(entry?: QueueStoreEntry): Promise<QueueStoreEntry | undefined>;
}

View File

@@ -0,0 +1,152 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { assert } from 'workbox-core/_private/assert.js';
import { QueueDb, } from './QueueDb.js';
import '../_version.js';
/**
* A class to manage storing requests from a Queue in IndexedDB,
* indexed by their queue name for easier access.
*
* Most developers will not need to access this class directly;
* it is exposed for advanced use cases.
*/
export class QueueStore {
/**
* Associates this instance with a Queue instance, so entries added can be
* identified by their queue name.
*
* @param {string} queueName
*/
constructor(queueName) {
this._queueName = queueName;
this._queueDb = new QueueDb();
}
/**
* Append an entry last in the queue.
*
* @param {Object} entry
* @param {Object} entry.requestData
* @param {number} [entry.timestamp]
* @param {Object} [entry.metadata]
*/
async pushEntry(entry) {
if (process.env.NODE_ENV !== 'production') {
assert.isType(entry, 'object', {
moduleName: 'workbox-background-sync',
className: 'QueueStore',
funcName: 'pushEntry',
paramName: 'entry',
});
assert.isType(entry.requestData, 'object', {
moduleName: 'workbox-background-sync',
className: 'QueueStore',
funcName: 'pushEntry',
paramName: 'entry.requestData',
});
}
// Don't specify an ID since one is automatically generated.
delete entry.id;
entry.queueName = this._queueName;
await this._queueDb.addEntry(entry);
}
/**
* Prepend an entry first in the queue.
*
* @param {Object} entry
* @param {Object} entry.requestData
* @param {number} [entry.timestamp]
* @param {Object} [entry.metadata]
*/
async unshiftEntry(entry) {
if (process.env.NODE_ENV !== 'production') {
assert.isType(entry, 'object', {
moduleName: 'workbox-background-sync',
className: 'QueueStore',
funcName: 'unshiftEntry',
paramName: 'entry',
});
assert.isType(entry.requestData, 'object', {
moduleName: 'workbox-background-sync',
className: 'QueueStore',
funcName: 'unshiftEntry',
paramName: 'entry.requestData',
});
}
const firstId = await this._queueDb.getFirstEntryId();
if (firstId) {
// Pick an ID one less than the lowest ID in the object store.
entry.id = firstId - 1;
}
else {
// Otherwise let the auto-incrementor assign the ID.
delete entry.id;
}
entry.queueName = this._queueName;
await this._queueDb.addEntry(entry);
}
/**
* Removes and returns the last entry in the queue matching the `queueName`.
*
* @return {Promise<QueueStoreEntry|undefined>}
*/
async popEntry() {
return this._removeEntry(await this._queueDb.getLastEntryByQueueName(this._queueName));
}
/**
* Removes and returns the first entry in the queue matching the `queueName`.
*
* @return {Promise<QueueStoreEntry|undefined>}
*/
async shiftEntry() {
return this._removeEntry(await this._queueDb.getFirstEntryByQueueName(this._queueName));
}
/**
* Returns all entries in the store matching the `queueName`.
*
* @param {Object} options See {@link workbox-background-sync.Queue~getAll}
* @return {Promise<Array<Object>>}
*/
async getAll() {
return await this._queueDb.getAllEntriesByQueueName(this._queueName);
}
/**
* Returns the number of entries in the store matching the `queueName`.
*
* @param {Object} options See {@link workbox-background-sync.Queue~size}
* @return {Promise<number>}
*/
async size() {
return await this._queueDb.getEntryCountByQueueName(this._queueName);
}
/**
* Deletes the entry for the given ID.
*
* WARNING: this method does not ensure the deleted entry belongs to this
* queue (i.e. matches the `queueName`). But this limitation is acceptable
* as this class is not publicly exposed. An additional check would make
* this method slower than it needs to be.
*
* @param {number} id
*/
async deleteEntry(id) {
await this._queueDb.deleteEntry(id);
}
/**
* Removes and returns the first or last entry in the queue (based on the
* `direction` argument) matching the `queueName`.
*
* @return {Promise<QueueStoreEntry|undefined>}
* @private
*/
async _removeEntry(entry) {
if (entry) {
await this.deleteEntry(entry.id);
}
return entry;
}
}

View File

@@ -0,0 +1 @@
export * from './QueueStore.js';

View File

@@ -0,0 +1,53 @@
import { MapLikeObject } from 'workbox-core/types.js';
import '../_version.js';
export interface RequestData extends MapLikeObject {
url: string;
headers: MapLikeObject;
body?: ArrayBuffer;
}
/**
* A class to make it easier to serialize and de-serialize requests so they
* can be stored in IndexedDB.
*
* Most developers will not need to access this class directly;
* it is exposed for advanced use cases.
*/
declare class StorableRequest {
private readonly _requestData;
/**
* Converts a Request object to a plain object that can be structured
* cloned or JSON-stringified.
*
* @param {Request} request
* @return {Promise<StorableRequest>}
*/
static fromRequest(request: Request): Promise<StorableRequest>;
/**
* Accepts an object of request data that can be used to construct a
* `Request` but can also be stored in IndexedDB.
*
* @param {Object} requestData An object of request data that includes the
* `url` plus any relevant properties of
* [requestInit]{@link https://fetch.spec.whatwg.org/#requestinit}.
*/
constructor(requestData: RequestData);
/**
* Returns a deep clone of the instances `_requestData` object.
*
* @return {Object}
*/
toObject(): RequestData;
/**
* Converts this instance to a Request.
*
* @return {Request}
*/
toRequest(): Request;
/**
* Creates and returns a deep clone of the instance.
*
* @return {StorableRequest}
*/
clone(): StorableRequest;
}
export { StorableRequest };

View File

@@ -0,0 +1,121 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { assert } from 'workbox-core/_private/assert.js';
import '../_version.js';
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.
*
* Most developers will not need to access this class directly;
* it is exposed for advanced use cases.
*/
class StorableRequest {
/**
* Converts a Request object to a plain object that can be structured
* cloned or JSON-stringified.
*
* @param {Request} request
* @return {Promise<StorableRequest>}
*/
static async fromRequest(request) {
const requestData = {
url: request.url,
headers: {},
};
// Set the body if present.
if (request.method !== 'GET') {
// Use ArrayBuffer to support non-text request bodies.
// NOTE: we can't use Blobs becuse Safari doesn't support storing
// Blobs in IndexedDB in some cases:
// https://github.com/dfahlander/Dexie.js/issues/618#issuecomment-398348457
requestData.body = await request.clone().arrayBuffer();
}
// Convert the headers from an iterable to an object.
for (const [key, value] of request.headers.entries()) {
requestData.headers[key] = value;
}
// Add all other serializable request properties
for (const prop of serializableProperties) {
if (request[prop] !== undefined) {
requestData[prop] = request[prop];
}
}
return new StorableRequest(requestData);
}
/**
* Accepts an object of request data that can be used to construct a
* `Request` but can also be stored in IndexedDB.
*
* @param {Object} requestData An object of request data that includes the
* `url` plus any relevant properties of
* [requestInit]{@link https://fetch.spec.whatwg.org/#requestinit}.
*/
constructor(requestData) {
if (process.env.NODE_ENV !== 'production') {
assert.isType(requestData, 'object', {
moduleName: 'workbox-background-sync',
className: 'StorableRequest',
funcName: 'constructor',
paramName: 'requestData',
});
assert.isType(requestData.url, 'string', {
moduleName: 'workbox-background-sync',
className: 'StorableRequest',
funcName: 'constructor',
paramName: 'requestData.url',
});
}
// If the request's mode is `navigate`, convert it to `same-origin` since
// navigation requests can't be constructed via script.
if (requestData['mode'] === 'navigate') {
requestData['mode'] = 'same-origin';
}
this._requestData = requestData;
}
/**
* Returns a deep clone of the instances `_requestData` object.
*
* @return {Object}
*/
toObject() {
const requestData = Object.assign({}, this._requestData);
requestData.headers = Object.assign({}, this._requestData.headers);
if (requestData.body) {
requestData.body = requestData.body.slice(0);
}
return requestData;
}
/**
* Converts this instance to a Request.
*
* @return {Request}
*/
toRequest() {
return new Request(this._requestData.url, this._requestData);
}
/**
* Creates and returns a deep clone of the instance.
*
* @return {StorableRequest}
*/
clone() {
return new StorableRequest(this.toObject());
}
}
export { StorableRequest };

View File

@@ -0,0 +1 @@
export * from './StorableRequest.js';

View File

@@ -0,0 +1,31 @@
{
"name": "workbox-background-sync",
"version": "6.6.0",
"license": "MIT",
"author": "Google's Web DevRel Team",
"description": "Queues failed requests and uses the Background Sync API to replay them when the network is available",
"repository": "googlechrome/workbox",
"bugs": "https://github.com/googlechrome/workbox/issues",
"homepage": "https://github.com/GoogleChrome/workbox",
"keywords": [
"workbox",
"workboxjs",
"service worker",
"sw",
"background",
"sync",
"workbox-plugin"
],
"workbox": {
"browserNamespace": "workbox.backgroundSync",
"packageType": "sw"
},
"main": "index.js",
"module": "index.mjs",
"types": "index.d.ts",
"dependencies": {
"idb": "^7.0.1",
"workbox-core": "6.6.0"
},
"gitHead": "252644491d9bb5a67518935ede6df530107c9475"
}

View File

@@ -0,0 +1,43 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {WorkboxPlugin} from 'workbox-core/types.js';
import {Queue, QueueOptions} from './Queue.js';
import './_version.js';
/**
* A class implementing the `fetchDidFail` lifecycle callback. This makes it
* easier to add failed requests to a background sync Queue.
*
* @memberof workbox-background-sync
*/
class BackgroundSyncPlugin implements WorkboxPlugin {
private readonly _queue: Queue;
/**
* @param {string} name See the {@link workbox-background-sync.Queue}
* documentation for parameter details.
* @param {Object} [options] See the
* {@link workbox-background-sync.Queue} documentation for
* parameter details.
*/
constructor(name: string, options?: QueueOptions) {
this._queue = new Queue(name, options);
}
/**
* @param {Object} options
* @param {Request} options.request
* @private
*/
fetchDidFail: WorkboxPlugin['fetchDidFail'] = async ({request}) => {
await this._queue.pushRequest({request});
};
}
export {BackgroundSyncPlugin};

View File

@@ -0,0 +1,487 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {WorkboxError} from 'workbox-core/_private/WorkboxError.js';
import {logger} from 'workbox-core/_private/logger.js';
import {assert} from 'workbox-core/_private/assert.js';
import {getFriendlyURL} from 'workbox-core/_private/getFriendlyURL.js';
import {QueueStore} from './lib/QueueStore.js';
import {QueueStoreEntry, UnidentifiedQueueStoreEntry} from './lib/QueueDb.js';
import {StorableRequest} from './lib/StorableRequest.js';
import './_version.js';
// Give TypeScript the correct global.
declare let self: ServiceWorkerGlobalScope;
interface OnSyncCallbackOptions {
queue: Queue;
}
interface OnSyncCallback {
(options: OnSyncCallbackOptions): void | Promise<void>;
}
export interface QueueOptions {
forceSyncFallback?: boolean;
maxRetentionTime?: number;
onSync?: OnSyncCallback;
}
interface QueueEntry {
request: Request;
timestamp?: number;
// We could use Record<string, unknown> as a type but that would be a breaking
// change, better do it in next major release.
// eslint-disable-next-line @typescript-eslint/ban-types
metadata?: object;
}
const TAG_PREFIX = 'workbox-background-sync';
const MAX_RETENTION_TIME = 60 * 24 * 7; // 7 days in minutes
const queueNames = new Set<string>();
/**
* Converts a QueueStore entry into the format exposed by Queue. This entails
* converting the request data into a real request and omitting the `id` and
* `queueName` properties.
*
* @param {UnidentifiedQueueStoreEntry} queueStoreEntry
* @return {Queue}
* @private
*/
const convertEntry = (
queueStoreEntry: UnidentifiedQueueStoreEntry,
): QueueEntry => {
const queueEntry: QueueEntry = {
request: new StorableRequest(queueStoreEntry.requestData).toRequest(),
timestamp: queueStoreEntry.timestamp,
};
if (queueStoreEntry.metadata) {
queueEntry.metadata = queueStoreEntry.metadata;
}
return queueEntry;
};
/**
* 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-background-sync
*/
class Queue {
private readonly _name: string;
private readonly _onSync: OnSyncCallback;
private readonly _maxRetentionTime: number;
private readonly _queueStore: QueueStore;
private readonly _forceSyncFallback: boolean;
private _syncInProgress = false;
private _requestsAddedDuringSync = false;
/**
* 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 {Function} [options.onSync] A function that gets invoked whenever
* the 'sync' event fires. The function is invoked with an object
* containing the `queue` property (referencing this instance), and you
* can use the callback to customize the replay behavior of the queue.
* When not set the `replayRequests()` method is called.
* Note: if the replay fails after a sync event, make sure you throw an
* error, so the browser knows to retry the sync event later.
* @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.
* @param {boolean} [options.forceSyncFallback=false] If `true`, instead
* of attempting to use background sync events, always attempt to replay
* queued request at service worker startup. Most folks will not need
* this, unless you explicitly target a runtime like Electron that
* exposes the interfaces for background sync, but does not have a working
* implementation.
*/
constructor(
name: string,
{forceSyncFallback, onSync, maxRetentionTime}: QueueOptions = {},
) {
// Ensure the store name is not already being used
if (queueNames.has(name)) {
throw new WorkboxError('duplicate-queue-name', {name});
} else {
queueNames.add(name);
}
this._name = name;
this._onSync = onSync || this.replayRequests;
this._maxRetentionTime = maxRetentionTime || MAX_RETENTION_TIME;
this._forceSyncFallback = Boolean(forceSyncFallback);
this._queueStore = new QueueStore(this._name);
this._addSyncListener();
}
/**
* @return {string}
*/
get name(): string {
return this._name;
}
/**
* Stores the passed request in IndexedDB (with its timestamp and any
* metadata) at the end of the queue.
*
* @param {QueueEntry} entry
* @param {Request} entry.request The request to store in the queue.
* @param {Object} [entry.metadata] Any metadata you want associated with the
* stored request. When requests are replayed you'll have access to this
* metadata object in case you need to modify the request beforehand.
* @param {number} [entry.timestamp] The timestamp (Epoch time in
* milliseconds) when the request was first added to the queue. This is
* used along with `maxRetentionTime` to remove outdated requests. In
* general you don't need to set this value, as it's automatically set
* for you (defaulting to `Date.now()`), but you can update it if you
* don't want particular requests to expire.
*/
async pushRequest(entry: QueueEntry): Promise<void> {
if (process.env.NODE_ENV !== 'production') {
assert!.isType(entry, 'object', {
moduleName: 'workbox-background-sync',
className: 'Queue',
funcName: 'pushRequest',
paramName: 'entry',
});
assert!.isInstance(entry.request, Request, {
moduleName: 'workbox-background-sync',
className: 'Queue',
funcName: 'pushRequest',
paramName: 'entry.request',
});
}
await this._addRequest(entry, 'push');
}
/**
* Stores the passed request in IndexedDB (with its timestamp and any
* metadata) at the beginning of the queue.
*
* @param {QueueEntry} entry
* @param {Request} entry.request The request to store in the queue.
* @param {Object} [entry.metadata] Any metadata you want associated with the
* stored request. When requests are replayed you'll have access to this
* metadata object in case you need to modify the request beforehand.
* @param {number} [entry.timestamp] The timestamp (Epoch time in
* milliseconds) when the request was first added to the queue. This is
* used along with `maxRetentionTime` to remove outdated requests. In
* general you don't need to set this value, as it's automatically set
* for you (defaulting to `Date.now()`), but you can update it if you
* don't want particular requests to expire.
*/
async unshiftRequest(entry: QueueEntry): Promise<void> {
if (process.env.NODE_ENV !== 'production') {
assert!.isType(entry, 'object', {
moduleName: 'workbox-background-sync',
className: 'Queue',
funcName: 'unshiftRequest',
paramName: 'entry',
});
assert!.isInstance(entry.request, Request, {
moduleName: 'workbox-background-sync',
className: 'Queue',
funcName: 'unshiftRequest',
paramName: 'entry.request',
});
}
await this._addRequest(entry, 'unshift');
}
/**
* Removes and returns the last request in the queue (along with its
* timestamp and any metadata). The returned object takes the form:
* `{request, timestamp, metadata}`.
*
* @return {Promise<QueueEntry | undefined>}
*/
async popRequest(): Promise<QueueEntry | undefined> {
return this._removeRequest('pop');
}
/**
* Removes and returns the first request in the queue (along with its
* timestamp and any metadata). The returned object takes the form:
* `{request, timestamp, metadata}`.
*
* @return {Promise<QueueEntry | undefined>}
*/
async shiftRequest(): Promise<QueueEntry | undefined> {
return this._removeRequest('shift');
}
/**
* Returns all the entries that have not expired (per `maxRetentionTime`).
* Any expired entries are removed from the queue.
*
* @return {Promise<Array<QueueEntry>>}
*/
async getAll(): Promise<Array<QueueEntry>> {
const allEntries = await this._queueStore.getAll();
const now = Date.now();
const unexpiredEntries = [];
for (const entry of allEntries) {
// Ignore requests older than maxRetentionTime. Call this function
// recursively until an unexpired request is found.
const maxRetentionTimeInMs = this._maxRetentionTime * 60 * 1000;
if (now - entry.timestamp > maxRetentionTimeInMs) {
await this._queueStore.deleteEntry(entry.id);
} else {
unexpiredEntries.push(convertEntry(entry));
}
}
return unexpiredEntries;
}
/**
* Returns the number of entries present in the queue.
* Note that expired entries (per `maxRetentionTime`) are also included in this count.
*
* @return {Promise<number>}
*/
async size(): Promise<number> {
return await this._queueStore.size();
}
/**
* Adds the entry to the QueueStore and registers for a sync event.
*
* @param {Object} entry
* @param {Request} entry.request
* @param {Object} [entry.metadata]
* @param {number} [entry.timestamp=Date.now()]
* @param {string} operation ('push' or 'unshift')
* @private
*/
async _addRequest(
{request, metadata, timestamp = Date.now()}: QueueEntry,
operation: 'push' | 'unshift',
): Promise<void> {
const storableRequest = await StorableRequest.fromRequest(request.clone());
const entry: UnidentifiedQueueStoreEntry = {
requestData: storableRequest.toObject(),
timestamp,
};
// Only include metadata if it's present.
if (metadata) {
entry.metadata = metadata;
}
switch (operation) {
case 'push':
await this._queueStore.pushEntry(entry);
break;
case 'unshift':
await this._queueStore.unshiftEntry(entry);
break;
}
if (process.env.NODE_ENV !== 'production') {
logger.log(
`Request for '${getFriendlyURL(request.url)}' has ` +
`been added to background sync queue '${this._name}'.`,
);
}
// Don't register for a sync if we're in the middle of a sync. Instead,
// we wait until the sync is complete and call register if
// `this._requestsAddedDuringSync` is true.
if (this._syncInProgress) {
this._requestsAddedDuringSync = true;
} else {
await this.registerSync();
}
}
/**
* Removes and returns the first or last (depending on `operation`) entry
* from the QueueStore that's not older than the `maxRetentionTime`.
*
* @param {string} operation ('pop' or 'shift')
* @return {Object|undefined}
* @private
*/
async _removeRequest(
operation: 'pop' | 'shift',
): Promise<QueueEntry | undefined> {
const now = Date.now();
let entry: QueueStoreEntry | undefined;
switch (operation) {
case 'pop':
entry = await this._queueStore.popEntry();
break;
case 'shift':
entry = await this._queueStore.shiftEntry();
break;
}
if (entry) {
// Ignore requests older than maxRetentionTime. Call this function
// recursively until an unexpired request is found.
const maxRetentionTimeInMs = this._maxRetentionTime * 60 * 1000;
if (now - entry.timestamp > maxRetentionTimeInMs) {
return this._removeRequest(operation);
}
return convertEntry(entry);
} else {
return undefined;
}
}
/**
* Loops through each request in the queue and attempts to re-fetch it.
* If any request fails to re-fetch, it's put back in the same position in
* the queue (which registers a retry for the next sync event).
*/
async replayRequests(): Promise<void> {
let entry;
while ((entry = await this.shiftRequest())) {
try {
await fetch(entry.request.clone());
if (process.env.NODE_ENV !== 'production') {
logger.log(
`Request for '${getFriendlyURL(entry.request.url)}' ` +
`has been replayed in queue '${this._name}'`,
);
}
} catch (error) {
await this.unshiftRequest(entry);
if (process.env.NODE_ENV !== 'production') {
logger.log(
`Request for '${getFriendlyURL(entry.request.url)}' ` +
`failed to replay, putting it back in queue '${this._name}'`,
);
}
throw new WorkboxError('queue-replay-failed', {name: this._name});
}
}
if (process.env.NODE_ENV !== 'production') {
logger.log(
`All requests in queue '${this.name}' have successfully ` +
`replayed; the queue is now empty!`,
);
}
}
/**
* Registers a sync event with a tag unique to this instance.
*/
async registerSync(): Promise<void> {
// See https://github.com/GoogleChrome/workbox/issues/2393
if ('sync' in self.registration && !this._forceSyncFallback) {
try {
await self.registration.sync.register(`${TAG_PREFIX}:${this._name}`);
} catch (err) {
// This means the registration failed for some reason, possibly due to
// the user disabling it.
if (process.env.NODE_ENV !== 'production') {
logger.warn(
`Unable to register sync event for '${this._name}'.`,
err,
);
}
}
}
}
/**
* In sync-supporting browsers, this adds a listener for the sync event.
* In non-sync-supporting browsers, or if _forceSyncFallback is true, this
* will retry the queue on service worker startup.
*
* @private
*/
private _addSyncListener() {
// See https://github.com/GoogleChrome/workbox/issues/2393
if ('sync' in self.registration && !this._forceSyncFallback) {
self.addEventListener('sync', (event: SyncEvent) => {
if (event.tag === `${TAG_PREFIX}:${this._name}`) {
if (process.env.NODE_ENV !== 'production') {
logger.log(
`Background sync for tag '${event.tag}' ` + `has been received`,
);
}
const syncComplete = async () => {
this._syncInProgress = true;
let syncError;
try {
await this._onSync({queue: this});
} catch (error) {
if (error instanceof Error) {
syncError = error;
// Rethrow the error. Note: the logic in the finally clause
// will run before this gets rethrown.
throw syncError;
}
} finally {
// New items may have been added to the queue during the sync,
// so we need to register for a new sync if that's happened...
// Unless there was an error during the sync, in which
// case the browser will automatically retry later, as long
// as `event.lastChance` is not true.
if (
this._requestsAddedDuringSync &&
!(syncError && !event.lastChance)
) {
await this.registerSync();
}
this._syncInProgress = false;
this._requestsAddedDuringSync = false;
}
};
event.waitUntil(syncComplete());
}
});
} else {
if (process.env.NODE_ENV !== 'production') {
logger.log(`Background sync replaying without background sync event`);
}
// If the browser doesn't support background sync, or the developer has
// opted-in to not using it, retry every time the service worker starts up
// as a fallback.
void this._onSync({queue: this});
}
}
/**
* Returns the set of queue names. This is primarily used to reset the list
* of queue names in tests.
*
* @return {Set<string>}
*
* @private
*/
static get _queueNames(): Set<string> {
return queueNames;
}
}
export {Queue};

View File

@@ -0,0 +1,14 @@
/*
Copyright 2021 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import './_version.js';
// This is a temporary workaround to expose something from ./lib/ via our
// top-level public API.
// TODO: In Workbox v7, move the actual code from ./lib/ to this file.
export {QueueStore} from './lib/QueueStore';

View File

@@ -0,0 +1,14 @@
/*
Copyright 2021 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import './_version.js';
// This is a temporary workaround to expose something from ./lib/ via our
// top-level public API.
// TODO: In Workbox v7, move the actual code from ./lib/ to this file.
export {StorableRequest} from './lib/StorableRequest';

View File

@@ -0,0 +1,2 @@
// @ts-ignore
try{self['workbox:background-sync:6.6.0']&&_()}catch(e){}

View File

@@ -0,0 +1,40 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {BackgroundSyncPlugin} from './BackgroundSyncPlugin.js';
import {Queue, QueueOptions} from './Queue.js';
import {QueueStore} from './QueueStore.js';
import {StorableRequest} from './StorableRequest.js';
import './_version.js';
// See https://github.com/GoogleChrome/workbox/issues/2946
interface SyncManager {
getTags(): Promise<string[]>;
register(tag: string): Promise<void>;
}
declare global {
interface ServiceWorkerRegistration {
readonly sync: SyncManager;
}
interface SyncEvent extends ExtendableEvent {
readonly lastChance: boolean;
readonly tag: string;
}
interface ServiceWorkerGlobalScopeEventMap {
sync: SyncEvent;
}
}
/**
* @module workbox-background-sync
*/
export {BackgroundSyncPlugin, Queue, QueueOptions, QueueStore, StorableRequest};

View File

@@ -0,0 +1,200 @@
/*
Copyright 2021 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {openDB, DBSchema, IDBPDatabase} from 'idb';
import {RequestData} from './StorableRequest.js';
import '../_version.js';
interface QueueDBSchema extends DBSchema {
requests: {
key: number;
value: QueueStoreEntry;
indexes: {queueName: string};
};
}
const DB_VERSION = 3;
const DB_NAME = 'workbox-background-sync';
const REQUEST_OBJECT_STORE_NAME = 'requests';
const QUEUE_NAME_INDEX = 'queueName';
export interface UnidentifiedQueueStoreEntry {
requestData: RequestData;
timestamp: number;
id?: number;
queueName?: string;
// We could use Record<string, unknown> as a type but that would be a breaking
// change, better do it in next major release.
// eslint-disable-next-line @typescript-eslint/ban-types
metadata?: object;
}
export interface QueueStoreEntry extends UnidentifiedQueueStoreEntry {
id: number;
}
/**
* A class to interact directly an IndexedDB created specifically to save and
* retrieve QueueStoreEntries. This class encapsulates all the schema details
* to store the representation of a Queue.
*
* @private
*/
export class QueueDb {
private _db: IDBPDatabase<QueueDBSchema> | null = null;
/**
* Add QueueStoreEntry to underlying db.
*
* @param {UnidentifiedQueueStoreEntry} entry
*/
async addEntry(entry: UnidentifiedQueueStoreEntry): Promise<void> {
const db = await this.getDb();
const tx = db.transaction(REQUEST_OBJECT_STORE_NAME, 'readwrite', {
durability: 'relaxed',
});
await tx.store.add(entry as QueueStoreEntry);
await tx.done;
}
/**
* Returns the first entry id in the ObjectStore.
*
* @return {number | undefined}
*/
async getFirstEntryId(): Promise<number | undefined> {
const db = await this.getDb();
const cursor = await db
.transaction(REQUEST_OBJECT_STORE_NAME)
.store.openCursor();
return cursor?.value.id;
}
/**
* Get all the entries filtered by index
*
* @param queueName
* @return {Promise<QueueStoreEntry[]>}
*/
async getAllEntriesByQueueName(
queueName: string,
): Promise<QueueStoreEntry[]> {
const db = await this.getDb();
const results = await db.getAllFromIndex(
REQUEST_OBJECT_STORE_NAME,
QUEUE_NAME_INDEX,
IDBKeyRange.only(queueName),
);
return results ? results : new Array<QueueStoreEntry>();
}
/**
* Returns the number of entries filtered by index
*
* @param queueName
* @return {Promise<number>}
*/
async getEntryCountByQueueName(queueName: string): Promise<number> {
const db = await this.getDb();
return db.countFromIndex(
REQUEST_OBJECT_STORE_NAME,
QUEUE_NAME_INDEX,
IDBKeyRange.only(queueName),
);
}
/**
* Deletes a single entry by id.
*
* @param {number} id the id of the entry to be deleted
*/
async deleteEntry(id: number): Promise<void> {
const db = await this.getDb();
await db.delete(REQUEST_OBJECT_STORE_NAME, id);
}
/**
*
* @param queueName
* @returns {Promise<QueueStoreEntry | undefined>}
*/
async getFirstEntryByQueueName(
queueName: string,
): Promise<QueueStoreEntry | undefined> {
return await this.getEndEntryFromIndex(IDBKeyRange.only(queueName), 'next');
}
/**
*
* @param queueName
* @returns {Promise<QueueStoreEntry | undefined>}
*/
async getLastEntryByQueueName(
queueName: string,
): Promise<QueueStoreEntry | undefined> {
return await this.getEndEntryFromIndex(IDBKeyRange.only(queueName), 'prev');
}
/**
* Returns either the first or the last entries, depending on direction.
* Filtered by index.
*
* @param {IDBCursorDirection} direction
* @param {IDBKeyRange} query
* @return {Promise<QueueStoreEntry | undefined>}
* @private
*/
async getEndEntryFromIndex(
query: IDBKeyRange,
direction: IDBCursorDirection,
): Promise<QueueStoreEntry | undefined> {
const db = await this.getDb();
const cursor = await db
.transaction(REQUEST_OBJECT_STORE_NAME)
.store.index(QUEUE_NAME_INDEX)
.openCursor(query, direction);
return cursor?.value;
}
/**
* Returns an open connection to the database.
*
* @private
*/
private async getDb() {
if (!this._db) {
this._db = await openDB(DB_NAME, DB_VERSION, {
upgrade: this._upgradeDb,
});
}
return this._db;
}
/**
* Upgrades QueueDB
*
* @param {IDBPDatabase<QueueDBSchema>} db
* @param {number} oldVersion
* @private
*/
private _upgradeDb(db: IDBPDatabase<QueueDBSchema>, oldVersion: number) {
if (oldVersion > 0 && oldVersion < DB_VERSION) {
if (db.objectStoreNames.contains(REQUEST_OBJECT_STORE_NAME)) {
db.deleteObjectStore(REQUEST_OBJECT_STORE_NAME);
}
}
const objStore = db.createObjectStore(REQUEST_OBJECT_STORE_NAME, {
autoIncrement: true,
keyPath: 'id',
});
objStore.createIndex(QUEUE_NAME_INDEX, QUEUE_NAME_INDEX, {unique: false});
}
}

View File

@@ -0,0 +1,179 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {assert} from 'workbox-core/_private/assert.js';
import {
UnidentifiedQueueStoreEntry,
QueueStoreEntry,
QueueDb,
} from './QueueDb.js';
import '../_version.js';
/**
* A class to manage storing requests from a Queue in IndexedDB,
* indexed by their queue name for easier access.
*
* Most developers will not need to access this class directly;
* it is exposed for advanced use cases.
*/
export class QueueStore {
private readonly _queueName: string;
private readonly _queueDb: QueueDb;
/**
* Associates this instance with a Queue instance, so entries added can be
* identified by their queue name.
*
* @param {string} queueName
*/
constructor(queueName: string) {
this._queueName = queueName;
this._queueDb = new QueueDb();
}
/**
* Append an entry last in the queue.
*
* @param {Object} entry
* @param {Object} entry.requestData
* @param {number} [entry.timestamp]
* @param {Object} [entry.metadata]
*/
async pushEntry(entry: UnidentifiedQueueStoreEntry): Promise<void> {
if (process.env.NODE_ENV !== 'production') {
assert!.isType(entry, 'object', {
moduleName: 'workbox-background-sync',
className: 'QueueStore',
funcName: 'pushEntry',
paramName: 'entry',
});
assert!.isType(entry.requestData, 'object', {
moduleName: 'workbox-background-sync',
className: 'QueueStore',
funcName: 'pushEntry',
paramName: 'entry.requestData',
});
}
// Don't specify an ID since one is automatically generated.
delete entry.id;
entry.queueName = this._queueName;
await this._queueDb.addEntry(entry);
}
/**
* Prepend an entry first in the queue.
*
* @param {Object} entry
* @param {Object} entry.requestData
* @param {number} [entry.timestamp]
* @param {Object} [entry.metadata]
*/
async unshiftEntry(entry: UnidentifiedQueueStoreEntry): Promise<void> {
if (process.env.NODE_ENV !== 'production') {
assert!.isType(entry, 'object', {
moduleName: 'workbox-background-sync',
className: 'QueueStore',
funcName: 'unshiftEntry',
paramName: 'entry',
});
assert!.isType(entry.requestData, 'object', {
moduleName: 'workbox-background-sync',
className: 'QueueStore',
funcName: 'unshiftEntry',
paramName: 'entry.requestData',
});
}
const firstId = await this._queueDb.getFirstEntryId();
if (firstId) {
// Pick an ID one less than the lowest ID in the object store.
entry.id = firstId - 1;
} else {
// Otherwise let the auto-incrementor assign the ID.
delete entry.id;
}
entry.queueName = this._queueName;
await this._queueDb.addEntry(entry);
}
/**
* Removes and returns the last entry in the queue matching the `queueName`.
*
* @return {Promise<QueueStoreEntry|undefined>}
*/
async popEntry(): Promise<QueueStoreEntry | undefined> {
return this._removeEntry(
await this._queueDb.getLastEntryByQueueName(this._queueName),
);
}
/**
* Removes and returns the first entry in the queue matching the `queueName`.
*
* @return {Promise<QueueStoreEntry|undefined>}
*/
async shiftEntry(): Promise<QueueStoreEntry | undefined> {
return this._removeEntry(
await this._queueDb.getFirstEntryByQueueName(this._queueName),
);
}
/**
* Returns all entries in the store matching the `queueName`.
*
* @param {Object} options See {@link workbox-background-sync.Queue~getAll}
* @return {Promise<Array<Object>>}
*/
async getAll(): Promise<QueueStoreEntry[]> {
return await this._queueDb.getAllEntriesByQueueName(this._queueName);
}
/**
* Returns the number of entries in the store matching the `queueName`.
*
* @param {Object} options See {@link workbox-background-sync.Queue~size}
* @return {Promise<number>}
*/
async size(): Promise<number> {
return await this._queueDb.getEntryCountByQueueName(this._queueName);
}
/**
* Deletes the entry for the given ID.
*
* WARNING: this method does not ensure the deleted entry belongs to this
* queue (i.e. matches the `queueName`). But this limitation is acceptable
* as this class is not publicly exposed. An additional check would make
* this method slower than it needs to be.
*
* @param {number} id
*/
async deleteEntry(id: number): Promise<void> {
await this._queueDb.deleteEntry(id);
}
/**
* Removes and returns the first or last entry in the queue (based on the
* `direction` argument) matching the `queueName`.
*
* @return {Promise<QueueStoreEntry|undefined>}
* @private
*/
async _removeEntry(
entry?: QueueStoreEntry,
): Promise<QueueStoreEntry | undefined> {
if (entry) {
await this.deleteEntry(entry.id);
}
return entry;
}
}

View File

@@ -0,0 +1,156 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {assert} from 'workbox-core/_private/assert.js';
import {MapLikeObject} from 'workbox-core/types.js';
import '../_version.js';
type SerializableProperties =
| 'method'
| 'referrer'
| 'referrerPolicy'
| 'mode'
| 'credentials'
| 'cache'
| 'redirect'
| 'integrity'
| 'keepalive';
const serializableProperties: SerializableProperties[] = [
'method',
'referrer',
'referrerPolicy',
'mode',
'credentials',
'cache',
'redirect',
'integrity',
'keepalive',
];
export interface RequestData extends MapLikeObject {
url: string;
headers: MapLikeObject;
body?: ArrayBuffer;
}
/**
* A class to make it easier to serialize and de-serialize requests so they
* can be stored in IndexedDB.
*
* Most developers will not need to access this class directly;
* it is exposed for advanced use cases.
*/
class StorableRequest {
private readonly _requestData: RequestData;
/**
* Converts a Request object to a plain object that can be structured
* cloned or JSON-stringified.
*
* @param {Request} request
* @return {Promise<StorableRequest>}
*/
static async fromRequest(request: Request): Promise<StorableRequest> {
const requestData: RequestData = {
url: request.url,
headers: {},
};
// Set the body if present.
if (request.method !== 'GET') {
// Use ArrayBuffer to support non-text request bodies.
// NOTE: we can't use Blobs becuse Safari doesn't support storing
// Blobs in IndexedDB in some cases:
// https://github.com/dfahlander/Dexie.js/issues/618#issuecomment-398348457
requestData.body = await request.clone().arrayBuffer();
}
// Convert the headers from an iterable to an object.
for (const [key, value] of request.headers.entries()) {
requestData.headers[key] = value;
}
// Add all other serializable request properties
for (const prop of serializableProperties) {
if (request[prop] !== undefined) {
requestData[prop] = request[prop];
}
}
return new StorableRequest(requestData);
}
/**
* Accepts an object of request data that can be used to construct a
* `Request` but can also be stored in IndexedDB.
*
* @param {Object} requestData An object of request data that includes the
* `url` plus any relevant properties of
* [requestInit]{@link https://fetch.spec.whatwg.org/#requestinit}.
*/
constructor(requestData: RequestData) {
if (process.env.NODE_ENV !== 'production') {
assert!.isType(requestData, 'object', {
moduleName: 'workbox-background-sync',
className: 'StorableRequest',
funcName: 'constructor',
paramName: 'requestData',
});
assert!.isType(requestData.url, 'string', {
moduleName: 'workbox-background-sync',
className: 'StorableRequest',
funcName: 'constructor',
paramName: 'requestData.url',
});
}
// If the request's mode is `navigate`, convert it to `same-origin` since
// navigation requests can't be constructed via script.
if (requestData['mode'] === 'navigate') {
requestData['mode'] = 'same-origin';
}
this._requestData = requestData;
}
/**
* Returns a deep clone of the instances `_requestData` object.
*
* @return {Object}
*/
toObject(): RequestData {
const requestData = Object.assign({}, this._requestData);
requestData.headers = Object.assign({}, this._requestData.headers);
if (requestData.body) {
requestData.body = requestData.body.slice(0);
}
return requestData;
}
/**
* Converts this instance to a Request.
*
* @return {Request}
*/
toRequest(): Request {
return new Request(this._requestData.url, this._requestData);
}
/**
* Creates and returns a deep clone of the instance.
*
* @return {StorableRequest}
*/
clone(): StorableRequest {
return new StorableRequest(this.toObject());
}
}
export {StorableRequest};

View File

@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"outDir": "./",
"rootDir": "./src",
"tsBuildInfoFile": "./tsconfig.tsbuildinfo"
},
"include": ["src/**/*.ts"],
"references": [{"path": "../workbox-core/"}]
}

File diff suppressed because one or more lines are too long