mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-06 09:20:43 +01:00
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
This commit is contained in:
parent
2ae2fdfd14
commit
ee664059d2
1627
package-lock.json
generated
1627
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -65,6 +65,8 @@
|
||||
"@types/node-fetch": "^2.5.10",
|
||||
"@types/node-forge": "^0.9.7",
|
||||
"@types/papaparse": "^5.2.5",
|
||||
"@types/proper-lockfile": "^4.1.2",
|
||||
"@types/retry": "^0.12.1",
|
||||
"@types/tldjs": "^2.3.0",
|
||||
"@types/zxcvbn": "^4.4.1",
|
||||
"clean-webpack-plugin": "^4.0.0",
|
||||
@ -107,6 +109,7 @@
|
||||
"node-forge": "0.10.0",
|
||||
"open": "^8.0.8",
|
||||
"papaparse": "^5.3.0",
|
||||
"proper-lockfile": "^4.1.2",
|
||||
"rxjs": "6.6.7",
|
||||
"tldjs": "^2.3.1",
|
||||
"zxcvbn": "^4.4.2"
|
||||
|
@ -10,6 +10,7 @@ import { LogLevelType } from "jslib-common/enums/logLevelType";
|
||||
import { AuthService } from "jslib-common/services/auth.service";
|
||||
|
||||
import { I18nService } from "./services/i18n.service";
|
||||
import { LowdbStorageService } from "./services/lowdbStorage.service";
|
||||
import { NodeEnvSecureStorageService } from "./services/nodeEnvSecureStorage.service";
|
||||
|
||||
import { CliPlatformUtilsService } from "jslib-node/cli/services/cliPlatformUtils.service";
|
||||
@ -44,7 +45,6 @@ import { TwoFactorService } from "jslib-common/services/twoFactor.service";
|
||||
import { UserVerificationService } from "jslib-common/services/userVerification.service";
|
||||
import { VaultTimeoutService } from "jslib-common/services/vaultTimeout.service";
|
||||
|
||||
import { LowdbStorageService } from "jslib-node/services/lowdbStorage.service";
|
||||
import { NodeApiService } from "jslib-node/services/nodeApi.service";
|
||||
import { NodeCryptoFunctionService } from "jslib-node/services/nodeCryptoFunction.service";
|
||||
|
||||
@ -129,7 +129,7 @@ export class Main {
|
||||
(level) => process.env.BITWARDENCLI_DEBUG !== "true" && level <= LogLevelType.Info
|
||||
);
|
||||
this.cryptoFunctionService = new NodeCryptoFunctionService();
|
||||
this.storageService = new LowdbStorageService(this.logService, null, p, false);
|
||||
this.storageService = new LowdbStorageService(this.logService, null, p, false, true);
|
||||
this.secureStorageService = new NodeEnvSecureStorageService(
|
||||
this.storageService,
|
||||
this.logService,
|
||||
|
@ -13,7 +13,7 @@ export class SendDeleteCommand {
|
||||
}
|
||||
|
||||
try {
|
||||
this.sendService.deleteWithServer(id);
|
||||
await this.sendService.deleteWithServer(id);
|
||||
return Response.success();
|
||||
} catch (e) {
|
||||
return Response.error(e);
|
||||
|
@ -67,7 +67,7 @@ export class UnlockCommand {
|
||||
}
|
||||
}
|
||||
|
||||
this.setNewSessionKey();
|
||||
await this.setNewSessionKey();
|
||||
const email = await this.stateService.getEmail();
|
||||
const kdf = await this.stateService.getKdfType();
|
||||
const kdfIterations = await this.stateService.getKdfIterations();
|
||||
|
42
src/services/lowdbStorage.service.ts
Normal file
42
src/services/lowdbStorage.service.ts
Normal file
@ -0,0 +1,42 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user