-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data-store-json): BrowserLocalStorageStore (#914)
- Loading branch information
1 parent
48fbee3
commit 7b520ab
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
packages/data-store-json/src/browser-local-storage-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { IIdentifier, IMessage, ManagedKeyInfo } from '@veramo/core' | ||
import { ManagedPrivateKey } from '@veramo/key-manager' | ||
import { | ||
DiffCallback, | ||
VeramoJsonCache, | ||
ClaimTableEntry, | ||
CredentialTableEntry, | ||
PresentationTableEntry, | ||
VeramoJsonStore, | ||
} from './types' | ||
|
||
/** | ||
* Implementation of {@link VeramoJsonStore} that uses browser localStorage to store data | ||
* | ||
* @example | ||
* ``` | ||
* const dataStore = BrowserLocalStorageStore.fromLocalStorage('veramo-state') | ||
* const plugin = new DataStoreJson(dataStore) | ||
* ``` | ||
*/ | ||
export class BrowserLocalStorageStore implements VeramoJsonStore { | ||
notifyUpdate: DiffCallback | ||
dids: Record<string, IIdentifier> | ||
keys: Record<string, ManagedKeyInfo> | ||
privateKeys: Record<string, ManagedPrivateKey> | ||
credentials: Record<string, CredentialTableEntry> | ||
claims: Record<string, ClaimTableEntry> | ||
presentations: Record<string, PresentationTableEntry> | ||
messages: Record<string, IMessage> | ||
|
||
private constructor(private localStorageKey: string) { | ||
this.notifyUpdate = async ( | ||
oldState: VeramoJsonCache, | ||
newState: VeramoJsonCache, | ||
) => { | ||
this.save(newState) | ||
} | ||
this.dids = {} | ||
this.keys = {} | ||
this.privateKeys = {} | ||
this.credentials = {} | ||
this.claims = {} | ||
this.presentations = {} | ||
this.messages = {} | ||
} | ||
|
||
public static fromLocalStorage(localStorageKey: string): BrowserLocalStorageStore { | ||
const store = new BrowserLocalStorageStore(localStorageKey) | ||
return store.load() | ||
} | ||
|
||
private load(): BrowserLocalStorageStore { | ||
if (typeof window !== 'undefined') { | ||
const rawCache = window.localStorage.getItem(this.localStorageKey) || '{}' | ||
let cache: VeramoJsonCache | ||
try { | ||
cache = JSON.parse(rawCache) | ||
} catch (e: any) { | ||
cache = {} | ||
} | ||
({ | ||
dids: this.dids, | ||
keys: this.keys, | ||
credentials: this.credentials, | ||
claims: this.claims, | ||
presentations: this.presentations, | ||
messages: this.messages, | ||
privateKeys: this.privateKeys, | ||
} = { | ||
dids: {}, | ||
keys: {}, | ||
credentials: {}, | ||
claims: {}, | ||
presentations: {}, | ||
messages: {}, | ||
privateKeys: {}, | ||
...cache, | ||
}) | ||
} | ||
return this | ||
} | ||
|
||
private save(newState: VeramoJsonCache): void { | ||
if (typeof window !== 'undefined') { | ||
window.localStorage.setItem(this.localStorageKey, JSON.stringify(newState)) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters