fake-indexeddb 6.2.5 → 6.2.7

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.
package/README.md CHANGED
@@ -204,19 +204,19 @@ import "fake-indexeddb/auto";
204
204
 
205
205
  ## Quality
206
206
 
207
- Here's a comparison of fake-indexeddb and real browser IndexedDB implementations on [the Web Platform Tests IndexedDB suite](https://wpt.fyi/results/IndexedDB) as of <!-- last_updated_date_start -->November 7, 2025<!-- last_updated_date_end -->:
207
+ Here's a comparison of fake-indexeddb and real browser IndexedDB implementations on [the Web Platform Tests IndexedDB suite](https://wpt.fyi/results/IndexedDB) as of <!-- last_updated_date_start -->October 29, 2026<!-- last_updated_date_end -->:
208
208
 
209
209
  <!-- DO NOT MANUALLY OVERWRITE THE TABLE BELOW! -->
210
210
  <!-- To update the results, run `pnpm run update-wpt-results` -->
211
211
  <!-- wpt_results_start -->
212
- <!-- wpt_results_total=1653 -->
212
+ <!-- wpt_results_total=1678 -->
213
213
  | Implementation | Version | Passed | % |
214
214
  | --- | --- | --- | --- |
215
- | Chrome | 144.0.7514.0 | 1651 | 99.9% |
216
- | Firefox | 146.0a1 | 1498 | 90.6% |
217
- | Safari | 231 preview | 1497 | 90.6% |
218
- | Ladybird | 1.0-cde3941d9f | 1426 | 86.3% |
219
- | fake-indexeddb | 6.2.5 | 1369 | 82.8% |
215
+ | Chrome | 159.0.8130.0 | 1672 | 99.6% |
216
+ | Firefox | 161.0a1 | 1671 | 99.6% |
217
+ | Safari | 255 preview | 1676 | 99.9% |
218
+ | Ladybird | 1.0-28077c350dc | 1668 | 99.4% |
219
+ | fake-indexeddb | 6.2.7 | 1369 | 81.6% |
220
220
  <!-- wpt_results_end -->
221
221
 
222
222
  Keep in mind that these tests include a lot of edge cases (such as rare error conditions), so even hitting ~40% likely means that the core IndexedDB functionality is covered. Your app will probably work fine.
@@ -6,11 +6,12 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _FDBKeyRange = _interopRequireDefault(require("./FDBKeyRange.js"));
8
8
  var _FDBObjectStore = _interopRequireDefault(require("./FDBObjectStore.js"));
9
- var _cmp = _interopRequireDefault(require("./lib/cmp.js"));
9
+ var _cmp = _interopRequireWildcard(require("./lib/cmp.js"));
10
10
  var _errors = require("./lib/errors.js");
11
11
  var _extractKey = _interopRequireDefault(require("./lib/extractKey.js"));
12
12
  var _valueToKey = _interopRequireDefault(require("./lib/valueToKey.js"));
13
13
  var _cloneValueForInsertion = require("./lib/cloneValueForInsertion.js");
14
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
16
  const getEffectiveObjectStore = cursor => {
16
17
  if (cursor.source instanceof _FDBObjectStore.default) {
@@ -33,7 +34,7 @@ const makeKeyRange = (range, lowers, uppers) => {
33
34
  if (lowerTemp === undefined) {
34
35
  continue;
35
36
  }
36
- if (lower === undefined || (0, _cmp.default)(lower, lowerTemp) === 1) {
37
+ if (lower === undefined || (0, _cmp.cmpKeys)(lower, lowerTemp) === 1) {
37
38
  lower = lowerTemp;
38
39
  }
39
40
  }
@@ -41,7 +42,7 @@ const makeKeyRange = (range, lowers, uppers) => {
41
42
  if (upperTemp === undefined) {
42
43
  continue;
43
44
  }
44
- if (upper === undefined || (0, _cmp.default)(upper, upperTemp) === -1) {
45
+ if (upper === undefined || (0, _cmp.cmpKeys)(upper, upperTemp) === -1) {
45
46
  upper = upperTemp;
46
47
  }
47
48
  }
@@ -114,8 +115,8 @@ class FDBCursor {
114
115
  if (this.direction === "next") {
115
116
  const range = makeKeyRange(this._range, [key, this._position], []);
116
117
  for (const record of records.values(range)) {
117
- const cmpResultKey = key !== undefined ? (0, _cmp.default)(record.key, key) : undefined;
118
- const cmpResultPosition = this._position !== undefined ? (0, _cmp.default)(record.key, this._position) : undefined;
118
+ const cmpResultKey = key !== undefined ? (0, _cmp.cmpKeys)(record.key, key) : undefined;
119
+ const cmpResultPosition = this._position !== undefined ? (0, _cmp.cmpKeys)(record.key, this._position) : undefined;
119
120
  if (key !== undefined) {
120
121
  if (cmpResultKey === -1) {
121
122
  continue;
@@ -125,7 +126,7 @@ class FDBCursor {
125
126
  if (cmpResultKey === -1) {
126
127
  continue;
127
128
  }
128
- const cmpResultPrimaryKey = (0, _cmp.default)(record.value, primaryKey);
129
+ const cmpResultPrimaryKey = (0, _cmp.cmpKeys)(record.value, primaryKey);
129
130
  if (cmpResultKey === 0 && cmpResultPrimaryKey === -1) {
130
131
  continue;
131
132
  }
@@ -139,7 +140,7 @@ class FDBCursor {
139
140
  if (cmpResultPosition === -1) {
140
141
  continue;
141
142
  }
142
- if (cmpResultPosition === 0 && (0, _cmp.default)(record.value, this._objectStorePosition) !== 1) {
143
+ if (cmpResultPosition === 0 && (0, _cmp.cmpKeys)(record.value, this._objectStorePosition) !== 1) {
143
144
  continue;
144
145
  }
145
146
  }
@@ -158,12 +159,12 @@ class FDBCursor {
158
159
  const range = makeKeyRange(this._range, [key, this._position], []);
159
160
  for (const record of records.values(range)) {
160
161
  if (key !== undefined) {
161
- if ((0, _cmp.default)(record.key, key) === -1) {
162
+ if ((0, _cmp.cmpKeys)(record.key, key) === -1) {
162
163
  continue;
163
164
  }
164
165
  }
165
166
  if (this._position !== undefined) {
166
- if ((0, _cmp.default)(record.key, this._position) !== 1) {
167
+ if ((0, _cmp.cmpKeys)(record.key, this._position) !== 1) {
167
168
  continue;
168
169
  }
169
170
  }
@@ -178,8 +179,8 @@ class FDBCursor {
178
179
  } else if (this.direction === "prev") {
179
180
  const range = makeKeyRange(this._range, [], [key, this._position]);
180
181
  for (const record of records.values(range, "prev")) {
181
- const cmpResultKey = key !== undefined ? (0, _cmp.default)(record.key, key) : undefined;
182
- const cmpResultPosition = this._position !== undefined ? (0, _cmp.default)(record.key, this._position) : undefined;
182
+ const cmpResultKey = key !== undefined ? (0, _cmp.cmpKeys)(record.key, key) : undefined;
183
+ const cmpResultPosition = this._position !== undefined ? (0, _cmp.cmpKeys)(record.key, this._position) : undefined;
183
184
  if (key !== undefined) {
184
185
  if (cmpResultKey === 1) {
185
186
  continue;
@@ -189,7 +190,7 @@ class FDBCursor {
189
190
  if (cmpResultKey === 1) {
190
191
  continue;
191
192
  }
192
- const cmpResultPrimaryKey = (0, _cmp.default)(record.value, primaryKey);
193
+ const cmpResultPrimaryKey = (0, _cmp.cmpKeys)(record.value, primaryKey);
193
194
  if (cmpResultKey === 0 && cmpResultPrimaryKey === 1) {
194
195
  continue;
195
196
  }
@@ -203,7 +204,7 @@ class FDBCursor {
203
204
  if (cmpResultPosition === 1) {
204
205
  continue;
205
206
  }
206
- if (cmpResultPosition === 0 && (0, _cmp.default)(record.value, this._objectStorePosition) !== -1) {
207
+ if (cmpResultPosition === 0 && (0, _cmp.cmpKeys)(record.value, this._objectStorePosition) !== -1) {
207
208
  continue;
208
209
  }
209
210
  }
@@ -220,12 +221,12 @@ class FDBCursor {
220
221
  const range = makeKeyRange(this._range, [], [key, this._position]);
221
222
  for (const record of records.values(range, "prev")) {
222
223
  if (key !== undefined) {
223
- if ((0, _cmp.default)(record.key, key) === 1) {
224
+ if ((0, _cmp.cmpKeys)(record.key, key) === 1) {
224
225
  continue;
225
226
  }
226
227
  }
227
228
  if (this._position !== undefined) {
228
- if ((0, _cmp.default)(record.key, this._position) !== -1) {
229
+ if ((0, _cmp.cmpKeys)(record.key, this._position) !== -1) {
229
230
  continue;
230
231
  }
231
232
  }
@@ -387,7 +388,7 @@ class FDBCursor {
387
388
  }
388
389
  if (key !== undefined) {
389
390
  key = (0, _valueToKey.default)(key);
390
- const cmpResult = (0, _cmp.default)(key, this._position);
391
+ const cmpResult = (0, _cmp.cmpKeys)(key, this._position);
391
392
  if (cmpResult <= 0 && (this.direction === "next" || this.direction === "nextunique") || cmpResult >= 0 && (this.direction === "prev" || this.direction === "prevunique")) {
392
393
  throw new _errors.DataError();
393
394
  }
@@ -428,11 +429,12 @@ class FDBCursor {
428
429
  throw new _errors.DataError();
429
430
  }
430
431
  key = (0, _valueToKey.default)(key);
431
- const cmpResult = (0, _cmp.default)(key, this._position);
432
+ const cmpResult = (0, _cmp.cmpKeys)(key, this._position);
432
433
  if (cmpResult === -1 && this.direction === "next" || cmpResult === 1 && this.direction === "prev") {
433
434
  throw new _errors.DataError();
434
435
  }
435
- const cmpResult2 = (0, _cmp.default)(primaryKey, this._objectStorePosition);
436
+ primaryKey = (0, _valueToKey.default)(primaryKey);
437
+ const cmpResult2 = (0, _cmp.cmpKeys)(primaryKey, this._objectStorePosition);
436
438
  if (cmpResult === 0) {
437
439
  if (cmpResult2 <= 0 && this.direction === "next" || cmpResult2 >= 0 && this.direction === "prev") {
438
440
  throw new _errors.DataError();
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _cmp = _interopRequireDefault(require("./lib/cmp.js"));
7
+ var _cmp = require("./lib/cmp.js");
8
8
  var _errors = require("./lib/errors.js");
9
9
  var _valueToKey = _interopRequireDefault(require("./lib/valueToKey.js"));
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -35,12 +35,12 @@ class FDBKeyRange {
35
35
  if (arguments.length < 2) {
36
36
  throw new TypeError();
37
37
  }
38
- const cmpResult = (0, _cmp.default)(lower, upper);
38
+ lower = (0, _valueToKey.default)(lower);
39
+ upper = (0, _valueToKey.default)(upper);
40
+ const cmpResult = (0, _cmp.cmpKeys)(lower, upper);
39
41
  if (cmpResult === 1 || cmpResult === 0 && (lowerOpen || upperOpen)) {
40
42
  throw new _errors.DataError();
41
43
  }
42
- lower = (0, _valueToKey.default)(lower);
43
- upper = (0, _valueToKey.default)(upper);
44
44
  return new FDBKeyRange(lower, upper, lowerOpen, upperOpen);
45
45
  }
46
46
  constructor(lower, upper, lowerOpen, upperOpen) {
@@ -57,13 +57,13 @@ class FDBKeyRange {
57
57
  }
58
58
  key = (0, _valueToKey.default)(key);
59
59
  if (this.lower !== undefined) {
60
- const cmpResult = (0, _cmp.default)(this.lower, key);
60
+ const cmpResult = (0, _cmp.cmpKeys)(this.lower, key);
61
61
  if (cmpResult === 1 || cmpResult === 0 && this.lowerOpen) {
62
62
  return false;
63
63
  }
64
64
  }
65
65
  if (this.upper !== undefined) {
66
- const cmpResult = (0, _cmp.default)(this.upper, key);
66
+ const cmpResult = (0, _cmp.cmpKeys)(this.upper, key);
67
67
  if (cmpResult === -1 || cmpResult === 0 && this.upperOpen) {
68
68
  return false;
69
69
  }
@@ -118,7 +118,7 @@ class FDBTransaction extends _FakeEventTarget.default {
118
118
  request.result = undefined;
119
119
  }
120
120
  });
121
- this._state = "finished";
121
+ this._markFinished();
122
122
  }
123
123
  abort() {
124
124
  if (this._state === "committing" || this._state === "finished") {
@@ -251,13 +251,31 @@ class FDBTransaction extends _FakeEventTarget.default {
251
251
  // Check if transaction complete event needs to be fired
252
252
  if (this._state !== "finished") {
253
253
  // Either aborted or committed already
254
- this._state = "finished";
254
+ this._markFinished();
255
255
  if (!this.error) {
256
256
  const event = new _FakeEvent.default("complete");
257
257
  this.dispatchEvent(event);
258
258
  }
259
259
  }
260
260
  }
261
+ _markFinished() {
262
+ this._state = "finished";
263
+
264
+ // Remove from the list of transactions attached to the linked database to avoid
265
+ // indefinite memory bloat (issue #170)
266
+ // Note we do this in a macrotask since other consumers immediately check if it's in the array
267
+ (0, _scheduling.queueTask)(() => {
268
+ const {
269
+ transactions
270
+ } = this.db._rawDatabase;
271
+ const idx = transactions.indexOf(this);
272
+ if (idx !== -1) {
273
+ transactions.splice(idx, 1);
274
+ } else {
275
+ throw new Error("should have been able to remove transaction");
276
+ }
277
+ });
278
+ }
261
279
  commit() {
262
280
  if (this._state !== "active") {
263
281
  throw new _errors.InvalidStateError();
@@ -82,11 +82,14 @@ class Index {
82
82
  return records;
83
83
  }
84
84
 
85
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-storing-a-record-into-an-object-store (step 7)
86
- storeRecord(newRecord) {
85
+ // The index key that an object store record with this value is stored under, or undefined if the value does not
86
+ // belong in this index. For a multiEntry index given an array, this is the array of individual index keys (invalid
87
+ // and duplicate elements removed). Shared by storeRecord and deleteRecord so that deletion finds exactly what
88
+ // insertion stored.
89
+ getIndexKey(value) {
87
90
  let indexKey;
88
91
  try {
89
- indexKey = (0, _extractKey.default)(this.keyPath, newRecord.value).key;
92
+ indexKey = (0, _extractKey.default)(this.keyPath, value).key;
90
93
  } catch (err) {
91
94
  if (err.name === "DataError") {
92
95
  // Invalid key is not an actual error, just means we do not store an entry in this index
@@ -100,20 +103,29 @@ class Index {
100
103
  } catch (e) {
101
104
  return;
102
105
  }
103
- } else {
104
- // remove any elements from index key that are not valid keys and remove any duplicate elements from index
105
- // key such that only one instance of the duplicate value remains.
106
- const keep = [];
107
- for (const part of indexKey) {
108
- if (keep.indexOf(part) < 0) {
109
- try {
110
- keep.push((0, _valueToKey.default)(part));
111
- } catch (err) {
112
- /* Do nothing */
113
- }
106
+ return indexKey;
107
+ }
108
+
109
+ // remove any elements from index key that are not valid keys and remove any duplicate elements from index
110
+ // key such that only one instance of the duplicate value remains.
111
+ const keep = [];
112
+ for (const part of indexKey) {
113
+ if (keep.indexOf(part) < 0) {
114
+ try {
115
+ keep.push((0, _valueToKey.default)(part));
116
+ } catch (err) {
117
+ /* Do nothing */
114
118
  }
115
119
  }
116
- indexKey = keep;
120
+ }
121
+ return keep;
122
+ }
123
+
124
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-storing-a-record-into-an-object-store (step 7)
125
+ storeRecord(newRecord) {
126
+ const indexKey = this.getIndexKey(newRecord.value);
127
+ if (indexKey === undefined) {
128
+ return;
117
129
  }
118
130
  if (!this.multiEntry || !Array.isArray(indexKey)) {
119
131
  if (this.unique) {
@@ -122,6 +134,10 @@ class Index {
122
134
  throw new _errors.ConstraintError();
123
135
  }
124
136
  }
137
+ this.records.put({
138
+ key: indexKey,
139
+ value: newRecord.key
140
+ });
125
141
  } else {
126
142
  if (this.unique) {
127
143
  for (const individualIndexKey of indexKey) {
@@ -131,13 +147,6 @@ class Index {
131
147
  }
132
148
  }
133
149
  }
134
- }
135
- if (!this.multiEntry || !Array.isArray(indexKey)) {
136
- this.records.put({
137
- key: indexKey,
138
- value: newRecord.key
139
- });
140
- } else {
141
150
  for (const individualIndexKey of indexKey) {
142
151
  this.records.put({
143
152
  key: individualIndexKey,
@@ -146,6 +155,22 @@ class Index {
146
155
  }
147
156
  }
148
157
  }
158
+
159
+ // Remove the index records that storeRecord created for this object store record. Looking them up by index key is
160
+ // O(log n) per index record, where scanning the whole index for records pointing at this primary key is O(n).
161
+ deleteRecord(record) {
162
+ const indexKey = this.getIndexKey(record.value);
163
+ if (indexKey === undefined) {
164
+ return;
165
+ }
166
+ if (!this.multiEntry || !Array.isArray(indexKey)) {
167
+ this.records.deleteByKeyAndValue(indexKey, record.key);
168
+ } else {
169
+ for (const individualIndexKey of indexKey) {
170
+ this.records.deleteByKeyAndValue(individualIndexKey, record.key);
171
+ }
172
+ }
173
+ }
149
174
  initialize(transaction) {
150
175
  if (this.initialized) {
151
176
  throw new Error("Index already initialized");
@@ -176,7 +176,7 @@ class ObjectStore {
176
176
  // Delete existing indexes
177
177
  if (existingRecord) {
178
178
  for (const rawIndex of this.rawIndexes.values()) {
179
- rawIndex.records.deleteByValue(newRecord.key);
179
+ rawIndex.deleteRecord(existingRecord);
180
180
  }
181
181
  }
182
182
 
@@ -210,7 +210,9 @@ class ObjectStore {
210
210
  }
211
211
  }
212
212
  for (const rawIndex of this.rawIndexes.values()) {
213
- rawIndex.records.deleteByValue(key);
213
+ for (const record of deletedRecords) {
214
+ rawIndex.deleteRecord(record);
215
+ }
214
216
  }
215
217
  }
216
218
 
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _FDBKeyRange = _interopRequireDefault(require("../FDBKeyRange.js"));
8
- var _cmp = _interopRequireDefault(require("./cmp.js"));
8
+ var _cmp = require("./cmp.js");
9
9
  var _binarySearchTree = _interopRequireDefault(require("./binarySearchTree.js"));
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
11
  class RecordStore {
@@ -34,16 +34,21 @@ class RecordStore {
34
34
  }
35
35
  return deletedRecords;
36
36
  }
37
- deleteByValue(key) {
38
- const range = key instanceof _FDBKeyRange.default ? key : _FDBKeyRange.default.only(key);
39
- const deletedRecords = [];
40
- for (const record of this.records.getAllRecords()) {
41
- if (range.includes(record.value)) {
42
- this.records.delete(record);
43
- deletedRecords.push(record);
44
- }
37
+
38
+ /**
39
+ * Delete the record with this key and this value, if there is one. Used by indexes, where the value is the primary
40
+ * key of the object store record that the index record points to.
41
+ */
42
+ deleteByKeyAndValue(key, value) {
43
+ // In a non-unique store the value is part of the sort order, so this finds the exact record. In a unique store
44
+ // it finds the only record with this key, which may point at a different primary key.
45
+ const record = this.records.get({
46
+ key,
47
+ value
48
+ });
49
+ if (record !== undefined && (0, _cmp.cmpKeys)(record.value, value) === 0) {
50
+ this.records.delete(record);
45
51
  }
46
- return deletedRecords;
47
52
  }
48
53
  clear() {
49
54
  const deletedRecords = [...this.records.getAllRecords()];
@@ -75,7 +80,7 @@ class RecordStore {
75
80
  next: () => {
76
81
  let current = next();
77
82
  // for nextunique, continue if we already emitted the lowest unique value
78
- while (!current.done && previousValue !== undefined && (0, _cmp.default)(previousValue.key, current.value.key) === 0) {
83
+ while (!current.done && previousValue !== undefined && (0, _cmp.cmpKeys)(previousValue.key, current.value.key) === 0) {
79
84
  current = next();
80
85
  }
81
86
  previousValue = current.value;
@@ -90,7 +95,7 @@ class RecordStore {
90
95
  let nextResult = next();
91
96
  return {
92
97
  next: () => {
93
- while (!nextResult.done && (0, _cmp.default)(current.value.key, nextResult.value.key) === 0) {
98
+ while (!nextResult.done && (0, _cmp.cmpKeys)(current.value.key, nextResult.value.key) === 0) {
94
99
  // note we return the _lowest_ possible value, hence set the current
95
100
  current = nextResult;
96
101
  nextResult = next();
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _FDBKeyRange = _interopRequireDefault(require("../FDBKeyRange.js"));
8
- var _cmp = _interopRequireDefault(require("./cmp.js"));
8
+ var _cmp = require("./cmp.js");
9
9
  var _errors = require("./errors.js");
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
11
  // what fraction of the total number of nodes are allowed to be deleted tombstones?
@@ -45,13 +45,13 @@ class BinarySearchTree {
45
45
  return !!this.get(record);
46
46
  }
47
47
  _compare(a, b) {
48
- const keyComparison = (0, _cmp.default)(a.key, b.key);
48
+ const keyComparison = (0, _cmp.cmpKeys)(a.key, b.key);
49
49
  if (keyComparison !== 0) {
50
50
  return keyComparison;
51
51
  }
52
52
  // if keys are unique, then we can (and must) avoid comparing the values, since they may be non-comparable
53
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);
54
+ return this._keysAreUnique ? 0 : (0, _cmp.cmpKeys)(a.value, b.value);
55
55
  }
56
56
  _getByComparator(node, comparator) {
57
57
  let current = node;
@@ -186,8 +186,8 @@ class BinarySearchTree {
186
186
  key
187
187
  }
188
188
  } = node;
189
- const lowerComparison = lower === undefined ? -1 : (0, _cmp.default)(lower, key);
190
- const upperComparison = upper === undefined ? 1 : (0, _cmp.default)(upper, key);
189
+ const lowerComparison = lower === undefined ? -1 : (0, _cmp.cmpKeys)(lower, key);
190
+ const upperComparison = upper === undefined ? 1 : (0, _cmp.cmpKeys)(upper, key);
191
191
 
192
192
  // if keys are non-unique then we need to go left/right even for equality
193
193
  // else we can just do LT/GT rather than LTE/GTE as a slight optimization
@@ -280,14 +280,12 @@ class BinarySearchTree {
280
280
  }
281
281
 
282
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) {
283
+ _rebuild(records, parent, red, startIndex = 0, endIndex = records.length) {
284
+ const length = endIndex - startIndex;
285
+ if (length <= 0) {
288
286
  return undefined;
289
287
  }
290
- const mid = length >>> 1; // like Math.floor(records.length / 2) but fast
288
+ const mid = startIndex + (length >>> 1); // the >>> is like Math.floor(length / 2) but fast
291
289
 
292
290
  const node = {
293
291
  record: records[mid],
@@ -297,8 +295,8 @@ class BinarySearchTree {
297
295
  deleted: false,
298
296
  red
299
297
  };
300
- const left = this._rebuild(records.slice(0, mid), node, !red);
301
- const right = this._rebuild(records.slice(mid + 1), node, !red);
298
+ const left = this._rebuild(records, node, !red, startIndex, mid);
299
+ const right = this._rebuild(records, node, !red, mid + 1, endIndex);
302
300
  node.left = left;
303
301
  node.right = right;
304
302
  return node;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.cmpKeys = void 0;
7
7
  var _errors = require("./errors.js");
8
8
  var _valueToKey = _interopRequireDefault(require("./valueToKey.js"));
9
9
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -26,13 +26,15 @@ const getType = x => {
26
26
  throw new _errors.DataError();
27
27
  };
28
28
 
29
- // https://w3c.github.io/IndexedDB/#compare-two-keys
30
- const cmp = (first, second) => {
31
- if (second === undefined) {
32
- throw new TypeError();
33
- }
34
- first = (0, _valueToKey.default)(first);
35
- second = (0, _valueToKey.default)(second);
29
+ /**
30
+ * Compare two already-normalized IndexedDB keys (i.e. keys that have already
31
+ * been run through `valueToKey`). Use this on internal/hot paths where keys
32
+ * are known to be valid to avoid redundant `valueToKey` conversions.
33
+ *
34
+ * For comparing raw/untrusted input, use the default-exported `cmp` instead —
35
+ * it validates and normalizes both arguments before comparing.
36
+ */
37
+ const cmpKeys = (first, second) => {
36
38
  const t1 = getType(first);
37
39
  const t2 = getType(second);
38
40
  if (t1 !== t2) {
@@ -50,14 +52,34 @@ const cmp = (first, second) => {
50
52
  }
51
53
  return -1;
52
54
  }
55
+
56
+ // This block is effectively the same as the array comparison, but we avoid expensive `cmpKey` calls
53
57
  if (t1 === "Binary") {
54
- first = new Uint8Array(first);
55
- second = new Uint8Array(second);
58
+ const firstBytes = new Uint8Array(first);
59
+ const secondBytes = new Uint8Array(second);
60
+ const firstLen = firstBytes.length;
61
+ const secondLen = secondBytes.length;
62
+ const length = Math.min(firstLen, secondLen);
63
+ for (let i = 0; i < length; i++) {
64
+ if (firstBytes[i] > secondBytes[i]) {
65
+ return 1;
66
+ }
67
+ if (firstBytes[i] < secondBytes[i]) {
68
+ return -1;
69
+ }
70
+ }
71
+ if (firstLen > secondLen) {
72
+ return 1;
73
+ }
74
+ if (firstLen < secondLen) {
75
+ return -1;
76
+ }
77
+ return 0;
56
78
  }
57
- if (t1 === "Array" || t1 === "Binary") {
79
+ if (t1 === "Array") {
58
80
  const length = Math.min(first.length, second.length);
59
81
  for (let i = 0; i < length; i++) {
60
- const result = cmp(first[i], second[i]);
82
+ const result = cmpKeys(first[i], second[i]);
61
83
  if (result !== 0) {
62
84
  return result;
63
85
  }
@@ -81,5 +103,13 @@ const cmp = (first, second) => {
81
103
  }
82
104
  return first > second ? 1 : -1;
83
105
  };
84
- var _default = exports.default = cmp;
85
- module.exports = exports.default;
106
+
107
+ // https://w3c.github.io/IndexedDB/#compare-two-keys
108
+ exports.cmpKeys = cmpKeys;
109
+ const cmp = (first, second) => {
110
+ if (second === undefined) {
111
+ throw new TypeError();
112
+ }
113
+ return cmpKeys((0, _valueToKey.default)(first), (0, _valueToKey.default)(second));
114
+ };
115
+ var _default = exports.default = cmp;
@@ -1,6 +1,6 @@
1
1
  import FDBKeyRange from "./FDBKeyRange.js";
2
2
  import FDBObjectStore from "./FDBObjectStore.js";
3
- import cmp from "./lib/cmp.js";
3
+ import cmp, { cmpKeys } from "./lib/cmp.js";
4
4
  import { DataError, InvalidAccessError, InvalidStateError, ReadOnlyError, TransactionInactiveError } from "./lib/errors.js";
5
5
  import extractKey from "./lib/extractKey.js";
6
6
  import valueToKey from "./lib/valueToKey.js";
@@ -26,7 +26,7 @@ const makeKeyRange = (range, lowers, uppers) => {
26
26
  if (lowerTemp === undefined) {
27
27
  continue;
28
28
  }
29
- if (lower === undefined || cmp(lower, lowerTemp) === 1) {
29
+ if (lower === undefined || cmpKeys(lower, lowerTemp) === 1) {
30
30
  lower = lowerTemp;
31
31
  }
32
32
  }
@@ -34,7 +34,7 @@ const makeKeyRange = (range, lowers, uppers) => {
34
34
  if (upperTemp === undefined) {
35
35
  continue;
36
36
  }
37
- if (upper === undefined || cmp(upper, upperTemp) === -1) {
37
+ if (upper === undefined || cmpKeys(upper, upperTemp) === -1) {
38
38
  upper = upperTemp;
39
39
  }
40
40
  }
@@ -107,8 +107,8 @@ class FDBCursor {
107
107
  if (this.direction === "next") {
108
108
  const range = makeKeyRange(this._range, [key, this._position], []);
109
109
  for (const record of records.values(range)) {
110
- const cmpResultKey = key !== undefined ? cmp(record.key, key) : undefined;
111
- const cmpResultPosition = this._position !== undefined ? cmp(record.key, this._position) : undefined;
110
+ const cmpResultKey = key !== undefined ? cmpKeys(record.key, key) : undefined;
111
+ const cmpResultPosition = this._position !== undefined ? cmpKeys(record.key, this._position) : undefined;
112
112
  if (key !== undefined) {
113
113
  if (cmpResultKey === -1) {
114
114
  continue;
@@ -118,7 +118,7 @@ class FDBCursor {
118
118
  if (cmpResultKey === -1) {
119
119
  continue;
120
120
  }
121
- const cmpResultPrimaryKey = cmp(record.value, primaryKey);
121
+ const cmpResultPrimaryKey = cmpKeys(record.value, primaryKey);
122
122
  if (cmpResultKey === 0 && cmpResultPrimaryKey === -1) {
123
123
  continue;
124
124
  }
@@ -132,7 +132,7 @@ class FDBCursor {
132
132
  if (cmpResultPosition === -1) {
133
133
  continue;
134
134
  }
135
- if (cmpResultPosition === 0 && cmp(record.value, this._objectStorePosition) !== 1) {
135
+ if (cmpResultPosition === 0 && cmpKeys(record.value, this._objectStorePosition) !== 1) {
136
136
  continue;
137
137
  }
138
138
  }
@@ -151,12 +151,12 @@ class FDBCursor {
151
151
  const range = makeKeyRange(this._range, [key, this._position], []);
152
152
  for (const record of records.values(range)) {
153
153
  if (key !== undefined) {
154
- if (cmp(record.key, key) === -1) {
154
+ if (cmpKeys(record.key, key) === -1) {
155
155
  continue;
156
156
  }
157
157
  }
158
158
  if (this._position !== undefined) {
159
- if (cmp(record.key, this._position) !== 1) {
159
+ if (cmpKeys(record.key, this._position) !== 1) {
160
160
  continue;
161
161
  }
162
162
  }
@@ -171,8 +171,8 @@ class FDBCursor {
171
171
  } else if (this.direction === "prev") {
172
172
  const range = makeKeyRange(this._range, [], [key, this._position]);
173
173
  for (const record of records.values(range, "prev")) {
174
- const cmpResultKey = key !== undefined ? cmp(record.key, key) : undefined;
175
- const cmpResultPosition = this._position !== undefined ? cmp(record.key, this._position) : undefined;
174
+ const cmpResultKey = key !== undefined ? cmpKeys(record.key, key) : undefined;
175
+ const cmpResultPosition = this._position !== undefined ? cmpKeys(record.key, this._position) : undefined;
176
176
  if (key !== undefined) {
177
177
  if (cmpResultKey === 1) {
178
178
  continue;
@@ -182,7 +182,7 @@ class FDBCursor {
182
182
  if (cmpResultKey === 1) {
183
183
  continue;
184
184
  }
185
- const cmpResultPrimaryKey = cmp(record.value, primaryKey);
185
+ const cmpResultPrimaryKey = cmpKeys(record.value, primaryKey);
186
186
  if (cmpResultKey === 0 && cmpResultPrimaryKey === 1) {
187
187
  continue;
188
188
  }
@@ -196,7 +196,7 @@ class FDBCursor {
196
196
  if (cmpResultPosition === 1) {
197
197
  continue;
198
198
  }
199
- if (cmpResultPosition === 0 && cmp(record.value, this._objectStorePosition) !== -1) {
199
+ if (cmpResultPosition === 0 && cmpKeys(record.value, this._objectStorePosition) !== -1) {
200
200
  continue;
201
201
  }
202
202
  }
@@ -213,12 +213,12 @@ class FDBCursor {
213
213
  const range = makeKeyRange(this._range, [], [key, this._position]);
214
214
  for (const record of records.values(range, "prev")) {
215
215
  if (key !== undefined) {
216
- if (cmp(record.key, key) === 1) {
216
+ if (cmpKeys(record.key, key) === 1) {
217
217
  continue;
218
218
  }
219
219
  }
220
220
  if (this._position !== undefined) {
221
- if (cmp(record.key, this._position) !== -1) {
221
+ if (cmpKeys(record.key, this._position) !== -1) {
222
222
  continue;
223
223
  }
224
224
  }
@@ -380,7 +380,7 @@ class FDBCursor {
380
380
  }
381
381
  if (key !== undefined) {
382
382
  key = valueToKey(key);
383
- const cmpResult = cmp(key, this._position);
383
+ const cmpResult = cmpKeys(key, this._position);
384
384
  if (cmpResult <= 0 && (this.direction === "next" || this.direction === "nextunique") || cmpResult >= 0 && (this.direction === "prev" || this.direction === "prevunique")) {
385
385
  throw new DataError();
386
386
  }
@@ -421,11 +421,12 @@ class FDBCursor {
421
421
  throw new DataError();
422
422
  }
423
423
  key = valueToKey(key);
424
- const cmpResult = cmp(key, this._position);
424
+ const cmpResult = cmpKeys(key, this._position);
425
425
  if (cmpResult === -1 && this.direction === "next" || cmpResult === 1 && this.direction === "prev") {
426
426
  throw new DataError();
427
427
  }
428
- const cmpResult2 = cmp(primaryKey, this._objectStorePosition);
428
+ primaryKey = valueToKey(primaryKey);
429
+ const cmpResult2 = cmpKeys(primaryKey, this._objectStorePosition);
429
430
  if (cmpResult === 0) {
430
431
  if (cmpResult2 <= 0 && this.direction === "next" || cmpResult2 >= 0 && this.direction === "prev") {
431
432
  throw new DataError();
@@ -1,4 +1,4 @@
1
- import cmp from "./lib/cmp.js";
1
+ import { cmpKeys } from "./lib/cmp.js";
2
2
  import { DataError } from "./lib/errors.js";
3
3
  import valueToKey from "./lib/valueToKey.js";
4
4
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#range-concept
@@ -28,12 +28,12 @@ class FDBKeyRange {
28
28
  if (arguments.length < 2) {
29
29
  throw new TypeError();
30
30
  }
31
- const cmpResult = cmp(lower, upper);
31
+ lower = valueToKey(lower);
32
+ upper = valueToKey(upper);
33
+ const cmpResult = cmpKeys(lower, upper);
32
34
  if (cmpResult === 1 || cmpResult === 0 && (lowerOpen || upperOpen)) {
33
35
  throw new DataError();
34
36
  }
35
- lower = valueToKey(lower);
36
- upper = valueToKey(upper);
37
37
  return new FDBKeyRange(lower, upper, lowerOpen, upperOpen);
38
38
  }
39
39
  constructor(lower, upper, lowerOpen, upperOpen) {
@@ -50,13 +50,13 @@ class FDBKeyRange {
50
50
  }
51
51
  key = valueToKey(key);
52
52
  if (this.lower !== undefined) {
53
- const cmpResult = cmp(this.lower, key);
53
+ const cmpResult = cmpKeys(this.lower, key);
54
54
  if (cmpResult === 1 || cmpResult === 0 && this.lowerOpen) {
55
55
  return false;
56
56
  }
57
57
  }
58
58
  if (this.upper !== undefined) {
59
- const cmpResult = cmp(this.upper, key);
59
+ const cmpResult = cmpKeys(this.upper, key);
60
60
  if (cmpResult === -1 || cmpResult === 0 && this.upperOpen) {
61
61
  return false;
62
62
  }
@@ -111,7 +111,7 @@ class FDBTransaction extends FakeEventTarget {
111
111
  request.result = undefined;
112
112
  }
113
113
  });
114
- this._state = "finished";
114
+ this._markFinished();
115
115
  }
116
116
  abort() {
117
117
  if (this._state === "committing" || this._state === "finished") {
@@ -244,13 +244,31 @@ class FDBTransaction extends FakeEventTarget {
244
244
  // Check if transaction complete event needs to be fired
245
245
  if (this._state !== "finished") {
246
246
  // Either aborted or committed already
247
- this._state = "finished";
247
+ this._markFinished();
248
248
  if (!this.error) {
249
249
  const event = new FakeEvent("complete");
250
250
  this.dispatchEvent(event);
251
251
  }
252
252
  }
253
253
  }
254
+ _markFinished() {
255
+ this._state = "finished";
256
+
257
+ // Remove from the list of transactions attached to the linked database to avoid
258
+ // indefinite memory bloat (issue #170)
259
+ // Note we do this in a macrotask since other consumers immediately check if it's in the array
260
+ queueTask(() => {
261
+ const {
262
+ transactions
263
+ } = this.db._rawDatabase;
264
+ const idx = transactions.indexOf(this);
265
+ if (idx !== -1) {
266
+ transactions.splice(idx, 1);
267
+ } else {
268
+ throw new Error("should have been able to remove transaction");
269
+ }
270
+ });
271
+ }
254
272
  commit() {
255
273
  if (this._state !== "active") {
256
274
  throw new InvalidStateError();
@@ -75,11 +75,14 @@ class Index {
75
75
  return records;
76
76
  }
77
77
 
78
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-storing-a-record-into-an-object-store (step 7)
79
- storeRecord(newRecord) {
78
+ // The index key that an object store record with this value is stored under, or undefined if the value does not
79
+ // belong in this index. For a multiEntry index given an array, this is the array of individual index keys (invalid
80
+ // and duplicate elements removed). Shared by storeRecord and deleteRecord so that deletion finds exactly what
81
+ // insertion stored.
82
+ getIndexKey(value) {
80
83
  let indexKey;
81
84
  try {
82
- indexKey = extractKey(this.keyPath, newRecord.value).key;
85
+ indexKey = extractKey(this.keyPath, value).key;
83
86
  } catch (err) {
84
87
  if (err.name === "DataError") {
85
88
  // Invalid key is not an actual error, just means we do not store an entry in this index
@@ -93,20 +96,29 @@ class Index {
93
96
  } catch (e) {
94
97
  return;
95
98
  }
96
- } else {
97
- // remove any elements from index key that are not valid keys and remove any duplicate elements from index
98
- // key such that only one instance of the duplicate value remains.
99
- const keep = [];
100
- for (const part of indexKey) {
101
- if (keep.indexOf(part) < 0) {
102
- try {
103
- keep.push(valueToKey(part));
104
- } catch (err) {
105
- /* Do nothing */
106
- }
99
+ return indexKey;
100
+ }
101
+
102
+ // remove any elements from index key that are not valid keys and remove any duplicate elements from index
103
+ // key such that only one instance of the duplicate value remains.
104
+ const keep = [];
105
+ for (const part of indexKey) {
106
+ if (keep.indexOf(part) < 0) {
107
+ try {
108
+ keep.push(valueToKey(part));
109
+ } catch (err) {
110
+ /* Do nothing */
107
111
  }
108
112
  }
109
- indexKey = keep;
113
+ }
114
+ return keep;
115
+ }
116
+
117
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-storing-a-record-into-an-object-store (step 7)
118
+ storeRecord(newRecord) {
119
+ const indexKey = this.getIndexKey(newRecord.value);
120
+ if (indexKey === undefined) {
121
+ return;
110
122
  }
111
123
  if (!this.multiEntry || !Array.isArray(indexKey)) {
112
124
  if (this.unique) {
@@ -115,6 +127,10 @@ class Index {
115
127
  throw new ConstraintError();
116
128
  }
117
129
  }
130
+ this.records.put({
131
+ key: indexKey,
132
+ value: newRecord.key
133
+ });
118
134
  } else {
119
135
  if (this.unique) {
120
136
  for (const individualIndexKey of indexKey) {
@@ -124,13 +140,6 @@ class Index {
124
140
  }
125
141
  }
126
142
  }
127
- }
128
- if (!this.multiEntry || !Array.isArray(indexKey)) {
129
- this.records.put({
130
- key: indexKey,
131
- value: newRecord.key
132
- });
133
- } else {
134
143
  for (const individualIndexKey of indexKey) {
135
144
  this.records.put({
136
145
  key: individualIndexKey,
@@ -139,6 +148,22 @@ class Index {
139
148
  }
140
149
  }
141
150
  }
151
+
152
+ // Remove the index records that storeRecord created for this object store record. Looking them up by index key is
153
+ // O(log n) per index record, where scanning the whole index for records pointing at this primary key is O(n).
154
+ deleteRecord(record) {
155
+ const indexKey = this.getIndexKey(record.value);
156
+ if (indexKey === undefined) {
157
+ return;
158
+ }
159
+ if (!this.multiEntry || !Array.isArray(indexKey)) {
160
+ this.records.deleteByKeyAndValue(indexKey, record.key);
161
+ } else {
162
+ for (const individualIndexKey of indexKey) {
163
+ this.records.deleteByKeyAndValue(individualIndexKey, record.key);
164
+ }
165
+ }
166
+ }
142
167
  initialize(transaction) {
143
168
  if (this.initialized) {
144
169
  throw new Error("Index already initialized");
@@ -169,7 +169,7 @@ class ObjectStore {
169
169
  // Delete existing indexes
170
170
  if (existingRecord) {
171
171
  for (const rawIndex of this.rawIndexes.values()) {
172
- rawIndex.records.deleteByValue(newRecord.key);
172
+ rawIndex.deleteRecord(existingRecord);
173
173
  }
174
174
  }
175
175
 
@@ -203,7 +203,9 @@ class ObjectStore {
203
203
  }
204
204
  }
205
205
  for (const rawIndex of this.rawIndexes.values()) {
206
- rawIndex.records.deleteByValue(key);
206
+ for (const record of deletedRecords) {
207
+ rawIndex.deleteRecord(record);
208
+ }
207
209
  }
208
210
  }
209
211
 
@@ -1,5 +1,5 @@
1
1
  import FDBKeyRange from "../FDBKeyRange.js";
2
- import cmp from "./cmp.js";
2
+ import { cmpKeys } from "./cmp.js";
3
3
  import BinarySearchTree from "./binarySearchTree.js";
4
4
  class RecordStore {
5
5
  constructor(keysAreUnique) {
@@ -27,16 +27,21 @@ class RecordStore {
27
27
  }
28
28
  return deletedRecords;
29
29
  }
30
- deleteByValue(key) {
31
- const range = key instanceof FDBKeyRange ? key : FDBKeyRange.only(key);
32
- const deletedRecords = [];
33
- for (const record of this.records.getAllRecords()) {
34
- if (range.includes(record.value)) {
35
- this.records.delete(record);
36
- deletedRecords.push(record);
37
- }
30
+
31
+ /**
32
+ * Delete the record with this key and this value, if there is one. Used by indexes, where the value is the primary
33
+ * key of the object store record that the index record points to.
34
+ */
35
+ deleteByKeyAndValue(key, value) {
36
+ // In a non-unique store the value is part of the sort order, so this finds the exact record. In a unique store
37
+ // it finds the only record with this key, which may point at a different primary key.
38
+ const record = this.records.get({
39
+ key,
40
+ value
41
+ });
42
+ if (record !== undefined && cmpKeys(record.value, value) === 0) {
43
+ this.records.delete(record);
38
44
  }
39
- return deletedRecords;
40
45
  }
41
46
  clear() {
42
47
  const deletedRecords = [...this.records.getAllRecords()];
@@ -68,7 +73,7 @@ class RecordStore {
68
73
  next: () => {
69
74
  let current = next();
70
75
  // for nextunique, continue if we already emitted the lowest unique value
71
- while (!current.done && previousValue !== undefined && cmp(previousValue.key, current.value.key) === 0) {
76
+ while (!current.done && previousValue !== undefined && cmpKeys(previousValue.key, current.value.key) === 0) {
72
77
  current = next();
73
78
  }
74
79
  previousValue = current.value;
@@ -83,7 +88,7 @@ class RecordStore {
83
88
  let nextResult = next();
84
89
  return {
85
90
  next: () => {
86
- while (!nextResult.done && cmp(current.value.key, nextResult.value.key) === 0) {
91
+ while (!nextResult.done && cmpKeys(current.value.key, nextResult.value.key) === 0) {
87
92
  // note we return the _lowest_ possible value, hence set the current
88
93
  current = nextResult;
89
94
  nextResult = next();
@@ -1,5 +1,5 @@
1
1
  import FDBKeyRange from "../FDBKeyRange.js";
2
- import cmp from "./cmp.js";
2
+ import { cmpKeys } from "./cmp.js";
3
3
  import { ConstraintError } from "./errors.js";
4
4
  // what fraction of the total number of nodes are allowed to be deleted tombstones?
5
5
  const MAX_TOMBSTONE_FACTOR = 2 / 3;
@@ -38,13 +38,13 @@ export default class BinarySearchTree {
38
38
  return !!this.get(record);
39
39
  }
40
40
  _compare(a, b) {
41
- const keyComparison = cmp(a.key, b.key);
41
+ const keyComparison = cmpKeys(a.key, b.key);
42
42
  if (keyComparison !== 0) {
43
43
  return keyComparison;
44
44
  }
45
45
  // if keys are unique, then we can (and must) avoid comparing the values, since they may be non-comparable
46
46
  // (e.g. in the case of an ObjectStore, they are record objects)
47
- return this._keysAreUnique ? 0 : cmp(a.value, b.value);
47
+ return this._keysAreUnique ? 0 : cmpKeys(a.value, b.value);
48
48
  }
49
49
  _getByComparator(node, comparator) {
50
50
  let current = node;
@@ -179,8 +179,8 @@ export default class BinarySearchTree {
179
179
  key
180
180
  }
181
181
  } = node;
182
- const lowerComparison = lower === undefined ? -1 : cmp(lower, key);
183
- const upperComparison = upper === undefined ? 1 : cmp(upper, key);
182
+ const lowerComparison = lower === undefined ? -1 : cmpKeys(lower, key);
183
+ const upperComparison = upper === undefined ? 1 : cmpKeys(upper, key);
184
184
 
185
185
  // if keys are non-unique then we need to go left/right even for equality
186
186
  // else we can just do LT/GT rather than LTE/GTE as a slight optimization
@@ -273,14 +273,12 @@ export default class BinarySearchTree {
273
273
  }
274
274
 
275
275
  // rebuild the whole tree from scratch, used to avoid too many deletion tombstones accumulating
276
- _rebuild(records, parent, red) {
277
- const {
278
- length
279
- } = records;
280
- if (!length) {
276
+ _rebuild(records, parent, red, startIndex = 0, endIndex = records.length) {
277
+ const length = endIndex - startIndex;
278
+ if (length <= 0) {
281
279
  return undefined;
282
280
  }
283
- const mid = length >>> 1; // like Math.floor(records.length / 2) but fast
281
+ const mid = startIndex + (length >>> 1); // the >>> is like Math.floor(length / 2) but fast
284
282
 
285
283
  const node = {
286
284
  record: records[mid],
@@ -290,8 +288,8 @@ export default class BinarySearchTree {
290
288
  deleted: false,
291
289
  red
292
290
  };
293
- const left = this._rebuild(records.slice(0, mid), node, !red);
294
- const right = this._rebuild(records.slice(mid + 1), node, !red);
291
+ const left = this._rebuild(records, node, !red, startIndex, mid);
292
+ const right = this._rebuild(records, node, !red, mid + 1, endIndex);
295
293
  node.left = left;
296
294
  node.right = right;
297
295
  return node;
@@ -19,13 +19,15 @@ const getType = x => {
19
19
  throw new DataError();
20
20
  };
21
21
 
22
- // https://w3c.github.io/IndexedDB/#compare-two-keys
23
- const cmp = (first, second) => {
24
- if (second === undefined) {
25
- throw new TypeError();
26
- }
27
- first = valueToKey(first);
28
- second = valueToKey(second);
22
+ /**
23
+ * Compare two already-normalized IndexedDB keys (i.e. keys that have already
24
+ * been run through `valueToKey`). Use this on internal/hot paths where keys
25
+ * are known to be valid to avoid redundant `valueToKey` conversions.
26
+ *
27
+ * For comparing raw/untrusted input, use the default-exported `cmp` instead —
28
+ * it validates and normalizes both arguments before comparing.
29
+ */
30
+ export const cmpKeys = (first, second) => {
29
31
  const t1 = getType(first);
30
32
  const t2 = getType(second);
31
33
  if (t1 !== t2) {
@@ -43,14 +45,34 @@ const cmp = (first, second) => {
43
45
  }
44
46
  return -1;
45
47
  }
48
+
49
+ // This block is effectively the same as the array comparison, but we avoid expensive `cmpKey` calls
46
50
  if (t1 === "Binary") {
47
- first = new Uint8Array(first);
48
- second = new Uint8Array(second);
51
+ const firstBytes = new Uint8Array(first);
52
+ const secondBytes = new Uint8Array(second);
53
+ const firstLen = firstBytes.length;
54
+ const secondLen = secondBytes.length;
55
+ const length = Math.min(firstLen, secondLen);
56
+ for (let i = 0; i < length; i++) {
57
+ if (firstBytes[i] > secondBytes[i]) {
58
+ return 1;
59
+ }
60
+ if (firstBytes[i] < secondBytes[i]) {
61
+ return -1;
62
+ }
63
+ }
64
+ if (firstLen > secondLen) {
65
+ return 1;
66
+ }
67
+ if (firstLen < secondLen) {
68
+ return -1;
69
+ }
70
+ return 0;
49
71
  }
50
- if (t1 === "Array" || t1 === "Binary") {
72
+ if (t1 === "Array") {
51
73
  const length = Math.min(first.length, second.length);
52
74
  for (let i = 0; i < length; i++) {
53
- const result = cmp(first[i], second[i]);
75
+ const result = cmpKeys(first[i], second[i]);
54
76
  if (result !== 0) {
55
77
  return result;
56
78
  }
@@ -74,4 +96,12 @@ const cmp = (first, second) => {
74
96
  }
75
97
  return first > second ? 1 : -1;
76
98
  };
99
+
100
+ // https://w3c.github.io/IndexedDB/#compare-two-keys
101
+ const cmp = (first, second) => {
102
+ if (second === undefined) {
103
+ throw new TypeError();
104
+ }
105
+ return cmpKeys(valueToKey(first), valueToKey(second));
106
+ };
77
107
  export default cmp;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fake-indexeddb",
3
- "version": "6.2.5",
3
+ "version": "6.2.7",
4
4
  "description": "Fake IndexedDB: a pure JS in-memory implementation of the IndexedDB API",
5
5
  "homepage": "https://github.com/dumbmatter/fakeIndexedDB",
6
6
  "repository": {