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