range.js 15 KB

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