UNPKG

@dodgeball/trust-sdk-client

Version:
853 lines 60.9 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { cancelError, DodgeballApiVersion, DodgeballInvalidConfigError, DodgeballMissingConfigError, IntegrationPurpose, systemError, VerificationOutcome, VerificationStatus, } from "./types"; import { DEFAULT_CONFIG, DEFAULT_VERIFICATION_OPTIONS, DISABLED_SESSION_ID, DISABLED_SOURCE_TOKEN, DODGEBALL_SESSION_KEY, DodgeballSessionMessageType, MIN_TOKEN_REFRESH_INTERVAL_MS, } from "./constants"; import { Logger, LogLevel, Severity } from "./logger"; import { constructApiUrl, getInitializationConfig, queryVerification, setVerificationResponse, } from "./utilities"; import Identifier from "./Identifier"; import IntegrationLoader from "./IntegrationLoader"; import Cookies from "js-cookie"; import cloneDeep from "lodash.clonedeep"; import { v4 as uuidv4 } from "uuid"; var Dodgeball = (function () { function Dodgeball(publicKey, config) { var _this = this; var _a; this.publicKey = ""; this.seenSteps = {}; this.integrationLoader = null; this.integrations = []; this.areIntegrationsLoaded = false; this.onIntegrationsLoaded = []; this.isSourcing = false; this.onSource = []; this.sourceToken = ""; this.sourceTokenExpiry = 0; this.refreshSourceTokenHandle = null; this.sessionHelperIsLoaded = false; this.sessionHelperIframe = null; this.onSessionHelper = []; this.useSessionFallback = false; if (publicKey == null || (publicKey === null || publicKey === void 0 ? void 0 : publicKey.length) === 0) { throw new DodgeballMissingConfigError("publicApiKey", publicKey); } this.publicKey = publicKey; this.config = Object.assign(cloneDeep(DEFAULT_CONFIG), cloneDeep(config || {})); if (Object.keys(DodgeballApiVersion).indexOf(this.config.apiVersion) < 0) { throw new DodgeballInvalidConfigError("config.apiVersion", this.config.apiVersion, Object.keys(DodgeballApiVersion)); } var logLevel = (_a = this.config.logLevel) !== null && _a !== void 0 ? _a : LogLevel.INFO; if (Object.keys(LogLevel).indexOf(logLevel) < 0) { throw new DodgeballInvalidConfigError("config.logLevel", logLevel, Object.keys(LogLevel)); } Logger.filterLevel = Severity[logLevel]; this.identifier = new Identifier({ cookiesEnabled: !this.config.disableCookies, apiUrl: this.config.apiUrl, apiVersion: this.config.apiVersion, publicKey: this.publicKey, }); Logger.trace("Dodgeball constructor called").log(); if (this.config.isEnabled) { setTimeout(function () { return __awaiter(_this, void 0, void 0, function () { var initConfigScript, initConfig, waitForInitializationConfig, integrations, _i, _a, callback, existingSource, observers, userAgentString, isChromeAgent, isSafariAgent, useCookies, sessionHelperIframe, apiUrl; var _this = this; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: initConfigScript = typeof document !== "undefined" ? document.querySelector("script[data-dodgeball]") : null; if (!initConfigScript) return [3, 2]; waitForInitializationConfig = new Promise(function (resolve) { initConfigScript.addEventListener("load", function () { if (typeof window !== "undefined") { if (!window.hasOwnProperty("_dodgeball_init_conf")) { resolve(window._dodgeball_init_conf); } } resolve({ requestId: uuidv4(), libs: [], }); }); }); return [4, waitForInitializationConfig]; case 1: initConfig = _c.sent(); return [3, 4]; case 2: return [4, getInitializationConfig({ url: this.config.apiUrl, token: this.publicKey, version: this.config.apiVersion, })]; case 3: initConfig = _c.sent(); _c.label = 4; case 4: if (!(initConfig === null || initConfig === void 0 ? void 0 : initConfig.hasOwnProperty("libs"))) return [3, 11]; this.integrationLoader = new IntegrationLoader({ requireSrc: initConfig.requireSrc, parentContext: { publicKey: this.publicKey, config: this.config, clearScreen: this.clearScreen.bind(this), }, }); if (!(initConfig && initConfig.libs)) return [3, 6]; return [4, this.integrationLoader.loadIntegrations(initConfig.libs, initConfig.requestId)]; case 5: integrations = _c.sent(); if (integrations) { this.integrations = __spreadArray(__spreadArray([], this.integrations, true), integrations, true); } _c.label = 6; case 6: this.areIntegrationsLoaded = true; if (!(this.onIntegrationsLoaded.length > 0)) return [3, 10]; _i = 0, _a = this.onIntegrationsLoaded; _c.label = 7; case 7: if (!(_i < _a.length)) return [3, 10]; callback = _a[_i]; return [4, callback()]; case 8: _c.sent(); _c.label = 9; case 9: _i++; return [3, 7]; case 10: existingSource = this.identifier.getSource(); if (existingSource) { this.sourceToken = existingSource.token; this.sourceTokenExpiry = (_b = existingSource.expiry) !== null && _b !== void 0 ? _b : 0; this.registerSourceTokenRefresh(); } else { setTimeout(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.generateSourceToken()]; case 1: _a.sent(); return [2]; } }); }); }, 0); } if (this.config.sessionId) { observers = this.integrationLoader.filterIntegrationsByPurpose(this.integrations, IntegrationPurpose.OBSERVE); observers.forEach(function (observer) { observer.observe({ sessionId: _this.config.sessionId, userId: _this.config.userId, sourceToken: _this.sourceToken, }); }); } return [3, 12]; case 11: Logger.error("Error Loading Initialization Configuration.", initConfig).log(); _c.label = 12; case 12: if (typeof document !== "undefined" && this.config.enableCrossDomainSession) { userAgentString = navigator.userAgent; isChromeAgent = userAgentString.indexOf("Chrome") > -1; isSafariAgent = userAgentString.indexOf("Safari") > -1; if (isChromeAgent && isSafariAgent) { isSafariAgent = false; } useCookies = isSafariAgent; if (useCookies) { this.sessionHelperIsLoaded = true; this.useSessionFallback = true; if (this.onSessionHelper.length > 0) { this.onSessionHelper.forEach(function (callback) { callback(); }); this.onSessionHelper = []; } } else { sessionHelperIframe = document.getElementById("_db-sessionUtil"); if (!sessionHelperIframe) { apiUrl = constructApiUrl(this.config.apiUrl, this.config.apiVersion); sessionHelperIframe = document.createElement("iframe"); sessionHelperIframe.setAttribute("id", "_db-sessionUtil"); sessionHelperIframe.setAttribute("src", "".concat(apiUrl, "sessionUtil?publicKey=").concat(this.publicKey)); sessionHelperIframe.setAttribute("style", "display: none;"); sessionHelperIframe.setAttribute("sandbox", "allow-scripts allow-same-origin"); sessionHelperIframe.addEventListener("load", function () { _this.sessionHelperIsLoaded = true; setTimeout(function () { if (_this.onSessionHelper.length > 0) { _this.onSessionHelper.forEach(function (callback) { callback(); }); _this.onSessionHelper = []; } }, 0); }); sessionHelperIframe.addEventListener("error", function () { Logger.error("Error Loading Session Helper. Using fallback").log(); _this.sessionHelperIsLoaded = true; _this.useSessionFallback = true; setTimeout(function () { if (_this.onSessionHelper.length > 0) { _this.onSessionHelper.forEach(function (callback) { callback(); }); _this.onSessionHelper = []; } }, 0); }); document.body.appendChild(sessionHelperIframe); } else { this.sessionHelperIsLoaded = true; if (this.onSessionHelper.length > 0) { this.onSessionHelper.forEach(function (callback) { callback(); }); this.onSessionHelper = []; } } this.sessionHelperIframe = sessionHelperIframe; } } else { this.sessionHelperIsLoaded = true; this.useSessionFallback = true; if (this.onSessionHelper.length > 0) { this.onSessionHelper.forEach(function (callback) { callback(); }); this.onSessionHelper = []; } } return [2]; } }); }); }, 0); } } Dodgeball.prototype.isSourceTokenValid = function () { return this.sourceTokenExpiry > Date.now(); }; Dodgeball.prototype.registerSourceTokenRefresh = function () { var _this = this; if (this.refreshSourceTokenHandle) { clearTimeout(this.refreshSourceTokenHandle); } var nextRefresh = this.sourceTokenExpiry - 60 * 1000 - Date.now(); if (isNaN(nextRefresh) || nextRefresh < MIN_TOKEN_REFRESH_INTERVAL_MS) { nextRefresh = MIN_TOKEN_REFRESH_INTERVAL_MS; } this.refreshSourceTokenHandle = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.generateSourceToken()]; case 1: _a.sent(); return [2]; } }); }); }, nextRefresh); }; Dodgeball.prototype.sendSessionUtilMessage = function (message) { var _this = this; var sendMessage = function () { var _a, _b; if (_this.useSessionFallback) { switch (message.type) { case DodgeballSessionMessageType.GET_SESSION: var sessionId = null; if (!_this.config.disableCookies) { var sessionCookie = Cookies.get(DODGEBALL_SESSION_KEY); if (sessionCookie) { try { var parsedSessionCookie = JSON.parse(sessionCookie); if (parsedSessionCookie.expiry > Date.now()) { sessionId = parsedSessionCookie.sessionId; } else { Cookies.remove(DODGEBALL_SESSION_KEY); } } catch (e) { Cookies.remove(DODGEBALL_SESSION_KEY); } } } else { sessionId = window.localStorage.getItem(message.key); } window.postMessage({ type: "_DB_GET_RESPONSE", key: message.key, value: sessionId, }, window.origin); break; case DodgeballSessionMessageType.SET_SESSION: if (!_this.config.disableCookies) { var hostnameParts = window.location.hostname .split(".") .slice(-2); Cookies.set(DODGEBALL_SESSION_KEY, message.value, { domain: hostnameParts.join("."), expires: 365, }); } else { window.localStorage.setItem(message.key, message.value); } break; case DodgeballSessionMessageType.CLEAR_SESSION: if (!_this.config.disableCookies) { Cookies.remove(DODGEBALL_SESSION_KEY); } else { window.localStorage.removeItem(message.key); } break; } } else { (_b = (_a = _this.sessionHelperIframe) === null || _a === void 0 ? void 0 : _a.contentWindow) === null || _b === void 0 ? void 0 : _b.postMessage(message, _this.config.apiUrl); } }; if (!this.sessionHelperIsLoaded) { this.onSessionHelper.push(sendMessage); } else { sendMessage(); } }; Dodgeball.prototype.generateSourceToken = function () { return __awaiter(this, void 0, void 0, function () { var getSourceToken; var _this = this; return __generator(this, function (_a) { getSourceToken = function () { return __awaiter(_this, void 0, void 0, function () { var identifiers, newSource; return __generator(this, function (_a) { switch (_a.label) { case 0: identifiers = this.integrationLoader.filterIntegrationsByPurpose(this.integrations, IntegrationPurpose.IDENTIFY); return [4, this.identifier.generateSourceToken(identifiers)]; case 1: newSource = _a.sent(); this.sourceToken = newSource.token; this.sourceTokenExpiry = newSource.expiry; this.registerSourceTokenRefresh(); this.isSourcing = false; if (this.onSource.length > 0) { this.onSource.forEach(function (callback) { callback(); }); this.onSource = []; } return [2]; } }); }); }; return [2, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { var sourceToken, e_1; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 6, , 7]); if (!!this.isSourcing) return [3, 4]; this.isSourcing = true; if (!this.areIntegrationsLoaded) return [3, 2]; return [4, getSourceToken()]; case 1: sourceToken = _a.sent(); resolve(sourceToken); return [3, 3]; case 2: this.onIntegrationsLoaded.push(function () { return __awaiter(_this, void 0, void 0, function () { var sourceToken; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, getSourceToken()]; case 1: sourceToken = _a.sent(); resolve(sourceToken); return [2]; } }); }); }); _a.label = 3; case 3: return [3, 5]; case 4: this.onSource.push(function () { resolve(_this.sourceToken); }); _a.label = 5; case 5: return [3, 7]; case 6: e_1 = _a.sent(); Logger.error("Error Generating Source Token", e_1).log(); this.isSourcing = false; reject(e_1); return [3, 7]; case 7: return [2]; } }); }); })]; }); }); }; Dodgeball.prototype.filterSeenSteps = function (steps) { var _this = this; return steps.filter(function (step) { return !_this.seenSteps[step.id]; }); }; Dodgeball.prototype.handleVerificationStep = function (verification, step, context, shouldContinuePolling) { return __awaiter(this, void 0, void 0, function () { var integration, sourceToken, sourceToken_1, error_1; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: this.seenSteps[step.id] = step; if (!step.name) return [3, 8]; Logger.trace("Handle Verification Step - About to load:", { step: step, }).log(); _a.label = 1; case 1: _a.trys.push([1, 7, , 8]); return [4, this.integrationLoader.loadIntegration(__assign({}, step), step.id)]; case 2: integration = (_a.sent()); if (this.integrations.indexOf(integration) < 0) { this.integrations.push(integration); } delete step.content; Logger.info("Handle Verification Step - Loaded integration", { integration: integration.name, }).log(); if (!(integration.purposes.includes(IntegrationPurpose.OBSERVE) && this.config.sessionId)) return [3, 4]; return [4, this.getSourceToken()]; case 3: sourceToken = _a.sent(); integration.observe({ sessionId: this.config.sessionId, userId: this.config.userId, sourceToken: sourceToken, }); shouldContinuePolling(); _a.label = 4; case 4: if (integration.purposes.includes(IntegrationPurpose.IDENTIFY)) { integration.identify(); shouldContinuePolling(); } if (integration.purposes.includes(IntegrationPurpose.QUALIFY)) { integration.qualify(context); shouldContinuePolling(); } if (!integration.purposes.includes(IntegrationPurpose.EXECUTE)) return [3, 6]; return [4, this.getSourceToken()]; case 5: sourceToken_1 = _a.sent(); integration.execute(step, context, function (response, sendResponse) { if (sendResponse === void 0) { sendResponse = true; } shouldContinuePolling(); if (sendResponse) { return setVerificationResponse(_this.config.apiUrl, _this.publicKey, sourceToken_1, _this.config.apiVersion, verification, step.verificationStepId, response); } else { return Promise.resolve(null); } }, function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!context.onError) return [3, 2]; return [4, context.onError(cancelError())]; case 1: _a.sent(); _a.label = 2; case 2: return [2]; } }); }); }); _a.label = 6; case 6: return [3, 8]; case 7: error_1 = _a.sent(); Logger.error("Handle Verification Step - Could not process step:", error_1) .setParameters({ step: step }) .log(); return [3, 8]; case 8: return [2]; } }); }); }; Dodgeball.prototype.verifyTimeDelta = function (startDate, options) { var maxDelta = options === null || options === void 0 ? void 0 : options.maxDuration; var toReturn = maxDelta ? Date.now() - startDate.valueOf() < maxDelta : true; return toReturn; }; Dodgeball.prototype.handleVerificationOutcome = function (verification, context, options) { var _this = this; (function () { return __awaiter(_this, void 0, void 0, function () { var isTerminal, numIterations, isFirstIteration, startTime, currentPollingInterval_1, getRandomIntInclusive, _loop_1, this_1; var _this = this; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: Logger.trace("Handle Verification Outcome - Called", { verification: verification, }).log(); if (!(verification === null || verification === void 0 ? void 0 : verification.id)) { Logger.error("Handle Verification Outcome - Verification has no id").log(); } if (!this.config.isEnabled) return [3, 4]; isTerminal = false; numIterations = 0; isFirstIteration = true; startTime = new Date(); currentPollingInterval_1 = options.pollingInterval; getRandomIntInclusive = function (min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1) + min); }; _loop_1 = function () { var temp, response, e_2, verificationSteps_1, executors, _i, executors_1, executor; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!(numIterations > 0)) return [3, 5]; return [4, new Promise(function (resolve) { return setTimeout(resolve, currentPollingInterval_1); })]; case 1: _c.sent(); if (numIterations > options.numAtInitialPollingInterval) { temp = Math.min(options.maxPollingInterval, options.pollingInterval * Math.pow(2, Math.max(0, numIterations - options.numAtInitialPollingInterval))); currentPollingInterval_1 = temp / 2 + getRandomIntInclusive(0, temp / 2); } _c.label = 2; case 2: _c.trys.push([2, 4, , 5]); Logger.trace("Handle Verification Outcome - Querying Verification Status").log(); return [4, queryVerification(this_1.config.apiUrl, this_1.publicKey, this_1.config.apiVersion, verification)]; case 3: response = _c.sent(); verification = response.verification; return [3, 5]; case 4: e_2 = _c.sent(); Logger.error("Error Querying Verification Status.", e_2).log(); return [3, 5]; case 5: isTerminal = !this_1.isRunning(verification); numIterations += 1; if (!!isTerminal) return [3, 8]; verificationSteps_1 = this_1.filterSeenSteps((_a = verification.nextSteps) !== null && _a !== void 0 ? _a : []); _c.label = 6; case 6: if (!(verificationSteps_1 && verificationSteps_1.length > 0)) return [3, 8]; return [4, new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.handleVerificationStep(verification, verificationSteps_1[0], context, resolve)]; case 1: _a.sent(); verificationSteps_1 = this.filterSeenSteps(verificationSteps_1); return [2]; } }); }); })]; case 7: _c.sent(); numIterations = 1; currentPollingInterval_1 = options.pollingInterval; startTime = new Date(); return [3, 6]; case 8: if (!this_1.isAllowed(verification)) return [3, 18]; if (!isFirstIteration) return [3, 11]; if (!context.onApproved) return [3, 10]; return [4, context.onApproved(verification)]; case 9: _c.sent(); _c.label = 10; case 10: return [3, 17]; case 11: executors = this_1.integrationLoader.filterIntegrationsByPurpose(this_1.integrations, IntegrationPurpose.EXECUTE); _i = 0, executors_1 = executors; _c.label = 12; case 12: if (!(_i < executors_1.length)) return [3, 15]; executor = executors_1[_i]; return [4, executor.cleanup()]; case 13: _c.sent(); _c.label = 14; case 14: _i++; return [3, 12]; case 15: if (!context.onVerified) return [3, 17]; return [4, context.onVerified(verification)]; case 16: _c.sent(); _c.label = 17; case 17: return [3, 34]; case 18: if (!this_1.isDenied(verification)) return [3, 21]; this_1.clearScreen(); if (!context.onDenied) return [3, 20]; return [4, context.onDenied(verification)]; case 19: _c.sent(); _c.label = 20; case 20: return [3, 34]; case 21: if (!this_1.isRunning(verification)) return [3, 27]; if (!(verification.status === VerificationStatus.BLOCKED)) return [3, 24]; if (!context.onBlocked) return [3, 23]; return [4, context.onBlocked(verification)]; case 22: _c.sent(); _c.label = 23; case 23: return [3, 26]; case 24: if (!context.onPending) return [3, 26]; return [4, context.onPending(verification)]; case 25: _c.sent(); _c.label = 26; case 26: return [3, 34]; case 27: if (!this_1.isUndecided(verification)) return [3, 30]; if (!context.onUndecided) return [3, 29]; return [4, context.onUndecided(verification)]; case 28: _c.sent(); _c.label = 29; case 29: return [3, 34]; case 30: if (!this_1.hasError(verification)) return [3, 33]; this_1.clearScreen(); if (!context.onError) return [3, 32]; return [4, context.onError(systemError(verification.error))]; case 31: _c.sent(); _c.label = 32; case 32: return [3, 34]; case 33: if (!this_1.isCancelled(verification)) { if (verification === null || verification === void 0 ? void 0 : verification.status) { Logger.error("Unknown Verification State:\nStatus:".concat(verification === null || verification === void 0 ? void 0 : verification.status, "\nOutcome:").concat(verification === null || verification === void 0 ? void 0 : verification.outcome)).log(); } else { Logger.error("Error Retrieving Verification:", verification).log(); } } _c.label = 34; case 34: isFirstIteration = false; return [2]; } }); }; this_1 = this; _b.label = 1; case 1: if (!(!isTerminal && (numIterations == 0 || this.verifyTimeDelta(startTime, options)))) return [3, 3]; return [5, _loop_1()]; case 2: _b.sent(); return [3, 1]; case 3: return [2]; case 4: if (!context.onApproved) return [3, 6]; return [4, context.onApproved(verification)]; case 5: _b.sent(); _b.label = 6; case 6: return [2]; } }); }); })(); }; Dodgeball.prototype.clearScreen = function () { var executors = this.integrationLoader.filterIntegrationsByPurpose(this.integrations, IntegrationPurpose.EXECUTE); for (var _i = 0, executors_2 = executors; _i < executors_2.length; _i++) { var executor = executors_2[_i]; if (Object.getPrototypeOf(executor).constructor.removeModal) { Object.getPrototypeOf(executor).constructor.removeModal(); } } }; Dodgeball.prototype.track = function (sessionId, userId) { var _this = this; try { this.config.sessionId = sessionId; this.config.userId = userId; if (this.config.isEnabled) { if (sessionId) { var updateObservers = function () { (function () { return __awaiter(_this, void 0, void 0, function () { var sourceToken, observers; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.getSourceToken()]; case 1: sourceToken = _a.sent(); observers = this.integrationLoader.filterIntegrationsByPurpose(this.integrations, IntegrationPurpose.OBSERVE); observers.forEach(function (observer) { observer.observe({ sessionId: sessionId, userId: userId, sourceToken: sourceToken }); }); return [2]; } }); }); })(); }; if (this.areIntegrationsLoaded) { updateObservers(); } else { this.onIntegrationsLoaded.push(updateObservers); } } } } catch (e) { Logger.error("Error Updating Observers", e).log(); } return; }; Dodgeball.prototype.attachIdentifierMetadata = function (sourceToken) { return __awaiter(this, void 0, void 0, function () { var identifiers; return __generator(this, function (_a) { try { identifiers = this.integrationLoader.filterIntegrationsByPurpose(this.integrations, IntegrationPurpose.IDENTIFY); return [2, this.identifier.saveSourceTokenMetadata(sourceToken, identifiers)]; } catch (error) { Logger.error("Error Attaching Metadata", error).log(); return [2, Promise.reject(error)]; } return [2]; }); }); }; Dodgeball.prototype.getSourceToken = function (onSource) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { try { return [2, new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.config.isEnabled) return [3, 4]; if (!this.isSourceTokenValid()) return [3, 2]; return [4, this.attachIdentifierMetadata(this.sourceToken)]; case 1: _a.sent(); if (onSource) { onSource(this.sourceToken); } resolve(this.sourceToken); return [3, 3]; case 2: if (onSource) { this.onSource.push((function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.attachIdentifierMetadata(this.sourceToken)]; case 1: _a.sent(); onSource(this.sourceToken); resolve(this.sourceToken); return [2]; } }); }); }).bind(this)); } else { this.onSource.push((function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.attachIdentifierMetadata(this.sourceToken)]; case 1: _a.sent(); resolve(thi