Fix crash on invalid server versions

Clean up countVersion function a bit
This commit is contained in:
Christopher Cookman 2023-01-06 10:37:48 -07:00
parent c62ee75bf2
commit 2865f7af72
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -210,8 +210,10 @@ function findHighestVersion() {
for (const key in serverList.servers) { for (const key in serverList.servers) {
if (serverList.servers.hasOwnProperty(key)) { if (serverList.servers.hasOwnProperty(key)) {
const currentVersion = serverList.servers[key].version; const currentVersion = serverList.servers[key].version;
if (semver.gt(currentVersion, highestVersion)) { if (semver.valid(currentVersion)) { // check if currentVersion is a valid semver string
highestVersion = currentVersion; if (semver.gt(currentVersion, highestVersion)) {
highestVersion = currentVersion;
}
} }
} }
} }
@ -227,8 +229,10 @@ function findLowestVersion() {
for (const key in serverList.servers) { for (const key in serverList.servers) {
if (serverList.servers.hasOwnProperty(key)) { if (serverList.servers.hasOwnProperty(key)) {
const currentVersion = serverList.servers[key].version; const currentVersion = serverList.servers[key].version;
if (semver.lt(currentVersion, lowestVersion)) { if (semver.valid(currentVersion)) { // check if currentVersion is a valid semver string
lowestVersion = currentVersion; if (semver.lt(currentVersion, lowestVersion)) {
lowestVersion = currentVersion;
}
} }
} }
} }
@ -259,19 +263,14 @@ var versions = {};
// Track server versions // Track server versions
function countVersions() { function countVersions() {
console.log(`${colors.cyan(`[INFO ${new Date()}]`)} Counting server versions...`); console.log(`${colors.cyan(`[INFO ${new Date()}]`)} Counting server versions...`);
versions = {}; const versions = {};
for (var key in serverList.servers) { for (const key in serverList.servers) {
if (serverList.servers.hasOwnProperty(key)) { const server = serverList.servers[key];
if (versions[serverList.servers[key].version] == undefined) { versions[server.version] = (versions[server.version] || 0) + 1;
versions[serverList.servers[key].version] = 1;
} else {
versions[serverList.servers[key].version] += 1;
}
}
} }
console.log(`${colors.cyan(`[INFO ${new Date()}]`)} ${objectLength(versions)} versions found!`); console.log(`${colors.cyan(`[INFO ${new Date()}]`)} ${Object.keys(versions).length} versions found!`);
return versions; return versions;
}; }
// updateMasterList function // updateMasterList function
function updateMasterList() { function updateMasterList() {