mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
* 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>
149 lines
4.5 KiB
TypeScript
149 lines
4.5 KiB
TypeScript
import * as fs from "fs";
|
|
import * as path from "path";
|
|
|
|
import * as lowdb from "lowdb";
|
|
import * as FileSync from "lowdb/adapters/FileSync";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
import { AbstractStorageService } from "@bitwarden/common/abstractions/storage.service";
|
|
import { NodeUtils } from "@bitwarden/common/misc/nodeUtils";
|
|
import { sequentialize } from "@bitwarden/common/misc/sequentialize";
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
|
|
|
export class LowdbStorageService implements AbstractStorageService {
|
|
protected dataFilePath: string;
|
|
private db: lowdb.LowdbSync<any>;
|
|
private defaults: any;
|
|
private ready = false;
|
|
|
|
constructor(
|
|
protected logService: LogService,
|
|
defaults?: any,
|
|
private dir?: string,
|
|
private allowCache = false
|
|
) {
|
|
this.defaults = defaults;
|
|
}
|
|
|
|
@sequentialize(() => "lowdbStorageInit")
|
|
async init() {
|
|
if (this.ready) {
|
|
return;
|
|
}
|
|
|
|
this.logService.info("Initializing lowdb storage service.");
|
|
let adapter: lowdb.AdapterSync<any>;
|
|
if (Utils.isNode && this.dir != null) {
|
|
if (!fs.existsSync(this.dir)) {
|
|
this.logService.warning(`Could not find dir, "${this.dir}"; creating it instead.`);
|
|
NodeUtils.mkdirpSync(this.dir, "700");
|
|
this.logService.info(`Created dir "${this.dir}".`);
|
|
}
|
|
this.dataFilePath = path.join(this.dir, "data.json");
|
|
if (!fs.existsSync(this.dataFilePath)) {
|
|
this.logService.warning(
|
|
`Could not find data file, "${this.dataFilePath}"; creating it instead.`
|
|
);
|
|
fs.writeFileSync(this.dataFilePath, "", { mode: 0o600 });
|
|
fs.chmodSync(this.dataFilePath, 0o600);
|
|
this.logService.info(`Created data file "${this.dataFilePath}" with chmod 600.`);
|
|
} else {
|
|
this.logService.info(`db file "${this.dataFilePath} already exists"; using existing db`);
|
|
}
|
|
await this.lockDbFile(() => {
|
|
adapter = new FileSync(this.dataFilePath);
|
|
});
|
|
}
|
|
try {
|
|
this.logService.info("Attempting to create lowdb storage adapter.");
|
|
this.db = lowdb(adapter);
|
|
this.logService.info("Successfully created lowdb storage adapter.");
|
|
} catch (e) {
|
|
if (e instanceof SyntaxError) {
|
|
this.logService.warning(
|
|
`Error creating lowdb storage adapter, "${e.message}"; emptying data file.`
|
|
);
|
|
if (fs.existsSync(this.dataFilePath)) {
|
|
const backupPath = this.dataFilePath + ".bak";
|
|
this.logService.warning(`Writing backup of data file to ${backupPath}`);
|
|
await fs.copyFile(this.dataFilePath, backupPath, () => {
|
|
this.logService.warning(
|
|
`Error while creating data file backup, "${e.message}". No backup may have been created.`
|
|
);
|
|
});
|
|
}
|
|
adapter.write({});
|
|
this.db = lowdb(adapter);
|
|
} else {
|
|
this.logService.error(`Error creating lowdb storage adapter, "${e.message}".`);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
if (this.defaults != null) {
|
|
this.lockDbFile(() => {
|
|
this.logService.info("Writing defaults.");
|
|
this.readForNoCache();
|
|
this.db.defaults(this.defaults).write();
|
|
this.logService.info("Successfully wrote defaults to db.");
|
|
});
|
|
}
|
|
|
|
this.ready = true;
|
|
}
|
|
|
|
async get<T>(key: string): Promise<T> {
|
|
await this.waitForReady();
|
|
return this.lockDbFile(() => {
|
|
this.readForNoCache();
|
|
const val = this.db.get(key).value();
|
|
this.logService.debug(`Successfully read ${key} from db`);
|
|
if (val == null) {
|
|
return null;
|
|
}
|
|
return val as T;
|
|
});
|
|
}
|
|
|
|
has(key: string): Promise<boolean> {
|
|
return this.get(key).then((v) => v != null);
|
|
}
|
|
|
|
async save(key: string, obj: any): Promise<any> {
|
|
await this.waitForReady();
|
|
return this.lockDbFile(() => {
|
|
this.readForNoCache();
|
|
this.db.set(key, obj).write();
|
|
this.logService.debug(`Successfully wrote ${key} to db`);
|
|
return;
|
|
});
|
|
}
|
|
|
|
async remove(key: string): Promise<any> {
|
|
await this.waitForReady();
|
|
return this.lockDbFile(() => {
|
|
this.readForNoCache();
|
|
this.db.unset(key).write();
|
|
this.logService.debug(`Successfully removed ${key} from db`);
|
|
return;
|
|
});
|
|
}
|
|
|
|
protected async lockDbFile<T>(action: () => T): Promise<T> {
|
|
// Lock methods implemented in clients
|
|
return Promise.resolve(action());
|
|
}
|
|
|
|
private readForNoCache() {
|
|
if (!this.allowCache) {
|
|
this.db.read();
|
|
}
|
|
}
|
|
|
|
private async waitForReady() {
|
|
if (!this.ready) {
|
|
await this.init();
|
|
}
|
|
}
|
|
}
|