You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

144 line
3.7 KiB

  1. /*
  2. Copyright 2017 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {DBWrapper} from 'workbox-core/_private/DBWrapper.mjs';
  14. import '../_version.mjs';
  15. // Allows minifier to mangle this name
  16. const REVISON_IDB_FIELD = 'revision';
  17. const URL_IDB_FIELD = 'url';
  18. const DB_STORE_NAME = 'precached-details-models';
  19. /**
  20. * This model will track the relevant information of entries that
  21. * are cached and their matching revision details.
  22. *
  23. * @private
  24. */
  25. class PrecachedDetailsModel {
  26. /**
  27. * Construct a new model for a specific cache.
  28. *
  29. * @param {string} dbName
  30. * @private
  31. */
  32. constructor(dbName) {
  33. // This ensures the db name contains only letters, numbers, '-', '_' and '$'
  34. const filteredDBName = dbName.replace(/[^\w-]/g, '_');
  35. this._db = new DBWrapper(filteredDBName, 2, {
  36. onupgradeneeded: this._handleUpgrade,
  37. });
  38. }
  39. /**
  40. * Should perform an upgrade of indexedDB.
  41. *
  42. * @param {Event} evt
  43. *
  44. * @private
  45. */
  46. _handleUpgrade(evt) {
  47. const db = evt.target.result;
  48. if (evt.oldVersion < 2) {
  49. // IndexedDB version 1 used both 'workbox-precaching' and
  50. // 'precached-details-model' before upgrading to version 2.
  51. // Delete them and create a new store with latest schema.
  52. if (db.objectStoreNames.contains('workbox-precaching')) {
  53. db.deleteObjectStore('workbox-precaching');
  54. }
  55. if (db.objectStoreNames.contains(DB_STORE_NAME)) {
  56. db.deleteObjectStore(DB_STORE_NAME);
  57. }
  58. }
  59. db.createObjectStore(DB_STORE_NAME);
  60. }
  61. /**
  62. * Check if an entry is already cached. Returns false if
  63. * the entry isn't cached or the revision has changed.
  64. *
  65. * @param {string} cacheName
  66. * @param {PrecacheEntry} precacheEntry
  67. * @return {boolean}
  68. *
  69. * @private
  70. */
  71. async _isEntryCached(cacheName, precacheEntry) {
  72. const revisionDetails = await this._getRevision(precacheEntry._entryId);
  73. if (revisionDetails !== precacheEntry._revision) {
  74. return false;
  75. }
  76. const openCache = await caches.open(cacheName);
  77. const cachedResponse = await openCache.match(precacheEntry._cacheRequest);
  78. return !!cachedResponse;
  79. }
  80. /**
  81. * @return {Promise<Array>}
  82. *
  83. * @private
  84. */
  85. async _getAllEntries() {
  86. return await this._db.getAllMatching(DB_STORE_NAME, {
  87. includeKeys: true,
  88. });
  89. }
  90. /**
  91. * Get the current revision details.
  92. *
  93. * @param {Object} entryId
  94. * @return {Promise<string|null>}
  95. *
  96. * @private
  97. */
  98. async _getRevision(entryId) {
  99. const data = await this._db.get(DB_STORE_NAME, entryId);
  100. return data ? data[REVISON_IDB_FIELD] : null;
  101. }
  102. /**
  103. * Add an entry to the details model.
  104. *
  105. * @param {PrecacheEntry} precacheEntry
  106. *
  107. * @private
  108. */
  109. async _addEntry(precacheEntry) {
  110. await this._db.put(
  111. DB_STORE_NAME,
  112. {
  113. [REVISON_IDB_FIELD]: precacheEntry._revision,
  114. [URL_IDB_FIELD]: precacheEntry._cacheRequest.url,
  115. },
  116. precacheEntry._entryId
  117. );
  118. }
  119. /**
  120. * Delete entry from details model.
  121. *
  122. * @param {string} entryId
  123. *
  124. * @private
  125. */
  126. async _deleteEntry(entryId) {
  127. await this._db.delete(DB_STORE_NAME, entryId);
  128. }
  129. }
  130. export default PrecachedDetailsModel;