From cd54aedb9f694496876ca90cf77788ec4f1e1993 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 16 May 2018 15:22:32 -0400 Subject: [PATCH] lowdb storage service --- package-lock.json | 72 +++++++++++++++++----------- package.json | 3 +- src/bw.ts | 8 ++-- src/services/lowdbStorage.service.ts | 39 +++++++++++++++ src/services/nodeStorage.service.ts | 51 -------------------- 5 files changed, 89 insertions(+), 84 deletions(-) create mode 100644 src/services/lowdbStorage.service.ts delete mode 100644 src/services/nodeStorage.service.ts diff --git a/package-lock.json b/package-lock.json index d4228cd5ac..d5f9e26a98 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,21 @@ "commander": "2.15.1" } }, + "@types/lodash": { + "version": "4.14.108", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.108.tgz", + "integrity": "sha512-WD2vUOKfBBVHxWUV9iMR9RMfpuf8HquxWeAq2yqGVL7Nc4JW2+sQama0pREMqzNI3Tutj0PyxYUJwuoxxvX+xA==", + "dev": true + }, + "@types/lowdb": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@types/lowdb/-/lowdb-1.0.1.tgz", + "integrity": "sha512-tvpzG1N62KKdmCXdGgl3cC4wOJ2LJffQCQN3UHUznHFfLYx3U8ojsb2bRdflxoZiYwAgAlF7ROC6BIxfd1NVCg==", + "dev": true, + "requires": { + "@types/lodash": "4.14.108" + } + }, "@types/node": { "version": "10.0.8", "resolved": "https://registry.npmjs.org/@types/node/-/node-10.0.8.tgz", @@ -2459,7 +2474,8 @@ "imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", - "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=" + "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=", + "dev": true }, "in-publish": { "version": "2.0.0", @@ -2633,6 +2649,11 @@ "isobject": "3.0.1" } }, + "is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.1.0.tgz", + "integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o=" + }, "is-stream": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", @@ -2856,8 +2877,7 @@ "lodash": { "version": "4.17.10", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", - "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==", - "dev": true + "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==" }, "longest": { "version": "1.0.1", @@ -2865,6 +2885,18 @@ "integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=", "dev": true }, + "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.10", + "pify": "3.0.0", + "steno": "0.4.4" + } + }, "lru-cache": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.3.tgz", @@ -3197,14 +3229,6 @@ } } }, - "node-localstorage": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/node-localstorage/-/node-localstorage-1.3.1.tgz", - "integrity": "sha512-NMWCSWWc6JbHT5PyWlNT2i8r7PgGYXVntmKawY83k/M0UJScZ5jirb61TLnqKwd815DfBQu+lR3sRw08SPzIaQ==", - "requires": { - "write-file-atomic": "1.3.4" - } - }, "normalize-package-data": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz", @@ -3489,8 +3513,7 @@ "pify": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", - "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=", - "dev": true + "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=" }, "pinkie": { "version": "2.0.4", @@ -4097,11 +4120,6 @@ "integrity": "sha1-xB8vbDn8FtHNF61LXYlhFK5HDVU=", "dev": true }, - "slide": { - "version": "1.1.6", - "resolved": "https://registry.npmjs.org/slide/-/slide-1.1.6.tgz", - "integrity": "sha1-VusCfWW00tzmyy4tMsTUr8nh1wc=" - }, "snapdragon": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/snapdragon/-/snapdragon-0.8.2.tgz", @@ -4351,6 +4369,14 @@ } } }, + "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", @@ -5091,16 +5117,6 @@ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true }, - "write-file-atomic": { - "version": "1.3.4", - "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-1.3.4.tgz", - "integrity": "sha1-+Aek8LHZ6ROuekgRLmzDrxmRtF8=", - "requires": { - "graceful-fs": "4.1.11", - "imurmurhash": "0.1.4", - "slide": "1.1.6" - } - }, "xtend": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", diff --git a/package.json b/package.json index 04884ac91c..4cf4e4b267 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ }, "devDependencies": { "@types/commander": "^2.12.2", + "@types/lowdb": "^1.0.1", "@types/node": "^10.0.8", "@types/readline-sync": "^1.4.3", "clean-webpack-plugin": "^0.1.17", @@ -57,9 +58,9 @@ "dependencies": { "chalk": "2.4.1", "commander": "2.15.1", + "lowdb": "1.0.0", "node-fetch": "2.1.2", "node-forge": "0.7.1", - "node-localstorage": "1.3.1", "readline-sync": "1.4.9" } } diff --git a/src/bw.ts b/src/bw.ts index 343cf2c25d..3fca72eddb 100644 --- a/src/bw.ts +++ b/src/bw.ts @@ -1,9 +1,9 @@ import { AuthService } from 'jslib/services/auth.service'; import { I18nService } from './services/i18n.service'; +import { LowdbStorageService } from './services/lowdbStorage.service'; import { NodeEnvSecureStorageService } from './services/nodeEnvSecureStorage.service'; import { NodePlatformUtilsService } from './services/nodePlatformUtils.service'; -import { NodeStorageService } from './services/nodeStorage.service'; import { NoopMessagingService } from './services/noopMessaging.service'; import { AppIdService } from 'jslib/services/appId.service'; @@ -29,8 +29,8 @@ import { Program } from './program'; export class Main { messagingService: NoopMessagingService; - storageService: NodeStorageService; - secureStorageService: NodeStorageService; + storageService: LowdbStorageService; + secureStorageService: NodeEnvSecureStorageService; i18nService: I18nService; platformUtilsService: NodePlatformUtilsService; constantsService: ConstantsService; @@ -58,7 +58,7 @@ export class Main { this.i18nService = new I18nService('en', './locales'); this.platformUtilsService = new NodePlatformUtilsService(); this.cryptoFunctionService = new NodeCryptoFunctionService(); - this.storageService = new NodeStorageService('Bitwarden CLI'); + this.storageService = new LowdbStorageService('Bitwarden CLI'); this.secureStorageService = new NodeEnvSecureStorageService(this.storageService, () => this.cryptoService); this.cryptoService = new CryptoService(this.storageService, this.secureStorageService, this.cryptoFunctionService); diff --git a/src/services/lowdbStorage.service.ts b/src/services/lowdbStorage.service.ts new file mode 100644 index 0000000000..fc2a61d76c --- /dev/null +++ b/src/services/lowdbStorage.service.ts @@ -0,0 +1,39 @@ +import * as lowdb from 'lowdb'; +import * as FileSync from 'lowdb/adapters/FileSync'; + +import { StorageService } from 'jslib/abstractions/storage.service'; +import { Utils } from 'jslib/misc/utils'; + +export class LowdbStorageService implements StorageService { + private db: lowdb.LowdbSync; + + constructor(appDirName: string) { + let path = null; + if (process.platform === 'darwin') { + path = process.env.HOME + 'Library/Application Support'; + } else if (process.platform === 'win32') { + path = process.env.APPDATA; + } else { + path = process.env.HOME + '.config'; + } + path += ('/' + appDirName + '/data.json'); + + const adapter = new FileSync(path); + this.db = lowdb(adapter); + } + + 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(); + } +} diff --git a/src/services/nodeStorage.service.ts b/src/services/nodeStorage.service.ts deleted file mode 100644 index 90d1fb8e33..0000000000 --- a/src/services/nodeStorage.service.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { StorageService } from 'jslib/abstractions/storage.service'; -import { Utils } from 'jslib/misc/utils'; - -let localStorage = Utils.isBrowser ? window.localStorage : null; - -export class NodeStorageService implements StorageService { - constructor(appDirName: string) { - if (Utils.isNode && localStorage == null) { - const LocalStorage = require('node-localstorage').LocalStorage; - let path = null; - if (process.platform === 'darwin') { - path = process.env.HOME + 'Library/Application Support'; - } else if (process.platform === 'win32') { - path = process.env.APPDATA; - } else { - path = process.env.HOME + '.config'; - } - path += ('/' + appDirName + '/data'); - localStorage = new LocalStorage(path); - } - } - - get(key: string): Promise { - const val = localStorage.getItem(key); - if (val != null) { - try { - const obj = JSON.parse(val); - if (obj != null && obj[key] != null) { - return Promise.resolve(obj[key] as T); - } - } catch { } - } - return Promise.resolve(null); - } - - save(key: string, obj: any): Promise { - let val: string = null; - if (obj != null) { - const o: any = {}; - o[key] = obj; - val = JSON.stringify(o); - } - localStorage.setItem(key, val); - return Promise.resolve(); - } - - remove(key: string): Promise { - localStorage.removeItem(key); - return Promise.resolve(); - } -}