-
Notifications
You must be signed in to change notification settings - Fork 10
/
protocol-version.js
69 lines (62 loc) · 1.58 KB
/
protocol-version.js
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
60
61
62
63
64
65
66
67
68
69
module.exports = class {
static _parse(str) {
const split = str.split('.').map((x) => parseInt(x));
if (split.length !== 2) throw new Error('Length does not match');
return {
major: split[0],
minor: split[1],
};
}
/**
* Returns the minimal supported frontend protocol version.
* @returns {string}
*/
static minSupported() {
return '1.0';
}
/**
* Returns the maximal supported frontend protocol version.
* @returns {string}
*/
static maxSupported() {
return '1.1';
}
/**
* Returns the minimal supported frontend protocol version necessary for the given feature.
* @param feature
* @returns {string}
*/
static get(feature) {
switch (feature) {
case 'pairing':
case 'chained-sessions':
return '1.1';
default:
throw new Error('Protocol version requested of unknown feature');
}
}
/**
* Checks whether version x is above version y
* @param {string} x
* @param {string} y
* @returns {boolean}
*/
static above(x, y) {
const parsedX = this._parse(x);
const parsedY = this._parse(y);
if (parsedX.major === parsedY.major) return parsedX.minor > parsedY.minor;
return parsedX.major > parsedY.major;
}
/**
* Checks whether version x is below version y
* @param {string} x
* @param {string} y
* @returns {boolean}
*/
static below(x, y) {
const parsedX = this._parse(x);
const parsedY = this._parse(y);
if (parsedX.major === parsedY.major) return parsedX.minor < parsedY.minor;
return parsedX.major < parsedY.major;
}
};