fake-indexeddb 6.2.3 → 6.2.5
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 +23 -15
- package/auto/index.js +1 -1
- package/auto/index.mjs +1 -1
- package/build/cjs/FDBCursor.js +2 -1
- package/build/cjs/FDBDatabase.js +45 -20
- package/build/cjs/FDBFactory.js +16 -6
- package/build/cjs/FDBIndex.js +13 -9
- package/build/cjs/FDBObjectStore.js +26 -22
- package/build/cjs/FDBTransaction.js +12 -0
- package/build/cjs/lib/Database.js +5 -1
- package/build/cjs/lib/FakeEventTarget.js +6 -3
- package/build/cjs/lib/ObjectStore.js +60 -20
- package/build/cjs/lib/cloneValueForInsertion.js +30 -0
- package/build/cjs/lib/validateRequiredArguments.js +13 -0
- package/build/cjs/lib/valueToKeyWithoutThrowing.js +25 -5
- package/build/esm/FDBCursor.js +2 -1
- package/build/esm/FDBDatabase.js +45 -20
- package/build/esm/FDBFactory.js +16 -6
- package/build/esm/FDBIndex.js +13 -9
- package/build/esm/FDBObjectStore.js +26 -22
- package/build/esm/FDBTransaction.js +12 -0
- package/build/esm/lib/Database.js +5 -1
- package/build/esm/lib/FakeEventTarget.js +6 -3
- package/build/esm/lib/ObjectStore.js +60 -20
- package/build/esm/lib/cloneValueForInsertion.js +24 -0
- package/build/esm/lib/validateRequiredArguments.js +7 -0
- package/build/esm/lib/valueToKeyWithoutThrowing.js +25 -5
- package/package.json +4 -2
|
@@ -58,20 +58,40 @@ const valueToKeyWithoutThrowing = (input, seen) => {
|
|
|
58
58
|
return INVALID_VALUE;
|
|
59
59
|
}
|
|
60
60
|
seen.add(input);
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
|
|
62
|
+
// This algorithm is tricky to account for `bindings-inject-keys-bypass.any.js`. We _should_ return early when
|
|
63
|
+
// encountering an invalid key/type, but we also need to avoid triggering `Object.prototype['10']` if it's been
|
|
64
|
+
// overridden. One simple way to do this (and which doesn't rely on sparse arrays or other exotic solutions that
|
|
65
|
+
// could cause de-opts) is to use `Array.from()` with a mapper function, which does not trigger the prototype
|
|
66
|
+
// setter [1]. It does prevent an early return, but we can at least short-circuit inside the mapper function
|
|
67
|
+
// (which isn't strictly necessary to pass the WPTs, but is closer to the spec).
|
|
68
|
+
// [1]: See https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.from, specifically
|
|
69
|
+
// the chain CreateDataPropertyOrThrow -> CreateDataProperty -> DefineOwnProperty which defines
|
|
70
|
+
// the array element as an "own" property.
|
|
71
|
+
let hasInvalid = false;
|
|
72
|
+
const keys = Array.from({
|
|
73
|
+
length: input.length
|
|
74
|
+
}, (_, i) => {
|
|
75
|
+
if (hasInvalid) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
63
78
|
const hop = Object.hasOwn(input, i);
|
|
64
79
|
if (!hop) {
|
|
65
80
|
// If hop is false, return "invalid value".
|
|
66
|
-
|
|
81
|
+
hasInvalid = true;
|
|
82
|
+
return;
|
|
67
83
|
}
|
|
68
84
|
const entry = input[i];
|
|
69
85
|
const key = valueToKeyWithoutThrowing(entry, seen);
|
|
70
86
|
// If key is "invalid value" or "invalid type" abort these steps and return "invalid value".
|
|
71
87
|
if (key === INVALID_VALUE || key === INVALID_TYPE) {
|
|
72
|
-
|
|
88
|
+
hasInvalid = true;
|
|
89
|
+
return;
|
|
73
90
|
}
|
|
74
|
-
|
|
91
|
+
return key;
|
|
92
|
+
});
|
|
93
|
+
if (hasInvalid) {
|
|
94
|
+
return INVALID_VALUE;
|
|
75
95
|
}
|
|
76
96
|
return keys;
|
|
77
97
|
} else {
|
package/build/esm/FDBCursor.js
CHANGED
|
@@ -4,6 +4,7 @@ import cmp 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";
|
|
7
|
+
import { cloneValueForInsertion } from "./lib/cloneValueForInsertion.js";
|
|
7
8
|
const getEffectiveObjectStore = cursor => {
|
|
8
9
|
if (cursor.source instanceof FDBObjectStore) {
|
|
9
10
|
return cursor.source;
|
|
@@ -298,7 +299,7 @@ class FDBCursor {
|
|
|
298
299
|
if (!this._gotValue || !Object.hasOwn(this, "value")) {
|
|
299
300
|
throw new InvalidStateError();
|
|
300
301
|
}
|
|
301
|
-
const clone =
|
|
302
|
+
const clone = cloneValueForInsertion(value, transaction);
|
|
302
303
|
if (effectiveObjectStore.keyPath !== null) {
|
|
303
304
|
let tempKey;
|
|
304
305
|
try {
|
package/build/esm/FDBDatabase.js
CHANGED
|
@@ -5,19 +5,21 @@ import FakeEventTarget from "./lib/FakeEventTarget.js";
|
|
|
5
5
|
import ObjectStore from "./lib/ObjectStore.js";
|
|
6
6
|
import validateKeyPath from "./lib/validateKeyPath.js";
|
|
7
7
|
import closeConnection from "./lib/closeConnection.js";
|
|
8
|
+
// Common first 3 steps of https://www.w3.org/TR/IndexedDB/#dom-idbdatabase-createobjectstore and https://www.w3.org/TR/IndexedDB/#dom-idbdatabase-deleteobjectstore
|
|
8
9
|
const confirmActiveVersionchangeTransaction = database => {
|
|
9
|
-
if
|
|
10
|
-
|
|
10
|
+
// Let transaction be database’s upgrade transaction if it is not null, or throw an "InvalidStateError" DOMException otherwise.
|
|
11
|
+
let transaction;
|
|
12
|
+
if (database._runningVersionchangeTransaction) {
|
|
13
|
+
// Find the latest versionchange transaction
|
|
14
|
+
transaction = database._rawDatabase.transactions.findLast(tx => {
|
|
15
|
+
return tx.mode === "versionchange";
|
|
16
|
+
});
|
|
11
17
|
}
|
|
12
|
-
|
|
13
|
-
// Find the latest versionchange transaction
|
|
14
|
-
const transactions = database._rawDatabase.transactions.filter(tx => {
|
|
15
|
-
return tx.mode === "versionchange";
|
|
16
|
-
});
|
|
17
|
-
const transaction = transactions[transactions.length - 1];
|
|
18
|
-
if (!transaction || transaction._state === "finished") {
|
|
18
|
+
if (!transaction) {
|
|
19
19
|
throw new InvalidStateError();
|
|
20
20
|
}
|
|
21
|
+
|
|
22
|
+
// If transaction’s state is not active, then throw a "TransactionInactiveError" DOMException.
|
|
21
23
|
if (transaction._state !== "active") {
|
|
22
24
|
throw new TransactionInactiveError();
|
|
23
25
|
}
|
|
@@ -55,43 +57,66 @@ class FDBDatabase extends FakeEventTarget {
|
|
|
55
57
|
if (autoIncrement && (keyPath === "" || Array.isArray(keyPath))) {
|
|
56
58
|
throw new InvalidAccessError();
|
|
57
59
|
}
|
|
60
|
+
|
|
61
|
+
// Save for rollbackLog
|
|
58
62
|
const objectStoreNames = [...this.objectStoreNames];
|
|
59
|
-
transaction.
|
|
60
|
-
const objectStore = this._rawDatabase.rawObjectStores.get(name);
|
|
61
|
-
if (objectStore) {
|
|
62
|
-
objectStore.deleted = true;
|
|
63
|
-
}
|
|
64
|
-
this.objectStoreNames = new FakeDOMStringList(...objectStoreNames);
|
|
65
|
-
transaction._scope.delete(name);
|
|
66
|
-
this._rawDatabase.rawObjectStores.delete(name);
|
|
67
|
-
});
|
|
63
|
+
const transactionObjectStoreNames = [...transaction.objectStoreNames];
|
|
68
64
|
const rawObjectStore = new ObjectStore(this._rawDatabase, name, keyPath, autoIncrement);
|
|
69
65
|
this.objectStoreNames._push(name);
|
|
70
66
|
this.objectStoreNames._sort();
|
|
71
67
|
transaction._scope.add(name);
|
|
68
|
+
transaction._createdObjectStores.add(rawObjectStore);
|
|
72
69
|
this._rawDatabase.rawObjectStores.set(name, rawObjectStore);
|
|
73
70
|
transaction.objectStoreNames = new FakeDOMStringList(...this.objectStoreNames);
|
|
71
|
+
transaction._rollbackLog.push(() => {
|
|
72
|
+
rawObjectStore.deleted = true;
|
|
73
|
+
this.objectStoreNames = new FakeDOMStringList(...objectStoreNames);
|
|
74
|
+
transaction.objectStoreNames = new FakeDOMStringList(...transactionObjectStoreNames);
|
|
75
|
+
transaction._scope.delete(rawObjectStore.name);
|
|
76
|
+
this._rawDatabase.rawObjectStores.delete(rawObjectStore.name);
|
|
77
|
+
});
|
|
74
78
|
return transaction.objectStore(name);
|
|
75
79
|
}
|
|
80
|
+
|
|
81
|
+
// https://www.w3.org/TR/IndexedDB/#dom-idbdatabase-deleteobjectstore
|
|
76
82
|
deleteObjectStore(name) {
|
|
77
83
|
if (name === undefined) {
|
|
78
84
|
throw new TypeError();
|
|
79
85
|
}
|
|
80
86
|
const transaction = confirmActiveVersionchangeTransaction(this);
|
|
87
|
+
|
|
88
|
+
// Let store be the object store named name in database, or throw a "NotFoundError" DOMException if none.
|
|
81
89
|
const store = this._rawDatabase.rawObjectStores.get(name);
|
|
82
90
|
if (store === undefined) {
|
|
83
91
|
throw new NotFoundError();
|
|
84
92
|
}
|
|
93
|
+
|
|
94
|
+
// Remove store from this’s object store set.
|
|
95
|
+
// This method synchronously modifies the objectStoreNames property on the IDBDatabase instance on which it was called.
|
|
85
96
|
this.objectStoreNames = new FakeDOMStringList(...Array.from(this.objectStoreNames).filter(objectStoreName => {
|
|
86
97
|
return objectStoreName !== name;
|
|
87
98
|
}));
|
|
88
99
|
transaction.objectStoreNames = new FakeDOMStringList(...this.objectStoreNames);
|
|
100
|
+
|
|
101
|
+
// If there is an object store handle associated with store and transaction, remove all entries from its index set.
|
|
102
|
+
const objectStore = transaction._objectStoresCache.get(name);
|
|
103
|
+
let prevIndexNames;
|
|
104
|
+
if (objectStore) {
|
|
105
|
+
prevIndexNames = [...objectStore.indexNames];
|
|
106
|
+
objectStore.indexNames = new FakeDOMStringList();
|
|
107
|
+
}
|
|
89
108
|
transaction._rollbackLog.push(() => {
|
|
90
109
|
store.deleted = false;
|
|
91
|
-
this._rawDatabase.rawObjectStores.set(name, store);
|
|
92
|
-
this.objectStoreNames._push(name);
|
|
110
|
+
this._rawDatabase.rawObjectStores.set(store.name, store);
|
|
111
|
+
this.objectStoreNames._push(store.name);
|
|
112
|
+
transaction.objectStoreNames._push(store.name);
|
|
93
113
|
this.objectStoreNames._sort();
|
|
114
|
+
if (objectStore && prevIndexNames) {
|
|
115
|
+
objectStore.indexNames = new FakeDOMStringList(...prevIndexNames);
|
|
116
|
+
}
|
|
94
117
|
});
|
|
118
|
+
|
|
119
|
+
// Destroy store.
|
|
95
120
|
store.deleted = true;
|
|
96
121
|
this._rawDatabase.rawObjectStores.delete(name);
|
|
97
122
|
transaction._objectStoresCache.delete(name);
|
package/build/esm/FDBFactory.js
CHANGED
|
@@ -7,6 +7,7 @@ import enforceRange from "./lib/enforceRange.js";
|
|
|
7
7
|
import { AbortError, VersionError } from "./lib/errors.js";
|
|
8
8
|
import FakeEvent from "./lib/FakeEvent.js";
|
|
9
9
|
import { queueTask } from "./lib/scheduling.js";
|
|
10
|
+
import { validateRequiredArguments } from "./lib/validateRequiredArguments.js";
|
|
10
11
|
// https://w3c.github.io/IndexedDB/#connection-queue
|
|
11
12
|
const runTaskInConnectionQueue = (connectionQueues, name, task) => {
|
|
12
13
|
// Let queue be the connection queue for storageKey and name.
|
|
@@ -213,26 +214,28 @@ const runVersionchangeTransaction = (connection, version, request, cb) => {
|
|
|
213
214
|
} else {
|
|
214
215
|
queueTask(concludeUpgrade);
|
|
215
216
|
}
|
|
216
|
-
transaction.
|
|
217
|
+
transaction._prioritizedListeners.set("error", () => {
|
|
217
218
|
connection._runningVersionchangeTransaction = false;
|
|
218
219
|
connection._oldVersion = undefined;
|
|
219
220
|
// throw arguments[0].target.error;
|
|
220
221
|
// console.log("error in versionchange transaction - not sure if anything needs to be done here", e.target.error.name);
|
|
221
222
|
});
|
|
222
|
-
transaction.
|
|
223
|
+
transaction._prioritizedListeners.set("abort", () => {
|
|
223
224
|
connection._runningVersionchangeTransaction = false;
|
|
224
225
|
connection._oldVersion = undefined;
|
|
225
|
-
request.transaction = null;
|
|
226
226
|
queueTask(() => {
|
|
227
|
+
// Reset transaction in a tick after onabort (upgrade-transaction-lifecycle-user-aborted.any)
|
|
228
|
+
request.transaction = null;
|
|
227
229
|
cb(new AbortError());
|
|
228
230
|
});
|
|
229
231
|
});
|
|
230
|
-
transaction.
|
|
232
|
+
transaction._prioritizedListeners.set("complete", () => {
|
|
231
233
|
connection._runningVersionchangeTransaction = false;
|
|
232
234
|
connection._oldVersion = undefined;
|
|
233
|
-
request.transaction = null;
|
|
234
235
|
// Let other complete event handlers run before continuing
|
|
235
236
|
queueTask(() => {
|
|
237
|
+
// Reset transaction in a tick after oncomplete (upgrade-transaction-lifecycle-committed.any.js)
|
|
238
|
+
request.transaction = null;
|
|
236
239
|
if (connection._closePending) {
|
|
237
240
|
cb(new AbortError());
|
|
238
241
|
} else {
|
|
@@ -298,13 +301,19 @@ const openDatabase = (databases, connectionQueues, name, version, request, cb) =
|
|
|
298
301
|
runTaskInConnectionQueue(connectionQueues, name, openDBTask);
|
|
299
302
|
};
|
|
300
303
|
class FDBFactory {
|
|
301
|
-
cmp = cmp;
|
|
302
304
|
_databases = new Map();
|
|
303
305
|
// https://w3c.github.io/IndexedDB/#connection-queue
|
|
304
306
|
_connectionQueues = new Map(); // promise chain as lightweight FIFO task queue
|
|
305
307
|
|
|
308
|
+
// https://w3c.github.io/IndexedDB/#dom-idbfactory-cmp
|
|
309
|
+
cmp(first, second) {
|
|
310
|
+
validateRequiredArguments(arguments.length, 2, "IDBFactory.cmp");
|
|
311
|
+
return cmp(first, second);
|
|
312
|
+
}
|
|
313
|
+
|
|
306
314
|
// https://w3c.github.io/IndexedDB/#dom-idbfactory-deletedatabase
|
|
307
315
|
deleteDatabase(name) {
|
|
316
|
+
validateRequiredArguments(arguments.length, 1, "IDBFactory.deleteDatabase");
|
|
308
317
|
const request = new FDBOpenDBRequest();
|
|
309
318
|
request.source = null;
|
|
310
319
|
queueTask(() => {
|
|
@@ -334,6 +343,7 @@ class FDBFactory {
|
|
|
334
343
|
|
|
335
344
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBFactory-open-IDBOpenDBRequest-DOMString-name-unsigned-long-long-version
|
|
336
345
|
open(name, version) {
|
|
346
|
+
validateRequiredArguments(arguments.length, 1, "IDBFactory.open");
|
|
337
347
|
if (arguments.length > 1 && version !== undefined) {
|
|
338
348
|
// Based on spec, not sure why "MAX_SAFE_INTEGER" instead of "unsigned long long", but it's needed to pass
|
|
339
349
|
// tests
|
package/build/esm/FDBIndex.js
CHANGED
|
@@ -63,15 +63,19 @@ class FDBIndex {
|
|
|
63
63
|
const index = this.objectStore._rawObjectStore.rawIndexes.get(indexName);
|
|
64
64
|
return index && !index.deleted;
|
|
65
65
|
}).sort());
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
66
|
+
|
|
67
|
+
// https://www.w3.org/TR/IndexedDB/#abort-an-upgrade-transaction - "If handle’s index was not newly created during transaction, set handle’s name to its index’s name."
|
|
68
|
+
if (!this.objectStore.transaction._createdIndexes.has(this._rawIndex)) {
|
|
69
|
+
transaction._rollbackLog.push(() => {
|
|
70
|
+
this._name = oldName;
|
|
71
|
+
this._rawIndex.name = oldName;
|
|
72
|
+
this.objectStore._indexesCache.delete(name);
|
|
73
|
+
this.objectStore._indexesCache.set(oldName, this);
|
|
74
|
+
this.objectStore._rawObjectStore.rawIndexes.delete(name);
|
|
75
|
+
this.objectStore._rawObjectStore.rawIndexes.set(oldName, this._rawIndex);
|
|
76
|
+
this.objectStore.indexNames = new FakeDOMStringList(...oldIndexNames);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBIndex-openCursor-IDBRequest-any-range-IDBCursorDirection-direction
|
|
@@ -14,6 +14,7 @@ import valueToKeyRange from "./lib/valueToKeyRange.js";
|
|
|
14
14
|
import { getKeyPath } from "./lib/getKeyPath.js";
|
|
15
15
|
import extractGetAllOptions from "./lib/extractGetAllOptions.js";
|
|
16
16
|
import enforceRange from "./lib/enforceRange.js";
|
|
17
|
+
import { cloneValueForInsertion } from "./lib/cloneValueForInsertion.js";
|
|
17
18
|
const confirmActiveTransaction = objectStore => {
|
|
18
19
|
if (objectStore._rawObjectStore.deleted) {
|
|
19
20
|
throw new InvalidStateError();
|
|
@@ -32,7 +33,7 @@ const buildRecordAddPut = (objectStore, value, key) => {
|
|
|
32
33
|
throw new DataError();
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
|
-
const clone =
|
|
36
|
+
const clone = cloneValueForInsertion(value, objectStore.transaction);
|
|
36
37
|
if (objectStore.keyPath !== null) {
|
|
37
38
|
const tempKey = extractKey(objectStore.keyPath, clone);
|
|
38
39
|
if (tempKey.type === "found") {
|
|
@@ -103,17 +104,21 @@ class FDBObjectStore {
|
|
|
103
104
|
this.transaction._scope.delete(oldName);
|
|
104
105
|
transaction._scope.add(name);
|
|
105
106
|
transaction.objectStoreNames = new FakeDOMStringList(...Array.from(transaction._scope).sort());
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
107
|
+
|
|
108
|
+
// https://www.w3.org/TR/IndexedDB/#abort-an-upgrade-transaction - "If handle’s object store was not newly created during transaction, set handle’s name to its object store’s name."
|
|
109
|
+
if (!this.transaction._createdObjectStores.has(this._rawObjectStore)) {
|
|
110
|
+
transaction._rollbackLog.push(() => {
|
|
111
|
+
this._name = oldName;
|
|
112
|
+
this._rawObjectStore.name = oldName;
|
|
113
|
+
this.transaction._objectStoresCache.delete(name);
|
|
114
|
+
this.transaction._objectStoresCache.set(oldName, this);
|
|
115
|
+
this._rawObjectStore.rawDatabase.rawObjectStores.delete(name);
|
|
116
|
+
this._rawObjectStore.rawDatabase.rawObjectStores.set(oldName, this._rawObjectStore);
|
|
117
|
+
transaction.db.objectStoreNames = new FakeDOMStringList(...oldObjectStoreNames);
|
|
118
|
+
transaction._scope = oldScope;
|
|
119
|
+
transaction.objectStoreNames = new FakeDOMStringList(...oldTransactionObjectStoreNames);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
117
122
|
}
|
|
118
123
|
put(value, key) {
|
|
119
124
|
if (arguments.length === 0) {
|
|
@@ -299,21 +304,20 @@ class FDBObjectStore {
|
|
|
299
304
|
// create and return an IDBIndex object. Instead the implementation must queue up an operation to abort the
|
|
300
305
|
// "versionchange" transaction which was used for the createIndex call.
|
|
301
306
|
|
|
307
|
+
// Save for rollbackLog
|
|
302
308
|
const indexNames = [...this.indexNames];
|
|
303
|
-
this.transaction._rollbackLog.push(() => {
|
|
304
|
-
const index2 = this._rawObjectStore.rawIndexes.get(name);
|
|
305
|
-
if (index2) {
|
|
306
|
-
index2.deleted = true;
|
|
307
|
-
}
|
|
308
|
-
this.indexNames = new FakeDOMStringList(...indexNames);
|
|
309
|
-
this._rawObjectStore.rawIndexes.delete(name);
|
|
310
|
-
});
|
|
311
309
|
const index = new Index(this._rawObjectStore, name, keyPath, multiEntry, unique);
|
|
312
310
|
this.indexNames._push(name);
|
|
313
311
|
this.indexNames._sort();
|
|
312
|
+
this.transaction._createdIndexes.add(index);
|
|
314
313
|
this._rawObjectStore.rawIndexes.set(name, index);
|
|
315
314
|
index.initialize(this.transaction); // This is async by design
|
|
316
315
|
|
|
316
|
+
this.transaction._rollbackLog.push(() => {
|
|
317
|
+
index.deleted = true;
|
|
318
|
+
this.indexNames = new FakeDOMStringList(...indexNames);
|
|
319
|
+
this._rawObjectStore.rawIndexes.delete(index.name);
|
|
320
|
+
});
|
|
317
321
|
return new FDBIndex(this, index);
|
|
318
322
|
}
|
|
319
323
|
|
|
@@ -351,8 +355,8 @@ class FDBObjectStore {
|
|
|
351
355
|
}
|
|
352
356
|
this.transaction._rollbackLog.push(() => {
|
|
353
357
|
rawIndex.deleted = false;
|
|
354
|
-
this._rawObjectStore.rawIndexes.set(name, rawIndex);
|
|
355
|
-
this.indexNames._push(name);
|
|
358
|
+
this._rawObjectStore.rawIndexes.set(rawIndex.name, rawIndex);
|
|
359
|
+
this.indexNames._push(rawIndex.name);
|
|
356
360
|
this.indexNames._sort();
|
|
357
361
|
});
|
|
358
362
|
this.indexNames = new FakeDOMStringList(...Array.from(this.indexNames).filter(indexName => {
|
|
@@ -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,7 +17,10 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
16
17
|
onabort = null;
|
|
17
18
|
oncomplete = null;
|
|
18
19
|
onerror = null;
|
|
20
|
+
_prioritizedListeners = new Map();
|
|
19
21
|
_requests = [];
|
|
22
|
+
_createdIndexes = new Set();
|
|
23
|
+
_createdObjectStores = new Set();
|
|
20
24
|
constructor(storeNames, mode, durability, db) {
|
|
21
25
|
super();
|
|
22
26
|
this._scope = new Set(storeNames);
|
|
@@ -24,6 +28,14 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
24
28
|
this.durability = durability;
|
|
25
29
|
this.db = db;
|
|
26
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
|
+
}
|
|
27
39
|
}
|
|
28
40
|
|
|
29
41
|
// https://w3c.github.io/IndexedDB/#abort-transaction
|
|
@@ -17,8 +17,10 @@ class Database {
|
|
|
17
17
|
|
|
18
18
|
// The next transaction to run is the first waiting one that doesn't overlap with either a running one or a
|
|
19
19
|
// preceding waiting one. This allows non-overlapping transactions to run in parallel.
|
|
20
|
+
// The exception is readonly transactions, which are allowed to run in parallel with other readonly
|
|
21
|
+
// transactions, even with overlapping scopes, since no data is being modified.
|
|
20
22
|
const next = waiting.find((transaction, i) => {
|
|
21
|
-
const anyRunning = running.some(other => intersection(other._scope, transaction._scope).size > 0);
|
|
23
|
+
const anyRunning = running.some(other => !(transaction.mode === "readonly" && other.mode === "readonly") && intersection(other._scope, transaction._scope).size > 0);
|
|
22
24
|
if (anyRunning) {
|
|
23
25
|
return false;
|
|
24
26
|
}
|
|
@@ -26,6 +28,8 @@ class Database {
|
|
|
26
28
|
// If any _preceding_ waiting transactions are blocked, then that's also blocking.
|
|
27
29
|
// E.g. if you have 3 transactions: [a], [a,b], and [b,c], then [a] blocks [a,b] which blocks [b,c]
|
|
28
30
|
// until [a] is complete, even though [a] and [b,c] share no overlap.
|
|
31
|
+
// Note that readonly transactions do not have to be handled as a special case here,
|
|
32
|
+
// because if any transactions with overlapping scopes are blocked, then we can assume they are
|
|
29
33
|
const anyWaiting = waiting.slice(0, i).some(other => intersection(other._scope, transaction._scope).size > 0);
|
|
30
34
|
return !anyWaiting;
|
|
31
35
|
});
|
|
@@ -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
|
});
|
|
@@ -82,14 +82,22 @@ class ObjectStore {
|
|
|
82
82
|
newRecord.key = key;
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
+
const rollbackLogForThisOperation = [];
|
|
85
86
|
if (this.keyGenerator !== null && newRecord.key === undefined) {
|
|
87
|
+
let rolledBack = false;
|
|
88
|
+
const keyGeneratorBefore = this.keyGenerator.num;
|
|
89
|
+
const rollbackKeyGenerator = () => {
|
|
90
|
+
if (rolledBack) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
rolledBack = true;
|
|
94
|
+
if (this.keyGenerator) {
|
|
95
|
+
this.keyGenerator.num = keyGeneratorBefore;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
rollbackLogForThisOperation.push(rollbackKeyGenerator);
|
|
86
99
|
if (rollbackLog) {
|
|
87
|
-
|
|
88
|
-
rollbackLog.push(() => {
|
|
89
|
-
if (this.keyGenerator) {
|
|
90
|
-
this.keyGenerator.num = keyGeneratorBefore;
|
|
91
|
-
}
|
|
92
|
-
});
|
|
100
|
+
rollbackLog.push(rollbackKeyGenerator);
|
|
93
101
|
}
|
|
94
102
|
newRecord.key = this.keyGenerator.next();
|
|
95
103
|
|
|
@@ -112,28 +120,50 @@ class ObjectStore {
|
|
|
112
120
|
identifier = remainingKeyPath.slice(0, i);
|
|
113
121
|
remainingKeyPath = remainingKeyPath.slice(i + 1);
|
|
114
122
|
if (!Object.hasOwn(object, identifier)) {
|
|
115
|
-
|
|
123
|
+
// Bypass prototype when setting (See `bindings-inject-values-bypass.any.js`)
|
|
124
|
+
// Equivalent to `object[identifier] = ...` without using `Object.prototype`
|
|
125
|
+
Object.defineProperty(object, identifier, {
|
|
126
|
+
configurable: true,
|
|
127
|
+
enumerable: true,
|
|
128
|
+
writable: true,
|
|
129
|
+
value: {}
|
|
130
|
+
});
|
|
116
131
|
}
|
|
117
132
|
object = object[identifier];
|
|
118
133
|
}
|
|
119
134
|
}
|
|
120
135
|
identifier = remainingKeyPath;
|
|
121
|
-
|
|
136
|
+
|
|
137
|
+
// Bypass prototype when setting (See `bindings-inject-values-bypass.any.js`)
|
|
138
|
+
// Equivalent to `object[identifier] = ...` without using `Object.prototype`
|
|
139
|
+
Object.defineProperty(object, identifier, {
|
|
140
|
+
configurable: true,
|
|
141
|
+
enumerable: true,
|
|
142
|
+
writable: true,
|
|
143
|
+
value: newRecord.key
|
|
144
|
+
});
|
|
122
145
|
}
|
|
123
146
|
} else if (this.keyGenerator !== null && typeof newRecord.key === "number") {
|
|
124
147
|
this.keyGenerator.setIfLarger(newRecord.key);
|
|
125
148
|
}
|
|
126
149
|
const existingRecord = this.records.put(newRecord, noOverwrite);
|
|
150
|
+
let rolledBack = false;
|
|
151
|
+
const rollbackStoreRecord = () => {
|
|
152
|
+
if (rolledBack) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
rolledBack = true;
|
|
156
|
+
if (existingRecord) {
|
|
157
|
+
// overwrite on rollback
|
|
158
|
+
this.storeRecord(existingRecord, false);
|
|
159
|
+
} else {
|
|
160
|
+
// delete on rollback
|
|
161
|
+
this.deleteRecord(newRecord.key);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
rollbackLogForThisOperation.push(rollbackStoreRecord);
|
|
127
165
|
if (rollbackLog) {
|
|
128
|
-
rollbackLog.push(
|
|
129
|
-
if (existingRecord) {
|
|
130
|
-
// overwrite on rollback
|
|
131
|
-
this.storeRecord(existingRecord, false);
|
|
132
|
-
} else {
|
|
133
|
-
// delete on rollback
|
|
134
|
-
this.deleteRecord(newRecord.key);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
166
|
+
rollbackLog.push(rollbackStoreRecord);
|
|
137
167
|
}
|
|
138
168
|
|
|
139
169
|
// Delete existing indexes
|
|
@@ -144,10 +174,20 @@ class ObjectStore {
|
|
|
144
174
|
}
|
|
145
175
|
|
|
146
176
|
// Update indexes
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
rawIndex.
|
|
177
|
+
try {
|
|
178
|
+
for (const rawIndex of this.rawIndexes.values()) {
|
|
179
|
+
if (rawIndex.initialized) {
|
|
180
|
+
rawIndex.storeRecord(newRecord);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
} catch (err) {
|
|
184
|
+
// If this request fails here and preventDefault is used to stop the transaction from aborting, we need to roll back the addition of this record to the store, otherwise it will be present in subsequent requests on this transaction. Same for key generator.
|
|
185
|
+
if (err.name === "ConstraintError") {
|
|
186
|
+
for (const rollback of rollbackLogForThisOperation) {
|
|
187
|
+
rollback();
|
|
188
|
+
}
|
|
150
189
|
}
|
|
190
|
+
throw err;
|
|
151
191
|
}
|
|
152
192
|
return newRecord.key;
|
|
153
193
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// https://w3c.github.io/IndexedDB/#clone-value
|
|
2
|
+
// Note that we only need to call this during insertions because the spec does not expect any cloning during retrieval,
|
|
3
|
+
// only `StructuredDeserialize()` (e.g. see [1]). This is also only required for values, not keys, since keys do not
|
|
4
|
+
// require cloning during insertion (e.g. see [2]).
|
|
5
|
+
// [1]: https://w3c.github.io/IndexedDB/#retrieve-multiple-items-from-an-object-store
|
|
6
|
+
// [2]: https://w3c.github.io/IndexedDB/#add-or-put
|
|
7
|
+
export function cloneValueForInsertion(value, transaction) {
|
|
8
|
+
// Assert: transaction’s state is active.
|
|
9
|
+
if (transaction._state !== "active") {
|
|
10
|
+
throw new Error("Assert: transaction state is active");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Set transaction’s state to inactive.
|
|
14
|
+
transaction._state = "inactive";
|
|
15
|
+
try {
|
|
16
|
+
// Let serialized be StructuredSerializeForStorage(value).
|
|
17
|
+
// Let clone be ? StructuredDeserialize(serialized, targetRealm).
|
|
18
|
+
// Return clone.
|
|
19
|
+
return structuredClone(value);
|
|
20
|
+
} finally {
|
|
21
|
+
// Set transaction’s state to active.
|
|
22
|
+
transaction._state = "active";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// WebIDL requires passing the right number of non-optional arguments, e.g. IDBFactory.open() must have at least 1 arg
|
|
2
|
+
export function validateRequiredArguments(numArguments, expectedNumArguments, methodName) {
|
|
3
|
+
if (numArguments < expectedNumArguments) {
|
|
4
|
+
// imitate Firefox's error message
|
|
5
|
+
throw new TypeError(`${methodName}: At least ${expectedNumArguments} ${expectedNumArguments === 1 ? "argument" : "arguments"} ` + `required, but only ${arguments.length} passed`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
@@ -51,20 +51,40 @@ const valueToKeyWithoutThrowing = (input, seen) => {
|
|
|
51
51
|
return INVALID_VALUE;
|
|
52
52
|
}
|
|
53
53
|
seen.add(input);
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
|
|
55
|
+
// This algorithm is tricky to account for `bindings-inject-keys-bypass.any.js`. We _should_ return early when
|
|
56
|
+
// encountering an invalid key/type, but we also need to avoid triggering `Object.prototype['10']` if it's been
|
|
57
|
+
// overridden. One simple way to do this (and which doesn't rely on sparse arrays or other exotic solutions that
|
|
58
|
+
// could cause de-opts) is to use `Array.from()` with a mapper function, which does not trigger the prototype
|
|
59
|
+
// setter [1]. It does prevent an early return, but we can at least short-circuit inside the mapper function
|
|
60
|
+
// (which isn't strictly necessary to pass the WPTs, but is closer to the spec).
|
|
61
|
+
// [1]: See https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.from, specifically
|
|
62
|
+
// the chain CreateDataPropertyOrThrow -> CreateDataProperty -> DefineOwnProperty which defines
|
|
63
|
+
// the array element as an "own" property.
|
|
64
|
+
let hasInvalid = false;
|
|
65
|
+
const keys = Array.from({
|
|
66
|
+
length: input.length
|
|
67
|
+
}, (_, i) => {
|
|
68
|
+
if (hasInvalid) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
56
71
|
const hop = Object.hasOwn(input, i);
|
|
57
72
|
if (!hop) {
|
|
58
73
|
// If hop is false, return "invalid value".
|
|
59
|
-
|
|
74
|
+
hasInvalid = true;
|
|
75
|
+
return;
|
|
60
76
|
}
|
|
61
77
|
const entry = input[i];
|
|
62
78
|
const key = valueToKeyWithoutThrowing(entry, seen);
|
|
63
79
|
// If key is "invalid value" or "invalid type" abort these steps and return "invalid value".
|
|
64
80
|
if (key === INVALID_VALUE || key === INVALID_TYPE) {
|
|
65
|
-
|
|
81
|
+
hasInvalid = true;
|
|
82
|
+
return;
|
|
66
83
|
}
|
|
67
|
-
|
|
84
|
+
return key;
|
|
85
|
+
});
|
|
86
|
+
if (hasInvalid) {
|
|
87
|
+
return INVALID_VALUE;
|
|
68
88
|
}
|
|
69
89
|
return keys;
|
|
70
90
|
} else {
|