From 7d3b50c1fb96fa581b9916d20f2850f7bcc07a71 Mon Sep 17 00:00:00 2001 From: Jaissica Hora Date: Tue, 9 Sep 2025 09:46:18 -0400 Subject: [PATCH] refactor: created factory pattern for Vault --- src/batchUploader.ts | 21 +++++----- src/constants.ts | 5 +++ src/foregroundTimeTracker.ts | 7 ++-- src/mp-instance.ts | 6 +-- src/vault.ts | 15 +++++++ test/src/tests-identity-utils.ts | 15 +++---- test/src/vault.spec.ts | 70 +++++++++++++++++++------------- 7 files changed, 87 insertions(+), 52 deletions(-) diff --git a/src/batchUploader.ts b/src/batchUploader.ts index 12b379116..ad9d7fe5f 100644 --- a/src/batchUploader.ts +++ b/src/batchUploader.ts @@ -4,7 +4,8 @@ import { SDKEvent, SDKEventCustomFlags, SDKLoggerApi } from './sdkRuntimeModels' import { convertEvents } from './sdkToEventsApiConverter'; import { MessageType, EventType } from './types'; import { getRampNumber, isEmpty } from './utils'; -import { SessionStorageVault, LocalStorageVault } from './vault'; +import { createVault, BaseVault } from './vault'; +import { VaultKind } from './constants'; import { AsyncUploader, FetchUploader, @@ -41,8 +42,8 @@ export class BatchUploader { mpInstance: IMParticleWebSDKInstance; uploadUrl: string; batchingEnabled: boolean; - private eventVault: SessionStorageVault; - private batchVault: LocalStorageVault; + private eventVault: BaseVault; + private batchVault: BaseVault; private offlineStorageEnabled: boolean = false; private uploader: AsyncUploader; private lastASTEventTime: number = 0; @@ -75,18 +76,16 @@ export class BatchUploader { this.offlineStorageEnabled = this.isOfflineStorageAvailable(); if (this.offlineStorageEnabled) { - this.eventVault = new SessionStorageVault( + this.eventVault = createVault( `${mpInstance._Store.storageName}-events`, - { - logger: mpInstance.Logger, - } + VaultKind.SessionStorage, + { logger: mpInstance.Logger } ); - this.batchVault = new LocalStorageVault( + this.batchVault = createVault( `${mpInstance._Store.storageName}-batches`, - { - logger: mpInstance.Logger, - } + VaultKind.LocalStorage, + { logger: mpInstance.Logger } ); // Load Events from Session Storage in case we have any in storage diff --git a/src/constants.ts b/src/constants.ts index ea45293ef..a634c8871 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -204,6 +204,11 @@ const Constants = { export default Constants; +export enum VaultKind { + LocalStorage = 'local', + SessionStorage = 'session', +} + // https://go.mparticle.com/work/SQDSDKS-6080 export const ONE_DAY_IN_SECONDS = 60 * 60 * 24; export const MILLIS_IN_ONE_SEC = 1000; diff --git a/src/foregroundTimeTracker.ts b/src/foregroundTimeTracker.ts index c64ca3eff..a6b71dbdc 100644 --- a/src/foregroundTimeTracker.ts +++ b/src/foregroundTimeTracker.ts @@ -1,16 +1,17 @@ import { isNumber } from './utils'; -import { LocalStorageVault } from './vault'; +import { createVault, BaseVault } from './vault'; +import { VaultKind } from './constants'; export default class ForegroundTimeTracker { private isTrackerActive: boolean = false; private localStorageName: string = ''; - private timerVault: LocalStorageVault; + private timerVault: BaseVault; public startTime: number = 0; public totalTime: number = 0; constructor(timerKey: string) { this.localStorageName = `mprtcl-tos-${timerKey}`; - this.timerVault = new LocalStorageVault(this.localStorageName); + this.timerVault = createVault(this.localStorageName, VaultKind.LocalStorage); this.loadTimeFromStorage(); this.addHandlers(); if (document.hidden === false) { diff --git a/src/mp-instance.ts b/src/mp-instance.ts index a3739bddd..b4b6e0a39 100644 --- a/src/mp-instance.ts +++ b/src/mp-instance.ts @@ -17,7 +17,7 @@ // jQuery v1.10.2 | (c) 2005, 2013 jQuery Foundation, Inc. | jquery.org/license import { EventType, IdentityType, CommerceEventType, PromotionActionType, ProductActionType, MessageType } from './types'; -import Constants from './constants'; +import Constants, { VaultKind } from './constants'; import APIClient, { IAPIClient } from './apiClient'; import Helpers from './helpers'; import NativeSdkHelpers from './nativeSdkHelpers'; @@ -37,7 +37,7 @@ import KitBlocker from './kitBlocking'; import ConfigAPIClient, { IKitConfigs } from './configAPIClient'; import IdentityAPIClient from './identityApiClient'; import { isFunction, parseConfig } from './utils'; -import { LocalStorageVault } from './vault'; +import { createVault } from './vault'; import { removeExpiredIdentityCacheDates } from './identity-utils'; import IntegrationCapture from './integrationCapture'; import { IPreInit, processReadyQueue } from './pre-init-utils'; @@ -1523,7 +1523,7 @@ function createKitBlocker(config, mpInstance) { } function createIdentityCache(mpInstance) { - return new LocalStorageVault(`${mpInstance._Store.storageName}-id-cache`, { + return createVault(`${mpInstance._Store.storageName}-id-cache`, VaultKind.LocalStorage, { logger: mpInstance.Logger, }); } diff --git a/src/vault.ts b/src/vault.ts index 57d2a6dc1..9186b3064 100644 --- a/src/vault.ts +++ b/src/vault.ts @@ -1,5 +1,6 @@ import { Logger } from '@mparticle/web-sdk'; import { isEmpty, isNumber } from './utils'; +import { VaultKind } from './constants'; export interface IVaultOptions { logger?: Logger; @@ -102,4 +103,18 @@ export class SessionStorageVault extends BaseVault { constructor(storageKey: string, options?: IVaultOptions) { super(storageKey, window.sessionStorage, options); } +} + +export function createVault( + storageKey: string, + kind: VaultKind, + options?: IVaultOptions +): BaseVault { + switch (kind) { + case VaultKind.SessionStorage: + return new SessionStorageVault(storageKey, options); + case VaultKind.LocalStorage: + default: + return new LocalStorageVault(storageKey, options); + } } \ No newline at end of file diff --git a/test/src/tests-identity-utils.ts b/test/src/tests-identity-utils.ts index 0c957760a..13bf7abd3 100644 --- a/test/src/tests-identity-utils.ts +++ b/test/src/tests-identity-utils.ts @@ -10,7 +10,8 @@ import { ICachedIdentityCall, hasIdentityRequestChanged, } from "../../src/identity-utils"; -import { LocalStorageVault } from "../../src/vault"; +import { createVault } from "../../src/vault"; +import { VaultKind } from "../../src/constants"; import { Dictionary, generateHash } from "../../src/utils"; import { expect } from 'chai'; import { @@ -37,7 +38,7 @@ const knownIdentities: IKnownIdentities = createKnownIdentities({ DEVICE_ID ); -const cacheVault = new LocalStorageVault(localStorageIDKey); +const cacheVault = createVault>(localStorageIDKey, VaultKind.LocalStorage); const identifyResponse: IdentityResultBody = { context: null, @@ -292,7 +293,7 @@ describe('identity-utils', () => { const mpIdCache = window.localStorage.getItem(localStorageIDKey); expect(mpIdCache).to.equal(null); - const cacheVault = new LocalStorageVault(localStorageIDKey); + const cacheVault = createVault>(localStorageIDKey, VaultKind.LocalStorage); const result = hasValidCachedIdentity('identify', userIdentities, cacheVault); expect(result).to.equal(false); @@ -302,7 +303,7 @@ describe('identity-utils', () => { const mpIdCache = window.localStorage.getItem(localStorageIDKey); expect(mpIdCache).to.equal(null); - const cacheVault = new LocalStorageVault(localStorageIDKey); + const cacheVault = createVault>(localStorageIDKey, VaultKind.LocalStorage); // check to ensure there is nothing on the cache first const result1 = hasValidCachedIdentity('identify', userIdentities, cacheVault); @@ -394,7 +395,7 @@ describe('identity-utils', () => { // set up clock in order to force some time stamps to expire later in the test const clock = sinon.useFakeTimers(); - const cacheVault = new LocalStorageVault(localStorageIDKey); + const cacheVault = createVault>(localStorageIDKey, VaultKind.LocalStorage); const knownIdentities1: IKnownIdentities = createKnownIdentities({ userIdentities: {customerid: 'id1'}}, DEVICE_ID @@ -464,7 +465,7 @@ describe('identity-utils', () => { const mpInstance = window.mParticle.getInstance(); - const cacheVault = new LocalStorageVault(localStorageIDKey); + const cacheVault = createVault>(localStorageIDKey, VaultKind.LocalStorage); const customerId = {customerid: 'id1'} const knownIdentities1: IKnownIdentities = createKnownIdentities({ @@ -512,7 +513,7 @@ describe('identity-utils', () => { window.mParticle.init(apiKey, window.mParticle.config); const mpInstance = window.mParticle.getInstance(); - const cacheVault = new LocalStorageVault(localStorageIDKey); + const cacheVault = createVault>(localStorageIDKey, VaultKind.LocalStorage); const customerId = {customerid: 'id1'} const knownIdentities1: IKnownIdentities = createKnownIdentities({ diff --git a/test/src/vault.spec.ts b/test/src/vault.spec.ts index c9ee53085..4905ff68d 100644 --- a/test/src/vault.spec.ts +++ b/test/src/vault.spec.ts @@ -1,7 +1,8 @@ import { Batch } from '@mparticle/event-models'; import { expect } from 'chai'; import { Dictionary } from '../../src/utils'; -import { SessionStorageVault, LocalStorageVault } from '../../src/vault'; +import { createVault, LocalStorageVault } from '../../src/vault'; +import { VaultKind } from '../../src/constants'; const testObject: Dictionary> = { foo: { foo: 'bar', buzz: 'bazz' }, @@ -26,9 +27,10 @@ describe('Vault', () => { it('should store an object', () => { const storageKey = 'test-key-store-object'; - const vault = new SessionStorageVault< - Dictionary> - >(storageKey); + const vault = createVault>>( + storageKey, + VaultKind.SessionStorage + ); vault.store(testObject); @@ -41,8 +43,9 @@ describe('Vault', () => { it('should store an array', () => { const storageKey = 'test-key-store-array'; - const vault = new SessionStorageVault[]>( + const vault = createVault[]>( storageKey, + VaultKind.SessionStorage ); vault.store(testArray); @@ -56,7 +59,7 @@ describe('Vault', () => { it('should store a string', () => { const storageKey = 'test-key-store-string'; - const vault = new SessionStorageVault(storageKey); + const vault = createVault(storageKey, VaultKind.SessionStorage); vault.store(testString); @@ -69,7 +72,7 @@ describe('Vault', () => { it('should store a number', () => { const storageKey = 'test-key-store-number'; - const vault = new SessionStorageVault(storageKey); + const vault = createVault(storageKey, VaultKind.SessionStorage); vault.store(testNumber); @@ -89,9 +92,10 @@ describe('Vault', () => { JSON.stringify(testObject), ); - const vault = new SessionStorageVault< - Dictionary> - >(storageKey); + const vault = createVault>>( + storageKey, + VaultKind.SessionStorage + ); const retrievedItem = vault.retrieve(); @@ -109,8 +113,9 @@ describe('Vault', () => { JSON.stringify(testArray), ); - const vault = new SessionStorageVault[]>( + const vault = createVault[]>( storageKey, + VaultKind.SessionStorage ); const retrievedItem = vault.retrieve(); @@ -126,7 +131,7 @@ describe('Vault', () => { JSON.stringify(testString), ); - const vault = new SessionStorageVault(storageKey); + const vault = createVault(storageKey, VaultKind.SessionStorage); const retrievedItem = vault.retrieve(); @@ -141,7 +146,7 @@ describe('Vault', () => { JSON.stringify(testNumber), ); - const vault = new SessionStorageVault(storageKey); + const vault = createVault(storageKey, VaultKind.SessionStorage); const retrievedItem = vault.retrieve(); @@ -158,9 +163,10 @@ describe('Vault', () => { JSON.stringify(testObject), ); - const vault = new SessionStorageVault< - Dictionary> - >(storageKey); + const vault = createVault>>( + storageKey, + VaultKind.SessionStorage + ); vault.purge(); @@ -178,8 +184,9 @@ describe('Vault', () => { JSON.stringify(testArray), ); - const vault = new SessionStorageVault[]>( + const vault = createVault[]>( storageKey, + VaultKind.SessionStorage ); vault.purge(); @@ -201,9 +208,10 @@ describe('Vault', () => { it('should store an object', () => { const storageKey = 'test-key-store-object'; - const vault = new LocalStorageVault< - Dictionary> - >(storageKey); + const vault = createVault>>( + storageKey, + VaultKind.LocalStorage + ); vault.store(testObject); @@ -216,8 +224,9 @@ describe('Vault', () => { it('should store an array', () => { const storageKey = 'test-key-store-array'; - const vault = new LocalStorageVault[]>( + const vault = createVault[]>( storageKey, + VaultKind.LocalStorage ); vault.store(testArray); @@ -231,7 +240,7 @@ describe('Vault', () => { it('should store a string', () => { const storageKey = 'test-key-store-array'; - const vault = new LocalStorageVault(storageKey); + const vault = createVault(storageKey, VaultKind.LocalStorage); vault.store(testString); @@ -244,7 +253,7 @@ describe('Vault', () => { it('should store a number', () => { const storageKey = 'test-key-store-array'; - const vault = new LocalStorageVault(storageKey); + const vault = createVault(storageKey, VaultKind.LocalStorage); vault.store(testNumber); @@ -264,9 +273,10 @@ describe('Vault', () => { JSON.stringify(testObject), ); - const vault = new LocalStorageVault< - Dictionary> - >(storageKey); + const vault = createVault>>( + storageKey, + VaultKind.LocalStorage + ); const retrievedItem = vault.retrieve(); @@ -284,8 +294,9 @@ describe('Vault', () => { JSON.stringify(testArray), ); - const vault = new LocalStorageVault[]>( + const vault = createVault[]>( storageKey, + VaultKind.LocalStorage ); const retrievedItem = vault.retrieve(); @@ -398,7 +409,10 @@ describe('Vault', () => { source_request_id: 'source-request-id-5', }; - const vault = new LocalStorageVault[]>(storageKey); + const vault = createVault[]>( + storageKey, + VaultKind.LocalStorage + ); vault.store([batch1, batch2, batch3, batch4, batch5]);