mirror of
https://github.com/bitwarden/browser
synced 2026-02-14 15:33:55 +00:00
* Implement Backup Storage Location For Browser Disk * Remove Testing Change * Remove Comment * Add Comment For `disk-backup-local-storage` * Require Matching `valuesRequireDeserialization` values
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { Observable, Subject } from "rxjs";
|
|
|
|
import {
|
|
AbstractStorageService,
|
|
ObservableStorageService,
|
|
StorageUpdate,
|
|
} from "../abstractions/storage.service";
|
|
import { StorageOptions } from "../models/domain/storage-options";
|
|
|
|
export class WindowStorageService implements AbstractStorageService, ObservableStorageService {
|
|
private readonly updatesSubject = new Subject<StorageUpdate>();
|
|
|
|
updates$: Observable<StorageUpdate>;
|
|
constructor(private readonly storage: Storage) {
|
|
this.updates$ = this.updatesSubject.asObservable();
|
|
}
|
|
|
|
get valuesRequireDeserialization(): boolean {
|
|
return true;
|
|
}
|
|
|
|
get<T>(key: string, options?: StorageOptions): Promise<T> {
|
|
const jsonValue = this.storage.getItem(key);
|
|
if (jsonValue != null) {
|
|
return Promise.resolve(JSON.parse(jsonValue) as T);
|
|
}
|
|
|
|
return Promise.resolve(null);
|
|
}
|
|
|
|
async has(key: string, options?: StorageOptions): Promise<boolean> {
|
|
return (await this.get(key, options)) != null;
|
|
}
|
|
|
|
save<T>(key: string, obj: T, options?: StorageOptions): Promise<void> {
|
|
if (obj == null) {
|
|
return this.remove(key, options);
|
|
}
|
|
|
|
if (obj instanceof Set) {
|
|
obj = Array.from(obj) as T;
|
|
}
|
|
|
|
this.storage.setItem(key, JSON.stringify(obj));
|
|
this.updatesSubject.next({ key, updateType: "save" });
|
|
}
|
|
|
|
remove(key: string, options?: StorageOptions): Promise<void> {
|
|
this.storage.removeItem(key);
|
|
this.updatesSubject.next({ key, updateType: "remove" });
|
|
return Promise.resolve();
|
|
}
|
|
|
|
getKeys(): string[] {
|
|
return Object.keys(this.storage);
|
|
}
|
|
}
|