re.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. const {
  2. MAX_SAFE_COMPONENT_LENGTH,
  3. MAX_SAFE_BUILD_LENGTH,
  4. MAX_LENGTH,
  5. } = require('./constants')
  6. const debug = require('./debug')
  7. exports = module.exports = {}
  8. // The actual regexps go on exports.re
  9. const re = exports.re = []
  10. const safeRe = exports.safeRe = []
  11. const src = exports.src = []
  12. const safeSrc = exports.safeSrc = []
  13. const t = exports.t = {}
  14. let R = 0
  15. const LETTERDASHNUMBER = '[a-zA-Z0-9-]'
  16. // Replace some greedy regex tokens to prevent regex dos issues. These regex are
  17. // used internally via the safeRe object since all inputs in this library get
  18. // normalized first to trim and collapse all extra whitespace. The original
  19. // regexes are exported for userland consumption and lower level usage. A
  20. // future breaking change could export the safer regex only with a note that
  21. // all input should have extra whitespace removed.
  22. const safeRegexReplacements = [
  23. ['\\s', 1],
  24. ['\\d', MAX_LENGTH],
  25. [LETTERDASHNUMBER, MAX_SAFE_BUILD_LENGTH],
  26. ]
  27. const makeSafeRegex = (value) => {
  28. for (const [token, max] of safeRegexReplacements) {
  29. value = value
  30. .split(`${token}*`).join(`${token}{0,${max}}`)
  31. .split(`${token}+`).join(`${token}{1,${max}}`)
  32. }
  33. return value
  34. }
  35. const createToken = (name, value, isGlobal) => {
  36. const safe = makeSafeRegex(value)
  37. const index = R++
  38. debug(name, index, value)
  39. t[name] = index
  40. src[index] = value
  41. safeSrc[index] = safe
  42. re[index] = new RegExp(value, isGlobal ? 'g' : undefined)
  43. safeRe[index] = new RegExp(safe, isGlobal ? 'g' : undefined)
  44. }
  45. // The following Regular Expressions can be used for tokenizing,
  46. // validating, and parsing SemVer version strings.
  47. // ## Numeric Identifier
  48. // A single `0`, or a non-zero digit followed by zero or more digits.
  49. createToken('NUMERICIDENTIFIER', '0|[1-9]\\d*')
  50. createToken('NUMERICIDENTIFIERLOOSE', '\\d+')
  51. // ## Non-numeric Identifier
  52. // Zero or more digits, followed by a letter or hyphen, and then zero or
  53. // more letters, digits, or hyphens.
  54. createToken('NONNUMERICIDENTIFIER', `\\d*[a-zA-Z-]${LETTERDASHNUMBER}*`)
  55. // ## Main Version
  56. // Three dot-separated numeric identifiers.
  57. createToken('MAINVERSION', `(${src[t.NUMERICIDENTIFIER]})\\.` +
  58. `(${src[t.NUMERICIDENTIFIER]})\\.` +
  59. `(${src[t.NUMERICIDENTIFIER]})`)
  60. createToken('MAINVERSIONLOOSE', `(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +
  61. `(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +
  62. `(${src[t.NUMERICIDENTIFIERLOOSE]})`)
  63. // ## Pre-release Version Identifier
  64. // A numeric identifier, or a non-numeric identifier.
  65. createToken('PRERELEASEIDENTIFIER', `(?:${src[t.NUMERICIDENTIFIER]
  66. }|${src[t.NONNUMERICIDENTIFIER]})`)
  67. createToken('PRERELEASEIDENTIFIERLOOSE', `(?:${src[t.NUMERICIDENTIFIERLOOSE]
  68. }|${src[t.NONNUMERICIDENTIFIER]})`)
  69. // ## Pre-release Version
  70. // Hyphen, followed by one or more dot-separated pre-release version
  71. // identifiers.
  72. createToken('PRERELEASE', `(?:-(${src[t.PRERELEASEIDENTIFIER]
  73. }(?:\\.${src[t.PRERELEASEIDENTIFIER]})*))`)
  74. createToken('PRERELEASELOOSE', `(?:-?(${src[t.PRERELEASEIDENTIFIERLOOSE]
  75. }(?:\\.${src[t.PRERELEASEIDENTIFIERLOOSE]})*))`)
  76. // ## Build Metadata Identifier
  77. // Any combination of digits, letters, or hyphens.
  78. createToken('BUILDIDENTIFIER', `${LETTERDASHNUMBER}+`)
  79. // ## Build Metadata
  80. // Plus sign, followed by one or more period-separated build metadata
  81. // identifiers.
  82. createToken('BUILD', `(?:\\+(${src[t.BUILDIDENTIFIER]
  83. }(?:\\.${src[t.BUILDIDENTIFIER]})*))`)
  84. // ## Full Version String
  85. // A main version, followed optionally by a pre-release version and
  86. // build metadata.
  87. // Note that the only major, minor, patch, and pre-release sections of
  88. // the version string are capturing groups. The build metadata is not a
  89. // capturing group, because it should not ever be used in version
  90. // comparison.
  91. createToken('FULLPLAIN', `v?${src[t.MAINVERSION]
  92. }${src[t.PRERELEASE]}?${
  93. src[t.BUILD]}?`)
  94. createToken('FULL', `^${src[t.FULLPLAIN]}$`)
  95. // like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
  96. // also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
  97. // common in the npm registry.
  98. createToken('LOOSEPLAIN', `[v=\\s]*${src[t.MAINVERSIONLOOSE]
  99. }${src[t.PRERELEASELOOSE]}?${
  100. src[t.BUILD]}?`)
  101. createToken('LOOSE', `^${src[t.LOOSEPLAIN]}$`)
  102. createToken('GTLT', '((?:<|>)?=?)')
  103. // Something like "2.*" or "1.2.x".
  104. // Note that "x.x" is a valid xRange identifer, meaning "any version"
  105. // Only the first item is strictly required.
  106. createToken('XRANGEIDENTIFIERLOOSE', `${src[t.NUMERICIDENTIFIERLOOSE]}|x|X|\\*`)
  107. createToken('XRANGEIDENTIFIER', `${src[t.NUMERICIDENTIFIER]}|x|X|\\*`)
  108. createToken('XRANGEPLAIN', `[v=\\s]*(${src[t.XRANGEIDENTIFIER]})` +
  109. `(?:\\.(${src[t.XRANGEIDENTIFIER]})` +
  110. `(?:\\.(${src[t.XRANGEIDENTIFIER]})` +
  111. `(?:${src[t.PRERELEASE]})?${
  112. src[t.BUILD]}?` +
  113. `)?)?`)
  114. createToken('XRANGEPLAINLOOSE', `[v=\\s]*(${src[t.XRANGEIDENTIFIERLOOSE]})` +
  115. `(?:\\.(${src[t.XRANGEIDENTIFIERLOOSE]})` +
  116. `(?:\\.(${src[t.XRANGEIDENTIFIERLOOSE]})` +
  117. `(?:${src[t.PRERELEASELOOSE]})?${
  118. src[t.BUILD]}?` +
  119. `)?)?`)
  120. createToken('XRANGE', `^${src[t.GTLT]}\\s*${src[t.XRANGEPLAIN]}$`)
  121. createToken('XRANGELOOSE', `^${src[t.GTLT]}\\s*${src[t.XRANGEPLAINLOOSE]}$`)
  122. // Coercion.
  123. // Extract anything that could conceivably be a part of a valid semver
  124. createToken('COERCEPLAIN', `${'(^|[^\\d])' +
  125. '(\\d{1,'}${MAX_SAFE_COMPONENT_LENGTH}})` +
  126. `(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?` +
  127. `(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?`)
  128. createToken('COERCE', `${src[t.COERCEPLAIN]}(?:$|[^\\d])`)
  129. createToken('COERCEFULL', src[t.COERCEPLAIN] +
  130. `(?:${src[t.PRERELEASE]})?` +
  131. `(?:${src[t.BUILD]})?` +
  132. `(?:$|[^\\d])`)
  133. createToken('COERCERTL', src[t.COERCE], true)
  134. createToken('COERCERTLFULL', src[t.COERCEFULL], true)
  135. // Tilde ranges.
  136. // Meaning is "reasonably at or greater than"
  137. createToken('LONETILDE', '(?:~>?)')
  138. createToken('TILDETRIM', `(\\s*)${src[t.LONETILDE]}\\s+`, true)
  139. exports.tildeTrimReplace = '$1~'
  140. createToken('TILDE', `^${src[t.LONETILDE]}${src[t.XRANGEPLAIN]}$`)
  141. createToken('TILDELOOSE', `^${src[t.LONETILDE]}${src[t.XRANGEPLAINLOOSE]}$`)
  142. // Caret ranges.
  143. // Meaning is "at least and backwards compatible with"
  144. createToken('LONECARET', '(?:\\^)')
  145. createToken('CARETTRIM', `(\\s*)${src[t.LONECARET]}\\s+`, true)
  146. exports.caretTrimReplace = '$1^'
  147. createToken('CARET', `^${src[t.LONECARET]}${src[t.XRANGEPLAIN]}$`)
  148. createToken('CARETLOOSE', `^${src[t.LONECARET]}${src[t.XRANGEPLAINLOOSE]}$`)
  149. // A simple gt/lt/eq thing, or just "" to indicate "any version"
  150. createToken('COMPARATORLOOSE', `^${src[t.GTLT]}\\s*(${src[t.LOOSEPLAIN]})$|^$`)
  151. createToken('COMPARATOR', `^${src[t.GTLT]}\\s*(${src[t.FULLPLAIN]})$|^$`)
  152. // An expression to strip any whitespace between the gtlt and the thing
  153. // it modifies, so that `> 1.2.3` ==> `>1.2.3`
  154. createToken('COMPARATORTRIM', `(\\s*)${src[t.GTLT]
  155. }\\s*(${src[t.LOOSEPLAIN]}|${src[t.XRANGEPLAIN]})`, true)
  156. exports.comparatorTrimReplace = '$1$2$3'
  157. // Something like `1.2.3 - 1.2.4`
  158. // Note that these all use the loose form, because they'll be
  159. // checked against either the strict or loose comparator form
  160. // later.
  161. createToken('HYPHENRANGE', `^\\s*(${src[t.XRANGEPLAIN]})` +
  162. `\\s+-\\s+` +
  163. `(${src[t.XRANGEPLAIN]})` +
  164. `\\s*$`)
  165. createToken('HYPHENRANGELOOSE', `^\\s*(${src[t.XRANGEPLAINLOOSE]})` +
  166. `\\s+-\\s+` +
  167. `(${src[t.XRANGEPLAINLOOSE]})` +
  168. `\\s*$`)
  169. // Star ranges basically just allow anything at all.
  170. createToken('STAR', '(<|>)?=?\\s*\\*')
  171. // >=0.0.0 is like a star
  172. createToken('GTE0', '^\\s*>=\\s*0\\.0\\.0\\s*$')
  173. createToken('GTE0PRE', '^\\s*>=\\s*0\\.0\\.0-0\\s*$')