mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-11 05:58:56 +08:00
fix(core): version guard (#15226)
fix #15225 #### PR Dependency Tree * **PR #15226** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved self-hosted server version validation. * Correctly supports compatible beta and release-candidate versions. * Rejects outdated pre-release versions consistently. * Provides the appropriate unsupported-version guidance when compatibility requirements are not met. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
+5
-3
@@ -1,9 +1,11 @@
|
|||||||
import type { Server } from '@affine/core/modules/cloud';
|
import type { Server } from '@affine/core/modules/cloud';
|
||||||
import { MIN_SUPPORTED_SERVER_VERSION } from '@affine/core/modules/cloud/stores/server-config';
|
import {
|
||||||
|
isSupportedServerVersion,
|
||||||
|
MIN_SUPPORTED_SERVER_VERSION,
|
||||||
|
} from '@affine/core/modules/cloud/stores/server-config';
|
||||||
import { useI18n } from '@affine/i18n';
|
import { useI18n } from '@affine/i18n';
|
||||||
import { useLiveData } from '@toeverything/infra';
|
import { useLiveData } from '@toeverything/infra';
|
||||||
import { cssVarV2 } from '@toeverything/theme/v2';
|
import { cssVarV2 } from '@toeverything/theme/v2';
|
||||||
import semver from 'semver';
|
|
||||||
|
|
||||||
const rules = [
|
const rules = [
|
||||||
{
|
{
|
||||||
@@ -46,7 +48,7 @@ export const useSelfhostLoginVersionGuard = (server: Server) => {
|
|||||||
useLiveData(server.config$.selector(c => c.version)) ?? '0.0.0';
|
useLiveData(server.config$.selector(c => c.version)) ?? '0.0.0';
|
||||||
|
|
||||||
for (const rule of rules) {
|
for (const rule of rules) {
|
||||||
if (semver.lt(serverVersion, rule.min)) {
|
if (!isSupportedServerVersion(serverVersion)) {
|
||||||
return rule.tip(
|
return rule.tip(
|
||||||
t['error.UNSUPPORTED_SERVER_VERSION']({
|
t['error.UNSUPPORTED_SERVER_VERSION']({
|
||||||
requiredVersion: `>=${rule.min}`,
|
requiredVersion: `>=${rule.min}`,
|
||||||
|
|||||||
@@ -9,13 +9,17 @@ import {
|
|||||||
describe('server config version guard', () => {
|
describe('server config version guard', () => {
|
||||||
test('accepts supported server versions', () => {
|
test('accepts supported server versions', () => {
|
||||||
expect(() => assertSupportedServerVersion('0.27.0')).not.toThrow();
|
expect(() => assertSupportedServerVersion('0.27.0')).not.toThrow();
|
||||||
|
expect(() => assertSupportedServerVersion('0.27.0-beta.5')).not.toThrow();
|
||||||
|
expect(() => assertSupportedServerVersion('0.27.0-rc.1')).not.toThrow();
|
||||||
expect(() => assertSupportedServerVersion('0.28.0')).not.toThrow();
|
expect(() => assertSupportedServerVersion('0.28.0')).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('rejects old server versions', () => {
|
test('rejects old server versions', () => {
|
||||||
expect(() => assertSupportedServerVersion('0.26.9')).toThrow(
|
for (const version of ['0.26.9', '0.26.9-beta.5']) {
|
||||||
UserFriendlyError
|
expect(() => assertSupportedServerVersion(version)).toThrow(
|
||||||
);
|
UserFriendlyError
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('rejects missing or invalid server versions', () => {
|
test('rejects missing or invalid server versions', () => {
|
||||||
|
|||||||
@@ -47,16 +47,18 @@ export function createUnsupportedServerVersionError(version?: string | null) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertSupportedServerVersion(version?: string | null) {
|
export function isSupportedServerVersion(version?: string | null) {
|
||||||
if (!version) {
|
const normalized = version && semver.valid(version, { loose: true });
|
||||||
throw createUnsupportedServerVersionError(version);
|
return (
|
||||||
}
|
!!normalized &&
|
||||||
|
semver.gte(normalized, `${MIN_SUPPORTED_SERVER_VERSION}-0`, {
|
||||||
|
loose: true,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const normalized = semver.valid(version, { loose: true });
|
export function assertSupportedServerVersion(version?: string | null) {
|
||||||
if (
|
if (!isSupportedServerVersion(version)) {
|
||||||
!normalized ||
|
|
||||||
semver.lt(normalized, MIN_SUPPORTED_SERVER_VERSION, { loose: true })
|
|
||||||
) {
|
|
||||||
throw createUnsupportedServerVersionError(version);
|
throw createUnsupportedServerVersionError(version);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user