fake-indexeddb 6.2.2 → 6.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +23 -15
  2. package/auto/index.js +1 -1
  3. package/auto/index.mjs +1 -1
  4. package/build/cjs/FDBCursor.js +2 -1
  5. package/build/cjs/FDBDatabase.js +45 -20
  6. package/build/cjs/FDBFactory.js +285 -137
  7. package/build/cjs/FDBIndex.js +13 -9
  8. package/build/cjs/FDBObjectStore.js +26 -22
  9. package/build/cjs/FDBTransaction.js +51 -11
  10. package/build/cjs/lib/Database.js +25 -12
  11. package/build/cjs/lib/FakeEventTarget.js +17 -5
  12. package/build/cjs/lib/ObjectStore.js +60 -20
  13. package/build/cjs/lib/cloneValueForInsertion.js +30 -0
  14. package/build/cjs/lib/extractGetAllOptions.js +2 -2
  15. package/build/cjs/lib/intersection.js +18 -0
  16. package/build/cjs/lib/isPotentiallyValidKeyRange.js +13 -20
  17. package/build/cjs/lib/scheduling.js +4 -4
  18. package/build/cjs/lib/validateRequiredArguments.js +13 -0
  19. package/build/cjs/lib/valueToKey.js +8 -53
  20. package/build/cjs/lib/valueToKeyWithoutThrowing.js +102 -0
  21. package/build/esm/FDBCursor.js +2 -1
  22. package/build/esm/FDBDatabase.js +45 -20
  23. package/build/esm/FDBFactory.js +285 -137
  24. package/build/esm/FDBIndex.js +13 -9
  25. package/build/esm/FDBObjectStore.js +26 -22
  26. package/build/esm/FDBTransaction.js +51 -11
  27. package/build/esm/lib/Database.js +25 -12
  28. package/build/esm/lib/FakeEventTarget.js +17 -5
  29. package/build/esm/lib/ObjectStore.js +60 -20
  30. package/build/esm/lib/cloneValueForInsertion.js +24 -0
  31. package/build/esm/lib/extractGetAllOptions.js +2 -2
  32. package/build/esm/lib/intersection.js +12 -0
  33. package/build/esm/lib/isPotentiallyValidKeyRange.js +12 -20
  34. package/build/esm/lib/scheduling.js +4 -4
  35. package/build/esm/lib/validateRequiredArguments.js +7 -0
  36. package/build/esm/lib/valueToKey.js +7 -52
  37. package/build/esm/lib/valueToKeyWithoutThrowing.js +95 -0
  38. package/package.json +4 -2
@@ -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 (!database._runningVersionchangeTransaction) {
10
- throw new InvalidStateError();
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._rollbackLog.push(() => {
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);
@@ -7,6 +7,16 @@ 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";
11
+ // https://w3c.github.io/IndexedDB/#connection-queue
12
+ const runTaskInConnectionQueue = (connectionQueues, name, task) => {
13
+ // Let queue be the connection queue for storageKey and name.
14
+ // (note FakeIndexedDB does not support storageKeys currently)
15
+ // Add request to queue.
16
+ // Wait until all previous requests in queue have been processed.
17
+ const queue = connectionQueues.get(name) ?? Promise.resolve();
18
+ connectionQueues.set(name, queue.then(task));
19
+ };
10
20
  const waitForOthersClosedDelete = (databases, name, openDatabases, cb) => {
11
21
  const anyOpen = openDatabases.some(openDatabase2 => {
12
22
  return !openDatabase2._closed && !openDatabase2._closePending;
@@ -19,164 +29,293 @@ const waitForOthersClosedDelete = (databases, name, openDatabases, cb) => {
19
29
  cb(null);
20
30
  };
21
31
 
22
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-deleting-a-database
23
- const deleteDatabase = (databases, name, request, cb) => {
24
- try {
25
- const db = databases.get(name);
26
- if (db === undefined) {
27
- cb(null);
28
- return;
29
- }
30
- db.deletePending = true;
31
- const openDatabases = db.connections.filter(connection => {
32
- return !connection._closed && !connection._closePending;
33
- });
34
- for (const openDatabase2 of openDatabases) {
35
- if (!openDatabase2._closePending) {
36
- const event = new FDBVersionChangeEvent("versionchange", {
37
- newVersion: null,
38
- oldVersion: db.version
32
+ // https://w3c.github.io/IndexedDB/#delete-a-database
33
+ const deleteDatabase = (databases, connectionQueues, name, request, cb) => {
34
+ const deleteDBTask = () => {
35
+ return new Promise(resolve => {
36
+ const db = databases.get(name);
37
+ const oldVersion = db !== undefined ? db.version : 0;
38
+ const onComplete = err => {
39
+ try {
40
+ if (err) {
41
+ cb(err);
42
+ } else {
43
+ cb(null, oldVersion);
44
+ }
45
+ } finally {
46
+ resolve();
47
+ }
48
+ };
49
+ try {
50
+ const db = databases.get(name);
51
+ if (db === undefined) {
52
+ onComplete(null);
53
+ return;
54
+ }
55
+
56
+ // Let openConnections be the set of all connections associated with db.
57
+ const openConnections = db.connections.filter(connection => {
58
+ return !connection._closed;
39
59
  });
40
- openDatabase2.dispatchEvent(event);
60
+
61
+ // For each entry of openConnections that does not have its close pending flag set to true, queue a
62
+ // database task to fire a version change event named versionchange at entry with db’s version and null.
63
+ for (const openDatabase2 of openConnections) {
64
+ if (!openDatabase2._closePending) {
65
+ queueTask(() => {
66
+ const event = new FDBVersionChangeEvent("versionchange", {
67
+ newVersion: null,
68
+ oldVersion: db.version
69
+ });
70
+ openDatabase2.dispatchEvent(event);
71
+ });
72
+ }
73
+ }
74
+
75
+ // Wait for all of the events to be fired. (i.e. queue a task)
76
+ queueTask(() => {
77
+ // If any of the connections in openConnections are still not closed, queue a database task to
78
+ // fire a version change event named blocked at request with db’s version and null.
79
+
80
+ const anyOpen = openConnections.some(openDatabase3 => {
81
+ return !openDatabase3._closed && !openDatabase3._closePending;
82
+ });
83
+
84
+ // If any of the connections in openConnections are still not closed, queue a database task to
85
+ // fire a version change event named blocked at request with db’s version and null.
86
+ if (anyOpen) {
87
+ queueTask(() => {
88
+ const event = new FDBVersionChangeEvent("blocked", {
89
+ newVersion: null,
90
+ oldVersion: db.version
91
+ });
92
+ request.dispatchEvent(event);
93
+ });
94
+ }
95
+
96
+ // Wait until all connections in openConnections are closed.
97
+ waitForOthersClosedDelete(databases, name, openConnections, onComplete);
98
+ });
99
+ } catch (err) {
100
+ onComplete(err);
41
101
  }
42
- }
43
- const anyOpen = openDatabases.some(openDatabase3 => {
44
- return !openDatabase3._closed && !openDatabase3._closePending;
45
102
  });
46
- if (request && anyOpen) {
47
- const event = new FDBVersionChangeEvent("blocked", {
48
- newVersion: null,
49
- oldVersion: db.version
50
- });
51
- request.dispatchEvent(event);
52
- }
53
- waitForOthersClosedDelete(databases, name, openDatabases, cb);
54
- } catch (err) {
55
- cb(err);
56
- }
103
+ };
104
+ runTaskInConnectionQueue(connectionQueues, name, deleteDBTask);
57
105
  };
58
106
 
59
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-running-a-versionchange-transaction
107
+ // https://w3c.github.io/IndexedDB/#ref-for-database-version%E2%91%A0%E2%91%A2
60
108
  const runVersionchangeTransaction = (connection, version, request, cb) => {
61
109
  connection._runningVersionchangeTransaction = true;
62
- const oldVersion = connection.version;
63
- const openDatabases = connection._rawDatabase.connections.filter(otherDatabase => {
110
+ const oldVersion = connection._oldVersion = connection.version;
111
+
112
+ // Let openConnections be the set of all connections, except connection, associated with db.
113
+ const openConnections = connection._rawDatabase.connections.filter(otherDatabase => {
64
114
  return connection !== otherDatabase;
65
115
  });
66
- for (const openDatabase2 of openDatabases) {
116
+
117
+ // For each entry of openConnections that does not have its close pending flag set to true, queue a
118
+ // database task to fire a version change event named versionchange at entry with db’s version and version.
119
+ for (const openDatabase2 of openConnections) {
67
120
  if (!openDatabase2._closed && !openDatabase2._closePending) {
68
- const event = new FDBVersionChangeEvent("versionchange", {
69
- newVersion: version,
70
- oldVersion
121
+ queueTask(() => {
122
+ const event = new FDBVersionChangeEvent("versionchange", {
123
+ newVersion: version,
124
+ oldVersion
125
+ });
126
+ openDatabase2.dispatchEvent(event);
71
127
  });
72
- openDatabase2.dispatchEvent(event);
73
128
  }
74
129
  }
75
- const anyOpen = openDatabases.some(openDatabase3 => {
76
- return !openDatabase3._closed && !openDatabase3._closePending;
77
- });
78
- if (anyOpen) {
79
- const event = new FDBVersionChangeEvent("blocked", {
80
- newVersion: version,
81
- oldVersion
82
- });
83
- request.dispatchEvent(event);
84
- }
85
- const waitForOthersClosed = () => {
86
- const anyOpen2 = openDatabases.some(openDatabase2 => {
87
- return !openDatabase2._closed && !openDatabase2._closePending;
130
+
131
+ // Wait for all of the events to be fired.
132
+ // (i.e. queue a task)
133
+ queueTask(() => {
134
+ const anyOpen = openConnections.some(openDatabase3 => {
135
+ return !openDatabase3._closed && !openDatabase3._closePending;
88
136
  });
89
- if (anyOpen2) {
90
- queueTask(waitForOthersClosed);
91
- return;
137
+
138
+ // If any of the connections in openConnections are still not closed, queue a database task to fire a version change event named blocked at request with db’s version and version.
139
+ if (anyOpen) {
140
+ queueTask(() => {
141
+ const event = new FDBVersionChangeEvent("blocked", {
142
+ newVersion: version,
143
+ oldVersion
144
+ });
145
+ request.dispatchEvent(event);
146
+ });
92
147
  }
93
148
 
94
- // Set the version of database to version. This change is considered part of the transaction, and so if the
95
- // transaction is aborted, this change is reverted.
96
- connection._rawDatabase.version = version;
97
- connection.version = version;
149
+ // Wait until all connections in openConnections are closed.
150
+ const waitForOthersClosed = () => {
151
+ const anyOpen2 = openConnections.some(openDatabase2 => {
152
+ return !openDatabase2._closed && !openDatabase2._closePending;
153
+ });
154
+ if (anyOpen2) {
155
+ queueTask(waitForOthersClosed);
156
+ return;
157
+ }
98
158
 
99
- // Get rid of this setImmediate?
100
- const transaction = connection.transaction(Array.from(connection.objectStoreNames), "versionchange");
101
- request.result = connection;
102
- request.readyState = "done";
103
- request.transaction = transaction;
104
- transaction._rollbackLog.push(() => {
105
- connection._rawDatabase.version = oldVersion;
106
- connection.version = oldVersion;
107
- });
108
- const event = new FDBVersionChangeEvent("upgradeneeded", {
109
- newVersion: version,
110
- oldVersion
111
- });
112
- request.dispatchEvent(event);
113
- transaction.addEventListener("error", () => {
114
- connection._runningVersionchangeTransaction = false;
115
- // throw arguments[0].target.error;
116
- // console.log("error in versionchange transaction - not sure if anything needs to be done here", e.target.error.name);
117
- });
118
- transaction.addEventListener("abort", () => {
119
- connection._runningVersionchangeTransaction = false;
120
- request.transaction = null;
121
- queueTask(() => {
122
- cb(new AbortError());
159
+ // Set the version of database to version. This change is considered part of the transaction, and so if the
160
+ // transaction is aborted, this change is reverted.
161
+ connection._rawDatabase.version = version;
162
+ connection.version = version;
163
+
164
+ // Get rid of this setImmediate?
165
+ const transaction = connection.transaction(Array.from(connection.objectStoreNames), "versionchange");
166
+
167
+ // associate the transaction with the open request for later lookup
168
+ transaction._openRequest = request;
169
+
170
+ // https://w3c.github.io/IndexedDB/#upgrade-a-database
171
+ // Set request’s result to connection.
172
+ request.result = connection;
173
+ // Set request’s done flag to true.
174
+ request.readyState = "done";
175
+ // Set request’s transaction to transaction.
176
+ request.transaction = transaction;
177
+ transaction._rollbackLog.push(() => {
178
+ connection._rawDatabase.version = oldVersion;
179
+ connection.version = oldVersion;
123
180
  });
124
- });
125
- transaction.addEventListener("complete", () => {
126
- connection._runningVersionchangeTransaction = false;
127
- request.transaction = null;
128
- // Let other complete event handlers run before continuing
129
- queueTask(() => {
130
- if (connection._closePending) {
131
- cb(new AbortError());
132
- } else {
133
- cb(null);
181
+
182
+ // Set transaction’s state to active.
183
+ transaction._state = "active";
184
+
185
+ // Let didThrow be the result of firing a version change event named upgradeneeded at request with old version and version.
186
+ const event = new FDBVersionChangeEvent("upgradeneeded", {
187
+ newVersion: version,
188
+ oldVersion
189
+ });
190
+ let didThrow = false;
191
+ try {
192
+ request.dispatchEvent(event);
193
+ } catch (_err) {
194
+ didThrow = true;
195
+ }
196
+ const concludeUpgrade = () => {
197
+ // If transaction’s state is active, then:
198
+ if (transaction._state === "active") {
199
+ // Set transaction’s state to inactive.
200
+ transaction._state = "inactive";
201
+ if (didThrow) {
202
+ // If didThrow is true, run abort a transaction with transaction and a newly created "AbortError" DOMException.
203
+ transaction._abort("AbortError");
204
+ }
134
205
  }
206
+ };
207
+
208
+ // The "upgrade a database" steps are supposed to run as a database task on the database access task source
209
+ // (i.e. off the main thread), but since we're actually running on the main thread, we have to be tricky:
210
+ // 1. If any `upgradeneeded` event handlers errored, abort synchronously
211
+ // 2. Else yield to allow any microtasks to run in response to that event
212
+ if (didThrow) {
213
+ concludeUpgrade();
214
+ } else {
215
+ queueTask(concludeUpgrade);
216
+ }
217
+ transaction.addEventListener("error", () => {
218
+ connection._runningVersionchangeTransaction = false;
219
+ connection._oldVersion = undefined;
220
+ // throw arguments[0].target.error;
221
+ // console.log("error in versionchange transaction - not sure if anything needs to be done here", e.target.error.name);
135
222
  });
136
- });
137
- };
138
- waitForOthersClosed();
223
+ transaction.addEventListener("abort", () => {
224
+ connection._runningVersionchangeTransaction = false;
225
+ connection._oldVersion = undefined;
226
+ request.transaction = null;
227
+ queueTask(() => {
228
+ cb(new AbortError());
229
+ });
230
+ });
231
+ transaction.addEventListener("complete", () => {
232
+ connection._runningVersionchangeTransaction = false;
233
+ connection._oldVersion = undefined;
234
+ request.transaction = null;
235
+ // Let other complete event handlers run before continuing
236
+ queueTask(() => {
237
+ if (connection._closePending) {
238
+ cb(new AbortError());
239
+ } else {
240
+ cb(null);
241
+ }
242
+ });
243
+ });
244
+ };
245
+ waitForOthersClosed();
246
+ });
139
247
  };
140
248
 
141
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-opening-a-database
142
- const openDatabase = (databases, name, version, request, cb) => {
143
- let db = databases.get(name);
144
- if (db === undefined) {
145
- db = new Database(name, 0);
146
- databases.set(name, db);
147
- }
148
- if (version === undefined) {
149
- version = db.version !== 0 ? db.version : 1;
150
- }
151
- if (db.version > version) {
152
- return cb(new VersionError());
153
- }
154
- const connection = new FDBDatabase(db);
155
- if (db.version < version) {
156
- runVersionchangeTransaction(connection, version, request, err => {
157
- if (err) {
158
- // DO THIS HERE: ensure that connection is closed by running the steps for closing a database connection before these
159
- // steps are aborted.
160
- return cb(err);
249
+ // https://w3c.github.io/IndexedDB/#opening
250
+ const openDatabase = (databases, connectionQueues, name, version, request, cb) => {
251
+ const openDBTask = () => {
252
+ return new Promise(resolve => {
253
+ const onComplete = err => {
254
+ try {
255
+ if (err) {
256
+ // DO THIS HERE: ensure that connection is closed by running the steps for closing a database connection before these
257
+ // steps are aborted.
258
+ cb(err);
259
+ } else {
260
+ cb(null, connection);
261
+ }
262
+ } finally {
263
+ resolve();
264
+ }
265
+ };
266
+
267
+ // Let db be the database named name in storageKey, or null otherwise.
268
+ let db = databases.get(name);
269
+ if (db === undefined) {
270
+ // If db is null, let db be a new database with name `name`, version 0 (zero), and with no object stores.
271
+ db = new Database(name, 0);
272
+ databases.set(name, db);
273
+ }
274
+
275
+ // If version is undefined, let version be 1 if db is null, or db’s version otherwise.
276
+ if (version === undefined) {
277
+ version = db.version !== 0 ? db.version : 1;
278
+ }
279
+
280
+ // If db’s version is greater than version, return a newly created "VersionError" DOMException and abort these steps.
281
+ if (db.version > version) {
282
+ return onComplete(new VersionError());
283
+ }
284
+
285
+ // Let connection be a new connection to db.
286
+ const connection = new FDBDatabase(db);
287
+
288
+ // If db’s version is less than version, then:
289
+ if (db.version < version) {
290
+ // (run a version change transaction and resolve so that the next promise in the queue will execute)
291
+ runVersionchangeTransaction(connection, version, request, err => {
292
+ onComplete(err);
293
+ });
294
+ } else {
295
+ onComplete(null);
161
296
  }
162
- cb(null, connection);
163
297
  });
164
- } else {
165
- cb(null, connection);
166
- }
298
+ };
299
+ runTaskInConnectionQueue(connectionQueues, name, openDBTask);
167
300
  };
168
301
  class FDBFactory {
169
- cmp = cmp;
170
302
  _databases = new Map();
303
+ // https://w3c.github.io/IndexedDB/#connection-queue
304
+ _connectionQueues = new Map(); // promise chain as lightweight FIFO task queue
305
+
306
+ // https://w3c.github.io/IndexedDB/#dom-idbfactory-cmp
307
+ cmp(first, second) {
308
+ validateRequiredArguments(arguments.length, 2, "IDBFactory.cmp");
309
+ return cmp(first, second);
310
+ }
171
311
 
172
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBFactory-deleteDatabase-IDBOpenDBRequest-DOMString-name
312
+ // https://w3c.github.io/IndexedDB/#dom-idbfactory-deletedatabase
173
313
  deleteDatabase(name) {
314
+ validateRequiredArguments(arguments.length, 1, "IDBFactory.deleteDatabase");
174
315
  const request = new FDBOpenDBRequest();
175
316
  request.source = null;
176
317
  queueTask(() => {
177
- const db = this._databases.get(name);
178
- const oldVersion = db !== undefined ? db.version : 0;
179
- deleteDatabase(this._databases, name, request, err => {
318
+ deleteDatabase(this._databases, this._connectionQueues, name, request, (err, oldVersion) => {
180
319
  if (err) {
181
320
  request.error = new DOMException(err.message, err.name);
182
321
  request.readyState = "done";
@@ -202,6 +341,7 @@ class FDBFactory {
202
341
 
203
342
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBFactory-open-IDBOpenDBRequest-DOMString-name-unsigned-long-long-version
204
343
  open(name, version) {
344
+ validateRequiredArguments(arguments.length, 1, "IDBFactory.open");
205
345
  if (arguments.length > 1 && version !== undefined) {
206
346
  // Based on spec, not sure why "MAX_SAFE_INTEGER" instead of "unsigned long long", but it's needed to pass
207
347
  // tests
@@ -213,7 +353,7 @@ class FDBFactory {
213
353
  const request = new FDBOpenDBRequest();
214
354
  request.source = null;
215
355
  queueTask(() => {
216
- openDatabase(this._databases, name, version, request, (err, connection) => {
356
+ openDatabase(this._databases, this._connectionQueues, name, version, request, (err, connection) => {
217
357
  if (err) {
218
358
  request.result = undefined;
219
359
  request.readyState = "done";
@@ -238,16 +378,24 @@ class FDBFactory {
238
378
 
239
379
  // https://w3c.github.io/IndexedDB/#dom-idbfactory-databases
240
380
  databases() {
241
- return new Promise(resolve => {
242
- const result = [];
243
- for (const [name, database] of this._databases) {
244
- result.push({
245
- name,
246
- version: database.version
247
- });
248
- }
249
- resolve(result);
250
- });
381
+ return Promise.resolve(Array.from(this._databases.entries(), ([name, database]) => {
382
+ const activeVersionChangeConnection = database.connections.find(connection => connection._runningVersionchangeTransaction);
383
+ // If a versionchange is in progress, report the old version. See `get-databases.any.js` test:
384
+ // "The result of databases() should contain the versions of databases at the time of calling,
385
+ // regardless of versionchange transactions currently running."
386
+ const version = activeVersionChangeConnection ? activeVersionChangeConnection._oldVersion : database.version;
387
+ return {
388
+ name,
389
+ version
390
+ };
391
+ }).filter(({
392
+ version
393
+ }) => {
394
+ // Ignore newly-created DBs with active versionchange transactions. See `get-databases.any.js` test:
395
+ // "The result of databases() should be only those databases which have been created at the
396
+ // time of calling, regardless of versionchange transactions currently running."
397
+ return version > 0;
398
+ }));
251
399
  }
252
400
  get [Symbol.toStringTag]() {
253
401
  return "IDBFactory";
@@ -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
- transaction._rollbackLog.push(() => {
67
- this._name = oldName;
68
- this._rawIndex.name = oldName;
69
- this.objectStore._indexesCache.delete(name);
70
- this.objectStore._indexesCache.set(oldName, this);
71
- this.objectStore._rawObjectStore.rawIndexes.delete(name);
72
- this.objectStore._rawObjectStore.rawIndexes.set(oldName, this._rawIndex);
73
- this.objectStore.indexNames = new FakeDOMStringList(...oldIndexNames);
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