semver.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  7. /** @typedef {string | number} VersionValue */
  8. /** @typedef {VersionValue | undefined} SemVerRangeItem */
  9. /** @typedef {(SemVerRangeItem | SemVerRangeItem[])[]} SemVerRange */
  10. /**
  11. * Returns parsed version.
  12. * @param {string} str version string
  13. * @returns {SemVerRange} parsed version
  14. */
  15. const parseVersion = (str) => {
  16. /**
  17. * Returns result.
  18. * @param {str} str str
  19. * @returns {VersionValue[]} result
  20. */
  21. var splitAndConvert = function (str) {
  22. return str.split(".").map(function (item) {
  23. // eslint-disable-next-line eqeqeq
  24. return +item == /** @type {string | number} */ (item) ? +item : item;
  25. });
  26. };
  27. var match =
  28. /** @type {RegExpExecArray} */
  29. (/^([^-+]+)?(?:-([^+]+))?(?:\+(.+))?$/.exec(str));
  30. /** @type {(VersionValue | undefined | [])[]} */
  31. var ver = match[1] ? splitAndConvert(match[1]) : [];
  32. if (match[2]) {
  33. ver.length++;
  34. ver.push.apply(ver, splitAndConvert(match[2]));
  35. }
  36. if (match[3]) {
  37. ver.push([]);
  38. ver.push.apply(ver, splitAndConvert(match[3]));
  39. }
  40. return ver;
  41. };
  42. module.exports.parseVersion = parseVersion;
  43. /* eslint-disable eqeqeq */
  44. /**
  45. * Returns true, iff a < b.
  46. * @param {string} a version
  47. * @param {string} b version
  48. * @returns {boolean} true, iff a < b
  49. */
  50. const versionLt = (a, b) => {
  51. // @ts-expect-error
  52. a = parseVersion(a);
  53. // @ts-expect-error
  54. b = parseVersion(b);
  55. var i = 0;
  56. for (;;) {
  57. // a b EOA object undefined number string
  58. // EOA a == b a < b b < a a < b a < b
  59. // object b < a (0) b < a a < b a < b
  60. // undefined a < b a < b (0) a < b a < b
  61. // number b < a b < a b < a (1) a < b
  62. // string b < a b < a b < a b < a (1)
  63. // EOA end of array
  64. // (0) continue on
  65. // (1) compare them via "<"
  66. // Handles first row in table
  67. if (i >= a.length) return i < b.length && (typeof b[i])[0] != "u";
  68. var aValue = a[i];
  69. var aType = (typeof aValue)[0];
  70. // Handles first column in table
  71. if (i >= b.length) return aType == "u";
  72. var bValue = b[i];
  73. var bType = (typeof bValue)[0];
  74. if (aType == bType) {
  75. if (aType != "o" && aType != "u" && aValue != bValue) {
  76. return aValue < bValue;
  77. }
  78. i++;
  79. } else {
  80. // Handles remaining cases
  81. if (aType == "o" && bType == "n") return true;
  82. return bType == "s" || aType == "u";
  83. }
  84. }
  85. };
  86. /* eslint-enable eqeqeq */
  87. module.exports.versionLt = versionLt;
  88. /**
  89. * Returns parsed range.
  90. * @param {string} str range string
  91. * @returns {SemVerRange} parsed range
  92. */
  93. module.exports.parseRange = (str) => {
  94. /**
  95. * Returns result.
  96. * @param {string} str str
  97. * @returns {VersionValue[]} result
  98. */
  99. const splitAndConvert = (str) => {
  100. return str
  101. .split(".")
  102. .map((item) => (item !== "NaN" && `${+item}` === item ? +item : item));
  103. };
  104. // see https://docs.npmjs.com/misc/semver#range-grammar for grammar
  105. /**
  106. * Returns the sem ver range item.
  107. * @param {string} str str
  108. * @returns {SemVerRangeItem[]}
  109. */
  110. const parsePartial = (str) => {
  111. const match =
  112. /** @type {RegExpExecArray} */
  113. (/^([^-+]+)?(?:-([^+]+))?(?:\+(.+))?$/.exec(str));
  114. /** @type {SemVerRangeItem[]} */
  115. const ver = match[1] ? [0, ...splitAndConvert(match[1])] : [0];
  116. if (match[2]) {
  117. ver.length++;
  118. ver.push.apply(ver, splitAndConvert(match[2]));
  119. }
  120. // remove trailing any matchers
  121. let last = ver[ver.length - 1];
  122. while (
  123. ver.length &&
  124. (last === undefined || /^[*xX]$/.test(/** @type {string} */ (last)))
  125. ) {
  126. ver.pop();
  127. last = ver[ver.length - 1];
  128. }
  129. return ver;
  130. };
  131. /**
  132. * Returns the sem ver range item.
  133. *
  134. * @param {SemVerRangeItem[]} range range
  135. * @returns {SemVerRangeItem[]}
  136. */
  137. const toFixed = (range) => {
  138. if (range.length === 1) {
  139. // Special case for "*" is "x.x.x" instead of "="
  140. return [0];
  141. } else if (range.length === 2) {
  142. // Special case for "1" is "1.x.x" instead of "=1"
  143. return [1, ...range.slice(1)];
  144. } else if (range.length === 3) {
  145. // Special case for "1.2" is "1.2.x" instead of "=1.2"
  146. return [2, ...range.slice(1)];
  147. }
  148. return [range.length, ...range.slice(1)];
  149. };
  150. /**
  151. * Returns result.
  152. *
  153. * @param {SemVerRangeItem[]} range
  154. * @returns {SemVerRangeItem[]} result
  155. */
  156. const negate = (range) => {
  157. return [-(/** @type { [number]} */ (range)[0]) - 1, ...range.slice(1)];
  158. };
  159. /**
  160. * Returns the sem ver range.
  161. * @param {string} str str
  162. * @returns {SemVerRange}
  163. */
  164. const parseSimple = (str) => {
  165. // simple ::= primitive | partial | tilde | caret
  166. // primitive ::= ( '<' | '>' | '>=' | '<=' | '=' | '!' ) ( ' ' ) * partial
  167. // tilde ::= '~' ( ' ' ) * partial
  168. // caret ::= '^' ( ' ' ) * partial
  169. const match = /^(\^|~|<=|<|>=|>|=|v|!)/.exec(str);
  170. const start = match ? match[0] : "";
  171. const remainder = parsePartial(
  172. start.length ? str.slice(start.length).trim() : str.trim()
  173. );
  174. switch (start) {
  175. case "^":
  176. if (remainder.length > 1 && remainder[1] === 0) {
  177. if (remainder.length > 2 && remainder[2] === 0) {
  178. return [3, ...remainder.slice(1)];
  179. }
  180. return [2, ...remainder.slice(1)];
  181. }
  182. return [1, ...remainder.slice(1)];
  183. case "~":
  184. if (remainder.length === 2 && remainder[0] === 0) {
  185. return [1, ...remainder.slice(1)];
  186. }
  187. return [2, ...remainder.slice(1)];
  188. case ">=":
  189. return remainder;
  190. case "=":
  191. case "v":
  192. case "":
  193. return toFixed(remainder);
  194. case "<":
  195. return negate(remainder);
  196. case ">": {
  197. // and( >=, not( = ) ) => >=, =, not, and
  198. const fixed = toFixed(remainder);
  199. // eslint-disable-next-line no-sparse-arrays
  200. return [, fixed, 0, remainder, 2];
  201. }
  202. case "<=":
  203. // or( <, = ) => <, =, or
  204. // eslint-disable-next-line no-sparse-arrays
  205. return [, toFixed(remainder), negate(remainder), 1];
  206. case "!": {
  207. // not =
  208. const fixed = toFixed(remainder);
  209. // eslint-disable-next-line no-sparse-arrays
  210. return [, fixed, 0];
  211. }
  212. default:
  213. throw new Error("Unexpected start value");
  214. }
  215. };
  216. /**
  217. * Returns result.
  218. *
  219. * @param {SemVerRangeItem[][]} items items
  220. * @param {number} fn fn
  221. * @returns {SemVerRange} result
  222. */
  223. const combine = (items, fn) => {
  224. if (items.length === 1) return items[0];
  225. const arr = [];
  226. for (const item of items.slice().reverse()) {
  227. if (0 in item) {
  228. arr.push(item);
  229. } else {
  230. arr.push(...item.slice(1));
  231. }
  232. }
  233. // eslint-disable-next-line no-sparse-arrays
  234. return [, ...arr, ...items.slice(1).map(() => fn)];
  235. };
  236. /**
  237. * Returns the sem ver range.
  238. * @param {string} str str
  239. * @returns {SemVerRange}
  240. */
  241. const parseRange = (str) => {
  242. // range ::= hyphen | simple ( ' ' ( ' ' ) * simple ) * | ''
  243. // hyphen ::= partial ( ' ' ) * ' - ' ( ' ' ) * partial
  244. const items = str.split(/\s+-\s+/);
  245. if (items.length === 1) {
  246. str = str.trim();
  247. /** @type {SemVerRangeItem[][]} */
  248. const items = [];
  249. const r = /[-0-9A-Za-z]\s+/g;
  250. var start = 0;
  251. /** @type {RegExpExecArray | null} */
  252. var match;
  253. while ((match = r.exec(str))) {
  254. const end = match.index + 1;
  255. items.push(
  256. /** @type {SemVerRangeItem[]} */
  257. (parseSimple(str.slice(start, end).trim()))
  258. );
  259. start = end;
  260. }
  261. items.push(
  262. /** @type {SemVerRangeItem[]} */
  263. (parseSimple(str.slice(start).trim()))
  264. );
  265. return combine(items, 2);
  266. }
  267. const a = parsePartial(items[0]);
  268. const b = parsePartial(items[1]);
  269. // >=a <=b => and( >=a, or( <b, =b ) ) => >=a, <b, =b, or, and
  270. // eslint-disable-next-line no-sparse-arrays
  271. return [, toFixed(b), negate(b), 1, a, 2];
  272. };
  273. /**
  274. * Returns the sem ver range.
  275. * @param {string} str str
  276. * @returns {SemVerRange}
  277. */
  278. const parseLogicalOr = (str) => {
  279. // range-set ::= range ( logical-or range ) *
  280. // logical-or ::= ( ' ' ) * '||' ( ' ' ) *
  281. const items =
  282. /** @type {SemVerRangeItem[][]} */
  283. (str.split(/\s*\|\|\s*/).map(parseRange));
  284. return combine(items, 1);
  285. };
  286. return parseLogicalOr(str);
  287. };
  288. /* eslint-disable eqeqeq */
  289. /**
  290. * Returns the string.
  291. * @param {SemVerRange} range
  292. * @returns {string}
  293. */
  294. const rangeToString = (range) => {
  295. var fixCount = /** @type {number} */ (range[0]);
  296. var str = "";
  297. if (range.length === 1) {
  298. return "*";
  299. } else if (fixCount + 0.5) {
  300. str +=
  301. fixCount == 0
  302. ? ">="
  303. : fixCount == -1
  304. ? "<"
  305. : fixCount == 1
  306. ? "^"
  307. : fixCount == 2
  308. ? "~"
  309. : fixCount > 0
  310. ? "="
  311. : "!=";
  312. var needDot = 1;
  313. for (var i = 1; i < range.length; i++) {
  314. var item = range[i];
  315. var t = (typeof item)[0];
  316. needDot--;
  317. str +=
  318. t == "u"
  319. ? // undefined: prerelease marker, add an "-"
  320. "-"
  321. : // number or string: add the item, set flag to add an "." between two of them
  322. (needDot > 0 ? "." : "") + ((needDot = 2), item);
  323. }
  324. return str;
  325. }
  326. /** @type {string[]} */
  327. var stack = [];
  328. // eslint-disable-next-line no-redeclare
  329. for (var i = 1; i < range.length; i++) {
  330. // eslint-disable-next-line no-redeclare
  331. var item = range[i];
  332. stack.push(
  333. item === 0
  334. ? "not(" + pop() + ")"
  335. : item === 1
  336. ? "(" + pop() + " || " + pop() + ")"
  337. : item === 2
  338. ? stack.pop() + " " + stack.pop()
  339. : rangeToString(/** @type {SemVerRange} */ (item))
  340. );
  341. }
  342. return pop();
  343. function pop() {
  344. return /** @type {string} */ (stack.pop()).replace(/^\((.+)\)$/, "$1");
  345. }
  346. };
  347. module.exports.rangeToString = rangeToString;
  348. /**
  349. * Returns if version satisfy the range.
  350. * @param {SemVerRange} range version range
  351. * @param {string} version the version
  352. * @returns {boolean} if version satisfy the range
  353. */
  354. const satisfy = (range, version) => {
  355. if (0 in range) {
  356. // @ts-expect-error
  357. version = parseVersion(version);
  358. var fixCount = /** @type {number} */ (range[0]);
  359. // when negated is set it swill set for < instead of >=
  360. var negated = fixCount < 0;
  361. if (negated) fixCount = -fixCount - 1;
  362. for (var i = 0, j = 1, isEqual = true; ; j++, i++) {
  363. // cspell:word nequal nequ
  364. // when isEqual = true:
  365. // range version: EOA/object undefined number string
  366. // EOA equal block big-ver big-ver
  367. // undefined bigger next big-ver big-ver
  368. // number smaller block cmp big-cmp
  369. // fixed number smaller block cmp-fix differ
  370. // string smaller block differ cmp
  371. // fixed string smaller block small-cmp cmp-fix
  372. // when isEqual = false:
  373. // range version: EOA/object undefined number string
  374. // EOA nequal block next-ver next-ver
  375. // undefined nequal block next-ver next-ver
  376. // number nequal block next next
  377. // fixed number nequal block next next (this never happens)
  378. // string nequal block next next
  379. // fixed string nequal block next next (this never happens)
  380. // EOA end of array
  381. // equal (version is equal range):
  382. // when !negated: return true,
  383. // when negated: return false
  384. // bigger (version is bigger as range):
  385. // when fixed: return false,
  386. // when !negated: return true,
  387. // when negated: return false,
  388. // smaller (version is smaller as range):
  389. // when !negated: return false,
  390. // when negated: return true
  391. // nequal (version is not equal range (> resp <)): return true
  392. // block (version is in different prerelease area): return false
  393. // differ (version is different from fixed range (string vs. number)): return false
  394. // next: continues to the next items
  395. // next-ver: when fixed: return false, continues to the next item only for the version, sets isEqual=false
  396. // big-ver: when fixed || negated: return false, continues to the next item only for the version, sets isEqual=false
  397. // next-nequ: continues to the next items, sets isEqual=false
  398. // cmp (negated === false): version < range => return false, version > range => next-nequ, else => next
  399. // cmp (negated === true): version > range => return false, version < range => next-nequ, else => next
  400. // cmp-fix: version == range => next, else => return false
  401. // big-cmp: when negated => return false, else => next-nequ
  402. // small-cmp: when negated => next-nequ, else => return false
  403. var rangeType =
  404. /** @type {"s" | "n" | "u" | ""} */
  405. (j < range.length ? (typeof range[j])[0] : "");
  406. /** @type {VersionValue | undefined} */
  407. var versionValue;
  408. /** @type {"n" | "s" | "u" | "o" | undefined} */
  409. var versionType;
  410. // Handles first column in both tables (end of version or object)
  411. if (
  412. i >= version.length ||
  413. ((versionValue = version[i]),
  414. (versionType = /** @type {"n" | "s" | "u" | "o"} */ (
  415. (typeof versionValue)[0]
  416. )) == "o")
  417. ) {
  418. // Handles nequal
  419. if (!isEqual) return true;
  420. // Handles bigger
  421. if (rangeType == "u") return j > fixCount && !negated;
  422. // Handles equal and smaller: (range === EOA) XOR negated
  423. return (rangeType == "") != negated; // equal + smaller
  424. }
  425. // Handles second column in both tables (version = undefined)
  426. if (versionType == "u") {
  427. if (!isEqual || rangeType != "u") {
  428. return false;
  429. }
  430. }
  431. // switch between first and second table
  432. else if (isEqual) {
  433. // Handle diagonal
  434. if (rangeType == versionType) {
  435. if (j <= fixCount) {
  436. // Handles "cmp-fix" cases
  437. if (versionValue != range[j]) {
  438. return false;
  439. }
  440. } else {
  441. // Handles "cmp" cases
  442. if (
  443. negated
  444. ? versionValue > /** @type {VersionValue[]} */ (range)[j]
  445. : versionValue < /** @type {VersionValue[]} */ (range)[j]
  446. ) {
  447. return false;
  448. }
  449. if (versionValue != range[j]) isEqual = false;
  450. }
  451. }
  452. // Handle big-ver
  453. else if (rangeType != "s" && rangeType != "n") {
  454. if (negated || j <= fixCount) return false;
  455. isEqual = false;
  456. j--;
  457. }
  458. // Handle differ, big-cmp and small-cmp
  459. else if (j <= fixCount || versionType < rangeType != negated) {
  460. return false;
  461. } else {
  462. isEqual = false;
  463. }
  464. } else {
  465. // Handles all "next-ver" cases in the second table
  466. // eslint-disable-next-line no-lonely-if
  467. if (rangeType != "s" && rangeType != "n") {
  468. isEqual = false;
  469. j--;
  470. }
  471. // next is applied by default
  472. }
  473. }
  474. }
  475. /** @type {(boolean | number)[]} */
  476. var stack = [];
  477. var p = stack.pop.bind(stack);
  478. // eslint-disable-next-line no-redeclare
  479. for (var i = 1; i < range.length; i++) {
  480. var item = /** @type {SemVerRangeItem[] | 0 | 1 | 2} */ (range[i]);
  481. stack.push(
  482. item == 1
  483. ? /** @type {() => number} */ (p)() | /** @type {() => number} */ (p)()
  484. : item == 2
  485. ? /** @type {() => number} */ (p)() &
  486. /** @type {() => number} */ (p)()
  487. : item
  488. ? satisfy(item, version)
  489. : !p()
  490. );
  491. }
  492. return !!p();
  493. };
  494. /* eslint-enable eqeqeq */
  495. module.exports.satisfy = satisfy;
  496. /**
  497. * Returns the string.
  498. * @param {SemVerRange | string | number | false | undefined} json
  499. * @returns {string}
  500. */
  501. module.exports.stringifyHoley = (json) => {
  502. switch (typeof json) {
  503. case "undefined":
  504. return "";
  505. case "object":
  506. if (Array.isArray(json)) {
  507. let str = "[";
  508. for (let i = 0; i < json.length; i++) {
  509. if (i !== 0) str += ",";
  510. str += this.stringifyHoley(json[i]);
  511. }
  512. str += "]";
  513. return str;
  514. }
  515. return JSON.stringify(json);
  516. default:
  517. return JSON.stringify(json);
  518. }
  519. };
  520. //#region runtime code: parseVersion
  521. /**
  522. * @param {RuntimeTemplate} runtimeTemplate
  523. * @returns {string}
  524. */
  525. exports.parseVersionRuntimeCode = (runtimeTemplate) =>
  526. `var parseVersion = ${runtimeTemplate.basicFunction("str", [
  527. "// see webpack/lib/util/semver.js for original code",
  528. `var p=${runtimeTemplate.supportsArrowFunction() ? "p=>" : "function(p)"}{return p.split(".").map(${runtimeTemplate.supportsArrowFunction() ? "p=>" : "function(p)"}{return+p==p?+p:p})},n=/^([^-+]+)?(?:-([^+]+))?(?:\\+(.+))?$/.exec(str),r=n[1]?p(n[1]):[];return n[2]&&(r.length++,r.push.apply(r,p(n[2]))),n[3]&&(r.push([]),r.push.apply(r,p(n[3]))),r;`
  529. ])}`;
  530. //#endregion
  531. //#region runtime code: versionLt
  532. /**
  533. * @param {RuntimeTemplate} runtimeTemplate
  534. * @returns {string}
  535. */
  536. exports.versionLtRuntimeCode = (runtimeTemplate) =>
  537. `var versionLt = ${runtimeTemplate.basicFunction("a, b", [
  538. "// see webpack/lib/util/semver.js for original code",
  539. 'a=parseVersion(a),b=parseVersion(b);for(var r=0;;){if(r>=a.length)return r<b.length&&"u"!=(typeof b[r])[0];var e=a[r],n=(typeof e)[0];if(r>=b.length)return"u"==n;var t=b[r],f=(typeof t)[0];if(n!=f)return"o"==n&&"n"==f||("s"==f||"u"==n);if("o"!=n&&"u"!=n&&e!=t)return e<t;r++}'
  540. ])}`;
  541. //#endregion
  542. //#region runtime code: rangeToString
  543. /**
  544. * @param {RuntimeTemplate} runtimeTemplate
  545. * @returns {string}
  546. */
  547. exports.rangeToStringRuntimeCode = (runtimeTemplate) =>
  548. `var rangeToString = ${runtimeTemplate.basicFunction("range", [
  549. "// see webpack/lib/util/semver.js for original code",
  550. 'var r=range[0],n="";if(1===range.length)return"*";if(r+.5){n+=0==r?">=":-1==r?"<":1==r?"^":2==r?"~":r>0?"=":"!=";for(var e=1,a=1;a<range.length;a++){e--,n+="u"==(typeof(t=range[a]))[0]?"-":(e>0?".":"")+(e=2,t)}return n}var g=[];for(a=1;a<range.length;a++){var t=range[a];g.push(0===t?"not("+o()+")":1===t?"("+o()+" || "+o()+")":2===t?g.pop()+" "+g.pop():rangeToString(t))}return o();function o(){return g.pop().replace(/^\\((.+)\\)$/,"$1")}'
  551. ])}`;
  552. //#endregion
  553. //#region runtime code: satisfy
  554. /**
  555. * @param {RuntimeTemplate} runtimeTemplate
  556. * @returns {string}
  557. */
  558. exports.satisfyRuntimeCode = (runtimeTemplate) =>
  559. `var satisfy = ${runtimeTemplate.basicFunction("range, version", [
  560. "// see webpack/lib/util/semver.js for original code",
  561. 'if(0 in range){version=parseVersion(version);var e=range[0],r=e<0;r&&(e=-e-1);for(var n=0,i=1,a=!0;;i++,n++){var f,s,g=i<range.length?(typeof range[i])[0]:"";if(n>=version.length||"o"==(s=(typeof(f=version[n]))[0]))return!a||("u"==g?i>e&&!r:""==g!=r);if("u"==s){if(!a||"u"!=g)return!1}else if(a)if(g==s)if(i<=e){if(f!=range[i])return!1}else{if(r?f>range[i]:f<range[i])return!1;f!=range[i]&&(a=!1)}else if("s"!=g&&"n"!=g){if(r||i<=e)return!1;a=!1,i--}else{if(i<=e||s<g!=r)return!1;a=!1}else"s"!=g&&"n"!=g&&(a=!1,i--)}}var t=[],o=t.pop.bind(t);for(n=1;n<range.length;n++){var u=range[n];t.push(1==u?o()|o():2==u?o()&o():u?satisfy(u,version):!o())}return!!o();'
  562. ])}`;
  563. //#endregion