All files origins.ts

100% Statements 43/43
100% Branches 18/18
100% Functions 2/2
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 12x 2x 10x 37x 8x 8x 37x 37x 37x 26x 22x 22x 18x 15x 37x 19x 19x 19x 19x 37x 37x  
type WebOriginOptions = {
  configuredUrl: string | undefined
  vercelUrl?: string
  vercelEnv?: string
  allowVercelUrl?: boolean
  variableName?: string
  localOrigin: string
}
 
// A deployment's own VERCEL_URL is only suitable for that app's origin.
// Cross-app preview links need the other project's explicit URL.
export function resolveWebOrigin({
  configuredUrl,
  vercelUrl,
  vercelEnv,
  allowVercelUrl = true,
  variableName = "web origin",
  localOrigin,
}: WebOriginOptions): string {
  const deployed = Boolean(vercelEnv || vercelUrl)
  const selected =
    configuredUrl ||
    (allowVercelUrl && vercelEnv !== "production" && vercelUrl
      ? `https://${vercelUrl}`
      : undefined)
  if (!selected && deployed) {
    throw new Error(`${variableName} must be configured for this deployment`)
  }
  const url = new URL(selected || localOrigin)
  if (
    !["http:", "https:"].includes(url.protocol) ||
    url.username ||
    url.password ||
    (deployed &&
      (url.protocol !== "https:" ||
        ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)))
  ) {
    throw new Error(
      `${variableName} must be a valid public HTTPS origin for deployments`
    )
  }
  return url.origin
}