fake-indexeddb 6.2.1 → 6.2.3

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.
@@ -5,9 +5,9 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _scheduling = require("./scheduling.js");
8
+ var _intersection = require("./intersection.js");
8
9
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-database
9
10
  class Database {
10
- deletePending = false;
11
11
  transactions = [];
12
12
  rawObjectStores = new Map();
13
13
  connections = [];
@@ -18,18 +18,27 @@ class Database {
18
18
  }
19
19
  processTransactions() {
20
20
  (0, _scheduling.queueTask)(() => {
21
- const anyRunning = this.transactions.some(transaction => {
22
- return transaction._started && transaction._state !== "finished";
23
- });
24
- if (!anyRunning) {
25
- const next = this.transactions.find(transaction => {
26
- return !transaction._started && transaction._state !== "finished";
27
- });
28
- if (next) {
29
- next.addEventListener("complete", this.processTransactions);
30
- next.addEventListener("abort", this.processTransactions);
31
- next._start();
21
+ const running = this.transactions.filter(transaction => transaction._started && transaction._state !== "finished");
22
+ const waiting = this.transactions.filter(transaction => !transaction._started && transaction._state !== "finished");
23
+
24
+ // The next transaction to run is the first waiting one that doesn't overlap with either a running one or a
25
+ // preceding waiting one. This allows non-overlapping transactions to run in parallel.
26
+ const next = waiting.find((transaction, i) => {
27
+ const anyRunning = running.some(other => (0, _intersection.intersection)(other._scope, transaction._scope).size > 0);
28
+ if (anyRunning) {
29
+ return false;
32
30
  }
31
+
32
+ // If any _preceding_ waiting transactions are blocked, then that's also blocking.
33
+ // E.g. if you have 3 transactions: [a], [a,b], and [b,c], then [a] blocks [a,b] which blocks [b,c]
34
+ // until [a] is complete, even though [a] and [b,c] share no overlap.
35
+ const anyWaiting = waiting.slice(0, i).some(other => (0, _intersection.intersection)(other._scope, transaction._scope).size > 0);
36
+ return !anyWaiting;
37
+ });
38
+ if (next) {
39
+ next.addEventListener("complete", this.processTransactions);
40
+ next.addEventListener("abort", this.processTransactions);
41
+ next._start();
33
42
  }
34
43
  });
35
44
  }
@@ -12,6 +12,15 @@ const stopped = (event, listener) => {
12
12
  // http://www.w3.org/TR/dom/#concept-event-listener-invoke
13
13
  const invokeEventListeners = (event, obj) => {
14
14
  event.currentTarget = obj;
15
+ const errors = [];
16
+ const invoke = callback => {
17
+ try {
18
+ // @ts-expect-error EventCallback's types are not quite right here
19
+ callback.call(event.currentTarget, event);
20
+ } catch (err) {
21
+ errors.push(err);
22
+ }
23
+ };
15
24
 
16
25
  // The callback might cause obj.listeners to mutate as we traverse it.
17
26
  // Take a copy of the array so that nothing sneaks in and we don't lose
@@ -20,9 +29,7 @@ const invokeEventListeners = (event, obj) => {
20
29
  if (event.type !== listener.type || stopped(event, listener)) {
21
30
  continue;
22
31
  }
23
-
24
- // @ts-ignore
25
- listener.callback.call(event.currentTarget, event);
32
+ invoke(listener.callback);
26
33
  }
27
34
  const typeToProp = {
28
35
  abort: "onabort",
@@ -46,10 +53,15 @@ const invokeEventListeners = (event, obj) => {
46
53
  type: event.type
47
54
  };
48
55
  if (!stopped(event, listener)) {
49
- // @ts-ignore
50
- listener.callback.call(event.currentTarget, event);
56
+ invoke(listener.callback);
51
57
  }
52
58
  }
59
+
60
+ // we want to execute all listeners before deciding if we want to throw, because there could be an error thrown by
61
+ // the first listener, but the second should still be invoked
62
+ if (errors.length) {
63
+ throw new AggregateError(errors);
64
+ }
53
65
  };
54
66
  class FakeEventTarget {
55
67
  listeners = [];
@@ -4,11 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _FDBRecord = _interopRequireDefault(require("../FDBRecord.js"));
7
8
  var _errors = require("./errors.js");
8
9
  var _extractKey = _interopRequireDefault(require("./extractKey.js"));
9
10
  var _RecordStore = _interopRequireDefault(require("./RecordStore.js"));
10
11
  var _valueToKey = _interopRequireDefault(require("./valueToKey.js"));
11
- var _FDBRecord = _interopRequireDefault(require("../FDBRecord.js"));
12
12
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
13
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-index
14
14
  class Index {
@@ -4,11 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _FDBRecord = _interopRequireDefault(require("../FDBRecord.js"));
7
8
  var _errors = require("./errors.js");
8
9
  var _extractKey = _interopRequireDefault(require("./extractKey.js"));
9
10
  var _KeyGenerator = _interopRequireDefault(require("./KeyGenerator.js"));
10
11
  var _RecordStore = _interopRequireDefault(require("./RecordStore.js"));
11
- var _FDBRecord = _interopRequireDefault(require("../FDBRecord.js"));
12
12
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
13
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-object-store
14
14
  class ObjectStore {
@@ -4,8 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _cmp = _interopRequireDefault(require("./cmp.js"));
8
7
  var _FDBKeyRange = _interopRequireDefault(require("../FDBKeyRange.js"));
8
+ var _cmp = _interopRequireDefault(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?
@@ -11,8 +11,8 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
11
11
  const extractGetAllOptions = (queryOrOptions, count, numArguments) => {
12
12
  let query;
13
13
  let direction;
14
- if ((0, _isPotentiallyValidKeyRange.default)(queryOrOptions)) {
15
- // queryOrOptions is FDBKeyRange | Key
14
+ if (queryOrOptions === undefined || queryOrOptions === null || (0, _isPotentiallyValidKeyRange.default)(queryOrOptions)) {
15
+ // queryOrOptions is FDBKeyRange | Key | null | undefined
16
16
  query = queryOrOptions;
17
17
  if (numArguments > 1 && count !== undefined) {
18
18
  count = (0, _enforceRange.default)(count, "unsigned long");
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.intersection = intersection;
7
+ /**
8
+ * Minimal polyfill of `Set.prototype.intersection`, available in Node 22+.
9
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/intersection
10
+ * @param set1
11
+ * @param set2
12
+ */
13
+ function intersection(set1, set2) {
14
+ if ("intersection" in set1) {
15
+ return set1.intersection(set2);
16
+ }
17
+ return new Set([...set1].filter(item => set2.has(item)));
18
+ }
@@ -5,29 +5,22 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _FDBKeyRange = _interopRequireDefault(require("../FDBKeyRange.js"));
8
- var _isSharedArrayBuffer = _interopRequireDefault(require("./isSharedArrayBuffer.js"));
8
+ var _valueToKeyWithoutThrowing = _interopRequireWildcard(require("./valueToKeyWithoutThrowing.js"));
9
+ 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); }
9
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
11
  // https://www.w3.org/TR/IndexedDB/#is-a-potentially-valid-key-range
11
12
  const isPotentiallyValidKeyRange = value => {
12
- // Many of these conditions recapitulate the same conditions in `valueToKeyRange.ts`
13
- return (
14
- // FDBKeyRange
15
- value instanceof _FDBKeyRange.default ||
16
- // null/undefined - nullDisallowedFlag=false is assumed here
17
- value === null || value === undefined ||
18
- // Number
19
- typeof value === "number" ||
20
- // Date
21
- Object.prototype.toString.call(value) === "[object Date]" ||
22
- // string
23
- typeof value === "string" ||
24
- // buffer source type
25
- // note: we are explicitly _not_ checking for detachedness here, to match Chromium's behavior
26
- // see: https://github.com/w3c/IndexedDB/issues/465
27
- value instanceof ArrayBuffer || (0, _isSharedArrayBuffer.default)(value) || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(value) ||
28
- // array exotic type
29
- Array.isArray(value)
30
- );
13
+ // If value is a key range, return true.
14
+ if (value instanceof _FDBKeyRange.default) {
15
+ return true;
16
+ }
17
+
18
+ // Let key be the result of converting a value to a key with value.
19
+ const key = (0, _valueToKeyWithoutThrowing.default)(value);
20
+
21
+ // If key is "invalid type" return false.
22
+ // Else return true.
23
+ return key !== _valueToKeyWithoutThrowing.INVALID_TYPE;
31
24
  };
32
25
  var _default = exports.default = isPotentiallyValidKeyRange;
33
26
  module.exports = exports.default;
@@ -25,10 +25,10 @@ function getSetImmediateFromJsdom() {
25
25
 
26
26
  // waiting on this PR for typescript types: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1249
27
27
 
28
- // 'user-blocking' runs right after microtasks, so equivalent to setTimeout but without the 4ms clamping
29
- const schedulerPostTask = typeof scheduler !== "undefined" && (fn => scheduler.postTask(fn, {
30
- priority: "user-blocking"
31
- }));
28
+ // 'postTask' runs right after microtasks, so equivalent to setTimeout but without the 4ms clamping.
29
+ // Using the default priority of 'user-visible' to avoid blocking input while still running fairly quickly.
30
+ // See: https://developer.mozilla.org/en-US/docs/Web/API/Prioritized_Task_Scheduling_API#task_priorities
31
+ const schedulerPostTask = typeof scheduler !== "undefined" && (fn => scheduler.postTask(fn));
32
32
 
33
33
  // fallback for environments that don't support any of the above
34
34
  const doSetTimeout = fn => setTimeout(fn, 0);
@@ -5,63 +5,18 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _errors = require("./errors.js");
8
- var _isSharedArrayBuffer = _interopRequireDefault(require("./isSharedArrayBuffer.js"));
9
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
+ var _valueToKeyWithoutThrowing = _interopRequireWildcard(require("./valueToKeyWithoutThrowing.js"));
9
+ 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); }
10
10
  // https://w3c.github.io/IndexedDB/#convert-value-to-key
11
+ // Plus throwing a DataError for invalid value/invalid key, which is commonly done
12
+ // in lots of IndexedDB operations
11
13
  const valueToKey = (input, seen) => {
12
- if (typeof input === "number") {
13
- if (isNaN(input)) {
14
- throw new _errors.DataError();
15
- }
16
- return input;
17
- } else if (Object.prototype.toString.call(input) === "[object Date]") {
18
- const ms = input.valueOf();
19
- if (isNaN(ms)) {
20
- throw new _errors.DataError();
21
- }
22
- return new Date(ms);
23
- } else if (typeof input === "string") {
24
- return input;
25
- } else if (
26
- // https://w3c.github.io/IndexedDB/#ref-for-dfn-buffer-source-type
27
- (input instanceof ArrayBuffer || (0, _isSharedArrayBuffer.default)(input) || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) &&
28
- // We can't consistently test detachedness, so instead we check if byteLength === 0
29
- // This isn't foolproof, but there's no perfect way to detect if Uint8Arrays or
30
- // SharedArrayBuffers are detached
31
- !("detached" in input ? input.detached : input.byteLength === 0)) {
32
- let arrayBuffer;
33
- let offset = 0;
34
- let length = 0;
35
- if (input instanceof ArrayBuffer || (0, _isSharedArrayBuffer.default)(input)) {
36
- arrayBuffer = input;
37
- length = input.byteLength;
38
- } else {
39
- arrayBuffer = input.buffer;
40
- offset = input.byteOffset;
41
- length = input.byteLength;
42
- }
43
- return arrayBuffer.slice(offset, offset + length);
44
- } else if (Array.isArray(input)) {
45
- if (seen === undefined) {
46
- seen = new Set();
47
- } else if (seen.has(input)) {
48
- throw new _errors.DataError();
49
- }
50
- seen.add(input);
51
- const keys = [];
52
- for (let i = 0; i < input.length; i++) {
53
- const hop = Object.hasOwn(input, i);
54
- if (!hop) {
55
- throw new _errors.DataError();
56
- }
57
- const entry = input[i];
58
- const key = valueToKey(entry, seen);
59
- keys.push(key);
60
- }
61
- return keys;
62
- } else {
14
+ const result = (0, _valueToKeyWithoutThrowing.default)(input, seen);
15
+ if (result === _valueToKeyWithoutThrowing.INVALID_VALUE || result === _valueToKeyWithoutThrowing.INVALID_TYPE) {
16
+ // If key is "invalid value" or "invalid type", throw a "DataError" DOMException
63
17
  throw new _errors.DataError();
64
18
  }
19
+ return result;
65
20
  };
66
21
  var _default = exports.default = valueToKey;
67
22
  module.exports = exports.default;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.INVALID_VALUE = exports.INVALID_TYPE = void 0;
7
+ var _isSharedArrayBuffer = _interopRequireDefault(require("./isSharedArrayBuffer.js"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
+ const INVALID_TYPE = exports.INVALID_TYPE = Symbol("INVALID_TYPE");
10
+ const INVALID_VALUE = exports.INVALID_VALUE = Symbol("INVALID_VALUE");
11
+
12
+ // https://w3c.github.io/IndexedDB/#convert-value-to-key
13
+ // The "without exceptions" version is because we typically want to throw exceptions (DataError) but not for
14
+ // the "is potentially valid key range" routine.
15
+ const valueToKeyWithoutThrowing = (input, seen) => {
16
+ if (typeof input === "number") {
17
+ if (isNaN(input)) {
18
+ // If input is NaN then return "invalid value".
19
+ return INVALID_VALUE;
20
+ }
21
+ return input;
22
+ } else if (Object.prototype.toString.call(input) === "[object Date]") {
23
+ const ms = input.valueOf();
24
+ if (isNaN(ms)) {
25
+ // If ms is NaN then return "invalid value".
26
+ return INVALID_VALUE;
27
+ }
28
+ return new Date(ms);
29
+ } else if (typeof input === "string") {
30
+ return input;
31
+ } else if (
32
+ // https://w3c.github.io/IndexedDB/#ref-for-dfn-buffer-source-type
33
+ input instanceof ArrayBuffer || (0, _isSharedArrayBuffer.default)(input) || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
34
+ // We can't consistently test detachedness, so instead we check if byteLength === 0
35
+ // This isn't foolproof, but there's no perfect way to detect if Uint8Arrays or
36
+ // SharedArrayBuffers are detached
37
+ if ("detached" in input ? input.detached : input.byteLength === 0) {
38
+ // If input is detached then return "invalid value".
39
+ return INVALID_VALUE;
40
+ }
41
+ let arrayBuffer;
42
+ let offset = 0;
43
+ let length = 0;
44
+ if (input instanceof ArrayBuffer || (0, _isSharedArrayBuffer.default)(input)) {
45
+ arrayBuffer = input;
46
+ length = input.byteLength;
47
+ } else {
48
+ arrayBuffer = input.buffer;
49
+ offset = input.byteOffset;
50
+ length = input.byteLength;
51
+ }
52
+ return arrayBuffer.slice(offset, offset + length);
53
+ } else if (Array.isArray(input)) {
54
+ if (seen === undefined) {
55
+ seen = new Set();
56
+ } else if (seen.has(input)) {
57
+ // If seen contains input, then return "invalid value".
58
+ return INVALID_VALUE;
59
+ }
60
+ seen.add(input);
61
+ const keys = [];
62
+ for (let i = 0; i < input.length; i++) {
63
+ const hop = Object.hasOwn(input, i);
64
+ if (!hop) {
65
+ // If hop is false, return "invalid value".
66
+ return INVALID_VALUE;
67
+ }
68
+ const entry = input[i];
69
+ const key = valueToKeyWithoutThrowing(entry, seen);
70
+ // If key is "invalid value" or "invalid type" abort these steps and return "invalid value".
71
+ if (key === INVALID_VALUE || key === INVALID_TYPE) {
72
+ return INVALID_VALUE;
73
+ }
74
+ keys.push(key);
75
+ }
76
+ return keys;
77
+ } else {
78
+ // Otherwise: Return "invalid type".
79
+ return INVALID_TYPE;
80
+ }
81
+ };
82
+ var _default = exports.default = valueToKeyWithoutThrowing;