-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check compatibility between server and client versions (#90)
* Check compatibility between server and client versions * Address review * Adjust arg name * Replace HealthCheckRequest with {}
- Loading branch information
Showing
6 changed files
with
262 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,59 @@ | ||
export const PACKAGE_VERSION = '1.12.0'; | ||
|
||
interface Version { | ||
major: number; | ||
minor: number; | ||
} | ||
|
||
export const ClientVersion = { | ||
/** | ||
* Parses a version string into a structured Version object. | ||
* @param version - The version string to parse (e.g., "1.2.3"). | ||
* @returns A Version object. | ||
* @throws If the version format is invalid. | ||
*/ | ||
parseVersion(version: string): Version { | ||
if (!version) { | ||
throw new Error('Version is null'); | ||
} | ||
|
||
let major = undefined; | ||
let minor = undefined; | ||
[major, minor] = version.split('.', 2); | ||
major = parseInt(major, 10); | ||
minor = parseInt(minor, 10); | ||
if (isNaN(major) || isNaN(minor)) { | ||
throw new Error(`Unable to parse version, expected format: x.y[.z], found: ${version}`); | ||
} | ||
return { | ||
major, | ||
minor, | ||
}; | ||
}, | ||
|
||
/** | ||
* Checks if the client version is compatible with the server version. | ||
* @param clientVersion - The client version string. | ||
* @param serverVersion - The server version string. | ||
* @returns True if compatible, otherwise false. | ||
*/ | ||
isCompatible(clientVersion: string, serverVersion: string): boolean { | ||
if (!clientVersion || !serverVersion) { | ||
console.debug( | ||
`Unable to compare versions with null values. Client: ${clientVersion}, Server: ${serverVersion}`, | ||
); | ||
return false; | ||
} | ||
|
||
if (clientVersion === serverVersion) return true; | ||
|
||
try { | ||
const client = ClientVersion.parseVersion(clientVersion); | ||
const server = ClientVersion.parseVersion(serverVersion); | ||
return client.major === server.major && Math.abs(client.minor - server.minor) <= 1; | ||
} catch (error) { | ||
console.debug(`Unable to compare versions: ${error as string}`); | ||
return false; | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,47 @@ | ||
import {test, expect} from 'vitest'; | ||
import {version} from '../../package.json'; | ||
import {PACKAGE_VERSION} from '../../src/client-version.js'; | ||
import {PACKAGE_VERSION, ClientVersion} from '../../src/client-version.js'; | ||
|
||
test('Client version is consistent', () => { | ||
expect(version).toBe(PACKAGE_VERSION); | ||
}); | ||
|
||
test.each([ | ||
{input: '1.2', expected: {major: 1, minor: 2}}, | ||
{input: '1.2.3', expected: {major: 1, minor: 2}}, | ||
])('parseVersion($input) should return $expected', ({input, expected}) => { | ||
const result = ClientVersion.parseVersion(input); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test.each([ | ||
{input: '', error: 'Version is null'}, | ||
{input: '1', error: 'Unable to parse version, expected format: x.y[.z], found: 1'}, | ||
{input: '1.', error: 'Unable to parse version, expected format: x.y[.z], found: 1.'}, | ||
{input: '.1', error: 'Unable to parse version, expected format: x.y[.z], found: .1'}, | ||
{input: '.1.', error: 'Unable to parse version, expected format: x.y[.z], found: .1.'}, | ||
{input: '1.a.1', error: 'Unable to parse version, expected format: x.y[.z], found: 1.a.1'}, | ||
{input: 'a.1.1', error: 'Unable to parse version, expected format: x.y[.z], found: a.1.1'}, | ||
])('parseVersion($input) should throw error $error', ({input, error}) => { | ||
expect(() => ClientVersion.parseVersion(input)).toThrow(error); | ||
}); | ||
|
||
test.each([ | ||
{client: '1.9.3.dev0', server: '2.8.1.dev12-something', expected: false}, | ||
{client: '1.9', server: '2.8', expected: false}, | ||
{client: '1', server: '2', expected: false}, | ||
{client: '1.9.0', server: '2.9.0', expected: false}, | ||
{client: '1.1.0', server: '1.2.9', expected: true}, | ||
{client: '1.2.7', server: '1.1.8.dev0', expected: true}, | ||
{client: '1.2.1', server: '1.2.29', expected: true}, | ||
{client: '1.2.0', server: '1.2.0', expected: true}, | ||
{client: '1.2.0', server: '1.4.0', expected: false}, | ||
{client: '1.4.0', server: '1.2.0', expected: false}, | ||
{client: '1.9.0', server: '3.7.0', expected: false}, | ||
{client: '3.0.0', server: '1.0.0', expected: false}, | ||
{client: '', server: '1.0.0', expected: false}, | ||
{client: '1.0.0', server: '', expected: false}, | ||
])('isCompatible($client, $server) should return $expected', ({client, server, expected}) => { | ||
const result = ClientVersion.isCompatible(client, server); | ||
expect(result).toEqual(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,59 @@ | ||
export const PACKAGE_VERSION = '1.12.0'; | ||
|
||
interface Version { | ||
major: number; | ||
minor: number; | ||
} | ||
|
||
export const ClientVersion = { | ||
/** | ||
* Parses a version string into a structured Version object. | ||
* @param version - The version string to parse (e.g., "1.2.3"). | ||
* @returns A Version object. | ||
* @throws If the version format is invalid. | ||
*/ | ||
parseVersion(version: string): Version { | ||
if (!version) { | ||
throw new Error('Version is null'); | ||
} | ||
|
||
let major = undefined; | ||
let minor = undefined; | ||
[major, minor] = version.split('.', 2); | ||
major = parseInt(major, 10); | ||
minor = parseInt(minor, 10); | ||
if (isNaN(major) || isNaN(minor)) { | ||
throw new Error(`Unable to parse version, expected format: x.y[.z], found: ${version}`); | ||
} | ||
return { | ||
major, | ||
minor, | ||
}; | ||
}, | ||
|
||
/** | ||
* Checks if the client version is compatible with the server version. | ||
* @param clientVersion - The client version string. | ||
* @param serverVersion - The server version string. | ||
* @returns True if compatible, otherwise false. | ||
*/ | ||
isCompatible(clientVersion: string, serverVersion: string): boolean { | ||
if (!clientVersion || !serverVersion) { | ||
console.debug( | ||
`Unable to compare versions with null values. Client: ${clientVersion}, Server: ${serverVersion}`, | ||
); | ||
return false; | ||
} | ||
|
||
if (clientVersion === serverVersion) return true; | ||
|
||
try { | ||
const client = ClientVersion.parseVersion(clientVersion); | ||
const server = ClientVersion.parseVersion(serverVersion); | ||
return client.major === server.major && Math.abs(client.minor - server.minor) <= 1; | ||
} catch (error) { | ||
console.debug(`Unable to compare versions: ${error as string}`); | ||
return false; | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,47 @@ | ||
import {test, expect} from 'vitest'; | ||
import {version} from '../../package.json'; | ||
import {PACKAGE_VERSION} from '../../src/client-version.js'; | ||
import {PACKAGE_VERSION, ClientVersion} from '../../src/client-version.js'; | ||
|
||
test('Client version is consistent', () => { | ||
expect(version).toBe(PACKAGE_VERSION); | ||
}); | ||
|
||
test.each([ | ||
{input: '1.2', expected: {major: 1, minor: 2}}, | ||
{input: '1.2.3', expected: {major: 1, minor: 2}}, | ||
])('parseVersion($input) should return $expected', ({input, expected}) => { | ||
const result = ClientVersion.parseVersion(input); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test.each([ | ||
{input: '', error: 'Version is null'}, | ||
{input: '1', error: 'Unable to parse version, expected format: x.y[.z], found: 1'}, | ||
{input: '1.', error: 'Unable to parse version, expected format: x.y[.z], found: 1.'}, | ||
{input: '.1', error: 'Unable to parse version, expected format: x.y[.z], found: .1'}, | ||
{input: '.1.', error: 'Unable to parse version, expected format: x.y[.z], found: .1.'}, | ||
{input: '1.a.1', error: 'Unable to parse version, expected format: x.y[.z], found: 1.a.1'}, | ||
{input: 'a.1.1', error: 'Unable to parse version, expected format: x.y[.z], found: a.1.1'}, | ||
])('parseVersion($input) should throw error $error', ({input, error}) => { | ||
expect(() => ClientVersion.parseVersion(input)).toThrow(error); | ||
}); | ||
|
||
test.each([ | ||
{client: '1.9.3.dev0', server: '2.8.1.dev12-something', expected: false}, | ||
{client: '1.9', server: '2.8', expected: false}, | ||
{client: '1', server: '2', expected: false}, | ||
{client: '1.9.0', server: '2.9.0', expected: false}, | ||
{client: '1.1.0', server: '1.2.9', expected: true}, | ||
{client: '1.2.7', server: '1.1.8.dev0', expected: true}, | ||
{client: '1.2.1', server: '1.2.29', expected: true}, | ||
{client: '1.2.0', server: '1.2.0', expected: true}, | ||
{client: '1.2.0', server: '1.4.0', expected: false}, | ||
{client: '1.4.0', server: '1.2.0', expected: false}, | ||
{client: '1.9.0', server: '3.7.0', expected: false}, | ||
{client: '3.0.0', server: '1.0.0', expected: false}, | ||
{client: '', server: '1.0.0', expected: false}, | ||
{client: '1.0.0', server: '', expected: false}, | ||
])('isCompatible($client, $server) should return $expected', ({client, server, expected}) => { | ||
const result = ClientVersion.isCompatible(client, server); | ||
expect(result).toEqual(expected); | ||
}); |