{"version":3,"sources":["node_modules/@datadog/browser-core/esm/tools/display.js","node_modules/@datadog/browser-core/esm/tools/getGlobalObject.js","node_modules/@datadog/browser-core/esm/tools/monitor.js","node_modules/@datadog/browser-core/esm/tools/getZoneJsOriginalValue.js","node_modules/@datadog/browser-core/esm/tools/timer.js","node_modules/@datadog/browser-core/esm/browser/addEventListener.js","node_modules/@datadog/browser-core/esm/tools/utils/byteUtils.js","node_modules/@datadog/browser-core/esm/tools/utils/functionUtils.js","node_modules/@datadog/browser-core/esm/tools/serialisation/jsonStringify.js","node_modules/@datadog/browser-core/esm/tools/serialisation/sanitize.js","node_modules/@datadog/browser-core/esm/tools/stackTrace/computeStackTrace.js","node_modules/@datadog/browser-core/esm/tools/stackTrace/handlingStack.js","node_modules/@datadog/browser-core/esm/domain/error/error.js","node_modules/@datadog/browser-core/esm/tools/utils/objectUtils.js","node_modules/@datadog/browser-core/esm/tools/experimentalFeatures.js","node_modules/@datadog/browser-core/esm/tools/catchUserErrors.js","node_modules/@datadog/browser-core/esm/tools/utils/numberUtils.js","node_modules/@datadog/browser-core/esm/tools/utils/timeUtils.js","node_modules/@datadog/browser-core/esm/tools/observable.js","node_modules/@datadog/browser-core/esm/tools/utils/stringUtils.js","node_modules/@datadog/browser-core/esm/tools/utils/browserDetection.js","node_modules/@datadog/browser-core/esm/browser/cookie.js","node_modules/@datadog/browser-core/esm/domain/session/storeStrategies/sessionStoreStrategy.js","node_modules/@datadog/browser-core/esm/tools/utils/polyfills.js","node_modules/@datadog/browser-core/esm/domain/session/sessionConstants.js","node_modules/@datadog/browser-core/esm/domain/session/sessionStateValidation.js","node_modules/@datadog/browser-core/esm/domain/session/sessionState.js","node_modules/@datadog/browser-core/esm/domain/session/oldCookiesMigration.js","node_modules/@datadog/browser-core/esm/domain/session/storeStrategies/sessionInCookie.js","node_modules/@datadog/browser-core/esm/domain/session/storeStrategies/sessionInLocalStorage.js","node_modules/@datadog/browser-core/esm/domain/session/sessionStoreOperations.js","node_modules/@datadog/browser-core/esm/domain/session/sessionStore.js","node_modules/@datadog/browser-core/esm/domain/trackingConsent.js","node_modules/@datadog/browser-core/esm/tools/utils/urlPolyfill.js","node_modules/@datadog/browser-core/esm/domain/configuration/intakeSites.js","node_modules/@datadog/browser-core/esm/domain/configuration/endpointBuilder.js","node_modules/@datadog/browser-core/esm/domain/configuration/tags.js","node_modules/@datadog/browser-core/esm/domain/configuration/transportConfiguration.js","node_modules/@datadog/browser-core/esm/domain/configuration/configuration.js","node_modules/@datadog/browser-core/esm/tools/sendToExtension.js","node_modules/@datadog/browser-core/esm/tools/utils/typeUtils.js","node_modules/@datadog/browser-core/esm/tools/mergeInto.js","node_modules/@datadog/browser-core/esm/domain/connectivity/connectivity.js","node_modules/@datadog/browser-core/esm/tools/utils/arrayUtils.js","node_modules/@datadog/browser-core/esm/tools/boundedBuffer.js","node_modules/@datadog/browser-core/esm/domain/telemetry/rawTelemetryEvent.types.js","node_modules/@datadog/browser-core/esm/domain/telemetry/telemetry.js","node_modules/@datadog/browser-core/esm/tools/utils/responseUtils.js","node_modules/@datadog/browser-core/esm/domain/error/error.types.js","node_modules/@datadog/browser-core/esm/transport/sendWithRetryStrategy.js","node_modules/@datadog/browser-core/esm/transport/httpRequest.js","node_modules/@datadog/browser-core/esm/transport/eventBridge.js","node_modules/@datadog/browser-core/esm/browser/pageMayExitObservable.js","node_modules/@datadog/browser-core/esm/transport/batch.js","node_modules/@datadog/browser-core/esm/transport/flushController.js","node_modules/@datadog/browser-core/esm/transport/startBatchWithReplica.js","node_modules/@datadog/browser-core/esm/tools/instrumentMethod.js","node_modules/@datadog/browser-core/esm/domain/error/trackRuntimeError.js","node_modules/@datadog/browser-core/esm/boot/init.js","node_modules/@datadog/browser-core/esm/boot/displayAlreadyInitializedError.js","node_modules/@datadog/browser-core/esm/domain/report/reportObservable.js","node_modules/@datadog/browser-core/esm/tools/valueHistory.js","node_modules/@datadog/browser-core/esm/domain/session/sessionManager.js","node_modules/@datadog/browser-core/esm/tools/encoder.js","node_modules/@datadog/browser-core/esm/tools/abstractLifeCycle.js","node_modules/@datadog/browser-core/esm/domain/eventRateLimiter/createEventRateLimiter.js","node_modules/@datadog/browser-core/esm/browser/runOnReadyState.js","node_modules/@datadog/browser-core/esm/browser/xhrObservable.js","node_modules/@datadog/browser-core/esm/browser/fetchObservable.js","node_modules/@datadog/browser-core/esm/tools/requestIdleCallback.js","node_modules/@datadog/browser-core/esm/tools/taskQueue.js","node_modules/@datadog/browser-core/esm/domain/console/consoleObservable.js","node_modules/@datadog/browser-core/esm/domain/context/contextUtils.js","node_modules/@datadog/browser-core/esm/domain/context/contextManager.js","node_modules/@datadog/browser-core/esm/domain/context/storeContextManager.js","node_modules/@datadog/browser-core/esm/domain/context/customerDataTracker.js","node_modules/@datadog/browser-core/esm/tools/readBytesFromStream.js","node_modules/@datadog/browser-core/esm/domain/synthetics/syntheticsWorkerValues.js","node_modules/@datadog/browser-core/esm/tools/matchOption.js","node_modules/@datadog/browser-rum-core/esm/domain/resource/resourceUtils.js","node_modules/@datadog/browser-rum-core/esm/browser/firstInputPolyfill.js","node_modules/@datadog/browser-rum-core/esm/browser/performanceObservable.js","node_modules/@datadog/browser-rum-core/esm/domain/contexts/commonContext.js","node_modules/@datadog/browser-rum-core/esm/domain/vital/vitalCollection.js","node_modules/@datadog/browser-rum-core/esm/domain/plugins.js","node_modules/@datadog/browser-rum-core/esm/domain/tracing/identifier.js","node_modules/@datadog/browser-rum-core/esm/domain/tracing/sampler.js","node_modules/@datadog/browser-rum-core/esm/domain/tracing/encodedContext.js","node_modules/@datadog/browser-rum-core/esm/domain/tracing/tracer.js","node_modules/@datadog/browser-rum-core/esm/domain/configuration/configuration.js","node_modules/@datadog/browser-rum-core/esm/domain/configuration/remoteConfiguration.js","node_modules/@datadog/browser-rum-core/esm/boot/preStartRum.js","node_modules/@datadog/browser-rum-core/esm/boot/rumPublicApi.js","node_modules/@datadog/browser-rum-core/esm/browser/domMutationObservable.js","node_modules/@datadog/browser-rum-core/esm/browser/windowOpenObservable.js","node_modules/@datadog/browser-rum-core/esm/domain/limitModification.js","node_modules/@datadog/browser-rum-core/esm/domain/assembly.js","node_modules/@datadog/browser-rum-core/esm/domain/contexts/internalContext.js","node_modules/@datadog/browser-rum-core/esm/domain/lifeCycle.js","node_modules/@datadog/browser-rum-core/esm/domain/contexts/viewHistory.js","node_modules/@datadog/browser-rum-core/esm/domain/requestCollection.js","node_modules/@datadog/browser-rum-core/esm/domain/discardNegativeDuration.js","node_modules/@datadog/browser-rum-core/esm/domain/trackEventCounts.js","node_modules/@datadog/browser-rum-core/esm/domain/waitPageActivityEnd.js","node_modules/@datadog/browser-rum-core/esm/browser/htmlDomUtils.js","node_modules/@datadog/browser-rum-core/esm/domain/privacy.js","node_modules/@datadog/browser-rum-core/esm/domain/action/getActionNameFromElement.js","node_modules/@datadog/browser-rum-core/esm/domain/getSelectorFromElement.js","node_modules/@datadog/browser-rum-core/esm/domain/action/clickChain.js","node_modules/@datadog/browser-rum-core/esm/domain/action/listenActionEvents.js","node_modules/@datadog/browser-rum-core/esm/domain/action/computeFrustration.js","node_modules/@datadog/browser-rum-core/esm/domain/action/interactionSelectorCache.js","node_modules/@datadog/browser-rum-core/esm/domain/action/trackClickActions.js","node_modules/@datadog/browser-rum-core/esm/domain/action/actionCollection.js","node_modules/@datadog/browser-rum-core/esm/domain/error/trackConsoleError.js","node_modules/@datadog/browser-rum-core/esm/domain/error/trackReportError.js","node_modules/@datadog/browser-rum-core/esm/domain/error/errorCollection.js","node_modules/@datadog/browser-rum-core/esm/domain/resource/matchRequestResourceEntry.js","node_modules/@datadog/browser-rum-core/esm/domain/tracing/getDocumentTraceId.js","node_modules/@datadog/browser-rum-core/esm/browser/performanceUtils.js","node_modules/@datadog/browser-rum-core/esm/domain/resource/retrieveInitialDocumentResourceTiming.js","node_modules/@datadog/browser-rum-core/esm/domain/resource/resourceCollection.js","node_modules/@datadog/browser-rum-core/esm/domain/view/trackViewEventCounts.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackFirstContentfulPaint.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackFirstInput.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackNavigationTimings.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackLargestContentfulPaint.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackFirstHidden.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackInitialViewMetrics.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackCumulativeLayoutShift.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/interactionCountPolyfill.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackInteractionToNextPaint.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackLoadingTime.js","node_modules/@datadog/browser-rum-core/esm/browser/scroll.js","node_modules/@datadog/browser-rum-core/esm/browser/viewportObservable.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackScrollMetrics.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewMetrics/trackCommonViewMetrics.js","node_modules/@datadog/browser-rum-core/esm/domain/view/trackViews.js","node_modules/@datadog/browser-rum-core/esm/domain/view/viewCollection.js","node_modules/@datadog/browser-rum-core/esm/domain/rumSessionManager.js","node_modules/@datadog/browser-rum-core/esm/transport/startRumBatch.js","node_modules/@datadog/browser-rum-core/esm/transport/startRumEventBridge.js","node_modules/@datadog/browser-rum-core/esm/domain/contexts/urlContexts.js","node_modules/@datadog/browser-rum-core/esm/browser/locationChangeObservable.js","node_modules/@datadog/browser-rum-core/esm/domain/contexts/featureFlagContext.js","node_modules/@datadog/browser-rum-core/esm/domain/startCustomerDataTelemetry.js","node_modules/@datadog/browser-rum-core/esm/domain/contexts/pageStateHistory.js","node_modules/@datadog/browser-rum-core/esm/domain/contexts/displayContext.js","node_modules/@datadog/browser-rum-core/esm/browser/cookieObservable.js","node_modules/@datadog/browser-rum-core/esm/domain/contexts/ciVisibilityContext.js","node_modules/@datadog/browser-rum-core/esm/domain/longAnimationFrame/longAnimationFrameCollection.js","node_modules/@datadog/browser-rum-core/esm/domain/longTask/longTaskCollection.js","node_modules/@datadog/browser-rum-core/esm/hooks.js","node_modules/@datadog/browser-rum-core/esm/domain/contexts/syntheticsContext.js","node_modules/@datadog/browser-rum-core/esm/boot/startRum.js","node_modules/@datadog/browser-rum-core/esm/domain/getSessionReplayUrl.js"],"sourcesContent":["/* eslint-disable local-rules/disallow-side-effects */\n/**\n * Keep references on console methods to avoid triggering patched behaviors\n *\n * NB: in some setup, console could already be patched by another SDK.\n * In this case, some display messages can be sent by the other SDK\n * but we should be safe from infinite loop nonetheless.\n */\nexport const ConsoleApiName = {\n log: 'log',\n debug: 'debug',\n info: 'info',\n warn: 'warn',\n error: 'error'\n};\n/**\n * When building JS bundles, some users might use a plugin[1] or configuration[2] to remove\n * \"console.*\" references. This causes some issue as we expect `console.*` to be defined.\n * As a workaround, let's use a variable alias, so those expressions won't be taken into account by\n * simple static analysis.\n *\n * [1]: https://babeljs.io/docs/babel-plugin-transform-remove-console/\n * [2]: https://github.com/terser/terser#compress-options (look for drop_console)\n */\nexport const globalConsole = console;\nexport const originalConsoleMethods = {};\nObject.keys(ConsoleApiName).forEach(name => {\n originalConsoleMethods[name] = globalConsole[name];\n});\nconst PREFIX = 'Datadog Browser SDK:';\nexport const display = {\n debug: originalConsoleMethods.debug.bind(globalConsole, PREFIX),\n log: originalConsoleMethods.log.bind(globalConsole, PREFIX),\n info: originalConsoleMethods.info.bind(globalConsole, PREFIX),\n warn: originalConsoleMethods.warn.bind(globalConsole, PREFIX),\n error: originalConsoleMethods.error.bind(globalConsole, PREFIX)\n};\nexport const DOCS_ORIGIN = 'https://docs.datadoghq.com';\nexport const DOCS_TROUBLESHOOTING = `${DOCS_ORIGIN}/real_user_monitoring/browser/troubleshooting`;\nexport const MORE_DETAILS = 'More details:';\n","/**\n * inspired by https://mathiasbynens.be/notes/globalthis\n */\nexport function getGlobalObject() {\n if (typeof globalThis === 'object') {\n return globalThis;\n }\n Object.defineProperty(Object.prototype, '_dd_temp_', {\n get() {\n return this;\n },\n configurable: true\n });\n // @ts-ignore _dd_temp is defined using defineProperty\n let globalObject = _dd_temp_;\n // @ts-ignore _dd_temp is defined using defineProperty\n delete Object.prototype._dd_temp_;\n if (typeof globalObject !== 'object') {\n // on safari _dd_temp_ is available on window but not globally\n // fallback on other browser globals check\n if (typeof self === 'object') {\n globalObject = self;\n } else if (typeof window === 'object') {\n globalObject = window;\n } else {\n globalObject = {};\n }\n }\n return globalObject;\n}\n","import { display } from './display';\nlet onMonitorErrorCollected;\nlet debugMode = false;\nexport function startMonitorErrorCollection(newOnMonitorErrorCollected) {\n onMonitorErrorCollected = newOnMonitorErrorCollected;\n}\nexport function setDebugMode(newDebugMode) {\n debugMode = newDebugMode;\n}\nexport function resetMonitor() {\n onMonitorErrorCollected = undefined;\n debugMode = false;\n}\nexport function monitored(_, __, descriptor) {\n const originalMethod = descriptor.value;\n descriptor.value = function (...args) {\n const decorated = onMonitorErrorCollected ? monitor(originalMethod) : originalMethod;\n return decorated.apply(this, args);\n };\n}\nexport function monitor(fn) {\n return function () {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return callMonitored(fn, this, arguments);\n }; // consider output type has input type\n}\nexport function callMonitored(fn, context, args) {\n try {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return fn.apply(context, args);\n } catch (e) {\n monitorError(e);\n }\n}\nexport function monitorError(e) {\n displayIfDebugEnabled(e);\n if (onMonitorErrorCollected) {\n try {\n onMonitorErrorCollected(e);\n } catch (e) {\n displayIfDebugEnabled(e);\n }\n }\n}\nexport function displayIfDebugEnabled(...args) {\n if (debugMode) {\n display.error('[MONITOR]', ...args);\n }\n}\n","import { getGlobalObject } from './getGlobalObject';\n/**\n * Gets the original value for a DOM API that was potentially patched by Zone.js.\n *\n * Zone.js[1] is a library that patches a bunch of JS and DOM APIs. It usually stores the original\n * value of the patched functions/constructors/methods in a hidden property prefixed by\n * __zone_symbol__.\n *\n * In multiple occasions, we observed that Zone.js is the culprit of important issues leading to\n * browser resource exhaustion (memory leak, high CPU usage). This method is used as a workaround to\n * use the original DOM API instead of the one patched by Zone.js.\n *\n * [1]: https://github.com/angular/angular/tree/main/packages/zone.js\n */\nexport function getZoneJsOriginalValue(target, name) {\n const browserWindow = getGlobalObject();\n let original;\n if (browserWindow.Zone && typeof browserWindow.Zone.__symbol__ === 'function') {\n original = target[browserWindow.Zone.__symbol__(name)];\n }\n if (!original) {\n original = target[name];\n }\n return original;\n}\n","import { getZoneJsOriginalValue } from './getZoneJsOriginalValue';\nimport { monitor } from './monitor';\nimport { getGlobalObject } from './getGlobalObject';\nexport function setTimeout(callback, delay) {\n return getZoneJsOriginalValue(getGlobalObject(), 'setTimeout')(monitor(callback), delay);\n}\nexport function clearTimeout(timeoutId) {\n getZoneJsOriginalValue(getGlobalObject(), 'clearTimeout')(timeoutId);\n}\nexport function setInterval(callback, delay) {\n return getZoneJsOriginalValue(getGlobalObject(), 'setInterval')(monitor(callback), delay);\n}\nexport function clearInterval(timeoutId) {\n getZoneJsOriginalValue(getGlobalObject(), 'clearInterval')(timeoutId);\n}\n","import { monitor } from '../tools/monitor';\nimport { getZoneJsOriginalValue } from '../tools/getZoneJsOriginalValue';\n/**\n * Add an event listener to an event target object (Window, Element, mock object...). This provides\n * a few conveniences compared to using `element.addEventListener` directly:\n *\n * * supports IE11 by: using an option object only if needed and emulating the `once` option\n *\n * * wraps the listener with a `monitor` function\n *\n * * returns a `stop` function to remove the listener\n */\nexport function addEventListener(configuration, eventTarget, eventName, listener, options) {\n return addEventListeners(configuration, eventTarget, [eventName], listener, options);\n}\n/**\n * Add event listeners to an event target object (Window, Element, mock object...). This provides\n * a few conveniences compared to using `element.addEventListener` directly:\n *\n * * supports IE11 by: using an option object only if needed and emulating the `once` option\n *\n * * wraps the listener with a `monitor` function\n *\n * * returns a `stop` function to remove the listener\n *\n * * with `once: true`, the listener will be called at most once, even if different events are listened\n */\nexport function addEventListeners(configuration, eventTarget, eventNames, listener, {\n once,\n capture,\n passive\n} = {}) {\n const listenerWithMonitor = monitor(event => {\n if (!event.isTrusted && !event.__ddIsTrusted && !configuration.allowUntrustedEvents) {\n return;\n }\n if (once) {\n stop();\n }\n listener(event);\n });\n const options = passive ? {\n capture,\n passive\n } : capture;\n // Use the window.EventTarget.prototype when possible to avoid wrong overrides (e.g: https://github.com/salesforce/lwc/issues/1824)\n const listenerTarget = window.EventTarget && eventTarget instanceof EventTarget ? window.EventTarget.prototype : eventTarget;\n const add = getZoneJsOriginalValue(listenerTarget, 'addEventListener');\n eventNames.forEach(eventName => add.call(eventTarget, eventName, listenerWithMonitor, options));\n function stop() {\n const remove = getZoneJsOriginalValue(listenerTarget, 'removeEventListener');\n eventNames.forEach(eventName => remove.call(eventTarget, eventName, listenerWithMonitor, options));\n }\n return {\n stop\n };\n}\n","export const ONE_KIBI_BYTE = 1024;\nexport const ONE_MEBI_BYTE = 1024 * ONE_KIBI_BYTE;\n// eslint-disable-next-line no-control-regex\nconst HAS_MULTI_BYTES_CHARACTERS = /[^\\u0000-\\u007F]/;\nexport function computeBytesCount(candidate) {\n // Accurate bytes count computations can degrade performances when there is a lot of events to process\n if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {\n return candidate.length;\n }\n if (window.TextEncoder !== undefined) {\n return new TextEncoder().encode(candidate).length;\n }\n return new Blob([candidate]).size;\n}\nexport function concatBuffers(buffers) {\n const length = buffers.reduce((total, buffer) => total + buffer.length, 0);\n const result = new Uint8Array(length);\n let offset = 0;\n for (const buffer of buffers) {\n result.set(buffer, offset);\n offset += buffer.length;\n }\n return result;\n}\n","import { setTimeout, clearTimeout } from '../timer';\n// use lodash API\nexport function throttle(fn, wait, options) {\n const needLeadingExecution = options && options.leading !== undefined ? options.leading : true;\n const needTrailingExecution = options && options.trailing !== undefined ? options.trailing : true;\n let inWaitPeriod = false;\n let pendingExecutionWithParameters;\n let pendingTimeoutId;\n return {\n throttled: (...parameters) => {\n if (inWaitPeriod) {\n pendingExecutionWithParameters = parameters;\n return;\n }\n if (needLeadingExecution) {\n fn(...parameters);\n } else {\n pendingExecutionWithParameters = parameters;\n }\n inWaitPeriod = true;\n pendingTimeoutId = setTimeout(() => {\n if (needTrailingExecution && pendingExecutionWithParameters) {\n fn(...pendingExecutionWithParameters);\n }\n inWaitPeriod = false;\n pendingExecutionWithParameters = undefined;\n }, wait);\n },\n cancel: () => {\n clearTimeout(pendingTimeoutId);\n inWaitPeriod = false;\n pendingExecutionWithParameters = undefined;\n }\n };\n}\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nexport function noop() {}\n","import { noop } from '../utils/functionUtils';\n/**\n * Custom implementation of JSON.stringify that ignores some toJSON methods. We need to do that\n * because some sites badly override toJSON on certain objects. Removing all toJSON methods from\n * nested values would be too costly, so we just detach them from the root value, and native classes\n * used to build JSON values (Array and Object).\n *\n * Note: this still assumes that JSON.stringify is correct.\n */\nexport function jsonStringify(value, replacer, space) {\n if (typeof value !== 'object' || value === null) {\n return JSON.stringify(value);\n }\n // Note: The order matter here. We need to detach toJSON methods on parent classes before their\n // subclasses.\n const restoreObjectPrototypeToJson = detachToJsonMethod(Object.prototype);\n const restoreArrayPrototypeToJson = detachToJsonMethod(Array.prototype);\n const restoreValuePrototypeToJson = detachToJsonMethod(Object.getPrototypeOf(value));\n const restoreValueToJson = detachToJsonMethod(value);\n try {\n return JSON.stringify(value, replacer, space);\n } catch (_a) {\n return '';\n } finally {\n restoreObjectPrototypeToJson();\n restoreArrayPrototypeToJson();\n restoreValuePrototypeToJson();\n restoreValueToJson();\n }\n}\nexport function detachToJsonMethod(value) {\n const object = value;\n const objectToJson = object.toJSON;\n if (objectToJson) {\n delete object.toJSON;\n return () => {\n object.toJSON = objectToJson;\n };\n }\n return noop;\n}\n","import { display } from '../display';\nimport { ONE_KIBI_BYTE } from '../utils/byteUtils';\nimport { detachToJsonMethod } from './jsonStringify';\n// The maximum size of a single event is 256KiB. By default, we ensure that user-provided data\n// going through sanitize fits inside our events, while leaving room for other contexts, metadata, ...\nconst SANITIZE_DEFAULT_MAX_CHARACTER_COUNT = 220 * ONE_KIBI_BYTE;\n// Symbol for the root element of the JSONPath used for visited objects\nconst JSON_PATH_ROOT_ELEMENT = '$';\n// When serializing (using JSON.stringify) a key of an object, { key: 42 } gets wrapped in quotes as \"key\".\n// With the separator (:), we need to add 3 characters to the count.\nconst KEY_DECORATION_LENGTH = 3;\nexport function sanitize(source, maxCharacterCount = SANITIZE_DEFAULT_MAX_CHARACTER_COUNT) {\n // Unbind any toJSON function we may have on [] or {} prototypes\n const restoreObjectPrototypeToJson = detachToJsonMethod(Object.prototype);\n const restoreArrayPrototypeToJson = detachToJsonMethod(Array.prototype);\n // Initial call to sanitizeProcessor - will populate containerQueue if source is an Array or a plain Object\n const containerQueue = [];\n const visitedObjectsWithPath = new WeakMap();\n const sanitizedData = sanitizeProcessor(source, JSON_PATH_ROOT_ELEMENT, undefined, containerQueue, visitedObjectsWithPath);\n const serializedSanitizedData = JSON.stringify(sanitizedData);\n let accumulatedCharacterCount = serializedSanitizedData ? serializedSanitizedData.length : 0;\n if (accumulatedCharacterCount > maxCharacterCount) {\n warnOverCharacterLimit(maxCharacterCount, 'discarded', source);\n return undefined;\n }\n while (containerQueue.length > 0 && accumulatedCharacterCount < maxCharacterCount) {\n const containerToProcess = containerQueue.shift();\n let separatorLength = 0; // 0 for the first element, 1 for subsequent elements\n // Arrays and Objects have to be handled distinctly to ensure\n // we do not pick up non-numerical properties from Arrays\n if (Array.isArray(containerToProcess.source)) {\n for (let key = 0; key < containerToProcess.source.length; key++) {\n const targetData = sanitizeProcessor(containerToProcess.source[key], containerToProcess.path, key, containerQueue, visitedObjectsWithPath);\n if (targetData !== undefined) {\n accumulatedCharacterCount += JSON.stringify(targetData).length;\n } else {\n // When an element of an Array (targetData) is undefined, it is serialized as null:\n // JSON.stringify([undefined]) => '[null]' - This accounts for 4 characters\n accumulatedCharacterCount += 4;\n }\n accumulatedCharacterCount += separatorLength;\n separatorLength = 1;\n if (accumulatedCharacterCount > maxCharacterCount) {\n warnOverCharacterLimit(maxCharacterCount, 'truncated', source);\n break;\n }\n ;\n containerToProcess.target[key] = targetData;\n }\n } else {\n for (const key in containerToProcess.source) {\n if (Object.prototype.hasOwnProperty.call(containerToProcess.source, key)) {\n const targetData = sanitizeProcessor(containerToProcess.source[key], containerToProcess.path, key, containerQueue, visitedObjectsWithPath);\n // When a property of an object has an undefined value, it will be dropped during serialization:\n // JSON.stringify({a:undefined}) => '{}'\n if (targetData !== undefined) {\n accumulatedCharacterCount += JSON.stringify(targetData).length + separatorLength + key.length + KEY_DECORATION_LENGTH;\n separatorLength = 1;\n }\n if (accumulatedCharacterCount > maxCharacterCount) {\n warnOverCharacterLimit(maxCharacterCount, 'truncated', source);\n break;\n }\n ;\n containerToProcess.target[key] = targetData;\n }\n }\n }\n }\n // Rebind detached toJSON functions\n restoreObjectPrototypeToJson();\n restoreArrayPrototypeToJson();\n return sanitizedData;\n}\n/**\n * Internal function to factorize the process common to the\n * initial call to sanitize, and iterations for Arrays and Objects\n *\n */\nfunction sanitizeProcessor(source, parentPath, key, queue, visitedObjectsWithPath) {\n // Start by handling toJSON, as we want to sanitize its output\n const sourceToSanitize = tryToApplyToJSON(source);\n if (!sourceToSanitize || typeof sourceToSanitize !== 'object') {\n return sanitizePrimitivesAndFunctions(sourceToSanitize);\n }\n const sanitizedSource = sanitizeObjects(sourceToSanitize);\n if (sanitizedSource !== '[Object]' && sanitizedSource !== '[Array]' && sanitizedSource !== '[Error]') {\n return sanitizedSource;\n }\n // Handle potential cyclic references\n // We need to use source as sourceToSanitize could be a reference to a new object\n // At this stage, we know the source is an object type\n const sourceAsObject = source;\n if (visitedObjectsWithPath.has(sourceAsObject)) {\n return `[Reference seen at ${visitedObjectsWithPath.get(sourceAsObject)}]`;\n }\n // Add processed source to queue\n const currentPath = key !== undefined ? `${parentPath}.${key}` : parentPath;\n const target = Array.isArray(sourceToSanitize) ? [] : {};\n visitedObjectsWithPath.set(sourceAsObject, currentPath);\n queue.push({\n source: sourceToSanitize,\n target,\n path: currentPath\n });\n return target;\n}\n/**\n * Handles sanitization of simple, non-object types\n *\n */\nfunction sanitizePrimitivesAndFunctions(value) {\n // BigInt cannot be serialized by JSON.stringify(), convert it to a string representation\n if (typeof value === 'bigint') {\n return `[BigInt] ${value.toString()}`;\n }\n // Functions cannot be serialized by JSON.stringify(). Moreover, if a faulty toJSON is present, it needs to be converted\n // so it won't prevent stringify from serializing later\n if (typeof value === 'function') {\n return `[Function] ${value.name || 'unknown'}`;\n }\n // JSON.stringify() does not serialize symbols.\n if (typeof value === 'symbol') {\n return `[Symbol] ${value.description || value.toString()}`;\n }\n return value;\n}\n/**\n * Handles sanitization of object types\n *\n * LIMITATIONS\n * - If a class defines a toStringTag Symbol, it will fall in the catch-all method and prevent enumeration of properties.\n * To avoid this, a toJSON method can be defined.\n */\nfunction sanitizeObjects(value) {\n try {\n if (value instanceof Event) {\n return sanitizeEvent(value);\n }\n if (value instanceof RegExp) {\n return `[RegExp] ${value.toString()}`;\n }\n // Handle all remaining object types in a generic way\n const result = Object.prototype.toString.call(value);\n const match = result.match(/\\[object (.*)\\]/);\n if (match && match[1]) {\n return `[${match[1]}]`;\n }\n } catch (_a) {\n // If the previous serialization attempts failed, and we cannot convert using\n // Object.prototype.toString, declare the value unserializable\n }\n return '[Unserializable]';\n}\nfunction sanitizeEvent(event) {\n return {\n type: event.type,\n isTrusted: event.isTrusted,\n currentTarget: event.currentTarget ? sanitizeObjects(event.currentTarget) : null,\n target: event.target ? sanitizeObjects(event.target) : null\n };\n}\n/**\n * Checks if a toJSON function exists and tries to execute it\n *\n */\nfunction tryToApplyToJSON(value) {\n const object = value;\n if (object && typeof object.toJSON === 'function') {\n try {\n return object.toJSON();\n } catch (_a) {\n // If toJSON fails, we continue by trying to serialize the value manually\n }\n }\n return value;\n}\n/**\n * Helper function to display the warning when the accumulated character count is over the limit\n */\nfunction warnOverCharacterLimit(maxCharacterCount, changeType, source) {\n display.warn(`The data provided has been ${changeType} as it is over the limit of ${maxCharacterCount} characters:`, source);\n}\n","/**\n * Cross-browser stack trace computation.\n *\n * Reference implementation: https://github.com/csnover/TraceKit/blob/04530298073c3823de72deb0b97e7b38ca7bcb59/tracekit.js\n */\nconst UNKNOWN_FUNCTION = '?';\nexport function computeStackTrace(ex) {\n const stack = [];\n let stackProperty = tryToGetString(ex, 'stack');\n const exString = String(ex);\n if (stackProperty && stackProperty.startsWith(exString)) {\n stackProperty = stackProperty.slice(exString.length);\n }\n if (stackProperty) {\n stackProperty.split('\\n').forEach(line => {\n const stackFrame = parseChromeLine(line) || parseChromeAnonymousLine(line) || parseWinLine(line) || parseGeckoLine(line);\n if (stackFrame) {\n if (!stackFrame.func && stackFrame.line) {\n stackFrame.func = UNKNOWN_FUNCTION;\n }\n stack.push(stackFrame);\n }\n });\n }\n return {\n message: tryToGetString(ex, 'message'),\n name: tryToGetString(ex, 'name'),\n stack\n };\n}\nconst fileUrl = '((?:file|https?|blob|chrome-extension|electron|native|eval|webpack|snippet||\\\\w+\\\\.|\\\\/).*?)';\nconst filePosition = '(?::(\\\\d+))';\nconst CHROME_LINE_RE = new RegExp(`^\\\\s*at (.*?) ?\\\\(${fileUrl}${filePosition}?${filePosition}?\\\\)?\\\\s*$`, 'i');\nconst CHROME_EVAL_RE = new RegExp(`\\\\((\\\\S*)${filePosition}${filePosition}\\\\)`);\nfunction parseChromeLine(line) {\n const parts = CHROME_LINE_RE.exec(line);\n if (!parts) {\n return;\n }\n const isNative = parts[2] && parts[2].indexOf('native') === 0; // start of line\n const isEval = parts[2] && parts[2].indexOf('eval') === 0; // start of line\n const submatch = CHROME_EVAL_RE.exec(parts[2]);\n if (isEval && submatch) {\n // throw out eval line/column and use top-most line/column number\n parts[2] = submatch[1]; // url\n parts[3] = submatch[2]; // line\n parts[4] = submatch[3]; // column\n }\n return {\n args: isNative ? [parts[2]] : [],\n column: parts[4] ? +parts[4] : undefined,\n func: parts[1] || UNKNOWN_FUNCTION,\n line: parts[3] ? +parts[3] : undefined,\n url: !isNative ? parts[2] : undefined\n };\n}\nconst CHROME_ANONYMOUS_FUNCTION_RE = new RegExp(`^\\\\s*at ?${fileUrl}${filePosition}?${filePosition}??\\\\s*$`, 'i');\nfunction parseChromeAnonymousLine(line) {\n const parts = CHROME_ANONYMOUS_FUNCTION_RE.exec(line);\n if (!parts) {\n return;\n }\n return {\n args: [],\n column: parts[3] ? +parts[3] : undefined,\n func: UNKNOWN_FUNCTION,\n line: parts[2] ? +parts[2] : undefined,\n url: parts[1]\n };\n}\nconst WINJS_LINE_RE = /^\\s*at (?:((?:\\[object object\\])?.+) )?\\(?((?:file|ms-appx|https?|webpack|blob):.*?):(\\d+)(?::(\\d+))?\\)?\\s*$/i;\nfunction parseWinLine(line) {\n const parts = WINJS_LINE_RE.exec(line);\n if (!parts) {\n return;\n }\n return {\n args: [],\n column: parts[4] ? +parts[4] : undefined,\n func: parts[1] || UNKNOWN_FUNCTION,\n line: +parts[3],\n url: parts[2]\n };\n}\nconst GECKO_LINE_RE = /^\\s*(.*?)(?:\\((.*?)\\))?(?:^|@)((?:file|https?|blob|chrome|webpack|resource|capacitor|\\[native).*?|[^@]*bundle)(?::(\\d+))?(?::(\\d+))?\\s*$/i;\nconst GECKO_EVAL_RE = /(\\S+) line (\\d+)(?: > eval line \\d+)* > eval/i;\nfunction parseGeckoLine(line) {\n const parts = GECKO_LINE_RE.exec(line);\n if (!parts) {\n return;\n }\n const isEval = parts[3] && parts[3].indexOf(' > eval') > -1;\n const submatch = GECKO_EVAL_RE.exec(parts[3]);\n if (isEval && submatch) {\n // throw out eval line/column and use top-most line number\n parts[3] = submatch[1];\n parts[4] = submatch[2];\n parts[5] = undefined; // no column when eval\n }\n return {\n args: parts[2] ? parts[2].split(',') : [],\n column: parts[5] ? +parts[5] : undefined,\n func: parts[1] || UNKNOWN_FUNCTION,\n line: parts[4] ? +parts[4] : undefined,\n url: parts[3]\n };\n}\nfunction tryToGetString(candidate, property) {\n if (typeof candidate !== 'object' || !candidate || !(property in candidate)) {\n return undefined;\n }\n const value = candidate[property];\n return typeof value === 'string' ? value : undefined;\n}\nexport function computeStackTraceFromOnErrorMessage(messageObj, url, line, column) {\n const stack = [{\n url,\n column,\n line\n }];\n const {\n name,\n message\n } = tryToParseMessage(messageObj);\n return {\n name,\n message,\n stack\n };\n}\n// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types\nconst ERROR_TYPES_RE = /^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): )?([\\s\\S]*)$/;\nfunction tryToParseMessage(messageObj) {\n let name;\n let message;\n if ({}.toString.call(messageObj) === '[object String]') {\n ;\n [, name, message] = ERROR_TYPES_RE.exec(messageObj);\n }\n return {\n name,\n message\n };\n}\n","import { callMonitored } from '../monitor';\nimport { computeStackTrace } from './computeStackTrace';\n/**\n * Creates a stacktrace without SDK internal frames.\n * Constraints:\n * - Has to be called at the utmost position of the call stack.\n * - No monitored function should encapsulate it, that is why we need to use callMonitored inside it.\n */\nexport function createHandlingStack(type) {\n /**\n * Skip the two internal frames:\n * - SDK API (console.error, ...)\n * - this function\n * in order to keep only the user calls\n */\n const internalFramesToSkip = 2;\n const error = new Error(type);\n error.name = 'HandlingStack';\n let formattedStack;\n callMonitored(() => {\n const stackTrace = computeStackTrace(error);\n stackTrace.stack = stackTrace.stack.slice(internalFramesToSkip);\n formattedStack = toStackTraceString(stackTrace);\n });\n return formattedStack;\n}\nexport function toStackTraceString(stack) {\n let result = formatErrorMessage(stack);\n stack.stack.forEach(frame => {\n const func = frame.func === '?' ? '' : frame.func;\n const args = frame.args && frame.args.length > 0 ? `(${frame.args.join(', ')})` : '';\n const line = frame.line ? `:${frame.line}` : '';\n const column = frame.line && frame.column ? `:${frame.column}` : '';\n result += `\\n at ${func}${args} @ ${frame.url}${line}${column}`;\n });\n return result;\n}\nexport function formatErrorMessage(stack) {\n return `${stack.name || 'Error'}: ${stack.message}`;\n}\n","import { sanitize } from '../../tools/serialisation/sanitize';\nimport { jsonStringify } from '../../tools/serialisation/jsonStringify';\nimport { computeStackTrace } from '../../tools/stackTrace/computeStackTrace';\nimport { toStackTraceString } from '../../tools/stackTrace/handlingStack';\nexport const NO_ERROR_STACK_PRESENT_MESSAGE = 'No stack, consider using an instance of Error';\nexport function computeRawError({\n stackTrace,\n originalError,\n handlingStack,\n componentStack,\n startClocks,\n nonErrorPrefix,\n source,\n handling\n}) {\n const isErrorInstance = isError(originalError);\n const message = computeMessage(stackTrace, isErrorInstance, nonErrorPrefix, originalError);\n const stack = hasUsableStack(isErrorInstance, stackTrace) ? toStackTraceString(stackTrace) : NO_ERROR_STACK_PRESENT_MESSAGE;\n const causes = isErrorInstance ? flattenErrorCauses(originalError, source) : undefined;\n const type = stackTrace ? stackTrace.name : undefined;\n const fingerprint = tryToGetFingerprint(originalError);\n const context = tryToGetErrorContext(originalError);\n return {\n startClocks,\n source,\n handling,\n handlingStack,\n componentStack,\n originalError,\n type,\n message,\n stack,\n causes,\n fingerprint,\n context\n };\n}\nfunction computeMessage(stackTrace, isErrorInstance, nonErrorPrefix, originalError) {\n // Favor stackTrace message only if tracekit has really been able to extract something meaningful (message + name)\n // TODO rework tracekit integration to avoid scattering error building logic\n return (stackTrace === null || stackTrace === void 0 ? void 0 : stackTrace.message) && (stackTrace === null || stackTrace === void 0 ? void 0 : stackTrace.name) ? stackTrace.message : !isErrorInstance ? `${nonErrorPrefix} ${jsonStringify(sanitize(originalError))}` : 'Empty message';\n}\nfunction hasUsableStack(isErrorInstance, stackTrace) {\n if (stackTrace === undefined) {\n return false;\n }\n if (isErrorInstance) {\n return true;\n }\n // handle cases where tracekit return stack = [] or stack = [{url: undefined, line: undefined, column: undefined}]\n // TODO rework tracekit integration to avoid generating those unusable stack\n return stackTrace.stack.length > 0 && (stackTrace.stack.length > 1 || stackTrace.stack[0].url !== undefined);\n}\nexport function tryToGetFingerprint(originalError) {\n return isError(originalError) && 'dd_fingerprint' in originalError ? String(originalError.dd_fingerprint) : undefined;\n}\nexport function tryToGetErrorContext(originalError) {\n if (originalError !== null && typeof originalError === 'object' && 'dd_context' in originalError) {\n return originalError.dd_context;\n }\n}\nexport function getFileFromStackTraceString(stack) {\n var _a;\n return (_a = /@ (.+)/.exec(stack)) === null || _a === void 0 ? void 0 : _a[1];\n}\nexport function isError(error) {\n return error instanceof Error || Object.prototype.toString.call(error) === '[object Error]';\n}\nexport function flattenErrorCauses(error, parentSource) {\n let currentError = error;\n const causes = [];\n while (isError(currentError === null || currentError === void 0 ? void 0 : currentError.cause) && causes.length < 10) {\n const stackTrace = computeStackTrace(currentError.cause);\n causes.push({\n message: currentError.cause.message,\n source: parentSource,\n type: stackTrace === null || stackTrace === void 0 ? void 0 : stackTrace.name,\n stack: stackTrace && toStackTraceString(stackTrace)\n });\n currentError = currentError.cause;\n }\n return causes.length ? causes : undefined;\n}\n","export function shallowClone(object) {\n return {\n ...object\n };\n}\nexport function objectHasValue(object, value) {\n return Object.keys(object).some(key => object[key] === value);\n}\nexport function isEmptyObject(object) {\n return Object.keys(object).length === 0;\n}\nexport function mapValues(object, fn) {\n const newObject = {};\n for (const key of Object.keys(object)) {\n newObject[key] = fn(object[key]);\n }\n return newObject;\n}\n","/**\n * LIMITATION:\n * For NPM setup, this feature flag singleton is shared between RUM and Logs product.\n * This means that an experimental flag set on the RUM product will be set on the Logs product.\n * So keep in mind that in certain configurations, your experimental feature flag may affect other products.\n *\n * FORMAT:\n * All feature flags should be snake_cased\n */\n// We want to use a real enum (i.e. not a const enum) here, to be able to check whether an arbitrary\n// string is an expected feature flag\nimport { objectHasValue } from './utils/objectUtils';\n// eslint-disable-next-line no-restricted-syntax\nexport var ExperimentalFeature = /*#__PURE__*/function (ExperimentalFeature) {\n ExperimentalFeature[\"WRITABLE_RESOURCE_GRAPHQL\"] = \"writable_resource_graphql\";\n ExperimentalFeature[\"MISSING_URL_CONTEXT_TELEMETRY\"] = \"missing_url_context_telemetry\";\n ExperimentalFeature[\"USER_ACCOUNT_TRACE_HEADER\"] = \"user_account_trace_header\";\n ExperimentalFeature[\"PROFILING\"] = \"profiling\";\n return ExperimentalFeature;\n}(ExperimentalFeature || {});\nconst enabledExperimentalFeatures = new Set();\nexport function initFeatureFlags(enableExperimentalFeatures) {\n if (Array.isArray(enableExperimentalFeatures)) {\n addExperimentalFeatures(enableExperimentalFeatures.filter(flag => objectHasValue(ExperimentalFeature, flag)));\n }\n}\nexport function addExperimentalFeatures(enabledFeatures) {\n enabledFeatures.forEach(flag => {\n enabledExperimentalFeatures.add(flag);\n });\n}\nexport function isExperimentalFeatureEnabled(featureName) {\n return enabledExperimentalFeatures.has(featureName);\n}\nexport function resetExperimentalFeatures() {\n enabledExperimentalFeatures.clear();\n}\nexport function getExperimentalFeatures() {\n return enabledExperimentalFeatures;\n}\n","import { display } from './display';\nexport function catchUserErrors(fn, errorMsg) {\n return (...args) => {\n try {\n return fn(...args);\n } catch (err) {\n display.error(errorMsg, err);\n }\n };\n}\n","/**\n * Return true if the draw is successful\n * @param threshold between 0 and 100\n */\nexport function performDraw(threshold) {\n return threshold !== 0 && Math.random() * 100 <= threshold;\n}\nexport function round(num, decimals) {\n return +num.toFixed(decimals);\n}\nexport function isPercentage(value) {\n return isNumber(value) && value >= 0 && value <= 100;\n}\nexport function isNumber(value) {\n return typeof value === 'number';\n}\n","import { isNumber, round } from './numberUtils';\nexport const ONE_SECOND = 1000;\nexport const ONE_MINUTE = 60 * ONE_SECOND;\nexport const ONE_HOUR = 60 * ONE_MINUTE;\nexport const ONE_DAY = 24 * ONE_HOUR;\nexport const ONE_YEAR = 365 * ONE_DAY;\nexport function relativeToClocks(relative) {\n return {\n relative,\n timeStamp: getCorrectedTimeStamp(relative)\n };\n}\nexport function timeStampToClocks(timeStamp) {\n return {\n relative: getRelativeTime(timeStamp),\n timeStamp\n };\n}\nfunction getCorrectedTimeStamp(relativeTime) {\n const correctedOrigin = dateNow() - performance.now();\n // apply correction only for positive drift\n if (correctedOrigin > getNavigationStart()) {\n return Math.round(addDuration(correctedOrigin, relativeTime));\n }\n return getTimeStamp(relativeTime);\n}\nexport function currentDrift() {\n return Math.round(dateNow() - addDuration(getNavigationStart(), performance.now()));\n}\nexport function toServerDuration(duration) {\n if (!isNumber(duration)) {\n return duration;\n }\n return round(duration * 1e6, 0);\n}\nexport function dateNow() {\n // Do not use `Date.now` because sometimes websites are wrongly \"polyfilling\" it. For example, we\n // had some users using a very old version of `datejs`, which patched `Date.now` to return a Date\n // instance instead of a timestamp[1]. Those users are unlikely to fix this, so let's handle this\n // case ourselves.\n // [1]: https://github.com/datejs/Datejs/blob/97f5c7c58c5bc5accdab8aa7602b6ac56462d778/src/core-debug.js#L14-L16\n return new Date().getTime();\n}\nexport function timeStampNow() {\n return dateNow();\n}\nexport function relativeNow() {\n return performance.now();\n}\nexport function clocksNow() {\n return {\n relative: relativeNow(),\n timeStamp: timeStampNow()\n };\n}\nexport function clocksOrigin() {\n return {\n relative: 0,\n timeStamp: getNavigationStart()\n };\n}\nexport function elapsed(start, end) {\n return end - start;\n}\nexport function addDuration(a, b) {\n return a + b;\n}\n// Get the time since the navigation was started.\nexport function getRelativeTime(timestamp) {\n return timestamp - getNavigationStart();\n}\nexport function getTimeStamp(relativeTime) {\n return Math.round(addDuration(getNavigationStart(), relativeTime));\n}\nexport function looksLikeRelativeTime(time) {\n return time < ONE_YEAR;\n}\n/**\n * Navigation start slightly change on some rare cases\n */\nlet navigationStart;\n/**\n * Notes: this does not use `performance.timeOrigin` because:\n * - It doesn't seem to reflect the actual time on which the navigation has started: it may be much farther in the past,\n * at least in Firefox 71. (see: https://bugzilla.mozilla.org/show_bug.cgi?id=1429926)\n * - It is not supported in Safari <15\n */\nfunction getNavigationStart() {\n if (navigationStart === undefined) {\n navigationStart = performance.timing.navigationStart;\n }\n return navigationStart;\n}\n","// eslint-disable-next-line no-restricted-syntax\nexport class Observable {\n constructor(onFirstSubscribe) {\n this.onFirstSubscribe = onFirstSubscribe;\n this.observers = [];\n }\n subscribe(f) {\n this.observers.push(f);\n if (this.observers.length === 1 && this.onFirstSubscribe) {\n this.onLastUnsubscribe = this.onFirstSubscribe(this) || undefined;\n }\n return {\n unsubscribe: () => {\n this.observers = this.observers.filter(other => f !== other);\n if (!this.observers.length && this.onLastUnsubscribe) {\n this.onLastUnsubscribe();\n }\n }\n };\n }\n notify(data) {\n this.observers.forEach(observer => observer(data));\n }\n}\nexport function mergeObservables(...observables) {\n return new Observable(globalObservable => {\n const subscriptions = observables.map(observable => observable.subscribe(data => globalObservable.notify(data)));\n return () => subscriptions.forEach(subscription => subscription.unsubscribe());\n });\n}\n","/**\n * UUID v4\n * from https://gist.github.com/jed/982883\n */\nexport function generateUUID(placeholder) {\n return placeholder ?\n // eslint-disable-next-line no-bitwise\n (parseInt(placeholder, 10) ^ Math.random() * 16 >> parseInt(placeholder, 10) / 4).toString(16) : `${1e7}-${1e3}-${4e3}-${8e3}-${1e11}`.replace(/[018]/g, generateUUID);\n}\nconst COMMA_SEPARATED_KEY_VALUE = /([\\w-]+)\\s*=\\s*([^;]+)/g;\nexport function findCommaSeparatedValue(rawString, name) {\n COMMA_SEPARATED_KEY_VALUE.lastIndex = 0;\n while (true) {\n const match = COMMA_SEPARATED_KEY_VALUE.exec(rawString);\n if (match) {\n if (match[1] === name) {\n return match[2];\n }\n } else {\n break;\n }\n }\n}\nexport function findCommaSeparatedValues(rawString) {\n const result = new Map();\n COMMA_SEPARATED_KEY_VALUE.lastIndex = 0;\n while (true) {\n const match = COMMA_SEPARATED_KEY_VALUE.exec(rawString);\n if (match) {\n result.set(match[1], match[2]);\n } else {\n break;\n }\n }\n return result;\n}\nexport function safeTruncate(candidate, length, suffix = '') {\n const lastChar = candidate.charCodeAt(length - 1);\n const isLastCharSurrogatePair = lastChar >= 0xd800 && lastChar <= 0xdbff;\n const correctedLength = isLastCharSurrogatePair ? length + 1 : length;\n if (candidate.length <= correctedLength) {\n return candidate;\n }\n return `${candidate.slice(0, correctedLength)}${suffix}`;\n}\n","export function isChromium() {\n return detectBrowserCached() === 0 /* Browser.CHROMIUM */;\n}\nexport function isSafari() {\n return detectBrowserCached() === 1 /* Browser.SAFARI */;\n}\nlet browserCache;\nfunction detectBrowserCached() {\n return browserCache !== null && browserCache !== void 0 ? browserCache : browserCache = detectBrowser();\n}\n// Exported only for tests\nexport function detectBrowser(browserWindow = window) {\n var _a;\n const userAgent = browserWindow.navigator.userAgent;\n if (browserWindow.chrome || /HeadlessChrome/.test(userAgent)) {\n return 0 /* Browser.CHROMIUM */;\n }\n if (\n // navigator.vendor is deprecated, but it is the most resilient way we found to detect\n // \"Apple maintained browsers\" (AKA Safari). If one day it gets removed, we still have the\n // useragent test as a semi-working fallback.\n ((_a = browserWindow.navigator.vendor) === null || _a === void 0 ? void 0 : _a.indexOf('Apple')) === 0 || /safari/i.test(userAgent) && !/chrome|android/i.test(userAgent)) {\n return 1 /* Browser.SAFARI */;\n }\n return 2 /* Browser.OTHER */;\n}\n","import { display } from '../tools/display';\nimport { ONE_MINUTE, ONE_SECOND } from '../tools/utils/timeUtils';\nimport { findCommaSeparatedValue, findCommaSeparatedValues, generateUUID } from '../tools/utils/stringUtils';\nexport function setCookie(name, value, expireDelay = 0, options) {\n const date = new Date();\n date.setTime(date.getTime() + expireDelay);\n const expires = `expires=${date.toUTCString()}`;\n const sameSite = options && options.crossSite ? 'none' : 'strict';\n const domain = options && options.domain ? `;domain=${options.domain}` : '';\n const secure = options && options.secure ? ';secure' : '';\n const partitioned = options && options.partitioned ? ';partitioned' : '';\n document.cookie = `${name}=${value};${expires};path=/;samesite=${sameSite}${domain}${secure}${partitioned}`;\n}\nexport function getCookie(name) {\n return findCommaSeparatedValue(document.cookie, name);\n}\nlet initCookieParsed;\n/**\n * Returns a cached value of the cookie. Use this during SDK initialization (and whenever possible)\n * to avoid accessing document.cookie multiple times.\n */\nexport function getInitCookie(name) {\n if (!initCookieParsed) {\n initCookieParsed = findCommaSeparatedValues(document.cookie);\n }\n return initCookieParsed.get(name);\n}\nexport function resetInitCookies() {\n initCookieParsed = undefined;\n}\nexport function deleteCookie(name, options) {\n setCookie(name, '', 0, options);\n}\nexport function areCookiesAuthorized(options) {\n if (document.cookie === undefined || document.cookie === null) {\n return false;\n }\n try {\n // Use a unique cookie name to avoid issues when the SDK is initialized multiple times during\n // the test cookie lifetime\n const testCookieName = `dd_cookie_test_${generateUUID()}`;\n const testCookieValue = 'test';\n setCookie(testCookieName, testCookieValue, ONE_MINUTE, options);\n const isCookieCorrectlySet = getCookie(testCookieName) === testCookieValue;\n deleteCookie(testCookieName, options);\n return isCookieCorrectlySet;\n } catch (error) {\n display.error(error);\n return false;\n }\n}\n/**\n * No API to retrieve it, number of levels for subdomain and suffix are unknown\n * strategy: find the minimal domain on which cookies are allowed to be set\n * https://web.dev/same-site-same-origin/#site\n */\nlet getCurrentSiteCache;\nexport function getCurrentSite() {\n if (getCurrentSiteCache === undefined) {\n // Use a unique cookie name to avoid issues when the SDK is initialized multiple times during\n // the test cookie lifetime\n const testCookieName = `dd_site_test_${generateUUID()}`;\n const testCookieValue = 'test';\n const domainLevels = window.location.hostname.split('.');\n let candidateDomain = domainLevels.pop();\n while (domainLevels.length && !getCookie(testCookieName)) {\n candidateDomain = `${domainLevels.pop()}.${candidateDomain}`;\n setCookie(testCookieName, testCookieValue, ONE_SECOND, {\n domain: candidateDomain\n });\n }\n deleteCookie(testCookieName, {\n domain: candidateDomain\n });\n getCurrentSiteCache = candidateDomain;\n }\n return getCurrentSiteCache;\n}\n","export const SESSION_STORE_KEY = '_dd_s';\n","export function findLast(array, predicate) {\n for (let i = array.length - 1; i >= 0; i -= 1) {\n const item = array[i];\n if (predicate(item, i, array)) {\n return item;\n }\n }\n return undefined;\n}\n// Keep the following wrapper functions as it can be mangled and will result in smaller bundle size that using\n// the native Object.values and Object.entries directly\nexport function objectValues(object) {\n return Object.values(object);\n}\nexport function objectEntries(object) {\n return Object.entries(object);\n}\n","import { ONE_HOUR, ONE_MINUTE, ONE_YEAR } from '../../tools/utils/timeUtils';\nexport const SESSION_TIME_OUT_DELAY = 4 * ONE_HOUR;\nexport const SESSION_EXPIRATION_DELAY = 15 * ONE_MINUTE;\nexport const SESSION_COOKIE_EXPIRATION_DELAY = ONE_YEAR;\nexport const SessionPersistence = {\n COOKIE: 'cookie',\n LOCAL_STORAGE: 'local-storage'\n};\n","export const SESSION_ENTRY_REGEXP = /^([a-zA-Z]+)=([a-z0-9-]+)$/;\nexport const SESSION_ENTRY_SEPARATOR = '&';\nexport function isValidSessionString(sessionString) {\n return !!sessionString && (sessionString.indexOf(SESSION_ENTRY_SEPARATOR) !== -1 || SESSION_ENTRY_REGEXP.test(sessionString));\n}\n","import { isEmptyObject } from '../../tools/utils/objectUtils';\nimport { objectEntries } from '../../tools/utils/polyfills';\nimport { dateNow } from '../../tools/utils/timeUtils';\nimport { generateUUID } from '../../tools/utils/stringUtils';\nimport { SESSION_EXPIRATION_DELAY, SESSION_TIME_OUT_DELAY } from './sessionConstants';\nimport { isValidSessionString, SESSION_ENTRY_REGEXP, SESSION_ENTRY_SEPARATOR } from './sessionStateValidation';\nexport const EXPIRED = '1';\nexport function getExpiredSessionState(previousSessionState, configuration) {\n const expiredSessionState = {\n isExpired: EXPIRED\n };\n if (configuration.trackAnonymousUser) {\n if (previousSessionState === null || previousSessionState === void 0 ? void 0 : previousSessionState.anonymousId) {\n expiredSessionState.anonymousId = previousSessionState === null || previousSessionState === void 0 ? void 0 : previousSessionState.anonymousId;\n } else {\n expiredSessionState.anonymousId = generateUUID();\n }\n }\n return expiredSessionState;\n}\nexport function isSessionInNotStartedState(session) {\n return isEmptyObject(session);\n}\nexport function isSessionStarted(session) {\n return !isSessionInNotStartedState(session);\n}\nexport function isSessionInExpiredState(session) {\n return session.isExpired !== undefined || !isActiveSession(session);\n}\n// An active session is a session in either `Tracked` or `NotTracked` state\nfunction isActiveSession(sessionState) {\n // created and expire can be undefined for versions which was not storing them\n // these checks could be removed when older versions will not be available/live anymore\n return (sessionState.created === undefined || dateNow() - Number(sessionState.created) < SESSION_TIME_OUT_DELAY) && (sessionState.expire === undefined || dateNow() < Number(sessionState.expire));\n}\nexport function expandSessionState(session) {\n session.expire = String(dateNow() + SESSION_EXPIRATION_DELAY);\n}\nexport function toSessionString(session) {\n return objectEntries(session)\n // we use `aid` as a key for anonymousId\n .map(([key, value]) => key === 'anonymousId' ? `aid=${value}` : `${key}=${value}`).join(SESSION_ENTRY_SEPARATOR);\n}\nexport function toSessionState(sessionString) {\n const session = {};\n if (isValidSessionString(sessionString)) {\n sessionString.split(SESSION_ENTRY_SEPARATOR).forEach(entry => {\n const matches = SESSION_ENTRY_REGEXP.exec(entry);\n if (matches !== null) {\n const [, key, value] = matches;\n if (key === 'aid') {\n // we use `aid` as a key for anonymousId\n session.anonymousId = value;\n } else {\n session[key] = value;\n }\n }\n });\n }\n return session;\n}\n","import { getInitCookie } from '../../browser/cookie';\nimport { SESSION_STORE_KEY } from './storeStrategies/sessionStoreStrategy';\nimport { expandSessionState, isSessionStarted } from './sessionState';\nexport const OLD_SESSION_COOKIE_NAME = '_dd';\nexport const OLD_RUM_COOKIE_NAME = '_dd_r';\nexport const OLD_LOGS_COOKIE_NAME = '_dd_l';\n// duplicate values to avoid dependency issues\nexport const RUM_SESSION_KEY = 'rum';\nexport const LOGS_SESSION_KEY = 'logs';\n/**\n * This migration should remain in the codebase as long as older versions are available/live\n * to allow older sdk versions to be upgraded to newer versions without compatibility issues.\n */\nexport function tryOldCookiesMigration(cookieStoreStrategy) {\n const sessionString = getInitCookie(SESSION_STORE_KEY);\n if (!sessionString) {\n const oldSessionId = getInitCookie(OLD_SESSION_COOKIE_NAME);\n const oldRumType = getInitCookie(OLD_RUM_COOKIE_NAME);\n const oldLogsType = getInitCookie(OLD_LOGS_COOKIE_NAME);\n const session = {};\n if (oldSessionId) {\n session.id = oldSessionId;\n }\n if (oldLogsType && /^[01]$/.test(oldLogsType)) {\n session[LOGS_SESSION_KEY] = oldLogsType;\n }\n if (oldRumType && /^[012]$/.test(oldRumType)) {\n session[RUM_SESSION_KEY] = oldRumType;\n }\n if (isSessionStarted(session)) {\n expandSessionState(session);\n cookieStoreStrategy.persistSession(session);\n }\n }\n}\n","import { isChromium } from '../../../tools/utils/browserDetection';\nimport { getCurrentSite, areCookiesAuthorized, getCookie, setCookie } from '../../../browser/cookie';\nimport { tryOldCookiesMigration } from '../oldCookiesMigration';\nimport { SESSION_COOKIE_EXPIRATION_DELAY, SESSION_EXPIRATION_DELAY, SESSION_TIME_OUT_DELAY, SessionPersistence } from '../sessionConstants';\nimport { toSessionString, toSessionState, getExpiredSessionState } from '../sessionState';\nimport { SESSION_STORE_KEY } from './sessionStoreStrategy';\nexport function selectCookieStrategy(initConfiguration) {\n const cookieOptions = buildCookieOptions(initConfiguration);\n return areCookiesAuthorized(cookieOptions) ? {\n type: SessionPersistence.COOKIE,\n cookieOptions\n } : undefined;\n}\nexport function initCookieStrategy(configuration, cookieOptions) {\n const cookieStore = {\n /**\n * Lock strategy allows mitigating issues due to concurrent access to cookie.\n * This issue concerns only chromium browsers and enabling this on firefox increases cookie write failures.\n */\n isLockEnabled: isChromium(),\n persistSession: persistSessionCookie(cookieOptions),\n retrieveSession: retrieveSessionCookie,\n expireSession: sessionState => expireSessionCookie(cookieOptions, sessionState, configuration)\n };\n tryOldCookiesMigration(cookieStore);\n return cookieStore;\n}\nfunction persistSessionCookie(options) {\n return session => {\n setCookie(SESSION_STORE_KEY, toSessionString(session), SESSION_EXPIRATION_DELAY, options);\n };\n}\nfunction expireSessionCookie(options, sessionState, configuration) {\n const expiredSessionState = getExpiredSessionState(sessionState, configuration);\n // we do not extend cookie expiration date\n setCookie(SESSION_STORE_KEY, toSessionString(expiredSessionState), configuration.trackAnonymousUser ? SESSION_COOKIE_EXPIRATION_DELAY : SESSION_TIME_OUT_DELAY, options);\n}\nfunction retrieveSessionCookie() {\n const sessionString = getCookie(SESSION_STORE_KEY);\n const sessionState = toSessionState(sessionString);\n return sessionState;\n}\nexport function buildCookieOptions(initConfiguration) {\n const cookieOptions = {};\n cookieOptions.secure = !!initConfiguration.useSecureSessionCookie || !!initConfiguration.usePartitionedCrossSiteSessionCookie;\n cookieOptions.crossSite = !!initConfiguration.usePartitionedCrossSiteSessionCookie;\n cookieOptions.partitioned = !!initConfiguration.usePartitionedCrossSiteSessionCookie;\n if (initConfiguration.trackSessionAcrossSubdomains) {\n cookieOptions.domain = getCurrentSite();\n }\n return cookieOptions;\n}\n","import { generateUUID } from '../../../tools/utils/stringUtils';\nimport { SessionPersistence } from '../sessionConstants';\nimport { toSessionString, toSessionState, getExpiredSessionState } from '../sessionState';\nimport { SESSION_STORE_KEY } from './sessionStoreStrategy';\nconst LOCAL_STORAGE_TEST_KEY = '_dd_test_';\nexport function selectLocalStorageStrategy() {\n try {\n const id = generateUUID();\n const testKey = `${LOCAL_STORAGE_TEST_KEY}${id}`;\n localStorage.setItem(testKey, id);\n const retrievedId = localStorage.getItem(testKey);\n localStorage.removeItem(testKey);\n return id === retrievedId ? {\n type: SessionPersistence.LOCAL_STORAGE\n } : undefined;\n } catch (_a) {\n return undefined;\n }\n}\nexport function initLocalStorageStrategy(configuration) {\n return {\n isLockEnabled: false,\n persistSession: persistInLocalStorage,\n retrieveSession: retrieveSessionFromLocalStorage,\n expireSession: sessionState => expireSessionFromLocalStorage(sessionState, configuration)\n };\n}\nfunction persistInLocalStorage(sessionState) {\n localStorage.setItem(SESSION_STORE_KEY, toSessionString(sessionState));\n}\nfunction retrieveSessionFromLocalStorage() {\n const sessionString = localStorage.getItem(SESSION_STORE_KEY);\n return toSessionState(sessionString);\n}\nfunction expireSessionFromLocalStorage(previousSessionState, configuration) {\n persistInLocalStorage(getExpiredSessionState(previousSessionState, configuration));\n}\n","import { setTimeout } from '../../tools/timer';\nimport { generateUUID } from '../../tools/utils/stringUtils';\nimport { expandSessionState, isSessionInExpiredState } from './sessionState';\nexport const LOCK_RETRY_DELAY = 10;\nexport const LOCK_MAX_TRIES = 100;\nconst bufferedOperations = [];\nlet ongoingOperations;\nexport function processSessionStoreOperations(operations, sessionStoreStrategy, numberOfRetries = 0) {\n var _a;\n const {\n isLockEnabled,\n persistSession,\n expireSession\n } = sessionStoreStrategy;\n const persistWithLock = session => persistSession({\n ...session,\n lock: currentLock\n });\n const retrieveStore = () => {\n const session = sessionStoreStrategy.retrieveSession();\n const lock = session.lock;\n if (session.lock) {\n delete session.lock;\n }\n return {\n session,\n lock\n };\n };\n if (!ongoingOperations) {\n ongoingOperations = operations;\n }\n if (operations !== ongoingOperations) {\n bufferedOperations.push(operations);\n return;\n }\n if (isLockEnabled && numberOfRetries >= LOCK_MAX_TRIES) {\n next(sessionStoreStrategy);\n return;\n }\n let currentLock;\n let currentStore = retrieveStore();\n if (isLockEnabled) {\n // if someone has lock, retry later\n if (currentStore.lock) {\n retryLater(operations, sessionStoreStrategy, numberOfRetries);\n return;\n }\n // acquire lock\n currentLock = generateUUID();\n persistWithLock(currentStore.session);\n // if lock is not acquired, retry later\n currentStore = retrieveStore();\n if (currentStore.lock !== currentLock) {\n retryLater(operations, sessionStoreStrategy, numberOfRetries);\n return;\n }\n }\n let processedSession = operations.process(currentStore.session);\n if (isLockEnabled) {\n // if lock corrupted after process, retry later\n currentStore = retrieveStore();\n if (currentStore.lock !== currentLock) {\n retryLater(operations, sessionStoreStrategy, numberOfRetries);\n return;\n }\n }\n if (processedSession) {\n if (isSessionInExpiredState(processedSession)) {\n expireSession(processedSession);\n } else {\n expandSessionState(processedSession);\n if (isLockEnabled) {\n persistWithLock(processedSession);\n } else {\n persistSession(processedSession);\n }\n }\n }\n if (isLockEnabled) {\n // correctly handle lock around expiration would require to handle this case properly at several levels\n // since we don't have evidence of lock issues around expiration, let's just not do the corruption check for it\n if (!(processedSession && isSessionInExpiredState(processedSession))) {\n // if lock corrupted after persist, retry later\n currentStore = retrieveStore();\n if (currentStore.lock !== currentLock) {\n retryLater(operations, sessionStoreStrategy, numberOfRetries);\n return;\n }\n persistSession(currentStore.session);\n processedSession = currentStore.session;\n }\n }\n // call after even if session is not persisted in order to perform operations on\n // up-to-date session state value => the value could have been modified by another tab\n (_a = operations.after) === null || _a === void 0 ? void 0 : _a.call(operations, processedSession || currentStore.session);\n next(sessionStoreStrategy);\n}\nfunction retryLater(operations, sessionStore, currentNumberOfRetries) {\n setTimeout(() => {\n processSessionStoreOperations(operations, sessionStore, currentNumberOfRetries + 1);\n }, LOCK_RETRY_DELAY);\n}\nfunction next(sessionStore) {\n ongoingOperations = undefined;\n const nextOperations = bufferedOperations.shift();\n if (nextOperations) {\n processSessionStoreOperations(nextOperations, sessionStore);\n }\n}\n","import { clearInterval, setInterval } from '../../tools/timer';\nimport { Observable } from '../../tools/observable';\nimport { ONE_SECOND, dateNow } from '../../tools/utils/timeUtils';\nimport { throttle } from '../../tools/utils/functionUtils';\nimport { generateUUID } from '../../tools/utils/stringUtils';\nimport { display } from '../../tools/display';\nimport { selectCookieStrategy, initCookieStrategy } from './storeStrategies/sessionInCookie';\nimport { getExpiredSessionState, isSessionInExpiredState, isSessionInNotStartedState, isSessionStarted } from './sessionState';\nimport { initLocalStorageStrategy, selectLocalStorageStrategy } from './storeStrategies/sessionInLocalStorage';\nimport { processSessionStoreOperations } from './sessionStoreOperations';\nimport { SessionPersistence } from './sessionConstants';\n/**\n * Every second, the storage will be polled to check for any change that can occur\n * to the session state in another browser tab, or another window.\n * This value has been determined from our previous cookie-only implementation.\n */\nexport const STORAGE_POLL_DELAY = ONE_SECOND;\n/**\n * Selects the correct session store strategy type based on the configuration and storage\n * availability.\n */\nexport function selectSessionStoreStrategyType(initConfiguration) {\n switch (initConfiguration.sessionPersistence) {\n case SessionPersistence.COOKIE:\n return selectCookieStrategy(initConfiguration);\n case SessionPersistence.LOCAL_STORAGE:\n return selectLocalStorageStrategy();\n case undefined:\n {\n let sessionStoreStrategyType = selectCookieStrategy(initConfiguration);\n if (!sessionStoreStrategyType && initConfiguration.allowFallbackToLocalStorage) {\n sessionStoreStrategyType = selectLocalStorageStrategy();\n }\n return sessionStoreStrategyType;\n }\n default:\n display.error(`Invalid session persistence '${String(initConfiguration.sessionPersistence)}'`);\n }\n}\n/**\n * Different session concepts:\n * - tracked, the session has an id and is updated along the user navigation\n * - not tracked, the session does not have an id but it is updated along the user navigation\n * - inactive, no session in store or session expired, waiting for a renew session\n */\nexport function startSessionStore(sessionStoreStrategyType, configuration, productKey, computeSessionState) {\n const renewObservable = new Observable();\n const expireObservable = new Observable();\n const sessionStateUpdateObservable = new Observable();\n const sessionStoreStrategy = sessionStoreStrategyType.type === SessionPersistence.COOKIE ? initCookieStrategy(configuration, sessionStoreStrategyType.cookieOptions) : initLocalStorageStrategy(configuration);\n const {\n expireSession\n } = sessionStoreStrategy;\n const watchSessionTimeoutId = setInterval(watchSession, STORAGE_POLL_DELAY);\n let sessionCache;\n startSession();\n const {\n throttled: throttledExpandOrRenewSession,\n cancel: cancelExpandOrRenewSession\n } = throttle(() => {\n processSessionStoreOperations({\n process: sessionState => {\n if (isSessionInNotStartedState(sessionState)) {\n return;\n }\n const synchronizedSession = synchronizeSession(sessionState);\n expandOrRenewSessionState(synchronizedSession);\n return synchronizedSession;\n },\n after: sessionState => {\n if (isSessionStarted(sessionState) && !hasSessionInCache()) {\n renewSessionInCache(sessionState);\n }\n sessionCache = sessionState;\n }\n }, sessionStoreStrategy);\n }, STORAGE_POLL_DELAY);\n function expandSession() {\n processSessionStoreOperations({\n process: sessionState => hasSessionInCache() ? synchronizeSession(sessionState) : undefined\n }, sessionStoreStrategy);\n }\n /**\n * allows two behaviors:\n * - if the session is active, synchronize the session cache without updating the session store\n * - if the session is not active, clear the session store and expire the session cache\n */\n function watchSession() {\n processSessionStoreOperations({\n process: sessionState => isSessionInExpiredState(sessionState) ? getExpiredSessionState(sessionState, configuration) : undefined,\n after: synchronizeSession\n }, sessionStoreStrategy);\n }\n function synchronizeSession(sessionState) {\n if (isSessionInExpiredState(sessionState)) {\n sessionState = getExpiredSessionState(sessionState, configuration);\n }\n if (hasSessionInCache()) {\n if (isSessionInCacheOutdated(sessionState)) {\n expireSessionInCache();\n } else {\n sessionStateUpdateObservable.notify({\n previousState: sessionCache,\n newState: sessionState\n });\n sessionCache = sessionState;\n }\n }\n return sessionState;\n }\n function startSession() {\n processSessionStoreOperations({\n process: sessionState => {\n if (isSessionInNotStartedState(sessionState)) {\n return getExpiredSessionState(sessionState, configuration);\n }\n },\n after: sessionState => {\n sessionCache = sessionState;\n }\n }, sessionStoreStrategy);\n }\n function expandOrRenewSessionState(sessionState) {\n if (isSessionInNotStartedState(sessionState)) {\n return false;\n }\n const {\n trackingType,\n isTracked\n } = computeSessionState(sessionState[productKey]);\n sessionState[productKey] = trackingType;\n delete sessionState.isExpired;\n if (isTracked && !sessionState.id) {\n sessionState.id = generateUUID();\n sessionState.created = String(dateNow());\n }\n }\n function hasSessionInCache() {\n return sessionCache[productKey] !== undefined;\n }\n function isSessionInCacheOutdated(sessionState) {\n return sessionCache.id !== sessionState.id || sessionCache[productKey] !== sessionState[productKey];\n }\n function expireSessionInCache() {\n sessionCache = getExpiredSessionState(sessionCache, configuration);\n expireObservable.notify();\n }\n function renewSessionInCache(sessionState) {\n sessionCache = sessionState;\n renewObservable.notify();\n }\n function updateSessionState(partialSessionState) {\n processSessionStoreOperations({\n process: sessionState => ({\n ...sessionState,\n ...partialSessionState\n }),\n after: synchronizeSession\n }, sessionStoreStrategy);\n }\n return {\n expandOrRenewSession: throttledExpandOrRenewSession,\n expandSession,\n getSession: () => sessionCache,\n renewObservable,\n expireObservable,\n sessionStateUpdateObservable,\n restartSession: startSession,\n expire: () => {\n cancelExpandOrRenewSession();\n expireSession(sessionCache);\n synchronizeSession(getExpiredSessionState(sessionCache, configuration));\n },\n stop: () => {\n clearInterval(watchSessionTimeoutId);\n },\n updateSessionState\n };\n}\n","import { Observable } from '../tools/observable';\nexport const TrackingConsent = {\n GRANTED: 'granted',\n NOT_GRANTED: 'not-granted'\n};\nexport function createTrackingConsentState(currentConsent) {\n const observable = new Observable();\n return {\n tryToInit(trackingConsent) {\n if (!currentConsent) {\n currentConsent = trackingConsent;\n }\n },\n update(trackingConsent) {\n currentConsent = trackingConsent;\n observable.notify();\n },\n isGranted() {\n return currentConsent === TrackingConsent.GRANTED;\n },\n observable\n };\n}\n","import { jsonStringify } from '../serialisation/jsonStringify';\nexport function normalizeUrl(url) {\n return buildUrl(url, location.href).href;\n}\nexport function isValidUrl(url) {\n try {\n return !!buildUrl(url);\n } catch (_a) {\n return false;\n }\n}\nexport function getPathName(url) {\n const pathname = buildUrl(url).pathname;\n return pathname[0] === '/' ? pathname : `/${pathname}`;\n}\nexport function buildUrl(url, base) {\n const supportedURL = getSupportedUrl();\n if (supportedURL) {\n try {\n return base !== undefined ? new supportedURL(url, base) : new supportedURL(url);\n } catch (error) {\n throw new Error(`Failed to construct URL: ${String(error)} ${jsonStringify({\n url,\n base\n })}`);\n }\n }\n if (base === undefined && !/:/.test(url)) {\n throw new Error(`Invalid URL: '${url}'`);\n }\n let doc = document;\n const anchorElement = doc.createElement('a');\n if (base !== undefined) {\n doc = document.implementation.createHTMLDocument('');\n const baseElement = doc.createElement('base');\n baseElement.href = base;\n doc.head.appendChild(baseElement);\n doc.body.appendChild(anchorElement);\n }\n anchorElement.href = url;\n return anchorElement;\n}\nconst originalURL = URL;\nlet isURLSupported;\nfunction getSupportedUrl() {\n if (isURLSupported === undefined) {\n try {\n const url = new originalURL('http://test/path');\n isURLSupported = url.href === 'http://test/path';\n } catch (_a) {\n isURLSupported = false;\n }\n }\n return isURLSupported ? originalURL : undefined;\n}\n","export const INTAKE_SITE_STAGING = 'datad0g.com';\nexport const INTAKE_SITE_FED_STAGING = 'dd0g-gov.com';\nexport const INTAKE_SITE_US1 = 'datadoghq.com';\nexport const INTAKE_SITE_EU1 = 'datadoghq.eu';\nexport const INTAKE_SITE_US1_FED = 'ddog-gov.com';\nexport const PCI_INTAKE_HOST_US1 = 'pci.browser-intake-datadoghq.com';\nexport const INTAKE_URL_PARAMETERS = ['ddsource', 'ddtags'];\n","import { timeStampNow } from '../../tools/utils/timeUtils';\nimport { normalizeUrl } from '../../tools/utils/urlPolyfill';\nimport { generateUUID } from '../../tools/utils/stringUtils';\nimport { INTAKE_SITE_US1, INTAKE_SITE_FED_STAGING, PCI_INTAKE_HOST_US1 } from './intakeSites';\nexport function createEndpointBuilder(initConfiguration, trackType, configurationTags) {\n const buildUrlWithParameters = createEndpointUrlWithParametersBuilder(initConfiguration, trackType);\n return {\n build(api, payload) {\n const parameters = buildEndpointParameters(initConfiguration, trackType, configurationTags, api, payload);\n return buildUrlWithParameters(parameters);\n },\n tags: configurationTags,\n urlPrefix: buildUrlWithParameters(''),\n trackType\n };\n}\n/**\n * Create a function used to build a full endpoint url from provided parameters. The goal of this\n * function is to pre-compute some parts of the URL to avoid re-computing everything on every\n * request, as only parameters are changing.\n */\nfunction createEndpointUrlWithParametersBuilder(initConfiguration, trackType) {\n const path = `/api/v2/${trackType}`;\n const proxy = initConfiguration.proxy;\n if (typeof proxy === 'string') {\n const normalizedProxyUrl = normalizeUrl(proxy);\n return parameters => `${normalizedProxyUrl}?ddforward=${encodeURIComponent(`${path}?${parameters}`)}`;\n }\n if (typeof proxy === 'function') {\n return parameters => proxy({\n path,\n parameters\n });\n }\n const host = buildEndpointHost(trackType, initConfiguration);\n return parameters => `https://${host}${path}?${parameters}`;\n}\nexport function buildEndpointHost(trackType, initConfiguration) {\n const {\n site = INTAKE_SITE_US1,\n internalAnalyticsSubdomain\n } = initConfiguration;\n if (trackType === 'logs' && initConfiguration.usePciIntake && site === INTAKE_SITE_US1) {\n return PCI_INTAKE_HOST_US1;\n }\n if (internalAnalyticsSubdomain && site === INTAKE_SITE_US1) {\n return `${internalAnalyticsSubdomain}.${INTAKE_SITE_US1}`;\n }\n if (site === INTAKE_SITE_FED_STAGING) {\n return `http-intake.logs.${site}`;\n }\n const domainParts = site.split('.');\n const extension = domainParts.pop();\n return `browser-intake-${domainParts.join('-')}.${extension}`;\n}\n/**\n * Build parameters to be used for an intake request. Parameters should be re-built for each\n * request, as they change randomly.\n */\nfunction buildEndpointParameters({\n clientToken,\n internalAnalyticsSubdomain\n}, trackType, configurationTags, api, {\n retry,\n encoding\n}) {\n const tags = [`sdk_version:${\"6.6.2\"}`, `api:${api}`].concat(configurationTags);\n if (retry) {\n tags.push(`retry_count:${retry.count}`, `retry_after:${retry.lastFailureStatus}`);\n }\n const parameters = ['ddsource=browser', `ddtags=${encodeURIComponent(tags.join(','))}`, `dd-api-key=${clientToken}`, `dd-evp-origin-version=${encodeURIComponent(\"6.6.2\")}`, 'dd-evp-origin=browser', `dd-request-id=${generateUUID()}`];\n if (encoding) {\n parameters.push(`dd-evp-encoding=${encoding}`);\n }\n if (trackType === 'rum') {\n parameters.push(`batch_time=${timeStampNow()}`);\n }\n if (internalAnalyticsSubdomain) {\n parameters.reverse();\n }\n return parameters.join('&');\n}\n","import { DOCS_ORIGIN, MORE_DETAILS, display } from '../../tools/display';\nexport const TAG_SIZE_LIMIT = 200;\nexport function buildTags(configuration) {\n const {\n env,\n service,\n version,\n datacenter\n } = configuration;\n const tags = [];\n if (env) {\n tags.push(buildTag('env', env));\n }\n if (service) {\n tags.push(buildTag('service', service));\n }\n if (version) {\n tags.push(buildTag('version', version));\n }\n if (datacenter) {\n tags.push(buildTag('datacenter', datacenter));\n }\n return tags;\n}\nexport function buildTag(key, rawValue) {\n // See https://docs.datadoghq.com/getting_started/tagging/#defining-tags for tags syntax. Note\n // that the backend may not follow the exact same rules, so we only want to display an informal\n // warning.\n const valueSizeLimit = TAG_SIZE_LIMIT - key.length - 1;\n if (rawValue.length > valueSizeLimit || hasForbiddenCharacters(rawValue)) {\n display.warn(`${key} value doesn't meet tag requirements and will be sanitized. ${MORE_DETAILS} ${DOCS_ORIGIN}/getting_started/tagging/#defining-tags`);\n }\n // Let the backend do most of the sanitization, but still make sure multiple tags can't be crafted\n // by forging a value containing commas.\n const sanitizedValue = rawValue.replace(/,/g, '_');\n return `${key}:${sanitizedValue}`;\n}\nfunction hasForbiddenCharacters(rawValue) {\n // Unicode property escapes is not supported in all browsers, so we use a try/catch.\n // Todo: Remove the try/catch when dropping support for Chrome 63 and Firefox 67\n // see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape#browser_compatibility\n if (!supportUnicodePropertyEscapes()) {\n return false;\n }\n // We use the Unicode property escapes to match any character that is a letter including other languages like Chinese, Japanese, etc.\n // p{Ll} matches a lowercase letter.\n // p{Lo} matches a letter that is neither uppercase nor lowercase (ex: Japanese characters).\n // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape#unicode_property_escapes_vs._character_classes\n return new RegExp('[^\\\\p{Ll}\\\\p{Lo}0-9_:./-]', 'u').test(rawValue);\n}\nexport function supportUnicodePropertyEscapes() {\n try {\n new RegExp('[\\\\p{Ll}]', 'u');\n return true;\n } catch (_a) {\n return false;\n }\n}\n","import { createEndpointBuilder } from './endpointBuilder';\nimport { buildTags } from './tags';\nimport { INTAKE_SITE_US1, INTAKE_URL_PARAMETERS } from './intakeSites';\nexport function computeTransportConfiguration(initConfiguration) {\n const site = initConfiguration.site || INTAKE_SITE_US1;\n const tags = buildTags(initConfiguration);\n const endpointBuilders = computeEndpointBuilders(initConfiguration, tags);\n const replicaConfiguration = computeReplicaConfiguration(initConfiguration, tags);\n return {\n replica: replicaConfiguration,\n site,\n ...endpointBuilders\n };\n}\nfunction computeEndpointBuilders(initConfiguration, tags) {\n return {\n logsEndpointBuilder: createEndpointBuilder(initConfiguration, 'logs', tags),\n rumEndpointBuilder: createEndpointBuilder(initConfiguration, 'rum', tags),\n profilingEndpointBuilder: createEndpointBuilder(initConfiguration, 'profile', tags),\n sessionReplayEndpointBuilder: createEndpointBuilder(initConfiguration, 'replay', tags)\n };\n}\nfunction computeReplicaConfiguration(initConfiguration, tags) {\n if (!initConfiguration.replica) {\n return;\n }\n const replicaConfiguration = {\n ...initConfiguration,\n site: INTAKE_SITE_US1,\n clientToken: initConfiguration.replica.clientToken\n };\n const replicaEndpointBuilders = {\n logsEndpointBuilder: createEndpointBuilder(replicaConfiguration, 'logs', tags),\n rumEndpointBuilder: createEndpointBuilder(replicaConfiguration, 'rum', tags)\n };\n return {\n applicationId: initConfiguration.replica.applicationId,\n ...replicaEndpointBuilders\n };\n}\nexport function isIntakeUrl(url) {\n // check if tags is present in the query string\n return INTAKE_URL_PARAMETERS.every(param => url.includes(param));\n}\n","import { catchUserErrors } from '../../tools/catchUserErrors';\nimport { DOCS_ORIGIN, MORE_DETAILS, display } from '../../tools/display';\nimport { ONE_SECOND } from '../../tools/utils/timeUtils';\nimport { isPercentage } from '../../tools/utils/numberUtils';\nimport { ONE_KIBI_BYTE } from '../../tools/utils/byteUtils';\nimport { objectHasValue } from '../../tools/utils/objectUtils';\nimport { selectSessionStoreStrategyType } from '../session/sessionStore';\nimport { TrackingConsent } from '../trackingConsent';\nimport { computeTransportConfiguration } from './transportConfiguration';\nexport const DefaultPrivacyLevel = {\n ALLOW: 'allow',\n MASK: 'mask',\n MASK_USER_INPUT: 'mask-user-input'\n};\nexport const TraceContextInjection = {\n ALL: 'all',\n SAMPLED: 'sampled'\n};\nfunction isString(tag, tagName) {\n if (tag !== undefined && tag !== null && typeof tag !== 'string') {\n display.error(`${tagName} must be defined as a string`);\n return false;\n }\n return true;\n}\nfunction isDatadogSite(site) {\n if (site && typeof site === 'string' && !/(datadog|ddog|datad0g|dd0g)/.test(site)) {\n display.error(`Site should be a valid Datadog site. ${MORE_DETAILS} ${DOCS_ORIGIN}/getting_started/site/.`);\n return false;\n }\n return true;\n}\nexport function isSampleRate(sampleRate, name) {\n if (sampleRate !== undefined && !isPercentage(sampleRate)) {\n display.error(`${name} Sample Rate should be a number between 0 and 100`);\n return false;\n }\n return true;\n}\nexport function validateAndBuildConfiguration(initConfiguration) {\n var _a, _b, _c, _d, _e, _f;\n if (!initConfiguration || !initConfiguration.clientToken) {\n display.error('Client Token is not configured, we will not send any data.');\n return;\n }\n if (!isDatadogSite(initConfiguration.site) || !isSampleRate(initConfiguration.sessionSampleRate, 'Session') || !isSampleRate(initConfiguration.telemetrySampleRate, 'Telemetry') || !isSampleRate(initConfiguration.telemetryConfigurationSampleRate, 'Telemetry Configuration') || !isSampleRate(initConfiguration.telemetryUsageSampleRate, 'Telemetry Usage') || !isString(initConfiguration.version, 'Version') || !isString(initConfiguration.env, 'Env') || !isString(initConfiguration.service, 'Service')) {\n return;\n }\n if (initConfiguration.trackingConsent !== undefined && !objectHasValue(TrackingConsent, initConfiguration.trackingConsent)) {\n display.error('Tracking Consent should be either \"granted\" or \"not-granted\"');\n return;\n }\n return {\n beforeSend: initConfiguration.beforeSend && catchUserErrors(initConfiguration.beforeSend, 'beforeSend threw an error:'),\n sessionStoreStrategyType: selectSessionStoreStrategyType(initConfiguration),\n sessionSampleRate: (_a = initConfiguration.sessionSampleRate) !== null && _a !== void 0 ? _a : 100,\n telemetrySampleRate: (_b = initConfiguration.telemetrySampleRate) !== null && _b !== void 0 ? _b : 20,\n telemetryConfigurationSampleRate: (_c = initConfiguration.telemetryConfigurationSampleRate) !== null && _c !== void 0 ? _c : 5,\n telemetryUsageSampleRate: (_d = initConfiguration.telemetryUsageSampleRate) !== null && _d !== void 0 ? _d : 5,\n service: initConfiguration.service || undefined,\n silentMultipleInit: !!initConfiguration.silentMultipleInit,\n allowUntrustedEvents: !!initConfiguration.allowUntrustedEvents,\n trackingConsent: (_e = initConfiguration.trackingConsent) !== null && _e !== void 0 ? _e : TrackingConsent.GRANTED,\n trackAnonymousUser: (_f = initConfiguration.trackAnonymousUser) !== null && _f !== void 0 ? _f : true,\n storeContextsAcrossPages: !!initConfiguration.storeContextsAcrossPages,\n /**\n * beacon payload max queue size implementation is 64kb\n * ensure that we leave room for logs, rum and potential other users\n */\n batchBytesLimit: 16 * ONE_KIBI_BYTE,\n eventRateLimiterThreshold: 3000,\n maxTelemetryEventsPerPage: 15,\n /**\n * flush automatically, aim to be lower than ALB connection timeout\n * to maximize connection reuse.\n */\n flushTimeout: 30 * ONE_SECOND,\n /**\n * Logs intake limit\n */\n batchMessagesLimit: 50,\n messageBytesLimit: 256 * ONE_KIBI_BYTE,\n ...computeTransportConfiguration(initConfiguration)\n };\n}\nexport function serializeConfiguration(initConfiguration) {\n return {\n session_sample_rate: initConfiguration.sessionSampleRate,\n telemetry_sample_rate: initConfiguration.telemetrySampleRate,\n telemetry_configuration_sample_rate: initConfiguration.telemetryConfigurationSampleRate,\n telemetry_usage_sample_rate: initConfiguration.telemetryUsageSampleRate,\n use_before_send: !!initConfiguration.beforeSend,\n use_partitioned_cross_site_session_cookie: initConfiguration.usePartitionedCrossSiteSessionCookie,\n use_secure_session_cookie: initConfiguration.useSecureSessionCookie,\n use_proxy: !!initConfiguration.proxy,\n silent_multiple_init: initConfiguration.silentMultipleInit,\n track_session_across_subdomains: initConfiguration.trackSessionAcrossSubdomains,\n track_anonymous_user: initConfiguration.trackAnonymousUser,\n session_persistence: initConfiguration.sessionPersistence,\n allow_fallback_to_local_storage: !!initConfiguration.allowFallbackToLocalStorage,\n store_contexts_across_pages: !!initConfiguration.storeContextsAcrossPages,\n allow_untrusted_events: !!initConfiguration.allowUntrustedEvents,\n tracking_consent: initConfiguration.trackingConsent\n };\n}\n","export function sendToExtension(type, payload) {\n const callback = window.__ddBrowserSdkExtensionCallback;\n if (callback) {\n callback({\n type,\n payload\n });\n }\n}\n","/**\n * Similar to `typeof`, but distinguish plain objects from `null` and arrays\n */\nexport function getType(value) {\n if (value === null) {\n return 'null';\n }\n if (Array.isArray(value)) {\n return 'array';\n }\n return typeof value;\n}\n","import { getType } from './utils/typeUtils';\n/**\n * Iterate over source and affect its sub values into destination, recursively.\n * If the source and destination can't be merged, return source.\n */\nexport function mergeInto(destination, source, circularReferenceChecker = createCircularReferenceChecker()) {\n // ignore the source if it is undefined\n if (source === undefined) {\n return destination;\n }\n if (typeof source !== 'object' || source === null) {\n // primitive values - just return source\n return source;\n } else if (source instanceof Date) {\n return new Date(source.getTime());\n } else if (source instanceof RegExp) {\n const flags = source.flags ||\n // old browsers compatibility\n [source.global ? 'g' : '', source.ignoreCase ? 'i' : '', source.multiline ? 'm' : '', source.sticky ? 'y' : '', source.unicode ? 'u' : ''].join('');\n return new RegExp(source.source, flags);\n }\n if (circularReferenceChecker.hasAlreadyBeenSeen(source)) {\n // remove circular references\n return undefined;\n } else if (Array.isArray(source)) {\n const merged = Array.isArray(destination) ? destination : [];\n for (let i = 0; i < source.length; ++i) {\n merged[i] = mergeInto(merged[i], source[i], circularReferenceChecker);\n }\n return merged;\n }\n const merged = getType(destination) === 'object' ? destination : {};\n for (const key in source) {\n if (Object.prototype.hasOwnProperty.call(source, key)) {\n merged[key] = mergeInto(merged[key], source[key], circularReferenceChecker);\n }\n }\n return merged;\n}\n/**\n * A simplistic implementation of a deep clone algorithm.\n * Caveats:\n * - It doesn't maintain prototype chains - don't use with instances of custom classes.\n * - It doesn't handle Map and Set\n */\nexport function deepClone(value) {\n return mergeInto(undefined, value);\n}\nexport function combine(...sources) {\n let destination;\n for (const source of sources) {\n // Ignore any undefined or null sources.\n if (source === undefined || source === null) {\n continue;\n }\n destination = mergeInto(destination, source);\n }\n return destination;\n}\nfunction createCircularReferenceChecker() {\n if (typeof WeakSet !== 'undefined') {\n const set = new WeakSet();\n return {\n hasAlreadyBeenSeen(value) {\n const has = set.has(value);\n if (!has) {\n set.add(value);\n }\n return has;\n }\n };\n }\n const array = [];\n return {\n hasAlreadyBeenSeen(value) {\n const has = array.indexOf(value) >= 0;\n if (!has) {\n array.push(value);\n }\n return has;\n }\n };\n}\n","export function getConnectivity() {\n var _a;\n const navigator = window.navigator;\n return {\n status: navigator.onLine ? 'connected' : 'not_connected',\n interfaces: navigator.connection && navigator.connection.type ? [navigator.connection.type] : undefined,\n effective_type: (_a = navigator.connection) === null || _a === void 0 ? void 0 : _a.effectiveType\n };\n}\n","export function removeDuplicates(array) {\n const set = new Set();\n array.forEach(item => set.add(item));\n return Array.from(set);\n}\nexport function removeItem(array, item) {\n const index = array.indexOf(item);\n if (index >= 0) {\n array.splice(index, 1);\n }\n}\n","import { removeItem } from './utils/arrayUtils';\nconst BUFFER_LIMIT = 500;\nexport function createBoundedBuffer() {\n const buffer = [];\n const add = callback => {\n const length = buffer.push(callback);\n if (length > BUFFER_LIMIT) {\n buffer.splice(0, 1);\n }\n };\n const remove = callback => {\n removeItem(buffer, callback);\n };\n const drain = arg => {\n buffer.forEach(callback => callback(arg));\n buffer.length = 0;\n };\n return {\n add,\n remove,\n drain\n };\n}\n","export const TelemetryType = {\n log: 'log',\n configuration: 'configuration',\n usage: 'usage'\n};\n","import { ConsoleApiName } from '../../tools/display';\nimport { NO_ERROR_STACK_PRESENT_MESSAGE, isError } from '../error/error';\nimport { toStackTraceString } from '../../tools/stackTrace/handlingStack';\nimport { getExperimentalFeatures } from '../../tools/experimentalFeatures';\nimport { INTAKE_SITE_STAGING, INTAKE_SITE_US1_FED } from '../configuration';\nimport { Observable } from '../../tools/observable';\nimport { timeStampNow } from '../../tools/utils/timeUtils';\nimport { displayIfDebugEnabled, startMonitorErrorCollection } from '../../tools/monitor';\nimport { sendToExtension } from '../../tools/sendToExtension';\nimport { performDraw } from '../../tools/utils/numberUtils';\nimport { jsonStringify } from '../../tools/serialisation/jsonStringify';\nimport { combine } from '../../tools/mergeInto';\nimport { computeStackTrace } from '../../tools/stackTrace/computeStackTrace';\nimport { getConnectivity } from '../connectivity';\nimport { createBoundedBuffer } from '../../tools/boundedBuffer';\nimport { TelemetryType } from './rawTelemetryEvent.types';\nconst ALLOWED_FRAME_URLS = ['https://www.datadoghq-browser-agent.com', 'https://www.datad0g-browser-agent.com', 'https://d3uc069fcn7uxw.cloudfront.net', 'https://d20xtzwzcl0ceb.cloudfront.net', 'http://localhost', ''];\nconst TELEMETRY_EXCLUDED_SITES = [INTAKE_SITE_US1_FED];\n// eslint-disable-next-line local-rules/disallow-side-effects\nlet preStartTelemetryBuffer = createBoundedBuffer();\nlet onRawTelemetryEventCollected = event => {\n preStartTelemetryBuffer.add(() => onRawTelemetryEventCollected(event));\n};\nexport function startTelemetry(telemetryService, configuration) {\n let contextProvider;\n const observable = new Observable();\n const alreadySentEvents = new Set();\n const telemetryEnabled = !TELEMETRY_EXCLUDED_SITES.includes(configuration.site) && performDraw(configuration.telemetrySampleRate);\n const telemetryEnabledPerType = {\n [TelemetryType.log]: telemetryEnabled,\n [TelemetryType.configuration]: telemetryEnabled && performDraw(configuration.telemetryConfigurationSampleRate),\n [TelemetryType.usage]: telemetryEnabled && performDraw(configuration.telemetryUsageSampleRate)\n };\n const runtimeEnvInfo = getRuntimeEnvInfo();\n onRawTelemetryEventCollected = rawEvent => {\n const stringifiedEvent = jsonStringify(rawEvent);\n if (telemetryEnabledPerType[rawEvent.type] && alreadySentEvents.size < configuration.maxTelemetryEventsPerPage && !alreadySentEvents.has(stringifiedEvent)) {\n const event = toTelemetryEvent(telemetryService, rawEvent, runtimeEnvInfo);\n observable.notify(event);\n sendToExtension('telemetry', event);\n alreadySentEvents.add(stringifiedEvent);\n }\n };\n startMonitorErrorCollection(addTelemetryError);\n function toTelemetryEvent(telemetryService, event, runtimeEnvInfo) {\n return combine({\n type: 'telemetry',\n date: timeStampNow(),\n service: telemetryService,\n version: \"6.6.2\",\n source: 'browser',\n _dd: {\n format_version: 2\n },\n telemetry: combine(event, {\n runtime_env: runtimeEnvInfo,\n connectivity: getConnectivity(),\n sdk_setup: \"npm\"\n }),\n experimental_features: Array.from(getExperimentalFeatures())\n }, contextProvider !== undefined ? contextProvider() : {});\n }\n return {\n setContextProvider: provider => {\n contextProvider = provider;\n },\n observable,\n enabled: telemetryEnabled\n };\n}\nfunction getRuntimeEnvInfo() {\n return {\n is_local_file: window.location.protocol === 'file:',\n is_worker: 'WorkerGlobalScope' in self\n };\n}\nexport function startFakeTelemetry() {\n const events = [];\n onRawTelemetryEventCollected = event => {\n events.push(event);\n };\n return events;\n}\n// need to be called after telemetry context is provided and observers are registered\nexport function drainPreStartTelemetry() {\n preStartTelemetryBuffer.drain();\n}\nexport function resetTelemetry() {\n preStartTelemetryBuffer = createBoundedBuffer();\n onRawTelemetryEventCollected = event => {\n preStartTelemetryBuffer.add(() => onRawTelemetryEventCollected(event));\n };\n}\n/**\n * Avoid mixing telemetry events from different data centers\n * but keep replicating staging events for reliability\n */\nexport function isTelemetryReplicationAllowed(configuration) {\n return configuration.site === INTAKE_SITE_STAGING;\n}\nexport function addTelemetryDebug(message, context) {\n displayIfDebugEnabled(ConsoleApiName.debug, message, context);\n onRawTelemetryEventCollected({\n type: TelemetryType.log,\n message,\n status: \"debug\" /* StatusType.debug */,\n ...context\n });\n}\nexport function addTelemetryError(e, context) {\n onRawTelemetryEventCollected({\n type: TelemetryType.log,\n status: \"error\" /* StatusType.error */,\n ...formatError(e),\n ...context\n });\n}\nexport function addTelemetryConfiguration(configuration) {\n onRawTelemetryEventCollected({\n type: TelemetryType.configuration,\n configuration\n });\n}\nexport function addTelemetryUsage(usage) {\n onRawTelemetryEventCollected({\n type: TelemetryType.usage,\n usage\n });\n}\nexport function formatError(e) {\n if (isError(e)) {\n const stackTrace = computeStackTrace(e);\n return {\n error: {\n kind: stackTrace.name,\n stack: toStackTraceString(scrubCustomerFrames(stackTrace))\n },\n message: stackTrace.message\n };\n }\n return {\n error: {\n stack: NO_ERROR_STACK_PRESENT_MESSAGE\n },\n message: `${\"Uncaught\" /* NonErrorPrefix.UNCAUGHT */} ${jsonStringify(e)}`\n };\n}\nexport function scrubCustomerFrames(stackTrace) {\n stackTrace.stack = stackTrace.stack.filter(frame => !frame.url || ALLOWED_FRAME_URLS.some(allowedFrameUrl => frame.url.startsWith(allowedFrameUrl)));\n return stackTrace;\n}\n","export function isServerError(status) {\n return status >= 500;\n}\nexport function tryToClone(response) {\n try {\n return response.clone();\n } catch (_a) {\n // clone can throw if the response has already been used by another instrumentation or is disturbed\n return;\n }\n}\n","export const ErrorSource = {\n AGENT: 'agent',\n CONSOLE: 'console',\n CUSTOM: 'custom',\n LOGGER: 'logger',\n NETWORK: 'network',\n SOURCE: 'source',\n REPORT: 'report'\n};\n","import { setTimeout } from '../tools/timer';\nimport { clocksNow, ONE_MINUTE, ONE_SECOND } from '../tools/utils/timeUtils';\nimport { ONE_MEBI_BYTE, ONE_KIBI_BYTE } from '../tools/utils/byteUtils';\nimport { isServerError } from '../tools/utils/responseUtils';\nimport { ErrorSource } from '../domain/error/error.types';\nexport const MAX_ONGOING_BYTES_COUNT = 80 * ONE_KIBI_BYTE;\nexport const MAX_ONGOING_REQUESTS = 32;\nexport const MAX_QUEUE_BYTES_COUNT = 3 * ONE_MEBI_BYTE;\nexport const MAX_BACKOFF_TIME = ONE_MINUTE;\nexport const INITIAL_BACKOFF_TIME = ONE_SECOND;\nexport function sendWithRetryStrategy(payload, state, sendStrategy, trackType, reportError) {\n if (state.transportStatus === 0 /* TransportStatus.UP */ && state.queuedPayloads.size() === 0 && state.bandwidthMonitor.canHandle(payload)) {\n send(payload, state, sendStrategy, {\n onSuccess: () => retryQueuedPayloads(0 /* RetryReason.AFTER_SUCCESS */, state, sendStrategy, trackType, reportError),\n onFailure: () => {\n state.queuedPayloads.enqueue(payload);\n scheduleRetry(state, sendStrategy, trackType, reportError);\n }\n });\n } else {\n state.queuedPayloads.enqueue(payload);\n }\n}\nfunction scheduleRetry(state, sendStrategy, trackType, reportError) {\n if (state.transportStatus !== 2 /* TransportStatus.DOWN */) {\n return;\n }\n setTimeout(() => {\n const payload = state.queuedPayloads.first();\n send(payload, state, sendStrategy, {\n onSuccess: () => {\n state.queuedPayloads.dequeue();\n state.currentBackoffTime = INITIAL_BACKOFF_TIME;\n retryQueuedPayloads(1 /* RetryReason.AFTER_RESUME */, state, sendStrategy, trackType, reportError);\n },\n onFailure: () => {\n state.currentBackoffTime = Math.min(MAX_BACKOFF_TIME, state.currentBackoffTime * 2);\n scheduleRetry(state, sendStrategy, trackType, reportError);\n }\n });\n }, state.currentBackoffTime);\n}\nfunction send(payload, state, sendStrategy, {\n onSuccess,\n onFailure\n}) {\n state.bandwidthMonitor.add(payload);\n sendStrategy(payload, response => {\n state.bandwidthMonitor.remove(payload);\n if (!shouldRetryRequest(response)) {\n state.transportStatus = 0 /* TransportStatus.UP */;\n onSuccess();\n } else {\n // do not consider transport down if another ongoing request could succeed\n state.transportStatus = state.bandwidthMonitor.ongoingRequestCount > 0 ? 1 /* TransportStatus.FAILURE_DETECTED */ : 2 /* TransportStatus.DOWN */;\n payload.retry = {\n count: payload.retry ? payload.retry.count + 1 : 1,\n lastFailureStatus: response.status\n };\n onFailure();\n }\n });\n}\nfunction retryQueuedPayloads(reason, state, sendStrategy, trackType, reportError) {\n if (reason === 0 /* RetryReason.AFTER_SUCCESS */ && state.queuedPayloads.isFull() && !state.queueFullReported) {\n reportError({\n message: `Reached max ${trackType} events size queued for upload: ${MAX_QUEUE_BYTES_COUNT / ONE_MEBI_BYTE}MiB`,\n source: ErrorSource.AGENT,\n startClocks: clocksNow()\n });\n state.queueFullReported = true;\n }\n const previousQueue = state.queuedPayloads;\n state.queuedPayloads = newPayloadQueue();\n while (previousQueue.size() > 0) {\n sendWithRetryStrategy(previousQueue.dequeue(), state, sendStrategy, trackType, reportError);\n }\n}\nfunction shouldRetryRequest(response) {\n return response.type !== 'opaque' && (response.status === 0 && !navigator.onLine || response.status === 408 || response.status === 429 || isServerError(response.status));\n}\nexport function newRetryState() {\n return {\n transportStatus: 0 /* TransportStatus.UP */,\n currentBackoffTime: INITIAL_BACKOFF_TIME,\n bandwidthMonitor: newBandwidthMonitor(),\n queuedPayloads: newPayloadQueue(),\n queueFullReported: false\n };\n}\nfunction newPayloadQueue() {\n const queue = [];\n return {\n bytesCount: 0,\n enqueue(payload) {\n if (this.isFull()) {\n return;\n }\n queue.push(payload);\n this.bytesCount += payload.bytesCount;\n },\n first() {\n return queue[0];\n },\n dequeue() {\n const payload = queue.shift();\n if (payload) {\n this.bytesCount -= payload.bytesCount;\n }\n return payload;\n },\n size() {\n return queue.length;\n },\n isFull() {\n return this.bytesCount >= MAX_QUEUE_BYTES_COUNT;\n }\n };\n}\nfunction newBandwidthMonitor() {\n return {\n ongoingRequestCount: 0,\n ongoingByteCount: 0,\n canHandle(payload) {\n return this.ongoingRequestCount === 0 || this.ongoingByteCount + payload.bytesCount <= MAX_ONGOING_BYTES_COUNT && this.ongoingRequestCount < MAX_ONGOING_REQUESTS;\n },\n add(payload) {\n this.ongoingRequestCount += 1;\n this.ongoingByteCount += payload.bytesCount;\n },\n remove(payload) {\n this.ongoingRequestCount -= 1;\n this.ongoingByteCount -= payload.bytesCount;\n }\n };\n}\n","import { monitor, monitorError } from '../tools/monitor';\nimport { newRetryState, sendWithRetryStrategy } from './sendWithRetryStrategy';\nexport function createHttpRequest(endpointBuilder, bytesLimit, reportError) {\n const retryState = newRetryState();\n const sendStrategyForRetry = (payload, onResponse) => fetchKeepAliveStrategy(endpointBuilder, bytesLimit, payload, onResponse);\n return {\n send: payload => {\n sendWithRetryStrategy(payload, retryState, sendStrategyForRetry, endpointBuilder.trackType, reportError);\n },\n /**\n * Since fetch keepalive behaves like regular fetch on Firefox,\n * keep using sendBeaconStrategy on exit\n */\n sendOnExit: payload => {\n sendBeaconStrategy(endpointBuilder, bytesLimit, payload);\n }\n };\n}\nfunction sendBeaconStrategy(endpointBuilder, bytesLimit, payload) {\n const canUseBeacon = !!navigator.sendBeacon && payload.bytesCount < bytesLimit;\n if (canUseBeacon) {\n try {\n const beaconUrl = endpointBuilder.build('beacon', payload);\n const isQueued = navigator.sendBeacon(beaconUrl, payload.data);\n if (isQueued) {\n return;\n }\n } catch (e) {\n reportBeaconError(e);\n }\n }\n fetchStrategy(endpointBuilder, payload);\n}\nlet hasReportedBeaconError = false;\nfunction reportBeaconError(e) {\n if (!hasReportedBeaconError) {\n hasReportedBeaconError = true;\n monitorError(e);\n }\n}\nexport function fetchKeepAliveStrategy(endpointBuilder, bytesLimit, payload, onResponse) {\n const canUseKeepAlive = isKeepAliveSupported() && payload.bytesCount < bytesLimit;\n if (canUseKeepAlive) {\n const fetchUrl = endpointBuilder.build('fetch-keepalive', payload);\n fetch(fetchUrl, {\n method: 'POST',\n body: payload.data,\n keepalive: true,\n mode: 'cors'\n }).then(monitor(response => onResponse === null || onResponse === void 0 ? void 0 : onResponse({\n status: response.status,\n type: response.type\n }))).catch(monitor(() => fetchStrategy(endpointBuilder, payload, onResponse)));\n } else {\n fetchStrategy(endpointBuilder, payload, onResponse);\n }\n}\nexport function fetchStrategy(endpointBuilder, payload, onResponse) {\n const fetchUrl = endpointBuilder.build('fetch', payload);\n fetch(fetchUrl, {\n method: 'POST',\n body: payload.data,\n mode: 'cors'\n }).then(monitor(response => onResponse === null || onResponse === void 0 ? void 0 : onResponse({\n status: response.status,\n type: response.type\n }))).catch(monitor(() => onResponse === null || onResponse === void 0 ? void 0 : onResponse({\n status: 0\n })));\n}\nfunction isKeepAliveSupported() {\n // Request can throw, cf https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#errors\n try {\n return window.Request && 'keepalive' in new Request('http://a');\n } catch (_a) {\n return false;\n }\n}\n","import { getGlobalObject } from '../tools/getGlobalObject';\nexport function getEventBridge() {\n const eventBridgeGlobal = getEventBridgeGlobal();\n if (!eventBridgeGlobal) {\n return;\n }\n return {\n getCapabilities() {\n var _a;\n return JSON.parse(((_a = eventBridgeGlobal.getCapabilities) === null || _a === void 0 ? void 0 : _a.call(eventBridgeGlobal)) || '[]');\n },\n getPrivacyLevel() {\n var _a;\n return (_a = eventBridgeGlobal.getPrivacyLevel) === null || _a === void 0 ? void 0 : _a.call(eventBridgeGlobal);\n },\n getAllowedWebViewHosts() {\n return JSON.parse(eventBridgeGlobal.getAllowedWebViewHosts());\n },\n send(eventType, event, viewId) {\n const view = viewId ? {\n id: viewId\n } : undefined;\n eventBridgeGlobal.send(JSON.stringify({\n eventType,\n event,\n view\n }));\n }\n };\n}\nexport function bridgeSupports(capability) {\n const bridge = getEventBridge();\n return !!bridge && bridge.getCapabilities().includes(capability);\n}\nexport function canUseEventBridge(currentHost) {\n var _a;\n if (currentHost === void 0) {\n currentHost = (_a = getGlobalObject().location) === null || _a === void 0 ? void 0 : _a.hostname;\n }\n const bridge = getEventBridge();\n return !!bridge && bridge.getAllowedWebViewHosts().some(allowedHost => currentHost === allowedHost || currentHost.endsWith(`.${allowedHost}`));\n}\nfunction getEventBridgeGlobal() {\n return getGlobalObject().DatadogEventBridge;\n}\n","import { Observable } from '../tools/observable';\nimport { objectValues } from '../tools/utils/polyfills';\nimport { addEventListeners, addEventListener } from './addEventListener';\nexport const PageExitReason = {\n HIDDEN: 'visibility_hidden',\n UNLOADING: 'before_unload',\n PAGEHIDE: 'page_hide',\n FROZEN: 'page_frozen'\n};\nexport function createPageMayExitObservable(configuration) {\n return new Observable(observable => {\n const {\n stop: stopListeners\n } = addEventListeners(configuration, window, [\"visibilitychange\" /* DOM_EVENT.VISIBILITY_CHANGE */, \"freeze\" /* DOM_EVENT.FREEZE */], event => {\n if (event.type === \"visibilitychange\" /* DOM_EVENT.VISIBILITY_CHANGE */ && document.visibilityState === 'hidden') {\n /**\n * Only event that guarantee to fire on mobile devices when the page transitions to background state\n * (e.g. when user switches to a different application, goes to homescreen, etc), or is being unloaded.\n */\n observable.notify({\n reason: PageExitReason.HIDDEN\n });\n } else if (event.type === \"freeze\" /* DOM_EVENT.FREEZE */) {\n /**\n * After transitioning in background a tab can be freezed to preserve resources. (cf: https://developer.chrome.com/blog/page-lifecycle-api)\n * Allow to collect events happening between hidden and frozen state.\n */\n observable.notify({\n reason: PageExitReason.FROZEN\n });\n }\n }, {\n capture: true\n });\n const stopBeforeUnloadListener = addEventListener(configuration, window, \"beforeunload\" /* DOM_EVENT.BEFORE_UNLOAD */, () => {\n observable.notify({\n reason: PageExitReason.UNLOADING\n });\n }).stop;\n return () => {\n stopListeners();\n stopBeforeUnloadListener();\n };\n });\n}\nexport function isPageExitReason(reason) {\n return objectValues(PageExitReason).includes(reason);\n}\n","import { DOCS_TROUBLESHOOTING, MORE_DETAILS, display } from '../tools/display';\nimport { objectValues } from '../tools/utils/polyfills';\nimport { isPageExitReason } from '../browser/pageMayExitObservable';\nimport { jsonStringify } from '../tools/serialisation/jsonStringify';\nimport { computeBytesCount } from '../tools/utils/byteUtils';\nexport function createBatch({\n encoder,\n request,\n flushController,\n messageBytesLimit\n}) {\n let upsertBuffer = {};\n const flushSubscription = flushController.flushObservable.subscribe(event => flush(event));\n function push(serializedMessage, estimatedMessageBytesCount, key) {\n flushController.notifyBeforeAddMessage(estimatedMessageBytesCount);\n if (key !== undefined) {\n upsertBuffer[key] = serializedMessage;\n flushController.notifyAfterAddMessage();\n } else {\n encoder.write(encoder.isEmpty ? serializedMessage : `\\n${serializedMessage}`, realMessageBytesCount => {\n flushController.notifyAfterAddMessage(realMessageBytesCount - estimatedMessageBytesCount);\n });\n }\n }\n function hasMessageFor(key) {\n return key !== undefined && upsertBuffer[key] !== undefined;\n }\n function remove(key) {\n const removedMessage = upsertBuffer[key];\n delete upsertBuffer[key];\n const messageBytesCount = encoder.estimateEncodedBytesCount(removedMessage);\n flushController.notifyAfterRemoveMessage(messageBytesCount);\n }\n function addOrUpdate(message, key) {\n const serializedMessage = jsonStringify(message);\n const estimatedMessageBytesCount = encoder.estimateEncodedBytesCount(serializedMessage);\n if (estimatedMessageBytesCount >= messageBytesLimit) {\n display.warn(`Discarded a message whose size was bigger than the maximum allowed size ${messageBytesLimit}KB. ${MORE_DETAILS} ${DOCS_TROUBLESHOOTING}/#technical-limitations`);\n return;\n }\n if (hasMessageFor(key)) {\n remove(key);\n }\n push(serializedMessage, estimatedMessageBytesCount, key);\n }\n function flush(event) {\n const upsertMessages = objectValues(upsertBuffer).join('\\n');\n upsertBuffer = {};\n const pageMightExit = isPageExitReason(event.reason);\n const send = pageMightExit ? request.sendOnExit : request.send;\n if (pageMightExit &&\n // Note: checking that the encoder is async is not strictly needed, but it's an optimization:\n // if the encoder is async we need to send two requests in some cases (one for encoded data\n // and the other for non-encoded data). But if it's not async, we don't have to worry about\n // it and always send a single request.\n encoder.isAsync) {\n const encoderResult = encoder.finishSync();\n // Send encoded messages\n if (encoderResult.outputBytesCount) {\n send(formatPayloadFromEncoder(encoderResult));\n }\n // Send messages that are not yet encoded at this point\n const pendingMessages = [encoderResult.pendingData, upsertMessages].filter(Boolean).join('\\n');\n if (pendingMessages) {\n send({\n data: pendingMessages,\n bytesCount: computeBytesCount(pendingMessages)\n });\n }\n } else {\n if (upsertMessages) {\n encoder.write(encoder.isEmpty ? upsertMessages : `\\n${upsertMessages}`);\n }\n encoder.finish(encoderResult => {\n send(formatPayloadFromEncoder(encoderResult));\n });\n }\n }\n return {\n flushController,\n add: addOrUpdate,\n upsert: addOrUpdate,\n stop: flushSubscription.unsubscribe\n };\n}\nfunction formatPayloadFromEncoder(encoderResult) {\n let data;\n if (typeof encoderResult.output === 'string') {\n data = encoderResult.output;\n } else {\n data = new Blob([encoderResult.output], {\n // This will set the 'Content-Type: text/plain' header. Reasoning:\n // * The intake rejects the request if there is no content type.\n // * The browser will issue CORS preflight requests if we set it to 'application/json', which\n // could induce higher intake load (and maybe has other impacts).\n // * Also it's not quite JSON, since we are concatenating multiple JSON objects separated by\n // new lines.\n type: 'text/plain'\n });\n }\n return {\n data,\n bytesCount: encoderResult.outputBytesCount,\n encoding: encoderResult.encoding\n };\n}\n","import { Observable } from '../tools/observable';\nimport { clearTimeout, setTimeout } from '../tools/timer';\n/**\n * Returns a \"flush controller\", responsible of notifying when flushing a pool of pending data needs\n * to happen. The implementation is designed to support both synchronous and asynchronous usages,\n * but relies on invariants described in each method documentation to keep a coherent state.\n */\nexport function createFlushController({\n messagesLimit,\n bytesLimit,\n durationLimit,\n pageMayExitObservable,\n sessionExpireObservable\n}) {\n const pageMayExitSubscription = pageMayExitObservable.subscribe(event => flush(event.reason));\n const sessionExpireSubscription = sessionExpireObservable.subscribe(() => flush('session_expire'));\n const flushObservable = new Observable(() => () => {\n pageMayExitSubscription.unsubscribe();\n sessionExpireSubscription.unsubscribe();\n });\n let currentBytesCount = 0;\n let currentMessagesCount = 0;\n function flush(flushReason) {\n if (currentMessagesCount === 0) {\n return;\n }\n const messagesCount = currentMessagesCount;\n const bytesCount = currentBytesCount;\n currentMessagesCount = 0;\n currentBytesCount = 0;\n cancelDurationLimitTimeout();\n flushObservable.notify({\n reason: flushReason,\n messagesCount,\n bytesCount\n });\n }\n let durationLimitTimeoutId;\n function scheduleDurationLimitTimeout() {\n if (durationLimitTimeoutId === undefined) {\n durationLimitTimeoutId = setTimeout(() => {\n flush('duration_limit');\n }, durationLimit);\n }\n }\n function cancelDurationLimitTimeout() {\n clearTimeout(durationLimitTimeoutId);\n durationLimitTimeoutId = undefined;\n }\n return {\n flushObservable,\n get messagesCount() {\n return currentMessagesCount;\n },\n /**\n * Notifies that a message will be added to a pool of pending messages waiting to be flushed.\n *\n * This function needs to be called synchronously, right before adding the message, so no flush\n * event can happen after `notifyBeforeAddMessage` and before adding the message.\n *\n * @param estimatedMessageBytesCount: an estimation of the message bytes count once it is\n * actually added.\n */\n notifyBeforeAddMessage(estimatedMessageBytesCount) {\n if (currentBytesCount + estimatedMessageBytesCount >= bytesLimit) {\n flush('bytes_limit');\n }\n // Consider the message to be added now rather than in `notifyAfterAddMessage`, because if no\n // message was added yet and `notifyAfterAddMessage` is called asynchronously, we still want\n // to notify when a flush is needed (for example on page exit).\n currentMessagesCount += 1;\n currentBytesCount += estimatedMessageBytesCount;\n scheduleDurationLimitTimeout();\n },\n /**\n * Notifies that a message *was* added to a pool of pending messages waiting to be flushed.\n *\n * This function can be called asynchronously after the message was added, but in this case it\n * should not be called if a flush event occurred in between.\n *\n * @param messageBytesCountDiff: the difference between the estimated message bytes count and\n * its actual bytes count once added to the pool.\n */\n notifyAfterAddMessage(messageBytesCountDiff = 0) {\n currentBytesCount += messageBytesCountDiff;\n if (currentMessagesCount >= messagesLimit) {\n flush('messages_limit');\n } else if (currentBytesCount >= bytesLimit) {\n flush('bytes_limit');\n }\n },\n /**\n * Notifies that a message was removed from a pool of pending messages waiting to be flushed.\n *\n * This function needs to be called synchronously, right after removing the message, so no flush\n * event can happen after removing the message and before `notifyAfterRemoveMessage`.\n *\n * @param messageBytesCount: the message bytes count that was added to the pool. Should\n * correspond to the sum of bytes counts passed to `notifyBeforeAddMessage` and\n * `notifyAfterAddMessage`.\n */\n notifyAfterRemoveMessage(messageBytesCount) {\n currentBytesCount -= messageBytesCount;\n currentMessagesCount -= 1;\n if (currentMessagesCount === 0) {\n cancelDurationLimitTimeout();\n }\n }\n };\n}\n","import { createBatch } from './batch';\nimport { createHttpRequest } from './httpRequest';\nimport { createFlushController } from './flushController';\nexport function startBatchWithReplica(configuration, primary, replica, reportError, pageMayExitObservable, sessionExpireObservable, batchFactoryImp = createBatch) {\n const primaryBatch = createBatchFromConfig(configuration, primary);\n const replicaBatch = replica && createBatchFromConfig(configuration, replica);\n function createBatchFromConfig(configuration, {\n endpoint,\n encoder\n }) {\n return batchFactoryImp({\n encoder,\n request: createHttpRequest(endpoint, configuration.batchBytesLimit, reportError),\n flushController: createFlushController({\n messagesLimit: configuration.batchMessagesLimit,\n bytesLimit: configuration.batchBytesLimit,\n durationLimit: configuration.flushTimeout,\n pageMayExitObservable,\n sessionExpireObservable\n }),\n messageBytesLimit: configuration.messageBytesLimit\n });\n }\n return {\n flushObservable: primaryBatch.flushController.flushObservable,\n add(message, replicated = true) {\n primaryBatch.add(message);\n if (replicaBatch && replicated) {\n replicaBatch.add(replica.transformMessage ? replica.transformMessage(message) : message);\n }\n },\n upsert: (message, key) => {\n primaryBatch.upsert(message, key);\n if (replicaBatch) {\n replicaBatch.upsert(replica.transformMessage ? replica.transformMessage(message) : message, key);\n }\n },\n stop: () => {\n primaryBatch.stop();\n if (replicaBatch) {\n replicaBatch.stop();\n }\n }\n };\n}\n","import { setTimeout } from './timer';\nimport { callMonitored } from './monitor';\nimport { noop } from './utils/functionUtils';\nimport { createHandlingStack } from './stackTrace/handlingStack';\n/**\n * Instruments a method on a object, calling the given callback before the original method is\n * invoked. The callback receives an object with information about the method call.\n *\n * This function makes sure that we are \"good citizens\" regarding third party instrumentations: when\n * removing the instrumentation, the original method is usually restored, but if a third party\n * instrumentation was set after ours, we keep it in place and just replace our instrumentation with\n * a noop.\n *\n * Note: it is generally better to instrument methods that are \"owned\" by the object instead of ones\n * that are inherited from the prototype chain. Example:\n * * do: `instrumentMethod(Array.prototype, 'push', ...)`\n * * don't: `instrumentMethod([], 'push', ...)`\n *\n * This method is also used to set event handler properties (ex: window.onerror = ...), as it has\n * the same requirements as instrumenting a method:\n * * if the event handler is already set by a third party, we need to call it and not just blindly\n * override it.\n * * if the event handler is set by a third party after us, we need to keep it in place when\n * removing ours.\n *\n * @example\n *\n * instrumentMethod(window, 'fetch', ({ target, parameters, onPostCall }) => {\n * console.log('Before calling fetch on', target, 'with parameters', parameters)\n *\n * onPostCall((result) => {\n * console.log('After fetch calling on', target, 'with parameters', parameters, 'and result', result)\n * })\n * })\n */\nexport function instrumentMethod(targetPrototype, method, onPreCall, {\n computeHandlingStack\n} = {}) {\n let original = targetPrototype[method];\n if (typeof original !== 'function') {\n if (method in targetPrototype && method.startsWith('on')) {\n original = noop;\n } else {\n return {\n stop: noop\n };\n }\n }\n let stopped = false;\n const instrumentation = function () {\n if (stopped) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call\n return original.apply(this, arguments);\n }\n const parameters = Array.from(arguments);\n let postCallCallback;\n callMonitored(onPreCall, null, [{\n target: this,\n parameters,\n onPostCall: callback => {\n postCallCallback = callback;\n },\n handlingStack: computeHandlingStack ? createHandlingStack('instrumented method') : undefined\n }]);\n // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n const result = original.apply(this, parameters);\n if (postCallCallback) {\n callMonitored(postCallCallback, null, [result]);\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return result;\n };\n targetPrototype[method] = instrumentation;\n return {\n stop: () => {\n stopped = true;\n // If the instrumentation has been removed by a third party, keep the last one\n if (targetPrototype[method] === instrumentation) {\n targetPrototype[method] = original;\n }\n }\n };\n}\nexport function instrumentSetter(targetPrototype, property, after) {\n const originalDescriptor = Object.getOwnPropertyDescriptor(targetPrototype, property);\n if (!originalDescriptor || !originalDescriptor.set || !originalDescriptor.configurable) {\n return {\n stop: noop\n };\n }\n const stoppedInstrumentation = noop;\n let instrumentation = (target, value) => {\n // put hooked setter into event loop to avoid of set latency\n setTimeout(() => {\n if (instrumentation !== stoppedInstrumentation) {\n after(target, value);\n }\n }, 0);\n };\n const instrumentationWrapper = function (value) {\n originalDescriptor.set.call(this, value);\n instrumentation(this, value);\n };\n Object.defineProperty(targetPrototype, property, {\n set: instrumentationWrapper\n });\n return {\n stop: () => {\n var _a;\n if (((_a = Object.getOwnPropertyDescriptor(targetPrototype, property)) === null || _a === void 0 ? void 0 : _a.set) === instrumentationWrapper) {\n Object.defineProperty(targetPrototype, property, originalDescriptor);\n }\n instrumentation = stoppedInstrumentation;\n }\n };\n}\n","import { instrumentMethod } from '../../tools/instrumentMethod';\nimport { clocksNow } from '../../tools/utils/timeUtils';\nimport { computeStackTrace, computeStackTraceFromOnErrorMessage } from '../../tools/stackTrace/computeStackTrace';\nimport { computeRawError, isError } from './error';\nimport { ErrorSource } from './error.types';\nexport function trackRuntimeError(errorObservable) {\n const handleRuntimeError = (stackTrace, originalError) => {\n const rawError = computeRawError({\n stackTrace,\n originalError,\n startClocks: clocksNow(),\n nonErrorPrefix: \"Uncaught\" /* NonErrorPrefix.UNCAUGHT */,\n source: ErrorSource.SOURCE,\n handling: \"unhandled\" /* ErrorHandling.UNHANDLED */\n });\n errorObservable.notify(rawError);\n };\n const {\n stop: stopInstrumentingOnError\n } = instrumentOnError(handleRuntimeError);\n const {\n stop: stopInstrumentingOnUnhandledRejection\n } = instrumentUnhandledRejection(handleRuntimeError);\n return {\n stop: () => {\n stopInstrumentingOnError();\n stopInstrumentingOnUnhandledRejection();\n }\n };\n}\nexport function instrumentOnError(callback) {\n return instrumentMethod(window, 'onerror', ({\n parameters: [messageObj, url, line, column, errorObj]\n }) => {\n let stackTrace;\n if (isError(errorObj)) {\n stackTrace = computeStackTrace(errorObj);\n } else {\n stackTrace = computeStackTraceFromOnErrorMessage(messageObj, url, line, column);\n }\n callback(stackTrace, errorObj !== null && errorObj !== void 0 ? errorObj : messageObj);\n });\n}\nexport function instrumentUnhandledRejection(callback) {\n return instrumentMethod(window, 'onunhandledrejection', ({\n parameters: [e]\n }) => {\n const reason = e.reason || 'Empty reason';\n const stack = computeStackTrace(reason);\n callback(stack, reason);\n });\n}\n","import { catchUserErrors } from '../tools/catchUserErrors';\nimport { setDebugMode } from '../tools/monitor';\nimport { display } from '../tools/display';\nexport function makePublicApi(stub) {\n const publicApi = {\n version: \"6.6.2\",\n // This API method is intentionally not monitored, since the only thing executed is the\n // user-provided 'callback'. All SDK usages executed in the callback should be monitored, and\n // we don't want to interfere with the user uncaught exceptions.\n onReady(callback) {\n callback();\n },\n ...stub\n };\n // Add a \"hidden\" property to set debug mode. We define it that way to hide it\n // as much as possible but of course it's not a real protection.\n Object.defineProperty(publicApi, '_setDebug', {\n get() {\n return setDebugMode;\n },\n enumerable: false\n });\n return publicApi;\n}\nexport function defineGlobal(global, name, api) {\n const existingGlobalVariable = global[name];\n if (existingGlobalVariable && !existingGlobalVariable.q && existingGlobalVariable.version) {\n display.warn('SDK is loaded more than once. This is unsupported and might have unexpected behavior.');\n }\n global[name] = api;\n if (existingGlobalVariable && existingGlobalVariable.q) {\n existingGlobalVariable.q.forEach(fn => catchUserErrors(fn, 'onReady callback threw an error:')());\n }\n}\n","import { display } from '../tools/display';\nexport function displayAlreadyInitializedError(sdkName, initConfiguration) {\n if (!initConfiguration.silentMultipleInit) {\n display.error(`${sdkName} is already initialized.`);\n }\n}\n","import { toStackTraceString } from '../../tools/stackTrace/handlingStack';\nimport { monitor } from '../../tools/monitor';\nimport { mergeObservables, Observable } from '../../tools/observable';\nimport { addEventListener } from '../../browser/addEventListener';\nimport { safeTruncate } from '../../tools/utils/stringUtils';\nimport { ErrorSource } from '../error/error.types';\nimport { clocksNow } from '../../tools/utils/timeUtils';\nexport const RawReportType = {\n intervention: 'intervention',\n deprecation: 'deprecation',\n cspViolation: 'csp_violation'\n};\nexport function initReportObservable(configuration, apis) {\n const observables = [];\n if (apis.includes(RawReportType.cspViolation)) {\n observables.push(createCspViolationReportObservable(configuration));\n }\n const reportTypes = apis.filter(api => api !== RawReportType.cspViolation);\n if (reportTypes.length) {\n observables.push(createReportObservable(reportTypes));\n }\n return mergeObservables(...observables);\n}\nfunction createReportObservable(reportTypes) {\n return new Observable(observable => {\n if (!window.ReportingObserver) {\n return;\n }\n const handleReports = monitor((reports, _) => reports.forEach(report => observable.notify(buildRawReportErrorFromReport(report))));\n const observer = new window.ReportingObserver(handleReports, {\n types: reportTypes,\n buffered: true\n });\n observer.observe();\n return () => {\n observer.disconnect();\n };\n });\n}\nfunction createCspViolationReportObservable(configuration) {\n return new Observable(observable => {\n const {\n stop\n } = addEventListener(configuration, document, \"securitypolicyviolation\" /* DOM_EVENT.SECURITY_POLICY_VIOLATION */, event => {\n observable.notify(buildRawReportErrorFromCspViolation(event));\n });\n return stop;\n });\n}\nfunction buildRawReportErrorFromReport(report) {\n const {\n type,\n body\n } = report;\n return buildRawReportError({\n type: body.id,\n message: `${type}: ${body.message}`,\n originalError: report,\n stack: buildStack(body.id, body.message, body.sourceFile, body.lineNumber, body.columnNumber)\n });\n}\nfunction buildRawReportErrorFromCspViolation(event) {\n const message = `'${event.blockedURI}' blocked by '${event.effectiveDirective}' directive`;\n return buildRawReportError({\n type: event.effectiveDirective,\n message: `${RawReportType.cspViolation}: ${message}`,\n originalError: event,\n csp: {\n disposition: event.disposition\n },\n stack: buildStack(event.effectiveDirective, event.originalPolicy ? `${message} of the policy \"${safeTruncate(event.originalPolicy, 100, '...')}\"` : 'no policy', event.sourceFile, event.lineNumber, event.columnNumber)\n });\n}\nfunction buildRawReportError(partial) {\n return {\n startClocks: clocksNow(),\n source: ErrorSource.REPORT,\n handling: \"unhandled\" /* ErrorHandling.UNHANDLED */,\n ...partial\n };\n}\nfunction buildStack(name, message, sourceFile, lineNumber, columnNumber) {\n return sourceFile ? toStackTraceString({\n name,\n message,\n stack: [{\n func: '?',\n url: sourceFile,\n line: lineNumber !== null && lineNumber !== void 0 ? lineNumber : undefined,\n column: columnNumber !== null && columnNumber !== void 0 ? columnNumber : undefined\n }]\n }) : undefined;\n}\n","import { setInterval, clearInterval } from './timer';\nimport { removeItem } from './utils/arrayUtils';\nimport { addDuration, relativeNow, ONE_MINUTE } from './utils/timeUtils';\nconst END_OF_TIMES = Infinity;\nexport const CLEAR_OLD_VALUES_INTERVAL = ONE_MINUTE;\nlet cleanupHistoriesInterval = null;\nconst cleanupTasks = new Set();\nfunction cleanupHistories() {\n cleanupTasks.forEach(task => task());\n}\nexport function createValueHistory({\n expireDelay,\n maxEntries\n}) {\n let entries = [];\n const deletedEntries = [];\n if (!cleanupHistoriesInterval) {\n cleanupHistoriesInterval = setInterval(() => cleanupHistories(), CLEAR_OLD_VALUES_INTERVAL);\n }\n const clearExpiredValues = () => {\n const oldTimeThreshold = relativeNow() - expireDelay;\n while (entries.length > 0 && entries[entries.length - 1].endTime < oldTimeThreshold) {\n const entry = entries.pop();\n if (entry) {\n deletedEntries.push(entry.startTime);\n }\n }\n };\n cleanupTasks.add(clearExpiredValues);\n /**\n * Add a value to the history associated with a start time. Returns a reference to this newly\n * added entry that can be removed or closed.\n */\n function add(value, startTime) {\n const entry = {\n value,\n startTime,\n endTime: END_OF_TIMES,\n remove: () => {\n removeItem(entries, entry);\n },\n close: endTime => {\n entry.endTime = endTime;\n }\n };\n if (maxEntries && entries.length >= maxEntries) {\n entries.pop();\n }\n entries.unshift(entry);\n return entry;\n }\n /**\n * Return the latest value that was active during `startTime`, or the currently active value\n * if no `startTime` is provided. This method assumes that entries are not overlapping.\n *\n * If `option.returnInactive` is true, returns the value at `startTime` (active or not).\n */\n function find(startTime = END_OF_TIMES, options = {\n returnInactive: false\n }) {\n for (const entry of entries) {\n if (entry.startTime <= startTime) {\n if (options.returnInactive || startTime <= entry.endTime) {\n return entry.value;\n }\n break;\n }\n }\n }\n /**\n * Helper function to close the currently active value, if any. This method assumes that entries\n * are not overlapping.\n */\n function closeActive(endTime) {\n const latestEntry = entries[0];\n if (latestEntry && latestEntry.endTime === END_OF_TIMES) {\n latestEntry.close(endTime);\n }\n }\n /**\n * Return all values with an active period overlapping with the duration,\n * or all values that were active during `startTime` if no duration is provided,\n * or all currently active values if no `startTime` is provided.\n */\n function findAll(startTime = END_OF_TIMES, duration = 0) {\n const endTime = addDuration(startTime, duration);\n return entries.filter(entry => entry.startTime <= endTime && startTime <= entry.endTime).map(entry => entry.value);\n }\n function getAllEntries() {\n return entries.map(({\n startTime,\n endTime,\n value\n }) => ({\n startTime,\n endTime: endTime === END_OF_TIMES ? 'Infinity' : endTime,\n value\n }));\n }\n function getDeletedEntries() {\n return deletedEntries;\n }\n /**\n * Remove all entries from this collection.\n */\n function reset() {\n entries = [];\n }\n /**\n * Stop internal garbage collection of past entries.\n */\n function stop() {\n cleanupTasks.delete(clearExpiredValues);\n if (cleanupTasks.size === 0 && cleanupHistoriesInterval) {\n clearInterval(cleanupHistoriesInterval);\n cleanupHistoriesInterval = null;\n }\n }\n return {\n add,\n find,\n closeActive,\n findAll,\n reset,\n stop,\n getAllEntries,\n getDeletedEntries\n };\n}\n","import { Observable } from '../../tools/observable';\nimport { createValueHistory } from '../../tools/valueHistory';\nimport { relativeNow, clocksOrigin, ONE_MINUTE } from '../../tools/utils/timeUtils';\nimport { addEventListener, addEventListeners } from '../../browser/addEventListener';\nimport { clearInterval, setInterval } from '../../tools/timer';\nimport { SESSION_TIME_OUT_DELAY } from './sessionConstants';\nimport { startSessionStore } from './sessionStore';\nexport const VISIBILITY_CHECK_DELAY = ONE_MINUTE;\nconst SESSION_CONTEXT_TIMEOUT_DELAY = SESSION_TIME_OUT_DELAY;\nlet stopCallbacks = [];\nexport function startSessionManager(configuration, productKey, computeSessionState, trackingConsentState) {\n const renewObservable = new Observable();\n const expireObservable = new Observable();\n // TODO - Improve configuration type and remove assertion\n const sessionStore = startSessionStore(configuration.sessionStoreStrategyType, configuration, productKey, computeSessionState);\n stopCallbacks.push(() => sessionStore.stop());\n const sessionContextHistory = createValueHistory({\n expireDelay: SESSION_CONTEXT_TIMEOUT_DELAY\n });\n stopCallbacks.push(() => sessionContextHistory.stop());\n sessionStore.renewObservable.subscribe(() => {\n sessionContextHistory.add(buildSessionContext(), relativeNow());\n renewObservable.notify();\n });\n sessionStore.expireObservable.subscribe(() => {\n expireObservable.notify();\n sessionContextHistory.closeActive(relativeNow());\n });\n // We expand/renew session unconditionally as tracking consent is always granted when the session\n // manager is started.\n sessionStore.expandOrRenewSession();\n sessionContextHistory.add(buildSessionContext(), clocksOrigin().relative);\n trackingConsentState.observable.subscribe(() => {\n if (trackingConsentState.isGranted()) {\n sessionStore.expandOrRenewSession();\n } else {\n sessionStore.expire();\n }\n });\n trackActivity(configuration, () => {\n if (trackingConsentState.isGranted()) {\n sessionStore.expandOrRenewSession();\n }\n });\n trackVisibility(configuration, () => sessionStore.expandSession());\n trackResume(configuration, () => sessionStore.restartSession());\n function buildSessionContext() {\n return {\n id: sessionStore.getSession().id,\n trackingType: sessionStore.getSession()[productKey],\n isReplayForced: !!sessionStore.getSession().forcedReplay,\n anonymousId: sessionStore.getSession().anonymousId\n };\n }\n return {\n findSession: (startTime, options) => sessionContextHistory.find(startTime, options),\n renewObservable,\n expireObservable,\n sessionStateUpdateObservable: sessionStore.sessionStateUpdateObservable,\n expire: sessionStore.expire,\n updateSessionState: sessionStore.updateSessionState\n };\n}\nexport function stopSessionManager() {\n stopCallbacks.forEach(e => e());\n stopCallbacks = [];\n}\nfunction trackActivity(configuration, expandOrRenewSession) {\n const {\n stop\n } = addEventListeners(configuration, window, [\"click\" /* DOM_EVENT.CLICK */, \"touchstart\" /* DOM_EVENT.TOUCH_START */, \"keydown\" /* DOM_EVENT.KEY_DOWN */, \"scroll\" /* DOM_EVENT.SCROLL */], expandOrRenewSession, {\n capture: true,\n passive: true\n });\n stopCallbacks.push(stop);\n}\nfunction trackVisibility(configuration, expandSession) {\n const expandSessionWhenVisible = () => {\n if (document.visibilityState === 'visible') {\n expandSession();\n }\n };\n const {\n stop\n } = addEventListener(configuration, document, \"visibilitychange\" /* DOM_EVENT.VISIBILITY_CHANGE */, expandSessionWhenVisible);\n stopCallbacks.push(stop);\n const visibilityCheckInterval = setInterval(expandSessionWhenVisible, VISIBILITY_CHECK_DELAY);\n stopCallbacks.push(() => {\n clearInterval(visibilityCheckInterval);\n });\n}\nfunction trackResume(configuration, cb) {\n const {\n stop\n } = addEventListener(configuration, window, \"resume\" /* DOM_EVENT.RESUME */, cb, {\n capture: true\n });\n stopCallbacks.push(stop);\n}\n","import { computeBytesCount } from './utils/byteUtils';\nexport function createIdentityEncoder() {\n let output = '';\n let outputBytesCount = 0;\n return {\n isAsync: false,\n get isEmpty() {\n return !output;\n },\n write(data, callback) {\n const additionalEncodedBytesCount = computeBytesCount(data);\n outputBytesCount += additionalEncodedBytesCount;\n output += data;\n if (callback) {\n callback(additionalEncodedBytesCount);\n }\n },\n finish(callback) {\n callback(this.finishSync());\n },\n finishSync() {\n const result = {\n output,\n outputBytesCount,\n rawBytesCount: outputBytesCount,\n pendingData: ''\n };\n output = '';\n outputBytesCount = 0;\n return result;\n },\n estimateEncodedBytesCount(data) {\n return data.length;\n }\n };\n}\n","// eslint-disable-next-line no-restricted-syntax\nexport class AbstractLifeCycle {\n constructor() {\n this.callbacks = {};\n }\n notify(eventType, data) {\n const eventCallbacks = this.callbacks[eventType];\n if (eventCallbacks) {\n eventCallbacks.forEach(callback => callback(data));\n }\n }\n subscribe(eventType, callback) {\n if (!this.callbacks[eventType]) {\n this.callbacks[eventType] = [];\n }\n this.callbacks[eventType].push(callback);\n return {\n unsubscribe: () => {\n this.callbacks[eventType] = this.callbacks[eventType].filter(other => callback !== other);\n }\n };\n }\n}\n","import { setTimeout } from '../../tools/timer';\nimport { clocksNow, ONE_MINUTE } from '../../tools/utils/timeUtils';\nimport { ErrorSource } from '../error/error.types';\nexport function createEventRateLimiter(eventType, limit, onLimitReached) {\n let eventCount = 0;\n let allowNextEvent = false;\n return {\n isLimitReached() {\n if (eventCount === 0) {\n setTimeout(() => {\n eventCount = 0;\n }, ONE_MINUTE);\n }\n eventCount += 1;\n if (eventCount <= limit || allowNextEvent) {\n allowNextEvent = false;\n return false;\n }\n if (eventCount === limit + 1) {\n allowNextEvent = true;\n try {\n onLimitReached({\n message: `Reached max number of ${eventType}s by minute: ${limit}`,\n source: ErrorSource.AGENT,\n startClocks: clocksNow()\n });\n } finally {\n allowNextEvent = false;\n }\n }\n return true;\n }\n };\n}\n","import { noop } from '../tools/utils/functionUtils';\nimport { addEventListener } from './addEventListener';\nexport function runOnReadyState(configuration, expectedReadyState, callback) {\n if (document.readyState === expectedReadyState || document.readyState === 'complete') {\n callback();\n return {\n stop: noop\n };\n }\n const eventName = expectedReadyState === 'complete' ? \"load\" /* DOM_EVENT.LOAD */ : \"DOMContentLoaded\" /* DOM_EVENT.DOM_CONTENT_LOADED */;\n return addEventListener(configuration, window, eventName, callback, {\n once: true\n });\n}\nexport function asyncRunOnReadyState(configuration, expectedReadyState) {\n return new Promise(resolve => {\n runOnReadyState(configuration, expectedReadyState, resolve);\n });\n}\n","import { instrumentMethod } from '../tools/instrumentMethod';\nimport { Observable } from '../tools/observable';\nimport { elapsed, clocksNow, timeStampNow } from '../tools/utils/timeUtils';\nimport { normalizeUrl } from '../tools/utils/urlPolyfill';\nimport { shallowClone } from '../tools/utils/objectUtils';\nimport { addEventListener } from './addEventListener';\nlet xhrObservable;\nconst xhrContexts = new WeakMap();\nexport function initXhrObservable(configuration) {\n if (!xhrObservable) {\n xhrObservable = createXhrObservable(configuration);\n }\n return xhrObservable;\n}\nfunction createXhrObservable(configuration) {\n return new Observable(observable => {\n const {\n stop: stopInstrumentingStart\n } = instrumentMethod(XMLHttpRequest.prototype, 'open', openXhr);\n const {\n stop: stopInstrumentingSend\n } = instrumentMethod(XMLHttpRequest.prototype, 'send', call => {\n sendXhr(call, configuration, observable);\n }, {\n computeHandlingStack: true\n });\n const {\n stop: stopInstrumentingAbort\n } = instrumentMethod(XMLHttpRequest.prototype, 'abort', abortXhr);\n return () => {\n stopInstrumentingStart();\n stopInstrumentingSend();\n stopInstrumentingAbort();\n };\n });\n}\nfunction openXhr({\n target: xhr,\n parameters: [method, url]\n}) {\n xhrContexts.set(xhr, {\n state: 'open',\n method: String(method).toUpperCase(),\n url: normalizeUrl(String(url))\n });\n}\nfunction sendXhr({\n target: xhr,\n handlingStack\n}, configuration, observable) {\n const context = xhrContexts.get(xhr);\n if (!context) {\n return;\n }\n const startContext = context;\n startContext.state = 'start';\n startContext.startClocks = clocksNow();\n startContext.isAborted = false;\n startContext.xhr = xhr;\n startContext.handlingStack = handlingStack;\n let hasBeenReported = false;\n const {\n stop: stopInstrumentingOnReadyStateChange\n } = instrumentMethod(xhr, 'onreadystatechange', () => {\n if (xhr.readyState === XMLHttpRequest.DONE) {\n // Try to report the XHR as soon as possible, because the XHR may be mutated by the\n // application during a future event. For example, Angular is calling .abort() on\n // completed requests during an onreadystatechange event, so the status becomes '0'\n // before the request is collected.\n onEnd();\n }\n });\n const onEnd = () => {\n unsubscribeLoadEndListener();\n stopInstrumentingOnReadyStateChange();\n if (hasBeenReported) {\n return;\n }\n hasBeenReported = true;\n const completeContext = context;\n completeContext.state = 'complete';\n completeContext.duration = elapsed(startContext.startClocks.timeStamp, timeStampNow());\n completeContext.status = xhr.status;\n observable.notify(shallowClone(completeContext));\n };\n const {\n stop: unsubscribeLoadEndListener\n } = addEventListener(configuration, xhr, 'loadend', onEnd);\n observable.notify(startContext);\n}\nfunction abortXhr({\n target: xhr\n}) {\n const context = xhrContexts.get(xhr);\n if (context) {\n context.isAborted = true;\n }\n}\n","import { instrumentMethod } from '../tools/instrumentMethod';\nimport { monitor } from '../tools/monitor';\nimport { Observable } from '../tools/observable';\nimport { clocksNow } from '../tools/utils/timeUtils';\nimport { normalizeUrl } from '../tools/utils/urlPolyfill';\nlet fetchObservable;\nexport function initFetchObservable() {\n if (!fetchObservable) {\n fetchObservable = createFetchObservable();\n }\n return fetchObservable;\n}\nexport function resetFetchObservable() {\n fetchObservable = undefined;\n}\nfunction createFetchObservable() {\n return new Observable(observable => {\n if (!window.fetch) {\n return;\n }\n const {\n stop\n } = instrumentMethod(window, 'fetch', call => beforeSend(call, observable), {\n computeHandlingStack: true\n });\n return stop;\n });\n}\nfunction beforeSend({\n parameters,\n onPostCall,\n handlingStack\n}, observable) {\n const [input, init] = parameters;\n let methodFromParams = init && init.method;\n if (methodFromParams === undefined && input instanceof Request) {\n methodFromParams = input.method;\n }\n const method = methodFromParams !== undefined ? String(methodFromParams).toUpperCase() : 'GET';\n const url = input instanceof Request ? input.url : normalizeUrl(String(input));\n const startClocks = clocksNow();\n const context = {\n state: 'start',\n init,\n input,\n method,\n startClocks,\n url,\n handlingStack\n };\n observable.notify(context);\n // Those properties can be changed by observable subscribers\n parameters[0] = context.input;\n parameters[1] = context.init;\n onPostCall(responsePromise => afterSend(observable, responsePromise, context));\n}\nfunction afterSend(observable, responsePromise, startContext) {\n const context = startContext;\n function reportFetch(partialContext) {\n context.state = 'resolve';\n Object.assign(context, partialContext);\n observable.notify(context);\n }\n responsePromise.then(monitor(response => {\n reportFetch({\n response,\n responseType: response.type,\n status: response.status,\n isAborted: false\n });\n }), monitor(error => {\n var _a, _b;\n reportFetch({\n status: 0,\n isAborted: ((_b = (_a = context.init) === null || _a === void 0 ? void 0 : _a.signal) === null || _b === void 0 ? void 0 : _b.aborted) || error instanceof DOMException && error.code === DOMException.ABORT_ERR,\n error\n });\n }));\n}\n","import { setTimeout, clearTimeout } from './timer';\nimport { monitor } from './monitor';\nimport { dateNow } from './utils/timeUtils';\n/**\n * 'requestIdleCallback' with a shim.\n */\nexport function requestIdleCallback(callback, opts) {\n // Note: check both 'requestIdleCallback' and 'cancelIdleCallback' existence because some polyfills only implement 'requestIdleCallback'.\n if (window.requestIdleCallback && window.cancelIdleCallback) {\n const id = window.requestIdleCallback(monitor(callback), opts);\n return () => window.cancelIdleCallback(id);\n }\n return requestIdleCallbackShim(callback);\n}\nexport const MAX_TASK_TIME = 50;\n/*\n * Shim from https://developer.chrome.com/blog/using-requestidlecallback#checking_for_requestidlecallback\n * Note: there is no simple way to support the \"timeout\" option, so we ignore it.\n */\nexport function requestIdleCallbackShim(callback) {\n const start = dateNow();\n const timeoutId = setTimeout(() => {\n callback({\n didTimeout: false,\n timeRemaining: () => Math.max(0, MAX_TASK_TIME - (dateNow() - start))\n });\n }, 0);\n return () => clearTimeout(timeoutId);\n}\n","import { ONE_SECOND } from './utils/timeUtils';\nimport { requestIdleCallback } from './requestIdleCallback';\n/**\n * Maximum delay before starting to execute tasks in the queue. We don't want to wait too long\n * before running tasks, as it might hurt reliability (ex: if the user navigates away, we might lose\n * the opportunity to send some data). We also don't want to run tasks too often, as it might hurt\n * performance.\n */\nconst IDLE_CALLBACK_TIMEOUT = ONE_SECOND;\n/**\n * Maximum amount of time allocated to running tasks when a timeout (`IDLE_CALLBACK_TIMEOUT`) is\n * reached. We should not run tasks for too long as it will hurt performance, but we should still\n * run some tasks to avoid postponing them forever.\n *\n * Rational: Running tasks for 30ms every second (IDLE_CALLBACK_TIMEOUT) should be acceptable.\n */\nexport const MAX_EXECUTION_TIME_ON_TIMEOUT = 30;\nexport function createTaskQueue() {\n const pendingTasks = [];\n function run(deadline) {\n let executionTimeRemaining;\n if (deadline.didTimeout) {\n const start = performance.now();\n executionTimeRemaining = () => MAX_EXECUTION_TIME_ON_TIMEOUT - (performance.now() - start);\n } else {\n executionTimeRemaining = deadline.timeRemaining.bind(deadline);\n }\n while (executionTimeRemaining() > 0 && pendingTasks.length) {\n pendingTasks.shift()();\n }\n if (pendingTasks.length) {\n scheduleNextRun();\n }\n }\n function scheduleNextRun() {\n requestIdleCallback(run, {\n timeout: IDLE_CALLBACK_TIMEOUT\n });\n }\n return {\n push(task) {\n if (pendingTasks.push(task) === 1) {\n scheduleNextRun();\n }\n }\n };\n}\n","import { flattenErrorCauses, isError, tryToGetFingerprint, tryToGetErrorContext } from '../error/error';\nimport { mergeObservables, Observable } from '../../tools/observable';\nimport { ConsoleApiName, globalConsole } from '../../tools/display';\nimport { callMonitored } from '../../tools/monitor';\nimport { sanitize } from '../../tools/serialisation/sanitize';\nimport { jsonStringify } from '../../tools/serialisation/jsonStringify';\nimport { ErrorSource } from '../error/error.types';\nimport { computeStackTrace } from '../../tools/stackTrace/computeStackTrace';\nimport { createHandlingStack, toStackTraceString, formatErrorMessage } from '../../tools/stackTrace/handlingStack';\nimport { clocksNow } from '../../tools/utils/timeUtils';\nlet consoleObservablesByApi = {};\nexport function initConsoleObservable(apis) {\n const consoleObservables = apis.map(api => {\n if (!consoleObservablesByApi[api]) {\n consoleObservablesByApi[api] = createConsoleObservable(api); // we are sure that the observable created for this api will yield the expected ConsoleLog type\n }\n return consoleObservablesByApi[api];\n });\n return mergeObservables(...consoleObservables);\n}\nexport function resetConsoleObservable() {\n consoleObservablesByApi = {};\n}\nfunction createConsoleObservable(api) {\n return new Observable(observable => {\n const originalConsoleApi = globalConsole[api];\n globalConsole[api] = (...params) => {\n originalConsoleApi.apply(console, params);\n const handlingStack = createHandlingStack('console error');\n callMonitored(() => {\n observable.notify(buildConsoleLog(params, api, handlingStack));\n });\n };\n return () => {\n globalConsole[api] = originalConsoleApi;\n };\n });\n}\nfunction buildConsoleLog(params, api, handlingStack) {\n const message = params.map(param => formatConsoleParameters(param)).join(' ');\n let error;\n if (api === ConsoleApiName.error) {\n const firstErrorParam = params.find(isError);\n error = {\n stack: firstErrorParam ? toStackTraceString(computeStackTrace(firstErrorParam)) : undefined,\n fingerprint: tryToGetFingerprint(firstErrorParam),\n causes: firstErrorParam ? flattenErrorCauses(firstErrorParam, 'console') : undefined,\n startClocks: clocksNow(),\n message,\n source: ErrorSource.CONSOLE,\n handling: \"handled\" /* ErrorHandling.HANDLED */,\n handlingStack,\n context: tryToGetErrorContext(firstErrorParam)\n };\n }\n return {\n api,\n message,\n error,\n handlingStack\n };\n}\nfunction formatConsoleParameters(param) {\n if (typeof param === 'string') {\n return sanitize(param);\n }\n if (isError(param)) {\n return formatErrorMessage(computeStackTrace(param));\n }\n return jsonStringify(sanitize(param), undefined, 2);\n}\n","import { display } from '../../tools/display';\nimport { getType } from '../../tools/utils/typeUtils';\n/**\n * Simple check to ensure an object is a valid context\n */\nexport function checkContext(maybeContext) {\n const isValid = getType(maybeContext) === 'object';\n if (!isValid) {\n display.error('Unsupported context:', maybeContext);\n }\n return isValid;\n}\n","import { deepClone } from '../../tools/mergeInto';\nimport { sanitize } from '../../tools/serialisation/sanitize';\nimport { Observable } from '../../tools/observable';\nimport { display } from '../../tools/display';\nimport { checkContext } from './contextUtils';\nfunction ensureProperties(context, propertiesConfig, name) {\n const newContext = {\n ...context\n };\n for (const [key, {\n required,\n type\n }] of Object.entries(propertiesConfig)) {\n /**\n * Ensure specified properties are strings as defined here:\n * https://docs.datadoghq.com/logs/log_configuration/attributes_naming_convention/#user-related-attributes\n */\n if (type === 'string' && key in newContext) {\n /* eslint-disable @typescript-eslint/no-base-to-string */\n newContext[key] = String(newContext[key]);\n }\n if (required && !(key in context)) {\n display.warn(`The property ${key} of ${name} is required; context will not be sent to the intake.`);\n }\n }\n return newContext;\n}\nexport function createContextManager(name = '', {\n customerDataTracker,\n propertiesConfig = {}\n} = {}) {\n let context = {};\n const changeObservable = new Observable();\n const contextManager = {\n getContext: () => deepClone(context),\n setContext: newContext => {\n if (checkContext(newContext)) {\n context = sanitize(ensureProperties(newContext, propertiesConfig, name));\n customerDataTracker === null || customerDataTracker === void 0 ? void 0 : customerDataTracker.updateCustomerData(context);\n } else {\n contextManager.clearContext();\n }\n changeObservable.notify();\n },\n setContextProperty: (key, property) => {\n context = sanitize(ensureProperties({\n ...context,\n [key]: property\n }, propertiesConfig, name));\n customerDataTracker === null || customerDataTracker === void 0 ? void 0 : customerDataTracker.updateCustomerData(context);\n changeObservable.notify();\n },\n removeContextProperty: key => {\n delete context[key];\n customerDataTracker === null || customerDataTracker === void 0 ? void 0 : customerDataTracker.updateCustomerData(context);\n ensureProperties(context, propertiesConfig, name);\n changeObservable.notify();\n },\n clearContext: () => {\n context = {};\n customerDataTracker === null || customerDataTracker === void 0 ? void 0 : customerDataTracker.resetCustomerData();\n changeObservable.notify();\n },\n changeObservable\n };\n return contextManager;\n}\n","import { addEventListener } from '../../browser/addEventListener';\nimport { combine } from '../../tools/mergeInto';\nimport { isEmptyObject } from '../../tools/utils/objectUtils';\nconst CONTEXT_STORE_KEY_PREFIX = '_dd_c';\nconst storageListeners = [];\nexport function storeContextManager(configuration, contextManager, productKey, customerDataType) {\n const storageKey = buildStorageKey(productKey, customerDataType);\n storageListeners.push(addEventListener(configuration, window, \"storage\" /* DOM_EVENT.STORAGE */, ({\n key\n }) => {\n if (storageKey === key) {\n synchronizeWithStorage();\n }\n }));\n contextManager.changeObservable.subscribe(dumpToStorage);\n const contextFromStorage = combine(getFromStorage(), contextManager.getContext());\n if (!isEmptyObject(contextFromStorage)) {\n contextManager.setContext(contextFromStorage);\n }\n function synchronizeWithStorage() {\n contextManager.setContext(getFromStorage());\n }\n function dumpToStorage() {\n localStorage.setItem(storageKey, JSON.stringify(contextManager.getContext()));\n }\n function getFromStorage() {\n const rawContext = localStorage.getItem(storageKey);\n return rawContext ? JSON.parse(rawContext) : {};\n }\n}\nexport function buildStorageKey(productKey, customerDataType) {\n return `${CONTEXT_STORE_KEY_PREFIX}_${productKey}_${customerDataType}`;\n}\nexport function removeStorageListeners() {\n storageListeners.map(listener => listener.stop());\n}\n","import { ONE_KIBI_BYTE, computeBytesCount } from '../../tools/utils/byteUtils';\nimport { throttle } from '../../tools/utils/functionUtils';\nimport { jsonStringify } from '../../tools/serialisation/jsonStringify';\nimport { DOCS_TROUBLESHOOTING, MORE_DETAILS, display } from '../../tools/display';\nimport { isEmptyObject } from '../../tools/utils/objectUtils';\n// RUM and logs batch bytes limit is 16KB\n// ensure that we leave room for other event attributes and maintain a decent amount of event per batch\n// (3KB (customer data) + 1KB (other attributes)) * 4 (events per batch) = 16KB\nexport const CUSTOMER_DATA_BYTES_LIMIT = 3 * ONE_KIBI_BYTE;\n// We observed that the compression ratio is around 8 in general, but we also want to keep a margin\n// because some data might not be compressed (ex: last view update on page exit). We chose 16KiB\n// because it is also the limit of the 'batchBytesCount' that we use for RUM and Logs data, but this\n// is a bit arbitrary.\nexport const CUSTOMER_COMPRESSED_DATA_BYTES_LIMIT = 16 * ONE_KIBI_BYTE;\nexport const BYTES_COMPUTATION_THROTTLING_DELAY = 200;\nexport function createCustomerDataTrackerManager(compressionStatus = 2 /* CustomerDataCompressionStatus.Disabled */) {\n const customerDataTrackers = new Map();\n let alreadyWarned = false;\n function checkCustomerDataLimit(initialBytesCount = 0) {\n if (alreadyWarned || compressionStatus === 0 /* CustomerDataCompressionStatus.Unknown */) {\n return;\n }\n const bytesCountLimit = compressionStatus === 2 /* CustomerDataCompressionStatus.Disabled */ ? CUSTOMER_DATA_BYTES_LIMIT : CUSTOMER_COMPRESSED_DATA_BYTES_LIMIT;\n let bytesCount = initialBytesCount;\n customerDataTrackers.forEach(tracker => {\n bytesCount += tracker.getBytesCount();\n });\n if (bytesCount > bytesCountLimit) {\n displayCustomerDataLimitReachedWarning(bytesCountLimit);\n alreadyWarned = true;\n }\n }\n return {\n /**\n * Creates a detached tracker. The manager will not store a reference to that tracker, and the\n * bytes count will be counted independently from other detached trackers.\n *\n * This is particularly useful when we don't know when the tracker will be unused, so we don't\n * leak memory (ex: when used in Logger instances).\n */\n createDetachedTracker: () => {\n const tracker = createCustomerDataTracker(() => checkCustomerDataLimit(tracker.getBytesCount()));\n return tracker;\n },\n /**\n * Creates a tracker if it doesn't exist, and returns it.\n */\n getOrCreateTracker: type => {\n if (!customerDataTrackers.has(type)) {\n customerDataTrackers.set(type, createCustomerDataTracker(checkCustomerDataLimit));\n }\n return customerDataTrackers.get(type);\n },\n setCompressionStatus: newCompressionStatus => {\n if (compressionStatus === 0 /* CustomerDataCompressionStatus.Unknown */) {\n compressionStatus = newCompressionStatus;\n checkCustomerDataLimit();\n }\n },\n getCompressionStatus: () => compressionStatus,\n stop: () => {\n customerDataTrackers.forEach(tracker => tracker.stop());\n customerDataTrackers.clear();\n }\n };\n}\nexport function createCustomerDataTracker(checkCustomerDataLimit) {\n let bytesCountCache = 0;\n // Throttle the bytes computation to minimize the impact on performance.\n // Especially useful if the user call context APIs synchronously multiple times in a row\n const {\n throttled: computeBytesCountThrottled,\n cancel: cancelComputeBytesCount\n } = throttle(context => {\n bytesCountCache = computeBytesCount(jsonStringify(context));\n checkCustomerDataLimit();\n }, BYTES_COMPUTATION_THROTTLING_DELAY);\n const resetBytesCount = () => {\n cancelComputeBytesCount();\n bytesCountCache = 0;\n };\n return {\n updateCustomerData: context => {\n if (isEmptyObject(context)) {\n resetBytesCount();\n } else {\n computeBytesCountThrottled(context);\n }\n },\n resetCustomerData: resetBytesCount,\n getBytesCount: () => bytesCountCache,\n stop: () => {\n cancelComputeBytesCount();\n }\n };\n}\nfunction displayCustomerDataLimitReachedWarning(bytesCountLimit) {\n display.warn(`Customer data exceeds the recommended ${bytesCountLimit / ONE_KIBI_BYTE}KiB threshold. ${MORE_DETAILS} ${DOCS_TROUBLESHOOTING}/#customer-data-exceeds-the-recommended-threshold-warning`);\n}\n","import { monitor } from './monitor';\nimport { noop } from './utils/functionUtils';\n/**\n * Read bytes from a ReadableStream until at least `limit` bytes have been read (or until the end of\n * the stream). The callback is invoked with the at most `limit` bytes, and indicates that the limit\n * has been exceeded if more bytes were available.\n */\nexport function readBytesFromStream(stream, callback, options) {\n const reader = stream.getReader();\n const chunks = [];\n let readBytesCount = 0;\n readMore();\n function readMore() {\n reader.read().then(monitor(result => {\n if (result.done) {\n onDone();\n return;\n }\n if (options.collectStreamBody) {\n chunks.push(result.value);\n }\n readBytesCount += result.value.length;\n if (readBytesCount > options.bytesLimit) {\n onDone();\n } else {\n readMore();\n }\n }), monitor(error => callback(error)));\n }\n function onDone() {\n reader.cancel().catch(\n // we don't care if cancel fails, but we still need to catch the error to avoid reporting it\n // as an unhandled rejection\n noop);\n let bytes;\n let limitExceeded;\n if (options.collectStreamBody) {\n let completeBuffer;\n if (chunks.length === 1) {\n // optimization: if the response is small enough to fit in a single buffer (provided by the browser), just\n // use it directly.\n completeBuffer = chunks[0];\n } else {\n // else, we need to copy buffers into a larger buffer to concatenate them.\n completeBuffer = new Uint8Array(readBytesCount);\n let offset = 0;\n chunks.forEach(chunk => {\n completeBuffer.set(chunk, offset);\n offset += chunk.length;\n });\n }\n bytes = completeBuffer.slice(0, options.bytesLimit);\n limitExceeded = completeBuffer.length > options.bytesLimit;\n }\n callback(undefined, bytes, limitExceeded);\n }\n}\n","import { getInitCookie } from '../../browser/cookie';\nexport const SYNTHETICS_TEST_ID_COOKIE_NAME = 'datadog-synthetics-public-id';\nexport const SYNTHETICS_RESULT_ID_COOKIE_NAME = 'datadog-synthetics-result-id';\nexport const SYNTHETICS_INJECTS_RUM_COOKIE_NAME = 'datadog-synthetics-injects-rum';\nexport function willSyntheticsInjectRum() {\n return Boolean(window._DATADOG_SYNTHETICS_INJECTS_RUM || getInitCookie(SYNTHETICS_INJECTS_RUM_COOKIE_NAME));\n}\nexport function getSyntheticsTestId() {\n const value = window._DATADOG_SYNTHETICS_PUBLIC_ID || getInitCookie(SYNTHETICS_TEST_ID_COOKIE_NAME);\n return typeof value === 'string' ? value : undefined;\n}\nexport function getSyntheticsResultId() {\n const value = window._DATADOG_SYNTHETICS_RESULT_ID || getInitCookie(SYNTHETICS_RESULT_ID_COOKIE_NAME);\n return typeof value === 'string' ? value : undefined;\n}\n","import { display } from './display';\nimport { getType } from './utils/typeUtils';\nexport function isMatchOption(item) {\n const itemType = getType(item);\n return itemType === 'string' || itemType === 'function' || item instanceof RegExp;\n}\n/**\n * Returns true if value can be matched by at least one of the provided MatchOptions.\n * When comparing strings, setting useStartsWith to true will compare the value with the start of\n * the option, instead of requiring an exact match.\n */\nexport function matchList(list, value, useStartsWith = false) {\n return list.some(item => {\n try {\n if (typeof item === 'function') {\n return item(value);\n } else if (item instanceof RegExp) {\n return item.test(value);\n } else if (typeof item === 'string') {\n return useStartsWith ? value.startsWith(item) : item === value;\n }\n } catch (e) {\n display.error(e);\n }\n return false;\n });\n}\n","import { addTelemetryDebug, elapsed, getPathName, isValidUrl, toServerDuration, isIntakeUrl } from '@datadog/browser-core';\nexport const FAKE_INITIAL_DOCUMENT = 'initial_document';\nconst RESOURCE_TYPES = [[\"document\" /* ResourceType.DOCUMENT */, initiatorType => FAKE_INITIAL_DOCUMENT === initiatorType], [\"xhr\" /* ResourceType.XHR */, initiatorType => 'xmlhttprequest' === initiatorType], [\"fetch\" /* ResourceType.FETCH */, initiatorType => 'fetch' === initiatorType], [\"beacon\" /* ResourceType.BEACON */, initiatorType => 'beacon' === initiatorType], [\"css\" /* ResourceType.CSS */, (_, path) => /\\.css$/i.test(path)], [\"js\" /* ResourceType.JS */, (_, path) => /\\.js$/i.test(path)], [\"image\" /* ResourceType.IMAGE */, (initiatorType, path) => ['image', 'img', 'icon'].includes(initiatorType) || /\\.(gif|jpg|jpeg|tiff|png|svg|ico)$/i.exec(path) !== null], [\"font\" /* ResourceType.FONT */, (_, path) => /\\.(woff|eot|woff2|ttf)$/i.exec(path) !== null], [\"media\" /* ResourceType.MEDIA */, (initiatorType, path) => ['audio', 'video'].includes(initiatorType) || /\\.(mp3|mp4)$/i.exec(path) !== null]];\nexport function computeResourceEntryType(entry) {\n const url = entry.name;\n if (!isValidUrl(url)) {\n addTelemetryDebug(`Failed to construct URL for \"${entry.name}\"`);\n return \"other\" /* ResourceType.OTHER */;\n }\n const path = getPathName(url);\n for (const [type, isType] of RESOURCE_TYPES) {\n if (isType(entry.initiatorType, path)) {\n return type;\n }\n }\n return \"other\" /* ResourceType.OTHER */;\n}\nfunction areInOrder(...numbers) {\n for (let i = 1; i < numbers.length; i += 1) {\n if (numbers[i - 1] > numbers[i]) {\n return false;\n }\n }\n return true;\n}\nexport function isResourceEntryRequestType(entry) {\n return entry.initiatorType === 'xmlhttprequest' || entry.initiatorType === 'fetch';\n}\nexport function computeResourceEntryDuration(entry) {\n const {\n duration,\n startTime,\n responseEnd\n } = entry;\n // Safari duration is always 0 on timings blocked by cross origin policies.\n if (duration === 0 && startTime < responseEnd) {\n return elapsed(startTime, responseEnd);\n }\n return duration;\n}\nexport function computeResourceEntryDetails(entry) {\n if (!hasValidResourceEntryTimings(entry)) {\n return undefined;\n }\n const {\n startTime,\n fetchStart,\n workerStart,\n redirectStart,\n redirectEnd,\n domainLookupStart,\n domainLookupEnd,\n connectStart,\n secureConnectionStart,\n connectEnd,\n requestStart,\n responseStart,\n responseEnd\n } = entry;\n const details = {\n download: formatTiming(startTime, responseStart, responseEnd),\n first_byte: formatTiming(startTime, requestStart, responseStart)\n };\n // Make sure a worker processing time is recorded\n if (0 < workerStart && workerStart < fetchStart) {\n details.worker = formatTiming(startTime, workerStart, fetchStart);\n }\n // Make sure a connection occurred\n if (fetchStart < connectEnd) {\n details.connect = formatTiming(startTime, connectStart, connectEnd);\n // Make sure a secure connection occurred\n if (connectStart <= secureConnectionStart && secureConnectionStart <= connectEnd) {\n details.ssl = formatTiming(startTime, secureConnectionStart, connectEnd);\n }\n }\n // Make sure a domain lookup occurred\n if (fetchStart < domainLookupEnd) {\n details.dns = formatTiming(startTime, domainLookupStart, domainLookupEnd);\n }\n // Make sure a redirection occurred\n if (startTime < redirectEnd) {\n details.redirect = formatTiming(startTime, redirectStart, redirectEnd);\n }\n return details;\n}\n/**\n * Entries with negative duration are unexpected and should be dismissed. The intake will ignore RUM\n * Resource events with negative durations anyway.\n * Since Chromium 128, more entries have unexpected negative durations, see\n * https://issues.chromium.org/issues/363031537\n */\nexport function hasValidResourceEntryDuration(entry) {\n return entry.duration >= 0;\n}\nexport function hasValidResourceEntryTimings(entry) {\n // Ensure timings are in the right order. On top of filtering out potential invalid\n // RumPerformanceResourceTiming, it will ignore entries from requests where timings cannot be\n // collected, for example cross origin requests without a \"Timing-Allow-Origin\" header allowing\n // it.\n const areCommonTimingsInOrder = areInOrder(entry.startTime, entry.fetchStart, entry.domainLookupStart, entry.domainLookupEnd, entry.connectStart, entry.connectEnd, entry.requestStart, entry.responseStart, entry.responseEnd);\n const areRedirectionTimingsInOrder = hasRedirection(entry) ? areInOrder(entry.startTime, entry.redirectStart, entry.redirectEnd, entry.fetchStart) : true;\n return areCommonTimingsInOrder && areRedirectionTimingsInOrder;\n}\nfunction hasRedirection(entry) {\n return entry.redirectEnd > entry.startTime;\n}\nfunction formatTiming(origin, start, end) {\n if (origin <= start && start <= end) {\n return {\n duration: toServerDuration(elapsed(start, end)),\n start: toServerDuration(elapsed(origin, start))\n };\n }\n}\n/**\n * The 'nextHopProtocol' is an empty string for cross-origin resources without CORS headers,\n * meaning the protocol is unknown, and we shouldn't report it.\n * https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/nextHopProtocol#cross-origin_resources\n */\nexport function computeResourceEntryProtocol(entry) {\n return entry.nextHopProtocol === '' ? undefined : entry.nextHopProtocol;\n}\n/**\n * Handles the 'deliveryType' property to distinguish between supported values ('cache', 'navigational-prefetch'),\n * undefined (unsupported in some browsers), and other cases ('other' for unknown or unrecognized values).\n * see: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/deliveryType\n */\nexport function computeResourceEntryDeliveryType(entry) {\n return entry.deliveryType === '' ? 'other' : entry.deliveryType;\n}\nexport function computeResourceEntrySize(entry) {\n // Make sure a request actually occurred\n if (entry.startTime < entry.responseStart) {\n const {\n encodedBodySize,\n decodedBodySize,\n transferSize\n } = entry;\n return {\n size: decodedBodySize,\n encoded_body_size: encodedBodySize,\n decoded_body_size: decodedBodySize,\n transfer_size: transferSize\n };\n }\n return {\n size: undefined,\n encoded_body_size: undefined,\n decoded_body_size: undefined,\n transfer_size: undefined\n };\n}\nexport function isAllowedRequestUrl(url) {\n return url && !isIntakeUrl(url);\n}\nconst DATA_URL_REGEX = /data:(.+)?(;base64)?,/g;\nexport const MAX_ATTRIBUTE_VALUE_CHAR_LENGTH = 24000;\nexport function isLongDataUrl(url) {\n if (url.length <= MAX_ATTRIBUTE_VALUE_CHAR_LENGTH) {\n return false;\n } else if (url.substring(0, 5) === 'data:') {\n // Avoid String.match RangeError: Maximum call stack size exceeded\n url = url.substring(0, MAX_ATTRIBUTE_VALUE_CHAR_LENGTH);\n return true;\n }\n return false;\n}\nexport function sanitizeDataUrl(url) {\n return `${url.match(DATA_URL_REGEX)[0]}[...]`;\n}\n","import { addEventListeners, dateNow, relativeNow } from '@datadog/browser-core';\n/**\n * first-input timing entry polyfill based on\n * https://github.com/GoogleChrome/web-vitals/blob/master/src/lib/polyfills/firstInputPolyfill.ts\n */\nexport function retrieveFirstInputTiming(configuration, callback) {\n const startTimeStamp = dateNow();\n let timingSent = false;\n const {\n stop: removeEventListeners\n } = addEventListeners(configuration, window, [\"click\" /* DOM_EVENT.CLICK */, \"mousedown\" /* DOM_EVENT.MOUSE_DOWN */, \"keydown\" /* DOM_EVENT.KEY_DOWN */, \"touchstart\" /* DOM_EVENT.TOUCH_START */, \"pointerdown\" /* DOM_EVENT.POINTER_DOWN */], evt => {\n // Only count cancelable events, which should trigger behavior important to the user.\n if (!evt.cancelable) {\n return;\n }\n // This timing will be used to compute the \"first Input delay\", which is the delta between\n // when the system received the event (e.g. evt.timeStamp) and when it could run the callback\n // (e.g. performance.now()).\n const timing = {\n entryType: 'first-input',\n processingStart: relativeNow(),\n processingEnd: relativeNow(),\n startTime: evt.timeStamp,\n duration: 0,\n // arbitrary value to avoid nullable duration and simplify INP logic\n name: '',\n cancelable: false,\n target: null,\n toJSON: () => ({})\n };\n if (evt.type === \"pointerdown\" /* DOM_EVENT.POINTER_DOWN */) {\n sendTimingIfPointerIsNotCancelled(configuration, timing);\n } else {\n sendTiming(timing);\n }\n }, {\n passive: true,\n capture: true\n });\n return {\n stop: removeEventListeners\n };\n /**\n * Pointer events are a special case, because they can trigger main or compositor thread behavior.\n * We differentiate these cases based on whether or not we see a pointercancel event, which are\n * fired when we scroll. If we're scrolling we don't need to report input delay since FID excludes\n * scrolling and pinch/zooming.\n */\n function sendTimingIfPointerIsNotCancelled(configuration, timing) {\n addEventListeners(configuration, window, [\"pointerup\" /* DOM_EVENT.POINTER_UP */, \"pointercancel\" /* DOM_EVENT.POINTER_CANCEL */], event => {\n if (event.type === \"pointerup\" /* DOM_EVENT.POINTER_UP */) {\n sendTiming(timing);\n }\n }, {\n once: true\n });\n }\n function sendTiming(timing) {\n if (!timingSent) {\n timingSent = true;\n removeEventListeners();\n // In some cases the recorded delay is clearly wrong, e.g. it's negative or it's larger than\n // the time between now and when the page was loaded.\n // - https://github.com/GoogleChromeLabs/first-input-delay/issues/4\n // - https://github.com/GoogleChromeLabs/first-input-delay/issues/6\n // - https://github.com/GoogleChromeLabs/first-input-delay/issues/7\n const delay = timing.processingStart - timing.startTime;\n if (delay >= 0 && delay < dateNow() - startTimeStamp) {\n callback(timing);\n }\n }\n }\n}\n","import { addEventListener, Observable, setTimeout, clearTimeout, monitor } from '@datadog/browser-core';\nimport { hasValidResourceEntryDuration, isAllowedRequestUrl } from '../domain/resource/resourceUtils';\nimport { retrieveFirstInputTiming } from './firstInputPolyfill';\n// We want to use a real enum (i.e. not a const enum) here, to be able to check whether an arbitrary\n// string is an expected performance entry\n// eslint-disable-next-line no-restricted-syntax\nexport var RumPerformanceEntryType = /*#__PURE__*/function (RumPerformanceEntryType) {\n RumPerformanceEntryType[\"EVENT\"] = \"event\";\n RumPerformanceEntryType[\"FIRST_INPUT\"] = \"first-input\";\n RumPerformanceEntryType[\"LARGEST_CONTENTFUL_PAINT\"] = \"largest-contentful-paint\";\n RumPerformanceEntryType[\"LAYOUT_SHIFT\"] = \"layout-shift\";\n RumPerformanceEntryType[\"LONG_TASK\"] = \"longtask\";\n RumPerformanceEntryType[\"LONG_ANIMATION_FRAME\"] = \"long-animation-frame\";\n RumPerformanceEntryType[\"NAVIGATION\"] = \"navigation\";\n RumPerformanceEntryType[\"PAINT\"] = \"paint\";\n RumPerformanceEntryType[\"RESOURCE\"] = \"resource\";\n RumPerformanceEntryType[\"VISIBILITY_STATE\"] = \"visibility-state\";\n return RumPerformanceEntryType;\n}(RumPerformanceEntryType || {});\nexport function createPerformanceObservable(configuration, options) {\n return new Observable(observable => {\n if (!window.PerformanceObserver) {\n return;\n }\n const handlePerformanceEntries = entries => {\n const rumPerformanceEntries = filterRumPerformanceEntries(entries);\n if (rumPerformanceEntries.length > 0) {\n observable.notify(rumPerformanceEntries);\n }\n };\n let timeoutId;\n let isObserverInitializing = true;\n const observer = new PerformanceObserver(monitor(entries => {\n // In Safari the performance observer callback is synchronous.\n // Because the buffered performance entry list can be quite large we delay the computation to prevent the SDK from blocking the main thread on init\n if (isObserverInitializing) {\n timeoutId = setTimeout(() => handlePerformanceEntries(entries.getEntries()));\n } else {\n handlePerformanceEntries(entries.getEntries());\n }\n }));\n try {\n observer.observe(options);\n } catch (_a) {\n // Some old browser versions (<= chrome 74 ) don't support the PerformanceObserver type and buffered options\n // In these cases, fallback to getEntriesByType and PerformanceObserver with entryTypes\n // TODO: remove this fallback in the next major version\n const fallbackSupportedEntryTypes = [RumPerformanceEntryType.RESOURCE, RumPerformanceEntryType.NAVIGATION, RumPerformanceEntryType.LONG_TASK, RumPerformanceEntryType.PAINT];\n if (fallbackSupportedEntryTypes.includes(options.type)) {\n if (options.buffered) {\n timeoutId = setTimeout(() => handlePerformanceEntries(performance.getEntriesByType(options.type)));\n }\n try {\n observer.observe({\n entryTypes: [options.type]\n });\n } catch (_b) {\n // Old versions of Safari are throwing \"entryTypes contained only unsupported types\"\n // errors when observing only unsupported entry types.\n //\n // We could use `supportPerformanceTimingEvent` to make sure we don't invoke\n // `observer.observe` with an unsupported entry type, but Safari 11 and 12 don't support\n // `Performance.supportedEntryTypes`, so doing so would lose support for these versions\n // even if they do support the entry type.\n return;\n }\n }\n }\n isObserverInitializing = false;\n manageResourceTimingBufferFull(configuration);\n let stopFirstInputTiming;\n if (!supportPerformanceTimingEvent(RumPerformanceEntryType.FIRST_INPUT) && options.type === RumPerformanceEntryType.FIRST_INPUT) {\n ;\n ({\n stop: stopFirstInputTiming\n } = retrieveFirstInputTiming(configuration, timing => {\n handlePerformanceEntries([timing]);\n }));\n }\n return () => {\n observer.disconnect();\n if (stopFirstInputTiming) {\n stopFirstInputTiming();\n }\n clearTimeout(timeoutId);\n };\n });\n}\nlet resourceTimingBufferFullListener;\nfunction manageResourceTimingBufferFull(configuration) {\n if (!resourceTimingBufferFullListener && supportPerformanceObject() && 'addEventListener' in performance) {\n // https://bugzilla.mozilla.org/show_bug.cgi?id=1559377\n resourceTimingBufferFullListener = addEventListener(configuration, performance, 'resourcetimingbufferfull', () => {\n performance.clearResourceTimings();\n });\n }\n return () => {\n resourceTimingBufferFullListener === null || resourceTimingBufferFullListener === void 0 ? void 0 : resourceTimingBufferFullListener.stop();\n };\n}\nfunction supportPerformanceObject() {\n return window.performance !== undefined && 'getEntries' in performance;\n}\nexport function supportPerformanceTimingEvent(entryType) {\n return window.PerformanceObserver && PerformanceObserver.supportedEntryTypes !== undefined && PerformanceObserver.supportedEntryTypes.includes(entryType);\n}\nfunction filterRumPerformanceEntries(entries) {\n return entries.filter(entry => !isForbiddenResource(entry));\n}\nfunction isForbiddenResource(entry) {\n return entry.entryType === RumPerformanceEntryType.RESOURCE && (!isAllowedRequestUrl(entry.name) || !hasValidResourceEntryDuration(entry));\n}\n","export function buildCommonContext(globalContextManager, userContextManager, accountContextManager, recorderApi) {\n return {\n context: globalContextManager.getContext(),\n user: userContextManager.getContext(),\n account: accountContextManager.getContext(),\n hasReplay: recorderApi.isRecording() ? true : undefined\n };\n}\n","import { clocksNow, combine, elapsed, generateUUID, toServerDuration } from '@datadog/browser-core';\nexport function createCustomVitalsState() {\n const vitalsByName = new Map();\n const vitalsByReference = new WeakMap();\n return {\n vitalsByName,\n vitalsByReference\n };\n}\nexport function startVitalCollection(lifeCycle, pageStateHistory, customVitalsState) {\n function isValid(vital) {\n return !pageStateHistory.wasInPageStateDuringPeriod(\"frozen\" /* PageState.FROZEN */, vital.startClocks.relative, vital.duration);\n }\n function addDurationVital(vital) {\n if (isValid(vital)) {\n lifeCycle.notify(12 /* LifeCycleEventType.RAW_RUM_EVENT_COLLECTED */, processVital(vital, true));\n }\n }\n return {\n addDurationVital,\n startDurationVital: (name, options = {}) => startDurationVital(customVitalsState, name, options),\n stopDurationVital: (nameOrRef, options = {}) => {\n stopDurationVital(addDurationVital, customVitalsState, nameOrRef, options);\n }\n };\n}\nexport function startDurationVital({\n vitalsByName,\n vitalsByReference\n}, name, options = {}) {\n const vital = {\n name,\n startClocks: clocksNow(),\n context: options.context,\n description: options.description\n };\n // To avoid leaking implementation details of the vital, we return a reference to it.\n const reference = {\n __dd_vital_reference: true\n };\n vitalsByName.set(name, vital);\n // To avoid memory leaks caused by the creation of numerous references (e.g., from improper useEffect implementations), we use a WeakMap.\n vitalsByReference.set(reference, vital);\n return reference;\n}\nexport function stopDurationVital(stopCallback, {\n vitalsByName,\n vitalsByReference\n}, nameOrRef, options = {}) {\n const vitalStart = typeof nameOrRef === 'string' ? vitalsByName.get(nameOrRef) : vitalsByReference.get(nameOrRef);\n if (!vitalStart) {\n return;\n }\n stopCallback(buildDurationVital(vitalStart, vitalStart.startClocks, options, clocksNow()));\n if (typeof nameOrRef === 'string') {\n vitalsByName.delete(nameOrRef);\n } else {\n vitalsByReference.delete(nameOrRef);\n }\n}\nfunction buildDurationVital(vitalStart, startClocks, stopOptions, stopClocks) {\n var _a;\n return {\n name: vitalStart.name,\n type: \"duration\" /* VitalType.DURATION */,\n startClocks,\n duration: elapsed(startClocks.timeStamp, stopClocks.timeStamp),\n context: combine(vitalStart.context, stopOptions.context),\n description: (_a = stopOptions.description) !== null && _a !== void 0 ? _a : vitalStart.description\n };\n}\nfunction processVital(vital, valueComputedBySdk) {\n const rawRumEvent = {\n date: vital.startClocks.timeStamp,\n vital: {\n id: generateUUID(),\n type: vital.type,\n name: vital.name,\n duration: toServerDuration(vital.duration),\n description: vital.description\n },\n type: \"vital\" /* RumEventType.VITAL */\n };\n if (valueComputedBySdk) {\n rawRumEvent._dd = {\n vital: {\n computed_value: true\n }\n };\n }\n return {\n rawRumEvent,\n startTime: vital.startClocks.relative,\n duration: vital.duration,\n customerContext: vital.context,\n domainContext: {}\n };\n}\n","export function callPluginsMethod(plugins, methodName, parameter) {\n if (!plugins) {\n return;\n }\n for (const plugin of plugins) {\n const method = plugin[methodName];\n if (method) {\n method(parameter);\n }\n }\n}\n","export function createTraceIdentifier() {\n return createIdentifier(64);\n}\nexport function createSpanIdentifier() {\n return createIdentifier(63);\n}\nfunction createIdentifier(bits) {\n const buffer = crypto.getRandomValues(new Uint32Array(2));\n if (bits === 63) {\n // eslint-disable-next-line no-bitwise\n buffer[buffer.length - 1] >>>= 1; // force 63-bit\n }\n // The `.toString` function is intentionally similar to Number and BigInt `.toString` method.\n //\n // JavaScript numbers can represent integers up to 48 bits, this is why we need two of them to\n // represent a 64 bits identifier. But BigInts don't have this limitation and can represent larger\n // integer values.\n //\n // In the future, when we drop browsers without BigInts support, we could use BigInts directly\n // represent identifiers by simply returning a BigInt from this function (as all we need is a\n // value with a `.toString` method).\n //\n // Examples:\n // const buffer = getCrypto().getRandomValues(new Uint32Array(2))\n // return BigInt(buffer[0]) + BigInt(buffer[1]) << 32n\n //\n // // Alternative with BigUint64Array (different Browser support than plain bigints!):\n // return crypto.getRandomValues(new BigUint64Array(1))[0]\n //\n // For now, let's keep using two plain numbers as having two different implementations (one for\n // browsers with BigInt support and one for older browsers) don't bring much value.\n return {\n toString(radix = 10) {\n let high = buffer[1];\n let low = buffer[0];\n let str = '';\n do {\n const mod = high % radix * 4294967296 + low;\n high = Math.floor(high / radix);\n low = Math.floor(mod / radix);\n str = (mod % radix).toString(radix) + str;\n } while (high || low);\n return str;\n }\n };\n}\nexport function toPaddedHexadecimalString(id) {\n return id.toString(16).padStart(16, '0');\n}\n","import { performDraw } from '@datadog/browser-core';\nlet sampleDecisionCache;\nexport function isTraceSampled(sessionId, sampleRate) {\n // Shortcuts for common cases. This is not strictly necessary, but it makes the code faster for\n // customers willing to ingest all traces.\n if (sampleRate === 100) {\n return true;\n }\n if (sampleRate === 0) {\n return false;\n }\n if (sampleDecisionCache && sessionId === sampleDecisionCache.sessionId) {\n return sampleDecisionCache.decision;\n }\n let decision;\n // @ts-expect-error BigInt might not be defined in every browser we support\n if (window.BigInt) {\n decision = sampleUsingKnuthFactor(BigInt(`0x${sessionId.split('-')[4]}`), sampleRate);\n } else {\n // For simplicity, we don't use consistent sampling for browser without BigInt support\n // TODO: remove this when all browser we support have BigInt support\n decision = performDraw(sampleRate);\n }\n sampleDecisionCache = {\n sessionId,\n decision\n };\n return decision;\n}\n// Exported for tests\nexport function resetSampleDecisionCache() {\n sampleDecisionCache = undefined;\n}\n/**\n * Perform sampling using the Knuth factor method. This method offer consistent sampling result\n * based on the provided identifier.\n *\n * @param identifier The identifier to use for sampling.\n * @param sampleRate The sample rate in percentage between 0 and 100.\n */\nexport function sampleUsingKnuthFactor(identifier, sampleRate) {\n // The formula is:\n //\n // (identifier * knuthFactor) % 2^64 < sampleRate * 2^64\n //\n // Because JavaScript numbers are 64-bit floats, we can't represent 64-bit integers, and the\n // modulo would be incorrect. Thus, we are using BigInts here.\n //\n // Implementation in other languages:\n // * Go https://github.com/DataDog/dd-trace-go/blob/ec6fbb1f2d517b7b8e69961052adf7136f3af773/ddtrace/tracer/sampler.go#L86-L91\n // * Python https://github.com/DataDog/dd-trace-py/blob/0cee2f066fb6e79aa15947c1514c0f406dea47c5/ddtrace/sampling_rule.py#L197\n // * Ruby https://github.com/DataDog/dd-trace-rb/blob/1a6e255cdcb7e7e22235ea5955f90f6dfa91045d/lib/datadog/tracing/sampling/rate_sampler.rb#L42\n // * C++ https://github.com/DataDog/dd-trace-cpp/blob/159629edc438ae45f2bb318eb7bd51abd05e94b5/src/datadog/trace_sampler.cpp#L58\n // * Java https://github.com/DataDog/dd-trace-java/blob/896dd6b380533216e0bdee59614606c8272d313e/dd-trace-core/src/main/java/datadog/trace/common/sampling/DeterministicSampler.java#L48\n //\n // Note: All implementations have slight variations. Some of them use '<=' instead of '<', and\n // use `sampleRate * 2^64 - 1` instead of `sampleRate * 2^64`. The following implementation\n // should adhere to the spec and is a bit simpler than using a 2^64-1 limit as there are less\n // BigInt arithmetic to write. In practice this does not matter, as we are using floating point\n // numbers in the end, and Number(2n**64n-1n) === Number(2n**64n).\n const knuthFactor = BigInt('1111111111111111111');\n const twoPow64 = BigInt('0x10000000000000000'); // 2n ** 64n\n const hash = identifier * knuthFactor % twoPow64;\n return Number(hash) <= sampleRate / 100 * Number(twoPow64);\n}\n","export const encodedContextCache = new Map();\nexport function getEncodedContext(key) {\n if (!key || typeof key !== 'string') {\n return undefined;\n }\n if (encodedContextCache.has(key)) {\n return encodedContextCache.get(key);\n }\n const encoded = encodeToUtf8Base64(key);\n encodedContextCache.set(key, encoded);\n return encoded;\n}\n/**\n *\n * Helper function to encode a string to base64 in UTF-8\n * Comply with the `_dd.p.usr` standard and avoid non-ASCII characters\n * https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa\n */\nexport function encodeToUtf8Base64(plainText) {\n const bytes = new TextEncoder().encode(plainText);\n const binString = String.fromCodePoint(...bytes);\n return btoa(binString);\n}\n","import { objectEntries, shallowClone, getType, isMatchOption, matchList, TraceContextInjection, isExperimentalFeatureEnabled, ExperimentalFeature } from '@datadog/browser-core';\nimport { createSpanIdentifier, createTraceIdentifier, toPaddedHexadecimalString } from './identifier';\nimport { isTraceSampled } from './sampler';\nimport { getEncodedContext } from './encodedContext';\nexport function isTracingOption(item) {\n const expectedItem = item;\n return getType(expectedItem) === 'object' && isMatchOption(expectedItem.match) && Array.isArray(expectedItem.propagatorTypes);\n}\n/**\n * Clear tracing information to avoid incomplete traces. Ideally, we should do it when the\n * request did not reach the server, but the browser does not expose this. So, we clear tracing\n * information if the request ended with status 0 without being aborted by the application.\n *\n * Reasoning:\n *\n * * Applications are usually aborting requests after a bit of time, for example when the user is\n * typing (autocompletion) or navigating away (in a SPA). With a performant device and good\n * network conditions, the request is likely to reach the server before being canceled.\n *\n * * Requests aborted otherwise (ex: lack of internet, CORS issue, blocked by a privacy extension)\n * are likely to finish quickly and without reaching the server.\n *\n * Of course, it might not be the case every time, but it should limit having incomplete traces a\n * bit.\n * */\nexport function clearTracingIfNeeded(context) {\n if (context.status === 0 && !context.isAborted) {\n context.traceId = undefined;\n context.spanId = undefined;\n context.traceSampled = undefined;\n }\n}\nexport function startTracer(configuration, sessionManager, getCommonContext) {\n return {\n clearTracingIfNeeded,\n traceFetch: context => injectHeadersIfTracingAllowed(configuration, context, sessionManager, getCommonContext, tracingHeaders => {\n var _a;\n if (context.input instanceof Request && !((_a = context.init) === null || _a === void 0 ? void 0 : _a.headers)) {\n context.input = new Request(context.input);\n Object.keys(tracingHeaders).forEach(key => {\n ;\n context.input.headers.append(key, tracingHeaders[key]);\n });\n } else {\n context.init = shallowClone(context.init);\n const headers = [];\n if (context.init.headers instanceof Headers) {\n context.init.headers.forEach((value, key) => {\n headers.push([key, value]);\n });\n } else if (Array.isArray(context.init.headers)) {\n context.init.headers.forEach(header => {\n headers.push(header);\n });\n } else if (context.init.headers) {\n Object.keys(context.init.headers).forEach(key => {\n headers.push([key, context.init.headers[key]]);\n });\n }\n context.init.headers = headers.concat(objectEntries(tracingHeaders));\n }\n }),\n traceXhr: (context, xhr) => injectHeadersIfTracingAllowed(configuration, context, sessionManager, getCommonContext, tracingHeaders => {\n Object.keys(tracingHeaders).forEach(name => {\n xhr.setRequestHeader(name, tracingHeaders[name]);\n });\n })\n };\n}\nfunction injectHeadersIfTracingAllowed(configuration, context, sessionManager, getCommonContext, inject) {\n const session = sessionManager.findTrackedSession();\n if (!session) {\n return;\n }\n const tracingOption = configuration.allowedTracingUrls.find(tracingOption => matchList([tracingOption.match], context.url, true));\n if (!tracingOption) {\n return;\n }\n const traceSampled = isTraceSampled(session.id, configuration.traceSampleRate);\n const shouldInjectHeaders = traceSampled || configuration.traceContextInjection === TraceContextInjection.ALL;\n if (!shouldInjectHeaders) {\n return;\n }\n context.traceSampled = traceSampled;\n context.traceId = createTraceIdentifier();\n context.spanId = createSpanIdentifier();\n inject(makeTracingHeaders(context.traceId, context.spanId, context.traceSampled, tracingOption.propagatorTypes, getCommonContext));\n}\n/**\n * When trace is not sampled, set priority to '0' instead of not adding the tracing headers\n * to prepare the implementation for sampling delegation.\n */\nfunction makeTracingHeaders(traceId, spanId, traceSampled, propagatorTypes, getCommonContext) {\n const tracingHeaders = {};\n propagatorTypes.forEach(propagatorType => {\n switch (propagatorType) {\n case 'datadog':\n {\n Object.assign(tracingHeaders, {\n 'x-datadog-origin': 'rum',\n 'x-datadog-parent-id': spanId.toString(),\n 'x-datadog-sampling-priority': traceSampled ? '1' : '0',\n 'x-datadog-trace-id': traceId.toString()\n });\n break;\n }\n // https://www.w3.org/TR/trace-context/\n case 'tracecontext':\n {\n Object.assign(tracingHeaders, {\n traceparent: `00-0000000000000000${toPaddedHexadecimalString(traceId)}-${toPaddedHexadecimalString(spanId)}-0${traceSampled ? '1' : '0'}`,\n tracestate: `dd=${getTraceStateDatadogItems(traceSampled, getCommonContext).join(';')}`\n });\n break;\n }\n // https://github.com/openzipkin/b3-propagation\n case 'b3':\n {\n Object.assign(tracingHeaders, {\n b3: `${toPaddedHexadecimalString(traceId)}-${toPaddedHexadecimalString(spanId)}-${traceSampled ? '1' : '0'}`\n });\n break;\n }\n case 'b3multi':\n {\n Object.assign(tracingHeaders, {\n 'X-B3-TraceId': toPaddedHexadecimalString(traceId),\n 'X-B3-SpanId': toPaddedHexadecimalString(spanId),\n 'X-B3-Sampled': traceSampled ? '1' : '0'\n });\n break;\n }\n }\n });\n return tracingHeaders;\n}\nfunction getTraceStateDatadogItems(traceSampled, getCommonContext) {\n const traceStateDatadogItems = [`s:${traceSampled ? '1' : '0'}`, 'o:rum'];\n if (isExperimentalFeatureEnabled(ExperimentalFeature.USER_ACCOUNT_TRACE_HEADER)) {\n /**\n * We should only enable this feature after the decoding service is available\n */\n const userIdEncoded = getEncodedContext(getCommonContext().user.id);\n const accountIdEncoded = getEncodedContext(getCommonContext().account.id);\n if (userIdEncoded) {\n traceStateDatadogItems.push(`t.usr.id:${userIdEncoded}`);\n }\n if (accountIdEncoded) {\n traceStateDatadogItems.push(`t.account.id:${accountIdEncoded}`);\n }\n }\n return traceStateDatadogItems;\n}\n","import { getType, isMatchOption, serializeConfiguration, DefaultPrivacyLevel, TraceContextInjection, display, objectHasValue, validateAndBuildConfiguration, isSampleRate, isNumber, isExperimentalFeatureEnabled, ExperimentalFeature } from '@datadog/browser-core';\nimport { isTracingOption } from '../tracing/tracer';\nexport const DEFAULT_PROPAGATOR_TYPES = ['tracecontext', 'datadog'];\nexport function validateAndBuildRumConfiguration(initConfiguration) {\n var _a, _b, _c, _d, _e, _f, _g;\n if (initConfiguration.trackFeatureFlagsForEvents !== undefined && !Array.isArray(initConfiguration.trackFeatureFlagsForEvents)) {\n display.warn('trackFeatureFlagsForEvents should be an array');\n }\n if (!initConfiguration.applicationId) {\n display.error('Application ID is not configured, no RUM data will be collected.');\n return;\n }\n if (!isSampleRate(initConfiguration.sessionReplaySampleRate, 'Session Replay') || !isSampleRate(initConfiguration.traceSampleRate, 'Trace')) {\n return;\n }\n if (initConfiguration.excludedActivityUrls !== undefined && !Array.isArray(initConfiguration.excludedActivityUrls)) {\n display.error('Excluded Activity Urls should be an array');\n return;\n }\n const allowedTracingUrls = validateAndBuildTracingOptions(initConfiguration);\n if (!allowedTracingUrls) {\n return;\n }\n const baseConfiguration = validateAndBuildConfiguration(initConfiguration);\n if (!baseConfiguration) {\n return;\n }\n const profilingEnabled = isExperimentalFeatureEnabled(ExperimentalFeature.PROFILING);\n const sessionReplaySampleRate = (_a = initConfiguration.sessionReplaySampleRate) !== null && _a !== void 0 ? _a : 0;\n return {\n applicationId: initConfiguration.applicationId,\n version: initConfiguration.version || undefined,\n actionNameAttribute: initConfiguration.actionNameAttribute,\n sessionReplaySampleRate,\n startSessionReplayRecordingManually: initConfiguration.startSessionReplayRecordingManually !== undefined ? !!initConfiguration.startSessionReplayRecordingManually : sessionReplaySampleRate === 0,\n traceSampleRate: (_b = initConfiguration.traceSampleRate) !== null && _b !== void 0 ? _b : 100,\n rulePsr: isNumber(initConfiguration.traceSampleRate) ? initConfiguration.traceSampleRate / 100 : undefined,\n allowedTracingUrls,\n excludedActivityUrls: (_c = initConfiguration.excludedActivityUrls) !== null && _c !== void 0 ? _c : [],\n workerUrl: initConfiguration.workerUrl,\n compressIntakeRequests: !!initConfiguration.compressIntakeRequests,\n trackUserInteractions: !!((_d = initConfiguration.trackUserInteractions) !== null && _d !== void 0 ? _d : true),\n trackViewsManually: !!initConfiguration.trackViewsManually,\n trackResources: !!((_e = initConfiguration.trackResources) !== null && _e !== void 0 ? _e : true),\n trackLongTasks: !!((_f = initConfiguration.trackLongTasks) !== null && _f !== void 0 ? _f : true),\n subdomain: initConfiguration.subdomain,\n defaultPrivacyLevel: objectHasValue(DefaultPrivacyLevel, initConfiguration.defaultPrivacyLevel) ? initConfiguration.defaultPrivacyLevel : DefaultPrivacyLevel.MASK,\n enablePrivacyForActionName: !!initConfiguration.enablePrivacyForActionName,\n customerDataTelemetrySampleRate: 1,\n traceContextInjection: objectHasValue(TraceContextInjection, initConfiguration.traceContextInjection) ? initConfiguration.traceContextInjection : TraceContextInjection.SAMPLED,\n plugins: initConfiguration.plugins || [],\n trackFeatureFlagsForEvents: initConfiguration.trackFeatureFlagsForEvents || [],\n profilingSampleRate: profilingEnabled ? (_g = initConfiguration.profilingSampleRate) !== null && _g !== void 0 ? _g : 0 : 0,\n // Enforce 0 if profiling is not enabled, and set 0 as default when not set.\n ...baseConfiguration\n };\n}\n/**\n * Validates allowedTracingUrls and converts match options to tracing options\n */\nfunction validateAndBuildTracingOptions(initConfiguration) {\n if (initConfiguration.allowedTracingUrls === undefined) {\n return [];\n }\n if (!Array.isArray(initConfiguration.allowedTracingUrls)) {\n display.error('Allowed Tracing URLs should be an array');\n return;\n }\n if (initConfiguration.allowedTracingUrls.length !== 0 && initConfiguration.service === undefined) {\n display.error('Service needs to be configured when tracing is enabled');\n return;\n }\n // Convert from (MatchOption | TracingOption) to TracingOption, remove unknown properties\n const tracingOptions = [];\n initConfiguration.allowedTracingUrls.forEach(option => {\n if (isMatchOption(option)) {\n tracingOptions.push({\n match: option,\n propagatorTypes: DEFAULT_PROPAGATOR_TYPES\n });\n } else if (isTracingOption(option)) {\n tracingOptions.push(option);\n } else {\n display.warn('Allowed Tracing Urls parameters should be a string, RegExp, function, or an object. Ignoring parameter', option);\n }\n });\n return tracingOptions;\n}\n/**\n * Combines the selected tracing propagators from the different options in allowedTracingUrls\n */\nfunction getSelectedTracingPropagators(configuration) {\n const usedTracingPropagators = new Set();\n if (Array.isArray(configuration.allowedTracingUrls) && configuration.allowedTracingUrls.length > 0) {\n configuration.allowedTracingUrls.forEach(option => {\n if (isMatchOption(option)) {\n DEFAULT_PROPAGATOR_TYPES.forEach(propagatorType => usedTracingPropagators.add(propagatorType));\n } else if (getType(option) === 'object' && Array.isArray(option.propagatorTypes)) {\n // Ensure we have an array, as we cannot rely on types yet (configuration is provided by users)\n option.propagatorTypes.forEach(propagatorType => usedTracingPropagators.add(propagatorType));\n }\n });\n }\n return Array.from(usedTracingPropagators);\n}\nexport function serializeRumConfiguration(configuration) {\n var _a;\n const baseSerializedConfiguration = serializeConfiguration(configuration);\n return {\n session_replay_sample_rate: configuration.sessionReplaySampleRate,\n start_session_replay_recording_manually: configuration.startSessionReplayRecordingManually,\n trace_sample_rate: configuration.traceSampleRate,\n trace_context_injection: configuration.traceContextInjection,\n action_name_attribute: configuration.actionNameAttribute,\n use_allowed_tracing_urls: Array.isArray(configuration.allowedTracingUrls) && configuration.allowedTracingUrls.length > 0,\n selected_tracing_propagators: getSelectedTracingPropagators(configuration),\n default_privacy_level: configuration.defaultPrivacyLevel,\n enable_privacy_for_action_name: configuration.enablePrivacyForActionName,\n use_excluded_activity_urls: Array.isArray(configuration.excludedActivityUrls) && configuration.excludedActivityUrls.length > 0,\n use_worker_url: !!configuration.workerUrl,\n compress_intake_requests: configuration.compressIntakeRequests,\n track_views_manually: configuration.trackViewsManually,\n track_user_interactions: configuration.trackUserInteractions,\n track_resources: configuration.trackResources,\n track_long_task: configuration.trackLongTasks,\n plugins: (_a = configuration.plugins) === null || _a === void 0 ? void 0 : _a.map(plugin => {\n var _a;\n return {\n name: plugin.name,\n ...((_a = plugin.getConfigurationTelemetry) === null || _a === void 0 ? void 0 : _a.call(plugin))\n };\n }),\n track_feature_flags_for_events: configuration.trackFeatureFlagsForEvents,\n ...baseSerializedConfiguration\n };\n}\n","import { display, addEventListener, buildEndpointHost } from '@datadog/browser-core';\nconst REMOTE_CONFIGURATION_VERSION = 'v1';\nexport function fetchAndApplyRemoteConfiguration(initConfiguration, callback) {\n fetchRemoteConfiguration(initConfiguration, remoteInitConfiguration => {\n callback(applyRemoteConfiguration(initConfiguration, remoteInitConfiguration));\n });\n}\nexport function applyRemoteConfiguration(initConfiguration, remoteInitConfiguration) {\n return {\n ...initConfiguration,\n ...remoteInitConfiguration\n };\n}\nexport function fetchRemoteConfiguration(configuration, callback) {\n const xhr = new XMLHttpRequest();\n addEventListener(configuration, xhr, 'load', function () {\n if (xhr.status === 200) {\n const remoteConfiguration = JSON.parse(xhr.responseText);\n callback(remoteConfiguration.rum);\n } else {\n displayRemoteConfigurationFetchingError();\n }\n });\n addEventListener(configuration, xhr, 'error', function () {\n displayRemoteConfigurationFetchingError();\n });\n xhr.open('GET', buildEndpoint(configuration));\n xhr.send();\n}\nexport function buildEndpoint(configuration) {\n return `https://sdk-configuration.${buildEndpointHost('rum', configuration)}/${REMOTE_CONFIGURATION_VERSION}/${encodeURIComponent(configuration.remoteConfigurationId)}.json`;\n}\nfunction displayRemoteConfigurationFetchingError() {\n display.error('Error fetching the remote configuration.');\n}\n","import { createBoundedBuffer, display, canUseEventBridge, displayAlreadyInitializedError, willSyntheticsInjectRum, noop, timeStampNow, clocksNow, getEventBridge, initFeatureFlags, addTelemetryConfiguration, initFetchObservable } from '@datadog/browser-core';\nimport { validateAndBuildRumConfiguration } from '../domain/configuration';\nimport { startDurationVital, stopDurationVital } from '../domain/vital/vitalCollection';\nimport { fetchAndApplyRemoteConfiguration, serializeRumConfiguration } from '../domain/configuration';\nimport { callPluginsMethod } from '../domain/plugins';\nexport function createPreStartStrategy({\n ignoreInitIfSyntheticsWillInjectRum,\n startDeflateWorker\n}, getCommonContext, trackingConsentState, customVitalsState, doStartRum) {\n const bufferApiCalls = createBoundedBuffer();\n let firstStartViewCall;\n let deflateWorker;\n let cachedInitConfiguration;\n let cachedConfiguration;\n const trackingConsentStateSubscription = trackingConsentState.observable.subscribe(tryStartRum);\n const emptyContext = {};\n function tryStartRum() {\n if (!cachedInitConfiguration || !cachedConfiguration || !trackingConsentState.isGranted()) {\n return;\n }\n trackingConsentStateSubscription.unsubscribe();\n let initialViewOptions;\n if (cachedConfiguration.trackViewsManually) {\n if (!firstStartViewCall) {\n return;\n }\n // An initial view is always created when starting RUM.\n // When tracking views automatically, any startView call before RUM start creates an extra\n // view.\n // When tracking views manually, we use the ViewOptions from the first startView call as the\n // initial view options, and we remove the actual startView call so we don't create an extra\n // view.\n bufferApiCalls.remove(firstStartViewCall.callback);\n initialViewOptions = firstStartViewCall.options;\n }\n const startRumResult = doStartRum(cachedConfiguration, deflateWorker, initialViewOptions);\n bufferApiCalls.drain(startRumResult);\n }\n function doInit(initConfiguration) {\n const eventBridgeAvailable = canUseEventBridge();\n if (eventBridgeAvailable) {\n initConfiguration = overrideInitConfigurationForBridge(initConfiguration);\n }\n // Update the exposed initConfiguration to reflect the bridge and remote configuration overrides\n cachedInitConfiguration = initConfiguration;\n addTelemetryConfiguration(serializeRumConfiguration(initConfiguration));\n if (cachedConfiguration) {\n displayAlreadyInitializedError('DD_RUM', initConfiguration);\n return;\n }\n const configuration = validateAndBuildRumConfiguration(initConfiguration);\n if (!configuration) {\n return;\n }\n if (!eventBridgeAvailable && !configuration.sessionStoreStrategyType) {\n display.warn('No storage available for session. We will not send any data.');\n return;\n }\n if (configuration.compressIntakeRequests && !eventBridgeAvailable && startDeflateWorker) {\n deflateWorker = startDeflateWorker(configuration, 'Datadog RUM',\n // Worker initialization can fail asynchronously, especially in Firefox where even CSP\n // issues are reported asynchronously. For now, the SDK will continue its execution even if\n // data won't be sent to Datadog. We could improve this behavior in the future.\n noop);\n if (!deflateWorker) {\n // `startDeflateWorker` should have logged an error message explaining the issue\n return;\n }\n }\n cachedConfiguration = configuration;\n // Instrumuent fetch to track network requests\n // This is needed in case the consent is not granted and some cutsomer\n // library (Apollo Client) is storing uninstrumented fetch to be used later\n // The subscrption is needed so that the instrumentation process is completed\n initFetchObservable().subscribe(noop);\n trackingConsentState.tryToInit(configuration.trackingConsent);\n tryStartRum();\n }\n const addDurationVital = vital => {\n bufferApiCalls.add(startRumResult => startRumResult.addDurationVital(vital));\n };\n const strategy = {\n init(initConfiguration, publicApi) {\n if (!initConfiguration) {\n display.error('Missing configuration');\n return;\n }\n // Set the experimental feature flags as early as possible, so we can use them in most places\n initFeatureFlags(initConfiguration.enableExperimentalFeatures);\n // Expose the initial configuration regardless of initialization success.\n cachedInitConfiguration = initConfiguration;\n // If we are in a Synthetics test configured to automatically inject a RUM instance, we want\n // to completely discard the customer application RUM instance by ignoring their init() call.\n // But, we should not ignore the init() call from the Synthetics-injected RUM instance, so the\n // internal `ignoreInitIfSyntheticsWillInjectRum` option is here to bypass this condition.\n if (ignoreInitIfSyntheticsWillInjectRum && willSyntheticsInjectRum()) {\n return;\n }\n callPluginsMethod(initConfiguration.plugins, 'onInit', {\n initConfiguration,\n publicApi\n });\n if (initConfiguration.remoteConfigurationId) {\n fetchAndApplyRemoteConfiguration(initConfiguration, doInit);\n } else {\n doInit(initConfiguration);\n }\n },\n get initConfiguration() {\n return cachedInitConfiguration;\n },\n getInternalContext: noop,\n stopSession: noop,\n addTiming(name, time = timeStampNow()) {\n bufferApiCalls.add(startRumResult => startRumResult.addTiming(name, time));\n },\n startView(options, startClocks = clocksNow()) {\n const callback = startRumResult => {\n startRumResult.startView(options, startClocks);\n };\n bufferApiCalls.add(callback);\n if (!firstStartViewCall) {\n firstStartViewCall = {\n options,\n callback\n };\n tryStartRum();\n }\n },\n setViewName(name) {\n bufferApiCalls.add(startRumResult => startRumResult.setViewName(name));\n },\n setViewContext(context) {\n bufferApiCalls.add(startRumResult => startRumResult.setViewContext(context));\n },\n setViewContextProperty(key, value) {\n bufferApiCalls.add(startRumResult => startRumResult.setViewContextProperty(key, value));\n },\n getViewContext: () => emptyContext,\n addAction(action, commonContext = getCommonContext()) {\n bufferApiCalls.add(startRumResult => startRumResult.addAction(action, commonContext));\n },\n addError(providedError, commonContext = getCommonContext()) {\n bufferApiCalls.add(startRumResult => startRumResult.addError(providedError, commonContext));\n },\n addFeatureFlagEvaluation(key, value) {\n bufferApiCalls.add(startRumResult => startRumResult.addFeatureFlagEvaluation(key, value));\n },\n startDurationVital(name, options) {\n return startDurationVital(customVitalsState, name, options);\n },\n stopDurationVital(name, options) {\n stopDurationVital(addDurationVital, customVitalsState, name, options);\n },\n addDurationVital\n };\n return strategy;\n}\nfunction overrideInitConfigurationForBridge(initConfiguration) {\n var _a, _b;\n return {\n ...initConfiguration,\n applicationId: '00000000-aaaa-0000-aaaa-000000000000',\n clientToken: 'empty',\n sessionSampleRate: 100,\n defaultPrivacyLevel: (_a = initConfiguration.defaultPrivacyLevel) !== null && _a !== void 0 ? _a : (_b = getEventBridge()) === null || _b === void 0 ? void 0 : _b.getPrivacyLevel()\n };\n}\n","import { addTelemetryUsage, createContextManager, deepClone, makePublicApi, monitor, clocksNow, callMonitored, createHandlingStack, sanitize, createIdentityEncoder, createCustomerDataTrackerManager, storeContextManager, displayAlreadyInitializedError, createTrackingConsentState, timeStampToClocks } from '@datadog/browser-core';\nimport { buildCommonContext } from '../domain/contexts/commonContext';\nimport { createCustomVitalsState } from '../domain/vital/vitalCollection';\nimport { callPluginsMethod } from '../domain/plugins';\nimport { createPreStartStrategy } from './preStartRum';\nconst RUM_STORAGE_KEY = 'rum';\nexport function makeRumPublicApi(startRumImpl, recorderApi, profilerApi, options = {}) {\n const customerDataTrackerManager = createCustomerDataTrackerManager(0 /* CustomerDataCompressionStatus.Unknown */);\n const globalContextManager = createContextManager('global context', {\n customerDataTracker: customerDataTrackerManager.getOrCreateTracker(2 /* CustomerDataType.GlobalContext */)\n });\n const userContextManager = createContextManager('user', {\n customerDataTracker: customerDataTrackerManager.getOrCreateTracker(1 /* CustomerDataType.User */),\n propertiesConfig: {\n id: {\n type: 'string'\n },\n name: {\n type: 'string'\n },\n email: {\n type: 'string'\n }\n }\n });\n const accountContextManager = createContextManager('account', {\n customerDataTracker: customerDataTrackerManager.getOrCreateTracker(4 /* CustomerDataType.Account */),\n propertiesConfig: {\n id: {\n type: 'string',\n required: true\n },\n name: {\n type: 'string'\n }\n }\n });\n const trackingConsentState = createTrackingConsentState();\n const customVitalsState = createCustomVitalsState();\n function getCommonContext() {\n return buildCommonContext(globalContextManager, userContextManager, accountContextManager, recorderApi);\n }\n let strategy = createPreStartStrategy(options, getCommonContext, trackingConsentState, customVitalsState, (configuration, deflateWorker, initialViewOptions) => {\n if (configuration.storeContextsAcrossPages) {\n storeContextManager(configuration, globalContextManager, RUM_STORAGE_KEY, 2 /* CustomerDataType.GlobalContext */);\n storeContextManager(configuration, userContextManager, RUM_STORAGE_KEY, 1 /* CustomerDataType.User */);\n storeContextManager(configuration, accountContextManager, RUM_STORAGE_KEY, 4 /* CustomerDataType.Account */);\n }\n customerDataTrackerManager.setCompressionStatus(deflateWorker ? 1 /* CustomerDataCompressionStatus.Enabled */ : 2 /* CustomerDataCompressionStatus.Disabled */);\n const startRumResult = startRumImpl(configuration, recorderApi, profilerApi, customerDataTrackerManager, getCommonContext, initialViewOptions, deflateWorker && options.createDeflateEncoder ? streamId => options.createDeflateEncoder(configuration, deflateWorker, streamId) : createIdentityEncoder, trackingConsentState, customVitalsState);\n recorderApi.onRumStart(startRumResult.lifeCycle, configuration, startRumResult.session, startRumResult.viewHistory, deflateWorker);\n profilerApi.onRumStart(startRumResult.lifeCycle, configuration, startRumResult.session, startRumResult.viewHistory);\n strategy = createPostStartStrategy(strategy, startRumResult);\n callPluginsMethod(configuration.plugins, 'onRumStart', {\n strategy\n });\n return startRumResult;\n });\n const startView = monitor(options => {\n const sanitizedOptions = typeof options === 'object' ? options : {\n name: options\n };\n if (sanitizedOptions.context) {\n customerDataTrackerManager.getOrCreateTracker(3 /* CustomerDataType.View */).updateCustomerData(sanitizedOptions.context);\n }\n strategy.startView(sanitizedOptions);\n addTelemetryUsage({\n feature: 'start-view'\n });\n });\n const rumPublicApi = makePublicApi({\n init: monitor(initConfiguration => {\n strategy.init(initConfiguration, rumPublicApi);\n }),\n setTrackingConsent: monitor(trackingConsent => {\n trackingConsentState.update(trackingConsent);\n addTelemetryUsage({\n feature: 'set-tracking-consent',\n tracking_consent: trackingConsent\n });\n }),\n setViewName: monitor(name => {\n strategy.setViewName(name);\n addTelemetryUsage({\n feature: 'set-view-name'\n });\n }),\n setViewContext: monitor(context => {\n strategy.setViewContext(context);\n addTelemetryUsage({\n feature: 'set-view-context'\n });\n }),\n setViewContextProperty: monitor((key, value) => {\n strategy.setViewContextProperty(key, value);\n addTelemetryUsage({\n feature: 'set-view-context-property'\n });\n }),\n getViewContext: monitor(() => {\n addTelemetryUsage({\n feature: 'set-view-context-property'\n });\n return strategy.getViewContext();\n }),\n setGlobalContext: monitor(context => {\n globalContextManager.setContext(context);\n addTelemetryUsage({\n feature: 'set-global-context'\n });\n }),\n getGlobalContext: monitor(() => globalContextManager.getContext()),\n setGlobalContextProperty: monitor((key, value) => {\n globalContextManager.setContextProperty(key, value);\n addTelemetryUsage({\n feature: 'set-global-context'\n });\n }),\n removeGlobalContextProperty: monitor(key => globalContextManager.removeContextProperty(key)),\n clearGlobalContext: monitor(() => globalContextManager.clearContext()),\n getInternalContext: monitor(startTime => strategy.getInternalContext(startTime)),\n getInitConfiguration: monitor(() => deepClone(strategy.initConfiguration)),\n addAction: (name, context) => {\n const handlingStack = createHandlingStack('action');\n callMonitored(() => {\n strategy.addAction({\n name: sanitize(name),\n context: sanitize(context),\n startClocks: clocksNow(),\n type: \"custom\" /* ActionType.CUSTOM */,\n handlingStack\n });\n addTelemetryUsage({\n feature: 'add-action'\n });\n });\n },\n addError: (error, context) => {\n const handlingStack = createHandlingStack('error');\n callMonitored(() => {\n strategy.addError({\n error,\n // Do not sanitize error here, it is needed unserialized by computeRawError()\n handlingStack,\n context: sanitize(context),\n startClocks: clocksNow()\n });\n addTelemetryUsage({\n feature: 'add-error'\n });\n });\n },\n addTiming: monitor((name, time) => {\n // TODO: next major decide to drop relative time support or update its behaviour\n strategy.addTiming(sanitize(name), time);\n }),\n setUser: monitor(newUser => {\n userContextManager.setContext(newUser);\n addTelemetryUsage({\n feature: 'set-user'\n });\n }),\n getUser: monitor(userContextManager.getContext),\n setUserProperty: monitor((key, property) => {\n userContextManager.setContextProperty(key, property);\n addTelemetryUsage({\n feature: 'set-user'\n });\n }),\n removeUserProperty: monitor(userContextManager.removeContextProperty),\n clearUser: monitor(userContextManager.clearContext),\n setAccount: monitor(accountContextManager.setContext),\n getAccount: monitor(accountContextManager.getContext),\n setAccountProperty: monitor(accountContextManager.setContextProperty),\n removeAccountProperty: monitor(accountContextManager.removeContextProperty),\n clearAccount: monitor(accountContextManager.clearContext),\n startView,\n stopSession: monitor(() => {\n strategy.stopSession();\n addTelemetryUsage({\n feature: 'stop-session'\n });\n }),\n addFeatureFlagEvaluation: monitor((key, value) => {\n strategy.addFeatureFlagEvaluation(sanitize(key), sanitize(value));\n addTelemetryUsage({\n feature: 'add-feature-flag-evaluation'\n });\n }),\n getSessionReplayLink: monitor(() => recorderApi.getSessionReplayLink()),\n startSessionReplayRecording: monitor(options => {\n recorderApi.start(options);\n addTelemetryUsage({\n feature: 'start-session-replay-recording',\n force: options && options.force\n });\n }),\n stopSessionReplayRecording: monitor(() => recorderApi.stop()),\n addDurationVital: monitor((name, options) => {\n addTelemetryUsage({\n feature: 'add-duration-vital'\n });\n strategy.addDurationVital({\n name: sanitize(name),\n type: \"duration\" /* VitalType.DURATION */,\n startClocks: timeStampToClocks(options.startTime),\n duration: options.duration,\n context: sanitize(options && options.context),\n description: sanitize(options && options.description)\n });\n }),\n startDurationVital: monitor((name, options) => {\n addTelemetryUsage({\n feature: 'start-duration-vital'\n });\n return strategy.startDurationVital(sanitize(name), {\n context: sanitize(options && options.context),\n description: sanitize(options && options.description)\n });\n }),\n stopDurationVital: monitor((nameOrRef, options) => {\n addTelemetryUsage({\n feature: 'stop-duration-vital'\n });\n strategy.stopDurationVital(typeof nameOrRef === 'string' ? sanitize(nameOrRef) : nameOrRef, {\n context: sanitize(options && options.context),\n description: sanitize(options && options.description)\n });\n })\n });\n return rumPublicApi;\n}\nfunction createPostStartStrategy(preStartStrategy, startRumResult) {\n return {\n init: initConfiguration => {\n displayAlreadyInitializedError('DD_RUM', initConfiguration);\n },\n initConfiguration: preStartStrategy.initConfiguration,\n ...startRumResult\n };\n}\n","import { monitor, noop, Observable, getZoneJsOriginalValue } from '@datadog/browser-core';\nexport function createDOMMutationObservable() {\n const MutationObserver = getMutationObserverConstructor();\n return new Observable(observable => {\n if (!MutationObserver) {\n return;\n }\n const observer = new MutationObserver(monitor(() => observable.notify()));\n observer.observe(document, {\n attributes: true,\n characterData: true,\n childList: true,\n subtree: true\n });\n return () => observer.disconnect();\n });\n}\nexport function getMutationObserverConstructor() {\n let constructor;\n const browserWindow = window;\n // Angular uses Zone.js to provide a context persisting across async tasks. Zone.js replaces the\n // global MutationObserver constructor with a patched version to support the context propagation.\n // There is an ongoing issue[1][2] with this setup when using a MutationObserver within a Angular\n // component: on some occasions, the callback is being called in an infinite loop, causing the\n // page to freeze (even if the callback is completely empty).\n //\n // To work around this issue, we try to get the original MutationObserver constructor stored by\n // Zone.js.\n //\n // [1] https://github.com/angular/angular/issues/26948\n // [2] https://github.com/angular/angular/issues/31712\n if (browserWindow.Zone) {\n // Zone.js 0.8.6+ is storing original class constructors into the browser 'window' object[3].\n //\n // [3] https://github.com/angular/angular/blob/6375fa79875c0fe7b815efc45940a6e6f5c9c9eb/packages/zone.js/lib/common/utils.ts#L288\n constructor = getZoneJsOriginalValue(browserWindow, 'MutationObserver');\n if (browserWindow.MutationObserver && constructor === browserWindow.MutationObserver) {\n // Anterior Zone.js versions (used in Angular 2) does not expose the original MutationObserver\n // in the 'window' object. Luckily, the patched MutationObserver class is storing an original\n // instance in its properties[4]. Let's get the original MutationObserver constructor from\n // there.\n //\n // [4] https://github.com/angular/zone.js/blob/v0.8.5/lib/common/utils.ts#L412\n const patchedInstance = new browserWindow.MutationObserver(noop);\n const originalInstance = getZoneJsOriginalValue(patchedInstance, 'originalInstance');\n constructor = originalInstance && originalInstance.constructor;\n }\n }\n if (!constructor) {\n constructor = browserWindow.MutationObserver;\n }\n return constructor;\n}\n","import { instrumentMethod, Observable } from '@datadog/browser-core';\nexport function createWindowOpenObservable() {\n const observable = new Observable();\n const {\n stop\n } = instrumentMethod(window, 'open', () => observable.notify());\n return {\n observable,\n stop\n };\n}\n","import { sanitize, deepClone, getType, objectEntries } from '@datadog/browser-core';\n/**\n * Allows declaring and enforcing modifications to specific fields of an object.\n * Only supports modifying properties of an object (even if nested in an array).\n * Does not support array manipulation (adding/removing items).\n */\nexport function limitModification(object, modifiableFieldPaths, modifier) {\n const clone = deepClone(object);\n const result = modifier(clone);\n objectEntries(modifiableFieldPaths).forEach(([fieldPath, fieldType]) =>\n // Traverse both object and clone simultaneously up to the path and apply the modification from the clone to the original object when the type is valid\n setValueAtPath(object, clone, fieldPath.split(/\\.|(?=\\[\\])/), fieldType));\n return result;\n}\nfunction setValueAtPath(object, clone, pathSegments, fieldType) {\n const [field, ...restPathSegments] = pathSegments;\n if (field === '[]') {\n if (Array.isArray(object) && Array.isArray(clone)) {\n object.forEach((item, i) => setValueAtPath(item, clone[i], restPathSegments, fieldType));\n }\n return;\n }\n if (!isValidObject(object) || !isValidObject(clone)) {\n return;\n }\n if (restPathSegments.length > 0) {\n return setValueAtPath(object[field], clone[field], restPathSegments, fieldType);\n }\n setNestedValue(object, field, clone[field], fieldType);\n}\nfunction setNestedValue(object, field, value, fieldType) {\n const newType = getType(value);\n if (newType === fieldType) {\n object[field] = sanitize(value);\n } else if (fieldType === 'object' && (newType === 'undefined' || newType === 'null')) {\n object[field] = {};\n }\n}\nfunction isValidObject(object) {\n return getType(object) === 'object';\n}\n","import { combine, isEmptyObject, timeStampNow, currentDrift, display, createEventRateLimiter, canUseEventBridge, round, isExperimentalFeatureEnabled, ExperimentalFeature, getConnectivity, addTelemetryDebug } from '@datadog/browser-core';\nimport { limitModification } from './limitModification';\nconst VIEW_MODIFIABLE_FIELD_PATHS = {\n 'view.name': 'string',\n 'view.url': 'string',\n 'view.referrer': 'string'\n};\nconst USER_CUSTOMIZABLE_FIELD_PATHS = {\n context: 'object'\n};\nconst ROOT_MODIFIABLE_FIELD_PATHS = {\n service: 'string',\n version: 'string'\n};\nlet modifiableFieldPathsByEvent;\nexport function startRumAssembly(configuration, lifeCycle, hooks, sessionManager, viewHistory, urlContexts, displayContext, getCommonContext, reportError) {\n modifiableFieldPathsByEvent = {\n [\"view\" /* RumEventType.VIEW */]: {\n 'view.performance.lcp.resource_url': 'string',\n ...USER_CUSTOMIZABLE_FIELD_PATHS,\n ...VIEW_MODIFIABLE_FIELD_PATHS\n },\n [\"error\" /* RumEventType.ERROR */]: {\n 'error.message': 'string',\n 'error.stack': 'string',\n 'error.resource.url': 'string',\n 'error.fingerprint': 'string',\n ...USER_CUSTOMIZABLE_FIELD_PATHS,\n ...VIEW_MODIFIABLE_FIELD_PATHS,\n ...ROOT_MODIFIABLE_FIELD_PATHS\n },\n [\"resource\" /* RumEventType.RESOURCE */]: {\n 'resource.url': 'string',\n ...(isExperimentalFeatureEnabled(ExperimentalFeature.WRITABLE_RESOURCE_GRAPHQL) ? {\n 'resource.graphql': 'object'\n } : {}),\n ...USER_CUSTOMIZABLE_FIELD_PATHS,\n ...VIEW_MODIFIABLE_FIELD_PATHS,\n ...ROOT_MODIFIABLE_FIELD_PATHS\n },\n [\"action\" /* RumEventType.ACTION */]: {\n 'action.target.name': 'string',\n ...USER_CUSTOMIZABLE_FIELD_PATHS,\n ...VIEW_MODIFIABLE_FIELD_PATHS,\n ...ROOT_MODIFIABLE_FIELD_PATHS\n },\n [\"long_task\" /* RumEventType.LONG_TASK */]: {\n 'long_task.scripts[].source_url': 'string',\n 'long_task.scripts[].invoker': 'string',\n ...USER_CUSTOMIZABLE_FIELD_PATHS,\n ...VIEW_MODIFIABLE_FIELD_PATHS\n },\n [\"vital\" /* RumEventType.VITAL */]: {\n ...USER_CUSTOMIZABLE_FIELD_PATHS,\n ...VIEW_MODIFIABLE_FIELD_PATHS\n }\n };\n const eventRateLimiters = {\n [\"error\" /* RumEventType.ERROR */]: createEventRateLimiter(\"error\" /* RumEventType.ERROR */, configuration.eventRateLimiterThreshold, reportError),\n [\"action\" /* RumEventType.ACTION */]: createEventRateLimiter(\"action\" /* RumEventType.ACTION */, configuration.eventRateLimiterThreshold, reportError),\n [\"vital\" /* RumEventType.VITAL */]: createEventRateLimiter(\"vital\" /* RumEventType.VITAL */, configuration.eventRateLimiterThreshold, reportError)\n };\n lifeCycle.subscribe(12 /* LifeCycleEventType.RAW_RUM_EVENT_COLLECTED */, ({\n startTime,\n duration,\n rawRumEvent,\n domainContext,\n savedCommonContext,\n customerContext\n }) => {\n const viewHistoryEntry = viewHistory.findView(startTime);\n const urlContext = urlContexts.findUrl(startTime);\n const session = sessionManager.findTrackedSession(startTime);\n if (session && viewHistoryEntry && !urlContext && isExperimentalFeatureEnabled(ExperimentalFeature.MISSING_URL_CONTEXT_TELEMETRY)) {\n addTelemetryDebug('Missing URL entry', {\n debug: {\n eventType: rawRumEvent.type,\n startTime,\n urlEntries: urlContexts.getAllEntries(),\n urlDeletedEntries: urlContexts.getDeletedEntries(),\n viewEntries: viewHistory.getAllEntries(),\n viewDeletedEntries: viewHistory.getDeletedEntries()\n }\n });\n }\n if (session && viewHistoryEntry && urlContext) {\n const commonContext = savedCommonContext || getCommonContext();\n const rumContext = {\n _dd: {\n format_version: 2,\n drift: currentDrift(),\n configuration: {\n session_sample_rate: round(configuration.sessionSampleRate, 3),\n session_replay_sample_rate: round(configuration.sessionReplaySampleRate, 3)\n },\n browser_sdk_version: canUseEventBridge() ? \"6.6.2\" : undefined\n },\n application: {\n id: configuration.applicationId\n },\n date: timeStampNow(),\n source: 'browser',\n session: {\n id: session.id,\n type: \"user\" /* SessionType.USER */\n },\n display: displayContext.get(),\n connectivity: getConnectivity(),\n context: commonContext.context\n };\n const serverRumEvent = combine(rumContext, hooks.triggerHook(0 /* HookNames.Assemble */, {\n eventType: rawRumEvent.type,\n startTime,\n duration\n }), {\n context: customerContext\n }, rawRumEvent);\n if (!('has_replay' in serverRumEvent.session)) {\n ;\n serverRumEvent.session.has_replay = commonContext.hasReplay;\n }\n if (serverRumEvent.type === 'view') {\n ;\n serverRumEvent.session.sampled_for_replay = session.sessionReplay === 1 /* SessionReplayState.SAMPLED */;\n }\n if (session.anonymousId && !commonContext.user.anonymous_id && !!configuration.trackAnonymousUser) {\n commonContext.user.anonymous_id = session.anonymousId;\n }\n if (!isEmptyObject(commonContext.user)) {\n ;\n serverRumEvent.usr = commonContext.user;\n }\n if (!isEmptyObject(commonContext.account) && commonContext.account.id) {\n ;\n serverRumEvent.account = commonContext.account;\n }\n if (shouldSend(serverRumEvent, configuration.beforeSend, domainContext, eventRateLimiters)) {\n if (isEmptyObject(serverRumEvent.context)) {\n delete serverRumEvent.context;\n }\n lifeCycle.notify(13 /* LifeCycleEventType.RUM_EVENT_COLLECTED */, serverRumEvent);\n }\n }\n });\n}\nfunction shouldSend(event, beforeSend, domainContext, eventRateLimiters) {\n var _a;\n if (beforeSend) {\n const result = limitModification(event, modifiableFieldPathsByEvent[event.type], event => beforeSend(event, domainContext));\n if (result === false && event.type !== \"view\" /* RumEventType.VIEW */) {\n return false;\n }\n if (result === false) {\n display.warn(\"Can't dismiss view events using beforeSend!\");\n }\n }\n const rateLimitReached = (_a = eventRateLimiters[event.type]) === null || _a === void 0 ? void 0 : _a.isLimitReached();\n return !rateLimitReached;\n}\n","/**\n * Internal context keep returning v1 format\n * to not break compatibility with logs data format\n */\nexport function startInternalContext(applicationId, sessionManager, viewHistory, actionContexts, urlContexts) {\n return {\n get: startTime => {\n const viewContext = viewHistory.findView(startTime);\n const urlContext = urlContexts.findUrl(startTime);\n const session = sessionManager.findTrackedSession(startTime);\n if (session && viewContext && urlContext) {\n const actionId = actionContexts.findActionId(startTime);\n return {\n application_id: applicationId,\n session_id: session.id,\n user_action: actionId ? {\n id: actionId\n } : undefined,\n view: {\n id: viewContext.id,\n name: viewContext.name,\n referrer: urlContext.referrer,\n url: urlContext.url\n }\n };\n }\n }\n };\n}\n","import { AbstractLifeCycle } from '@datadog/browser-core';\nexport const LifeCycle = AbstractLifeCycle;\n","import { SESSION_TIME_OUT_DELAY, createValueHistory } from '@datadog/browser-core';\nexport const VIEW_CONTEXT_TIME_OUT_DELAY = SESSION_TIME_OUT_DELAY;\nexport function startViewHistory(lifeCycle) {\n const viewValueHistory = createValueHistory({\n expireDelay: VIEW_CONTEXT_TIME_OUT_DELAY\n });\n lifeCycle.subscribe(1 /* LifeCycleEventType.BEFORE_VIEW_CREATED */, view => {\n viewValueHistory.add(buildViewHistoryEntry(view), view.startClocks.relative);\n });\n lifeCycle.subscribe(6 /* LifeCycleEventType.AFTER_VIEW_ENDED */, ({\n endClocks\n }) => {\n viewValueHistory.closeActive(endClocks.relative);\n });\n lifeCycle.subscribe(3 /* LifeCycleEventType.BEFORE_VIEW_UPDATED */, viewUpdate => {\n const currentView = viewValueHistory.find(viewUpdate.startClocks.relative);\n if (currentView && viewUpdate.name) {\n currentView.name = viewUpdate.name;\n }\n if (currentView && viewUpdate.context) {\n currentView.context = viewUpdate.context;\n }\n });\n lifeCycle.subscribe(10 /* LifeCycleEventType.SESSION_RENEWED */, () => {\n viewValueHistory.reset();\n });\n function buildViewHistoryEntry(view) {\n return {\n service: view.service,\n version: view.version,\n context: view.context,\n id: view.id,\n name: view.name,\n startClocks: view.startClocks\n };\n }\n return {\n findView: startTime => viewValueHistory.find(startTime),\n getAllEntries: () => viewValueHistory.getAllEntries(),\n getDeletedEntries: () => viewValueHistory.getDeletedEntries(),\n stop: () => {\n viewValueHistory.stop();\n }\n };\n}\n","import { initFetchObservable, initXhrObservable, readBytesFromStream, elapsed, timeStampNow, tryToClone } from '@datadog/browser-core';\nimport { isAllowedRequestUrl } from './resource/resourceUtils';\nimport { startTracer } from './tracing/tracer';\nlet nextRequestIndex = 1;\nexport function startRequestCollection(lifeCycle, configuration, sessionManager, getCommonContext) {\n const tracer = startTracer(configuration, sessionManager, getCommonContext);\n trackXhr(lifeCycle, configuration, tracer);\n trackFetch(lifeCycle, tracer);\n}\nexport function trackXhr(lifeCycle, configuration, tracer) {\n const subscription = initXhrObservable(configuration).subscribe(rawContext => {\n const context = rawContext;\n if (!isAllowedRequestUrl(context.url)) {\n return;\n }\n switch (context.state) {\n case 'start':\n tracer.traceXhr(context, context.xhr);\n context.requestIndex = getNextRequestIndex();\n lifeCycle.notify(7 /* LifeCycleEventType.REQUEST_STARTED */, {\n requestIndex: context.requestIndex,\n url: context.url\n });\n break;\n case 'complete':\n tracer.clearTracingIfNeeded(context);\n lifeCycle.notify(8 /* LifeCycleEventType.REQUEST_COMPLETED */, {\n duration: context.duration,\n method: context.method,\n requestIndex: context.requestIndex,\n spanId: context.spanId,\n startClocks: context.startClocks,\n status: context.status,\n traceId: context.traceId,\n traceSampled: context.traceSampled,\n type: \"xhr\" /* RequestType.XHR */,\n url: context.url,\n xhr: context.xhr,\n isAborted: context.isAborted,\n handlingStack: context.handlingStack\n });\n break;\n }\n });\n return {\n stop: () => subscription.unsubscribe()\n };\n}\nexport function trackFetch(lifeCycle, tracer) {\n const subscription = initFetchObservable().subscribe(rawContext => {\n const context = rawContext;\n if (!isAllowedRequestUrl(context.url)) {\n return;\n }\n switch (context.state) {\n case 'start':\n tracer.traceFetch(context);\n context.requestIndex = getNextRequestIndex();\n lifeCycle.notify(7 /* LifeCycleEventType.REQUEST_STARTED */, {\n requestIndex: context.requestIndex,\n url: context.url\n });\n break;\n case 'resolve':\n waitForResponseToComplete(context, duration => {\n tracer.clearTracingIfNeeded(context);\n lifeCycle.notify(8 /* LifeCycleEventType.REQUEST_COMPLETED */, {\n duration,\n method: context.method,\n requestIndex: context.requestIndex,\n responseType: context.responseType,\n spanId: context.spanId,\n startClocks: context.startClocks,\n status: context.status,\n traceId: context.traceId,\n traceSampled: context.traceSampled,\n type: \"fetch\" /* RequestType.FETCH */,\n url: context.url,\n response: context.response,\n init: context.init,\n input: context.input,\n isAborted: context.isAborted,\n handlingStack: context.handlingStack\n });\n });\n break;\n }\n });\n return {\n stop: () => subscription.unsubscribe()\n };\n}\nfunction getNextRequestIndex() {\n const result = nextRequestIndex;\n nextRequestIndex += 1;\n return result;\n}\nfunction waitForResponseToComplete(context, callback) {\n const clonedResponse = context.response && tryToClone(context.response);\n if (!clonedResponse || !clonedResponse.body) {\n // do not try to wait for the response if the clone failed, fetch error or null body\n callback(elapsed(context.startClocks.timeStamp, timeStampNow()));\n } else {\n readBytesFromStream(clonedResponse.body, () => {\n callback(elapsed(context.startClocks.timeStamp, timeStampNow()));\n }, {\n bytesLimit: Number.POSITIVE_INFINITY,\n collectStreamBody: false\n });\n }\n}\n","import { isNumber } from '@datadog/browser-core';\nexport function discardNegativeDuration(duration) {\n return isNumber(duration) && duration < 0 ? undefined : duration;\n}\n","import { noop } from '@datadog/browser-core';\nexport function trackEventCounts({\n lifeCycle,\n isChildEvent,\n onChange: callback = noop\n}) {\n const eventCounts = {\n errorCount: 0,\n longTaskCount: 0,\n resourceCount: 0,\n actionCount: 0,\n frustrationCount: 0\n };\n const subscription = lifeCycle.subscribe(13 /* LifeCycleEventType.RUM_EVENT_COLLECTED */, event => {\n var _a;\n if (event.type === 'view' || event.type === 'vital' || !isChildEvent(event)) {\n return;\n }\n switch (event.type) {\n case \"error\" /* RumEventType.ERROR */:\n eventCounts.errorCount += 1;\n callback();\n break;\n case \"action\" /* RumEventType.ACTION */:\n eventCounts.actionCount += 1;\n if (event.action.frustration) {\n eventCounts.frustrationCount += event.action.frustration.type.length;\n }\n callback();\n break;\n case \"long_task\" /* RumEventType.LONG_TASK */:\n eventCounts.longTaskCount += 1;\n callback();\n break;\n case \"resource\" /* RumEventType.RESOURCE */:\n if (!((_a = event._dd) === null || _a === void 0 ? void 0 : _a.discarded)) {\n eventCounts.resourceCount += 1;\n callback();\n }\n break;\n }\n });\n return {\n stop: () => {\n subscription.unsubscribe();\n },\n eventCounts\n };\n}\n","import { matchList, monitor, Observable, timeStampNow, setTimeout, clearTimeout } from '@datadog/browser-core';\nimport { createPerformanceObservable, RumPerformanceEntryType } from '../browser/performanceObservable';\n// Delay to wait for a page activity to validate the tracking process\nexport const PAGE_ACTIVITY_VALIDATION_DELAY = 100;\n// Delay to wait after a page activity to end the tracking process\nexport const PAGE_ACTIVITY_END_DELAY = 100;\n/**\n * Wait for the page activity end\n *\n * Detection lifecycle:\n * ```\n * Wait page activity end\n * .-------------------'--------------------.\n * v v\n * [Wait for a page activity ] [Wait for a maximum duration]\n * [timeout: VALIDATION_DELAY] [ timeout: maxDuration ]\n * / \\ |\n * v v |\n * [No page activity] [Page activity] |\n * | |,----------------------. |\n * v v | |\n * (Discard) [Wait for a page activity] | |\n * [ timeout: END_DELAY ] | |\n * / \\ | |\n * v v | |\n * [No page activity] [Page activity] | |\n * | | | |\n * | '------------' |\n * '-----------. ,--------------------'\n * v\n * (End)\n * ```\n *\n * Note: by assuming that maxDuration is greater than VALIDATION_DELAY, we are sure that if the\n * process is still alive after maxDuration, it has been validated.\n */\nexport function waitPageActivityEnd(lifeCycle, domMutationObservable, windowOpenObservable, configuration, pageActivityEndCallback, maxDuration) {\n const pageActivityObservable = createPageActivityObservable(lifeCycle, domMutationObservable, windowOpenObservable, configuration);\n return doWaitPageActivityEnd(pageActivityObservable, pageActivityEndCallback, maxDuration);\n}\nexport function doWaitPageActivityEnd(pageActivityObservable, pageActivityEndCallback, maxDuration) {\n let pageActivityEndTimeoutId;\n let hasCompleted = false;\n const validationTimeoutId = setTimeout(monitor(() => complete({\n hadActivity: false\n })), PAGE_ACTIVITY_VALIDATION_DELAY);\n const maxDurationTimeoutId = maxDuration !== undefined ? setTimeout(monitor(() => complete({\n hadActivity: true,\n end: timeStampNow()\n })), maxDuration) : undefined;\n const pageActivitySubscription = pageActivityObservable.subscribe(({\n isBusy\n }) => {\n clearTimeout(validationTimeoutId);\n clearTimeout(pageActivityEndTimeoutId);\n const lastChangeTime = timeStampNow();\n if (!isBusy) {\n pageActivityEndTimeoutId = setTimeout(monitor(() => complete({\n hadActivity: true,\n end: lastChangeTime\n })), PAGE_ACTIVITY_END_DELAY);\n }\n });\n const stop = () => {\n hasCompleted = true;\n clearTimeout(validationTimeoutId);\n clearTimeout(pageActivityEndTimeoutId);\n clearTimeout(maxDurationTimeoutId);\n pageActivitySubscription.unsubscribe();\n };\n function complete(event) {\n if (hasCompleted) {\n return;\n }\n stop();\n pageActivityEndCallback(event);\n }\n return {\n stop\n };\n}\nexport function createPageActivityObservable(lifeCycle, domMutationObservable, windowOpenObservable, configuration) {\n return new Observable(observable => {\n const subscriptions = [];\n let firstRequestIndex;\n let pendingRequestsCount = 0;\n subscriptions.push(domMutationObservable.subscribe(notifyPageActivity), windowOpenObservable.subscribe(notifyPageActivity), createPerformanceObservable(configuration, {\n type: RumPerformanceEntryType.RESOURCE\n }).subscribe(entries => {\n if (entries.some(entry => !isExcludedUrl(configuration, entry.name))) {\n notifyPageActivity();\n }\n }), lifeCycle.subscribe(7 /* LifeCycleEventType.REQUEST_STARTED */, startEvent => {\n if (isExcludedUrl(configuration, startEvent.url)) {\n return;\n }\n if (firstRequestIndex === undefined) {\n firstRequestIndex = startEvent.requestIndex;\n }\n pendingRequestsCount += 1;\n notifyPageActivity();\n }), lifeCycle.subscribe(8 /* LifeCycleEventType.REQUEST_COMPLETED */, request => {\n if (isExcludedUrl(configuration, request.url) || firstRequestIndex === undefined ||\n // If the request started before the tracking start, ignore it\n request.requestIndex < firstRequestIndex) {\n return;\n }\n pendingRequestsCount -= 1;\n notifyPageActivity();\n }));\n return () => {\n subscriptions.forEach(s => s.unsubscribe());\n };\n function notifyPageActivity() {\n observable.notify({\n isBusy: pendingRequestsCount > 0\n });\n }\n });\n}\nfunction isExcludedUrl(configuration, requestUrl) {\n return matchList(configuration.excludedActivityUrls, requestUrl);\n}\n","export function isTextNode(node) {\n return node.nodeType === Node.TEXT_NODE;\n}\nexport function isCommentNode(node) {\n return node.nodeType === Node.COMMENT_NODE;\n}\nexport function isElementNode(node) {\n return node.nodeType === Node.ELEMENT_NODE;\n}\nexport function isNodeShadowHost(node) {\n return isElementNode(node) && Boolean(node.shadowRoot);\n}\nexport function isNodeShadowRoot(node) {\n const shadowRoot = node;\n return !!shadowRoot.host && shadowRoot.nodeType === Node.DOCUMENT_FRAGMENT_NODE && isElementNode(shadowRoot.host);\n}\nexport function hasChildNodes(node) {\n return node.childNodes.length > 0 || isNodeShadowHost(node);\n}\nexport function forEachChildNodes(node, callback) {\n let child = node.firstChild;\n while (child) {\n callback(child);\n child = child.nextSibling;\n }\n if (isNodeShadowHost(node)) {\n callback(node.shadowRoot);\n }\n}\n/**\n * Return `host` in case if the current node is a shadow root otherwise will return the `parentNode`\n */\nexport function getParentNode(node) {\n return isNodeShadowRoot(node) ? node.host : node.parentNode;\n}\n","import { DefaultPrivacyLevel } from '@datadog/browser-core';\nimport { isElementNode, getParentNode, isTextNode } from '../browser/htmlDomUtils';\nexport const NodePrivacyLevel = {\n IGNORE: 'ignore',\n HIDDEN: 'hidden',\n ALLOW: DefaultPrivacyLevel.ALLOW,\n MASK: DefaultPrivacyLevel.MASK,\n MASK_USER_INPUT: DefaultPrivacyLevel.MASK_USER_INPUT\n};\nexport const PRIVACY_ATTR_NAME = 'data-dd-privacy';\n// Privacy Attrs\nexport const PRIVACY_ATTR_VALUE_ALLOW = 'allow';\nexport const PRIVACY_ATTR_VALUE_MASK = 'mask';\nexport const PRIVACY_ATTR_VALUE_MASK_USER_INPUT = 'mask-user-input';\nexport const PRIVACY_ATTR_VALUE_HIDDEN = 'hidden';\n// Privacy Classes - not all customers can set plain HTML attributes, so support classes too\nexport const PRIVACY_CLASS_PREFIX = 'dd-privacy-';\n// Private Replacement Templates\nexport const CENSORED_STRING_MARK = '***';\nexport const CENSORED_IMG_MARK = 'data:image/gif;base64,R0lGODlhAQABAIAAAMLCwgAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==';\nexport const FORM_PRIVATE_TAG_NAMES = {\n INPUT: true,\n OUTPUT: true,\n TEXTAREA: true,\n SELECT: true,\n OPTION: true,\n DATALIST: true,\n OPTGROUP: true\n};\nconst TEXT_MASKING_CHAR = 'x';\n/**\n * Get node privacy level by iterating over its ancestors. When the direct parent privacy level is\n * know, it is best to use something like:\n *\n * derivePrivacyLevelGivenParent(getNodeSelfPrivacyLevel(node), parentNodePrivacyLevel)\n */\nexport function getNodePrivacyLevel(node, defaultPrivacyLevel, cache) {\n if (cache && cache.has(node)) {\n return cache.get(node);\n }\n const parentNode = getParentNode(node);\n const parentNodePrivacyLevel = parentNode ? getNodePrivacyLevel(parentNode, defaultPrivacyLevel, cache) : defaultPrivacyLevel;\n const selfNodePrivacyLevel = getNodeSelfPrivacyLevel(node);\n const nodePrivacyLevel = reducePrivacyLevel(selfNodePrivacyLevel, parentNodePrivacyLevel);\n if (cache) {\n cache.set(node, nodePrivacyLevel);\n }\n return nodePrivacyLevel;\n}\n/**\n * Reduces the next privacy level based on self + parent privacy levels\n */\nexport function reducePrivacyLevel(childPrivacyLevel, parentNodePrivacyLevel) {\n switch (parentNodePrivacyLevel) {\n // These values cannot be overridden\n case NodePrivacyLevel.HIDDEN:\n case NodePrivacyLevel.IGNORE:\n return parentNodePrivacyLevel;\n }\n switch (childPrivacyLevel) {\n case NodePrivacyLevel.ALLOW:\n case NodePrivacyLevel.MASK:\n case NodePrivacyLevel.MASK_USER_INPUT:\n case NodePrivacyLevel.HIDDEN:\n case NodePrivacyLevel.IGNORE:\n return childPrivacyLevel;\n default:\n return parentNodePrivacyLevel;\n }\n}\n/**\n * Determines the node's own privacy level without checking for ancestors.\n */\nexport function getNodeSelfPrivacyLevel(node) {\n // Only Element types can have a privacy level set\n if (!isElementNode(node)) {\n return;\n }\n // Overrules for replay purpose\n if (node.tagName === 'BASE') {\n return NodePrivacyLevel.ALLOW;\n }\n // Overrules to enforce end-user protection\n if (node.tagName === 'INPUT') {\n const inputElement = node;\n if (inputElement.type === 'password' || inputElement.type === 'email' || inputElement.type === 'tel') {\n return NodePrivacyLevel.MASK;\n }\n if (inputElement.type === 'hidden') {\n return NodePrivacyLevel.MASK;\n }\n const autocomplete = inputElement.getAttribute('autocomplete');\n // Handle input[autocomplete=cc-number/cc-csc/cc-exp/cc-exp-month/cc-exp-year/new-password/current-password]\n if (autocomplete && (autocomplete.startsWith('cc-') || autocomplete.endsWith('-password'))) {\n return NodePrivacyLevel.MASK;\n }\n }\n // Check HTML privacy attributes and classes\n if (node.matches(getPrivacySelector(NodePrivacyLevel.HIDDEN))) {\n return NodePrivacyLevel.HIDDEN;\n }\n if (node.matches(getPrivacySelector(NodePrivacyLevel.MASK))) {\n return NodePrivacyLevel.MASK;\n }\n if (node.matches(getPrivacySelector(NodePrivacyLevel.MASK_USER_INPUT))) {\n return NodePrivacyLevel.MASK_USER_INPUT;\n }\n if (node.matches(getPrivacySelector(NodePrivacyLevel.ALLOW))) {\n return NodePrivacyLevel.ALLOW;\n }\n if (shouldIgnoreElement(node)) {\n return NodePrivacyLevel.IGNORE;\n }\n}\n/**\n * Helper aiming to unify `mask` and `mask-user-input` privacy levels:\n *\n * In the `mask` case, it is trivial: we should mask the element.\n *\n * In the `mask-user-input` case, we should mask the element only if it is a \"form\" element or the\n * direct parent is a form element for text nodes).\n *\n * Other `shouldMaskNode` cases are edge cases that should not matter too much (ex: should we mask a\n * node if it is ignored or hidden? it doesn't matter since it won't be serialized).\n */\nexport function shouldMaskNode(node, privacyLevel) {\n switch (privacyLevel) {\n case NodePrivacyLevel.MASK:\n case NodePrivacyLevel.HIDDEN:\n case NodePrivacyLevel.IGNORE:\n return true;\n case NodePrivacyLevel.MASK_USER_INPUT:\n return isTextNode(node) ? isFormElement(node.parentNode) : isFormElement(node);\n default:\n return false;\n }\n}\nfunction isFormElement(node) {\n if (!node || node.nodeType !== node.ELEMENT_NODE) {\n return false;\n }\n const element = node;\n if (element.tagName === 'INPUT') {\n switch (element.type) {\n case 'button':\n case 'color':\n case 'reset':\n case 'submit':\n return false;\n }\n }\n return !!FORM_PRIVATE_TAG_NAMES[element.tagName];\n}\n/**\n * Text censoring non-destructively maintains whitespace characters in order to preserve text shape\n * during replay.\n */\nexport const censorText = text => text.replace(/\\S/g, TEXT_MASKING_CHAR);\nexport function getTextContent(textNode, ignoreWhiteSpace, parentNodePrivacyLevel) {\n var _a;\n // The parent node may not be a html element which has a tagName attribute.\n // So just let it be undefined which is ok in this use case.\n const parentTagName = (_a = textNode.parentElement) === null || _a === void 0 ? void 0 : _a.tagName;\n let textContent = textNode.textContent || '';\n if (ignoreWhiteSpace && !textContent.trim()) {\n return;\n }\n const nodePrivacyLevel = parentNodePrivacyLevel;\n const isScript = parentTagName === 'SCRIPT';\n if (isScript) {\n // For perf reasons, we don't record script (heuristic)\n textContent = CENSORED_STRING_MARK;\n } else if (nodePrivacyLevel === NodePrivacyLevel.HIDDEN) {\n // Should never occur, but just in case, we set to CENSORED_MARK.\n textContent = CENSORED_STRING_MARK;\n } else if (shouldMaskNode(textNode, nodePrivacyLevel)) {\n if (\n // Scrambling the child list breaks text nodes for DATALIST/SELECT/OPTGROUP\n parentTagName === 'DATALIST' || parentTagName === 'SELECT' || parentTagName === 'OPTGROUP') {\n if (!textContent.trim()) {\n return;\n }\n } else if (parentTagName === 'OPTION') {\n //