1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-25 20:53:22 +00:00

replace electron store with lowdb

This commit is contained in:
Kyle Spearrin
2018-05-31 09:07:56 -04:00
parent 98e2e611f8
commit f618c0b5ee
7 changed files with 121 additions and 42 deletions

View File

@@ -1,36 +0,0 @@
import { StorageService } from '../../abstractions/storage.service';
// tslint:disable-next-line
const Store = require('electron-store');
export class ElectronStorageService implements StorageService {
private store: any;
constructor(defaults?: any) {
const storeConfig: any = {
defaults: {} as any,
name: 'data',
};
if (defaults != null) {
storeConfig.defaults = Object.assign({}, storeConfig.defaults, defaults);
}
this.store = new Store(storeConfig);
}
get<T>(key: string): Promise<T> {
const val = this.store.get(key) as T;
return Promise.resolve(val != null ? val : null);
}
save(key: string, obj: any): Promise<any> {
this.store.set(key, obj);
return Promise.resolve();
}
remove(key: string): Promise<any> {
this.store.delete(key);
return Promise.resolve();
}
}

16
src/misc/nodeUtils.ts Normal file
View File

@@ -0,0 +1,16 @@
import * as fs from 'fs';
import * as path from 'path';
export class NodeUtils {
static mkdirpSync(targetDir: string, mode = 755, relative = false, relativeDir: string = null) {
const initialDir = path.isAbsolute(targetDir) ? path.sep : '';
const baseDir = relative ? (relativeDir != null ? relativeDir : __dirname) : '.';
targetDir.split(path.sep).reduce((parentDir, childDir) => {
const dir = path.resolve(baseDir, parentDir, childDir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, mode);
}
return dir;
}, initialDir);
}
}

View File

@@ -13,9 +13,9 @@ export class Utils {
}
Utils.inited = true;
Utils.isNode = typeof window === 'undefined';
Utils.isBrowser = !Utils.isNode;
Utils.global = Utils.isNode ? global : window;
Utils.isNode = typeof process !== 'undefined' && (process as any).release.name === 'node';
Utils.isBrowser = typeof window !== 'undefined';
Utils.global = Utils.isNode && !Utils.isBrowser ? global : window;
}
static fromB64ToArray(str: string): Uint8Array {

View File

@@ -353,7 +353,7 @@ export class CipherService implements CipherServiceAbstraction {
const blob = new Blob([encData], { type: 'application/octet-stream' });
fd.append('data', blob, encFileName.encryptedString);
} catch (e) {
if (Utils.isNode) {
if (Utils.isNode && !Utils.isBrowser) {
fd.append('data', new Buffer(encData) as any, {
filepath: encFileName.encryptedString,
contentType: 'application/octet-stream',

View File

@@ -0,0 +1,51 @@
import * as fs from 'fs';
import * as lowdb from 'lowdb';
import * as FileSync from 'lowdb/adapters/FileSync';
import * as path from 'path';
import { StorageService } from '../abstractions/storage.service';
import { NodeUtils } from '../misc/nodeUtils';
import { Utils } from '../misc/utils';
export class LowdbStorageService implements StorageService {
private db: lowdb.LowdbSync<any>;
private defaults: any;
constructor(defaults?: any, dir?: string) {
this.defaults = defaults;
let adapter: lowdb.AdapterSync<any>;
if (Utils.isNode && dir != null) {
if (!fs.existsSync(dir)) {
NodeUtils.mkdirpSync(dir, 755);
}
const p = path.join(dir, 'data.json');
adapter = new FileSync(p);
} else if (Utils.isBrowser && !Utils.isNode) {
// local storage adapter for web
}
this.db = lowdb(adapter);
}
init() {
if (this.defaults != null) {
this.db.defaults(this.defaults).write();
}
}
get<T>(key: string): Promise<T> {
const val = this.db.get(key).value();
return Promise.resolve(val as T);
}
save(key: string, obj: any): Promise<any> {
this.db.set(key, obj).write();
return Promise.resolve();
}
remove(key: string): Promise<any> {
this.db.unset(key).write();
return Promise.resolve();
}
}