All files / apps / desktop/src / lib app-url.ts

100% Statements 11/11
100% Branches 10/10
100% Functions 1/1
100% Lines 9/9

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    1x           7x   7x   5x   4x 4x   7x 2x     2x    
import { APP_URL_ENV, DEV_URL } from '../constants'
 
const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1'])
 
// shortcut: read from the environment at start, fine for development; a packaged build bakes the
// production URL in when packaging lands (needs the domain of ticket B1).
// Remote content is loaded over https only; plain http is accepted for this machine alone.
export function resolveAppUrl(env: Record<string, string | undefined>): string {
  const value = env[APP_URL_ENV]?.trim()
 
  if (!value) return DEV_URL
 
  if (!URL.canParse(value)) throw new Error(`${APP_URL_ENV} is not a URL.`)
 
  const url = new URL(value)
  const isLocalHttp = url.protocol === 'http:' && LOOPBACK_HOSTS.has(url.hostname)
 
  if (url.protocol !== 'https:' && !isLocalHttp) {
    throw new Error(`${APP_URL_ENV} must be https, or http on localhost.`)
  }
 
  return url.href
}