refactor: drop semver (#15029)

This commit is contained in:
Alex Rudenko
2026-05-21 13:30:36 +00:00
committed by GitHub
parent 1f9a7e8308
commit c96f6ad99e
4 changed files with 79 additions and 19 deletions
+1 -1
View File
@@ -28405,6 +28405,7 @@
"version": "7.7.4",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz",
"integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==",
"dev": true,
"license": "ISC",
"bin": {
"semver": "bin/semver.js"
@@ -31668,7 +31669,6 @@
"license": "Apache-2.0",
"dependencies": {
"modern-tar": "^0.7.6",
"semver": "^7.7.4",
"yargs": "^17.7.2"
},
"bin": {
+1 -2
View File
@@ -84,8 +84,7 @@
],
"dependencies": {
"modern-tar": "^0.7.6",
"yargs": "^17.7.2",
"semver": "^7.7.4"
"yargs": "^17.7.2"
},
"peerDependencies": {
"proxy-agent": ">=8.0.1"
+24 -12
View File
@@ -8,8 +8,6 @@ import {execSync} from 'node:child_process';
import os from 'node:os';
import path from 'node:path';
import semver from 'semver';
import {getJSON} from '../httpUtil.js';
import {BrowserPlatform, ChromeReleaseChannel} from './types.js';
@@ -406,17 +404,31 @@ function getBaseUserDataDirPathMac() {
}
export function compareVersions(a: string, b: string): number {
if (!semver.valid(a)) {
throw new Error(`Version ${a} is not a valid semver version`);
const cleanA = a.trim();
const cleanB = b.trim();
const versionRegex = /^\d+(?:\.\d+){0,3}$/;
if (!versionRegex.test(cleanA)) {
throw new Error(`Version ${a} is not a valid Chrome version`);
}
if (!semver.valid(b)) {
throw new Error(`Version ${b} is not a valid semver version`);
if (!versionRegex.test(cleanB)) {
throw new Error(`Version ${b} is not a valid Chrome version`);
}
if (semver.gt(a, b)) {
return 1;
} else if (semver.lt(a, b)) {
return -1;
} else {
return 0;
const aParts = cleanA.split('.').map(Number);
const bParts = cleanB.split('.').map(Number);
for (let i = 0; i < 4; i++) {
const aPart = aParts[i] ?? 0;
const bPart = bParts[i] ?? 0;
if (aPart > bPart) {
return 1;
}
if (aPart < bPart) {
return -1;
}
}
return 0;
}
@@ -382,9 +382,58 @@ describe('Chrome', () => {
assert.strictEqual(await resolveBuildId('115.0.5790'), '115.0.5790.170');
});
it('should compare versions', async () => {
assert.ok(compareVersions('115.0.5790', '115.0.5789') >= 1);
assert.ok(compareVersions('115.0.5789', '115.0.5790') <= -1);
assert.ok(compareVersions('115.0.5790', '115.0.5790') === 0);
it('should compare versions', () => {
// 3-part comparison (like '115.0.5789')
assert.strictEqual(compareVersions('115.0.5790', '115.0.5789'), 1);
assert.strictEqual(compareVersions('115.0.5789', '115.0.5790'), -1);
assert.strictEqual(compareVersions('115.0.5789', '115.0.5789'), 0);
// 4-part comparison
assert.strictEqual(compareVersions('115.0.5790.170', '115.0.5790.169'), 1);
assert.strictEqual(compareVersions('115.0.5790.169', '115.0.5790.170'), -1);
assert.strictEqual(compareVersions('115.0.5790.170', '115.0.5790.170'), 0);
// Major version difference
assert.strictEqual(compareVersions('116.0.0.0', '115.100.1000.10000'), 1);
assert.strictEqual(compareVersions('115.100.1000.10000', '116.0.0.0'), -1);
// Minor/build/patch differences
assert.strictEqual(compareVersions('115.1.5790.170', '115.0.5790.170'), 1);
assert.strictEqual(compareVersions('115.0.5791.170', '115.0.5790.170'), 1);
// Different number of parts (missing parts treated as 0)
assert.strictEqual(compareVersions('115.0.5790', '115.0.5790.0'), 0);
assert.strictEqual(compareVersions('115.0.5790.1', '115.0.5790'), 1);
assert.strictEqual(compareVersions('115.0.5790', '115.0.5790.1'), -1);
assert.strictEqual(compareVersions('115', '115.0.0.0'), 0);
// Leading/trailing whitespace
assert.strictEqual(
compareVersions(' 115.0.5790.170 ', '115.0.5790.170'),
0,
);
// Invalid formats should throw
assert.throws(() => {
return compareVersions('invalid', '115.0.5790');
});
assert.throws(() => {
return compareVersions('115.0.5790', '115.a.5790');
});
assert.throws(() => {
return compareVersions('115.0.5790.170.5', '115.0.5790');
});
assert.throws(() => {
return compareVersions('115.0.5790.', '115.0.5790');
});
assert.throws(() => {
return compareVersions('.115', '115.0.5790');
});
assert.throws(() => {
return compareVersions('-115', '115.0.5790');
});
assert.throws(() => {
return compareVersions('115.-1.0', '115.0.5790');
});
});
});