From 9f1c7a0a32a5728ba6da7cfeacc0751c0f5838f3 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 9 Jun 2018 15:26:21 -0400 Subject: [PATCH] html/memory storage services --- src/app/services.module.ts | 8 ++-- src/services/htmlStorage.service.ts | 45 ++++++++++++++++++++ src/services/memoryStorage.service.ts | 26 ++++++++++++ src/services/webStorage.service.ts | 60 --------------------------- 4 files changed, 76 insertions(+), 63 deletions(-) create mode 100644 src/services/htmlStorage.service.ts create mode 100644 src/services/memoryStorage.service.ts delete mode 100644 src/services/webStorage.service.ts diff --git a/src/app/services.module.ts b/src/app/services.module.ts index 295813c641..fa8fabc936 100644 --- a/src/app/services.module.ts +++ b/src/app/services.module.ts @@ -6,9 +6,10 @@ import { import { ToasterModule } from 'angular2-toaster'; import { BroadcasterMessagingService } from '../services/broadcasterMessaging.service'; +import { HtmlStorageService } from '../services/htmlStorage.service'; import { I18nService } from '../services/i18n.service'; +import { MemoryStorageService } from '../services/memoryStorage.service'; import { WebPlatformUtilsService } from '../services/webPlatformUtils.service'; -import { WebStorageService } from '../services/webStorage.service'; import { AuthGuardService } from 'jslib/angular/services/auth-guard.service'; import { BroadcasterService } from 'jslib/angular/services/broadcaster.service'; @@ -70,10 +71,11 @@ const stateService = new StateService(); const broadcasterService = new BroadcasterService(); const messagingService = new BroadcasterMessagingService(broadcasterService); const platformUtilsService = new WebPlatformUtilsService(messagingService, i18nService); -const storageService: StorageServiceAbstraction = new WebStorageService(); +const storageService: StorageServiceAbstraction = new HtmlStorageService(); +const secureStorageService: StorageServiceAbstraction = new MemoryStorageService(); const cryptoFunctionService: CryptoFunctionServiceAbstraction = new WebCryptoFunctionService(window, platformUtilsService); -const cryptoService = new CryptoService(storageService, storageService, cryptoFunctionService); +const cryptoService = new CryptoService(storageService, secureStorageService, cryptoFunctionService); const tokenService = new TokenService(storageService); const appIdService = new AppIdService(storageService); const apiService = new ApiService(tokenService, platformUtilsService, diff --git a/src/services/htmlStorage.service.ts b/src/services/htmlStorage.service.ts new file mode 100644 index 0000000000..a13e10a486 --- /dev/null +++ b/src/services/htmlStorage.service.ts @@ -0,0 +1,45 @@ +import { StorageService } from 'jslib/abstractions/storage.service'; +import { ConstantsService } from 'jslib/services'; + +export class HtmlStorageService implements StorageService { + private localStorageKeys = new Set(['appId', 'anonymousAppId', 'rememberedEmail', + ConstantsService.disableFaviconKey, ConstantsService.lockOptionKey, ConstantsService.localeKey, + ConstantsService.lockOptionKey]); + + get(key: string): Promise { + let json: string = null; + if (this.localStorageKeys.has(key)) { + json = window.localStorage.getItem(key); + } else { + json = window.sessionStorage.getItem(key); + } + if (json != null) { + const obj = JSON.parse(json); + return Promise.resolve(obj as T); + } + return Promise.resolve(null); + } + + save(key: string, obj: any): Promise { + if (obj == null) { + return this.remove(key); + } + + const json = JSON.stringify(obj); + if (this.localStorageKeys.has(key)) { + window.localStorage.setItem(key, json); + } else { + window.sessionStorage.setItem(key, json); + } + return Promise.resolve(); + } + + remove(key: string): Promise { + if (this.localStorageKeys.has(key)) { + window.localStorage.removeItem(key); + } else { + window.sessionStorage.removeItem(key); + } + return Promise.resolve(); + } +} diff --git a/src/services/memoryStorage.service.ts b/src/services/memoryStorage.service.ts new file mode 100644 index 0000000000..30b33c1160 --- /dev/null +++ b/src/services/memoryStorage.service.ts @@ -0,0 +1,26 @@ +import { StorageService } from 'jslib/abstractions/storage.service'; + +export class MemoryStorageService implements StorageService { + private store = new Map(); + + get(key: string): Promise { + if (this.store.has(key)) { + const obj = this.store.get(key); + return Promise.resolve(obj as T); + } + return Promise.resolve(null); + } + + save(key: string, obj: any): Promise { + if (obj == null) { + return this.remove(key); + } + this.store.set(key, obj); + return Promise.resolve(); + } + + remove(key: string): Promise { + this.store.delete(key); + return Promise.resolve(); + } +} diff --git a/src/services/webStorage.service.ts b/src/services/webStorage.service.ts deleted file mode 100644 index 0fff0c49cc..0000000000 --- a/src/services/webStorage.service.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { StorageService } from 'jslib/abstractions/storage.service'; -import { ConstantsService } from 'jslib/services'; - -export class WebStorageService implements StorageService { - private memoryStore = new Map(); - private memoryKeys = new Set(['key']); - private localStorageKeys = new Set(['appId', 'anonymousAppId', 'rememberedEmail', - ConstantsService.disableFaviconKey, ConstantsService.lockOptionKey, ConstantsService.localeKey, - ConstantsService.lockOptionKey]); - - get(key: string): Promise { - if (this.memoryKeys.has(key)) { - if (this.memoryStore.has(key)) { - const obj = this.memoryStore.get(key); - return Promise.resolve(obj as T); - } - } else { - let json: string = null; - if (this.localStorageKeys.has(key)) { - json = window.localStorage.getItem(key); - } else { - json = window.sessionStorage.getItem(key); - } - if (json != null) { - const obj = JSON.parse(json); - return Promise.resolve(obj as T); - } - } - return Promise.resolve(null); - } - - save(key: string, obj: any): Promise { - if (obj == null) { - return this.remove(key); - } - - if (this.memoryKeys.has(key)) { - this.memoryStore.set(key, obj); - } else { - const json = JSON.stringify(obj); - if (this.localStorageKeys.has(key)) { - window.localStorage.setItem(key, json); - } else { - window.sessionStorage.setItem(key, json); - } - } - return Promise.resolve(); - } - - remove(key: string): Promise { - if (this.memoryKeys.has(key)) { - this.memoryStore.delete(key); - } else if (this.localStorageKeys.has(key)) { - window.localStorage.removeItem(key); - } else { - window.sessionStorage.removeItem(key); - } - return Promise.resolve(); - } -}