All files index.ts

100% Statements 73/73
100% Branches 20/20
100% Functions 7/7
100% Lines 73/73

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 7412x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 1x 1x 2x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 2x 2x 2x 1x 1x 1x 12x 12x 12x 10x 10x 8x 8x 7x 7x 12x 12x 12x 1x  
type Options = {
  domain?: string
  expires?: Date | string
  httpOnly?: boolean
  maxAge?: number
  path?: string
  sameSite?: 'Lax' | 'None' | 'Strict'
  secure?: boolean
}
 
const aliases = {
  domain: 'Domain',
  expires: 'Expires',
  httpOnly: 'HttpOnly',
  maxAge: 'Max-Age',
  path: 'Path',
  sameSite: 'SameSite',
  secure: 'Secure',
}
 
function base(name: string, value: string | number) {
  return name + (value || value === 0 ? '=' + value : '')
}
 
function bool(name: string) {
  return name
}
 
function date(name: string, value: Date | string) {
  return base(name, value instanceof Date ? value.toUTCString() : value)
}
 
const serializers = {
  domain: base,
  expires: date,
  httpOnly: bool,
  maxAge: base,
  path: base,
  sameSite: base,
  secure: bool,
}
 
export function parse(header: string): {
  [name: string]: string
} {
  const cookies = {}
  const parts = header
    .split(';')
    .map((s) => s.trim())
    .filter(Boolean)
 
  for (const part of parts) {
    const [key, value] = part.split('=')
    cookies[key] = decodeURIComponent(value)
  }
 
  return cookies
}
 
export function serialize(name: string, value: string, options: Options = {}) {
  const cookie = [base(name, value)]
 
  for (const key of Object.keys(options)) {
    const value = options[key]
    if (!value && value !== 0) continue
    const serializer = serializers[key]
    if (!serializer) continue
    const serialized = serializer(aliases[key], options[key])
    cookie.push(serialized)
  }
 
  return cookie.join('; ')
}