compat.cjs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. 'use strict';
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. Object.defineProperty(exports, '__esModule', {
  4. value: true
  5. });
  6. var safariFix = require('safari-14-idb-fix');
  7. function _interopDefaultLegacy(e) {
  8. return e && _typeof(e) === 'object' && 'default' in e ? e : {
  9. 'default': e
  10. };
  11. }
  12. var safariFix__default = /*#__PURE__*/_interopDefaultLegacy(safariFix);
  13. function promisifyRequest(request) {
  14. return new Promise(function (resolve, reject) {
  15. // @ts-ignore - file size hacks
  16. request.oncomplete = request.onsuccess = function () {
  17. return resolve(request.result);
  18. }; // @ts-ignore - file size hacks
  19. request.onabort = request.onerror = function () {
  20. return reject(request.error);
  21. };
  22. });
  23. }
  24. function createStore(dbName, storeName) {
  25. var dbp = safariFix__default['default']().then(function () {
  26. var request = indexedDB.open(dbName);
  27. request.onupgradeneeded = function () {
  28. return request.result.createObjectStore(storeName);
  29. };
  30. return promisifyRequest(request);
  31. });
  32. return function (txMode, callback) {
  33. return dbp.then(function (db) {
  34. return callback(db.transaction(storeName, txMode).objectStore(storeName));
  35. });
  36. };
  37. }
  38. var defaultGetStoreFunc;
  39. function defaultGetStore() {
  40. if (!defaultGetStoreFunc) {
  41. defaultGetStoreFunc = createStore('keyval-store', 'keyval');
  42. }
  43. return defaultGetStoreFunc;
  44. }
  45. /**
  46. * Get a value by its key.
  47. *
  48. * @param key
  49. * @param customStore Method to get a custom store. Use with caution (see the docs).
  50. */
  51. function get(key) {
  52. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  53. return customStore('readonly', function (store) {
  54. return promisifyRequest(store.get(key));
  55. });
  56. }
  57. /**
  58. * Set a value with a key.
  59. *
  60. * @param key
  61. * @param value
  62. * @param customStore Method to get a custom store. Use with caution (see the docs).
  63. */
  64. function set(key, value) {
  65. var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
  66. return customStore('readwrite', function (store) {
  67. store.put(value, key);
  68. return promisifyRequest(store.transaction);
  69. });
  70. }
  71. /**
  72. * Set multiple values at once. This is faster than calling set() multiple times.
  73. * It's also atomic – if one of the pairs can't be added, none will be added.
  74. *
  75. * @param entries Array of entries, where each entry is an array of `[key, value]`.
  76. * @param customStore Method to get a custom store. Use with caution (see the docs).
  77. */
  78. function setMany(entries) {
  79. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  80. return customStore('readwrite', function (store) {
  81. entries.forEach(function (entry) {
  82. return store.put(entry[1], entry[0]);
  83. });
  84. return promisifyRequest(store.transaction);
  85. });
  86. }
  87. /**
  88. * Get multiple values by their keys
  89. *
  90. * @param keys
  91. * @param customStore Method to get a custom store. Use with caution (see the docs).
  92. */
  93. function getMany(keys) {
  94. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  95. return customStore('readonly', function (store) {
  96. return Promise.all(keys.map(function (key) {
  97. return promisifyRequest(store.get(key));
  98. }));
  99. });
  100. }
  101. /**
  102. * Update a value. This lets you see the old value and update it as an atomic operation.
  103. *
  104. * @param key
  105. * @param updater A callback that takes the old value and returns a new value.
  106. * @param customStore Method to get a custom store. Use with caution (see the docs).
  107. */
  108. function update(key, updater) {
  109. var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
  110. return customStore('readwrite', function (store) {
  111. return (// Need to create the promise manually.
  112. // If I try to chain promises, the transaction closes in browsers
  113. // that use a promise polyfill (IE10/11).
  114. new Promise(function (resolve, reject) {
  115. store.get(key).onsuccess = function () {
  116. try {
  117. store.put(updater(this.result), key);
  118. resolve(promisifyRequest(store.transaction));
  119. } catch (err) {
  120. reject(err);
  121. }
  122. };
  123. })
  124. );
  125. });
  126. }
  127. /**
  128. * Delete a particular key from the store.
  129. *
  130. * @param key
  131. * @param customStore Method to get a custom store. Use with caution (see the docs).
  132. */
  133. function del(key) {
  134. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  135. return customStore('readwrite', function (store) {
  136. store.delete(key);
  137. return promisifyRequest(store.transaction);
  138. });
  139. }
  140. /**
  141. * Delete multiple keys at once.
  142. *
  143. * @param keys List of keys to delete.
  144. * @param customStore Method to get a custom store. Use with caution (see the docs).
  145. */
  146. function delMany(keys) {
  147. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  148. return customStore('readwrite', function (store) {
  149. keys.forEach(function (key) {
  150. return store.delete(key);
  151. });
  152. return promisifyRequest(store.transaction);
  153. });
  154. }
  155. /**
  156. * Clear all values in the store.
  157. *
  158. * @param customStore Method to get a custom store. Use with caution (see the docs).
  159. */
  160. function clear() {
  161. var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
  162. return customStore('readwrite', function (store) {
  163. store.clear();
  164. return promisifyRequest(store.transaction);
  165. });
  166. }
  167. function eachCursor(customStore, callback) {
  168. return customStore('readonly', function (store) {
  169. // This would be store.getAllKeys(), but it isn't supported by Edge or Safari.
  170. // And openKeyCursor isn't supported by Safari.
  171. store.openCursor().onsuccess = function () {
  172. if (!this.result) return;
  173. callback(this.result);
  174. this.result.continue();
  175. };
  176. return promisifyRequest(store.transaction);
  177. });
  178. }
  179. /**
  180. * Get all keys in the store.
  181. *
  182. * @param customStore Method to get a custom store. Use with caution (see the docs).
  183. */
  184. function keys() {
  185. var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
  186. var items = [];
  187. return eachCursor(customStore, function (cursor) {
  188. return items.push(cursor.key);
  189. }).then(function () {
  190. return items;
  191. });
  192. }
  193. /**
  194. * Get all values in the store.
  195. *
  196. * @param customStore Method to get a custom store. Use with caution (see the docs).
  197. */
  198. function values() {
  199. var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
  200. var items = [];
  201. return eachCursor(customStore, function (cursor) {
  202. return items.push(cursor.value);
  203. }).then(function () {
  204. return items;
  205. });
  206. }
  207. /**
  208. * Get all entries in the store. Each entry is an array of `[key, value]`.
  209. *
  210. * @param customStore Method to get a custom store. Use with caution (see the docs).
  211. */
  212. function entries() {
  213. var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
  214. var items = [];
  215. return eachCursor(customStore, function (cursor) {
  216. return items.push([cursor.key, cursor.value]);
  217. }).then(function () {
  218. return items;
  219. });
  220. }
  221. exports.clear = clear;
  222. exports.createStore = createStore;
  223. exports.del = del;
  224. exports.delMany = delMany;
  225. exports.entries = entries;
  226. exports.get = get;
  227. exports.getMany = getMany;
  228. exports.keys = keys;
  229. exports.promisifyRequest = promisifyRequest;
  230. exports.set = set;
  231. exports.setMany = setMany;
  232. exports.update = update;
  233. exports.values = values;