Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

Share one version-compare helper for plugin and server update checks

Gabe Kangas3aec825f5320
diff --git a/web/tests/pluginUpdateAvailable.test.ts b/web/tests/pluginUpdateAvailable.test.ts
index 86ee6ff9bd..5c9048b9c1 100644
--- a/web/tests/pluginUpdateAvailable.test.ts
+++ b/web/tests/pluginUpdateAvailable.test.ts
@@ -1,4 +1,29 @@
-import { isPluginUpdateAvailable } from '../utils/apis';
+import { isNewerVersion, isPluginUpdateAvailable } from '../utils/apis';
+
+describe('isNewerVersion', () => {
+ test('candidate strictly newer is true', () => {
+ expect(isNewerVersion('1.2.4', '1.2.3')).toBe(true);
+ expect(isNewerVersion('1.3.0', '1.2.9')).toBe(true);
+ expect(isNewerVersion('2.0.0', '1.9.9')).toBe(true);
+ });
+
+ test('equal or older candidate is false', () => {
+ expect(isNewerVersion('1.2.3', '1.2.3')).toBe(false);
+ expect(isNewerVersion('1.2.2', '1.2.3')).toBe(false);
+ expect(isNewerVersion('0.2.1', '0.3.0')).toBe(false); // the dev-build/downgrade case
+ });
+
+ test('missing versions are false (no crash)', () => {
+ expect(isNewerVersion(undefined, '1.2.3')).toBe(false);
+ expect(isNewerVersion('1.2.3', undefined)).toBe(false);
+ expect(isNewerVersion('', '')).toBe(false);
+ });
+
+ test('non-semver strings are false rather than throwing', () => {
+ expect(isNewerVersion('latest', '1.2.3')).toBe(false);
+ expect(isNewerVersion('1.2.3', 'dev')).toBe(false);
+ });
+});
describe('isPluginUpdateAvailable', () => {
describe('an update is offered only when the registry version is strictly newer', () => {
diff --git a/web/utils/apis.ts b/web/utils/apis.ts
index 6dd7644451..0674f19581 100644
--- a/web/utils/apis.ts
+++ b/web/utils/apis.ts
@@ -223,44 +223,49 @@ export async function getGithubRelease() {
return fetchExternalData(GITHUB_RELEASE_URL);
}
-function upToDate(local, remote) {
- return !semverGt(remote, local);
+// isNewerVersion reports whether `candidate` is a strictly-newer semver than
+// `current`. The shared primitive behind both the plugin-update check and the
+// Owncast server-upgrade check. Missing or non-semver version strings can't be
+// ordered, so it returns false rather than throwing or nagging about a change
+// it can't reason about.
+export function isNewerVersion(candidate?: string, current?: string): boolean {
+ if (!candidate || !current) {
+ return false;
+ }
+ try {
+ return semverGt(candidate, current);
+ } catch {
+ return false;
+ }
}
// isPluginUpdateAvailable reports whether the registry's latest version is
// strictly newer than what's installed. A newer local/dev build (e.g.
// installed 0.3.0 vs a registry 0.2.1) is NOT an update — only an older
// installed version is. Comparing with string inequality would wrongly flag a
-// dev build as "needs update" (and prompt a downgrade). Non-semver version
-// strings can't be ordered, so we conservatively report "no update" rather
-// than nagging about a change we can't reason about.
+// dev build as "needs update" (and prompt a downgrade).
export function isPluginUpdateAvailable(
installedVersion?: string,
latestVersion?: string,
): boolean {
- if (!installedVersion || !latestVersion) {
- return false;
- }
- try {
- return semverGt(latestVersion, installedVersion);
- } catch {
- return false;
- }
+ return isNewerVersion(latestVersion, installedVersion);
}
-// Make a request to the server status API and the Github releases API
-// and return a release if it's newer than the server version.
+// Make a request to the Github releases API and return the latest release
+// version if it's newer than the running server, otherwise null. Uses the same
+// semver comparison as the plugin update check (see isNewerVersion).
export async function upgradeVersionAvailable(currentVersion) {
const recentRelease = await getGithubRelease();
- let recentReleaseVersion = recentRelease.tag_name;
+ let recentReleaseVersion = recentRelease?.tag_name;
+ // No/failed release info (getGithubRelease returns {} on error): nothing to
+ // offer rather than throwing on an undefined tag.
+ if (!recentReleaseVersion) {
+ return null;
+ }
if (recentReleaseVersion.substr(0, 1) === 'v') {
recentReleaseVersion = recentReleaseVersion.substr(1);
}
- if (!upToDate(currentVersion, recentReleaseVersion)) {
- return recentReleaseVersion;
- }
-
- return null;
+ return isNewerVersion(recentReleaseVersion, currentVersion) ? recentReleaseVersion : null;
}