all files / src/ url.js

100% Statements 67/67
74.47% Branches 35/47
100% Functions 8/8
100% Lines 49/49
2 branches Ignored     
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                                                                                                                             11× 11× 11×                   24×         129×         140× 140×     140×     1400×     140×                                    
import Validators from './validators'
import { regFormat } from './helpers'
 
// user:pass BasicAuth (optional)
const BASIC_AUTH = '(?:\\S+(?::\\S*)?@)?'
 
// IP address dotted notation octets
const IPV4 =
  '(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)'
 
// the IPv6 matching part is from here: https://gist.github.com/syzdek/6086792
// IPv6 address in URLs are wrapped with []s
const IPV6 =
  `\\[(?:` +
  [
    // 1:2:3:4:5:6:7:8
    '(?:[\\da-f]{1,4}:){7,7}[\\da-f]{1,4}',
    // 2001:db8:3:4::192.0.2.33  64:ff9b::192.0.2.33  (IPv4-Embedded IPv6 Address)
    '(?:[\\da-f]{1,4}:){1,4}:' + IPV4,
    // ::255.255.255.255  ::ffff:255.255.255.255  ::ffff:0:255.255.255.255  (IPv4-mapped IPv6 addresses)
    '::(?:ffff(?::0{1,4}){0,1}:){0,1}' + IPV4,
    // 1::3:4:5:6:7:8  1::3:4:5:6:7:8 1::8
    '[\\da-f]{1,4}:(?:(?::[\\da-f]{1,4}){1,6})',
    // 1::4:5:6:7:8  1:2::4:5:6:7:8  1:2::8
    '(?:[\\da-f]{1,4}:){1,2}(?::[\\da-f]{1,4}){1,5}',
    // 1::5:6:7:8  1:2:3::5:6:7:8  1:2:3::8
    '(?:[\\da-f]{1,4}:){1,3}(?::[\\da-f]{1,4}){1,4}',
    // 1::6:7:8  1:2:3:4::6:7:8  1:2:3:4::8
    '(?:[\\da-f]{1,4}:){1,4}(?::[\\da-f]{1,4}){1,3}',
    // 1::7:8  1:2:3:4:5::7:8  1:2:3:4:5::8
    '(?:[\\da-f]{1,4}:){1,5}(?::[\\da-f]{1,4}){1,2}',
    // 1::8  1:2:3:4:5:6::8  1:2:3:4:5:6::8
    '(?:[\\da-f]{1,4}:){1,6}:[\\da-f]{1,4}',
    // 1::
    '(?:[\\da-f]{1,4}:){1,7}:',
    // ::2:3:4:5:6:7:8  ::2:3:4:5:6:7:8  ::8  ::
    ':(?:(?::[\\da-f]{1,4}){1,7}|:)'
  ].join('|') +
  ')\\]'
 
// host & domain names, may end with dot
const HOST =
  // can be replaced by
  // '(?:(?:[a-z0-9\\u00a1-\\uffff][a-z0-9\\u00a1-\\uffff_-]{0,62})?[a-z0-9\\u00a1-\\uffff]\\.)+' +
  '(?![-_])(?:[-\\w\\u00a1-\\uffff]{0,63}[^-_]\\.)+' +
  // TLD identifier name, may end with dot
  '(?:[a-z\\u00a1-\\uffff]{2,}\\.?)'
 
// port number (optional)
const PORT = '(?::\\d{2,5})?'
 
// resource path (optional)
const PATH = '(?:[/][^\\s?#]*)?'
 
const SEARCH = '(?:[?][^\\s#]*)?'
 
const HASH = '(?:[#]\\S*)?'
 
const DEFAULT_OPTIONS = {
  protocolIdentifier: true,
  basicAuth: true,
  local: true,
  ipv4: true,
  ipv6: true,
  host: true,
  port: true,
  path: true,
  search: true,
  hash: true
}
 
const STRIP_PROTOCOL_REG = /:?\/\/$/
 
const CLEAN_UP_PROPS = 'protocol ipv4 host path search hash'.split(' ')
 
function parseURL (url, options) {
  options = defaultOptions(options)
  let parts = url.match(buildReg(options, true))
  if (!parts) return null
  let group = 1
  let h = {
    protocol: parts[group++].replace(STRIP_PROTOCOL_REG, '')
  }
  Eif (options.basicAuth) {
    let auth = parts[group++]
    Eif (auth) {
      let [username, ...password] = auth.slice(0, -1).split(':')
      h.basicAuth = { username, password: password.length ? password.join(':') : void 0 }
    }
  }
  Eif (options.ipv4) h.ipv4 = parts[group++]
  Eif (options.ipv6) {
    let ipv6 = parts[group++]
    if (ipv6) h.ipv6 = ipv6.slice(1, -1)
  }
  Eif (options.host) h.host = parts[group++]
  Eif (options.local) h.host = parts[group++] || h.host
  Eif (options.port) {
    let port = parts[group++]
    Eif (port) h.port = +port.slice(1)
  }
  Eif (options.path) h.path = parts[group++]
  Eif (options.search) h.search = parts[group++]
  Eif (options.hash) h.hash = parts[group++]
 
  // Clean up
  CLEAN_UP_PROPS.forEach(k => {
    if (!h[k]) delete h[k]
  })
  return h
}
 
// Uses "format" internally which is already memoized
let url = regFormat(options => buildReg(defaultOptions(options), false), 'url')
 
url.parseURL = parseURL
 
export default url
 
function defaultOptions (options) {
  options = Object.assign({}, DEFAULT_OPTIONS, options)
  options.protocols = []
    .concat(options.protocol || options.protocols || Validators.defaultOptions.urlProtocols)
    .join('|')
  return options
}
 
function group (option, regPart, capture) {
  return option ? (capture ? `(${regPart})` : regPart) : ''
}
 
function buildReg (options, capture) {
  return new RegExp(
    '^' +
      group(true, `(?:(?:(?:${options.protocols}):)?\\/\\/)${options.protocolIdentifier ? '' : '?'}`, capture) +
      group(options.basicAuth, BASIC_AUTH, capture) +
      `(?:${[
        group(options.ipv4, IPV4, capture),
        group(options.ipv6, IPV6, capture),
        group(options.host, HOST, capture),
        group(options.local, 'localhost', capture)
      ].join('|')})` +
      group(options.port, PORT, capture) +
      group(options.path, PATH, capture) +
      group(options.search, SEARCH, capture) +
      group(options.hash, HASH, capture) +
      '$',
    'i'
  )
}