aboutsummaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/util
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2021-02-16 11:34:50 +0100
committerFlorian Dold <florian@dold.me>2021-02-16 13:47:01 +0100
commitdb59275b6b43f8fa7f36899ae81cb7139a2e80cb (patch)
tree66ef429005a0ab239a212c39ad4501b29f727f97 /packages/idb-bridge/src/util
parentd1f00aeaa26af6835ea3f47ac280b1e67d672fa2 (diff)
downloadwallet-core-db59275b6b43f8fa7f36899ae81cb7139a2e80cb.tar.xz
add more tests and fix various issues
Diffstat (limited to 'packages/idb-bridge/src/util')
-rw-r--r--packages/idb-bridge/src/util/cmp.ts6
-rw-r--r--packages/idb-bridge/src/util/enforceRange.ts4
-rw-r--r--packages/idb-bridge/src/util/extractKey.ts6
-rw-r--r--packages/idb-bridge/src/util/getIndexKeys.ts6
-rw-r--r--packages/idb-bridge/src/util/injectKey.ts20
-rw-r--r--packages/idb-bridge/src/util/makeStoreKeyValue.ts6
-rw-r--r--packages/idb-bridge/src/util/openPromise.ts4
-rw-r--r--packages/idb-bridge/src/util/validateKeyPath.ts7
-rw-r--r--packages/idb-bridge/src/util/valueToKey.ts4
9 files changed, 27 insertions, 36 deletions
diff --git a/packages/idb-bridge/src/util/cmp.ts b/packages/idb-bridge/src/util/cmp.ts
index ddd43f2a6..e7f26bf1a 100644
--- a/packages/idb-bridge/src/util/cmp.ts
+++ b/packages/idb-bridge/src/util/cmp.ts
@@ -15,7 +15,7 @@
*/
import { DataError } from "./errors";
-import valueToKey from "./valueToKey";
+import { valueToKey } from "./valueToKey";
const getType = (x: any) => {
if (typeof x === "number") {
@@ -38,7 +38,7 @@ const getType = (x: any) => {
};
// https://w3c.github.io/IndexedDB/#compare-two-keys
-const compareKeys = (first: any, second: any): -1 | 0 | 1 => {
+export const compareKeys = (first: any, second: any): -1 | 0 | 1 => {
if (second === undefined) {
throw new TypeError();
}
@@ -104,5 +104,3 @@ const compareKeys = (first: any, second: any): -1 | 0 | 1 => {
return first > second ? 1 : -1;
};
-
-export default compareKeys;
diff --git a/packages/idb-bridge/src/util/enforceRange.ts b/packages/idb-bridge/src/util/enforceRange.ts
index 87e135798..ed463a330 100644
--- a/packages/idb-bridge/src/util/enforceRange.ts
+++ b/packages/idb-bridge/src/util/enforceRange.ts
@@ -17,7 +17,7 @@
// https://heycam.github.io/webidl/#EnforceRange
-const enforceRange = (
+export const enforceRange = (
num: number,
type: "MAX_SAFE_INTEGER" | "unsigned long",
) => {
@@ -31,5 +31,3 @@ const enforceRange = (
return Math.floor(num);
}
};
-
-export default enforceRange;
diff --git a/packages/idb-bridge/src/util/extractKey.ts b/packages/idb-bridge/src/util/extractKey.ts
index 7aa8bd173..09306ddec 100644
--- a/packages/idb-bridge/src/util/extractKey.ts
+++ b/packages/idb-bridge/src/util/extractKey.ts
@@ -16,10 +16,10 @@
*/
import { IDBKeyPath, IDBValidKey } from "../idbtypes";
-import valueToKey from "./valueToKey";
+import { valueToKey } from "./valueToKey";
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-extracting-a-key-from-a-value-using-a-key-path
-const extractKey = (keyPath: IDBKeyPath | IDBKeyPath[], value: any) => {
+export const extractKey = (keyPath: IDBKeyPath | IDBKeyPath[], value: any) => {
if (Array.isArray(keyPath)) {
const result: IDBValidKey[] = [];
@@ -68,5 +68,3 @@ const extractKey = (keyPath: IDBKeyPath | IDBKeyPath[], value: any) => {
return object;
};
-
-export default extractKey;
diff --git a/packages/idb-bridge/src/util/getIndexKeys.ts b/packages/idb-bridge/src/util/getIndexKeys.ts
index 77b96b12f..8515d79ea 100644
--- a/packages/idb-bridge/src/util/getIndexKeys.ts
+++ b/packages/idb-bridge/src/util/getIndexKeys.ts
@@ -16,8 +16,8 @@
*/
import { IDBKeyPath, IDBValidKey } from "../idbtypes";
-import extractKey from "./extractKey";
-import valueToKey from "./valueToKey";
+import { extractKey } from "./extractKey";
+import { valueToKey } from "./valueToKey";
export function getIndexKeys(
value: any,
@@ -43,5 +43,3 @@ export function getIndexKeys(
throw Error(`unsupported key path: ${typeof keyPath}`);
}
}
-
-export default getIndexKeys;
diff --git a/packages/idb-bridge/src/util/injectKey.ts b/packages/idb-bridge/src/util/injectKey.ts
index 63c8deda4..02acfaa4c 100644
--- a/packages/idb-bridge/src/util/injectKey.ts
+++ b/packages/idb-bridge/src/util/injectKey.ts
@@ -30,6 +30,11 @@ export function injectKey(
);
}
+ const newValue = structuredClone(value);
+
+ // Position inside the new value where we'll place the key eventually.
+ let ptr = newValue;
+
const identifiers = keyPath.split(".");
if (identifiers.length === 0) {
throw new Error("Assert: identifiers is not empty");
@@ -42,26 +47,23 @@ export function injectKey(
}
for (const identifier of identifiers) {
- if (typeof value !== "object" && !Array.isArray(value)) {
- return false;
+ if (typeof ptr !== "object" && !Array.isArray(ptr)) {
+ throw new Error("can't inject key");
}
const hop = value.hasOwnProperty(identifier);
if (!hop) {
- return true;
+ ptr[identifier] = {};
}
- value = value[identifier];
+ ptr = ptr[identifier];
}
- if (!(typeof value === "object" || Array.isArray(value))) {
+ if (!(typeof ptr === "object" || Array.isArray(ptr))) {
throw new Error("can't inject key");
}
- const newValue = structuredClone(value);
- newValue[lastIdentifier] = structuredClone(key);
+ ptr[lastIdentifier] = structuredClone(key);
return newValue;
}
-
-export default injectKey;
diff --git a/packages/idb-bridge/src/util/makeStoreKeyValue.ts b/packages/idb-bridge/src/util/makeStoreKeyValue.ts
index 2281e983d..442a69ff5 100644
--- a/packages/idb-bridge/src/util/makeStoreKeyValue.ts
+++ b/packages/idb-bridge/src/util/makeStoreKeyValue.ts
@@ -14,11 +14,11 @@
permissions and limitations under the License.
*/
-import extractKey from "./extractKey";
+import { extractKey } from "./extractKey";
import { DataError } from "./errors";
-import valueToKey from "./valueToKey";
+import { valueToKey } from "./valueToKey";
import { structuredClone } from "./structuredClone";
-import injectKey from "./injectKey";
+import { injectKey } from "./injectKey";
import { IDBKeyPath, IDBValidKey } from "../idbtypes";
export interface StoreKeyResult {
diff --git a/packages/idb-bridge/src/util/openPromise.ts b/packages/idb-bridge/src/util/openPromise.ts
index 915060deb..395d69e7b 100644
--- a/packages/idb-bridge/src/util/openPromise.ts
+++ b/packages/idb-bridge/src/util/openPromise.ts
@@ -14,7 +14,7 @@
permissions and limitations under the License.
*/
-function openPromise<T>(): {
+export function openPromise<T>(): {
promise: Promise<T>;
resolve: (v?: T | PromiseLike<T>) => void;
reject: (err?: any) => void;
@@ -34,5 +34,3 @@ function openPromise<T>(): {
return { promise, resolve, reject };
}
-
-export default openPromise;
diff --git a/packages/idb-bridge/src/util/validateKeyPath.ts b/packages/idb-bridge/src/util/validateKeyPath.ts
index 2beb3c468..3bbe653b6 100644
--- a/packages/idb-bridge/src/util/validateKeyPath.ts
+++ b/packages/idb-bridge/src/util/validateKeyPath.ts
@@ -17,7 +17,10 @@
import { IDBKeyPath } from "../idbtypes";
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-valid-key-path
-const validateKeyPath = (keyPath: IDBKeyPath | IDBKeyPath[], parent?: "array" | "string") => {
+export const validateKeyPath = (
+ keyPath: IDBKeyPath | IDBKeyPath[],
+ parent?: "array" | "string",
+) => {
// This doesn't make sense to me based on the spec, but it is needed to pass the W3C KeyPath tests (see same
// comment in extractKey)
let myKeyPath: IDBKeyPath | IDBKeyPath[] = keyPath;
@@ -74,5 +77,3 @@ const validateKeyPath = (keyPath: IDBKeyPath | IDBKeyPath[], parent?: "array" |
throw new SyntaxError();
};
-
-export default validateKeyPath;
diff --git a/packages/idb-bridge/src/util/valueToKey.ts b/packages/idb-bridge/src/util/valueToKey.ts
index c3661f9a1..cfef779fe 100644
--- a/packages/idb-bridge/src/util/valueToKey.ts
+++ b/packages/idb-bridge/src/util/valueToKey.ts
@@ -18,7 +18,7 @@ import { IDBValidKey } from "..";
import { DataError } from "./errors";
// https://w3c.github.io/IndexedDB/#convert-a-value-to-a-input
-function valueToKey(
+export function valueToKey(
input: any,
seen?: Set<object>,
): IDBValidKey | IDBValidKey[] {
@@ -68,5 +68,3 @@ function valueToKey(
throw new DataError();
}
}
-
-export default valueToKey;