range.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. const replaceTildes = (comp, options) =>
  221. comp.trim().split(/\s+/).map((c) => {
  222. return replaceTilde(c, options)
  223. }).join(' ')
  224. const replaceTilde = (comp, options) => {
  225. const r = options.loose ? re[t.TILDELOOSE] : re[t.TILDE]
  226. return comp.replace(r, (_, M, m, p, pr) => {
  227. debug('tilde', comp, _, M, m, p, pr)
  228. let ret
  229. if (isX(M)) {
  230. ret = ''
  231. } else if (isX(m)) {
  232. ret = `>=${M}.0.0 <${+M + 1}.0.0-0`
  233. } else if (isX(p)) {
  234. // ~1.2 == >=1.2.0 <1.3.0-0
  235. ret = `>=${M}.${m}.0 <${M}.${+m + 1}.0-0`
  236. } else if (pr) {
  237. debug('replaceTilde pr', pr)
  238. ret = `>=${M}.${m}.${p}-${pr
  239. } <${M}.${+m + 1}.0-0`
  240. } else {
  241. // ~1.2.3 == >=1.2.3 <1.3.0-0
  242. ret = `>=${M}.${m}.${p
  243. } <${M}.${+m + 1}.0-0`
  244. }
  245. debug('tilde return', ret)
  246. return ret
  247. })
  248. }
  249. // ^ --> * (any, kinda silly)
  250. // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0-0
  251. // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0-0
  252. // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0-0
  253. // ^1.2.3 --> >=1.2.3 <2.0.0-0
  254. // ^1.2.0 --> >=1.2.0 <2.0.0-0
  255. const replaceCarets = (comp, options) =>
  256. comp.trim().split(/\s+/).map((c) => {
  257. return replaceCaret(c, options)
  258. }).join(' ')
  259. const replaceCaret = (comp, options) => {
  260. debug('caret', comp, options)
  261. const r = options.loose ? re[t.CARETLOOSE] : re[t.CARET]
  262. const z = options.includePrerelease ? '-0' : ''
  263. return comp.replace(r, (_, M, m, p, pr) => {
  264. debug('caret', comp, _, M, m, p, pr)
  265. let ret
  266. if (isX(M)) {
  267. ret = ''
  268. } else if (isX(m)) {
  269. ret = `>=${M}.0.0${z} <${+M + 1}.0.0-0`
  270. } else if (isX(p)) {
  271. if (M === '0') {
  272. ret = `>=${M}.${m}.0${z} <${M}.${+m + 1}.0-0`
  273. } else {
  274. ret = `>=${M}.${m}.0${z} <${+M + 1}.0.0-0`
  275. }
  276. } else if (pr) {
  277. debug('replaceCaret pr', pr)
  278. if (M === '0') {
  279. if (m === '0') {
  280. ret = `>=${M}.${m}.${p}-${pr
  281. } <${M}.${m}.${+p + 1}-0`
  282. } else {
  283. ret = `>=${M}.${m}.${p}-${pr
  284. } <${M}.${+m + 1}.0-0`
  285. }
  286. } else {
  287. ret = `>=${M}.${m}.${p}-${pr
  288. } <${+M + 1}.0.0-0`
  289. }
  290. } else {
  291. debug('no pr')
  292. if (M === '0') {
  293. if (m === '0') {
  294. ret = `>=${M}.${m}.${p
  295. }${z} <${M}.${m}.${+p + 1}-0`
  296. } else {
  297. ret = `>=${M}.${m}.${p
  298. }${z} <${M}.${+m + 1}.0-0`
  299. }
  300. } else {
  301. ret = `>=${M}.${m}.${p
  302. } <${+M + 1}.0.0-0`
  303. }
  304. }
  305. debug('caret return', ret)
  306. return ret
  307. })
  308. }
  309. const replaceXRanges = (comp, options) => {
  310. debug('replaceXRanges', comp, options)
  311. return comp.split(/\s+/).map((c) => {
  312. return replaceXRange(c, options)
  313. }).join(' ')
  314. }
  315. const replaceXRange = (comp, options) => {
  316. comp = comp.trim()
  317. const r = options.loose ? re[t.XRANGELOOSE] : re[t.XRANGE]
  318. return comp.replace(r, (ret, gtlt, M, m, p, pr) => {
  319. debug('xRange', comp, ret, gtlt, M, m, p, pr)
  320. const xM = isX(M)
  321. const xm = xM || isX(m)
  322. const xp = xm || isX(p)
  323. const anyX = xp
  324. if (gtlt === '=' && anyX) {
  325. gtlt = ''
  326. }
  327. // if we're including prereleases in the match, then we need
  328. // to fix this to -0, the lowest possible prerelease value
  329. pr = options.includePrerelease ? '-0' : ''
  330. if (xM) {
  331. if (gtlt === '>' || gtlt === '<') {
  332. // nothing is allowed
  333. ret = '<0.0.0-0'
  334. } else {
  335. // nothing is forbidden
  336. ret = '*'
  337. }
  338. } else if (gtlt && anyX) {
  339. // we know patch is an x, because we have any x at all.
  340. // replace X with 0
  341. if (xm) {
  342. m = 0
  343. }
  344. p = 0
  345. if (gtlt === '>') {
  346. // >1 => >=2.0.0
  347. // >1.2 => >=1.3.0
  348. gtlt = '>='
  349. if (xm) {
  350. M = +M + 1
  351. m = 0
  352. p = 0
  353. } else {
  354. m = +m + 1
  355. p = 0
  356. }
  357. } else if (gtlt === '<=') {
  358. // <=0.7.x is actually <0.8.0, since any 0.7.x should
  359. // pass. Similarly, <=7.x is actually <8.0.0, etc.
  360. gtlt = '<'
  361. if (xm) {
  362. M = +M + 1
  363. } else {
  364. m = +m + 1
  365. }
  366. }
  367. if (gtlt === '<') {
  368. pr = '-0'
  369. }
  370. ret = `${gtlt + M}.${m}.${p}${pr}`
  371. } else if (xm) {
  372. ret = `>=${M}.0.0${pr} <${+M + 1}.0.0-0`
  373. } else if (xp) {
  374. ret = `>=${M}.${m}.0${pr
  375. } <${M}.${+m + 1}.0-0`
  376. }
  377. debug('xRange return', ret)
  378. return ret
  379. })
  380. }
  381. // Because * is AND-ed with everything else in the comparator,
  382. // and '' means "any version", just remove the *s entirely.
  383. const replaceStars = (comp, options) => {
  384. debug('replaceStars', comp, options)
  385. // Looseness is ignored here. star is always as loose as it gets!
  386. return comp.trim().replace(re[t.STAR], '')
  387. }
  388. const replaceGTE0 = (comp, options) => {
  389. debug('replaceGTE0', comp, options)
  390. return comp.trim()
  391. .replace(re[options.includePrerelease ? t.GTE0PRE : t.GTE0], '')
  392. }
  393. // This function is passed to string.replace(re[t.HYPHENRANGE])
  394. // M, m, patch, prerelease, build
  395. // 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
  396. // 1.2.3 - 3.4 => >=1.2.0 <3.5.0-0 Any 3.4.x will do
  397. // 1.2 - 3.4 => >=1.2.0 <3.5.0-0
  398. const hyphenReplace = incPr => ($0,
  399. from, fM, fm, fp, fpr, fb,
  400. to, tM, tm, tp, tpr, tb) => {
  401. if (isX(fM)) {
  402. from = ''
  403. } else if (isX(fm)) {
  404. from = `>=${fM}.0.0${incPr ? '-0' : ''}`
  405. } else if (isX(fp)) {
  406. from = `>=${fM}.${fm}.0${incPr ? '-0' : ''}`
  407. } else if (fpr) {
  408. from = `>=${from}`
  409. } else {
  410. from = `>=${from}${incPr ? '-0' : ''}`
  411. }
  412. if (isX(tM)) {
  413. to = ''
  414. } else if (isX(tm)) {
  415. to = `<${+tM + 1}.0.0-0`
  416. } else if (isX(tp)) {
  417. to = `<${tM}.${+tm + 1}.0-0`
  418. } else if (tpr) {
  419. to = `<=${tM}.${tm}.${tp}-${tpr}`
  420. } else if (incPr) {
  421. to = `<${tM}.${tm}.${+tp + 1}-0`
  422. } else {
  423. to = `<=${to}`
  424. }
  425. return (`${from} ${to}`).trim()
  426. }
  427. const testSet = (set, version, options) => {
  428. for (let i = 0; i < set.length; i++) {
  429. if (!set[i].test(version)) {
  430. return false
  431. }
  432. }
  433. if (version.prerelease.length && !options.includePrerelease) {
  434. // Find the set of versions that are allowed to have prereleases
  435. // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
  436. // That should allow `1.2.3-pr.2` to pass.
  437. // However, `1.2.4-alpha.notready` should NOT be allowed,
  438. // even though it's within the range set by the comparators.
  439. for (let i = 0; i < set.length; i++) {
  440. debug(set[i].semver)
  441. if (set[i].semver === Comparator.ANY) {
  442. continue
  443. }
  444. if (set[i].semver.prerelease.length > 0) {
  445. const allowed = set[i].semver
  446. if (allowed.major === version.major &&
  447. allowed.minor === version.minor &&
  448. allowed.patch === version.patch) {
  449. return true
  450. }
  451. }
  452. }
  453. // Version has a -pre, but it's not one of the ones we like.
  454. return false
  455. }
  456. return true
  457. }