mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 16:23:44 +00:00
Lock lowdb storage file to avoid dirty data collisions (#273)
* Lock lowdb storage file to avoid dirty data collisions * Retry lock acquire rather than immediately fail * Add proper-lockfile types to dev dependencies * remove proper-lockfile from jslib. This package is incompatible with Browser implementations. * await lock on create
This commit is contained in:
@@ -10,34 +10,40 @@ import { NodeUtils } from '../misc/nodeUtils';
|
|||||||
import { Utils } from '../misc/utils';
|
import { Utils } from '../misc/utils';
|
||||||
|
|
||||||
export class LowdbStorageService implements StorageService {
|
export class LowdbStorageService implements StorageService {
|
||||||
|
protected dataFilePath: string;
|
||||||
private db: lowdb.LowdbSync<any>;
|
private db: lowdb.LowdbSync<any>;
|
||||||
private defaults: any;
|
private defaults: any;
|
||||||
private dataFilePath: string;
|
|
||||||
|
|
||||||
constructor(private logService: LogService, defaults?: any, dir?: string, private allowCache = false) {
|
constructor(protected logService: LogService, defaults?: any, private dir?: string, private allowCache = false) {
|
||||||
this.defaults = defaults;
|
this.defaults = defaults;
|
||||||
|
}
|
||||||
|
|
||||||
|
async init() {
|
||||||
this.logService.info('Initializing lowdb storage service.');
|
this.logService.info('Initializing lowdb storage service.');
|
||||||
let adapter: lowdb.AdapterSync<any>;
|
let adapter: lowdb.AdapterSync<any>;
|
||||||
if (Utils.isNode && dir != null) {
|
if (Utils.isNode && this.dir != null) {
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(this.dir)) {
|
||||||
this.logService.warning(`Could not find dir, "${dir}"; creating it instead.`);
|
this.logService.warning(`Could not find dir, "${this.dir}"; creating it instead.`);
|
||||||
NodeUtils.mkdirpSync(dir, '700');
|
NodeUtils.mkdirpSync(this.dir, '700');
|
||||||
this.logService.info(`Created dir "${dir}".`);
|
this.logService.info(`Created dir "${this.dir}".`);
|
||||||
}
|
|
||||||
this.dataFilePath = path.join(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.`);
|
|
||||||
}
|
}
|
||||||
|
this.dataFilePath = path.join(this.dir, 'data.json');
|
||||||
|
await this.lockDbFile(() => {
|
||||||
|
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`);
|
||||||
|
}
|
||||||
|
});
|
||||||
adapter = new FileSync(this.dataFilePath);
|
adapter = new FileSync(this.dataFilePath);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
this.logService.info('Attempting to create lowdb storage adapter.');
|
this.logService.info('Attempting to create lowdb storage adapter.');
|
||||||
this.db = lowdb(adapter);
|
this.db = lowdb(adapter);
|
||||||
this.logService.info('Successfuly created lowdb storage adapter.');
|
this.logService.info('Successfully created lowdb storage adapter.');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof SyntaxError) {
|
if (e instanceof SyntaxError) {
|
||||||
this.logService.warning(`Error creating lowdb storage adapter, "${e.message}"; emptying data file.`);
|
this.logService.warning(`Error creating lowdb storage adapter, "${e.message}"; emptying data file.`);
|
||||||
@@ -48,36 +54,50 @@ export class LowdbStorageService implements StorageService {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
|
||||||
if (this.defaults != null) {
|
if (this.defaults != null) {
|
||||||
this.logService.info('Writing defaults.');
|
this.lockDbFile(() => {
|
||||||
this.readForNoCache();
|
this.logService.info('Writing defaults.');
|
||||||
this.db.defaults(this.defaults).write();
|
this.readForNoCache();
|
||||||
this.logService.info('Successfully wrote defaults to db.');
|
this.db.defaults(this.defaults).write();
|
||||||
|
this.logService.info('Successfully wrote defaults to db.');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get<T>(key: string): Promise<T> {
|
get<T>(key: string): Promise<T> {
|
||||||
this.readForNoCache();
|
return this.lockDbFile(() => {
|
||||||
const val = this.db.get(key).value();
|
this.readForNoCache();
|
||||||
if (val == null) {
|
const val = this.db.get(key).value();
|
||||||
return Promise.resolve(null);
|
this.logService.debug(`Successfully read ${key} from db`);
|
||||||
}
|
if (val == null) {
|
||||||
return Promise.resolve(val as T);
|
return null;
|
||||||
|
}
|
||||||
|
return val as T;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
save(key: string, obj: any): Promise<any> {
|
save(key: string, obj: any): Promise<any> {
|
||||||
this.readForNoCache();
|
return this.lockDbFile(() => {
|
||||||
this.db.set(key, obj).write();
|
this.readForNoCache();
|
||||||
return Promise.resolve();
|
this.db.set(key, obj).write();
|
||||||
|
this.logService.debug(`Successfully wrote ${key} to db`);
|
||||||
|
return;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(key: string): Promise<any> {
|
remove(key: string): Promise<any> {
|
||||||
this.readForNoCache();
|
return this.lockDbFile(() => {
|
||||||
this.db.unset(key).write();
|
this.readForNoCache();
|
||||||
return Promise.resolve();
|
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() {
|
private readForNoCache() {
|
||||||
|
|||||||
Reference in New Issue
Block a user