{"version":3,"file":"proxy.mjs","names":[],"sources":["../src/proxy.ts"],"sourcesContent":["import { isProductionFromPublishableKey } from './keys';\nimport { isTruthy } from './underscore';\n\n/**\n *\n */\nexport function isValidProxyUrl(key: string | undefined) {\n  if (!key) {\n    return true;\n  }\n\n  return isHttpOrHttps(key) || isProxyUrlRelative(key);\n}\n\n/**\n *\n */\nexport function isHttpOrHttps(key: string | undefined) {\n  return /^http(s)?:\\/\\//.test(key || '');\n}\n\n/**\n *\n */\nexport function isProxyUrlRelative(key: string) {\n  return key.startsWith('/');\n}\n\n/**\n *\n */\nexport function proxyUrlToAbsoluteURL(url: string | undefined): string {\n  if (!url) {\n    return '';\n  }\n\n  if (!isProxyUrlRelative(url)) {\n    return url;\n  }\n\n  if (typeof window === 'undefined' || !window.location?.origin) {\n    return url;\n  }\n\n  return new URL(url, window.location.origin).toString();\n}\n\nconst AUTO_PROXY_HOST_SUFFIXES = ['.vercel.app'];\nexport const AUTO_PROXY_PATH = '/__clerk';\n\nexport function shouldAutoProxy(hostname: string): boolean {\n  return AUTO_PROXY_HOST_SUFFIXES.some(hostSuffix => hostname?.endsWith(hostSuffix)) ?? false;\n}\n\nfunction getDefaultEnvironment(): NodeJS.ProcessEnv {\n  return typeof process !== 'undefined' && process.env ? process.env : {};\n}\n\nexport function isAutoProxyDisabledFromEnvironment(environment = getDefaultEnvironment()): boolean {\n  return isTruthy(environment.CLERK_DISABLE_AUTO_PROXY);\n}\n\nfunction normalizeHostname(hostnameOrUrl: string): string {\n  if (hostnameOrUrl.startsWith('http://') || hostnameOrUrl.startsWith('https://')) {\n    try {\n      return new URL(hostnameOrUrl).hostname;\n    } catch {\n      return '';\n    }\n  }\n\n  return hostnameOrUrl.split('/')[0] || '';\n}\n\ntype GetAutoProxyUrlFromEnvironmentOptions = {\n  publishableKey: string;\n  hasDomain?: boolean;\n  hasProxyUrl?: boolean;\n  environment?: NodeJS.ProcessEnv;\n};\n\n/**\n * Determines if the current Vercel environment should use auto-proxy.\n * Note: This runs both at build time (static generation) and at runtime\n * (server-side rendering) via mergeNextClerkPropsWithEnv in providers.\n * The return value may become the proxyUrl or the script src prefix.\n */\nexport function getAutoProxyUrlFromEnvironment({\n  publishableKey,\n  hasDomain = false,\n  hasProxyUrl = false,\n  environment = getDefaultEnvironment(),\n}: GetAutoProxyUrlFromEnvironmentOptions): string {\n  if (hasProxyUrl || hasDomain || !isProductionFromPublishableKey(publishableKey)) {\n    return '';\n  }\n\n  if (isAutoProxyDisabledFromEnvironment(environment)) {\n    return '';\n  }\n\n  if (environment.VERCEL_TARGET_ENV !== 'production') {\n    return '';\n  }\n\n  const vercelProductionHostname = environment.VERCEL_PROJECT_PRODUCTION_URL;\n\n  if (!vercelProductionHostname || !shouldAutoProxy(normalizeHostname(vercelProductionHostname))) {\n    return '';\n  }\n\n  return AUTO_PROXY_PATH;\n}\n\n/**\n * Function that determines whether proxy should be used for a given URL.\n */\nexport type ShouldProxyFn = (url: URL) => boolean;\n"],"mappings":";;;;;;;AAMA,SAAgB,gBAAgB,KAAyB;CACvD,IAAI,CAAC,KACH,OAAO;CAGT,OAAO,cAAc,GAAG,KAAK,mBAAmB,GAAG;AACrD;;;;AAKA,SAAgB,cAAc,KAAyB;CACrD,OAAO,iBAAiB,KAAK,OAAO,EAAE;AACxC;;;;AAKA,SAAgB,mBAAmB,KAAa;CAC9C,OAAO,IAAI,WAAW,GAAG;AAC3B;;;;AAKA,SAAgB,sBAAsB,KAAiC;CACrE,IAAI,CAAC,KACH,OAAO;CAGT,IAAI,CAAC,mBAAmB,GAAG,GACzB,OAAO;CAGT,IAAI,OAAO,WAAW,eAAe,CAAC,OAAO,UAAU,QACrD,OAAO;CAGT,OAAO,IAAI,IAAI,KAAK,OAAO,SAAS,MAAM,CAAC,CAAC,SAAS;AACvD;AAEA,MAAM,2BAA2B,CAAC,aAAa;AAC/C,MAAa,kBAAkB;AAE/B,SAAgB,gBAAgB,UAA2B;CACzD,OAAO,yBAAyB,MAAK,eAAc,UAAU,SA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