-
Notifications
You must be signed in to change notification settings - Fork 3
/
BrowserLocalStorage.ts
56 lines (49 loc) · 1.07 KB
/
BrowserLocalStorage.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
import { AbstractStorage } from './AbstractStorage';
import { SmartBuffer } from '@ylide/smart-buffer';
/**
* @category Storage
* @description Class representing browser local storage
*/
export class BrowserLocalStorage extends AbstractStorage {
async init() {
return true;
}
async storeBytes(key: string, bytes: Uint8Array): Promise<boolean> {
try {
localStorage.setItem(key, new SmartBuffer(bytes).toBase64String());
return true;
} catch (err) {
return false;
}
}
async readBytes(key: string): Promise<Uint8Array | null> {
try {
const base64 = localStorage.getItem(key);
if (base64 === null) {
return null;
}
return SmartBuffer.ofBase64String(base64).bytes;
} catch {
return null;
}
}
async clear(): Promise<boolean> {
try {
localStorage.clear();
return true;
} catch (err) {
return false;
}
}
async getKeys(): Promise<string[]> {
return Object.keys(localStorage);
}
async delete(key: string): Promise<boolean> {
try {
localStorage.removeItem(key);
return true;
} catch (err) {
return false;
}
}
}