mirror of
https://github.com/bitwarden/browser
synced 2026-01-16 23:43:37 +00:00
* Use typescript-strict-plugin to iteratively turn on strict * Add strict testing to pipeline Can be executed locally through either `npm run test:types` for full type checking including spec files, or `npx tsc-strict` for only tsconfig.json included files. * turn on strict for scripts directory * Use plugin for all tsconfigs in monorepo vscode is capable of executing tsc with plugins, but uses the most relevant tsconfig to do so. If the plugin is not a part of that config, it is skipped and developers get no feedback of strict compile time issues. These updates remedy that at the cost of slightly more complex removal of the plugin when the time comes. * remove plugin from configs that extend one that already has it * Update workspace settings to honor strict plugin * Apply strict-plugin to native message test runner * Update vscode workspace to use root tsc version * `./node_modules/.bin/update-strict-comments` 🤖 This is a one-time operation. All future files should adhere to strict type checking. * Add fixme to `ts-strict-ignore` comments * `update-strict-comments` 🤖 repeated for new merge files
106 lines
2.7 KiB
TypeScript
106 lines
2.7 KiB
TypeScript
// FIXME: Update this file to be type safe and remove this and next line
|
|
// @ts-strict-ignore
|
|
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();
|
|
}
|
|
}
|