-
Notifications
You must be signed in to change notification settings - Fork 195
/
IndexedDbDPoPStore.ts
68 lines (60 loc) · 2.57 KB
/
IndexedDbDPoPStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { DPoPState, type DPoPStore } from "./DPoPStore";
/**
* Provides a default implementation of the DPoP store using IndexedDB.
*
* @public
*/
export class IndexedDbDPoPStore implements DPoPStore {
readonly _dbName: string = "oidc";
readonly _storeName: string = "dpop";
public async set(key: string, value: DPoPState): Promise<void> {
const store = await this.createStore(this._dbName, this._storeName);
await store("readwrite", (str: IDBObjectStore) => {
str.put(value, key);
return this.promisifyRequest(str.transaction);
});
}
public async get(key: string): Promise<DPoPState> {
const store = await this.createStore(this._dbName, this._storeName);
return await store("readonly", (str) => {
return this.promisifyRequest(str.get(key));
}) as DPoPState;
}
public async remove(key: string): Promise<DPoPState> {
const item = await this.get(key);
const store = await this.createStore(this._dbName, this._storeName);
await store("readwrite", (str) => {
return this.promisifyRequest(str.delete(key));
});
return item;
}
public async getAllKeys(): Promise<string[]> {
const store = await this.createStore(this._dbName, this._storeName);
return await store("readonly", (str) => {
return this.promisifyRequest(str.getAllKeys());
}) as string[];
}
promisifyRequest<T = undefined>(
request: IDBRequest<T> | IDBTransaction): Promise<T> {
return new Promise<T>((resolve, reject) => {
(request as IDBTransaction).oncomplete = (request as IDBRequest<T>).onsuccess = () => resolve((request as IDBRequest<T>).result);
(request as IDBTransaction).onabort = (request as IDBRequest<T>).onerror = () => reject((request as IDBRequest<T>).error);
});
}
async createStore<T>(
dbName: string,
storeName: string,
): Promise<(txMode: IDBTransactionMode, callback: (store: IDBObjectStore) => T | PromiseLike<T>) => Promise<T>> {
const request = indexedDB.open(dbName);
request.onupgradeneeded = () => request.result.createObjectStore(storeName);
const db = await this.promisifyRequest<IDBDatabase>(request);
return async (
txMode: IDBTransactionMode,
callback: (store: IDBObjectStore) => T | PromiseLike<T>,
) => {
const tx = db.transaction(storeName, txMode);
const store = tx.objectStore(storeName);
return await callback(store);
};
}
}