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.
@@ -6,6 +6,15 @@ const stopped = (event, listener) => {
6
6
  // http://www.w3.org/TR/dom/#concept-event-listener-invoke
7
7
  const invokeEventListeners = (event, obj) => {
8
8
  event.currentTarget = obj;
9
+ const errors = [];
10
+ const invoke = callback => {
11
+ try {
12
+ // @ts-expect-error EventCallback's types are not quite right here
13
+ callback.call(event.currentTarget, event);
14
+ } catch (err) {
15
+ errors.push(err);
16
+ }
17
+ };
9
18
 
10
19
  // The callback might cause obj.listeners to mutate as we traverse it.
11
20
  // Take a copy of the array so that nothing sneaks in and we don't lose
@@ -14,9 +23,7 @@ const invokeEventListeners = (event, obj) => {
14
23
  if (event.type !== listener.type || stopped(event, listener)) {
15
24
  continue;
16
25
  }
17
-
18
- // @ts-ignore
19
- listener.callback.call(event.currentTarget, event);
26
+ invoke(listener.callback);
20
27
  }
21
28
  const typeToProp = {
22
29
  abort: "onabort",
@@ -40,10 +47,15 @@ const invokeEventListeners = (event, obj) => {
40
47
  type: event.type
41
48
  };
42
49
  if (!stopped(event, listener)) {
43
- // @ts-ignore
44
- listener.callback.call(event.currentTarget, event);
50
+ invoke(listener.callback);
45
51
  }
46
52
  }
53
+
54
+ // we want to execute all listeners before deciding if we want to throw, because there could be an error thrown by
55
+ // the first listener, but the second should still be invoked
56
+ if (errors.length) {
57
+ throw new AggregateError(errors);
58
+ }
47
59
  };
48
60
  class FakeEventTarget {
49
61
  listeners = [];
@@ -1,9 +1,8 @@
1
+ import FDBRecord from "../FDBRecord.js";
1
2
  import { ConstraintError } from "./errors.js";
2
3
  import extractKey from "./extractKey.js";
3
4
  import RecordStore from "./RecordStore.js";
4
5
  import valueToKey from "./valueToKey.js";
5
- import FDBRecord from "../FDBRecord.js";
6
-
7
6
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-index
8
7
  class Index {
9
8
  deleted = false;
@@ -1,9 +1,8 @@
1
+ import FDBRecord from "../FDBRecord.js";
1
2
  import { DataError } from "./errors.js";
2
3
  import extractKey from "./extractKey.js";
3
4
  import KeyGenerator from "./KeyGenerator.js";
4
5
  import RecordStore from "./RecordStore.js";
5
- import FDBRecord from "../FDBRecord.js";
6
-
7
6
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-object-store
8
7
  class ObjectStore {
9
8
  deleted = false;
@@ -1,7 +1,6 @@
1
- import cmp from "./cmp.js";
2
1
  import FDBKeyRange from "../FDBKeyRange.js";
2
+ import cmp from "./cmp.js";
3
3
  import { ConstraintError } from "./errors.js";
4
-
5
4
  // what fraction of the total number of nodes are allowed to be deleted tombstones?
6
5
  const MAX_TOMBSTONE_FACTOR = 2 / 3;
7
6
  const EVERYTHING_KEY_RANGE = new FDBKeyRange(undefined, undefined, false, false);
@@ -1,12 +1,11 @@
1
1
  import isPotentiallyValidKeyRange from "./isPotentiallyValidKeyRange.js";
2
2
  import enforceRange from "./enforceRange.js";
3
-
4
3
  // https://www.w3.org/TR/IndexedDB/#create-request-to-retrieve-multiple-items
5
4
  const extractGetAllOptions = (queryOrOptions, count, numArguments) => {
6
5
  let query;
7
6
  let direction;
8
- if (isPotentiallyValidKeyRange(queryOrOptions)) {
9
- // queryOrOptions is FDBKeyRange | Key
7
+ if (queryOrOptions === undefined || queryOrOptions === null || isPotentiallyValidKeyRange(queryOrOptions)) {
8
+ // queryOrOptions is FDBKeyRange | Key | null | undefined
10
9
  query = queryOrOptions;
11
10
  if (numArguments > 1 && count !== undefined) {
12
11
  count = enforceRange(count, "unsigned long");
@@ -1,5 +1,4 @@
1
1
  import valueToKey from "./valueToKey.js";
2
-
3
2
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-extracting-a-key-from-a-value-using-a-key-path
4
3
  const extractKey = (keyPath, value) => {
5
4
  if (Array.isArray(keyPath)) {
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Minimal polyfill of `Set.prototype.intersection`, available in Node 22+.
3
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/intersection
4
+ * @param set1
5
+ * @param set2
6
+ */
7
+ export function intersection(set1, set2) {
8
+ if ("intersection" in set1) {
9
+ return set1.intersection(set2);
10
+ }
11
+ return new Set([...set1].filter(item => set2.has(item)));
12
+ }
@@ -1,26 +1,18 @@
1
1
  import FDBKeyRange from "../FDBKeyRange.js";
2
- import isSharedArrayBuffer from "./isSharedArrayBuffer.js";
2
+ import valueToKeyWithoutThrowing, { INVALID_TYPE } from "./valueToKeyWithoutThrowing.js";
3
3
 
4
4
  // https://www.w3.org/TR/IndexedDB/#is-a-potentially-valid-key-range
5
5
  const isPotentiallyValidKeyRange = value => {
6
- // Many of these conditions recapitulate the same conditions in `valueToKeyRange.ts`
7
- return (
8
- // FDBKeyRange
9
- value instanceof FDBKeyRange ||
10
- // null/undefined - nullDisallowedFlag=false is assumed here
11
- value === null || value === undefined ||
12
- // Number
13
- typeof value === "number" ||
14
- // Date
15
- Object.prototype.toString.call(value) === "[object Date]" ||
16
- // string
17
- typeof value === "string" ||
18
- // buffer source type
19
- // note: we are explicitly _not_ checking for detachedness here, to match Chromium's behavior
20
- // see: https://github.com/w3c/IndexedDB/issues/465
21
- value instanceof ArrayBuffer || isSharedArrayBuffer(value) || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(value) ||
22
- // array exotic type
23
- Array.isArray(value)
24
- );
6
+ // If value is a key range, return true.
7
+ if (value instanceof FDBKeyRange) {
8
+ return true;
9
+ }
10
+
11
+ // Let key be the result of converting a value to a key with value.
12
+ const key = valueToKeyWithoutThrowing(value);
13
+
14
+ // If key is "invalid type" return false.
15
+ // Else return true.
16
+ return key !== INVALID_TYPE;
25
17
  };
26
18
  export default isPotentiallyValidKeyRange;
@@ -19,10 +19,10 @@ function getSetImmediateFromJsdom() {
19
19
 
20
20
  // waiting on this PR for typescript types: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1249
21
21
 
22
- // 'user-blocking' runs right after microtasks, so equivalent to setTimeout but without the 4ms clamping
23
- const schedulerPostTask = typeof scheduler !== "undefined" && (fn => scheduler.postTask(fn, {
24
- priority: "user-blocking"
25
- }));
22
+ // 'postTask' runs right after microtasks, so equivalent to setTimeout but without the 4ms clamping.
23
+ // Using the default priority of 'user-visible' to avoid blocking input while still running fairly quickly.
24
+ // See: https://developer.mozilla.org/en-US/docs/Web/API/Prioritized_Task_Scheduling_API#task_priorities
25
+ const schedulerPostTask = typeof scheduler !== "undefined" && (fn => scheduler.postTask(fn));
26
26
 
27
27
  // fallback for environments that don't support any of the above
28
28
  const doSetTimeout = fn => setTimeout(fn, 0);
@@ -1,5 +1,4 @@
1
1
  import { SyntaxError } from "./errors.js";
2
-
3
2
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-valid-key-path
4
3
  const validateKeyPath = (keyPath, parent) => {
5
4
  // This doesn't make sense to me based on the spec, but it is needed to pass the W3C KeyPath tests (see same
@@ -1,60 +1,14 @@
1
1
  import { DataError } from "./errors.js";
2
- import isSharedArrayBuffer from "./isSharedArrayBuffer.js";
3
-
2
+ import valueToKeyWithoutThrowing, { INVALID_TYPE, INVALID_VALUE } from "./valueToKeyWithoutThrowing.js";
4
3
  // https://w3c.github.io/IndexedDB/#convert-value-to-key
4
+ // Plus throwing a DataError for invalid value/invalid key, which is commonly done
5
+ // in lots of IndexedDB operations
5
6
  const valueToKey = (input, seen) => {
6
- if (typeof input === "number") {
7
- if (isNaN(input)) {
8
- throw new DataError();
9
- }
10
- return input;
11
- } else if (Object.prototype.toString.call(input) === "[object Date]") {
12
- const ms = input.valueOf();
13
- if (isNaN(ms)) {
14
- throw new DataError();
15
- }
16
- return new Date(ms);
17
- } else if (typeof input === "string") {
18
- return input;
19
- } else if (
20
- // https://w3c.github.io/IndexedDB/#ref-for-dfn-buffer-source-type
21
- (input instanceof ArrayBuffer || isSharedArrayBuffer(input) || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) &&
22
- // We can't consistently test detachedness, so instead we check if byteLength === 0
23
- // This isn't foolproof, but there's no perfect way to detect if Uint8Arrays or
24
- // SharedArrayBuffers are detached
25
- !("detached" in input ? input.detached : input.byteLength === 0)) {
26
- let arrayBuffer;
27
- let offset = 0;
28
- let length = 0;
29
- if (input instanceof ArrayBuffer || isSharedArrayBuffer(input)) {
30
- arrayBuffer = input;
31
- length = input.byteLength;
32
- } else {
33
- arrayBuffer = input.buffer;
34
- offset = input.byteOffset;
35
- length = input.byteLength;
36
- }
37
- return arrayBuffer.slice(offset, offset + length);
38
- } else if (Array.isArray(input)) {
39
- if (seen === undefined) {
40
- seen = new Set();
41
- } else if (seen.has(input)) {
42
- throw new DataError();
43
- }
44
- seen.add(input);
45
- const keys = [];
46
- for (let i = 0; i < input.length; i++) {
47
- const hop = Object.hasOwn(input, i);
48
- if (!hop) {
49
- throw new DataError();
50
- }
51
- const entry = input[i];
52
- const key = valueToKey(entry, seen);
53
- keys.push(key);
54
- }
55
- return keys;
56
- } else {
7
+ const result = valueToKeyWithoutThrowing(input, seen);
8
+ if (result === INVALID_VALUE || result === INVALID_TYPE) {
9
+ // If key is "invalid value" or "invalid type", throw a "DataError" DOMException
57
10
  throw new DataError();
58
11
  }
12
+ return result;
59
13
  };
60
14
  export default valueToKey;
@@ -0,0 +1,75 @@
1
+ import isSharedArrayBuffer from "./isSharedArrayBuffer.js";
2
+ export const INVALID_TYPE = Symbol("INVALID_TYPE");
3
+ export const INVALID_VALUE = Symbol("INVALID_VALUE");
4
+
5
+ // https://w3c.github.io/IndexedDB/#convert-value-to-key
6
+ // The "without exceptions" version is because we typically want to throw exceptions (DataError) but not for
7
+ // the "is potentially valid key range" routine.
8
+ const valueToKeyWithoutThrowing = (input, seen) => {
9
+ if (typeof input === "number") {
10
+ if (isNaN(input)) {
11
+ // If input is NaN then return "invalid value".
12
+ return INVALID_VALUE;
13
+ }
14
+ return input;
15
+ } else if (Object.prototype.toString.call(input) === "[object Date]") {
16
+ const ms = input.valueOf();
17
+ if (isNaN(ms)) {
18
+ // If ms is NaN then return "invalid value".
19
+ return INVALID_VALUE;
20
+ }
21
+ return new Date(ms);
22
+ } else if (typeof input === "string") {
23
+ return input;
24
+ } else if (
25
+ // https://w3c.github.io/IndexedDB/#ref-for-dfn-buffer-source-type
26
+ input instanceof ArrayBuffer || isSharedArrayBuffer(input) || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
27
+ // We can't consistently test detachedness, so instead we check if byteLength === 0
28
+ // This isn't foolproof, but there's no perfect way to detect if Uint8Arrays or
29
+ // SharedArrayBuffers are detached
30
+ if ("detached" in input ? input.detached : input.byteLength === 0) {
31
+ // If input is detached then return "invalid value".
32
+ return INVALID_VALUE;
33
+ }
34
+ let arrayBuffer;
35
+ let offset = 0;
36
+ let length = 0;
37
+ if (input instanceof ArrayBuffer || isSharedArrayBuffer(input)) {
38
+ arrayBuffer = input;
39
+ length = input.byteLength;
40
+ } else {
41
+ arrayBuffer = input.buffer;
42
+ offset = input.byteOffset;
43
+ length = input.byteLength;
44
+ }
45
+ return arrayBuffer.slice(offset, offset + length);
46
+ } else if (Array.isArray(input)) {
47
+ if (seen === undefined) {
48
+ seen = new Set();
49
+ } else if (seen.has(input)) {
50
+ // If seen contains input, then return "invalid value".
51
+ return INVALID_VALUE;
52
+ }
53
+ seen.add(input);
54
+ const keys = [];
55
+ for (let i = 0; i < input.length; i++) {
56
+ const hop = Object.hasOwn(input, i);
57
+ if (!hop) {
58
+ // If hop is false, return "invalid value".
59
+ return INVALID_VALUE;
60
+ }
61
+ const entry = input[i];
62
+ const key = valueToKeyWithoutThrowing(entry, seen);
63
+ // If key is "invalid value" or "invalid type" abort these steps and return "invalid value".
64
+ if (key === INVALID_VALUE || key === INVALID_TYPE) {
65
+ return INVALID_VALUE;
66
+ }
67
+ keys.push(key);
68
+ }
69
+ return keys;
70
+ } else {
71
+ // Otherwise: Return "invalid type".
72
+ return INVALID_TYPE;
73
+ }
74
+ };
75
+ export default valueToKeyWithoutThrowing;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fake-indexeddb",
3
- "version": "6.2.1",
3
+ "version": "6.2.3",
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": {
@@ -131,6 +131,7 @@
131
131
  "dexie": "^4.0.11",
132
132
  "eslint": "^8.57.1",
133
133
  "eslint-config-prettier": "^9.1.0",
134
+ "eslint-plugin-import": "^2.32.0",
134
135
  "glob": "^11.0.2",
135
136
  "husky": "^9.1.7",
136
137
  "jest": "^30.0.0",