mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-05 09:10:53 +01:00
re-write data file if malformed json
This commit is contained in:
parent
a867c14b2a
commit
2870b7472b
@ -11,6 +11,7 @@ import { Utils } from '../misc/utils';
|
|||||||
export class LowdbStorageService implements StorageService {
|
export class LowdbStorageService implements StorageService {
|
||||||
private db: lowdb.LowdbSync<any>;
|
private db: lowdb.LowdbSync<any>;
|
||||||
private defaults: any;
|
private defaults: any;
|
||||||
|
private dataFilePath: string;
|
||||||
|
|
||||||
constructor(defaults?: any, dir?: string, private allowCache = false) {
|
constructor(defaults?: any, dir?: string, private allowCache = false) {
|
||||||
this.defaults = defaults;
|
this.defaults = defaults;
|
||||||
@ -20,25 +21,30 @@ export class LowdbStorageService implements StorageService {
|
|||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
NodeUtils.mkdirpSync(dir, '700');
|
NodeUtils.mkdirpSync(dir, '700');
|
||||||
}
|
}
|
||||||
const p = path.join(dir, 'data.json');
|
this.dataFilePath = path.join(dir, 'data.json');
|
||||||
adapter = new FileSync(p);
|
adapter = new FileSync(this.dataFilePath);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
this.db = lowdb(adapter);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof SyntaxError) {
|
||||||
|
fs.writeFileSync(this.dataFilePath, '');
|
||||||
|
this.db = lowdb(adapter);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.db = lowdb(adapter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
if (this.defaults != null) {
|
if (this.defaults != null) {
|
||||||
if (!this.allowCache) {
|
this.readForNoCache();
|
||||||
this.db.read();
|
|
||||||
}
|
|
||||||
this.db.defaults(this.defaults).write();
|
this.db.defaults(this.defaults).write();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get<T>(key: string): Promise<T> {
|
get<T>(key: string): Promise<T> {
|
||||||
if (!this.allowCache) {
|
this.readForNoCache();
|
||||||
this.db.read();
|
|
||||||
}
|
|
||||||
const val = this.db.get(key).value();
|
const val = this.db.get(key).value();
|
||||||
if (val == null) {
|
if (val == null) {
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
@ -47,18 +53,20 @@ export class LowdbStorageService implements StorageService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
save(key: string, obj: any): Promise<any> {
|
save(key: string, obj: any): Promise<any> {
|
||||||
if (!this.allowCache) {
|
this.readForNoCache();
|
||||||
this.db.read();
|
|
||||||
}
|
|
||||||
this.db.set(key, obj).write();
|
this.db.set(key, obj).write();
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(key: string): Promise<any> {
|
remove(key: string): Promise<any> {
|
||||||
if (!this.allowCache) {
|
this.readForNoCache();
|
||||||
this.db.read();
|
|
||||||
}
|
|
||||||
this.db.unset(key).write();
|
this.db.unset(key).write();
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readForNoCache() {
|
||||||
|
if (!this.allowCache) {
|
||||||
|
this.db.read();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user