All files / common get-cookie-domain.ts

100% Statements 10/10
92.85% Branches 13/14
100% Functions 1/1
100% Lines 10/10

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              25x 2x     23x 5x     18x   18x 10x     8x 2x     6x    
import tldjs from "tldjs"
import { CreateApiHandlerOptions } from "./get-base-url"
 
export function guessCookieDomain(
  url: string | undefined,
  options: CreateApiHandlerOptions,
) {
  if (!url || options.forceCookieDomain) {
    return options.forceCookieDomain
  }
 
  if (options.dontUseTldForCookieDomain) {
    return undefined
  }
 
  const parsed = tldjs.parse(url || "")
 
  if (!parsed.isValid || parsed.isIp) {
    return undefined
  }
 
  if (!parsed.domain) {
    return parsed.hostname
  }
 
  return parsed.domain
}