-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathclient-version.ts
More file actions
59 lines (53 loc) · 1.89 KB
/
client-version.ts
File metadata and controls
59 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
export const PACKAGE_VERSION = '1.15.1';
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;
}
},
};