Queue.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. Copyright 2018 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
  8. import { logger } from 'workbox-core/_private/logger.js';
  9. import { assert } from 'workbox-core/_private/assert.js';
  10. import { getFriendlyURL } from 'workbox-core/_private/getFriendlyURL.js';
  11. import { QueueStore } from './lib/QueueStore.js';
  12. import { StorableRequest } from './lib/StorableRequest.js';
  13. import './_version.js';
  14. const TAG_PREFIX = 'workbox-background-sync';
  15. const MAX_RETENTION_TIME = 60 * 24 * 7; // 7 days in minutes
  16. const queueNames = new Set();
  17. /**
  18. * Converts a QueueStore entry into the format exposed by Queue. This entails
  19. * converting the request data into a real request and omitting the `id` and
  20. * `queueName` properties.
  21. *
  22. * @param {Object} queueStoreEntry
  23. * @return {Object}
  24. * @private
  25. */
  26. const convertEntry = (queueStoreEntry) => {
  27. const queueEntry = {
  28. request: new StorableRequest(queueStoreEntry.requestData).toRequest(),
  29. timestamp: queueStoreEntry.timestamp,
  30. };
  31. if (queueStoreEntry.metadata) {
  32. queueEntry.metadata = queueStoreEntry.metadata;
  33. }
  34. return queueEntry;
  35. };
  36. /**
  37. * A class to manage storing failed requests in IndexedDB and retrying them
  38. * later. All parts of the storing and replaying process are observable via
  39. * callbacks.
  40. *
  41. * @memberof module:workbox-background-sync
  42. */
  43. class Queue {
  44. /**
  45. * Creates an instance of Queue with the given options
  46. *
  47. * @param {string} name The unique name for this queue. This name must be
  48. * unique as it's used to register sync events and store requests
  49. * in IndexedDB specific to this instance. An error will be thrown if
  50. * a duplicate name is detected.
  51. * @param {Object} [options]
  52. * @param {Function} [options.onSync] A function that gets invoked whenever
  53. * the 'sync' event fires. The function is invoked with an object
  54. * containing the `queue` property (referencing this instance), and you
  55. * can use the callback to customize the replay behavior of the queue.
  56. * When not set the `replayRequests()` method is called.
  57. * Note: if the replay fails after a sync event, make sure you throw an
  58. * error, so the browser knows to retry the sync event later.
  59. * @param {number} [options.maxRetentionTime=7 days] The amount of time (in
  60. * minutes) a request may be retried. After this amount of time has
  61. * passed, the request will be deleted from the queue.
  62. */
  63. constructor(name, { onSync, maxRetentionTime } = {}) {
  64. this._syncInProgress = false;
  65. this._requestsAddedDuringSync = false;
  66. // Ensure the store name is not already being used
  67. if (queueNames.has(name)) {
  68. throw new WorkboxError('duplicate-queue-name', { name });
  69. }
  70. else {
  71. queueNames.add(name);
  72. }
  73. this._name = name;
  74. this._onSync = onSync || this.replayRequests;
  75. this._maxRetentionTime = maxRetentionTime || MAX_RETENTION_TIME;
  76. this._queueStore = new QueueStore(this._name);
  77. this._addSyncListener();
  78. }
  79. /**
  80. * @return {string}
  81. */
  82. get name() {
  83. return this._name;
  84. }
  85. /**
  86. * Stores the passed request in IndexedDB (with its timestamp and any
  87. * metadata) at the end of the queue.
  88. *
  89. * @param {Object} entry
  90. * @param {Request} entry.request The request to store in the queue.
  91. * @param {Object} [entry.metadata] Any metadata you want associated with the
  92. * stored request. When requests are replayed you'll have access to this
  93. * metadata object in case you need to modify the request beforehand.
  94. * @param {number} [entry.timestamp] The timestamp (Epoch time in
  95. * milliseconds) when the request was first added to the queue. This is
  96. * used along with `maxRetentionTime` to remove outdated requests. In
  97. * general you don't need to set this value, as it's automatically set
  98. * for you (defaulting to `Date.now()`), but you can update it if you
  99. * don't want particular requests to expire.
  100. */
  101. async pushRequest(entry) {
  102. if (process.env.NODE_ENV !== 'production') {
  103. assert.isType(entry, 'object', {
  104. moduleName: 'workbox-background-sync',
  105. className: 'Queue',
  106. funcName: 'pushRequest',
  107. paramName: 'entry',
  108. });
  109. assert.isInstance(entry.request, Request, {
  110. moduleName: 'workbox-background-sync',
  111. className: 'Queue',
  112. funcName: 'pushRequest',
  113. paramName: 'entry.request',
  114. });
  115. }
  116. await this._addRequest(entry, 'push');
  117. }
  118. /**
  119. * Stores the passed request in IndexedDB (with its timestamp and any
  120. * metadata) at the beginning of the queue.
  121. *
  122. * @param {Object} entry
  123. * @param {Request} entry.request The request to store in the queue.
  124. * @param {Object} [entry.metadata] Any metadata you want associated with the
  125. * stored request. When requests are replayed you'll have access to this
  126. * metadata object in case you need to modify the request beforehand.
  127. * @param {number} [entry.timestamp] The timestamp (Epoch time in
  128. * milliseconds) when the request was first added to the queue. This is
  129. * used along with `maxRetentionTime` to remove outdated requests. In
  130. * general you don't need to set this value, as it's automatically set
  131. * for you (defaulting to `Date.now()`), but you can update it if you
  132. * don't want particular requests to expire.
  133. */
  134. async unshiftRequest(entry) {
  135. if (process.env.NODE_ENV !== 'production') {
  136. assert.isType(entry, 'object', {
  137. moduleName: 'workbox-background-sync',
  138. className: 'Queue',
  139. funcName: 'unshiftRequest',
  140. paramName: 'entry',
  141. });
  142. assert.isInstance(entry.request, Request, {
  143. moduleName: 'workbox-background-sync',
  144. className: 'Queue',
  145. funcName: 'unshiftRequest',
  146. paramName: 'entry.request',
  147. });
  148. }
  149. await this._addRequest(entry, 'unshift');
  150. }
  151. /**
  152. * Removes and returns the last request in the queue (along with its
  153. * timestamp and any metadata). The returned object takes the form:
  154. * `{request, timestamp, metadata}`.
  155. *
  156. * @return {Promise<Object>}
  157. */
  158. async popRequest() {
  159. return this._removeRequest('pop');
  160. }
  161. /**
  162. * Removes and returns the first request in the queue (along with its
  163. * timestamp and any metadata). The returned object takes the form:
  164. * `{request, timestamp, metadata}`.
  165. *
  166. * @return {Promise<Object>}
  167. */
  168. async shiftRequest() {
  169. return this._removeRequest('shift');
  170. }
  171. /**
  172. * Returns all the entries that have not expired (per `maxRetentionTime`).
  173. * Any expired entries are removed from the queue.
  174. *
  175. * @return {Promise<Array<Object>>}
  176. */
  177. async getAll() {
  178. const allEntries = await this._queueStore.getAll();
  179. const now = Date.now();
  180. const unexpiredEntries = [];
  181. for (const entry of allEntries) {
  182. // Ignore requests older than maxRetentionTime. Call this function
  183. // recursively until an unexpired request is found.
  184. const maxRetentionTimeInMs = this._maxRetentionTime * 60 * 1000;
  185. if (now - entry.timestamp > maxRetentionTimeInMs) {
  186. await this._queueStore.deleteEntry(entry.id);
  187. }
  188. else {
  189. unexpiredEntries.push(convertEntry(entry));
  190. }
  191. }
  192. return unexpiredEntries;
  193. }
  194. /**
  195. * Adds the entry to the QueueStore and registers for a sync event.
  196. *
  197. * @param {Object} entry
  198. * @param {Request} entry.request
  199. * @param {Object} [entry.metadata]
  200. * @param {number} [entry.timestamp=Date.now()]
  201. * @param {string} operation ('push' or 'unshift')
  202. * @private
  203. */
  204. async _addRequest({ request, metadata, timestamp = Date.now(), }, operation) {
  205. const storableRequest = await StorableRequest.fromRequest(request.clone());
  206. const entry = {
  207. requestData: storableRequest.toObject(),
  208. timestamp,
  209. };
  210. // Only include metadata if it's present.
  211. if (metadata) {
  212. entry.metadata = metadata;
  213. }
  214. await this._queueStore[`${operation}Entry`](entry);
  215. if (process.env.NODE_ENV !== 'production') {
  216. logger.log(`Request for '${getFriendlyURL(request.url)}' has ` +
  217. `been added to background sync queue '${this._name}'.`);
  218. }
  219. // Don't register for a sync if we're in the middle of a sync. Instead,
  220. // we wait until the sync is complete and call register if
  221. // `this._requestsAddedDuringSync` is true.
  222. if (this._syncInProgress) {
  223. this._requestsAddedDuringSync = true;
  224. }
  225. else {
  226. await this.registerSync();
  227. }
  228. }
  229. /**
  230. * Removes and returns the first or last (depending on `operation`) entry
  231. * from the QueueStore that's not older than the `maxRetentionTime`.
  232. *
  233. * @param {string} operation ('pop' or 'shift')
  234. * @return {Object|undefined}
  235. * @private
  236. */
  237. async _removeRequest(operation) {
  238. const now = Date.now();
  239. const entry = await this._queueStore[`${operation}Entry`]();
  240. if (entry) {
  241. // Ignore requests older than maxRetentionTime. Call this function
  242. // recursively until an unexpired request is found.
  243. const maxRetentionTimeInMs = this._maxRetentionTime * 60 * 1000;
  244. if (now - entry.timestamp > maxRetentionTimeInMs) {
  245. return this._removeRequest(operation);
  246. }
  247. return convertEntry(entry);
  248. }
  249. else {
  250. return undefined;
  251. }
  252. }
  253. /**
  254. * Loops through each request in the queue and attempts to re-fetch it.
  255. * If any request fails to re-fetch, it's put back in the same position in
  256. * the queue (which registers a retry for the next sync event).
  257. */
  258. async replayRequests() {
  259. let entry;
  260. while (entry = await this.shiftRequest()) {
  261. try {
  262. await fetch(entry.request.clone());
  263. if (process.env.NODE_ENV !== 'production') {
  264. logger.log(`Request for '${getFriendlyURL(entry.request.url)}'` +
  265. `has been replayed in queue '${this._name}'`);
  266. }
  267. }
  268. catch (error) {
  269. await this.unshiftRequest(entry);
  270. if (process.env.NODE_ENV !== 'production') {
  271. logger.log(`Request for '${getFriendlyURL(entry.request.url)}'` +
  272. `failed to replay, putting it back in queue '${this._name}'`);
  273. }
  274. throw new WorkboxError('queue-replay-failed', { name: this._name });
  275. }
  276. }
  277. if (process.env.NODE_ENV !== 'production') {
  278. logger.log(`All requests in queue '${this.name}' have successfully ` +
  279. `replayed; the queue is now empty!`);
  280. }
  281. }
  282. /**
  283. * Registers a sync event with a tag unique to this instance.
  284. */
  285. async registerSync() {
  286. if ('sync' in self.registration) {
  287. try {
  288. await self.registration.sync.register(`${TAG_PREFIX}:${this._name}`);
  289. }
  290. catch (err) {
  291. // This means the registration failed for some reason, possibly due to
  292. // the user disabling it.
  293. if (process.env.NODE_ENV !== 'production') {
  294. logger.warn(`Unable to register sync event for '${this._name}'.`, err);
  295. }
  296. }
  297. }
  298. }
  299. /**
  300. * In sync-supporting browsers, this adds a listener for the sync event.
  301. * In non-sync-supporting browsers, this will retry the queue on service
  302. * worker startup.
  303. *
  304. * @private
  305. */
  306. _addSyncListener() {
  307. if ('sync' in self.registration) {
  308. self.addEventListener('sync', (event) => {
  309. if (event.tag === `${TAG_PREFIX}:${this._name}`) {
  310. if (process.env.NODE_ENV !== 'production') {
  311. logger.log(`Background sync for tag '${event.tag}'` +
  312. `has been received`);
  313. }
  314. const syncComplete = async () => {
  315. this._syncInProgress = true;
  316. let syncError;
  317. try {
  318. await this._onSync({ queue: this });
  319. }
  320. catch (error) {
  321. syncError = error;
  322. // Rethrow the error. Note: the logic in the finally clause
  323. // will run before this gets rethrown.
  324. throw syncError;
  325. }
  326. finally {
  327. // New items may have been added to the queue during the sync,
  328. // so we need to register for a new sync if that's happened...
  329. // Unless there was an error during the sync, in which
  330. // case the browser will automatically retry later, as long
  331. // as `event.lastChance` is not true.
  332. if (this._requestsAddedDuringSync &&
  333. !(syncError && !event.lastChance)) {
  334. await this.registerSync();
  335. }
  336. this._syncInProgress = false;
  337. this._requestsAddedDuringSync = false;
  338. }
  339. };
  340. event.waitUntil(syncComplete());
  341. }
  342. });
  343. }
  344. else {
  345. if (process.env.NODE_ENV !== 'production') {
  346. logger.log(`Background sync replaying without background sync event`);
  347. }
  348. // If the browser doesn't support background sync, retry
  349. // every time the service worker starts up as a fallback.
  350. this._onSync({ queue: this });
  351. }
  352. }
  353. /**
  354. * Returns the set of queue names. This is primarily used to reset the list
  355. * of queue names in tests.
  356. *
  357. * @return {Set}
  358. *
  359. * @private
  360. */
  361. static get _queueNames() {
  362. return queueNames;
  363. }
  364. }
  365. export { Queue };