fake-indexeddb 6.2.2 → 6.2.4

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.
Files changed (38) hide show
  1. package/README.md +23 -15
  2. package/auto/index.js +1 -1
  3. package/auto/index.mjs +1 -1
  4. package/build/cjs/FDBCursor.js +2 -1
  5. package/build/cjs/FDBDatabase.js +45 -20
  6. package/build/cjs/FDBFactory.js +285 -137
  7. package/build/cjs/FDBIndex.js +13 -9
  8. package/build/cjs/FDBObjectStore.js +26 -22
  9. package/build/cjs/FDBTransaction.js +51 -11
  10. package/build/cjs/lib/Database.js +25 -12
  11. package/build/cjs/lib/FakeEventTarget.js +17 -5
  12. package/build/cjs/lib/ObjectStore.js +60 -20
  13. package/build/cjs/lib/cloneValueForInsertion.js +30 -0
  14. package/build/cjs/lib/extractGetAllOptions.js +2 -2
  15. package/build/cjs/lib/intersection.js +18 -0
  16. package/build/cjs/lib/isPotentiallyValidKeyRange.js +13 -20
  17. package/build/cjs/lib/scheduling.js +4 -4
  18. package/build/cjs/lib/validateRequiredArguments.js +13 -0
  19. package/build/cjs/lib/valueToKey.js +8 -53
  20. package/build/cjs/lib/valueToKeyWithoutThrowing.js +102 -0
  21. package/build/esm/FDBCursor.js +2 -1
  22. package/build/esm/FDBDatabase.js +45 -20
  23. package/build/esm/FDBFactory.js +285 -137
  24. package/build/esm/FDBIndex.js +13 -9
  25. package/build/esm/FDBObjectStore.js +26 -22
  26. package/build/esm/FDBTransaction.js +51 -11
  27. package/build/esm/lib/Database.js +25 -12
  28. package/build/esm/lib/FakeEventTarget.js +17 -5
  29. package/build/esm/lib/ObjectStore.js +60 -20
  30. package/build/esm/lib/cloneValueForInsertion.js +24 -0
  31. package/build/esm/lib/extractGetAllOptions.js +2 -2
  32. package/build/esm/lib/intersection.js +12 -0
  33. package/build/esm/lib/isPotentiallyValidKeyRange.js +12 -20
  34. package/build/esm/lib/scheduling.js +4 -4
  35. package/build/esm/lib/validateRequiredArguments.js +7 -0
  36. package/build/esm/lib/valueToKey.js +7 -52
  37. package/build/esm/lib/valueToKeyWithoutThrowing.js +95 -0
  38. package/package.json +4 -2
@@ -0,0 +1,95 @@
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
+
55
+ // This algorithm is tricky to account for `bindings-inject-keys-bypass.any.js`. We _should_ return early when
56
+ // encountering an invalid key/type, but we also need to avoid triggering `Object.prototype['10']` if it's been
57
+ // overridden. One simple way to do this (and which doesn't rely on sparse arrays or other exotic solutions that
58
+ // could cause de-opts) is to use `Array.from()` with a mapper function, which does not trigger the prototype
59
+ // setter [1]. It does prevent an early return, but we can at least short-circuit inside the mapper function
60
+ // (which isn't strictly necessary to pass the WPTs, but is closer to the spec).
61
+ // [1]: See https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.from, specifically
62
+ // the chain CreateDataPropertyOrThrow -> CreateDataProperty -> DefineOwnProperty which defines
63
+ // the array element as an "own" property.
64
+ let hasInvalid = false;
65
+ const keys = Array.from({
66
+ length: input.length
67
+ }, (_, i) => {
68
+ if (hasInvalid) {
69
+ return;
70
+ }
71
+ const hop = Object.hasOwn(input, i);
72
+ if (!hop) {
73
+ // If hop is false, return "invalid value".
74
+ hasInvalid = true;
75
+ return;
76
+ }
77
+ const entry = input[i];
78
+ const key = valueToKeyWithoutThrowing(entry, seen);
79
+ // If key is "invalid value" or "invalid type" abort these steps and return "invalid value".
80
+ if (key === INVALID_VALUE || key === INVALID_TYPE) {
81
+ hasInvalid = true;
82
+ return;
83
+ }
84
+ return key;
85
+ });
86
+ if (hasInvalid) {
87
+ return INVALID_VALUE;
88
+ }
89
+ return keys;
90
+ } else {
91
+ // Otherwise: Return "invalid type".
92
+ return INVALID_TYPE;
93
+ }
94
+ };
95
+ export default valueToKeyWithoutThrowing;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fake-indexeddb",
3
- "version": "6.2.2",
3
+ "version": "6.2.4",
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": {
@@ -100,7 +100,9 @@
100
100
  "test-qunit": "pnpm run build-qunit && OPENSSL_CONF=/dev/null node-qunit-phantomjs build/esm/test/indexedDBmock/index.html",
101
101
  "test-w3c": "node --test src/test/web-platform-tests/run-all.js",
102
102
  "test": "pnpm run lint && pnpm run build && pnpm run test-jest && node test/test.js && node test/dexie.js && pnpm run test-w3c && pnpm run test-mocha && pnpm run test-qunit",
103
- "prepare": "husky"
103
+ "prepare": "husky",
104
+ "update-wpt-results": "pnpm run build && node ./bin/update-browser-wpt-results.js && WRITE_TO_README=1 pnpm run test-w3c",
105
+ "version": "pnpm run update-wpt-results && git add README.md"
104
106
  },
105
107
  "author": "Jeremy Scheff <jdscheff@gmail.com> (https://dumbmatter.com/)",
106
108
  "license": "Apache-2.0",