@keplr-wallet/common 0.13.9 → 0.13.11
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.
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { KVStore } from "./interface";
|
|
2
2
|
export declare class IndexedDBKVStore implements KVStore {
|
|
3
3
|
protected readonly _prefix: string;
|
|
4
|
-
protected
|
|
4
|
+
protected static dbPromiseMap: Map<string, Promise<IDBDatabase>>;
|
|
5
5
|
constructor(_prefix: string);
|
|
6
6
|
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
7
7
|
set<T = unknown>(key: string, data: T | null): Promise<void>;
|
|
8
|
+
getAllKeys(): Promise<string[]>;
|
|
8
9
|
prefix(): string;
|
|
9
10
|
protected getDB(): Promise<IDBDatabase>;
|
|
11
|
+
protected openDB(): Promise<IDBDatabase>;
|
|
10
12
|
}
|
|
@@ -21,8 +21,10 @@ class IndexedDBKVStore {
|
|
|
21
21
|
return new Promise((resolve, reject) => {
|
|
22
22
|
const request = store.get(key);
|
|
23
23
|
request.onerror = (event) => {
|
|
24
|
+
var _a;
|
|
24
25
|
event.stopPropagation();
|
|
25
|
-
|
|
26
|
+
const error = event.target.error;
|
|
27
|
+
reject(new Error((_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : "Unknown IndexedDB error on get"));
|
|
26
28
|
};
|
|
27
29
|
request.onsuccess = () => {
|
|
28
30
|
if (!request.result) {
|
|
@@ -43,8 +45,10 @@ class IndexedDBKVStore {
|
|
|
43
45
|
return new Promise((resolve, reject) => {
|
|
44
46
|
const request = store.delete(key);
|
|
45
47
|
request.onerror = (event) => {
|
|
48
|
+
var _a;
|
|
46
49
|
event.stopPropagation();
|
|
47
|
-
|
|
50
|
+
const error = event.target.error;
|
|
51
|
+
reject(new Error((_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : "Unknown IndexedDB error on delete"));
|
|
48
52
|
};
|
|
49
53
|
request.onsuccess = () => {
|
|
50
54
|
resolve();
|
|
@@ -60,8 +64,10 @@ class IndexedDBKVStore {
|
|
|
60
64
|
data,
|
|
61
65
|
});
|
|
62
66
|
request.onerror = (event) => {
|
|
67
|
+
var _a;
|
|
63
68
|
event.stopPropagation();
|
|
64
|
-
|
|
69
|
+
const error = event.target.error;
|
|
70
|
+
reject(new Error((_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : "Unknown IndexedDB error on put"));
|
|
65
71
|
};
|
|
66
72
|
request.onsuccess = () => {
|
|
67
73
|
resolve();
|
|
@@ -70,33 +76,67 @@ class IndexedDBKVStore {
|
|
|
70
76
|
}
|
|
71
77
|
});
|
|
72
78
|
}
|
|
73
|
-
|
|
74
|
-
return this._prefix;
|
|
75
|
-
}
|
|
76
|
-
getDB() {
|
|
79
|
+
getAllKeys() {
|
|
77
80
|
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
+
const tx = (yield this.getDB()).transaction([this.prefix()], "readonly");
|
|
82
|
+
const store = tx.objectStore(this.prefix());
|
|
81
83
|
return new Promise((resolve, reject) => {
|
|
82
|
-
const request =
|
|
84
|
+
const request = store.getAllKeys();
|
|
83
85
|
request.onerror = (event) => {
|
|
86
|
+
var _a;
|
|
84
87
|
event.stopPropagation();
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
request.onupgradeneeded = (event) => {
|
|
88
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
const db = event.target.result;
|
|
91
|
-
db.createObjectStore(this.prefix(), { keyPath: "key" });
|
|
88
|
+
const error = event.target.error;
|
|
89
|
+
reject(new Error((_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : "Unknown IndexedDB error on getAllKeys"));
|
|
92
90
|
};
|
|
93
91
|
request.onsuccess = () => {
|
|
94
|
-
this.cachedDB = request.result;
|
|
95
92
|
resolve(request.result);
|
|
96
93
|
};
|
|
97
94
|
});
|
|
98
95
|
});
|
|
99
96
|
}
|
|
97
|
+
prefix() {
|
|
98
|
+
return this._prefix;
|
|
99
|
+
}
|
|
100
|
+
getDB() {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
const prefix = this.prefix();
|
|
103
|
+
if (!IndexedDBKVStore.dbPromiseMap.has(prefix)) {
|
|
104
|
+
IndexedDBKVStore.dbPromiseMap.set(prefix, this.openDB());
|
|
105
|
+
}
|
|
106
|
+
return IndexedDBKVStore.dbPromiseMap.get(prefix);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
openDB() {
|
|
110
|
+
const prefix = this.prefix();
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
const request = window.indexedDB.open(prefix);
|
|
113
|
+
request.onerror = (event) => {
|
|
114
|
+
var _a;
|
|
115
|
+
event.stopPropagation();
|
|
116
|
+
IndexedDBKVStore.dbPromiseMap.delete(prefix);
|
|
117
|
+
const error = event.target.error;
|
|
118
|
+
reject(new Error((_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : "Unknown IndexedDB error on openDB"));
|
|
119
|
+
};
|
|
120
|
+
request.onupgradeneeded = (event) => {
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
122
|
+
// @ts-ignore
|
|
123
|
+
const db = event.target.result;
|
|
124
|
+
db.createObjectStore(prefix, { keyPath: "key" });
|
|
125
|
+
};
|
|
126
|
+
request.onsuccess = () => {
|
|
127
|
+
const db = request.result;
|
|
128
|
+
db.onclose = () => {
|
|
129
|
+
IndexedDBKVStore.dbPromiseMap.delete(prefix);
|
|
130
|
+
};
|
|
131
|
+
db.onversionchange = () => {
|
|
132
|
+
db.close();
|
|
133
|
+
IndexedDBKVStore.dbPromiseMap.delete(prefix);
|
|
134
|
+
};
|
|
135
|
+
resolve(db);
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
}
|
|
100
139
|
}
|
|
140
|
+
IndexedDBKVStore.dbPromiseMap = new Map();
|
|
101
141
|
exports.IndexedDBKVStore = IndexedDBKVStore;
|
|
102
142
|
//# sourceMappingURL=indexed-db.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"indexed-db.js","sourceRoot":"","sources":["../../src/kv-store/indexed-db.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,MAAa,gBAAgB;IAG3B,YAA+B,OAAe;QAAf,YAAO,GAAP,OAAO,CAAQ;IAAG,CAAC;IAE5C,GAAG,CAAc,GAAW;;YAChC,MAAM,EAAE,GAAG,CAAC,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;YACzE,MAAM,KAAK,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;YAE5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBACrC,MAAM,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBAC/B,OAAO,CAAC,OAAO,GAAG,CAAC,KAAK,EAAE,EAAE
|
|
1
|
+
{"version":3,"file":"indexed-db.js","sourceRoot":"","sources":["../../src/kv-store/indexed-db.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,MAAa,gBAAgB;IAG3B,YAA+B,OAAe;QAAf,YAAO,GAAP,OAAO,CAAQ;IAAG,CAAC;IAE5C,GAAG,CAAc,GAAW;;YAChC,MAAM,EAAE,GAAG,CAAC,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;YACzE,MAAM,KAAK,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CA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AAC;AAD7D,4CAAgB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keplr-wallet/common",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.11",
|
|
4
4
|
"main": "build/index.js",
|
|
5
5
|
"author": "chainapsis",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"typecheck": "npx tsc -p tsconfig.check.json"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@keplr-wallet/crypto": "0.13.
|
|
25
|
-
"@keplr-wallet/types": "0.13.
|
|
24
|
+
"@keplr-wallet/crypto": "0.13.11",
|
|
25
|
+
"@keplr-wallet/types": "0.13.11",
|
|
26
26
|
"buffer": "^6.0.3",
|
|
27
27
|
"delay": "^4.4.0",
|
|
28
28
|
"p-queue": "^6.6.2"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "5998947a9853adcbed5872b849362f05b5e73f7e"
|
|
31
31
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { KVStore } from "./interface";
|
|
2
2
|
|
|
3
3
|
export class IndexedDBKVStore implements KVStore {
|
|
4
|
-
protected
|
|
4
|
+
protected static dbPromiseMap = new Map<string, Promise<IDBDatabase>>();
|
|
5
5
|
|
|
6
6
|
constructor(protected readonly _prefix: string) {}
|
|
7
7
|
|
|
@@ -14,7 +14,8 @@ export class IndexedDBKVStore implements KVStore {
|
|
|
14
14
|
request.onerror = (event) => {
|
|
15
15
|
event.stopPropagation();
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
const error = (event.target as IDBRequest).error;
|
|
18
|
+
reject(new Error(error?.message ?? "Unknown IndexedDB error on get"));
|
|
18
19
|
};
|
|
19
20
|
request.onsuccess = () => {
|
|
20
21
|
if (!request.result) {
|
|
@@ -36,7 +37,10 @@ export class IndexedDBKVStore implements KVStore {
|
|
|
36
37
|
request.onerror = (event) => {
|
|
37
38
|
event.stopPropagation();
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
const error = (event.target as IDBRequest).error;
|
|
41
|
+
reject(
|
|
42
|
+
new Error(error?.message ?? "Unknown IndexedDB error on delete")
|
|
43
|
+
);
|
|
40
44
|
};
|
|
41
45
|
request.onsuccess = () => {
|
|
42
46
|
resolve();
|
|
@@ -54,7 +58,8 @@ export class IndexedDBKVStore implements KVStore {
|
|
|
54
58
|
request.onerror = (event) => {
|
|
55
59
|
event.stopPropagation();
|
|
56
60
|
|
|
57
|
-
|
|
61
|
+
const error = (event.target as IDBRequest).error;
|
|
62
|
+
reject(new Error(error?.message ?? "Unknown IndexedDB error on put"));
|
|
58
63
|
};
|
|
59
64
|
request.onsuccess = () => {
|
|
60
65
|
resolve();
|
|
@@ -63,20 +68,50 @@ export class IndexedDBKVStore implements KVStore {
|
|
|
63
68
|
}
|
|
64
69
|
}
|
|
65
70
|
|
|
71
|
+
async getAllKeys(): Promise<string[]> {
|
|
72
|
+
const tx = (await this.getDB()).transaction([this.prefix()], "readonly");
|
|
73
|
+
const store = tx.objectStore(this.prefix());
|
|
74
|
+
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
const request = store.getAllKeys();
|
|
77
|
+
request.onerror = (event) => {
|
|
78
|
+
event.stopPropagation();
|
|
79
|
+
|
|
80
|
+
const error = (event.target as IDBRequest).error;
|
|
81
|
+
reject(
|
|
82
|
+
new Error(error?.message ?? "Unknown IndexedDB error on getAllKeys")
|
|
83
|
+
);
|
|
84
|
+
};
|
|
85
|
+
request.onsuccess = () => {
|
|
86
|
+
resolve(request.result as string[]);
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
66
91
|
prefix(): string {
|
|
67
92
|
return this._prefix;
|
|
68
93
|
}
|
|
69
94
|
|
|
70
95
|
protected async getDB(): Promise<IDBDatabase> {
|
|
71
|
-
|
|
72
|
-
|
|
96
|
+
const prefix = this.prefix();
|
|
97
|
+
if (!IndexedDBKVStore.dbPromiseMap.has(prefix)) {
|
|
98
|
+
IndexedDBKVStore.dbPromiseMap.set(prefix, this.openDB());
|
|
73
99
|
}
|
|
74
100
|
|
|
101
|
+
return IndexedDBKVStore.dbPromiseMap.get(prefix)!;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
protected openDB(): Promise<IDBDatabase> {
|
|
105
|
+
const prefix = this.prefix();
|
|
75
106
|
return new Promise((resolve, reject) => {
|
|
76
|
-
const request = window.indexedDB.open(
|
|
107
|
+
const request = window.indexedDB.open(prefix);
|
|
77
108
|
request.onerror = (event) => {
|
|
78
109
|
event.stopPropagation();
|
|
79
|
-
|
|
110
|
+
IndexedDBKVStore.dbPromiseMap.delete(prefix);
|
|
111
|
+
const error = (event.target as IDBRequest).error;
|
|
112
|
+
reject(
|
|
113
|
+
new Error(error?.message ?? "Unknown IndexedDB error on openDB")
|
|
114
|
+
);
|
|
80
115
|
};
|
|
81
116
|
|
|
82
117
|
request.onupgradeneeded = (event) => {
|
|
@@ -84,12 +119,21 @@ export class IndexedDBKVStore implements KVStore {
|
|
|
84
119
|
// @ts-ignore
|
|
85
120
|
const db = event.target.result;
|
|
86
121
|
|
|
87
|
-
db.createObjectStore(
|
|
122
|
+
db.createObjectStore(prefix, { keyPath: "key" });
|
|
88
123
|
};
|
|
89
124
|
|
|
90
125
|
request.onsuccess = () => {
|
|
91
|
-
|
|
92
|
-
|
|
126
|
+
const db = request.result;
|
|
127
|
+
|
|
128
|
+
db.onclose = () => {
|
|
129
|
+
IndexedDBKVStore.dbPromiseMap.delete(prefix);
|
|
130
|
+
};
|
|
131
|
+
db.onversionchange = () => {
|
|
132
|
+
db.close();
|
|
133
|
+
IndexedDBKVStore.dbPromiseMap.delete(prefix);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
resolve(db);
|
|
93
137
|
};
|
|
94
138
|
});
|
|
95
139
|
}
|