range.js 14 KB

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