Skip to content

Commit f5a23a9

Browse files
author
Santiago Gonzalez
committed
PR feedback. Rename CacheManager -> TokenCache
1 parent 6d17dc4 commit f5a23a9

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

lib/msal-node/src/cache/CacheManager.ts renamed to lib/msal-node/src/cache/TokenCache.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const defaultSerializedCache: JsonCache = {
2424
/**
2525
* In-memory token cache manager
2626
*/
27-
export class CacheManager {
27+
export class TokenCache {
2828

2929
private storage: Storage;
3030
private hasChanged: boolean;
@@ -84,7 +84,6 @@ export class CacheManager {
8484
async writeToPersistence(): Promise<void> {
8585
if (this.persistence) {
8686
let cache = Serializer.serializeAllCache(this.storage.getCache() as InMemoryCache);
87-
8887
const getMergedState = (stateFromDisk: string) => {
8988

9089
if (!StringUtils.isEmpty(stateFromDisk)) {
@@ -144,13 +143,13 @@ export class CacheManager {
144143
*/
145144
private mergeUpdates(oldState: any, newState: any): JsonCache {
146145

147-
let finalState = oldState;
148-
Object.keys(newState).forEach((newKey) => {
146+
let finalState = { ...oldState };
147+
Object.keys(newState).forEach((newKey: string) => {
149148
let newValue = newState[newKey];
150149

151150
// if oldState does not contain value but newValue does, add it
152151
if (!finalState.hasOwnProperty(newKey)) {
153-
if (newValue != null) {
152+
if (newValue !== null) {
154153
finalState[newKey] = newValue;
155154
}
156155
} else {
@@ -177,11 +176,11 @@ export class CacheManager {
177176
* @param newState
178177
*/
179178
private mergeRemovals(oldState: JsonCache, newState: JsonCache): JsonCache {
180-
const accounts = oldState.Account != null ? this.mergeRemovalsStringDict(oldState.Account, newState.Account) : oldState.Account;
181-
const accessTokens = oldState.AccessToken != null ? this.mergeRemovalsStringDict(oldState.AccessToken, newState.AccessToken) : oldState.AccessToken;
182-
const refreshTokens = oldState.RefreshToken != null ? this.mergeRemovalsStringDict(oldState.RefreshToken, newState.RefreshToken) : oldState.RefreshToken;
183-
const idTokens = oldState.IdToken != null ? this.mergeRemovalsStringDict(oldState.IdToken, newState.IdToken) : oldState.IdToken;
184-
const appMetadata = oldState.AppMetadata != null ? this.mergeRemovalsStringDict(oldState.AppMetadata, newState.AppMetadata) : oldState.AppMetadata;
179+
const accounts = oldState.Account !== null ? this.mergeRemovalsStringDict(oldState.Account, newState.Account) : oldState.Account;
180+
const accessTokens = oldState.AccessToken !== null ? this.mergeRemovalsStringDict(oldState.AccessToken, newState.AccessToken) : oldState.AccessToken;
181+
const refreshTokens = oldState.RefreshToken !== null ? this.mergeRemovalsStringDict(oldState.RefreshToken, newState.RefreshToken) : oldState.RefreshToken;
182+
const idTokens = oldState.IdToken !== null ? this.mergeRemovalsStringDict(oldState.IdToken, newState.IdToken) : oldState.IdToken;
183+
const appMetadata = oldState.AppMetadata !== null ? this.mergeRemovalsStringDict(oldState.AppMetadata, newState.AppMetadata) : oldState.AppMetadata;
185184

186185
return {
187186
Account: accounts,
@@ -192,14 +191,14 @@ export class CacheManager {
192191
}
193192
}
194193

195-
private mergeRemovalsStringDict(oldAccounts: StringDict, newAccounts?: StringDict): StringDict {
196-
let finalAccounts = oldAccounts;
197-
Object.keys(oldAccounts).forEach((oldKey) => {
198-
if (!newAccounts || !(newAccounts.hasOwnProperty(oldKey))) {
199-
delete finalAccounts[oldKey];
194+
private mergeRemovalsStringDict(oldState: StringDict, newState?: StringDict): StringDict {
195+
let finalState = { ...oldState };
196+
Object.keys(oldState).forEach((oldKey) => {
197+
if (!newState || !(newState.hasOwnProperty(oldKey))) {
198+
delete finalState[oldKey];
200199
}
201200
});
202-
return finalAccounts;
201+
return finalState;
203202
}
204203

205204
private overlayDefaults(passedInCache: JsonCache): JsonCache {

lib/msal-node/src/client/ClientApplication.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import { CryptoProvider } from '../crypto/CryptoProvider';
2424
import { Storage } from '../cache/Storage';
2525
import { version } from '../../package.json';
2626
import { Constants as NodeConstants } from './../utils/Constants';
27-
import { CacheManager } from '../cache/CacheManager';
27+
import { TokenCache } from '../cache/TokenCache';
2828

2929
export abstract class ClientApplication {
3030
private config: Configuration;
3131
private _authority: Authority;
3232
private readonly cryptoProvider: CryptoProvider;
3333
private storage: Storage;
34-
private cacheManager: CacheManager;
34+
private tokenCache: TokenCache;
3535

3636
/**
3737
* @constructor
@@ -40,7 +40,7 @@ export abstract class ClientApplication {
4040
protected constructor(configuration: Configuration) {
4141
this.config = buildAppConfiguration(configuration);
4242
this.storage = new Storage();
43-
this.cacheManager = new CacheManager(
43+
this.tokenCache = new TokenCache(
4444
this.storage,
4545
this.config.cache?.cachePlugin
4646
);
@@ -106,8 +106,8 @@ export abstract class ClientApplication {
106106
return refreshTokenClient.acquireToken(this.initializeRequestScopes(request) as RefreshTokenRequest);
107107
}
108108

109-
getCacheManager(): CacheManager {
110-
return this.cacheManager;
109+
getCacheManager(): TokenCache {
110+
return this.tokenCache;
111111
}
112112

113113
protected async buildOauthClientConfiguration(authority?: string): Promise<ClientConfiguration> {

lib/msal-node/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export { PublicClientApplication } from './client/PublicClientApplication';
22
export { ConfidentialClientApplication } from './client/ConfidentialClientApplication';
33
export { Configuration, buildAppConfiguration } from './config/Configuration';
44
export { Storage } from './cache/Storage';
5-
export { CacheManager } from './cache/CacheManager';
5+
export { TokenCache } from './cache/TokenCache';
66
export { ICachePlugin } from './cache/ICachePlugin';
77

88
// crypto

0 commit comments

Comments
 (0)