1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00

[PS-1107] User Verification Service Refactor (#3219)

* UserVerificationService refactor

* Remove temp change

* move import order

* Address PR feedback
This commit is contained in:
Justin Baur
2022-08-09 21:31:02 -04:00
committed by GitHub
parent 43d428b3df
commit 1ba76dec16
35 changed files with 209 additions and 108 deletions

View File

@@ -5,23 +5,48 @@ import { ipcMain } from "electron";
import { AbstractStorageService } from "@bitwarden/common/abstractions/storage.service";
import { NodeUtils } from "@bitwarden/common/misc/nodeUtils";
// 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 = require("electron-store");
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: any;
private store: ElectronStore;
constructor(dir: string, defaults = {}) {
if (!fs.existsSync(dir)) {
NodeUtils.mkdirpSync(dir, "700");
}
const storeConfig: any = {
const storeConfig: ElectronStoreOptions = {
defaults: defaults,
name: "data",
};
this.store = new Store(storeConfig);
ipcMain.handle("storageService", (event, options) => {
ipcMain.handle("storageService", (event, options: Options) => {
switch (options.action) {
case "get":
return this.get(options.key);
@@ -45,7 +70,7 @@ export class ElectronStorageService implements AbstractStorageService {
return Promise.resolve(val != null);
}
save(key: string, obj: any): Promise<any> {
save(key: string, obj: unknown): Promise<void> {
if (obj instanceof Set) {
obj = Array.from(obj);
}
@@ -53,7 +78,7 @@ export class ElectronStorageService implements AbstractStorageService {
return Promise.resolve();
}
remove(key: string): Promise<any> {
remove(key: string): Promise<void> {
this.store.delete(key);
return Promise.resolve();
}