From 9d8a8021deb526105114178650d70951bf7c1f8d Mon Sep 17 00:00:00 2001 From: Robert Ing Date: Tue, 23 Jan 2024 10:15:36 -0500 Subject: [PATCH 1/2] feat: Hash keys for idCache --- src/identity-utils.ts | 9 +++++---- test/src/tests-identity-utils.ts | 10 +++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/identity-utils.ts b/src/identity-utils.ts index 6cb31681..0d7bf342 100644 --- a/src/identity-utils.ts +++ b/src/identity-utils.ts @@ -7,6 +7,7 @@ import { IdentityAPIMethod } from './sdkRuntimeModels'; import { isObject } from './utils'; const { Identify, Modify, Login, Logout } = Constants.IdentityMethods; import { ONE_DAY_IN_SECONDS, MILLIS_IN_ONE_SEC } from './constants'; +import { generateHash } from './utils'; export interface IKnownIdentities extends UserIdentities { device_application_stamp?: string; @@ -62,7 +63,7 @@ export const cacheIdentityRequest = ( const cache: Dictionary = idCache.retrieve() || ({} as Dictionary); const cacheKey = concatenateIdentities(method, identities); - cache[cacheKey] = { responseText: xhr.responseText, status: xhr.status, expireTimestamp}; + cache[generateHash(cacheKey)] = { responseText: xhr.responseText, status: xhr.status, expireTimestamp}; idCache.store(cache); }; @@ -120,10 +121,10 @@ export const hasValidCachedIdentity = ( return false; } - const cacheKey: string = concatenateIdentities( + const cacheKey: number = generateHash(concatenateIdentities( method, proposedUserIdentities - ); + )); // if cache doesn't have the cacheKey, there is no valid cached identity if (!cache.hasOwnProperty(cacheKey)) { @@ -153,7 +154,7 @@ export const getCachedIdentity = ( ); const cache = idCache.retrieve(); - const cachedIdentity = cache ? cache[cacheKey] : null; + const cachedIdentity = cache ? cache[generateHash(cacheKey)] : null; return cachedIdentity; }; diff --git a/test/src/tests-identity-utils.ts b/test/src/tests-identity-utils.ts index c63b8ed3..b0b1814a 100644 --- a/test/src/tests-identity-utils.ts +++ b/test/src/tests-identity-utils.ts @@ -9,7 +9,7 @@ import { ICachedIdentityCall } from "../../src/identity-utils"; import { LocalStorageVault } from "../../src/vault"; -import { Dictionary } from "../../src/utils"; +import { Dictionary, generateHash } from "../../src/utils"; import { expect } from 'chai'; import { MILLISECONDS_IN_ONE_DAY, @@ -133,7 +133,7 @@ describe('identity-utils', () => { expect(Object.keys(updatedMpIdCache!).length).to.equal(1); const cachedKey = - 'identify:device_application_stamp=test-device-id;customerid=id1;'; + generateHash('identify:device_application_stamp=test-device-id;customerid=id1;'); expect(updatedMpIdCache!.hasOwnProperty(cachedKey)).to.equal(true); @@ -180,7 +180,7 @@ describe('identity-utils', () => { const updatedMpIdCache = cacheVault.retrieve(); expect(Object.keys(updatedMpIdCache!).length).to.equal(1); - const cachedKey = 'login:device_application_stamp=test-device-id;customerid=id1;'; + const cachedKey = generateHash('login:device_application_stamp=test-device-id;customerid=id1;'); expect(updatedMpIdCache!.hasOwnProperty(cachedKey)).to.equal(true); const cachedLoginCall: ICachedIdentityCall = updatedMpIdCache![ @@ -421,9 +421,9 @@ describe('identity-utils', () => { const updatedMpIdCache = cacheVault.retrieve(); const knownIdentities1CachedKey = - 'identify:device_application_stamp=test-device-id;customerid=id1;'; + generateHash('identify:device_application_stamp=test-device-id;customerid=id1;'); const knownIdentities2CachedKey = - 'identify:device_application_stamp=test-device-id;customerid=id2;'; + generateHash('identify:device_application_stamp=test-device-id;customerid=id2;'); // both known identities cache keys should exist on the cacheVault From c71ae92308c985c94a16396db1a449fad78cafea Mon Sep 17 00:00:00 2001 From: Robert Ing Date: Tue, 23 Jan 2024 10:38:44 -0500 Subject: [PATCH 2/2] refactor to make generating hashed key easier to read --- src/identity-utils.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/identity-utils.ts b/src/identity-utils.ts index 0d7bf342..37434109 100644 --- a/src/identity-utils.ts +++ b/src/identity-utils.ts @@ -62,8 +62,9 @@ export const cacheIdentityRequest = ( ): void => { const cache: Dictionary = idCache.retrieve() || ({} as Dictionary); const cacheKey = concatenateIdentities(method, identities); + const hashedKey = generateHash(cacheKey); - cache[generateHash(cacheKey)] = { responseText: xhr.responseText, status: xhr.status, expireTimestamp}; + cache[hashedKey] = { responseText: xhr.responseText, status: xhr.status, expireTimestamp}; idCache.store(cache); }; @@ -121,20 +122,21 @@ export const hasValidCachedIdentity = ( return false; } - const cacheKey: number = generateHash(concatenateIdentities( + const cacheKey: string = concatenateIdentities( method, proposedUserIdentities - )); + ); + const hashedKey = generateHash(cacheKey); // if cache doesn't have the cacheKey, there is no valid cached identity - if (!cache.hasOwnProperty(cacheKey)) { + if (!cache.hasOwnProperty(hashedKey)) { return false; } // If there is a valid cache key, compare the expireTimestamp to the current time. // If the current time is greater than the expireTimestamp, it is not a valid // cached identity. - const expireTimestamp = cache[cacheKey].expireTimestamp; + const expireTimestamp = cache[hashedKey].expireTimestamp; if (expireTimestamp < new Date().getTime()) { return false; @@ -152,9 +154,10 @@ export const getCachedIdentity = ( method, proposedUserIdentities ); + const hashedKey = generateHash(cacheKey); const cache = idCache.retrieve(); - const cachedIdentity = cache ? cache[generateHash(cacheKey)] : null; + const cachedIdentity = cache ? cache[hashedKey] : null; return cachedIdentity; };