1
0
mirror of https://github.com/bitwarden/cli synced 2025-12-24 12:13:13 +00:00
Files
cli/src/services/lowdbStorage.service.ts
Matt Gibson a969696541 Fix/lock lowdb file (#470)
* Lock data.json while running

* Await floating promises

* Increase retry frequency and attempt count for lock file

* tweak lock retry times

(cherry picked from commit ee664059d2)
2022-02-10 11:26:41 -05:00

43 lines
1.1 KiB
TypeScript

import * as lock from "proper-lockfile";
import { OperationOptions } from "retry";
import { LogService } from "jslib-common/abstractions/log.service";
import { LowdbStorageService as LowdbStorageServiceBase } from "jslib-node/services/lowdbStorage.service";
import { Utils } from "jslib-common/misc/utils";
const retries: OperationOptions = {
retries: 50,
minTimeout: 100,
maxTimeout: 250,
factor: 2,
};
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: retries }).then((release) => {
try {
return action();
} finally {
release();
}
});
} else {
return action();
}
}
}