fake-indexeddb 6.2.4 → 6.2.6
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 +7 -7
- package/build/cjs/FDBCursor.js +20 -18
- package/build/cjs/FDBFactory.js +7 -5
- package/build/cjs/FDBKeyRange.js +6 -6
- package/build/cjs/FDBTransaction.js +30 -2
- package/build/cjs/lib/FakeEventTarget.js +6 -3
- package/build/cjs/lib/RecordStore.js +3 -3
- package/build/cjs/lib/binarySearchTree.js +11 -13
- package/build/cjs/lib/cmp.js +44 -14
- package/build/cjs/test/fakeIndexedDB/auto.js +54 -0
- package/build/cjs/test/fakeIndexedDB/binarySearchTree.js +413 -0
- package/build/cjs/test/fakeIndexedDB/fakeIndexedDB.js +1018 -0
- package/build/cjs/test/fakeIndexedDB/forceCloseDatabase.js +99 -0
- package/build/cjs/test/fakeIndexedDB/getAll.js +707 -0
- package/build/cjs/test/fakeIndexedDB/memoryLeaks.js +42 -0
- package/build/esm/FDBCursor.js +19 -18
- package/build/esm/FDBFactory.js +7 -5
- package/build/esm/FDBKeyRange.js +6 -6
- package/build/esm/FDBTransaction.js +30 -2
- package/build/esm/lib/FakeEventTarget.js +6 -3
- package/build/esm/lib/RecordStore.js +3 -3
- package/build/esm/lib/binarySearchTree.js +11 -13
- package/build/esm/lib/cmp.js +41 -11
- package/build/esm/test/fakeIndexedDB/auto.js +52 -0
- package/build/esm/test/fakeIndexedDB/binarySearchTree.js +409 -0
- package/build/esm/test/fakeIndexedDB/fakeIndexedDB.js +1014 -0
- package/build/esm/test/fakeIndexedDB/forceCloseDatabase.js +95 -0
- package/build/esm/test/fakeIndexedDB/getAll.js +703 -0
- package/build/esm/test/fakeIndexedDB/memoryLeaks.js +38 -0
- package/package.json +1 -1
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var assert = _interopRequireWildcard(require("assert"));
|
|
4
|
+
var _fakeIndexedDB = _interopRequireDefault(require("../../fakeIndexedDB.js"));
|
|
5
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
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); }
|
|
7
|
+
const req = r => new Promise((res, rej) => {
|
|
8
|
+
r.onsuccess = () => res(r.result);
|
|
9
|
+
r.onerror = () => rej(r.error);
|
|
10
|
+
});
|
|
11
|
+
const openDb = async name => {
|
|
12
|
+
const open = _fakeIndexedDB.default.open(name, 1);
|
|
13
|
+
open.onupgradeneeded = () => open.result.createObjectStore("s");
|
|
14
|
+
return await req(open);
|
|
15
|
+
};
|
|
16
|
+
describe("memory leaks", () => {
|
|
17
|
+
it("does not leak memory in database.transactions (issue #170)", async () => {
|
|
18
|
+
const db = await openDb("test-mem-leaks");
|
|
19
|
+
for (let i = 0; i < 100; i++) {
|
|
20
|
+
const tx = db.transaction("s", "readwrite");
|
|
21
|
+
tx.objectStore("s").put("foo", "k" + i % 10);
|
|
22
|
+
await new Promise(r => {
|
|
23
|
+
tx.oncomplete = r;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// double macrotask to avoid race conditions in the test
|
|
28
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
29
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
30
|
+
assert.equal(db?._rawDatabase.transactions.length, 0, "Transactions should not be held onto indefinitely");
|
|
31
|
+
});
|
|
32
|
+
it("does not leak memory an aborted transaction (issue #170)", async () => {
|
|
33
|
+
const db = await openDb("test-mem-leaks-2");
|
|
34
|
+
const tx = db.transaction("s", "readwrite");
|
|
35
|
+
tx.abort();
|
|
36
|
+
|
|
37
|
+
// double macrotask to avoid race conditions in the test
|
|
38
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
39
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
40
|
+
assert.equal(db?._rawDatabase.transactions.length, 0, "Transactions should not be held onto indefinitely");
|
|
41
|
+
});
|
|
42
|
+
});
|
package/build/esm/FDBCursor.js
CHANGED
|
@@ -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 ||
|
|
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 ||
|
|
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 ?
|
|
111
|
-
const cmpResultPosition = 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 =
|
|
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 &&
|
|
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 (
|
|
154
|
+
if (cmpKeys(record.key, key) === -1) {
|
|
155
155
|
continue;
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
158
|
if (this._position !== undefined) {
|
|
159
|
-
if (
|
|
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 ?
|
|
175
|
-
const cmpResultPosition = 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 =
|
|
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 &&
|
|
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 (
|
|
216
|
+
if (cmpKeys(record.key, key) === 1) {
|
|
217
217
|
continue;
|
|
218
218
|
}
|
|
219
219
|
}
|
|
220
220
|
if (this._position !== undefined) {
|
|
221
|
-
if (
|
|
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 =
|
|
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 =
|
|
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
|
-
|
|
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();
|
package/build/esm/FDBFactory.js
CHANGED
|
@@ -214,26 +214,28 @@ const runVersionchangeTransaction = (connection, version, request, cb) => {
|
|
|
214
214
|
} else {
|
|
215
215
|
queueTask(concludeUpgrade);
|
|
216
216
|
}
|
|
217
|
-
transaction.
|
|
217
|
+
transaction._prioritizedListeners.set("error", () => {
|
|
218
218
|
connection._runningVersionchangeTransaction = false;
|
|
219
219
|
connection._oldVersion = undefined;
|
|
220
220
|
// throw arguments[0].target.error;
|
|
221
221
|
// console.log("error in versionchange transaction - not sure if anything needs to be done here", e.target.error.name);
|
|
222
222
|
});
|
|
223
|
-
transaction.
|
|
223
|
+
transaction._prioritizedListeners.set("abort", () => {
|
|
224
224
|
connection._runningVersionchangeTransaction = false;
|
|
225
225
|
connection._oldVersion = undefined;
|
|
226
|
-
request.transaction = null;
|
|
227
226
|
queueTask(() => {
|
|
227
|
+
// Reset transaction in a tick after onabort (upgrade-transaction-lifecycle-user-aborted.any)
|
|
228
|
+
request.transaction = null;
|
|
228
229
|
cb(new AbortError());
|
|
229
230
|
});
|
|
230
231
|
});
|
|
231
|
-
transaction.
|
|
232
|
+
transaction._prioritizedListeners.set("complete", () => {
|
|
232
233
|
connection._runningVersionchangeTransaction = false;
|
|
233
234
|
connection._oldVersion = undefined;
|
|
234
|
-
request.transaction = null;
|
|
235
235
|
// Let other complete event handlers run before continuing
|
|
236
236
|
queueTask(() => {
|
|
237
|
+
// Reset transaction in a tick after oncomplete (upgrade-transaction-lifecycle-committed.any.js)
|
|
238
|
+
request.transaction = null;
|
|
237
239
|
if (connection._closePending) {
|
|
238
240
|
cb(new AbortError());
|
|
239
241
|
} else {
|
package/build/esm/FDBKeyRange.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
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 =
|
|
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 =
|
|
59
|
+
const cmpResult = cmpKeys(this.upper, key);
|
|
60
60
|
if (cmpResult === -1 || cmpResult === 0 && this.upperOpen) {
|
|
61
61
|
return false;
|
|
62
62
|
}
|
|
@@ -5,6 +5,7 @@ import FakeDOMStringList from "./lib/FakeDOMStringList.js";
|
|
|
5
5
|
import FakeEvent from "./lib/FakeEvent.js";
|
|
6
6
|
import FakeEventTarget from "./lib/FakeEventTarget.js";
|
|
7
7
|
import { queueTask } from "./lib/scheduling.js";
|
|
8
|
+
const prioritizedListenerTypes = ["error", "abort", "complete"];
|
|
8
9
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#transaction
|
|
9
10
|
class FDBTransaction extends FakeEventTarget {
|
|
10
11
|
_state = "active";
|
|
@@ -16,6 +17,7 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
16
17
|
onabort = null;
|
|
17
18
|
oncomplete = null;
|
|
18
19
|
onerror = null;
|
|
20
|
+
_prioritizedListeners = new Map();
|
|
19
21
|
_requests = [];
|
|
20
22
|
_createdIndexes = new Set();
|
|
21
23
|
_createdObjectStores = new Set();
|
|
@@ -26,6 +28,14 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
26
28
|
this.durability = durability;
|
|
27
29
|
this.db = db;
|
|
28
30
|
this.objectStoreNames = new FakeDOMStringList(...Array.from(this._scope).sort());
|
|
31
|
+
for (const type of prioritizedListenerTypes) {
|
|
32
|
+
// Attach prioritized (internal) listeners before any external listeners are attached.
|
|
33
|
+
// This ensures that these listeners run with the same timing regardless of whether
|
|
34
|
+
// the user uses `on*` or `addEventListener` for event listeners.
|
|
35
|
+
this.addEventListener(type, () => {
|
|
36
|
+
this._prioritizedListeners.get(type)?.();
|
|
37
|
+
});
|
|
38
|
+
}
|
|
29
39
|
}
|
|
30
40
|
|
|
31
41
|
// https://w3c.github.io/IndexedDB/#abort-transaction
|
|
@@ -101,7 +111,7 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
101
111
|
request.result = undefined;
|
|
102
112
|
}
|
|
103
113
|
});
|
|
104
|
-
this.
|
|
114
|
+
this._markFinished();
|
|
105
115
|
}
|
|
106
116
|
abort() {
|
|
107
117
|
if (this._state === "committing" || this._state === "finished") {
|
|
@@ -234,13 +244,31 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
234
244
|
// Check if transaction complete event needs to be fired
|
|
235
245
|
if (this._state !== "finished") {
|
|
236
246
|
// Either aborted or committed already
|
|
237
|
-
this.
|
|
247
|
+
this._markFinished();
|
|
238
248
|
if (!this.error) {
|
|
239
249
|
const event = new FakeEvent("complete");
|
|
240
250
|
this.dispatchEvent(event);
|
|
241
251
|
}
|
|
242
252
|
}
|
|
243
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
|
+
}
|
|
244
272
|
commit() {
|
|
245
273
|
if (this._state !== "active") {
|
|
246
274
|
throw new InvalidStateError();
|
|
@@ -7,8 +7,9 @@ const stopped = (event, listener) => {
|
|
|
7
7
|
const invokeEventListeners = (event, obj) => {
|
|
8
8
|
event.currentTarget = obj;
|
|
9
9
|
const errors = [];
|
|
10
|
-
const invoke =
|
|
10
|
+
const invoke = callbackOrObject => {
|
|
11
11
|
try {
|
|
12
|
+
const callback = typeof callbackOrObject === "function" ? callbackOrObject : callbackOrObject.handleEvent;
|
|
12
13
|
// @ts-expect-error EventCallback's types are not quite right here
|
|
13
14
|
callback.call(event.currentTarget, event);
|
|
14
15
|
} catch (err) {
|
|
@@ -62,14 +63,16 @@ class FakeEventTarget {
|
|
|
62
63
|
|
|
63
64
|
// These will be overridden in individual subclasses and made not readonly
|
|
64
65
|
|
|
65
|
-
addEventListener(type, callback,
|
|
66
|
+
addEventListener(type, callback, options) {
|
|
67
|
+
const capture = !!(typeof options === "object" && options ? options.capture : options);
|
|
66
68
|
this.listeners.push({
|
|
67
69
|
callback,
|
|
68
70
|
capture,
|
|
69
71
|
type
|
|
70
72
|
});
|
|
71
73
|
}
|
|
72
|
-
removeEventListener(type, callback,
|
|
74
|
+
removeEventListener(type, callback, options) {
|
|
75
|
+
const capture = !!(typeof options === "object" && options ? options.capture : options);
|
|
73
76
|
const i = this.listeners.findIndex(listener => {
|
|
74
77
|
return listener.type === type && listener.callback === callback && listener.capture === capture;
|
|
75
78
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import FDBKeyRange from "../FDBKeyRange.js";
|
|
2
|
-
import
|
|
2
|
+
import { cmpKeys } from "./cmp.js";
|
|
3
3
|
import BinarySearchTree from "./binarySearchTree.js";
|
|
4
4
|
class RecordStore {
|
|
5
5
|
constructor(keysAreUnique) {
|
|
@@ -68,7 +68,7 @@ class RecordStore {
|
|
|
68
68
|
next: () => {
|
|
69
69
|
let current = next();
|
|
70
70
|
// for nextunique, continue if we already emitted the lowest unique value
|
|
71
|
-
while (!current.done && previousValue !== undefined &&
|
|
71
|
+
while (!current.done && previousValue !== undefined && cmpKeys(previousValue.key, current.value.key) === 0) {
|
|
72
72
|
current = next();
|
|
73
73
|
}
|
|
74
74
|
previousValue = current.value;
|
|
@@ -83,7 +83,7 @@ class RecordStore {
|
|
|
83
83
|
let nextResult = next();
|
|
84
84
|
return {
|
|
85
85
|
next: () => {
|
|
86
|
-
while (!nextResult.done &&
|
|
86
|
+
while (!nextResult.done && cmpKeys(current.value.key, nextResult.value.key) === 0) {
|
|
87
87
|
// note we return the _lowest_ possible value, hence set the current
|
|
88
88
|
current = nextResult;
|
|
89
89
|
nextResult = next();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import FDBKeyRange from "../FDBKeyRange.js";
|
|
2
|
-
import
|
|
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 =
|
|
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 :
|
|
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 :
|
|
183
|
-
const upperComparison = upper === undefined ? 1 :
|
|
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
|
-
|
|
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(
|
|
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
|
|
294
|
-
const right = this._rebuild(records
|
|
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;
|
package/build/esm/lib/cmp.js
CHANGED
|
@@ -19,13 +19,15 @@ const getType = x => {
|
|
|
19
19
|
throw new DataError();
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
48
|
-
|
|
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"
|
|
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 =
|
|
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;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as assert from "assert";
|
|
2
|
+
import * as fakeIndexedDB from "../../index.js";
|
|
3
|
+
|
|
4
|
+
// `indexedDB` is read-only, all others are read-write
|
|
5
|
+
const props = Object.keys(fakeIndexedDB).filter(prop => prop.startsWith("IDB") || prop === "indexedDB");
|
|
6
|
+
describe("auto", () => {
|
|
7
|
+
it("correctly overrides globals in fake-indexeddb/auto", async () => {
|
|
8
|
+
// simulate what the native property descriptors do
|
|
9
|
+
Object.defineProperty(globalThis, "indexedDB", {
|
|
10
|
+
set: undefined,
|
|
11
|
+
get: () => undefined,
|
|
12
|
+
enumerable: false,
|
|
13
|
+
configurable: true
|
|
14
|
+
});
|
|
15
|
+
for (const prop of props) {
|
|
16
|
+
Object.defineProperty(globalThis, prop, {
|
|
17
|
+
value: undefined,
|
|
18
|
+
enumerable: false,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// @ts-expect-error relative to the build/ directory
|
|
25
|
+
await import("../../../../auto/index.mjs");
|
|
26
|
+
|
|
27
|
+
// check our own globals
|
|
28
|
+
for (const prop of props) {
|
|
29
|
+
const descriptor = Object.getOwnPropertyDescriptor(globalThis, prop);
|
|
30
|
+
assert.equal(descriptor.value, fakeIndexedDB[prop]);
|
|
31
|
+
assert.equal(descriptor.enumerable, false);
|
|
32
|
+
assert.equal(descriptor.configurable, true);
|
|
33
|
+
assert.equal(descriptor.writable, true);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// check that we can still overwrite them with `globalThis.<prop> = ...`
|
|
37
|
+
for (const prop of props) {
|
|
38
|
+
const fake = {};
|
|
39
|
+
globalThis[prop] = fake;
|
|
40
|
+
assert.equal(globalThis[prop], fake);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
it("exports as cjs directly, without `default` member - issue #130", async () => {
|
|
44
|
+
// @ts-expect-error relative to the build/ directory
|
|
45
|
+
await import("../../../../auto/index.js");
|
|
46
|
+
|
|
47
|
+
// ensure we directly set the export as `module.exports` rather than `module.exports.default`
|
|
48
|
+
for (const prop of props) {
|
|
49
|
+
assert.ok(!globalThis[prop].default);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
});
|