fake-indexeddb 6.2.6 → 6.2.8

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 -->November 8, 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.8149.0 | 1672 | 99.6% |
216
+ | Firefox | 160.0b5 | 1671 | 99.6% |
217
+ | Safari | 256 preview | 1677 | 99.9% |
218
+ | Ladybird | 1.0-d48145ebfce | 1668 | 99.4% |
219
+ | fake-indexeddb | 6.2.8 | 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.
@@ -271,9 +271,12 @@ class FDBTransaction extends _FakeEventTarget.default {
271
271
  const idx = transactions.indexOf(this);
272
272
  if (idx !== -1) {
273
273
  transactions.splice(idx, 1);
274
- } else {
275
- throw new Error("should have been able to remove transaction");
276
274
  }
275
+ // Note: it's possible to hit this twice in the same aborted transaction
276
+ // due to an abort inside an `onerror` (issue #173). In that case we hit:
277
+ // 1. once due to the user's `abort()`
278
+ // 2. once again due to the `if (!event.canceled)` above
279
+ // So we can just ignore the fact that the transaction was already removed
277
280
  });
278
281
  }
279
282
  commit() {
@@ -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
@@ -96,24 +99,35 @@ class Index {
96
99
  }
97
100
  if (!this.multiEntry || !Array.isArray(indexKey)) {
98
101
  try {
99
- (0, _valueToKey.default)(indexKey);
102
+ // "Convert a value to a key": a single key path returns the
103
+ // value as-is (e.g. a typed array), so store the converted
104
+ // key, not the raw value.
105
+ return (0, _valueToKey.default)(indexKey);
100
106
  } catch (e) {
101
107
  return;
102
108
  }
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
- }
109
+ }
110
+
111
+ // remove any elements from index key that are not valid keys and remove any duplicate elements from index
112
+ // key such that only one instance of the duplicate value remains.
113
+ const keep = [];
114
+ for (const part of indexKey) {
115
+ if (keep.indexOf(part) < 0) {
116
+ try {
117
+ keep.push((0, _valueToKey.default)(part));
118
+ } catch (err) {
119
+ /* Do nothing */
114
120
  }
115
121
  }
116
- indexKey = keep;
122
+ }
123
+ return keep;
124
+ }
125
+
126
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-storing-a-record-into-an-object-store (step 7)
127
+ storeRecord(newRecord) {
128
+ const indexKey = this.getIndexKey(newRecord.value);
129
+ if (indexKey === undefined) {
130
+ return;
117
131
  }
118
132
  if (!this.multiEntry || !Array.isArray(indexKey)) {
119
133
  if (this.unique) {
@@ -122,6 +136,10 @@ class Index {
122
136
  throw new _errors.ConstraintError();
123
137
  }
124
138
  }
139
+ this.records.put({
140
+ key: indexKey,
141
+ value: newRecord.key
142
+ });
125
143
  } else {
126
144
  if (this.unique) {
127
145
  for (const individualIndexKey of indexKey) {
@@ -131,13 +149,6 @@ class Index {
131
149
  }
132
150
  }
133
151
  }
134
- }
135
- if (!this.multiEntry || !Array.isArray(indexKey)) {
136
- this.records.put({
137
- key: indexKey,
138
- value: newRecord.key
139
- });
140
- } else {
141
152
  for (const individualIndexKey of indexKey) {
142
153
  this.records.put({
143
154
  key: individualIndexKey,
@@ -146,6 +157,22 @@ class Index {
146
157
  }
147
158
  }
148
159
  }
160
+
161
+ // Remove the index records that storeRecord created for this object store record. Looking them up by index key is
162
+ // O(log n) per index record, where scanning the whole index for records pointing at this primary key is O(n).
163
+ deleteRecord(record) {
164
+ const indexKey = this.getIndexKey(record.value);
165
+ if (indexKey === undefined) {
166
+ return;
167
+ }
168
+ if (!this.multiEntry || !Array.isArray(indexKey)) {
169
+ this.records.deleteByKeyAndValue(indexKey, record.key);
170
+ } else {
171
+ for (const individualIndexKey of indexKey) {
172
+ this.records.deleteByKeyAndValue(individualIndexKey, record.key);
173
+ }
174
+ }
175
+ }
149
176
  initialize(transaction) {
150
177
  if (this.initialized) {
151
178
  throw new Error("Index already initialized");
@@ -8,6 +8,7 @@ var _FDBRecord = _interopRequireDefault(require("../FDBRecord.js"));
8
8
  var _errors = require("./errors.js");
9
9
  var _extractKey = _interopRequireDefault(require("./extractKey.js"));
10
10
  var _KeyGenerator = _interopRequireDefault(require("./KeyGenerator.js"));
11
+ var _valueToKey = _interopRequireDefault(require("./valueToKey.js"));
11
12
  var _RecordStore = _interopRequireDefault(require("./RecordStore.js"));
12
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
14
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-object-store
@@ -86,7 +87,11 @@ class ObjectStore {
86
87
  if (this.keyPath !== null) {
87
88
  const key = (0, _extractKey.default)(this.keyPath, newRecord.value).key;
88
89
  if (key !== undefined) {
89
- newRecord.key = key;
90
+ // "Convert a value to a key": a single key path returns the
91
+ // value as-is (e.g. a typed array), so convert it here. For
92
+ // an array key path, extractKey already converted each part
93
+ // and valueToKey is idempotent on converted keys.
94
+ newRecord.key = (0, _valueToKey.default)(key);
90
95
  }
91
96
  }
92
97
  const rollbackLogForThisOperation = [];
@@ -176,7 +181,7 @@ class ObjectStore {
176
181
  // Delete existing indexes
177
182
  if (existingRecord) {
178
183
  for (const rawIndex of this.rawIndexes.values()) {
179
- rawIndex.records.deleteByValue(newRecord.key);
184
+ rawIndex.deleteRecord(existingRecord);
180
185
  }
181
186
  }
182
187
 
@@ -210,7 +215,9 @@ class ObjectStore {
210
215
  }
211
216
  }
212
217
  for (const rawIndex of this.rawIndexes.values()) {
213
- rawIndex.records.deleteByValue(key);
218
+ for (const record of deletedRecords) {
219
+ rawIndex.deleteRecord(record);
220
+ }
214
221
  }
215
222
  }
216
223
 
@@ -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()];
@@ -264,9 +264,12 @@ class FDBTransaction extends FakeEventTarget {
264
264
  const idx = transactions.indexOf(this);
265
265
  if (idx !== -1) {
266
266
  transactions.splice(idx, 1);
267
- } else {
268
- throw new Error("should have been able to remove transaction");
269
267
  }
268
+ // Note: it's possible to hit this twice in the same aborted transaction
269
+ // due to an abort inside an `onerror` (issue #173). In that case we hit:
270
+ // 1. once due to the user's `abort()`
271
+ // 2. once again due to the `if (!event.canceled)` above
272
+ // So we can just ignore the fact that the transaction was already removed
270
273
  });
271
274
  }
272
275
  commit() {
@@ -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
@@ -89,24 +92,35 @@ class Index {
89
92
  }
90
93
  if (!this.multiEntry || !Array.isArray(indexKey)) {
91
94
  try {
92
- valueToKey(indexKey);
95
+ // "Convert a value to a key": a single key path returns the
96
+ // value as-is (e.g. a typed array), so store the converted
97
+ // key, not the raw value.
98
+ return valueToKey(indexKey);
93
99
  } catch (e) {
94
100
  return;
95
101
  }
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
- }
102
+ }
103
+
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(valueToKey(part));
111
+ } catch (err) {
112
+ /* Do nothing */
107
113
  }
108
114
  }
109
- indexKey = keep;
115
+ }
116
+ return keep;
117
+ }
118
+
119
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-storing-a-record-into-an-object-store (step 7)
120
+ storeRecord(newRecord) {
121
+ const indexKey = this.getIndexKey(newRecord.value);
122
+ if (indexKey === undefined) {
123
+ return;
110
124
  }
111
125
  if (!this.multiEntry || !Array.isArray(indexKey)) {
112
126
  if (this.unique) {
@@ -115,6 +129,10 @@ class Index {
115
129
  throw new ConstraintError();
116
130
  }
117
131
  }
132
+ this.records.put({
133
+ key: indexKey,
134
+ value: newRecord.key
135
+ });
118
136
  } else {
119
137
  if (this.unique) {
120
138
  for (const individualIndexKey of indexKey) {
@@ -124,13 +142,6 @@ class Index {
124
142
  }
125
143
  }
126
144
  }
127
- }
128
- if (!this.multiEntry || !Array.isArray(indexKey)) {
129
- this.records.put({
130
- key: indexKey,
131
- value: newRecord.key
132
- });
133
- } else {
134
145
  for (const individualIndexKey of indexKey) {
135
146
  this.records.put({
136
147
  key: individualIndexKey,
@@ -139,6 +150,22 @@ class Index {
139
150
  }
140
151
  }
141
152
  }
153
+
154
+ // Remove the index records that storeRecord created for this object store record. Looking them up by index key is
155
+ // O(log n) per index record, where scanning the whole index for records pointing at this primary key is O(n).
156
+ deleteRecord(record) {
157
+ const indexKey = this.getIndexKey(record.value);
158
+ if (indexKey === undefined) {
159
+ return;
160
+ }
161
+ if (!this.multiEntry || !Array.isArray(indexKey)) {
162
+ this.records.deleteByKeyAndValue(indexKey, record.key);
163
+ } else {
164
+ for (const individualIndexKey of indexKey) {
165
+ this.records.deleteByKeyAndValue(individualIndexKey, record.key);
166
+ }
167
+ }
168
+ }
142
169
  initialize(transaction) {
143
170
  if (this.initialized) {
144
171
  throw new Error("Index already initialized");
@@ -2,6 +2,7 @@ import FDBRecord from "../FDBRecord.js";
2
2
  import { DataError } from "./errors.js";
3
3
  import extractKey from "./extractKey.js";
4
4
  import KeyGenerator from "./KeyGenerator.js";
5
+ import valueToKey from "./valueToKey.js";
5
6
  import RecordStore from "./RecordStore.js";
6
7
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-object-store
7
8
  class ObjectStore {
@@ -79,7 +80,11 @@ class ObjectStore {
79
80
  if (this.keyPath !== null) {
80
81
  const key = extractKey(this.keyPath, newRecord.value).key;
81
82
  if (key !== undefined) {
82
- newRecord.key = key;
83
+ // "Convert a value to a key": a single key path returns the
84
+ // value as-is (e.g. a typed array), so convert it here. For
85
+ // an array key path, extractKey already converted each part
86
+ // and valueToKey is idempotent on converted keys.
87
+ newRecord.key = valueToKey(key);
83
88
  }
84
89
  }
85
90
  const rollbackLogForThisOperation = [];
@@ -169,7 +174,7 @@ class ObjectStore {
169
174
  // Delete existing indexes
170
175
  if (existingRecord) {
171
176
  for (const rawIndex of this.rawIndexes.values()) {
172
- rawIndex.records.deleteByValue(newRecord.key);
177
+ rawIndex.deleteRecord(existingRecord);
173
178
  }
174
179
  }
175
180
 
@@ -203,7 +208,9 @@ class ObjectStore {
203
208
  }
204
209
  }
205
210
  for (const rawIndex of this.rawIndexes.values()) {
206
- rawIndex.records.deleteByValue(key);
211
+ for (const record of deletedRecords) {
212
+ rawIndex.deleteRecord(record);
213
+ }
207
214
  }
208
215
  }
209
216
 
@@ -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()];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fake-indexeddb",
3
- "version": "6.2.6",
3
+ "version": "6.2.8",
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": {
@@ -1,54 +0,0 @@
1
- "use strict";
2
-
3
- var assert = _interopRequireWildcard(require("assert"));
4
- var fakeIndexedDB = _interopRequireWildcard(require("../../index.js"));
5
- 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); }
6
- // `indexedDB` is read-only, all others are read-write
7
- const props = Object.keys(fakeIndexedDB).filter(prop => prop.startsWith("IDB") || prop === "indexedDB");
8
- describe("auto", () => {
9
- it("correctly overrides globals in fake-indexeddb/auto", async () => {
10
- // simulate what the native property descriptors do
11
- Object.defineProperty(globalThis, "indexedDB", {
12
- set: undefined,
13
- get: () => undefined,
14
- enumerable: false,
15
- configurable: true
16
- });
17
- for (const prop of props) {
18
- Object.defineProperty(globalThis, prop, {
19
- value: undefined,
20
- enumerable: false,
21
- configurable: true,
22
- writable: true
23
- });
24
- }
25
-
26
- // @ts-expect-error relative to the build/ directory
27
- await import("../../../../auto/index.mjs");
28
-
29
- // check our own globals
30
- for (const prop of props) {
31
- const descriptor = Object.getOwnPropertyDescriptor(globalThis, prop);
32
- assert.equal(descriptor.value, fakeIndexedDB[prop]);
33
- assert.equal(descriptor.enumerable, false);
34
- assert.equal(descriptor.configurable, true);
35
- assert.equal(descriptor.writable, true);
36
- }
37
-
38
- // check that we can still overwrite them with `globalThis.<prop> = ...`
39
- for (const prop of props) {
40
- const fake = {};
41
- globalThis[prop] = fake;
42
- assert.equal(globalThis[prop], fake);
43
- }
44
- });
45
- it("exports as cjs directly, without `default` member - issue #130", async () => {
46
- // @ts-expect-error relative to the build/ directory
47
- await import("../../../../auto/index.js");
48
-
49
- // ensure we directly set the export as `module.exports` rather than `module.exports.default`
50
- for (const prop of props) {
51
- assert.ok(!globalThis[prop].default);
52
- }
53
- });
54
- });