range.js 15 KB

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