{"version":3,"file":"cookie.mjs","names":[],"sources":["../../src/browser/cookie.ts"],"sourcesContent":["import { ONE_SECOND } from '@openobserve/js-core/time'\nimport {\n  findAllCommaSeparatedValues,\n  findCommaSeparatedValue,\n  findCommaSeparatedValues,\n  generateUUID,\n} from '../tools/utils/stringUtils'\nimport { buildUrl } from '../tools/utils/urlPolyfill'\nimport { globalObject } from '../tools/globalObject'\n\nexport interface CookieOptions {\n  secure?: boolean\n  crossSite?: boolean\n  partitioned?: boolean\n  domain?: string\n}\n\nexport function setCookie(name: string, value: string, expireDelay: number = 0, options?: CookieOptions) {\n  const date = new Date()\n  date.setTime(date.getTime() + expireDelay)\n  const expires = `expires=${date.toUTCString()}`\n  const sameSite = options?.crossSite ? 'none' : 'strict'\n  const domain = options?.domain ? `;domain=${options.domain}` : ''\n  const secure = options?.secure ? ';secure' : ''\n  const partitioned = options?.partitioned ? ';partitioned' : ''\n  document.cookie = `${name}=${value};${expires};path=/;samesite=${sameSite}${domain}${secure}${partitioned}`\n}\n\n/**\n * Returns the value of the cookie with the given name\n * If there are multiple cookies with the same name, returns the first one\n */\nexport function getCookie(name: string) {\n  const value = findCommaSeparatedValue(document.cookie, name)\n\n  if (value === '') {\n    return\n  }\n\n  return value\n}\n\n/**\n * Returns all the values of the cookies with the given name\n */\nexport function getCookies(name: string): string[] {\n  return findAllCommaSeparatedValues(document.cookie).get(name) || []\n}\n\nlet initCookieParsed: Map<string, string> | undefined\n\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 *\n * ⚠️ If there are multiple cookies with the same name, returns the LAST one (unlike `getCookie()`)\n */\nexport function getInitCookie(name: string) {\n  if (!initCookieParsed) {\n    initCookieParsed = findCommaSeparatedValues(document.cookie)\n  }\n  return initCookieParsed.get(name)\n}\n\nexport function resetInitCookies() {\n  initCookieParsed = undefined\n}\n\nexport function deleteCookie(name: string, options?: CookieOptions) {\n  setCookie(name, '', 0, options)\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: string | undefined\nexport function getCurrentSite(\n  hostname = globalObject.location?.hostname ?? '',\n  referrer = typeof document !== 'undefined' ? document.referrer : ''\n): string | undefined {\n  // Cookie probing requires document.cookie, which is not available in Web Workers or Node.js\n  if (typeof document === 'undefined') {\n    return undefined\n  }\n  if (getCurrentSiteCache === undefined) {\n    const defaultHostName = getCookieDefaultHostName(hostname, referrer)\n    if (defaultHostName) {\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 = `oo_site_test_${generateUUID()}`\n      const testCookieValue = 'test'\n\n      const domainLevels = defaultHostName.split('.')\n      let candidateDomain = domainLevels.pop()!\n      while (domainLevels.length && !getCookie(testCookieName)) {\n        candidateDomain = `${domainLevels.pop()!}.${candidateDomain}`\n        setCookie(testCookieName, testCookieValue, ONE_SECOND, { domain: candidateDomain })\n      }\n      deleteCookie(testCookieName, { domain: candidateDomain })\n      getCurrentSiteCache = candidateDomain\n    }\n  }\n\n  return getCurrentSiteCache\n}\n\nfunction getCookieDefaultHostName(hostname: string, referrer: string) {\n  try {\n    return hostname || buildUrl(referrer).hostname\n  } catch {\n    // Ignore\n  }\n}\n\nexport function resetGetCurrentSite() {\n  getCurrentSiteCache = undefined\n}\n"],"mappings":";;;;;AAiBA,SAAgB,UAAU,MAAc,OAAe,cAAsB,GAAG,SAAyB;CACvG,MAAM,uBAAO,IAAI,KAAK;CACtB,KAAK,QAAQ,KAAK,QAAQ,IAAI,WAAW;CACzC,MAAM,UAAU,WAAW,KAAK,YAAY;CAC5C,MAAM,WAAW,SAAS,YAAY,SAAS;CAC/C,MAAM,SAAS,SAAS,SAAS,WAAW,QAAQ,WAAW;CAC/D,MAAM,SAAS,SAAS,SAAS,YAAY;CAC7C,MAAM,cAAc,SAAS,cAAc,iBAAiB;CAC5D,SAA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