Open History to see the last commit
1interface ParsedSemver {
2 readonly major: number;
3 readonly minor: number;
4 readonly patch: number;
5 readonly prerelease: ReadonlyArray<string>;
6}
7
8const SEMVER_NUMBER_SEGMENT = /^\d+$/;
9
10export function normalizeSemverVersion(version: string): string {
11 const [main, prerelease] = version.trim().split("-", 2);
12 const segments = (main ?? "")
13 .split(".")
14 .map((segment) => segment.trim())
15 .filter((segment) => segment.length > 0);
16
17 if (segments.length === 2) {
18 segments.push("0");
19 }
20
21 return prerelease ? `${segments.join(".")}-${prerelease}` : segments.join(".");
22}
23
24export function parseSemver(value: string): ParsedSemver | null {
25 const normalized = normalizeSemverVersion(value).replace(/^v/, "");
26 const [main = "", prerelease] = normalized.split("-", 2);
27 const segments = main.split(".");
28 if (segments.length !== 3) {
29 return null;
30 }
31
32 const [majorSegment, minorSegment, patchSegment] = segments;
33 if (majorSegment === undefined || minorSegment === undefined || patchSegment === undefined) {
34 return null;
35 }
36 if (
37 !SEMVER_NUMBER_SEGMENT.test(majorSegment) ||
38 !SEMVER_NUMBER_SEGMENT.test(minorSegment) ||
39 !SEMVER_NUMBER_SEGMENT.test(patchSegment)
40 ) {
41 return null;
42 }
43
44 const major = Number.parseInt(majorSegment, 10);
45 const minor = Number.parseInt(minorSegment, 10);
46 const patch = Number.parseInt(patchSegment, 10);
47 if (![major, minor, patch].every(Number.isInteger)) {
48 return null;
49 }
50
51 return {
52 major,
53 minor,
54 patch,
55 prerelease:
56 prerelease
57 ?.split(".")
58 .map((segment) => segment.trim())
59 .filter((segment) => segment.length > 0) ?? [],
60 };
61}
62
63function comparePrereleaseIdentifier(left: string, right: string): number {
64 const leftNumeric = SEMVER_NUMBER_SEGMENT.test(left);
65 const rightNumeric = SEMVER_NUMBER_SEGMENT.test(right);
66
67 if (leftNumeric && rightNumeric) {
68 return Number.parseInt(left, 10) - Number.parseInt(right, 10);
69 }
70 if (leftNumeric) {
71 return -1;
72 }
73 if (rightNumeric) {
74 return 1;
75 }
76 return left.localeCompare(right);
77}
78
79export function compareSemverVersions(left: string, right: string): number {
80 const parsedLeft = parseSemver(left);
81 const parsedRight = parseSemver(right);
82 if (!parsedLeft || !parsedRight) {
83 return left.localeCompare(right);
84 }
85
86 if (parsedLeft.major !== parsedRight.major) {
87 return parsedLeft.major - parsedRight.major;
88 }
89 if (parsedLeft.minor !== parsedRight.minor) {
90 return parsedLeft.minor - parsedRight.minor;
91 }
92 if (parsedLeft.patch !== parsedRight.patch) {
93 return parsedLeft.patch - parsedRight.patch;
94 }
95
96 if (parsedLeft.prerelease.length === 0 && parsedRight.prerelease.length === 0) {
97 return 0;
98 }
99 if (parsedLeft.prerelease.length === 0) {
100 return 1;
101 }
102 if (parsedRight.prerelease.length === 0) {
103 return -1;
104 }
105
106 const length = Math.max(parsedLeft.prerelease.length, parsedRight.prerelease.length);
107 for (let index = 0; index < length; index += 1) {
108 const leftIdentifier = parsedLeft.prerelease[index];
109 const rightIdentifier = parsedRight.prerelease[index];
110 if (leftIdentifier === undefined) {
111 return -1;
112 }
113 if (rightIdentifier === undefined) {
114 return 1;
115 }
116 const comparison = comparePrereleaseIdentifier(leftIdentifier, rightIdentifier);
117 if (comparison !== 0) {
118 return comparison;
119 }
120 }
121
122 return 0;
123}
124
125/**
126 * Small semver range checker for CLI/runtime gates.
127 *
128 * Keep the function body valid plain JavaScript: SSH startup stringifies this
129 * function and runs it on remote Node versions before TypeScript support is known.
130 *
131 * @param rawVersion Version string, with or without a leading `v`.
132 * @param range Space-separated comparators, with `||` range groups.
133 * @returns Whether `rawVersion` satisfies the supported range syntax.
134 */
135export const satisfiesSemverRange: (rawVersion: string, range: string) => boolean =
136 function satisfiesSemverRange(rawVersion, range) {
137 const normalizedVersion = String(rawVersion).trim().replace(/^v/, "");
138 const versionMatch = normalizedVersion.match(
139 /^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-[0-9A-Za-z.-]+)?$/,
140 );
141 if (!versionMatch) {
142 return false;
143 }
144
145 const version = {
146 major: Number(versionMatch[1]),
147 minor: Number(versionMatch[2] || 0),
148 patch: Number(versionMatch[3] || 0),
149 };
150
151 return range.split("||").some((group) => {
152 const comparators = group.trim().split(/\s+/).filter(Boolean);
153 if (comparators.length === 0) {
154 return false;
155 }
156 return comparators.every((comparator) => {
157 const match = comparator.trim().match(/^(\^|>=|>|<=|<|=)?\s*v?(\d+(?:\.\d+){0,2})$/);
158 if (!match) {
159 return false;
160 }
161 const targetVersion = match[2];
162 if (targetVersion === undefined) {
163 return false;
164 }
165 const targetMatch = targetVersion.match(/^(\d+)(?:\.(\d+))?(?:\.(\d+))?$/);
166 if (!targetMatch) {
167 return false;
168 }
169 const target = {
170 major: Number(targetMatch[1]),
171 minor: Number(targetMatch[2] || 0),
172 patch: Number(targetMatch[3] || 0),
173 };
174 const compared =
175 version.major !== target.major
176 ? version.major > target.major
177 ? 1
178 : -1
179 : version.minor !== target.minor
180 ? version.minor > target.minor
181 ? 1
182 : -1
183 : version.patch !== target.patch
184 ? version.patch > target.patch
185 ? 1
186 : -1
187 : 0;
188 const operator = match[1] || "=";
189 switch (operator) {
190 case "^":
191 if (compared < 0) {
192 return false;
193 }
194 if (target.major > 0) {
195 return version.major === target.major;
196 }
197 if (target.minor > 0) {
198 return version.major === 0 && version.minor === target.minor;
199 }
200 return version.major === 0 && version.minor === 0 && version.patch === target.patch;
201 case ">=":
202 return compared >= 0;
203 case ">":
204 return compared > 0;
205 case "<=":
206 return compared <= 0;
207 case "<":
208 return compared < 0;
209 case "=":
210 return compared === 0;
211 default:
212 return false;
213 }
214 });
215 });
216 };