Skip to content

Commit

Permalink
Cache full xhr responseText and status
Browse files Browse the repository at this point in the history
  • Loading branch information
rmi22186 committed Dec 18, 2023
1 parent 8cdfa90 commit 0ffa864
Show file tree
Hide file tree
Showing 10 changed files with 607 additions and 70 deletions.
325 changes: 319 additions & 6 deletions src/identity-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ import { LocalStorageVault } from './vault';
import Types from './types';
import { IdentityApiData, UserIdentities } from '@mparticle/web-sdk';
import { isObject } from './utils';
import { MParticleWebSDK } from './sdkRuntimeModels';

export interface IKnownIdentities extends UserIdentities {
device_application_stamp?: string;
}

export interface ICachedIdentityCall extends UserIdentities {
responseText: string;
status: number;
expireTimestamp: number;
}

export const cacheIdentityRequest = (
method: string,
identities: IKnownIdentities,
mpid: string,
expireTimestamp: number,
idCache: LocalStorageVault<Dictionary>
idCache: LocalStorageVault<Dictionary>,
xhr: XMLHttpRequest
): void => {
let cache = idCache.retrieve() || {};

let cache: Dictionary<ICachedIdentityCall> = idCache.retrieve() || {};
let cacheKey = concatenateIdentities(method, identities);

cache[cacheKey] = { mpid, expireTimestamp };
cache[cacheKey] = { responseText: xhr.responseText, status: xhr.status, expireTimestamp};
idCache.store(cache);
};

Expand All @@ -33,7 +39,7 @@ export const concatenateIdentities = (
// set DAS first since it is not an official identity type
let cacheKey: string = `${method}:device_application_stamp=${userIdentities.device_application_stamp};`;
const idLength: number = Object.keys(userIdentities).length;
let concatenatedIdentities: string;
let concatenatedIdentities: string = '';

if (idLength) {
let userIDArray: Array<string> = new Array(idLength);
Expand Down Expand Up @@ -83,6 +89,22 @@ export const shouldCallIdentity = (
return true;
};

export const getCachedIdentity = (
method: string,
proposedUserIdentities: IKnownIdentities,
idCache: LocalStorageVault<Dictionary>
): Dictionary<string | number | boolean> | null => {
const cacheKey: string = concatenateIdentities(
method,
proposedUserIdentities
);

const cache = idCache.retrieve();
const cachedIdentity = cache ? cache[cacheKey] : null;

return cachedIdentity;
};

export const createKnownIdentities = (
identityApiData: IdentityApiData,
deviceId: string
Expand All @@ -103,3 +125,294 @@ export const createKnownIdentities = (

return identitiesResult;
};

// export const parseCachedIdentityResponse = (
// cachedIdentity: ICachedIdentityCall,
// previousMPID: string,
// callback,
// // identityApiData,
// // method,
// // knownIdentities,
// mpInstance: MParticleWebSDK
// ) {
// var prevUser = mpInstance.Identity.getUser(previousMPID),
// newUser,
// mpidIsNotInCookies,
// identityApiResult,
// indexOfMPID,
// newIdentitiesByType = {},
// previousUIByName = prevUser
// ? prevUser.getUserIdentities().userIdentities
// : {},
// previousUIByNameCopy = mpInstance._Helpers.extend(
// {},
// previousUIByName
// );
// // mpInstance._Store.identityCallInFlight = false;

// mpInstance._Store.isLoggedIn = cachedIdentity.isLoggedIn;

// // set currentUser
// if (
// !prevUser ||
// (prevUser.getMPID() &&
// identityApiResult.mpid &&
// identityApiResult.mpid !== prevUser.getMPID())
// ) {
// mpInstance._Store.mpid = identityApiResult.mpid;

// if (prevUser) {
// mpInstance._Persistence.setLastSeenTime(previousMPID);
// }

// if (
// !mpInstance._Persistence.getFirstSeenTime(
// identityApiResult.mpid
// )
// )
// mpidIsNotInCookies = true;
// mpInstance._Persistence.setFirstSeenTime(
// identityApiResult.mpid
// );
// }

// if (xhr.status === 200) {
// // const CACHE_HEADER = 'X-MP-Max-Age';
// // const idCacheTimeout = xhr.getAllResponseHeaders();
// // magic code to get CACHE_HEADER
// const oneDayInMS = 86400 * 60 * 60 * 24;
// const timeout = new Date().getTime() + oneDayInMS;

// if (method === 'login' || method === 'identify') {
// cacheIdentityRequest(
// method,
// knownIdentities,
// identityApiResult.mpid,
// timeout,
// self.idCache
// );
// }

// if (method === 'modify') {
// newIdentitiesByType = mpInstance._Identity.IdentityRequest.combineUserIdentities(
// previousUIByName,
// identityApiData.userIdentities
// );

// mpInstance._Persistence.saveUserIdentitiesToPersistence(
// previousMPID,
// newIdentitiesByType
// );
// } else {
// var incomingUser = self.IdentityAPI.getUser(
// identityApiResult.mpid
// );

// var incomingMpidUIByName = incomingUser
// ? incomingUser.getUserIdentities().userIdentities
// : {};

// var incomingMpidUIByNameCopy = mpInstance._Helpers.extend(
// {},
// incomingMpidUIByName
// );
// mpInstance.Logger.verbose(
// 'Successfully parsed Identity Response'
// );

// //this covers an edge case where, users stored before "firstSeenTime" was introduced
// //will not have a value for "fst" until the current MPID changes, and in some cases,
// //the current MPID will never change
// if (
// method === 'identify' &&
// prevUser &&
// identityApiResult.mpid === prevUser.getMPID()
// ) {
// mpInstance._Persistence.setFirstSeenTime(
// identityApiResult.mpid
// );
// }

// indexOfMPID = mpInstance._Store.currentSessionMPIDs.indexOf(
// identityApiResult.mpid
// );

// if (
// mpInstance._Store.sessionId &&
// identityApiResult.mpid &&
// previousMPID !== identityApiResult.mpid &&
// indexOfMPID < 0
// ) {
// mpInstance._Store.currentSessionMPIDs.push(
// identityApiResult.mpid
// );
// }

// if (indexOfMPID > -1) {
// mpInstance._Store.currentSessionMPIDs = mpInstance._Store.currentSessionMPIDs
// .slice(0, indexOfMPID)
// .concat(
// mpInstance._Store.currentSessionMPIDs.slice(
// indexOfMPID + 1,
// mpInstance._Store.currentSessionMPIDs
// .length
// )
// );
// mpInstance._Store.currentSessionMPIDs.push(
// identityApiResult.mpid
// );
// }

// mpInstance._CookieSyncManager.attemptCookieSync(
// previousMPID,
// identityApiResult.mpid,
// mpidIsNotInCookies
// );

// self.checkIdentitySwap(
// previousMPID,
// identityApiResult.mpid,
// mpInstance._Store.currentSessionMPIDs
// );

// if (
// identityApiData &&
// identityApiData.userIdentities &&
// Object.keys(identityApiData.userIdentities).length
// ) {
// newIdentitiesByType = self.IdentityRequest.combineUserIdentities(
// incomingMpidUIByName,
// identityApiData.userIdentities
// );
// }

// mpInstance._Persistence.saveUserIdentitiesToPersistence(
// identityApiResult.mpid,
// newIdentitiesByType
// );
// mpInstance._Persistence.update();

// mpInstance._Persistence.findPrevCookiesBasedOnUI(
// identityApiData
// );

// mpInstance._Store.context =
// identityApiResult.context ||
// mpInstance._Store.context;
// }

// newUser = mpInstance.Identity.getCurrentUser();

// if (
// identityApiData &&
// identityApiData.onUserAlias &&
// mpInstance._Helpers.Validators.isFunction(
// identityApiData.onUserAlias
// )
// ) {
// try {
// mpInstance.Logger.warning(
// 'Deprecated function onUserAlias will be removed in future releases'
// );
// identityApiData.onUserAlias(prevUser, newUser);
// } catch (e) {
// mpInstance.Logger.error(
// 'There was an error with your onUserAlias function - ' +
// e
// );
// }
// }
// var persistence = mpInstance._Persistence.getPersistence();

// if (newUser) {
// mpInstance._Persistence.storeDataInMemory(
// persistence,
// newUser.getMPID()
// );
// if (
// !prevUser ||
// newUser.getMPID() !== prevUser.getMPID() ||
// prevUser.isLoggedIn() !== newUser.isLoggedIn()
// ) {
// mpInstance._Forwarders.initForwarders(
// newUser.getUserIdentities().userIdentities,
// mpInstance._APIClient.prepareForwardingStats
// );
// }
// mpInstance._Forwarders.setForwarderUserIdentities(
// newUser.getUserIdentities().userIdentities
// );
// mpInstance._Forwarders.setForwarderOnIdentityComplete(
// newUser,
// method
// );
// mpInstance._Forwarders.setForwarderOnUserIdentified(
// newUser,
// method
// );
// }
// var newIdentitiesByName = {};

// for (var key in newIdentitiesByType) {
// newIdentitiesByName[
// Types.IdentityType.getIdentityName(
// mpInstance._Helpers.parseNumber(key)
// )
// ] = newIdentitiesByType[key];
// }

// self.sendUserIdentityChangeEvent(
// newIdentitiesByName,
// method,
// identityApiResult.mpid,
// method === 'modify'
// ? previousUIByNameCopy
// : incomingMpidUIByNameCopy
// );
// }

// if (callback) {
// if (xhr.status === 0) {
// mpInstance._Helpers.invokeCallback(
// callback,
// HTTPCodes.noHttpCoverage,
// identityApiResult || null,
// newUser
// );
// } else {
// mpInstance._Helpers.invokeCallback(
// callback,
// xhr.status,
// identityApiResult || null,
// newUser
// );
// }
// } else {
// if (
// identityApiResult &&
// identityApiResult.errors &&
// identityApiResult.errors.length
// ) {
// mpInstance.Logger.error(
// 'Received HTTP response code of ' +
// xhr.status +
// ' - ' +
// identityApiResult.errors[0].message
// );
// }
// }

// mpInstance._APIClient.processQueuedEvents();
// } catch (e) {
// if (callback) {
// mpInstance._Helpers.invokeCallback(
// callback,
// xhr.status,
// identityApiResult || null
// );
// }
// mpInstance.Logger.error(
// 'Error parsing JSON response from Identity server: ' + e
// );
// }
// };
Loading

0 comments on commit 0ffa864

Please sign in to comment.