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.
@@ -6,11 +6,12 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _FDBKeyRange = _interopRequireDefault(require("./FDBKeyRange.js"));
8
8
  var _FDBObjectStore = _interopRequireDefault(require("./FDBObjectStore.js"));
9
- var _cmp = _interopRequireDefault(require("./lib/cmp.js"));
9
+ var _cmp = _interopRequireWildcard(require("./lib/cmp.js"));
10
10
  var _errors = require("./lib/errors.js");
11
11
  var _extractKey = _interopRequireDefault(require("./lib/extractKey.js"));
12
12
  var _valueToKey = _interopRequireDefault(require("./lib/valueToKey.js"));
13
13
  var _cloneValueForInsertion = require("./lib/cloneValueForInsertion.js");
14
+ 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); }
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
16
  const getEffectiveObjectStore = cursor => {
16
17
  if (cursor.source instanceof _FDBObjectStore.default) {
@@ -33,7 +34,7 @@ const makeKeyRange = (range, lowers, uppers) => {
33
34
  if (lowerTemp === undefined) {
34
35
  continue;
35
36
  }
36
- if (lower === undefined || (0, _cmp.default)(lower, lowerTemp) === 1) {
37
+ if (lower === undefined || (0, _cmp.cmpKeys)(lower, lowerTemp) === 1) {
37
38
  lower = lowerTemp;
38
39
  }
39
40
  }
@@ -41,7 +42,7 @@ const makeKeyRange = (range, lowers, uppers) => {
41
42
  if (upperTemp === undefined) {
42
43
  continue;
43
44
  }
44
- if (upper === undefined || (0, _cmp.default)(upper, upperTemp) === -1) {
45
+ if (upper === undefined || (0, _cmp.cmpKeys)(upper, upperTemp) === -1) {
45
46
  upper = upperTemp;
46
47
  }
47
48
  }
@@ -114,8 +115,8 @@ class FDBCursor {
114
115
  if (this.direction === "next") {
115
116
  const range = makeKeyRange(this._range, [key, this._position], []);
116
117
  for (const record of records.values(range)) {
117
- const cmpResultKey = key !== undefined ? (0, _cmp.default)(record.key, key) : undefined;
118
- const cmpResultPosition = this._position !== undefined ? (0, _cmp.default)(record.key, this._position) : undefined;
118
+ const cmpResultKey = key !== undefined ? (0, _cmp.cmpKeys)(record.key, key) : undefined;
119
+ const cmpResultPosition = this._position !== undefined ? (0, _cmp.cmpKeys)(record.key, this._position) : undefined;
119
120
  if (key !== undefined) {
120
121
  if (cmpResultKey === -1) {
121
122
  continue;
@@ -125,7 +126,7 @@ class FDBCursor {
125
126
  if (cmpResultKey === -1) {
126
127
  continue;
127
128
  }
128
- const cmpResultPrimaryKey = (0, _cmp.default)(record.value, primaryKey);
129
+ const cmpResultPrimaryKey = (0, _cmp.cmpKeys)(record.value, primaryKey);
129
130
  if (cmpResultKey === 0 && cmpResultPrimaryKey === -1) {
130
131
  continue;
131
132
  }
@@ -139,7 +140,7 @@ class FDBCursor {
139
140
  if (cmpResultPosition === -1) {
140
141
  continue;
141
142
  }
142
- if (cmpResultPosition === 0 && (0, _cmp.default)(record.value, this._objectStorePosition) !== 1) {
143
+ if (cmpResultPosition === 0 && (0, _cmp.cmpKeys)(record.value, this._objectStorePosition) !== 1) {
143
144
  continue;
144
145
  }
145
146
  }
@@ -158,12 +159,12 @@ class FDBCursor {
158
159
  const range = makeKeyRange(this._range, [key, this._position], []);
159
160
  for (const record of records.values(range)) {
160
161
  if (key !== undefined) {
161
- if ((0, _cmp.default)(record.key, key) === -1) {
162
+ if ((0, _cmp.cmpKeys)(record.key, key) === -1) {
162
163
  continue;
163
164
  }
164
165
  }
165
166
  if (this._position !== undefined) {
166
- if ((0, _cmp.default)(record.key, this._position) !== 1) {
167
+ if ((0, _cmp.cmpKeys)(record.key, this._position) !== 1) {
167
168
  continue;
168
169
  }
169
170
  }
@@ -178,8 +179,8 @@ class FDBCursor {
178
179
  } else if (this.direction === "prev") {
179
180
  const range = makeKeyRange(this._range, [], [key, this._position]);
180
181
  for (const record of records.values(range, "prev")) {
181
- const cmpResultKey = key !== undefined ? (0, _cmp.default)(record.key, key) : undefined;
182
- const cmpResultPosition = this._position !== undefined ? (0, _cmp.default)(record.key, this._position) : undefined;
182
+ const cmpResultKey = key !== undefined ? (0, _cmp.cmpKeys)(record.key, key) : undefined;
183
+ const cmpResultPosition = this._position !== undefined ? (0, _cmp.cmpKeys)(record.key, this._position) : undefined;
183
184
  if (key !== undefined) {
184
185
  if (cmpResultKey === 1) {
185
186
  continue;
@@ -189,7 +190,7 @@ class FDBCursor {
189
190
  if (cmpResultKey === 1) {
190
191
  continue;
191
192
  }
192
- const cmpResultPrimaryKey = (0, _cmp.default)(record.value, primaryKey);
193
+ const cmpResultPrimaryKey = (0, _cmp.cmpKeys)(record.value, primaryKey);
193
194
  if (cmpResultKey === 0 && cmpResultPrimaryKey === 1) {
194
195
  continue;
195
196
  }
@@ -203,7 +204,7 @@ class FDBCursor {
203
204
  if (cmpResultPosition === 1) {
204
205
  continue;
205
206
  }
206
- if (cmpResultPosition === 0 && (0, _cmp.default)(record.value, this._objectStorePosition) !== -1) {
207
+ if (cmpResultPosition === 0 && (0, _cmp.cmpKeys)(record.value, this._objectStorePosition) !== -1) {
207
208
  continue;
208
209
  }
209
210
  }
@@ -220,12 +221,12 @@ class FDBCursor {
220
221
  const range = makeKeyRange(this._range, [], [key, this._position]);
221
222
  for (const record of records.values(range, "prev")) {
222
223
  if (key !== undefined) {
223
- if ((0, _cmp.default)(record.key, key) === 1) {
224
+ if ((0, _cmp.cmpKeys)(record.key, key) === 1) {
224
225
  continue;
225
226
  }
226
227
  }
227
228
  if (this._position !== undefined) {
228
- if ((0, _cmp.default)(record.key, this._position) !== -1) {
229
+ if ((0, _cmp.cmpKeys)(record.key, this._position) !== -1) {
229
230
  continue;
230
231
  }
231
232
  }
@@ -387,7 +388,7 @@ class FDBCursor {
387
388
  }
388
389
  if (key !== undefined) {
389
390
  key = (0, _valueToKey.default)(key);
390
- const cmpResult = (0, _cmp.default)(key, this._position);
391
+ const cmpResult = (0, _cmp.cmpKeys)(key, this._position);
391
392
  if (cmpResult <= 0 && (this.direction === "next" || this.direction === "nextunique") || cmpResult >= 0 && (this.direction === "prev" || this.direction === "prevunique")) {
392
393
  throw new _errors.DataError();
393
394
  }
@@ -428,11 +429,12 @@ class FDBCursor {
428
429
  throw new _errors.DataError();
429
430
  }
430
431
  key = (0, _valueToKey.default)(key);
431
- const cmpResult = (0, _cmp.default)(key, this._position);
432
+ const cmpResult = (0, _cmp.cmpKeys)(key, this._position);
432
433
  if (cmpResult === -1 && this.direction === "next" || cmpResult === 1 && this.direction === "prev") {
433
434
  throw new _errors.DataError();
434
435
  }
435
- const cmpResult2 = (0, _cmp.default)(primaryKey, this._objectStorePosition);
436
+ primaryKey = (0, _valueToKey.default)(primaryKey);
437
+ const cmpResult2 = (0, _cmp.cmpKeys)(primaryKey, this._objectStorePosition);
436
438
  if (cmpResult === 0) {
437
439
  if (cmpResult2 <= 0 && this.direction === "next" || cmpResult2 >= 0 && this.direction === "prev") {
438
440
  throw new _errors.DataError();
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _cmp = _interopRequireDefault(require("./lib/cmp.js"));
7
+ var _cmp = require("./lib/cmp.js");
8
8
  var _errors = require("./lib/errors.js");
9
9
  var _valueToKey = _interopRequireDefault(require("./lib/valueToKey.js"));
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -35,12 +35,12 @@ class FDBKeyRange {
35
35
  if (arguments.length < 2) {
36
36
  throw new TypeError();
37
37
  }
38
- const cmpResult = (0, _cmp.default)(lower, upper);
38
+ lower = (0, _valueToKey.default)(lower);
39
+ upper = (0, _valueToKey.default)(upper);
40
+ const cmpResult = (0, _cmp.cmpKeys)(lower, upper);
39
41
  if (cmpResult === 1 || cmpResult === 0 && (lowerOpen || upperOpen)) {
40
42
  throw new _errors.DataError();
41
43
  }
42
- lower = (0, _valueToKey.default)(lower);
43
- upper = (0, _valueToKey.default)(upper);
44
44
  return new FDBKeyRange(lower, upper, lowerOpen, upperOpen);
45
45
  }
46
46
  constructor(lower, upper, lowerOpen, upperOpen) {
@@ -57,13 +57,13 @@ class FDBKeyRange {
57
57
  }
58
58
  key = (0, _valueToKey.default)(key);
59
59
  if (this.lower !== undefined) {
60
- const cmpResult = (0, _cmp.default)(this.lower, key);
60
+ const cmpResult = (0, _cmp.cmpKeys)(this.lower, key);
61
61
  if (cmpResult === 1 || cmpResult === 0 && this.lowerOpen) {
62
62
  return false;
63
63
  }
64
64
  }
65
65
  if (this.upper !== undefined) {
66
- const cmpResult = (0, _cmp.default)(this.upper, key);
66
+ const cmpResult = (0, _cmp.cmpKeys)(this.upper, key);
67
67
  if (cmpResult === -1 || cmpResult === 0 && this.upperOpen) {
68
68
  return false;
69
69
  }
@@ -118,7 +118,7 @@ class FDBTransaction extends _FakeEventTarget.default {
118
118
  request.result = undefined;
119
119
  }
120
120
  });
121
- this._state = "finished";
121
+ this._markFinished();
122
122
  }
123
123
  abort() {
124
124
  if (this._state === "committing" || this._state === "finished") {
@@ -251,13 +251,31 @@ class FDBTransaction extends _FakeEventTarget.default {
251
251
  // Check if transaction complete event needs to be fired
252
252
  if (this._state !== "finished") {
253
253
  // Either aborted or committed already
254
- this._state = "finished";
254
+ this._markFinished();
255
255
  if (!this.error) {
256
256
  const event = new _FakeEvent.default("complete");
257
257
  this.dispatchEvent(event);
258
258
  }
259
259
  }
260
260
  }
261
+ _markFinished() {
262
+ this._state = "finished";
263
+
264
+ // Remove from the list of transactions attached to the linked database to avoid
265
+ // indefinite memory bloat (issue #170)
266
+ // Note we do this in a macrotask since other consumers immediately check if it's in the array
267
+ (0, _scheduling.queueTask)(() => {
268
+ const {
269
+ transactions
270
+ } = this.db._rawDatabase;
271
+ const idx = transactions.indexOf(this);
272
+ if (idx !== -1) {
273
+ transactions.splice(idx, 1);
274
+ } else {
275
+ throw new Error("should have been able to remove transaction");
276
+ }
277
+ });
278
+ }
261
279
  commit() {
262
280
  if (this._state !== "active") {
263
281
  throw new _errors.InvalidStateError();
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _FDBKeyRange = _interopRequireDefault(require("../FDBKeyRange.js"));
8
- var _cmp = _interopRequireDefault(require("./cmp.js"));
8
+ var _cmp = require("./cmp.js");
9
9
  var _binarySearchTree = _interopRequireDefault(require("./binarySearchTree.js"));
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
11
  class RecordStore {
@@ -75,7 +75,7 @@ class RecordStore {
75
75
  next: () => {
76
76
  let current = next();
77
77
  // for nextunique, continue if we already emitted the lowest unique value
78
- while (!current.done && previousValue !== undefined && (0, _cmp.default)(previousValue.key, current.value.key) === 0) {
78
+ while (!current.done && previousValue !== undefined && (0, _cmp.cmpKeys)(previousValue.key, current.value.key) === 0) {
79
79
  current = next();
80
80
  }
81
81
  previousValue = current.value;
@@ -90,7 +90,7 @@ class RecordStore {
90
90
  let nextResult = next();
91
91
  return {
92
92
  next: () => {
93
- while (!nextResult.done && (0, _cmp.default)(current.value.key, nextResult.value.key) === 0) {
93
+ while (!nextResult.done && (0, _cmp.cmpKeys)(current.value.key, nextResult.value.key) === 0) {
94
94
  // note we return the _lowest_ possible value, hence set the current
95
95
  current = nextResult;
96
96
  nextResult = next();
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _FDBKeyRange = _interopRequireDefault(require("../FDBKeyRange.js"));
8
- var _cmp = _interopRequireDefault(require("./cmp.js"));
8
+ var _cmp = require("./cmp.js");
9
9
  var _errors = require("./errors.js");
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
11
  // what fraction of the total number of nodes are allowed to be deleted tombstones?
@@ -45,13 +45,13 @@ class BinarySearchTree {
45
45
  return !!this.get(record);
46
46
  }
47
47
  _compare(a, b) {
48
- const keyComparison = (0, _cmp.default)(a.key, b.key);
48
+ const keyComparison = (0, _cmp.cmpKeys)(a.key, b.key);
49
49
  if (keyComparison !== 0) {
50
50
  return keyComparison;
51
51
  }
52
52
  // if keys are unique, then we can (and must) avoid comparing the values, since they may be non-comparable
53
53
  // (e.g. in the case of an ObjectStore, they are record objects)
54
- return this._keysAreUnique ? 0 : (0, _cmp.default)(a.value, b.value);
54
+ return this._keysAreUnique ? 0 : (0, _cmp.cmpKeys)(a.value, b.value);
55
55
  }
56
56
  _getByComparator(node, comparator) {
57
57
  let current = node;
@@ -186,8 +186,8 @@ class BinarySearchTree {
186
186
  key
187
187
  }
188
188
  } = node;
189
- const lowerComparison = lower === undefined ? -1 : (0, _cmp.default)(lower, key);
190
- const upperComparison = upper === undefined ? 1 : (0, _cmp.default)(upper, key);
189
+ const lowerComparison = lower === undefined ? -1 : (0, _cmp.cmpKeys)(lower, key);
190
+ const upperComparison = upper === undefined ? 1 : (0, _cmp.cmpKeys)(upper, key);
191
191
 
192
192
  // if keys are non-unique then we need to go left/right even for equality
193
193
  // else we can just do LT/GT rather than LTE/GTE as a slight optimization
@@ -280,14 +280,12 @@ class BinarySearchTree {
280
280
  }
281
281
 
282
282
  // rebuild the whole tree from scratch, used to avoid too many deletion tombstones accumulating
283
- _rebuild(records, parent, red) {
284
- const {
285
- length
286
- } = records;
287
- if (!length) {
283
+ _rebuild(records, parent, red, startIndex = 0, endIndex = records.length) {
284
+ const length = endIndex - startIndex;
285
+ if (length <= 0) {
288
286
  return undefined;
289
287
  }
290
- const mid = length >>> 1; // like Math.floor(records.length / 2) but fast
288
+ const mid = startIndex + (length >>> 1); // the >>> is like Math.floor(length / 2) but fast
291
289
 
292
290
  const node = {
293
291
  record: records[mid],
@@ -297,8 +295,8 @@ class BinarySearchTree {
297
295
  deleted: false,
298
296
  red
299
297
  };
300
- const left = this._rebuild(records.slice(0, mid), node, !red);
301
- const right = this._rebuild(records.slice(mid + 1), node, !red);
298
+ const left = this._rebuild(records, node, !red, startIndex, mid);
299
+ const right = this._rebuild(records, node, !red, mid + 1, endIndex);
302
300
  node.left = left;
303
301
  node.right = right;
304
302
  return node;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.cmpKeys = void 0;
7
7
  var _errors = require("./errors.js");
8
8
  var _valueToKey = _interopRequireDefault(require("./valueToKey.js"));
9
9
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -26,13 +26,15 @@ const getType = x => {
26
26
  throw new _errors.DataError();
27
27
  };
28
28
 
29
- // https://w3c.github.io/IndexedDB/#compare-two-keys
30
- const cmp = (first, second) => {
31
- if (second === undefined) {
32
- throw new TypeError();
33
- }
34
- first = (0, _valueToKey.default)(first);
35
- second = (0, _valueToKey.default)(second);
29
+ /**
30
+ * Compare two already-normalized IndexedDB keys (i.e. keys that have already
31
+ * been run through `valueToKey`). Use this on internal/hot paths where keys
32
+ * are known to be valid to avoid redundant `valueToKey` conversions.
33
+ *
34
+ * For comparing raw/untrusted input, use the default-exported `cmp` instead —
35
+ * it validates and normalizes both arguments before comparing.
36
+ */
37
+ const cmpKeys = (first, second) => {
36
38
  const t1 = getType(first);
37
39
  const t2 = getType(second);
38
40
  if (t1 !== t2) {
@@ -50,14 +52,34 @@ const cmp = (first, second) => {
50
52
  }
51
53
  return -1;
52
54
  }
55
+
56
+ // This block is effectively the same as the array comparison, but we avoid expensive `cmpKey` calls
53
57
  if (t1 === "Binary") {
54
- first = new Uint8Array(first);
55
- second = new Uint8Array(second);
58
+ const firstBytes = new Uint8Array(first);
59
+ const secondBytes = new Uint8Array(second);
60
+ const firstLen = firstBytes.length;
61
+ const secondLen = secondBytes.length;
62
+ const length = Math.min(firstLen, secondLen);
63
+ for (let i = 0; i < length; i++) {
64
+ if (firstBytes[i] > secondBytes[i]) {
65
+ return 1;
66
+ }
67
+ if (firstBytes[i] < secondBytes[i]) {
68
+ return -1;
69
+ }
70
+ }
71
+ if (firstLen > secondLen) {
72
+ return 1;
73
+ }
74
+ if (firstLen < secondLen) {
75
+ return -1;
76
+ }
77
+ return 0;
56
78
  }
57
- if (t1 === "Array" || t1 === "Binary") {
79
+ if (t1 === "Array") {
58
80
  const length = Math.min(first.length, second.length);
59
81
  for (let i = 0; i < length; i++) {
60
- const result = cmp(first[i], second[i]);
82
+ const result = cmpKeys(first[i], second[i]);
61
83
  if (result !== 0) {
62
84
  return result;
63
85
  }
@@ -81,5 +103,13 @@ const cmp = (first, second) => {
81
103
  }
82
104
  return first > second ? 1 : -1;
83
105
  };
84
- var _default = exports.default = cmp;
85
- module.exports = exports.default;
106
+
107
+ // https://w3c.github.io/IndexedDB/#compare-two-keys
108
+ exports.cmpKeys = cmpKeys;
109
+ const cmp = (first, second) => {
110
+ if (second === undefined) {
111
+ throw new TypeError();
112
+ }
113
+ return cmpKeys((0, _valueToKey.default)(first), (0, _valueToKey.default)(second));
114
+ };
115
+ var _default = exports.default = cmp;
@@ -0,0 +1,54 @@
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
+ });