index.cjs 6.3 KB

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