mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-17 16:53:25 +00:00
Lock lowdb file (#95)
* Lock lowdb file when using. Do not allow caching * Linter fixes * Move to non-jslib lowdbstorage to allow for lockfile * update jslib * Must ensure db file exists prior to initialization proper-lockfile throws if the file its locking does not exist * update jslib * Let base handle file initialization
This commit is contained in:
29
src/services/lowdbStorage.service.ts
Normal file
29
src/services/lowdbStorage.service.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import * as fs from 'fs';
|
||||
import * as lock from 'proper-lockfile';
|
||||
|
||||
import { LogService } from 'jslib/abstractions/log.service';
|
||||
|
||||
import { LowdbStorageService as LowdbStorageServiceBase } from 'jslib/services/lowdbStorage.service';
|
||||
|
||||
import { Utils } from 'jslib/misc/utils';
|
||||
|
||||
export class LowdbStorageService extends LowdbStorageServiceBase {
|
||||
constructor(logService: LogService, defaults?: any, dir?: string, allowCache = false, private requireLock = false) {
|
||||
super(logService, defaults, dir, allowCache);
|
||||
}
|
||||
|
||||
protected async lockDbFile<T>(action: () => T): Promise<T> {
|
||||
if (this.requireLock && !Utils.isNullOrWhitespace(this.dataFilePath)) {
|
||||
this.logService.info('acquiring db file lock');
|
||||
return await lock.lock(this.dataFilePath, { retries: 3 }).then(release => {
|
||||
try {
|
||||
return action();
|
||||
} finally {
|
||||
release();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return action();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user