fake-indexeddb 6.0.1 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +23 -2
  2. package/auto/index.js +39 -16
  3. package/auto/index.mjs +39 -16
  4. package/build/cjs/FDBCursor.js +2 -2
  5. package/build/cjs/FDBCursorWithValue.js +2 -2
  6. package/build/cjs/FDBDatabase.js +27 -9
  7. package/build/cjs/FDBFactory.js +3 -3
  8. package/build/cjs/FDBIndex.js +37 -15
  9. package/build/cjs/FDBKeyRange.js +2 -2
  10. package/build/cjs/FDBObjectStore.js +37 -15
  11. package/build/cjs/FDBOpenDBRequest.js +2 -2
  12. package/build/cjs/FDBRecord.js +36 -0
  13. package/build/cjs/FDBRequest.js +2 -2
  14. package/build/cjs/FDBTransaction.js +4 -3
  15. package/build/cjs/FDBVersionChangeEvent.js +2 -2
  16. package/build/cjs/forceCloseDatabase.js +28 -0
  17. package/build/cjs/index.js +14 -0
  18. package/build/cjs/lib/FakeDOMStringList.js +27 -57
  19. package/build/cjs/lib/FakeEventTarget.js +1 -0
  20. package/build/cjs/lib/Index.js +23 -7
  21. package/build/cjs/lib/ObjectStore.js +40 -14
  22. package/build/cjs/lib/RecordStore.js +68 -97
  23. package/build/cjs/lib/binarySearchTree.js +308 -0
  24. package/build/cjs/lib/errors.js +23 -1
  25. package/build/cjs/lib/extractGetAllOptions.js +40 -0
  26. package/build/cjs/lib/extractKey.js +4 -1
  27. package/build/cjs/lib/getKeyPath.js +16 -0
  28. package/build/cjs/lib/isPotentiallyValidKeyRange.js +33 -0
  29. package/build/cjs/lib/isSharedArrayBuffer.js +10 -0
  30. package/build/cjs/lib/scheduling.js +11 -1
  31. package/build/cjs/lib/validateKeyPath.js +5 -4
  32. package/build/cjs/lib/valueToKey.js +10 -5
  33. package/build/esm/FDBCursor.js +2 -2
  34. package/build/esm/FDBCursorWithValue.js +2 -2
  35. package/build/esm/FDBDatabase.js +24 -6
  36. package/build/esm/FDBFactory.js +3 -3
  37. package/build/esm/FDBIndex.js +37 -15
  38. package/build/esm/FDBKeyRange.js +2 -2
  39. package/build/esm/FDBObjectStore.js +37 -15
  40. package/build/esm/FDBOpenDBRequest.js +2 -2
  41. package/build/esm/FDBRecord.js +29 -0
  42. package/build/esm/FDBRequest.js +2 -2
  43. package/build/esm/FDBTransaction.js +4 -3
  44. package/build/esm/FDBVersionChangeEvent.js +2 -2
  45. package/build/esm/forceCloseDatabase.js +22 -0
  46. package/build/esm/index.js +3 -1
  47. package/build/esm/lib/FakeDOMStringList.js +27 -58
  48. package/build/esm/lib/FakeEventTarget.js +1 -0
  49. package/build/esm/lib/Index.js +23 -7
  50. package/build/esm/lib/ObjectStore.js +42 -15
  51. package/build/esm/lib/RecordStore.js +68 -97
  52. package/build/esm/lib/binarySearchTree.js +300 -0
  53. package/build/esm/lib/errors.js +21 -0
  54. package/build/esm/lib/extractGetAllOptions.js +33 -0
  55. package/build/esm/lib/extractKey.js +4 -1
  56. package/build/esm/lib/getKeyPath.js +10 -0
  57. package/build/esm/lib/isPotentiallyValidKeyRange.js +26 -0
  58. package/build/esm/lib/isSharedArrayBuffer.js +3 -0
  59. package/build/esm/lib/scheduling.js +11 -1
  60. package/build/esm/lib/validateKeyPath.js +2 -0
  61. package/build/esm/lib/valueToKey.js +10 -5
  62. package/package.json +15 -10
  63. package/types.d.ts +4 -0
  64. package/build/cjs/lib/binarySearch.js +0 -85
  65. package/build/esm/lib/binarySearch.js +0 -74
@@ -0,0 +1,308 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _cmp = _interopRequireDefault(require("./cmp.js"));
8
+ var _FDBKeyRange = _interopRequireDefault(require("../FDBKeyRange.js"));
9
+ var _errors = require("./errors.js");
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ // what fraction of the total number of nodes are allowed to be deleted tombstones?
12
+ const MAX_TOMBSTONE_FACTOR = 2 / 3;
13
+ const EVERYTHING_KEY_RANGE = new _FDBKeyRange.default(undefined, undefined, false, false);
14
+ /**
15
+ * Simple red-black binary tree with some aspects of a scapegoat tree. The main goal here is simplicity of
16
+ * implementation, tailored to the needs of IndexedDB.
17
+ *
18
+ * Basically this implements a [red-black tree][1] for insertions, but uses the much simpler [scapegoat tree][2]
19
+ * strategy for deletions. Deletions are a simple matter of rebuilding the tree from scratch if more than 2/3 of the
20
+ * tree is full of deleted (tombstone) markers.
21
+ *
22
+ * [1]: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree
23
+ * [2]: https://en.wikipedia.org/wiki/Scapegoat_tree
24
+ */
25
+ class BinarySearchTree {
26
+ _numTombstones = 0;
27
+ _numNodes = 0;
28
+
29
+ /**
30
+ *
31
+ * @param keysAreUnique - whether keys can be unique, and thus whether we cn skip checking `record.value` when
32
+ * comparing. This is basically used to distinguish ObjectStores (where the value is the entire object, not used
33
+ * as a key) from non-unique Indexes (where both the key and the value are meaningful keys used for sorting)
34
+ */
35
+ constructor(keysAreUnique) {
36
+ this._keysAreUnique = !!keysAreUnique;
37
+ }
38
+ size() {
39
+ return this._numNodes - this._numTombstones;
40
+ }
41
+ get(record) {
42
+ return this._getByComparator(this._root, otherRecord => this._compare(record, otherRecord));
43
+ }
44
+ contains(record) {
45
+ return !!this.get(record);
46
+ }
47
+ _compare(a, b) {
48
+ const keyComparison = (0, _cmp.default)(a.key, b.key);
49
+ if (keyComparison !== 0) {
50
+ return keyComparison;
51
+ }
52
+ // if keys are unique, then we can (and must) avoid comparing the values, since they may be non-comparable
53
+ // (e.g. in the case of an ObjectStore, they are record objects)
54
+ return this._keysAreUnique ? 0 : (0, _cmp.default)(a.value, b.value);
55
+ }
56
+ _getByComparator(node, comparator) {
57
+ let current = node;
58
+ while (current) {
59
+ const comparison = comparator(current.record);
60
+ if (comparison < 0) {
61
+ current = current.left;
62
+ } else if (comparison > 0) {
63
+ current = current.right;
64
+ } else {
65
+ return current.record;
66
+ }
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Put a new record, and return the overwritten record if an overwrite occurred.
72
+ * @param record
73
+ * @param noOverwrite - throw a ConstraintError in case of overwrite
74
+ */
75
+ put(record, noOverwrite = false) {
76
+ if (!this._root) {
77
+ this._root = {
78
+ record,
79
+ left: undefined,
80
+ right: undefined,
81
+ parent: undefined,
82
+ deleted: false,
83
+ // the root is always black in a red-black tree
84
+ red: false
85
+ };
86
+ this._numNodes++;
87
+ return;
88
+ }
89
+ return this._put(this._root, record, noOverwrite);
90
+ }
91
+ _put(node, record, noOverwrite) {
92
+ const comparison = this._compare(record, node.record);
93
+ if (comparison < 0) {
94
+ if (node.left) {
95
+ return this._put(node.left, record, noOverwrite);
96
+ } else {
97
+ node.left = {
98
+ record,
99
+ left: undefined,
100
+ right: undefined,
101
+ parent: node,
102
+ deleted: false,
103
+ red: true
104
+ };
105
+ this._onNewNodeInserted(node.left);
106
+ }
107
+ } else if (comparison > 0) {
108
+ if (node.right) {
109
+ return this._put(node.right, record, noOverwrite);
110
+ } else {
111
+ node.right = {
112
+ record,
113
+ left: undefined,
114
+ right: undefined,
115
+ parent: node,
116
+ deleted: false,
117
+ red: true
118
+ };
119
+ this._onNewNodeInserted(node.right);
120
+ }
121
+ } else if (node.deleted) {
122
+ // undelete
123
+ node.deleted = false;
124
+ node.record = record;
125
+ this._numTombstones--;
126
+ } else if (noOverwrite) {
127
+ // replace not allowed in case of noOverwrite
128
+ throw new _errors.ConstraintError();
129
+ } else {
130
+ // replace, don't add, so no need to increment. return the overwritten record
131
+ const overwrittenRecord = node.record;
132
+ node.record = record;
133
+ return overwrittenRecord;
134
+ }
135
+ }
136
+ delete(record) {
137
+ if (!this._root) {
138
+ return;
139
+ }
140
+ this._delete(this._root, record);
141
+ if (this._numTombstones > this._numNodes * MAX_TOMBSTONE_FACTOR) {
142
+ // to keep the implementation simple, and because most users of fake-indexeddb are not going to be deleting
143
+ // a lot of nodes, just rebuild the whole tree (defragment) if the tree is too full of tombstones,
144
+ // as inspired by the scapegoat tree: https://en.wikipedia.org/wiki/Scapegoat_tree#Deletion
145
+ const records = [...this.getAllRecords()];
146
+ this._root = this._rebuild(records, undefined, false);
147
+ this._numNodes = records.length;
148
+ this._numTombstones = 0;
149
+ }
150
+ }
151
+ _delete(node, record) {
152
+ if (!node) {
153
+ return;
154
+ }
155
+ const comparison = this._compare(record, node.record);
156
+ if (comparison < 0) {
157
+ this._delete(node.left, record);
158
+ } else if (comparison > 0) {
159
+ this._delete(node.right, record);
160
+ } else if (!node.deleted) {
161
+ this._numTombstones++;
162
+ node.deleted = true;
163
+ }
164
+ }
165
+ *getAllRecords(descending = false) {
166
+ yield* this.getRecords(EVERYTHING_KEY_RANGE, descending);
167
+ }
168
+ *getRecords(keyRange, descending = false) {
169
+ yield* this._getRecordsForNode(this._root, keyRange, descending);
170
+ }
171
+ *_getRecordsForNode(node, keyRange, descending = false) {
172
+ if (!node) {
173
+ return;
174
+ }
175
+ yield* this._findRecords(node, keyRange, descending);
176
+ }
177
+ *_findRecords(node, keyRange, descending = false) {
178
+ const {
179
+ lower,
180
+ upper,
181
+ lowerOpen,
182
+ upperOpen
183
+ } = keyRange;
184
+ const {
185
+ record: {
186
+ key
187
+ }
188
+ } = node;
189
+ const lowerComparison = lower === undefined ? -1 : (0, _cmp.default)(lower, key);
190
+ const upperComparison = upper === undefined ? 1 : (0, _cmp.default)(upper, key);
191
+
192
+ // if keys are non-unique then we need to go left/right even for equality
193
+ // else we can just do LT/GT rather than LTE/GTE as a slight optimization
194
+ const moreLeft = this._keysAreUnique ? lowerComparison < 0 : lowerComparison <= 0;
195
+ const moreRight = this._keysAreUnique ? upperComparison > 0 : upperComparison >= 0;
196
+
197
+ // in descending mode we start with rightmost nodes, else leftmost
198
+ const moreStart = descending ? moreRight : moreLeft;
199
+ const moreEnd = descending ? moreLeft : moreRight;
200
+ const start = descending ? "right" : "left";
201
+ const end = descending ? "left" : "right";
202
+
203
+ // does the current record actually match the key range?
204
+ const lowerMatches = lowerOpen ? lowerComparison < 0 : lowerComparison <= 0;
205
+ const upperMatches = upperOpen ? upperComparison > 0 : upperComparison >= 0;
206
+ if (moreStart && node[start]) {
207
+ yield* this._findRecords(node[start], keyRange, descending);
208
+ }
209
+ if (lowerMatches && upperMatches && !node.deleted) {
210
+ yield node.record;
211
+ }
212
+ if (moreEnd && node[end]) {
213
+ yield* this._findRecords(node[end], keyRange, descending);
214
+ }
215
+ }
216
+ _onNewNodeInserted(newNode) {
217
+ this._numNodes++;
218
+ this._rebalanceTree(newNode);
219
+ }
220
+
221
+ // based on https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Insertion
222
+ _rebalanceTree(node) {
223
+ let parent = node.parent;
224
+ do {
225
+ // case 1 - no red/black violation
226
+ if (!parent.red) {
227
+ return;
228
+ }
229
+ const grandparent = parent.parent;
230
+ if (!grandparent) {
231
+ // case #4 - parent is the red root, node is also red, so parent goes black
232
+ parent.red = false;
233
+ return;
234
+ }
235
+ const parentIsRightChild = parent === grandparent.right;
236
+ const uncle = parentIsRightChild ? grandparent.left : grandparent.right;
237
+ if (!uncle || !uncle.red) {
238
+ if (node === (parentIsRightChild ? parent.left : parent.right)) {
239
+ // case #5 - parent is red but uncle is black
240
+ this._rotateSubtree(parent, parentIsRightChild);
241
+ node = parent;
242
+ parent = parentIsRightChild ? grandparent.right : grandparent.left;
243
+ }
244
+
245
+ // case #6 - node is "outer" grandchild of grandparent
246
+ this._rotateSubtree(grandparent, !parentIsRightChild);
247
+ parent.red = false;
248
+ grandparent.red = true;
249
+ return;
250
+ }
251
+
252
+ // case #2 - parent and uncle are both red, so both of them go black and grandparent goes red
253
+ parent.red = false;
254
+ uncle.red = false;
255
+ grandparent.red = true;
256
+ node = grandparent;
257
+ } while (node.parent ? parent = node.parent : false);
258
+
259
+ // case #3 - current node is the root, all constraints satisfied
260
+ }
261
+
262
+ // based on https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Implementation
263
+ _rotateSubtree(node, right) {
264
+ const parent = node.parent;
265
+ const newRoot = right ? node.left : node.right; // opposite direction
266
+ const newChild = right ? newRoot.right : newRoot.left;
267
+ node[right ? "left" : "right"] = newChild;
268
+ if (newChild) {
269
+ newChild.parent = node;
270
+ }
271
+ newRoot[right ? "right" : "left"] = node;
272
+ newRoot.parent = parent;
273
+ node.parent = newRoot;
274
+ if (parent) {
275
+ parent[node === parent.right ? "right" : "left"] = newRoot;
276
+ } else {
277
+ this._root = newRoot;
278
+ }
279
+ return newRoot;
280
+ }
281
+
282
+ // rebuild the whole tree from scratch, used to avoid too many deletion tombstones accumulating
283
+ _rebuild(records, parent, red) {
284
+ const {
285
+ length
286
+ } = records;
287
+ if (!length) {
288
+ return undefined;
289
+ }
290
+ const mid = length >>> 1; // like Math.floor(records.length / 2) but fast
291
+
292
+ const node = {
293
+ record: records[mid],
294
+ left: undefined,
295
+ right: undefined,
296
+ parent,
297
+ deleted: false,
298
+ red
299
+ };
300
+ const left = this._rebuild(records.slice(0, mid), node, !red);
301
+ const right = this._rebuild(records.slice(mid + 1), node, !red);
302
+ node.left = left;
303
+ node.right = right;
304
+ return node;
305
+ }
306
+ }
307
+ exports.default = BinarySearchTree;
308
+ module.exports = exports.default;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.VersionError = exports.TransactionInactiveError = exports.ReadOnlyError = exports.NotFoundError = exports.InvalidStateError = exports.InvalidAccessError = exports.DataError = exports.DataCloneError = exports.ConstraintError = exports.AbortError = void 0;
6
+ exports.VersionError = exports.TransactionInactiveError = exports.SyntaxError = exports.ReadOnlyError = exports.NotFoundError = exports.InvalidStateError = exports.InvalidAccessError = exports.DataError = exports.DataCloneError = exports.ConstraintError = exports.AbortError = void 0;
7
7
  const messages = {
8
8
  AbortError: "A request was aborted, for example through a call to IDBTransaction.abort.",
9
9
  ConstraintError: "A mutation operation in the transaction failed because a constraint was not satisfied. For example, an object such as an object store or index already exists and a request attempted to create a new one.",
@@ -14,8 +14,20 @@ const messages = {
14
14
  NotFoundError: "The operation failed because the requested database object could not be found. For example, an object store did not exist but was being opened.",
15
15
  ReadOnlyError: 'The mutating operation was attempted in a "readonly" transaction.',
16
16
  TransactionInactiveError: "A request was placed against a transaction which is currently not active, or which is finished.",
17
+ SyntaxError: "The keypath argument contains an invalid key path",
17
18
  VersionError: "An attempt was made to open a database using a lower version than the existing version."
18
19
  };
20
+
21
+ // Cannot set an error code on an error using the normal setter;
22
+ // this leads to "Cannot set property code of which has only a getter"
23
+ const setErrorCode = (error, value) => {
24
+ Object.defineProperty(error, 'code', {
25
+ value,
26
+ writable: false,
27
+ enumerable: true,
28
+ configurable: false
29
+ });
30
+ };
19
31
  class AbortError extends DOMException {
20
32
  constructor(message = messages.AbortError) {
21
33
  super(message, "AbortError");
@@ -37,6 +49,7 @@ exports.DataCloneError = DataCloneError;
37
49
  class DataError extends DOMException {
38
50
  constructor(message = messages.DataError) {
39
51
  super(message, "DataError");
52
+ setErrorCode(this, 0);
40
53
  }
41
54
  }
42
55
  exports.DataError = DataError;
@@ -49,6 +62,7 @@ exports.InvalidAccessError = InvalidAccessError;
49
62
  class InvalidStateError extends DOMException {
50
63
  constructor(message = messages.InvalidStateError) {
51
64
  super(message, "InvalidStateError");
65
+ setErrorCode(this, 11);
52
66
  }
53
67
  }
54
68
  exports.InvalidStateError = InvalidStateError;
@@ -64,9 +78,17 @@ class ReadOnlyError extends DOMException {
64
78
  }
65
79
  }
66
80
  exports.ReadOnlyError = ReadOnlyError;
81
+ class SyntaxError extends DOMException {
82
+ constructor(message = messages.VersionError) {
83
+ super(message, "SyntaxError");
84
+ setErrorCode(this, 12);
85
+ }
86
+ }
87
+ exports.SyntaxError = SyntaxError;
67
88
  class TransactionInactiveError extends DOMException {
68
89
  constructor(message = messages.TransactionInactiveError) {
69
90
  super(message, "TransactionInactiveError");
91
+ setErrorCode(this, 0);
70
92
  }
71
93
  }
72
94
  exports.TransactionInactiveError = TransactionInactiveError;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _isPotentiallyValidKeyRange = _interopRequireDefault(require("./isPotentiallyValidKeyRange.js"));
8
+ var _enforceRange = _interopRequireDefault(require("./enforceRange.js"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ // https://www.w3.org/TR/IndexedDB/#create-request-to-retrieve-multiple-items
11
+ const extractGetAllOptions = (queryOrOptions, count, numArguments) => {
12
+ let query;
13
+ let direction;
14
+ if ((0, _isPotentiallyValidKeyRange.default)(queryOrOptions)) {
15
+ // queryOrOptions is FDBKeyRange | Key
16
+ query = queryOrOptions;
17
+ if (numArguments > 1 && count !== undefined) {
18
+ count = (0, _enforceRange.default)(count, "unsigned long");
19
+ }
20
+ } else {
21
+ // queryOrOptions is FDBGetAllOptions
22
+ const getAllOptions = queryOrOptions;
23
+ if (getAllOptions.query !== undefined) {
24
+ query = getAllOptions.query;
25
+ }
26
+ if (getAllOptions.count !== undefined) {
27
+ count = (0, _enforceRange.default)(getAllOptions.count, "unsigned long");
28
+ }
29
+ if (getAllOptions.direction !== undefined) {
30
+ direction = getAllOptions.direction;
31
+ }
32
+ }
33
+ return {
34
+ query,
35
+ count,
36
+ direction
37
+ };
38
+ };
39
+ var _default = exports.default = extractGetAllOptions;
40
+ module.exports = exports.default;
@@ -42,7 +42,10 @@ const extractKey = (keyPath, value) => {
42
42
  identifier = remainingKeyPath;
43
43
  remainingKeyPath = null;
44
44
  }
45
- if (object === undefined || object === null || !Object.hasOwn(object, identifier)) {
45
+
46
+ // special cases: https://w3c.github.io/IndexedDB/#evaluate-a-key-path-on-a-value
47
+ const isSpecialIdentifier = identifier === "length" && (typeof object === "string" || Array.isArray(object)) || (identifier === "size" || identifier === "type") && typeof Blob !== "undefined" && object instanceof Blob || (identifier === "name" || identifier === "lastModified") && typeof File !== "undefined" && object instanceof File;
48
+ if (!isSpecialIdentifier && (typeof object !== "object" || object === null || !Object.hasOwn(object, identifier))) {
46
49
  return {
47
50
  type: "notFound"
48
51
  };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getKeyPath = getKeyPath;
7
+ // Keys provided as functions or arrays or objects need to be stringified
8
+ const convertKey = key => typeof key === 'object' && key ? key + '' : key;
9
+
10
+ // https://www.w3.org/TR/IndexedDB/#dom-idbobjectstore-keypath
11
+ function getKeyPath(keyPath) {
12
+ // It's important to clone the Array here because of the WPT test:
13
+ // "Different instances are returned from different store instances."
14
+ // Also note that the same instance must be returned across multiple gets
15
+ return Array.isArray(keyPath) ? keyPath.map(convertKey) : convertKey(keyPath);
16
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _FDBKeyRange = _interopRequireDefault(require("../FDBKeyRange.js"));
8
+ var _isSharedArrayBuffer = _interopRequireDefault(require("./isSharedArrayBuffer.js"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ // https://www.w3.org/TR/IndexedDB/#is-a-potentially-valid-key-range
11
+ const isPotentiallyValidKeyRange = value => {
12
+ // Many of these conditions recapitulate the same conditions in `valueToKeyRange.ts`
13
+ return (
14
+ // FDBKeyRange
15
+ value instanceof _FDBKeyRange.default ||
16
+ // null/undefined - nullDisallowedFlag=false is assumed here
17
+ value === null || value === undefined ||
18
+ // Number
19
+ typeof value === "number" ||
20
+ // Date
21
+ Object.prototype.toString.call(value) === "[object Date]" ||
22
+ // string
23
+ typeof value === "string" ||
24
+ // buffer source type
25
+ // note: we are explicitly _not_ checking for detachedness here, to match Chromium's behavior
26
+ // see: https://github.com/w3c/IndexedDB/issues/465
27
+ value instanceof ArrayBuffer || (0, _isSharedArrayBuffer.default)(value) || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(value) ||
28
+ // array exotic type
29
+ Array.isArray(value)
30
+ );
31
+ };
32
+ var _default = exports.default = isPotentiallyValidKeyRange;
33
+ module.exports = exports.default;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = isSharedArrayBuffer;
7
+ function isSharedArrayBuffer(input) {
8
+ return typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer;
9
+ }
10
+ module.exports = exports.default;
@@ -23,6 +23,16 @@ function getSetImmediateFromJsdom() {
23
23
  }
24
24
  }
25
25
 
26
+ // waiting on this PR for typescript types: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1249
27
+
28
+ // 'user-blocking' runs right after microtasks, so equivalent to setTimeout but without the 4ms clamping
29
+ const schedulerPostTask = typeof scheduler !== "undefined" && (fn => scheduler.postTask(fn, {
30
+ priority: "user-blocking"
31
+ }));
32
+
33
+ // fallback for environments that don't support any of the above
34
+ const doSetTimeout = fn => setTimeout(fn, 0);
35
+
26
36
  // Schedules a task to run later. Use Node.js's setImmediate if available and
27
37
  // setTimeout otherwise. Note that options like process.nextTick or
28
38
  // queueMicrotask will likely not work: IndexedDB semantics require that
@@ -30,7 +40,7 @@ function getSetImmediateFromJsdom() {
30
40
  // tick queue and microtask queue run within the current event loop macrotask,
31
41
  // so they'd process database operations too quickly.
32
42
  const queueTask = fn => {
33
- const setImmediate = globalThis.setImmediate || getSetImmediateFromJsdom() || (fn => setTimeout(fn, 0));
43
+ const setImmediate = globalThis.setImmediate || getSetImmediateFromJsdom() || schedulerPostTask || doSetTimeout;
34
44
  setImmediate(fn);
35
45
  };
36
46
  exports.queueTask = queueTask;
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _errors = require("./errors.js");
7
8
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-valid-key-path
8
9
  const validateKeyPath = (keyPath, parent) => {
9
10
  // This doesn't make sense to me based on the spec, but it is needed to pass the W3C KeyPath tests (see same
@@ -25,16 +26,16 @@ const validateKeyPath = (keyPath, parent) => {
25
26
  return;
26
27
  }
27
28
  } catch (err) {
28
- throw new SyntaxError(err.message);
29
+ throw new _errors.SyntaxError(err.message);
29
30
  }
30
31
  if (keyPath.indexOf(" ") >= 0) {
31
- throw new SyntaxError("The keypath argument contains an invalid key path (no spaces allowed).");
32
+ throw new _errors.SyntaxError("The keypath argument contains an invalid key path (no spaces allowed).");
32
33
  }
33
34
  }
34
35
  if (Array.isArray(keyPath) && keyPath.length > 0) {
35
36
  if (parent) {
36
37
  // No nested arrays
37
- throw new SyntaxError("The keypath argument contains an invalid key path (nested arrays).");
38
+ throw new _errors.SyntaxError("The keypath argument contains an invalid key path (nested arrays).");
38
39
  }
39
40
  for (const part of keyPath) {
40
41
  validateKeyPath(part, "array");
@@ -47,7 +48,7 @@ const validateKeyPath = (keyPath, parent) => {
47
48
  }
48
49
  return;
49
50
  }
50
- throw new SyntaxError();
51
+ throw new _errors.SyntaxError();
51
52
  };
52
53
  var _default = exports.default = validateKeyPath;
53
54
  module.exports = exports.default;
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _errors = require("./errors.js");
8
+ var _isSharedArrayBuffer = _interopRequireDefault(require("./isSharedArrayBuffer.js"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
10
  // https://w3c.github.io/IndexedDB/#convert-value-to-key
9
11
  const valueToKey = (input, seen) => {
10
12
  if (typeof input === "number") {
@@ -20,11 +22,17 @@ const valueToKey = (input, seen) => {
20
22
  return new Date(ms);
21
23
  } else if (typeof input === "string") {
22
24
  return input;
23
- } else if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
25
+ } else if (
26
+ // https://w3c.github.io/IndexedDB/#ref-for-dfn-buffer-source-type
27
+ (input instanceof ArrayBuffer || (0, _isSharedArrayBuffer.default)(input) || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) &&
28
+ // We can't consistently test detachedness, so instead we check if byteLength === 0
29
+ // This isn't foolproof, but there's no perfect way to detect if Uint8Arrays or
30
+ // SharedArrayBuffers are detached
31
+ !("detached" in input ? input.detached : input.byteLength === 0)) {
24
32
  let arrayBuffer;
25
33
  let offset = 0;
26
34
  let length = 0;
27
- if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer) {
35
+ if (input instanceof ArrayBuffer || (0, _isSharedArrayBuffer.default)(input)) {
28
36
  arrayBuffer = input;
29
37
  length = input.byteLength;
30
38
  } else {
@@ -32,9 +40,6 @@ const valueToKey = (input, seen) => {
32
40
  offset = input.byteOffset;
33
41
  length = input.byteLength;
34
42
  }
35
- if (arrayBuffer.detached) {
36
- return new ArrayBuffer(0);
37
- }
38
43
  return arrayBuffer.slice(offset, offset + length);
39
44
  } else if (Array.isArray(input)) {
40
45
  if (seen === undefined) {
@@ -464,8 +464,8 @@ class FDBCursor {
464
464
  source: this
465
465
  });
466
466
  }
467
- toString() {
468
- return "[object IDBCursor]";
467
+ get [Symbol.toStringTag]() {
468
+ return "IDBCursor";
469
469
  }
470
470
  }
471
471
  export default FDBCursor;
@@ -4,8 +4,8 @@ class FDBCursorWithValue extends FDBCursor {
4
4
  constructor(source, range, direction, request) {
5
5
  super(source, range, direction, request);
6
6
  }
7
- toString() {
8
- return "[object IDBCursorWithValue]";
7
+ get [Symbol.toStringTag]() {
8
+ return "IDBCursorWithValue";
9
9
  }
10
10
  }
11
11
  export default FDBCursorWithValue;