{"version":3,"file":"configuration.mjs","names":[],"sources":["../../../src/domain/configuration/configuration.ts"],"sourcesContent":["import { catchUserErrors } from '../../tools/catchUserErrors'\nimport { DOCS_ORIGIN, MORE_DETAILS, display } from '../../tools/display'\nimport type { RawTelemetryConfiguration } from '../telemetry'\nimport { isPercentage } from '../../tools/utils/numberUtils'\nimport { objectHasValue } from '../../tools/utils/objectUtils'\nimport type { CookieOptions } from '../../browser/cookie'\nimport { getCurrentSite } from '../../browser/cookie'\nimport { TrackingConsent } from '../trackingConsent'\nimport type { SessionPersistence } from '../session/sessionConstants'\nimport type { MatchOption } from '../../tools/matchOption'\nimport { isAllowedTrackingOrigins } from '../allowedTrackingOrigins'\nimport { INTAKE_SITE_US1 } from '../intakeSites'\nimport type { Site } from '../intakeSites'\n\n/**\n * Default privacy level for the browser SDK.\n *\n * [Replay Privacy Options](https://docs.datadoghq.com/real_user_monitoring/session_replay/browser/privacy_options) for further information.\n */\nexport const DefaultPrivacyLevel = {\n  ALLOW: 'allow',\n  MASK: 'mask',\n  MASK_USER_INPUT: 'mask-user-input',\n  MASK_UNLESS_ALLOWLISTED: 'mask-unless-allowlisted',\n} as const\nexport type DefaultPrivacyLevel = (typeof DefaultPrivacyLevel)[keyof typeof DefaultPrivacyLevel]\n\n/**\n * Trace context injection option.\n *\n * See [Connect RUM and Traces](https://docs.datadoghq.com/real_user_monitoring/platform/connect_rum_and_traces/?tab=browserrum) for further information.\n */\nexport const TraceContextInjection = {\n  ALL: 'all',\n  SAMPLED: 'sampled',\n} as const\n\n/**\n * Trace context injection option.\n *\n * See [Connect RUM and Traces](https://docs.datadoghq.com/real_user_monitoring/platform/connect_rum_and_traces/?tab=browserrum) for further information.\n *\n */\nexport type TraceContextInjection = (typeof TraceContextInjection)[keyof typeof TraceContextInjection]\n\nexport interface InitConfiguration {\n  /**\n   * The client token for Datadog. Required for authenticating your application with Datadog.\n   *\n   * @category Authentication\n   */\n  clientToken: string\n\n  /**\n   * A callback function that can be used to modify events before they are sent to Datadog.\n   *\n   * @category Data Collection\n   */\n  beforeSend?: GenericBeforeSendCallback | undefined\n\n  /**\n   * The percentage of sessions tracked. A value between 0 and 100.\n   *\n   * @category Data Collection\n   * @defaultValue 100\n   */\n  sessionSampleRate?: number | undefined\n\n  /**\n   * The percentage of telemetry events sent. A value between 0 and 100.\n   *\n   * @category Data Collection\n   * @defaultValue 20\n   */\n  telemetrySampleRate?: number | undefined\n\n  /**\n   * Initialization fails silently if the RUM Browser SDK is already initialized on the page.\n   *\n   * @defaultValue false\n   */\n  silentMultipleInit?: boolean | undefined\n\n  /**\n   * Which storage strategy to use for persisting sessions. Can be 'cookie', 'local-storage', or 'memory'.\n   * When an array is provided, the SDK will attempt each persistence type in the order specified.\n   *\n   * Important: If you are using the RUM and Logs Browser SDKs, this option must be configured with identical values\n   *\n   * Note: 'memory' option is only for use with single-page applications. All page loads will start a new session, likely resulting in an increase in total number of RUM sessions\n   *\n   * @category Session Persistence\n   * @defaultValue \"cookie\"\n   */\n  sessionPersistence?: SessionPersistence | SessionPersistence[] | undefined\n\n  /**\n   * Allow listening to DOM events dispatched programmatically ([untrusted events](https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted)). Enabling this option can be useful if you heavily rely on programmatic events, such as in an automated UI test environment.\n   *\n   * @defaultValue false\n   */\n  allowUntrustedEvents?: boolean | undefined\n\n  /**\n   * Store global context and user context in localStorage to preserve them along the user navigation.\n   * See [Contexts life cycle](https://docs.datadoghq.com/real_user_monitoring/browser/advanced_configuration/?tab=npm#contexts-life-cycle) for further information.\n   *\n   * @defaultValue false\n   */\n  storeContextsAcrossPages?: boolean | undefined\n\n  /**\n   * Set the initial user tracking consent state.\n   * See [User tracking consent](https://docs.datadoghq.com/real_user_monitoring/browser/advanced_configuration/?tab=npm#user-tracking-consent) for further information.\n   *\n   * @category Privacy\n   * @defaultValue granted\n   */\n  trackingConsent?: TrackingConsent | undefined\n\n  /**\n   * List of origins where the SDK is allowed to run when used in a browser extension context.\n   * Matches urls against the extensions origin.\n   * If not provided and the SDK is running in a browser extension, the SDK will not run.\n   */\n  allowedTrackingOrigins?: MatchOption[] | undefined\n\n  // transport options\n  /**\n   * Optional proxy URL, for example: https://www.proxy.com/path.\n   * See [Proxy Your Browser RUM Data](https://docs.datadoghq.com/real_user_monitoring/guide/proxy-rum-data) for further information.\n   *\n   * @category Transport\n   */\n  proxy?: string | ProxyFn | undefined\n\n  /**\n   * The Datadog [site](https://docs.datadoghq.com/getting_started/site) parameter of your organization.\n   *\n   * @category Transport\n   * @defaultValue datadoghq.com\n   */\n  site?: Site | undefined\n\n  /**\n   * The OpenObserve ingestion API version used to build intake URLs.\n   *\n   * @category Transport\n   * @defaultValue v1\n   */\n  apiVersion?: string | undefined\n\n  /**\n   * The OpenObserve organization identifier, used as a path segment of intake URLs.\n   *\n   * @category Transport\n   * @defaultValue default\n   */\n  organizationIdentifier?: string | undefined\n\n  /**\n   * Send data to the intake over plain HTTP instead of HTTPS. Only intended for local\n   * development against a non-TLS OpenObserve instance.\n   *\n   * @category Transport\n   */\n  insecureHTTP?: boolean | undefined\n\n  // tag and context options\n  /**\n   * The service name for your application. Follows the [tag syntax requirements](https://docs.datadoghq.com/getting_started/tagging/#define-tags).\n   *\n   * @category Data Collection\n   */\n  service?: string | undefined | null\n\n  /**\n   * The application’s environment, for example: prod, pre-prod, and staging. Follows the [tag syntax requirements](https://docs.datadoghq.com/getting_started/tagging/#define-tags).\n   *\n   * @category Data Collection\n   */\n  env?: string | undefined | null\n\n  /**\n   * The application’s version, for example: 1.2.3, 6c44da20, and 2020.02.13. Follows the [tag syntax requirements](https://docs.datadoghq.com/getting_started/tagging/#define-tags).\n   *\n   * @category Data Collection\n   */\n  version?: string | undefined | null\n\n  // cookie options\n  /**\n   * Use a partitioned secure cross-site session cookie. This allows the RUM Browser SDK to run when the site is loaded from another one (iframe). Implies `useSecureSessionCookie`.\n   *\n   * Important: If you are using the RUM and Logs Browser SDKs, this option must be configured with identical values\n   *\n   * @category Session Persistence\n   * @defaultValue false\n   */\n  usePartitionedCrossSiteSessionCookie?: boolean | undefined\n\n  /**\n   * Use a secure session cookie. This disables RUM events sent on insecure (non-HTTPS) connections.\n   *\n   * Important: If you are using the RUM and Logs Browser SDKs, this option must be configured with identical values\n   *\n   * @category Session Persistence\n   * @defaultValue false\n   */\n  useSecureSessionCookie?: boolean | undefined\n\n  /**\n   * Preserve the session across subdomains for the same site.\n   *\n   * Important: If you are using the RUM and Logs Browser SDKs, this option must be configured with identical values\n   *\n   * @category Session Persistence\n   * @defaultValue false\n   */\n  trackSessionAcrossSubdomains?: boolean | undefined\n\n  /**\n   * Track anonymous user for the same site and extend cookie expiration date\n   *\n   * @category Data Collection\n   * @defaultValue true\n   */\n  trackAnonymousUser?: boolean | undefined\n\n  // internal options\n  /**\n   * [Internal option] Enable experimental features\n   *\n   * @internal\n   */\n  enableExperimentalFeatures?: string[] | undefined\n\n  /**\n   * [Internal option] Configure the dual shipping to another datacenter\n   *\n   * @internal\n   */\n  replica?: ReplicaInitConfiguration | undefined\n\n  /**\n   * [Internal option] Set the datacenter from where the data is dual shipped\n   *\n   * @internal\n   */\n  datacenter?: string\n\n  /**\n   * [Internal option] The percentage of telemetry configuration sent. A value between 0 and 100.\n   *\n   * @internal\n   * @defaultValue 5\n   */\n  telemetryConfigurationSampleRate?: number\n\n  /**\n   * [Internal option] The percentage of telemetry usage sent. A value between 0 and 100.\n   *\n   * @internal\n   * @defaultValue 5\n   */\n  telemetryUsageSampleRate?: number\n\n  /**\n   * [Internal option] Additional configuration for the SDK.\n   *\n   * @internal\n   */\n  source?: 'browser' | 'flutter' | 'unity' | undefined\n\n  /**\n   * [Internal option] Additional configuration for the SDK.\n   *\n   * @internal\n   */\n  sdkVersion?: string | undefined\n\n  /**\n   * [Internal option] Additional configuration for the SDK.\n   *\n   * @internal\n   */\n  variant?: string | undefined\n}\n\n// This type is only used to build the core configuration. Logs and RUM SDKs are using a proper type\n// for this option.\ntype GenericBeforeSendCallback = (event: any, context?: any) => unknown\n\n/**\n * path: /api/vX/product\n * parameters: xxx=yyy&zzz=aaa\n */\nexport type ProxyFn = (options: { path: string; parameters: string; subdomain?: string }) => string\n\n/**\n * @internal\n */\nexport interface ReplicaInitConfiguration {\n  applicationId?: string\n  clientToken: string\n}\n\nexport type SdkSource = 'browser' | 'flutter' | 'unity'\n\nexport interface Configuration {\n  clientToken: string\n  datacenter: string | undefined\n  proxy: string | ProxyFn | undefined\n  site: Site\n  apiVersion: string\n  organizationIdentifier: string\n  insecureHTTP: boolean | undefined\n  source: SdkSource\n  beforeSend: GenericBeforeSendCallback | undefined\n  cookieOptions: CookieOptions | undefined\n  sessionPersistence: SessionPersistence | SessionPersistence[] | undefined\n  sessionSampleRate: number\n  telemetrySampleRate: number\n  telemetryConfigurationSampleRate: number\n  telemetryUsageSampleRate: number\n  service?: string | undefined\n  version?: string | undefined\n  env?: string | undefined\n  silentMultipleInit: boolean\n  trackingConsent: TrackingConsent\n  storeContextsAcrossPages: boolean\n  trackAnonymousUser?: boolean\n\n  // internal\n  sdkVersion: string | undefined\n  variant: string | undefined\n  replica: ReplicaInitConfiguration | undefined\n}\n\nfunction isString(tag: unknown, tagName: string): tag is string | undefined | null {\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}\n\nexport function isSampleRate(sampleRate: unknown, name: string) {\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}\n\nexport function validateAndBuildConfiguration(\n  initConfiguration: InitConfiguration,\n  errorStack?: string\n): Configuration | undefined {\n  if (!initConfiguration?.clientToken) {\n    display.error('Client Token is not configured, we will not send any data.')\n    return\n  }\n\n  if (\n    initConfiguration.allowedTrackingOrigins !== undefined &&\n    !Array.isArray(initConfiguration.allowedTrackingOrigins)\n  ) {\n    display.error('Allowed Tracking Origins must be an array')\n    return\n  }\n\n  if (\n    !isSampleRate(initConfiguration.sessionSampleRate, 'Session') ||\n    !isSampleRate(initConfiguration.telemetrySampleRate, 'Telemetry') ||\n    !isSampleRate(initConfiguration.telemetryConfigurationSampleRate, 'Telemetry Configuration') ||\n    !isSampleRate(initConfiguration.telemetryUsageSampleRate, 'Telemetry Usage') ||\n    !isString(initConfiguration.version, 'Version') ||\n    !isString(initConfiguration.env, 'Env') ||\n    !isString(initConfiguration.service, 'Service') ||\n    !isAllowedTrackingOrigins(initConfiguration, errorStack ?? '')\n  ) {\n    return\n  }\n\n  if (\n    initConfiguration.trackingConsent !== undefined &&\n    !objectHasValue(TrackingConsent, initConfiguration.trackingConsent)\n  ) {\n    display.error('Tracking Consent should be either \"granted\" or \"not-granted\"')\n    return\n  }\n\n  return {\n    clientToken: initConfiguration.clientToken,\n    proxy: initConfiguration.proxy,\n    site: initConfiguration.site || INTAKE_SITE_US1,\n    apiVersion: initConfiguration.apiVersion ?? 'v1',\n    organizationIdentifier: initConfiguration.organizationIdentifier ?? 'default',\n    insecureHTTP: initConfiguration.insecureHTTP,\n    source: validateSource(initConfiguration.source),\n    beforeSend:\n      initConfiguration.beforeSend && catchUserErrors(initConfiguration.beforeSend, 'beforeSend threw an error:'),\n    cookieOptions: buildCookieOptions(initConfiguration),\n    sessionPersistence: initConfiguration.sessionPersistence,\n    sessionSampleRate: initConfiguration.sessionSampleRate ?? 100,\n    telemetrySampleRate: initConfiguration.telemetrySampleRate ?? 20,\n    telemetryConfigurationSampleRate: initConfiguration.telemetryConfigurationSampleRate ?? 5,\n    telemetryUsageSampleRate: initConfiguration.telemetryUsageSampleRate ?? 5,\n    service: initConfiguration.service ?? undefined,\n    env: initConfiguration.env ?? undefined,\n    version: initConfiguration.version ?? undefined,\n    datacenter: initConfiguration.datacenter ?? undefined,\n    silentMultipleInit: !!initConfiguration.silentMultipleInit,\n    trackingConsent: initConfiguration.trackingConsent ?? TrackingConsent.GRANTED,\n    trackAnonymousUser: initConfiguration.trackAnonymousUser ?? true,\n    storeContextsAcrossPages: !!initConfiguration.storeContextsAcrossPages,\n\n    /**\n     * The source of the SDK, used for support plugins purposes.\n     */\n    variant: initConfiguration.variant,\n    sdkVersion: initConfiguration.sdkVersion,\n    replica: initConfiguration.replica,\n  }\n}\n\nexport function buildCookieOptions(initConfiguration: InitConfiguration): CookieOptions | undefined {\n  const cookieOptions: CookieOptions = {}\n\n  cookieOptions.secure =\n    !!initConfiguration.useSecureSessionCookie || !!initConfiguration.usePartitionedCrossSiteSessionCookie\n  cookieOptions.crossSite = !!initConfiguration.usePartitionedCrossSiteSessionCookie\n  cookieOptions.partitioned = !!initConfiguration.usePartitionedCrossSiteSessionCookie\n\n  if (initConfiguration.trackSessionAcrossSubdomains) {\n    const currentSite = getCurrentSite()\n    if (!currentSite) {\n      return\n    }\n    cookieOptions.domain = currentSite\n  }\n\n  return cookieOptions\n}\n\nexport function serializeConfiguration(initConfiguration: 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: Array.isArray(initConfiguration.sessionPersistence)\n      ? initConfiguration.sessionPersistence[0]\n      : initConfiguration.sessionPersistence,\n    store_contexts_across_pages: !!initConfiguration.storeContextsAcrossPages,\n    allow_untrusted_events: !!initConfiguration.allowUntrustedEvents,\n    tracking_consent: initConfiguration.trackingConsent,\n    use_allowed_tracking_origins: Array.isArray(initConfiguration.allowedTrackingOrigins),\n    source: initConfiguration.source,\n    sdk_version: initConfiguration.sdkVersion,\n    variant: initConfiguration.variant,\n  } satisfies RawTelemetryConfiguration\n}\n\nfunction validateSource(source: string | undefined): SdkSource {\n  if (source === 'flutter' || source === 'unity') {\n    return source\n  }\n  return 'browser'\n}\n"],"mappings":";;;;;;;;;;;;;;AAmBA,MAAa,sBAAsB;CACjC,OAAO;CACP,MAAM;CACN,iBAAiB;CACjB,yBAAyB;AAC3B;;;;;;AAQA,MAAa,wBAAwB;CACnC,KAAK;CACL,SAAS;AACX;AAgTA,SAAS,SAAS,KAAc,SAAmD;CACjF,IAAI,QAAQ,KAAA,KAAa,QAAQ,QAAQ,OAAO,QAAQ,UAAU;EAChE,QAAQ,MAAM,GAAG,QAAQ,6BAA6B;EACtD,OAAO;CACT;CACA,OAAO;AACT;AAEA,SAAgB,aAAa,YAAqB,MAAc;CAC9D,IAAI,eAAe,KAAA,KAAa,CAAC,aAAa,UAAU,GAAG;EACzD,QAAQ,MAAM,GAAG,KAAK,kDAAkD;EACxE,OAAO;CACT;CACA,OAAO;AACT;AAEA,SAAgB,8BACd,mBACA,YAC2B;CAC3B,IAAI,CAAC,mBAAmB,aAAa;EACnC,QAAQ,MAAM,4DAA4D;EAC1E;CACF;CAEA,IACE,kBAAkB,2BAA2B,KAAA,KAC7C,CAAC,MAAM,QAAQ,kBAAkB,sBAAsB,GACvD;EACA,QAAQ,MAAM,2CAA2C;EACzD;CACF;CAEA,IACE,CAAC,aAAa,kBAAkB,mBAAmB,SAAS,KAC5D,CAAC,aAAa,kBAAkB,qBAAqB,WAAW,KAChE,CAAC,aAAa,kBAAkB,kCAAkC,yBAAyB,KAC3F,CAAC,aAAa,kBAAkB,0BAA0B,iBAAiB,KAC3E,CAAC,SAAS,kBAAkB,SAAS,SAAS,KAC9C,CAAC,SAAS,kBAAkB,KAAK,KAAK,KACtC,CAAC,SAAS,kBAAkB,SAAS,SAAS,KAC9C,CAAC,yBAAyB,mBAAmB,c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