1
0
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:
Matt Gibson
2021-02-17 10:33:05 -06:00
committed by GitHub
parent e5d0b3a372
commit 2583068dbd
16 changed files with 107 additions and 96 deletions

View 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();
}
}
}