fake-indexeddb 6.2.2 → 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.
- package/build/cjs/FDBFactory.js +276 -136
- package/build/cjs/FDBTransaction.js +49 -11
- package/build/cjs/lib/Database.js +21 -12
- package/build/cjs/lib/FakeEventTarget.js +17 -5
- package/build/cjs/lib/extractGetAllOptions.js +2 -2
- package/build/cjs/lib/intersection.js +18 -0
- package/build/cjs/lib/isPotentiallyValidKeyRange.js +13 -20
- package/build/cjs/lib/scheduling.js +4 -4
- package/build/cjs/lib/valueToKey.js +8 -53
- package/build/cjs/lib/valueToKeyWithoutThrowing.js +82 -0
- package/build/esm/FDBFactory.js +276 -136
- package/build/esm/FDBTransaction.js +49 -11
- package/build/esm/lib/Database.js +21 -12
- package/build/esm/lib/FakeEventTarget.js +17 -5
- package/build/esm/lib/extractGetAllOptions.js +2 -2
- package/build/esm/lib/intersection.js +12 -0
- package/build/esm/lib/isPotentiallyValidKeyRange.js +12 -20
- package/build/esm/lib/scheduling.js +4 -4
- package/build/esm/lib/valueToKey.js +7 -52
- package/build/esm/lib/valueToKeyWithoutThrowing.js +75 -0
- package/package.json +1 -1
|
@@ -4,8 +4,8 @@ import enforceRange from "./enforceRange.js";
|
|
|
4
4
|
const extractGetAllOptions = (queryOrOptions, count, numArguments) => {
|
|
5
5
|
let query;
|
|
6
6
|
let direction;
|
|
7
|
-
if (isPotentiallyValidKeyRange(queryOrOptions)) {
|
|
8
|
-
// queryOrOptions is FDBKeyRange | Key
|
|
7
|
+
if (queryOrOptions === undefined || queryOrOptions === null || isPotentiallyValidKeyRange(queryOrOptions)) {
|
|
8
|
+
// queryOrOptions is FDBKeyRange | Key | null | undefined
|
|
9
9
|
query = queryOrOptions;
|
|
10
10
|
if (numArguments > 1 && count !== undefined) {
|
|
11
11
|
count = enforceRange(count, "unsigned long");
|
|
@@ -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
|
|
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
|
-
//
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
// '
|
|
23
|
-
|
|
24
|
-
|
|
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,59 +1,14 @@
|
|
|
1
1
|
import { DataError } from "./errors.js";
|
|
2
|
-
import
|
|
2
|
+
import valueToKeyWithoutThrowing, { INVALID_TYPE, INVALID_VALUE } from "./valueToKeyWithoutThrowing.js";
|
|
3
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
|
|
4
6
|
const valueToKey = (input, seen) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
return input;
|
|
10
|
-
} else if (Object.prototype.toString.call(input) === "[object Date]") {
|
|
11
|
-
const ms = input.valueOf();
|
|
12
|
-
if (isNaN(ms)) {
|
|
13
|
-
throw new DataError();
|
|
14
|
-
}
|
|
15
|
-
return new Date(ms);
|
|
16
|
-
} else if (typeof input === "string") {
|
|
17
|
-
return input;
|
|
18
|
-
} else if (
|
|
19
|
-
// https://w3c.github.io/IndexedDB/#ref-for-dfn-buffer-source-type
|
|
20
|
-
(input instanceof ArrayBuffer || isSharedArrayBuffer(input) || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) &&
|
|
21
|
-
// We can't consistently test detachedness, so instead we check if byteLength === 0
|
|
22
|
-
// This isn't foolproof, but there's no perfect way to detect if Uint8Arrays or
|
|
23
|
-
// SharedArrayBuffers are detached
|
|
24
|
-
!("detached" in input ? input.detached : input.byteLength === 0)) {
|
|
25
|
-
let arrayBuffer;
|
|
26
|
-
let offset = 0;
|
|
27
|
-
let length = 0;
|
|
28
|
-
if (input instanceof ArrayBuffer || isSharedArrayBuffer(input)) {
|
|
29
|
-
arrayBuffer = input;
|
|
30
|
-
length = input.byteLength;
|
|
31
|
-
} else {
|
|
32
|
-
arrayBuffer = input.buffer;
|
|
33
|
-
offset = input.byteOffset;
|
|
34
|
-
length = input.byteLength;
|
|
35
|
-
}
|
|
36
|
-
return arrayBuffer.slice(offset, offset + length);
|
|
37
|
-
} else if (Array.isArray(input)) {
|
|
38
|
-
if (seen === undefined) {
|
|
39
|
-
seen = new Set();
|
|
40
|
-
} else if (seen.has(input)) {
|
|
41
|
-
throw new DataError();
|
|
42
|
-
}
|
|
43
|
-
seen.add(input);
|
|
44
|
-
const keys = [];
|
|
45
|
-
for (let i = 0; i < input.length; i++) {
|
|
46
|
-
const hop = Object.hasOwn(input, i);
|
|
47
|
-
if (!hop) {
|
|
48
|
-
throw new DataError();
|
|
49
|
-
}
|
|
50
|
-
const entry = input[i];
|
|
51
|
-
const key = valueToKey(entry, seen);
|
|
52
|
-
keys.push(key);
|
|
53
|
-
}
|
|
54
|
-
return keys;
|
|
55
|
-
} 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
|
|
56
10
|
throw new DataError();
|
|
57
11
|
}
|
|
12
|
+
return result;
|
|
58
13
|
};
|
|
59
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