utils.js 5.7 KB

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