Skip to content

Commit

Permalink
Merge pull request #459 from jellyfin/drop-compare-versions
Browse files Browse the repository at this point in the history
Drop compare-versions dependency
  • Loading branch information
thornbill authored May 10, 2023
2 parents 4dcdd8b + a6acce1 commit 91c1fc4
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 26 deletions.
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
"typedoc": "0.24.6",
"typescript": "5.0.4"
},
"dependencies": {
"compare-versions": "5.0.3"
},
"peerDependencies": {
"axios": "^1.3.4"
}
Expand Down
3 changes: 1 addition & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export default {
preserveModulesRoot: 'src'
},
external: [
'axios',
'compare-versions'
'axios'
],
plugins: [ typescript() ]
};
33 changes: 20 additions & 13 deletions src/discovery/recommended-server-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import type { AxiosError, AxiosResponse } from 'axios';
import { compare } from 'compare-versions';

import type { PublicSystemInfo } from '../generated-client/models/public-system-info';
import type { Jellyfin } from '../jellyfin';
Expand All @@ -14,6 +13,7 @@ import { RecommendedServerInfoScore } from '../models/recommended-server-info';
import type { RecommendedServerIssue } from '../models/recommended-server-issue';
import { ProductNameIssue, SlowResponseIssue, SystemInfoIssue, VersionMissingIssue, VersionOutdatedIssue, VersionUnsupportedIssue } from '../models/recommended-server-issue';
import { getSystemApi } from '../utils/api/system-api';
import { isVersionLess } from '../utils/versioning';
import { API_VERSION, MINIMUM_VERSION } from '../versions';

/** The result of a SystemInfo request. */
Expand Down Expand Up @@ -52,18 +52,25 @@ function toRecommendedServerInfo(result: SystemInfoResult): RecommendedServerInf
}

const version = result.response?.data.Version;
if (!version) {
// No version was returned
issues.push(new VersionMissingIssue());
scores.push(RecommendedServerInfoScore.BAD);
} else if (compare(version, MINIMUM_VERSION, '<')) {
// Version is less than the minimum supported
issues.push(new VersionUnsupportedIssue(version));
scores.push(RecommendedServerInfoScore.OK);
} else if (compare(version, API_VERSION, '<')) {
// Version is less than the generated client, but above the minimum
issues.push(new VersionOutdatedIssue(version));
scores.push(RecommendedServerInfoScore.GOOD);
try {
if (!version) {
// No version was returned
issues.push(new VersionMissingIssue());
scores.push(RecommendedServerInfoScore.BAD);
} else if (isVersionLess(version, MINIMUM_VERSION)) {
// Version is less than the minimum supported
issues.push(new VersionUnsupportedIssue(version));
scores.push(RecommendedServerInfoScore.OK);
} else if (isVersionLess(version, API_VERSION)) {
// Version is less than the generated client, but above the minimum
issues.push(new VersionOutdatedIssue(version));
scores.push(RecommendedServerInfoScore.GOOD);
}
} catch (error) {
if (error instanceof TypeError) {
issues.push(new VersionMissingIssue());
scores.push(RecommendedServerInfoScore.BAD);
}
}

if (result.responseTime > SLOW_RESPONSE_TIME) {
Expand Down
87 changes: 87 additions & 0 deletions src/utils/__tests__/versioning.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { isVersionLess } from '..';

/**
* Versioning tests
*
* @group unit/utils
*/
describe('Versioning checks', () => {
it("doesn't allow versions with negative numbers", () => {
expect(() => isVersionLess('-10.5.3', '10.8.-9')).toThrow(TypeError);
});
it("doesn't allow versions with non-numeric characters", () => {
expect(() => isVersionLess('10.2.7-beta', '15.8.9')).toThrow(TypeError);
});
/**
* Equal
*/
it('false on equal version: 0.0.0', () => {
expect(isVersionLess('0.0.0', '0.0.0')).toBe(false);
});
it('false on equal version: 0.0.5', () => {
expect(isVersionLess('0.0.5', '0.0.5')).toBe(false);
});
it('false on equal version: 0.5.0', () => {
expect(isVersionLess('0.5.0', '0.5.0')).toBe(false);
});
it('false on equal version: 5.0.0', () => {
expect(isVersionLess('5.0.0', '5.0.0')).toBe(false);
});
it('false on equal version: 10.8.9', () => {
expect(isVersionLess('10.8.9', '10.8.9')).toBe(false);
});
/**
* Greater
*/
it('1.0.0 is greater than 0.0.7', () => {
expect(isVersionLess('1.0.0', '0.0.7')).toBe(false);
});
it('1.0.0 is greater than 0.5.0', () => {
expect(isVersionLess('1.0.0', '0.5.0')).toBe(false);
});
it('1.0.0 is greater than 0.5.7', () => {
expect(isVersionLess('1.0.0', '0.5.7')).toBe(false);
});
it('2.0.0 is greater than 1.0.0', () => {
expect(isVersionLess('2.0.0', '1.0.0')).toBe(false);
});
it('0.5.0 is greater than 0.0.7', () => {
expect(isVersionLess('0.5.0', '0.0.7')).toBe(false);
});
it('0.5.7 is greater than 0.0.7', () => {
expect(isVersionLess('0.5.7', '0.0.7')).toBe(false);
});
it('2-digit versions: 12.25.34 is greater than 12.17.89', () => {
expect(isVersionLess('12.25.34', '12.17.89')).toBe(false);
});
/**
* Less
*/
it('0.0.7 is less than 1.0.0', () => {
expect(isVersionLess('0.0.7', '1.0.0')).toBe(true);
});
it('0.5.0 is less than 1.0.0', () => {
expect(isVersionLess('0.5.0', '1.0.0')).toBe(true);
});
it('0.5.7 is less than 1.0.0', () => {
expect(isVersionLess('0.5.7', '1.0.0')).toBe(true);
});
it('1.0.0 is less than 2.0.0', () => {
expect(isVersionLess('1.0.0', '2.0.0')).toBe(true);
});
it('0.0.7 is less than 0.5.0', () => {
expect(isVersionLess('0.0.7', '0.5.0')).toBe(true);
});
it('0.0.7 is less than 0.5.7', () => {
expect(isVersionLess('0.0.7', '0.5.7')).toBe(true);
});
it('2-digit versions: 12.17.89 is less than 12.25.34', () => {
expect(isVersionLess('12.17.89', '12.25.34')).toBe(true);
});
});
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './address-candidates';
export * from './authentication';
export * from './url';
export * from './browser-profiles';
export * from './versioning';
37 changes: 37 additions & 0 deletions src/utils/versioning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @returns - Array of 3 numbers or undefined
* (if the passed string it's not in the MAJOR.MINOR.PATCH numeric-only format)
*/
function parseVersion(version: string): number[] | undefined {
const arr = version.split('.').map(Number);

if (arr.length === 3 && arr.every((n) => n >= 0)) {
return arr;
}
}

/**
* Check if given version is less than a baseline
*
* Versions must be in semver format: X.Y.Z (Major.Minor.Patch)
* @throws If version is not in a numeric-only semver format
* @param check - The version to check
* @param baseline - The minimum version considered supported
*/
export function isVersionLess(check: string, baseline: string): boolean {
const parsedCheck = parseVersion(check);
const parsedBaseline = parseVersion(baseline);

if (!parsedCheck || !parsedBaseline) {
throw new TypeError("Version doesn't match a numeric-only semver format");
}

const [majorCheck, minorCheck, patchCheck] = parsedCheck;
const [majorBaseline, minorBaseline, patchBaseline] = parsedBaseline;

return (
majorCheck < majorBaseline ||
(majorCheck === majorBaseline && minorCheck < minorBaseline) ||
(majorCheck === majorBaseline && minorCheck === minorBaseline && patchCheck < patchBaseline)
);
}

0 comments on commit 91c1fc4

Please sign in to comment.