fake-indexeddb 6.2.5 → 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.
@@ -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
+ });
@@ -1,6 +1,6 @@
1
1
  import FDBKeyRange from "./FDBKeyRange.js";
2
2
  import FDBObjectStore from "./FDBObjectStore.js";
3
- import cmp from "./lib/cmp.js";
3
+ import cmp, { cmpKeys } from "./lib/cmp.js";
4
4
  import { DataError, InvalidAccessError, InvalidStateError, ReadOnlyError, TransactionInactiveError } from "./lib/errors.js";
5
5
  import extractKey from "./lib/extractKey.js";
6
6
  import valueToKey from "./lib/valueToKey.js";
@@ -26,7 +26,7 @@ const makeKeyRange = (range, lowers, uppers) => {
26
26
  if (lowerTemp === undefined) {
27
27
  continue;
28
28
  }
29
- if (lower === undefined || cmp(lower, lowerTemp) === 1) {
29
+ if (lower === undefined || cmpKeys(lower, lowerTemp) === 1) {
30
30
  lower = lowerTemp;
31
31
  }
32
32
  }
@@ -34,7 +34,7 @@ const makeKeyRange = (range, lowers, uppers) => {
34
34
  if (upperTemp === undefined) {
35
35
  continue;
36
36
  }
37
- if (upper === undefined || cmp(upper, upperTemp) === -1) {
37
+ if (upper === undefined || cmpKeys(upper, upperTemp) === -1) {
38
38
  upper = upperTemp;
39
39
  }
40
40
  }
@@ -107,8 +107,8 @@ class FDBCursor {
107
107
  if (this.direction === "next") {
108
108
  const range = makeKeyRange(this._range, [key, this._position], []);
109
109
  for (const record of records.values(range)) {
110
- const cmpResultKey = key !== undefined ? cmp(record.key, key) : undefined;
111
- const cmpResultPosition = this._position !== undefined ? cmp(record.key, this._position) : undefined;
110
+ const cmpResultKey = key !== undefined ? cmpKeys(record.key, key) : undefined;
111
+ const cmpResultPosition = this._position !== undefined ? cmpKeys(record.key, this._position) : undefined;
112
112
  if (key !== undefined) {
113
113
  if (cmpResultKey === -1) {
114
114
  continue;
@@ -118,7 +118,7 @@ class FDBCursor {
118
118
  if (cmpResultKey === -1) {
119
119
  continue;
120
120
  }
121
- const cmpResultPrimaryKey = cmp(record.value, primaryKey);
121
+ const cmpResultPrimaryKey = cmpKeys(record.value, primaryKey);
122
122
  if (cmpResultKey === 0 && cmpResultPrimaryKey === -1) {
123
123
  continue;
124
124
  }
@@ -132,7 +132,7 @@ class FDBCursor {
132
132
  if (cmpResultPosition === -1) {
133
133
  continue;
134
134
  }
135
- if (cmpResultPosition === 0 && cmp(record.value, this._objectStorePosition) !== 1) {
135
+ if (cmpResultPosition === 0 && cmpKeys(record.value, this._objectStorePosition) !== 1) {
136
136
  continue;
137
137
  }
138
138
  }
@@ -151,12 +151,12 @@ class FDBCursor {
151
151
  const range = makeKeyRange(this._range, [key, this._position], []);
152
152
  for (const record of records.values(range)) {
153
153
  if (key !== undefined) {
154
- if (cmp(record.key, key) === -1) {
154
+ if (cmpKeys(record.key, key) === -1) {
155
155
  continue;
156
156
  }
157
157
  }
158
158
  if (this._position !== undefined) {
159
- if (cmp(record.key, this._position) !== 1) {
159
+ if (cmpKeys(record.key, this._position) !== 1) {
160
160
  continue;
161
161
  }
162
162
  }
@@ -171,8 +171,8 @@ class FDBCursor {
171
171
  } else if (this.direction === "prev") {
172
172
  const range = makeKeyRange(this._range, [], [key, this._position]);
173
173
  for (const record of records.values(range, "prev")) {
174
- const cmpResultKey = key !== undefined ? cmp(record.key, key) : undefined;
175
- const cmpResultPosition = this._position !== undefined ? cmp(record.key, this._position) : undefined;
174
+ const cmpResultKey = key !== undefined ? cmpKeys(record.key, key) : undefined;
175
+ const cmpResultPosition = this._position !== undefined ? cmpKeys(record.key, this._position) : undefined;
176
176
  if (key !== undefined) {
177
177
  if (cmpResultKey === 1) {
178
178
  continue;
@@ -182,7 +182,7 @@ class FDBCursor {
182
182
  if (cmpResultKey === 1) {
183
183
  continue;
184
184
  }
185
- const cmpResultPrimaryKey = cmp(record.value, primaryKey);
185
+ const cmpResultPrimaryKey = cmpKeys(record.value, primaryKey);
186
186
  if (cmpResultKey === 0 && cmpResultPrimaryKey === 1) {
187
187
  continue;
188
188
  }
@@ -196,7 +196,7 @@ class FDBCursor {
196
196
  if (cmpResultPosition === 1) {
197
197
  continue;
198
198
  }
199
- if (cmpResultPosition === 0 && cmp(record.value, this._objectStorePosition) !== -1) {
199
+ if (cmpResultPosition === 0 && cmpKeys(record.value, this._objectStorePosition) !== -1) {
200
200
  continue;
201
201
  }
202
202
  }
@@ -213,12 +213,12 @@ class FDBCursor {
213
213
  const range = makeKeyRange(this._range, [], [key, this._position]);
214
214
  for (const record of records.values(range, "prev")) {
215
215
  if (key !== undefined) {
216
- if (cmp(record.key, key) === 1) {
216
+ if (cmpKeys(record.key, key) === 1) {
217
217
  continue;
218
218
  }
219
219
  }
220
220
  if (this._position !== undefined) {
221
- if (cmp(record.key, this._position) !== -1) {
221
+ if (cmpKeys(record.key, this._position) !== -1) {
222
222
  continue;
223
223
  }
224
224
  }
@@ -380,7 +380,7 @@ class FDBCursor {
380
380
  }
381
381
  if (key !== undefined) {
382
382
  key = valueToKey(key);
383
- const cmpResult = cmp(key, this._position);
383
+ const cmpResult = cmpKeys(key, this._position);
384
384
  if (cmpResult <= 0 && (this.direction === "next" || this.direction === "nextunique") || cmpResult >= 0 && (this.direction === "prev" || this.direction === "prevunique")) {
385
385
  throw new DataError();
386
386
  }
@@ -421,11 +421,12 @@ class FDBCursor {
421
421
  throw new DataError();
422
422
  }
423
423
  key = valueToKey(key);
424
- const cmpResult = cmp(key, this._position);
424
+ const cmpResult = cmpKeys(key, this._position);
425
425
  if (cmpResult === -1 && this.direction === "next" || cmpResult === 1 && this.direction === "prev") {
426
426
  throw new DataError();
427
427
  }
428
- const cmpResult2 = cmp(primaryKey, this._objectStorePosition);
428
+ primaryKey = valueToKey(primaryKey);
429
+ const cmpResult2 = cmpKeys(primaryKey, this._objectStorePosition);
429
430
  if (cmpResult === 0) {
430
431
  if (cmpResult2 <= 0 && this.direction === "next" || cmpResult2 >= 0 && this.direction === "prev") {
431
432
  throw new DataError();
@@ -1,4 +1,4 @@
1
- import cmp from "./lib/cmp.js";
1
+ import { cmpKeys } from "./lib/cmp.js";
2
2
  import { DataError } from "./lib/errors.js";
3
3
  import valueToKey from "./lib/valueToKey.js";
4
4
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#range-concept
@@ -28,12 +28,12 @@ class FDBKeyRange {
28
28
  if (arguments.length < 2) {
29
29
  throw new TypeError();
30
30
  }
31
- const cmpResult = cmp(lower, upper);
31
+ lower = valueToKey(lower);
32
+ upper = valueToKey(upper);
33
+ const cmpResult = cmpKeys(lower, upper);
32
34
  if (cmpResult === 1 || cmpResult === 0 && (lowerOpen || upperOpen)) {
33
35
  throw new DataError();
34
36
  }
35
- lower = valueToKey(lower);
36
- upper = valueToKey(upper);
37
37
  return new FDBKeyRange(lower, upper, lowerOpen, upperOpen);
38
38
  }
39
39
  constructor(lower, upper, lowerOpen, upperOpen) {
@@ -50,13 +50,13 @@ class FDBKeyRange {
50
50
  }
51
51
  key = valueToKey(key);
52
52
  if (this.lower !== undefined) {
53
- const cmpResult = cmp(this.lower, key);
53
+ const cmpResult = cmpKeys(this.lower, key);
54
54
  if (cmpResult === 1 || cmpResult === 0 && this.lowerOpen) {
55
55
  return false;
56
56
  }
57
57
  }
58
58
  if (this.upper !== undefined) {
59
- const cmpResult = cmp(this.upper, key);
59
+ const cmpResult = cmpKeys(this.upper, key);
60
60
  if (cmpResult === -1 || cmpResult === 0 && this.upperOpen) {
61
61
  return false;
62
62
  }
@@ -111,7 +111,7 @@ class FDBTransaction extends FakeEventTarget {
111
111
  request.result = undefined;
112
112
  }
113
113
  });
114
- this._state = "finished";
114
+ this._markFinished();
115
115
  }
116
116
  abort() {
117
117
  if (this._state === "committing" || this._state === "finished") {
@@ -244,13 +244,31 @@ class FDBTransaction extends FakeEventTarget {
244
244
  // Check if transaction complete event needs to be fired
245
245
  if (this._state !== "finished") {
246
246
  // Either aborted or committed already
247
- this._state = "finished";
247
+ this._markFinished();
248
248
  if (!this.error) {
249
249
  const event = new FakeEvent("complete");
250
250
  this.dispatchEvent(event);
251
251
  }
252
252
  }
253
253
  }
254
+ _markFinished() {
255
+ this._state = "finished";
256
+
257
+ // Remove from the list of transactions attached to the linked database to avoid
258
+ // indefinite memory bloat (issue #170)
259
+ // Note we do this in a macrotask since other consumers immediately check if it's in the array
260
+ queueTask(() => {
261
+ const {
262
+ transactions
263
+ } = this.db._rawDatabase;
264
+ const idx = transactions.indexOf(this);
265
+ if (idx !== -1) {
266
+ transactions.splice(idx, 1);
267
+ } else {
268
+ throw new Error("should have been able to remove transaction");
269
+ }
270
+ });
271
+ }
254
272
  commit() {
255
273
  if (this._state !== "active") {
256
274
  throw new InvalidStateError();
@@ -1,5 +1,5 @@
1
1
  import FDBKeyRange from "../FDBKeyRange.js";
2
- import cmp from "./cmp.js";
2
+ import { cmpKeys } from "./cmp.js";
3
3
  import BinarySearchTree from "./binarySearchTree.js";
4
4
  class RecordStore {
5
5
  constructor(keysAreUnique) {
@@ -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 && cmp(previousValue.key, current.value.key) === 0) {
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 && cmp(current.value.key, nextResult.value.key) === 0) {
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 cmp from "./cmp.js";
2
+ import { cmpKeys } from "./cmp.js";
3
3
  import { ConstraintError } from "./errors.js";
4
4
  // what fraction of the total number of nodes are allowed to be deleted tombstones?
5
5
  const MAX_TOMBSTONE_FACTOR = 2 / 3;
@@ -38,13 +38,13 @@ export default class BinarySearchTree {
38
38
  return !!this.get(record);
39
39
  }
40
40
  _compare(a, b) {
41
- const keyComparison = cmp(a.key, b.key);
41
+ const keyComparison = cmpKeys(a.key, b.key);
42
42
  if (keyComparison !== 0) {
43
43
  return keyComparison;
44
44
  }
45
45
  // if keys are unique, then we can (and must) avoid comparing the values, since they may be non-comparable
46
46
  // (e.g. in the case of an ObjectStore, they are record objects)
47
- return this._keysAreUnique ? 0 : cmp(a.value, b.value);
47
+ return this._keysAreUnique ? 0 : cmpKeys(a.value, b.value);
48
48
  }
49
49
  _getByComparator(node, comparator) {
50
50
  let current = node;
@@ -179,8 +179,8 @@ export default class BinarySearchTree {
179
179
  key
180
180
  }
181
181
  } = node;
182
- const lowerComparison = lower === undefined ? -1 : cmp(lower, key);
183
- const upperComparison = upper === undefined ? 1 : cmp(upper, key);
182
+ const lowerComparison = lower === undefined ? -1 : cmpKeys(lower, key);
183
+ const upperComparison = upper === undefined ? 1 : cmpKeys(upper, key);
184
184
 
185
185
  // if keys are non-unique then we need to go left/right even for equality
186
186
  // else we can just do LT/GT rather than LTE/GTE as a slight optimization
@@ -273,14 +273,12 @@ export default class BinarySearchTree {
273
273
  }
274
274
 
275
275
  // rebuild the whole tree from scratch, used to avoid too many deletion tombstones accumulating
276
- _rebuild(records, parent, red) {
277
- const {
278
- length
279
- } = records;
280
- if (!length) {
276
+ _rebuild(records, parent, red, startIndex = 0, endIndex = records.length) {
277
+ const length = endIndex - startIndex;
278
+ if (length <= 0) {
281
279
  return undefined;
282
280
  }
283
- const mid = length >>> 1; // like Math.floor(records.length / 2) but fast
281
+ const mid = startIndex + (length >>> 1); // the >>> is like Math.floor(length / 2) but fast
284
282
 
285
283
  const node = {
286
284
  record: records[mid],
@@ -290,8 +288,8 @@ export default class BinarySearchTree {
290
288
  deleted: false,
291
289
  red
292
290
  };
293
- const left = this._rebuild(records.slice(0, mid), node, !red);
294
- const right = this._rebuild(records.slice(mid + 1), node, !red);
291
+ const left = this._rebuild(records, node, !red, startIndex, mid);
292
+ const right = this._rebuild(records, node, !red, mid + 1, endIndex);
295
293
  node.left = left;
296
294
  node.right = right;
297
295
  return node;
@@ -19,13 +19,15 @@ const getType = x => {
19
19
  throw new DataError();
20
20
  };
21
21
 
22
- // https://w3c.github.io/IndexedDB/#compare-two-keys
23
- const cmp = (first, second) => {
24
- if (second === undefined) {
25
- throw new TypeError();
26
- }
27
- first = valueToKey(first);
28
- second = valueToKey(second);
22
+ /**
23
+ * Compare two already-normalized IndexedDB keys (i.e. keys that have already
24
+ * been run through `valueToKey`). Use this on internal/hot paths where keys
25
+ * are known to be valid to avoid redundant `valueToKey` conversions.
26
+ *
27
+ * For comparing raw/untrusted input, use the default-exported `cmp` instead —
28
+ * it validates and normalizes both arguments before comparing.
29
+ */
30
+ export const cmpKeys = (first, second) => {
29
31
  const t1 = getType(first);
30
32
  const t2 = getType(second);
31
33
  if (t1 !== t2) {
@@ -43,14 +45,34 @@ const cmp = (first, second) => {
43
45
  }
44
46
  return -1;
45
47
  }
48
+
49
+ // This block is effectively the same as the array comparison, but we avoid expensive `cmpKey` calls
46
50
  if (t1 === "Binary") {
47
- first = new Uint8Array(first);
48
- second = new Uint8Array(second);
51
+ const firstBytes = new Uint8Array(first);
52
+ const secondBytes = new Uint8Array(second);
53
+ const firstLen = firstBytes.length;
54
+ const secondLen = secondBytes.length;
55
+ const length = Math.min(firstLen, secondLen);
56
+ for (let i = 0; i < length; i++) {
57
+ if (firstBytes[i] > secondBytes[i]) {
58
+ return 1;
59
+ }
60
+ if (firstBytes[i] < secondBytes[i]) {
61
+ return -1;
62
+ }
63
+ }
64
+ if (firstLen > secondLen) {
65
+ return 1;
66
+ }
67
+ if (firstLen < secondLen) {
68
+ return -1;
69
+ }
70
+ return 0;
49
71
  }
50
- if (t1 === "Array" || t1 === "Binary") {
72
+ if (t1 === "Array") {
51
73
  const length = Math.min(first.length, second.length);
52
74
  for (let i = 0; i < length; i++) {
53
- const result = cmp(first[i], second[i]);
75
+ const result = cmpKeys(first[i], second[i]);
54
76
  if (result !== 0) {
55
77
  return result;
56
78
  }
@@ -74,4 +96,12 @@ const cmp = (first, second) => {
74
96
  }
75
97
  return first > second ? 1 : -1;
76
98
  };
99
+
100
+ // https://w3c.github.io/IndexedDB/#compare-two-keys
101
+ const cmp = (first, second) => {
102
+ if (second === undefined) {
103
+ throw new TypeError();
104
+ }
105
+ return cmpKeys(valueToKey(first), valueToKey(second));
106
+ };
77
107
  export default cmp;
@@ -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
+ });