semver.js 15 KB

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