123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- 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;
- const queueNames = new Set();
- const convertEntry = (queueStoreEntry) => {
- const queueEntry = {
- request: new StorableRequest(queueStoreEntry.requestData).toRequest(),
- timestamp: queueStoreEntry.timestamp,
- };
- if (queueStoreEntry.metadata) {
- queueEntry.metadata = queueStoreEntry.metadata;
- }
- return queueEntry;
- };
- class Queue {
-
- constructor(name, { onSync, maxRetentionTime } = {}) {
- this._syncInProgress = false;
- this._requestsAddedDuringSync = false;
-
- 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._queueStore = new QueueStore(this._name);
- this._addSyncListener();
- }
-
- get name() {
- return this._name;
- }
-
- 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');
- }
-
- 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');
- }
-
- async popRequest() {
- return this._removeRequest('pop');
- }
-
- async shiftRequest() {
- return this._removeRequest('shift');
- }
-
- async getAll() {
- const allEntries = await this._queueStore.getAll();
- const now = Date.now();
- const unexpiredEntries = [];
- for (const entry of allEntries) {
-
-
- const maxRetentionTimeInMs = this._maxRetentionTime * 60 * 1000;
- if (now - entry.timestamp > maxRetentionTimeInMs) {
- await this._queueStore.deleteEntry(entry.id);
- }
- else {
- unexpiredEntries.push(convertEntry(entry));
- }
- }
- return unexpiredEntries;
- }
-
- async _addRequest({ request, metadata, timestamp = Date.now(), }, operation) {
- const storableRequest = await StorableRequest.fromRequest(request.clone());
- const entry = {
- requestData: storableRequest.toObject(),
- timestamp,
- };
-
- if (metadata) {
- entry.metadata = metadata;
- }
- await this._queueStore[`${operation}Entry`](entry);
- if (process.env.NODE_ENV !== 'production') {
- logger.log(`Request for '${getFriendlyURL(request.url)}' has ` +
- `been added to background sync queue '${this._name}'.`);
- }
-
-
-
- if (this._syncInProgress) {
- this._requestsAddedDuringSync = true;
- }
- else {
- await this.registerSync();
- }
- }
-
- async _removeRequest(operation) {
- const now = Date.now();
- const entry = await this._queueStore[`${operation}Entry`]();
- if (entry) {
-
-
- const maxRetentionTimeInMs = this._maxRetentionTime * 60 * 1000;
- if (now - entry.timestamp > maxRetentionTimeInMs) {
- return this._removeRequest(operation);
- }
- return convertEntry(entry);
- }
- else {
- return undefined;
- }
- }
-
- 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!`);
- }
- }
-
- async registerSync() {
- if ('sync' in self.registration) {
- try {
- await self.registration.sync.register(`${TAG_PREFIX}:${this._name}`);
- }
- catch (err) {
-
-
- if (process.env.NODE_ENV !== 'production') {
- logger.warn(`Unable to register sync event for '${this._name}'.`, err);
- }
- }
- }
- }
-
- _addSyncListener() {
- if ('sync' in self.registration) {
- 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) {
- syncError = error;
-
-
- throw syncError;
- }
- finally {
-
-
-
-
-
- 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`);
- }
-
-
- this._onSync({ queue: this });
- }
- }
-
- static get _queueNames() {
- return queueNames;
- }
- }
- export { Queue };
|