utils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. 'use strict'
  2. const { HEX } = require('./scopedChars')
  3. function normalizeIPv4 (host) {
  4. if (findToken(host, '.') < 3) { return { host, isIPV4: false } }
  5. const matches = host.match(/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/u) || []
  6. const [address] = matches
  7. if (address) {
  8. return { host: stripLeadingZeros(address, '.'), isIPV4: true }
  9. } else {
  10. return { host, isIPV4: false }
  11. }
  12. }
  13. /**
  14. * @param {string[]} input
  15. * @param {boolean} [keepZero=false]
  16. * @returns {string|undefined}
  17. */
  18. function stringArrayToHexStripped (input, keepZero = false) {
  19. let acc = ''
  20. let strip = true
  21. for (const c of input) {
  22. if (HEX[c] === undefined) return undefined
  23. if (c !== '0' && strip === true) strip = false
  24. if (!strip) acc += c
  25. }
  26. if (keepZero && acc.length === 0) acc = '0'
  27. return acc
  28. }
  29. function getIPV6 (input) {
  30. let tokenCount = 0
  31. const output = { error: false, address: '', zone: '' }
  32. const address = []
  33. const buffer = []
  34. let isZone = false
  35. let endipv6Encountered = false
  36. let endIpv6 = false
  37. function consume () {
  38. if (buffer.length) {
  39. if (isZone === false) {
  40. const hex = stringArrayToHexStripped(buffer)
  41. if (hex !== undefined) {
  42. address.push(hex)
  43. } else {
  44. output.error = true
  45. return false
  46. }
  47. }
  48. buffer.length = 0
  49. }
  50. return true
  51. }
  52. for (let i = 0; i < input.length; i++) {
  53. const cursor = input[i]
  54. if (cursor === '[' || cursor === ']') { continue }
  55. if (cursor === ':') {
  56. if (endipv6Encountered === true) {
  57. endIpv6 = true
  58. }
  59. if (!consume()) { break }
  60. tokenCount++
  61. address.push(':')
  62. if (tokenCount > 7) {
  63. // not valid
  64. output.error = true
  65. break
  66. }
  67. if (i - 1 >= 0 && input[i - 1] === ':') {
  68. endipv6Encountered = true
  69. }
  70. continue
  71. } else if (cursor === '%') {
  72. if (!consume()) { break }
  73. // switch to zone detection
  74. isZone = true
  75. } else {
  76. buffer.push(cursor)
  77. continue
  78. }
  79. }
  80. if (buffer.length) {
  81. if (isZone) {
  82. output.zone = buffer.join('')
  83. } else if (endIpv6) {
  84. address.push(buffer.join(''))
  85. } else {
  86. address.push(stringArrayToHexStripped(buffer))
  87. }
  88. }
  89. output.address = address.join('')
  90. return output
  91. }
  92. function normalizeIPv6 (host, opts = {}) {
  93. if (findToken(host, ':') < 2) { return { host, isIPV6: false } }
  94. const ipv6 = getIPV6(host)
  95. if (!ipv6.error) {
  96. let newHost = ipv6.address
  97. let escapedHost = ipv6.address
  98. if (ipv6.zone) {
  99. newHost += '%' + ipv6.zone
  100. escapedHost += '%25' + ipv6.zone
  101. }
  102. return { host: newHost, escapedHost, isIPV6: true }
  103. } else {
  104. return { host, isIPV6: false }
  105. }
  106. }
  107. function stripLeadingZeros (str, token) {
  108. let out = ''
  109. let skip = true
  110. const l = str.length
  111. for (let i = 0; i < l; i++) {
  112. const c = str[i]
  113. if (c === '0' && skip) {
  114. if ((i + 1 <= l && str[i + 1] === token) || i + 1 === l) {
  115. out += c
  116. skip = false
  117. }
  118. } else {
  119. if (c === token) {
  120. skip = true
  121. } else {
  122. skip = false
  123. }
  124. out += c
  125. }
  126. }
  127. return out
  128. }
  129. function findToken (str, token) {
  130. let ind = 0
  131. for (let i = 0; i < str.length; i++) {
  132. if (str[i] === token) ind++
  133. }
  134. return ind
  135. }
  136. const RDS1 = /^\.\.?\//u
  137. const RDS2 = /^\/\.(?:\/|$)/u
  138. const RDS3 = /^\/\.\.(?:\/|$)/u
  139. const RDS5 = /^\/?(?:.|\n)*?(?=\/|$)/u
  140. function removeDotSegments (input) {
  141. const output = []
  142. while (input.length) {
  143. if (input.match(RDS1)) {
  144. input = input.replace(RDS1, '')
  145. } else if (input.match(RDS2)) {
  146. input = input.replace(RDS2, '/')
  147. } else if (input.match(RDS3)) {
  148. input = input.replace(RDS3, '/')
  149. output.pop()
  150. } else if (input === '.' || input === '..') {
  151. input = ''
  152. } else {
  153. const im = input.match(RDS5)
  154. if (im) {
  155. const s = im[0]
  156. input = input.slice(s.length)
  157. output.push(s)
  158. } else {
  159. throw new Error('Unexpected dot segment condition')
  160. }
  161. }
  162. }
  163. return output.join('')
  164. }
  165. function normalizeComponentEncoding (components, esc) {
  166. const func = esc !== true ? escape : unescape
  167. if (components.scheme !== undefined) {
  168. components.scheme = func(components.scheme)
  169. }
  170. if (components.userinfo !== undefined) {
  171. components.userinfo = func(components.userinfo)
  172. }
  173. if (components.host !== undefined) {
  174. components.host = func(components.host)
  175. }
  176. if (components.path !== undefined) {
  177. components.path = func(components.path)
  178. }
  179. if (components.query !== undefined) {
  180. components.query = func(components.query)
  181. }
  182. if (components.fragment !== undefined) {
  183. components.fragment = func(components.fragment)
  184. }
  185. return components
  186. }
  187. function recomposeAuthority (components, options) {
  188. const uriTokens = []
  189. if (components.userinfo !== undefined) {
  190. uriTokens.push(components.userinfo)
  191. uriTokens.push('@')
  192. }
  193. if (components.host !== undefined) {
  194. let host = unescape(components.host)
  195. const ipV4res = normalizeIPv4(host)
  196. if (ipV4res.isIPV4) {
  197. host = ipV4res.host
  198. } else {
  199. const ipV6res = normalizeIPv6(ipV4res.host, { isIPV4: false })
  200. if (ipV6res.isIPV6 === true) {
  201. host = `[${ipV6res.escapedHost}]`
  202. } else {
  203. host = components.host
  204. }
  205. }
  206. uriTokens.push(host)
  207. }
  208. if (typeof components.port === 'number' || typeof components.port === 'string') {
  209. uriTokens.push(':')
  210. uriTokens.push(String(components.port))
  211. }
  212. return uriTokens.length ? uriTokens.join('') : undefined
  213. };
  214. module.exports = {
  215. recomposeAuthority,
  216. normalizeComponentEncoding,
  217. removeDotSegments,
  218. normalizeIPv4,
  219. normalizeIPv6,
  220. stringArrayToHexStripped
  221. }