From f618c0b5ee7bcf3cc351721611fabf57dd785211 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 31 May 2018 09:07:56 -0400 Subject: [PATCH] replace electron store with lowdb --- package-lock.json | 50 +++++++++++++++++- package.json | 2 + .../services/electronStorage.service.ts | 36 ------------- src/misc/nodeUtils.ts | 16 ++++++ src/misc/utils.ts | 6 +-- src/services/cipher.service.ts | 2 +- src/services/lowdbStorage.service.ts | 51 +++++++++++++++++++ 7 files changed, 121 insertions(+), 42 deletions(-) delete mode 100644 src/electron/services/electronStorage.service.ts create mode 100644 src/misc/nodeUtils.ts create mode 100644 src/services/lowdbStorage.service.ts diff --git a/package-lock.json b/package-lock.json index 81032db08c..91dce5732d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 55038134e1..fbdc1ccbed 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/electron/services/electronStorage.service.ts b/src/electron/services/electronStorage.service.ts deleted file mode 100644 index 953e33c9d9..0000000000 --- a/src/electron/services/electronStorage.service.ts +++ /dev/null @@ -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(key: string): Promise { - const val = this.store.get(key) as T; - return Promise.resolve(val != null ? val : null); - } - - save(key: string, obj: any): Promise { - this.store.set(key, obj); - return Promise.resolve(); - } - - remove(key: string): Promise { - this.store.delete(key); - return Promise.resolve(); - } -} diff --git a/src/misc/nodeUtils.ts b/src/misc/nodeUtils.ts new file mode 100644 index 0000000000..e4a79276ca --- /dev/null +++ b/src/misc/nodeUtils.ts @@ -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); + } +} diff --git a/src/misc/utils.ts b/src/misc/utils.ts index 77535331df..3ca7ffefdf 100644 --- a/src/misc/utils.ts +++ b/src/misc/utils.ts @@ -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 { diff --git a/src/services/cipher.service.ts b/src/services/cipher.service.ts index cc0ee018a8..45b2b93e99 100644 --- a/src/services/cipher.service.ts +++ b/src/services/cipher.service.ts @@ -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', diff --git a/src/services/lowdbStorage.service.ts b/src/services/lowdbStorage.service.ts new file mode 100644 index 0000000000..8aab0eae5a --- /dev/null +++ b/src/services/lowdbStorage.service.ts @@ -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; + private defaults: any; + + constructor(defaults?: any, dir?: string) { + this.defaults = defaults; + + let adapter: lowdb.AdapterSync; + 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(key: string): Promise { + const val = this.db.get(key).value(); + return Promise.resolve(val as T); + } + + save(key: string, obj: any): Promise { + this.db.set(key, obj).write(); + return Promise.resolve(); + } + + remove(key: string): Promise { + this.db.unset(key).write(); + return Promise.resolve(); + } +}