1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-24 16:49:26 +01: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

50
package-lock.json generated
View File

@ -105,6 +105,21 @@
"integrity": "sha512-loKBID6UL4QjhD2scuvv6oAPlQ/WAY7aYTDyKlKo7fIgriLS8EZExqT567cHL5CY6si51MRoX1+r3mitD3eYrA==",
"dev": true
},
"@types/lodash": {
"version": "4.14.109",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.109.tgz",
"integrity": "sha512-hop8SdPUEzbcJm6aTsmuwjIYQo1tqLseKCM+s2bBqTU2gErwI4fE+aqUVOlscPSQbKHKgtMMPoC+h4AIGOJYvw==",
"dev": true
},
"@types/lowdb": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@types/lowdb/-/lowdb-1.0.2.tgz",
"integrity": "sha512-0lS8jOba45tcXne01LXkw06x8uqpIKuh8LTwTOo2zmIXCVoXXmIxAemAGoLJvzNc8Q0qBG+fJT0xJMx7N0FLtA==",
"dev": true,
"requires": {
"@types/lodash": "4.14.109"
}
},
"@types/lunr": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/@types/lunr/-/lunr-2.1.5.tgz",
@ -4220,6 +4235,11 @@
"integrity": "sha1-IHurkWOEmcB7Kt8kCkGochADRXU=",
"dev": true
},
"is-promise": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.1.0.tgz",
"integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o="
},
"is-redirect": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-redirect/-/is-redirect-1.0.0.tgz",
@ -5413,8 +5433,7 @@
"lodash": {
"version": "4.17.4",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz",
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=",
"dev": true
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4="
},
"lodash.isequal": {
"version": "4.5.0",
@ -5494,6 +5513,25 @@
"signal-exit": "3.0.2"
}
},
"lowdb": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/lowdb/-/lowdb-1.0.0.tgz",
"integrity": "sha512-2+x8esE/Wb9SQ1F9IHaYWfsC9FIecLOPrK4g17FGEayjUWH172H6nwicRovGvSE2CPZouc2MCIqCI7h9d+GftQ==",
"requires": {
"graceful-fs": "4.1.11",
"is-promise": "2.1.0",
"lodash": "4.17.4",
"pify": "3.0.0",
"steno": "0.4.4"
},
"dependencies": {
"pify": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz",
"integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY="
}
}
},
"lowercase-keys": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz",
@ -8044,6 +8082,14 @@
"integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=",
"dev": true
},
"steno": {
"version": "0.4.4",
"resolved": "https://registry.npmjs.org/steno/-/steno-0.4.4.tgz",
"integrity": "sha1-BxEFvfwobmYVwEA8J+nXtdy4Vcs=",
"requires": {
"graceful-fs": "4.1.11"
}
},
"stream-browserify": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/stream-browserify/-/stream-browserify-2.0.1.tgz",

View File

@ -27,6 +27,7 @@
"@types/form-data": "^2.2.1",
"@types/jasmine": "^2.8.2",
"@types/keytar": "^4.0.1",
"@types/lowdb": "^1.0.1",
"@types/lunr": "2.1.5",
"@types/node": "8.0.19",
"@types/node-fetch": "^1.6.9",
@ -75,6 +76,7 @@
"electron-updater": "2.21.4",
"form-data": "2.3.2",
"keytar": "4.1.0",
"lowdb": "1.0.0",
"lunr": "2.1.6",
"node-fetch": "2.1.2",
"node-forge": "0.7.1",

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