|
| 1 | +import { Storage } from './Storage'; |
| 2 | +import { Serializer, Deserializer, InMemoryCache, JsonCache } from '@azure/msal-common'; |
| 3 | +import { ICachePlugin } from './ICachePlugin'; |
| 4 | + |
| 5 | +const defaultSerializedCache: JsonCache = { |
| 6 | + Account: {}, |
| 7 | + IdToken: {}, |
| 8 | + AccessToken: {}, |
| 9 | + RefreshToken: {}, |
| 10 | + AppMetadata: {} |
| 11 | +}; |
| 12 | + |
| 13 | + |
| 14 | +export class CacheManager { |
| 15 | + |
| 16 | + hasChanged: boolean; |
| 17 | + storage: Storage; |
| 18 | + persistence: ICachePlugin; |
| 19 | + |
| 20 | + constructor(storage: Storage, cachePlugin?: ICachePlugin) { |
| 21 | + this.hasChanged = false; |
| 22 | + this.storage = storage; |
| 23 | + this.storage.registerChangeEmitter(this.handleChangeEvent.bind(this)) |
| 24 | + if (cachePlugin) { |
| 25 | + this.setPersistence(cachePlugin); |
| 26 | + } |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + setPersistence(persistence: ICachePlugin): void { |
| 31 | + this.persistence = persistence; |
| 32 | + } |
| 33 | + |
| 34 | + handleChangeEvent() { |
| 35 | + this.hasChanged = true; |
| 36 | + } |
| 37 | + |
| 38 | + mergeState(oldState: JsonCache, currentState: JsonCache) { |
| 39 | + // TODO |
| 40 | + // mergeUpdates(old, new) |
| 41 | + // mergeRemovals(old, new) |
| 42 | + return { |
| 43 | + ...oldState, |
| 44 | + ...currentState |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + //TODO think about separating serialize / deserialize from writeToPersistance and readFromPersistance |
| 49 | + |
| 50 | + async serialize(): Promise<string> { |
| 51 | + const cache: string = JSON.stringify(Serializer.serializeAllCache(this.storage.getCache())); |
| 52 | + if (this.persistence) { |
| 53 | + await this.persistence.writeToStorage((stateFromDisk) => { |
| 54 | + const jsonFromDisk = JSON.parse(stateFromDisk); |
| 55 | + return JSON.stringify( |
| 56 | + this.mergeState( |
| 57 | + jsonFromDisk, |
| 58 | + Serializer.serializeAllCache(this.storage.getCache()) |
| 59 | + ), null, 4 |
| 60 | + ); |
| 61 | + }); |
| 62 | + } |
| 63 | + this.hasChanged = false; |
| 64 | + return cache; |
| 65 | + } |
| 66 | + |
| 67 | + async deserialize(cache?: string): Promise<void> { |
| 68 | + let deserializedCache: InMemoryCache; |
| 69 | + if (this.persistence) { |
| 70 | + const stringCacheFromStorage = await this.persistence.readFromStorage(); |
| 71 | + deserializedCache = Deserializer.deserializeAllCache(this.overlayDefaults(JSON.parse(stringCacheFromStorage))); |
| 72 | + } else if (cache) { |
| 73 | + deserializedCache = Deserializer.deserializeAllCache(this.overlayDefaults(JSON.parse(cache))); |
| 74 | + } else { |
| 75 | + throw Error("Cache Must be passed in or configured"); |
| 76 | + } |
| 77 | + this.storage.setCache(deserializedCache) |
| 78 | + } |
| 79 | + |
| 80 | + cacheHasChanged(): boolean { |
| 81 | + return this.hasChanged; |
| 82 | + } |
| 83 | + |
| 84 | + private overlayDefaults (passedInCache: JsonCache): JsonCache { |
| 85 | + return { |
| 86 | + Account: { ...defaultSerializedCache.Account, ...passedInCache.Account }, |
| 87 | + IdToken: { ...defaultSerializedCache.IdToken, ...passedInCache.IdToken }, |
| 88 | + AccessToken: { ...defaultSerializedCache.AccessToken, ...passedInCache.AccessToken }, |
| 89 | + RefreshToken: { ...defaultSerializedCache.RefreshToken, ...passedInCache.RefreshToken }, |
| 90 | + AppMetadata: { ...defaultSerializedCache.AppMetadata, ...passedInCache.AppMetadata} |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments