range.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // hoisted class for cyclic dependency
  2. class Range {
  3. constructor (range, options) {
  4. options = parseOptions(options)
  5. if (range instanceof Range) {
  6. if (
  7. range.loose === !!options.loose &&
  8. range.includePrerelease === !!options.includePrerelease
  9. ) {
  10. return range
  11. } else {
  12. return new Range(range.raw, options)
  13. }
  14. }
  15. if (range instanceof Comparator) {
  16. // just put it in the set and return
  17. this.raw = range.value
  18. this.set = [[range]]
  19. this.format()
  20. return this
  21. }
  22. this.options = options
  23. this.loose = !!options.loose
  24. this.includePrerelease = !!options.includePrerelease
  25. // First, split based on boolean or ||
  26. this.raw = range
  27. this.set = range
  28. .split(/\s*\|\|\s*/)
  29. // map the range to a 2d array of comparators
  30. .map(range => this.parseRange(range.trim()))
  31. // throw out any comparator lists that are empty
  32. // this generally means that it was not a valid range, which is allowed
  33. // in loose mode, but will still throw if the WHOLE range is invalid.
  34. .filter(c => c.length)
  35. if (!this.set.length) {
  36. throw new TypeError(`Invalid SemVer Range: ${range}`)
  37. }
  38. // if we have any that are not the null set, throw out null sets.
  39. if (this.set.length > 1) {
  40. // keep the first one, in case they're all null sets
  41. const first = this.set[0]
  42. this.set = this.set.filter(c => !isNullSet(c[0]))
  43. if (this.set.length === 0)
  44. this.set = [first]
  45. else if (this.set.length > 1) {
  46. // if we have any that are *, then the range is just *
  47. for (const c of this.set) {
  48. if (c.length === 1 && isAny(c[0])) {
  49. this.set = [c]
  50. break
  51. }
  52. }
  53. }
  54. }
  55. this.format()
  56. }
  57. format () {
  58. this.range = this.set
  59. .map((comps) => {
  60. return comps.join(' ').trim()
  61. })
  62. .join('||')
  63. .trim()
  64. return this.range
  65. }
  66. toString () {
  67. return this.range
  68. }
  69. parseRange (range) {
  70. range = range.trim()
  71. // memoize range parsing for performance.
  72. // this is a very hot path, and fully deterministic.
  73. const memoOpts = Object.keys(this.options).join(',')
  74. const memoKey = `parseRange:${memoOpts}:${range}`
  75. const cached = cache.get(memoKey)
  76. if (cached)
  77. return cached
  78. const loose = this.options.loose
  79. // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
  80. const hr = loose ? re[t.HYPHENRANGELOOSE] : re[t.HYPHENRANGE]
  81. range = range.replace(hr, hyphenReplace(this.options.includePrerelease))
  82. debug('hyphen replace', range)
  83. // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
  84. range = range.replace(re[t.COMPARATORTRIM], comparatorTrimReplace)
  85. debug('comparator trim', range, re[t.COMPARATORTRIM])
  86. // `~ 1.2.3` => `~1.2.3`
  87. range = range.replace(re[t.TILDETRIM], tildeTrimReplace)
  88. // `^ 1.2.3` => `^1.2.3`
  89. range = range.replace(re[t.CARETTRIM], caretTrimReplace)
  90. // normalize spaces
  91. range = range.split(/\s+/).join(' ')
  92. // At this point, the range is completely trimmed and
  93. // ready to be split into comparators.
  94. const compRe = loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR]
  95. const rangeList = range
  96. .split(' ')
  97. .map(comp => parseComparator(comp, this.options))
  98. .join(' ')
  99. .split(/\s+/)
  100. // >=0.0.0 is equivalent to *
  101. .map(comp => replaceGTE0(comp, this.options))
  102. // in loose mode, throw out any that are not valid comparators
  103. .filter(this.options.loose ? comp => !!comp.match(compRe) : () => true)
  104. .map(comp => new Comparator(comp, this.options))
  105. // if any comparators are the null set, then replace with JUST null set
  106. // if more than one comparator, remove any * comparators
  107. // also, don't include the same comparator more than once
  108. const l = rangeList.length
  109. const rangeMap = new Map()
  110. for (const comp of rangeList) {
  111. if (isNullSet(comp))
  112. return [comp]
  113. rangeMap.set(comp.value, comp)
  114. }
  115. if (rangeMap.size > 1 && rangeMap.has(''))
  116. rangeMap.delete('')
  117. const result = [...rangeMap.values()]
  118. cache.set(memoKey, result)
  119. return result
  120. }
  121. intersects (range, options) {
  122. if (!(range instanceof Range)) {
  123. throw new TypeError('a Range is required')
  124. }
  125. return this.set.some((thisComparators) => {
  126. return (
  127. isSatisfiable(thisComparators, options) &&
  128. range.set.some((rangeComparators) => {
  129. return (
  130. isSatisfiable(rangeComparators, options) &&
  131. thisComparators.every((thisComparator) => {
  132. return rangeComparators.every((rangeComparator) => {
  133. return thisComparator.intersects(rangeComparator, options)
  134. })
  135. })
  136. )
  137. })
  138. )
  139. })
  140. }
  141. // if ANY of the sets match ALL of its comparators, then pass
  142. test (version) {
  143. if (!version) {
  144. return false
  145. }
  146. if (typeof version === 'string') {
  147. try {
  148. version = new SemVer(version, this.options)
  149. } catch (er) {
  150. return false
  151. }
  152. }
  153. for (let i = 0; i < this.set.length; i++) {
  154. if (testSet(this.set[i], version, this.options)) {
  155. return true
  156. }
  157. }
  158. return false
  159. }
  160. }
  161. module.exports = Range
  162. const LRU = require('lru-cache')
  163. const cache = new LRU({ max: 1000 })
  164. const parseOptions = require('../internal/parse-options')
  165. const Comparator = require('./comparator')
  166. const debug = require('../internal/debug')
  167. const SemVer = require('./semver')
  168. const {
  169. re,
  170. t,
  171. comparatorTrimReplace,
  172. tildeTrimReplace,
  173. caretTrimReplace
  174. } = require('../internal/re')
  175. const isNullSet = c => c.value === '<0.0.0-0'
  176. const isAny = c => c.value === ''
  177. // take a set of comparators and determine whether there
  178. // exists a version which can satisfy it
  179. const isSatisfiable = (comparators, options) => {
  180. let result = true
  181. const remainingComparators = comparators.slice()
  182. let testComparator = remainingComparators.pop()
  183. while (result && remainingComparators.length) {
  184. result = remainingComparators.every((otherComparator) => {
  185. return testComparator.intersects(otherComparator, options)
  186. })
  187. testComparator = remainingComparators.pop()
  188. }
  189. return result
  190. }
  191. // comprised of xranges, tildes, stars, and gtlt's at this point.
  192. // already replaced the hyphen ranges
  193. // turn into a set of JUST comparators.
  194. const parseComparator = (comp, options) => {
  195. debug('comp', comp, options)
  196. comp = replaceCarets(comp, options)
  197. debug('caret', comp)
  198. comp = replaceTildes(comp, options)
  199. debug('tildes', comp)
  200. comp = replaceXRanges(comp, options)
  201. debug('xrange', comp)
  202. comp = replaceStars(comp, options)
  203. debug('stars', comp)
  204. return comp
  205. }
  206. const isX = id => !id || id.toLowerCase() === 'x' || id === '*'
  207. // ~, ~> --> * (any, kinda silly)
  208. // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0-0
  209. // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0-0
  210. // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0-0
  211. // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0-0
  212. // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0-0
  213. const replaceTildes = (comp, options) =>
  214. comp.trim().split(/\s+/).map((comp) => {
  215. return replaceTilde(comp, options)
  216. }).join(' ')
  217. const replaceTilde = (comp, options) => {
  218. const r = options.loose ? re[t.TILDELOOSE] : re[t.TILDE]
  219. return comp.replace(r, (_, M, m, p, pr) => {
  220. debug('tilde', comp, _, M, m, p, pr)
  221. let ret
  222. if (isX(M)) {
  223. ret = ''
  224. } else if (isX(m)) {
  225. ret = `>=${M}.0.0 <${+M + 1}.0.0-0`
  226. } else if (isX(p)) {
  227. // ~1.2 == >=1.2.0 <1.3.0-0
  228. ret = `>=${M}.${m}.0 <${M}.${+m + 1}.0-0`
  229. } else if (pr) {
  230. debug('replaceTilde pr', pr)
  231. ret = `>=${M}.${m}.${p}-${pr
  232. } <${M}.${+m + 1}.0-0`
  233. } else {
  234. // ~1.2.3 == >=1.2.3 <1.3.0-0
  235. ret = `>=${M}.${m}.${p
  236. } <${M}.${+m + 1}.0-0`
  237. }
  238. debug('tilde return', ret)
  239. return ret
  240. })
  241. }
  242. // ^ --> * (any, kinda silly)
  243. // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0-0
  244. // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0-0
  245. // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0-0
  246. // ^1.2.3 --> >=1.2.3 <2.0.0-0
  247. // ^1.2.0 --> >=1.2.0 <2.0.0-0
  248. const replaceCarets = (comp, options) =>
  249. comp.trim().split(/\s+/).map((comp) => {
  250. return replaceCaret(comp, options)
  251. }).join(' ')
  252. const replaceCaret = (comp, options) => {
  253. debug('caret', comp, options)
  254. const r = options.loose ? re[t.CARETLOOSE] : re[t.CARET]
  255. const z = options.includePrerelease ? '-0' : ''
  256. return comp.replace(r, (_, M, m, p, pr) => {
  257. debug('caret', comp, _, M, m, p, pr)
  258. let ret
  259. if (isX(M)) {
  260. ret = ''
  261. } else if (isX(m)) {
  262. ret = `>=${M}.0.0${z} <${+M + 1}.0.0-0`
  263. } else if (isX(p)) {
  264. if (M === '0') {
  265. ret = `>=${M}.${m}.0${z} <${M}.${+m + 1}.0-0`
  266. } else {
  267. ret = `>=${M}.${m}.0${z} <${+M + 1}.0.0-0`
  268. }
  269. } else if (pr) {
  270. debug('replaceCaret pr', pr)
  271. if (M === '0') {
  272. if (m === '0') {
  273. ret = `>=${M}.${m}.${p}-${pr
  274. } <${M}.${m}.${+p + 1}-0`
  275. } else {
  276. ret = `>=${M}.${m}.${p}-${pr
  277. } <${M}.${+m + 1}.0-0`
  278. }
  279. } else {
  280. ret = `>=${M}.${m}.${p}-${pr
  281. } <${+M + 1}.0.0-0`
  282. }
  283. } else {
  284. debug('no pr')
  285. if (M === '0') {
  286. if (m === '0') {
  287. ret = `>=${M}.${m}.${p
  288. }${z} <${M}.${m}.${+p + 1}-0`
  289. } else {
  290. ret = `>=${M}.${m}.${p
  291. }${z} <${M}.${+m + 1}.0-0`
  292. }
  293. } else {
  294. ret = `>=${M}.${m}.${p
  295. } <${+M + 1}.0.0-0`
  296. }
  297. }
  298. debug('caret return', ret)
  299. return ret
  300. })
  301. }
  302. const replaceXRanges = (comp, options) => {
  303. debug('replaceXRanges', comp, options)
  304. return comp.split(/\s+/).map((comp) => {
  305. return replaceXRange(comp, options)
  306. }).join(' ')
  307. }
  308. const replaceXRange = (comp, options) => {
  309. comp = comp.trim()
  310. const r = options.loose ? re[t.XRANGELOOSE] : re[t.XRANGE]
  311. return comp.replace(r, (ret, gtlt, M, m, p, pr) => {
  312. debug('xRange', comp, ret, gtlt, M, m, p, pr)
  313. const xM = isX(M)
  314. const xm = xM || isX(m)
  315. const xp = xm || isX(p)
  316. const anyX = xp
  317. if (gtlt === '=' && anyX) {
  318. gtlt = ''
  319. }
  320. // if we're including prereleases in the match, then we need
  321. // to fix this to -0, the lowest possible prerelease value
  322. pr = options.includePrerelease ? '-0' : ''
  323. if (xM) {
  324. if (gtlt === '>' || gtlt === '<') {
  325. // nothing is allowed
  326. ret = '<0.0.0-0'
  327. } else {
  328. // nothing is forbidden
  329. ret = '*'
  330. }
  331. } else if (gtlt && anyX) {
  332. // we know patch is an x, because we have any x at all.
  333. // replace X with 0
  334. if (xm) {
  335. m = 0
  336. }
  337. p = 0
  338. if (gtlt === '>') {
  339. // >1 => >=2.0.0
  340. // >1.2 => >=1.3.0
  341. gtlt = '>='
  342. if (xm) {
  343. M = +M + 1
  344. m = 0
  345. p = 0
  346. } else {
  347. m = +m + 1
  348. p = 0
  349. }
  350. } else if (gtlt === '<=') {
  351. // <=0.7.x is actually <0.8.0, since any 0.7.x should
  352. // pass. Similarly, <=7.x is actually <8.0.0, etc.
  353. gtlt = '<'
  354. if (xm) {
  355. M = +M + 1
  356. } else {
  357. m = +m + 1
  358. }
  359. }
  360. if (gtlt === '<')
  361. pr = '-0'
  362. ret = `${gtlt + M}.${m}.${p}${pr}`
  363. } else if (xm) {
  364. ret = `>=${M}.0.0${pr} <${+M + 1}.0.0-0`
  365. } else if (xp) {
  366. ret = `>=${M}.${m}.0${pr
  367. } <${M}.${+m + 1}.0-0`
  368. }
  369. debug('xRange return', ret)
  370. return ret
  371. })
  372. }
  373. // Because * is AND-ed with everything else in the comparator,
  374. // and '' means "any version", just remove the *s entirely.
  375. const replaceStars = (comp, options) => {
  376. debug('replaceStars', comp, options)
  377. // Looseness is ignored here. star is always as loose as it gets!
  378. return comp.trim().replace(re[t.STAR], '')
  379. }
  380. const replaceGTE0 = (comp, options) => {
  381. debug('replaceGTE0', comp, options)
  382. return comp.trim()
  383. .replace(re[options.includePrerelease ? t.GTE0PRE : t.GTE0], '')
  384. }
  385. // This function is passed to string.replace(re[t.HYPHENRANGE])
  386. // M, m, patch, prerelease, build
  387. // 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
  388. // 1.2.3 - 3.4 => >=1.2.0 <3.5.0-0 Any 3.4.x will do
  389. // 1.2 - 3.4 => >=1.2.0 <3.5.0-0
  390. const hyphenReplace = incPr => ($0,
  391. from, fM, fm, fp, fpr, fb,
  392. to, tM, tm, tp, tpr, tb) => {
  393. if (isX(fM)) {
  394. from = ''
  395. } else if (isX(fm)) {
  396. from = `>=${fM}.0.0${incPr ? '-0' : ''}`
  397. } else if (isX(fp)) {
  398. from = `>=${fM}.${fm}.0${incPr ? '-0' : ''}`
  399. } else if (fpr) {
  400. from = `>=${from}`
  401. } else {
  402. from = `>=${from}${incPr ? '-0' : ''}`
  403. }
  404. if (isX(tM)) {
  405. to = ''
  406. } else if (isX(tm)) {
  407. to = `<${+tM + 1}.0.0-0`
  408. } else if (isX(tp)) {
  409. to = `<${tM}.${+tm + 1}.0-0`
  410. } else if (tpr) {
  411. to = `<=${tM}.${tm}.${tp}-${tpr}`
  412. } else if (incPr) {
  413. to = `<${tM}.${tm}.${+tp + 1}-0`
  414. } else {
  415. to = `<=${to}`
  416. }
  417. return (`${from} ${to}`).trim()
  418. }
  419. const testSet = (set, version, options) => {
  420. for (let i = 0; i < set.length; i++) {
  421. if (!set[i].test(version)) {
  422. return false
  423. }
  424. }
  425. if (version.prerelease.length && !options.includePrerelease) {
  426. // Find the set of versions that are allowed to have prereleases
  427. // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
  428. // That should allow `1.2.3-pr.2` to pass.
  429. // However, `1.2.4-alpha.notready` should NOT be allowed,
  430. // even though it's within the range set by the comparators.
  431. for (let i = 0; i < set.length; i++) {
  432. debug(set[i].semver)
  433. if (set[i].semver === Comparator.ANY) {
  434. continue
  435. }
  436. if (set[i].semver.prerelease.length > 0) {
  437. const allowed = set[i].semver
  438. if (allowed.major === version.major &&
  439. allowed.minor === version.minor &&
  440. allowed.patch === version.patch) {
  441. return true
  442. }
  443. }
  444. }
  445. // Version has a -pre, but it's not one of the ones we like.
  446. return false
  447. }
  448. return true
  449. }