The best way to control Claude Code, Codex and more from any computer or phone
| 1 | interface ParsedSemver { |
| 2 | readonly major: number; |
| 3 | readonly minor: number; |
| 4 | readonly patch: number; |
| 5 | readonly prerelease: ReadonlyArray<string>; |
| 6 | } |
| 7 | |
| 8 | const SEMVER_NUMBER_SEGMENT = /^\d+$/; |
| 9 | |
| 10 | export function normalizeSemverVersion(version: string): string { |
| 11 | const [versionWithoutBuildMetadata = ""] = version.trim().split("+", 1); |
| 12 | const [main, ...prereleaseParts] = versionWithoutBuildMetadata.split("-"); |
| 13 | const prerelease = prereleaseParts.join("-"); |
| 14 | const segments: string[] = []; |
| 15 | for (const segment of (main ?? "").split(".")) { |
| 16 | const trimmed = segment.trim(); |
| 17 | if (trimmed.length > 0) { |
| 18 | segments.push(trimmed); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | while (segments.length > 0 && segments.length < 3) { |
| 23 | segments.push("0"); |
| 24 | } |
| 25 | |
| 26 | return prerelease ? `${segments.join(".")}-${prerelease}` : segments.join("."); |
| 27 | } |
| 28 | |
| 29 | export function parseSemver(value: string): ParsedSemver | null { |
| 30 | const normalized = normalizeSemverVersion(value).replace(/^v/, ""); |
| 31 | const [main = "", ...prereleaseParts] = normalized.split("-"); |
| 32 | const prerelease = prereleaseParts.join("-"); |
| 33 | const segments = main.split("."); |
| 34 | if (segments.length !== 3) { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | const [majorSegment, minorSegment, patchSegment] = segments; |
| 39 | if (majorSegment === undefined || minorSegment === undefined || patchSegment === undefined) { |
| 40 | return null; |
| 41 | } |
| 42 | if ( |
| 43 | !SEMVER_NUMBER_SEGMENT.test(majorSegment) || |
| 44 | !SEMVER_NUMBER_SEGMENT.test(minorSegment) || |
| 45 | !SEMVER_NUMBER_SEGMENT.test(patchSegment) |
| 46 | ) { |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | const major = Number.parseInt(majorSegment, 10); |
| 51 | const minor = Number.parseInt(minorSegment, 10); |
| 52 | const patch = Number.parseInt(patchSegment, 10); |
| 53 | if (![major, minor, patch].every(Number.isInteger)) { |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | return { |
| 58 | major, |
| 59 | minor, |
| 60 | patch, |
| 61 | prerelease: parsePrereleaseSegments(prerelease), |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | function parsePrereleaseSegments(prerelease: string | undefined): ReadonlyArray<string> { |
| 66 | if (prerelease === undefined) { |
| 67 | return []; |
| 68 | } |
| 69 | const segments: string[] = []; |
| 70 | for (const segment of prerelease.split(".")) { |
| 71 | const trimmed = segment.trim(); |
| 72 | if (trimmed.length > 0) { |
| 73 | segments.push(trimmed); |
| 74 | } |
| 75 | } |
| 76 | return segments; |
| 77 | } |
| 78 | |
| 79 | function comparePrereleaseIdentifier(left: string, right: string): number { |
| 80 | const leftNumeric = SEMVER_NUMBER_SEGMENT.test(left); |
| 81 | const rightNumeric = SEMVER_NUMBER_SEGMENT.test(right); |
| 82 | |
| 83 | if (leftNumeric && rightNumeric) { |
| 84 | const leftNumber = BigInt(left); |
| 85 | const rightNumber = BigInt(right); |
| 86 | return leftNumber === rightNumber ? 0 : leftNumber < rightNumber ? -1 : 1; |
| 87 | } |
| 88 | if (leftNumeric) { |
| 89 | return -1; |
| 90 | } |
| 91 | if (rightNumeric) { |
| 92 | return 1; |
| 93 | } |
| 94 | return left === right ? 0 : left < right ? -1 : 1; |
| 95 | } |
| 96 | |
| 97 | export function compareSemverVersions(left: string, right: string): number { |
| 98 | const parsedLeft = parseSemver(left); |
| 99 | const parsedRight = parseSemver(right); |
| 100 | if (!parsedLeft || !parsedRight) { |
| 101 | return left.localeCompare(right); |
| 102 | } |
| 103 | |
| 104 | if (parsedLeft.major !== parsedRight.major) { |
| 105 | return parsedLeft.major - parsedRight.major; |
| 106 | } |
| 107 | if (parsedLeft.minor !== parsedRight.minor) { |
| 108 | return parsedLeft.minor - parsedRight.minor; |
| 109 | } |
| 110 | if (parsedLeft.patch !== parsedRight.patch) { |
| 111 | return parsedLeft.patch - parsedRight.patch; |
| 112 | } |
| 113 | |
| 114 | if (parsedLeft.prerelease.length === 0 && parsedRight.prerelease.length === 0) { |
| 115 | return 0; |
| 116 | } |
| 117 | if (parsedLeft.prerelease.length === 0) { |
| 118 | return 1; |
| 119 | } |
| 120 | if (parsedRight.prerelease.length === 0) { |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | const length = Math.max(parsedLeft.prerelease.length, parsedRight.prerelease.length); |
| 125 | for (let index = 0; index < length; index += 1) { |
| 126 | const leftIdentifier = parsedLeft.prerelease[index]; |
| 127 | const rightIdentifier = parsedRight.prerelease[index]; |
| 128 | if (leftIdentifier === undefined) { |
| 129 | return -1; |
| 130 | } |
| 131 | if (rightIdentifier === undefined) { |
| 132 | return 1; |
| 133 | } |
| 134 | const comparison = comparePrereleaseIdentifier(leftIdentifier, rightIdentifier); |
| 135 | if (comparison !== 0) { |
| 136 | return comparison; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /** Keep plain JavaScript: SSH startup stringifies this for remote Node versions without known TS support. */ |
| 144 | export const satisfiesSemverRange: (rawVersion: string, range: string) => boolean = |
| 145 | function satisfiesSemverRange(rawVersion, range) { |
| 146 | const normalizedVersion = String(rawVersion).trim().replace(/^v/, ""); |
| 147 | const versionMatch = normalizedVersion.match( |
| 148 | /^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-[0-9A-Za-z.-]+)?$/, |
| 149 | ); |
| 150 | if (!versionMatch) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | const version = { |
| 155 | major: Number(versionMatch[1]), |
| 156 | minor: Number(versionMatch[2] || 0), |
| 157 | patch: Number(versionMatch[3] || 0), |
| 158 | }; |
| 159 | |
| 160 | return range.split("||").some((group) => { |
| 161 | const comparators = group |
| 162 | .trim() |
| 163 | .replace(/(\^|>=|>|<=|<|=)\s+(?=v?\d)/g, "$1") |
| 164 | .split(/\s+/) |
| 165 | .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 || targetMatch[2] === undefined) { |
| 208 | return version.major === target.major; |
| 209 | } |
| 210 | if (target.minor > 0 || targetMatch[3] === undefined) { |
| 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 | }; |