All files / src/internal url.ts

92.85% Statements 13/14
90.9% Branches 10/11
100% Functions 4/4
92.85% Lines 13/14

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      68x 68x 68x       67x       85x 1x   84x       206x 206x 7x     199x         36x 36x    
import { stripTrailing } from './text'
 
export function sanitizeAuthorityHost(authorityHost: string): string {
  const trimmed = authorityHost.trim()
  const url = requireHttpsUrl(trimmed, 'authorityHost')
  Iif (url.href.includes('?') || url.href.includes('#')) {
    throw new TypeError('authorityHost cannot contain a query or fragment')
  }
 
  return stripTrailing(trimmed, '/')
}
 
export function encodeResourceSegment(value: string, name: string): string {
  if (value.trim().length === 0 || value === '.' || value === '..') {
    throw new TypeError(`${name} must be a non-empty string other than "." or ".."`)
  }
  return encodeURIComponent(value)
}
 
export function requireHttpsUrl(input: string, name: string): URL {
  const url = new URL(input)
  if (url.protocol !== 'https:') {
    throw new TypeError(`${name} must use HTTPS`)
  }
 
  return url
}
 
/** HTTPS service endpoint without trailing slashes, query or fragment. */
export function normalizeServiceUrl(input: string, name: string): string {
  const url = requireHttpsUrl(input, name)
  return `${url.origin}${stripTrailing(url.pathname, '/')}`
}