bitwarden-estensione-browser/apps/desktop/src/services/electron-storage.service.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-03-12 03:36:29 +01:00
import * as fs from "fs";
2022-02-22 15:39:11 +01:00
import { ipcMain } from "electron";
2019-03-12 03:36:29 +01:00
PS-813 Add memory storage to state service (#2892) * Use abstract methods and generics in StorageService * Prepend `Abstract` to abstract classes * Create session browser storage service * Use memory storage service for state memory * Inject memory storage service * Maintain filename extensions to help ide formatting * Preserve state if it's still in memory * Use jslib's memory storage service * linter * Create prototypes on stored objects * standardize package scripts * Add type safety to `withPrototype` decorators * webpack notify manifest version * Fix desktop * linter * Fix script * Improve prototye application * do not change prototype if it already matches desired * fix error with object values prototype application * Handle null state * Apply prototypes to browser-specific state * Add angular language server to recommended extensions * Improve browser state service tests * Start testing state Service * Fix abstract returns * Move test setup files to not be picked up by default glob matchers * Add key generation service * Add low-dependency encrypt service * Back crypto service with encrypt service. We'll want to work items that don't require state over to encrypt service * Add new storage service and tests * Properly init more stored values * Fix reload issues when state service is recovering state from session storage Co-authored-by: Thomas Avery <Thomas-Avery@users.noreply.github.com> Co-authored-by: Justin Baur <admin@justinbaur.com> * Simplify encrypt service * Do not log mac failures for local-backed session storage * `content` changed to `main` in #2245 * Fix CLI * Remove loggin * PR feedback * Merge remote-tracking branch 'origin/master' into add-memory-storage-to-state-service * Fix desktop * Fix decrypt method signature * Minify if not development * Key is required Co-authored-by: Thomas Avery <Thomas-Avery@users.noreply.github.com> Co-authored-by: Justin Baur <admin@justinbaur.com>
2022-06-27 19:38:12 +02:00
import { AbstractStorageService } from "@bitwarden/common/abstractions/storage.service";
2022-06-14 17:10:53 +02:00
import { NodeUtils } from "@bitwarden/common/misc/nodeUtils";
2019-03-12 03:36:29 +01:00
// See: https://github.com/sindresorhus/electron-store/blob/main/index.d.ts
interface ElectronStoreOptions {
defaults: unknown;
name: string;
}
type ElectronStoreConstructor = new (options: ElectronStoreOptions) => ElectronStore;
2022-02-22 15:39:11 +01:00
// 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">;
2019-03-12 03:36:29 +01:00
PS-813 Add memory storage to state service (#2892) * Use abstract methods and generics in StorageService * Prepend `Abstract` to abstract classes * Create session browser storage service * Use memory storage service for state memory * Inject memory storage service * Maintain filename extensions to help ide formatting * Preserve state if it's still in memory * Use jslib's memory storage service * linter * Create prototypes on stored objects * standardize package scripts * Add type safety to `withPrototype` decorators * webpack notify manifest version * Fix desktop * linter * Fix script * Improve prototye application * do not change prototype if it already matches desired * fix error with object values prototype application * Handle null state * Apply prototypes to browser-specific state * Add angular language server to recommended extensions * Improve browser state service tests * Start testing state Service * Fix abstract returns * Move test setup files to not be picked up by default glob matchers * Add key generation service * Add low-dependency encrypt service * Back crypto service with encrypt service. We'll want to work items that don't require state over to encrypt service * Add new storage service and tests * Properly init more stored values * Fix reload issues when state service is recovering state from session storage Co-authored-by: Thomas Avery <Thomas-Avery@users.noreply.github.com> Co-authored-by: Justin Baur <admin@justinbaur.com> * Simplify encrypt service * Do not log mac failures for local-backed session storage * `content` changed to `main` in #2245 * Fix CLI * Remove loggin * PR feedback * Merge remote-tracking branch 'origin/master' into add-memory-storage-to-state-service * Fix desktop * Fix decrypt method signature * Minify if not development * Key is required Co-authored-by: Thomas Avery <Thomas-Avery@users.noreply.github.com> Co-authored-by: Justin Baur <admin@justinbaur.com>
2022-06-27 19:38:12 +02:00
export class ElectronStorageService implements AbstractStorageService {
private store: ElectronStore;
2019-03-12 03:36:29 +01:00
constructor(dir: string, defaults = {}) {
if (!fs.existsSync(dir)) {
NodeUtils.mkdirpSync(dir, "700");
}
const storeConfig: ElectronStoreOptions = {
2019-03-12 03:36:29 +01:00
defaults: defaults,
name: "data",
2021-12-16 13:36:21 +01:00
};
2019-03-12 03:36:29 +01:00
this.store = new Store(storeConfig);
2021-12-16 13:36:21 +01:00
ipcMain.handle("storageService", (event, options: Options) => {
switch (options.action) {
2021-12-16 13:36:21 +01:00
case "get":
return this.get(options.key);
2021-12-16 13:36:21 +01:00
case "has":
return this.has(options.key);
2021-12-16 13:36:21 +01:00
case "save":
return this.save(options.key, options.obj);
case "remove":
return this.remove(options.key);
2021-12-16 13:36:21 +01:00
}
});
}
2019-03-12 03:36:29 +01:00
get<T>(key: string): Promise<T> {
const val = this.store.get(key) as T;
return Promise.resolve(val != null ? val : null);
2021-12-16 13:36:21 +01:00
}
has(key: string): Promise<boolean> {
2019-03-12 03:36:29 +01:00
const val = this.store.get(key);
return Promise.resolve(val != null);
2021-12-16 13:36:21 +01:00
}
save(key: string, obj: unknown): Promise<void> {
if (obj instanceof Set) {
obj = Array.from(obj);
2019-03-12 03:36:29 +01:00
}
this.store.set(key, obj);
return Promise.resolve();
2021-12-16 13:36:21 +01:00
}
remove(key: string): Promise<void> {
2019-03-12 03:36:29 +01:00
this.store.delete(key);
return Promise.resolve();
2021-12-16 13:36:21 +01:00
}
2019-03-12 03:36:29 +01:00
}