1
0
mirror of https://github.com/bitwarden/browser synced 2026-01-04 09:33:27 +00:00
Files
browser/apps/desktop/src/platform/services/electron-storage.service.ts
Andreas Coroiu 9d060be48c [PM-9442] Add tests for undefined state values and proper emulation of ElectronStorageService in tests (#9910)
* fix: handle undefined value in migration 66

* fix: the if-statement was typo

* feat: duplicate error behavior in fake storage service

* feat: fix all migrations that were setting undefined values

* feat: add test for disabled fingrint in migration 66

* fix: default single user state saving undefined value to state

* revert: awaiting floating promise

gonna fix this in a separate PR

* Revert "feat: fix all migrations that were setting undefined values"

This reverts commit 034713256c.

* feat: automatically convert save to remove

* Revert "fix: default single user state saving undefined value to state"

This reverts commit 6c36da6ba5.
2024-07-03 16:06:55 +02:00

104 lines
2.6 KiB
TypeScript

import * as fs from "fs";
import { ipcMain } from "electron";
import { Subject } from "rxjs";
import {
AbstractStorageService,
StorageUpdate,
} from "@bitwarden/common/platform/abstractions/storage.service";
import { NodeUtils } from "@bitwarden/node/node-utils";
// See: https://github.com/sindresorhus/electron-store/blob/main/index.d.ts
interface ElectronStoreOptions {
defaults: unknown;
name: string;
}
type ElectronStoreConstructor = new (options: ElectronStoreOptions) => ElectronStore;
// eslint-disable-next-line
const Store: ElectronStoreConstructor = require("electron-store");
interface ElectronStore {
get: (key: string) => unknown;
set: (key: string, obj: unknown) => void;
delete: (key: string) => void;
}
interface BaseOptions<T extends string> {
action: T;
key: string;
}
interface SaveOptions extends BaseOptions<"save"> {
obj: unknown;
}
type Options = BaseOptions<"get"> | BaseOptions<"has"> | SaveOptions | BaseOptions<"remove">;
export class ElectronStorageService implements AbstractStorageService {
private store: ElectronStore;
private updatesSubject = new Subject<StorageUpdate>();
updates$;
constructor(dir: string, defaults = {}) {
if (!fs.existsSync(dir)) {
NodeUtils.mkdirpSync(dir, "700");
}
const storeConfig: ElectronStoreOptions = {
defaults: defaults,
name: "data",
};
this.store = new Store(storeConfig);
this.updates$ = this.updatesSubject.asObservable();
ipcMain.handle("storageService", (event, options: Options) => {
switch (options.action) {
case "get":
return this.get(options.key);
case "has":
return this.has(options.key);
case "save":
return this.save(options.key, options.obj);
case "remove":
return this.remove(options.key);
}
});
}
get valuesRequireDeserialization(): boolean {
return true;
}
get<T>(key: string): Promise<T> {
const val = this.store.get(key) as T;
return Promise.resolve(val != null ? val : null);
}
has(key: string): Promise<boolean> {
const val = this.store.get(key);
return Promise.resolve(val != null);
}
save(key: string, obj: unknown): Promise<void> {
if (obj === undefined) {
return this.remove(key);
}
if (obj instanceof Set) {
obj = Array.from(obj);
}
this.store.set(key, obj);
this.updatesSubject.next({ key, updateType: "save" });
return Promise.resolve();
}
remove(key: string): Promise<void> {
this.store.delete(key);
this.updatesSubject.next({ key, updateType: "remove" });
return Promise.resolve();
}
}