node-red-contrib-ccu 4.9.0 → 4.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/README.md +2 -1
- package/nodes/ccu-connection.js +127 -4
- package/nodes/lib/lite.js +179 -42
- package/nodes/lib/metaprovider.js +327 -333
- package/nodes/lib/status.js +18 -2
- package/nodes/locales/de/ccu-connection.html +1 -1
- package/nodes/locales/en-US/ccu-connection.html +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,50 @@ Notable changes to node-red-contrib-ccu. Format follows
|
|
|
5
5
|
user-visible symptom and the cause, not the commit list (the release notes
|
|
6
6
|
append commits automatically).
|
|
7
7
|
|
|
8
|
+
## 4.10.1 (2026-11-10)
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- **openccu-lite: a connection whose token reads only the names lost its
|
|
13
|
+
events and commands after the update to 4.7 or later, without a word.**
|
|
14
|
+
Since 4.7 a connection to openccu-lite with a token in the connection node
|
|
15
|
+
moves its interfaces to the system's lite-rpc. A token made for the names
|
|
16
|
+
alone (`meta:read`, all the direct connection needed before) may not use
|
|
17
|
+
lite-rpc: the system refused the interface list and the event stream, and
|
|
18
|
+
the log said only `lite-rpc connected: no interface running`. The
|
|
19
|
+
connection now asks the system what the token may do before it moves
|
|
20
|
+
anything. Without `rpc:read` the interfaces stay on the direct connection
|
|
21
|
+
with callbacks, as before the update, and the log says that lite-rpc needs
|
|
22
|
+
`rpc:read` (events and device lists) and `rpc:operate` (commands). Where
|
|
23
|
+
the connection type is set to _openccu-lite_, the log has an error naming
|
|
24
|
+
the missing scopes and the nodes show `token lacks rpc:read, rpc:operate`
|
|
25
|
+
instead of waiting. A token with `rpc:read` but without `rpc:operate`
|
|
26
|
+
connects through lite-rpc and gets a warning that commands are refused.
|
|
27
|
+
(#189, @reini12345)
|
|
28
|
+
|
|
29
|
+
## 4.10.0 (2026-11-09)
|
|
30
|
+
|
|
31
|
+
### Changed
|
|
32
|
+
|
|
33
|
+
- **openccu-lite: the names come from occulite-client's live copy.** The
|
|
34
|
+
connection's own HTTP client for the metadata API (snapshot, change
|
|
35
|
+
stream, re-reads) is gone; names, rooms and functions now come from
|
|
36
|
+
occulite-client, in the ReGa shape as before. Where the connection node
|
|
37
|
+
has its own token (as pairing makes it), one client serves the names and
|
|
38
|
+
the interfaces - one connection to the system instead of two. Where the
|
|
39
|
+
names and the interfaces have different credentials (RedMatic on the
|
|
40
|
+
system: the system's read-only token for the names, RedMatic's own token
|
|
41
|
+
for lite-rpc) or the interfaces go the CCU way, a names-only client follows
|
|
42
|
+
the names and opens no lite-rpc stream of its own.
|
|
43
|
+
|
|
44
|
+
### Fixed
|
|
45
|
+
|
|
46
|
+
- **openccu-lite: a system that did not answer the first connect of the
|
|
47
|
+
lite-rpc client is connected later.** The client was given up after a
|
|
48
|
+
failed first connect while the log said it kept trying; now a new client
|
|
49
|
+
is opened after 5 s, growing to a minute. A refused credential, or a system
|
|
50
|
+
that is not openccu-lite, still ends it with a message.
|
|
51
|
+
|
|
8
52
|
## 4.9.0 (2026-11-09)
|
|
9
53
|
|
|
10
54
|
### Added
|
package/README.md
CHANGED
|
@@ -57,7 +57,8 @@ CCU-Jack keep their own connections.
|
|
|
57
57
|
rpc node may call any method) and for reading the names. Alternatively an API token made on the system goes into
|
|
58
58
|
the **openccu-lite token** field by hand - for lite-rpc it needs the `rpc:*` scope of what the flow does
|
|
59
59
|
(`rpc:read` for events and reads, `rpc:operate` for `setValue`, `rpc:configure` for paramsets and links,
|
|
60
|
-
`rpc:admin` for the rest).
|
|
60
|
+
`rpc:admin` for the rest). A token without `rpc:read` (one made for the names alone) keeps the direct connection
|
|
61
|
+
with callbacks under _auto_, and the log says what lite-rpc would need.
|
|
61
62
|
- **System variables, programs and HM-Script do not exist there.** The `ccu-sysvar`, `ccu-program`, `ccu-script` and
|
|
62
63
|
`ccu-poll` nodes stay in the flows and answer every message with an error.
|
|
63
64
|
|
package/nodes/ccu-connection.js
CHANGED
|
@@ -732,8 +732,14 @@ module.exports = function (RED) {
|
|
|
732
732
|
? config.connectionType
|
|
733
733
|
: 'auto';
|
|
734
734
|
this.liteToken = config.metaToken || lite.readAddonToken();
|
|
735
|
+
// the enabled interfaces, for startMeta()'s choice of one client for names and interfaces
|
|
736
|
+
this.liteConfig = config;
|
|
735
737
|
this.lite = null;
|
|
736
738
|
this.liteIfaces = [];
|
|
739
|
+
// B-44: the scopes the system lists for the lite-rpc credential (undefined: not known)
|
|
740
|
+
// and, per interface, why it cannot be reached (the nodes show it)
|
|
741
|
+
this.liteScopes = undefined;
|
|
742
|
+
this.ifaceError = {};
|
|
737
743
|
this.metaMode = false;
|
|
738
744
|
this.meta = null;
|
|
739
745
|
// #167: minutes between re-reads of channel names, rooms and
|
|
@@ -1056,6 +1062,11 @@ module.exports = function (RED) {
|
|
|
1056
1062
|
}
|
|
1057
1063
|
|
|
1058
1064
|
waiting = Boolean(waiting) && !connected;
|
|
1065
|
+
if (connected && this.ifaceError[iface]) {
|
|
1066
|
+
delete this.ifaceError[iface];
|
|
1067
|
+
this.ifaceStatus[iface] = null; // the nodes drop the error text
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1059
1070
|
const waitingChanged = Boolean(this.ifaceWaiting[iface]) !== waiting;
|
|
1060
1071
|
if (this.ifaceStatus[iface] !== connected || waitingChanged) {
|
|
1061
1072
|
if (this.ifaceStatus[iface] !== connected) {
|
|
@@ -1124,7 +1135,11 @@ module.exports = function (RED) {
|
|
|
1124
1135
|
pushStatus() {
|
|
1125
1136
|
Object.keys(this.users).forEach((id) => {
|
|
1126
1137
|
if (typeof this.users[id].setStatus === 'function') {
|
|
1127
|
-
this.users[id].setStatus({
|
|
1138
|
+
this.users[id].setStatus({
|
|
1139
|
+
ifaceStatus: this.ifaceStatus,
|
|
1140
|
+
ifaceWaiting: this.ifaceWaiting,
|
|
1141
|
+
ifaceError: this.ifaceError,
|
|
1142
|
+
});
|
|
1128
1143
|
}
|
|
1129
1144
|
});
|
|
1130
1145
|
}
|
|
@@ -1797,8 +1812,54 @@ module.exports = function (RED) {
|
|
|
1797
1812
|
);
|
|
1798
1813
|
}
|
|
1799
1814
|
|
|
1815
|
+
return this.checkLiteScopes().then(() => this.startMetaProvider());
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
/**
|
|
1819
|
+
* B-44: what the lite-rpc credential may do, asked once before the interfaces are moved
|
|
1820
|
+
* there - a token made for the names alone (meta:read, enough for the CCU way up to 4.6)
|
|
1821
|
+
* cannot follow them to lite-rpc, and liteInterfaces() keeps them on the direct path then.
|
|
1822
|
+
* @returns {Promise<void>}
|
|
1823
|
+
*/
|
|
1824
|
+
checkLiteScopes() {
|
|
1825
|
+
if (
|
|
1826
|
+
this.connectionType === 'ccu' ||
|
|
1827
|
+
!this.liteToken ||
|
|
1828
|
+
!lite.LITE_INTERFACES.some((iface) => this.liteConfig[this.ifaceTypes[iface].conf + 'Enabled'])
|
|
1829
|
+
) {
|
|
1830
|
+
return Promise.resolve();
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
return lite
|
|
1834
|
+
.tokenScopes({
|
|
1835
|
+
url: (this.tlsEnabled ? 'https' : 'http') + '://' + this.host + ':' + this.metaPort,
|
|
1836
|
+
token: this.liteToken,
|
|
1837
|
+
insecure: Boolean(this.inSecure),
|
|
1838
|
+
fingerprint256: this.fingerprint256,
|
|
1839
|
+
})
|
|
1840
|
+
.then((scopes) => {
|
|
1841
|
+
this.liteScopes = scopes;
|
|
1842
|
+
if (scopes) {
|
|
1843
|
+
this.logger.debug('openccu-lite: the lite-rpc credential holds ' + scopes.join(', '));
|
|
1844
|
+
}
|
|
1845
|
+
});
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
/**
|
|
1849
|
+
* The names from the system's metadata api (startMeta's second half).
|
|
1850
|
+
* @returns {Promise<void>}
|
|
1851
|
+
*/
|
|
1852
|
+
startMetaProvider() {
|
|
1853
|
+
// one client for names and interfaces where the lite-rpc credential reads the names too
|
|
1854
|
+
// (the connection node's own token, as pairing makes it); a names-only client of its own
|
|
1855
|
+
// where they differ (RedMatic: the system's local token for the names, the addon token
|
|
1856
|
+
// for lite-rpc) or where the interfaces go another way
|
|
1857
|
+
const shared =
|
|
1858
|
+
Boolean(this.metaToken) &&
|
|
1859
|
+
this.metaToken === this.liteToken &&
|
|
1860
|
+
this.liteInterfaces(this.liteConfig, true).length > 0;
|
|
1800
1861
|
return new Promise((resolve) => {
|
|
1801
|
-
const done = setTimeout(resolve, 5000);
|
|
1862
|
+
const done = setTimeout(resolve, shared ? 0 : 5000);
|
|
1802
1863
|
this.meta = new metaProvider.MetaProvider({
|
|
1803
1864
|
host: this.host,
|
|
1804
1865
|
port: this.metaPort,
|
|
@@ -1806,6 +1867,7 @@ module.exports = function (RED) {
|
|
|
1806
1867
|
insecure: Boolean(this.inSecure),
|
|
1807
1868
|
fingerprint256: this.fingerprint256,
|
|
1808
1869
|
token: this.metaToken,
|
|
1870
|
+
shared,
|
|
1809
1871
|
logger: this.logger,
|
|
1810
1872
|
onNames: (names) => {
|
|
1811
1873
|
this.applyMetaNames(names);
|
|
@@ -2570,12 +2632,14 @@ module.exports = function (RED) {
|
|
|
2570
2632
|
* lite-rpc carries (not CUxD, not CCU-Jack), on an openccu-lite system, with a credential,
|
|
2571
2633
|
* unless the connection type says `ccu`.
|
|
2572
2634
|
* @param {object} config the node's config
|
|
2635
|
+
* @param {boolean} [quiet] no log line for a CCU by choice (startMeta's look ahead, which runs
|
|
2636
|
+
* with the system detected and a credential at hand)
|
|
2573
2637
|
* @returns {string[]}
|
|
2574
2638
|
*/
|
|
2575
|
-
liteInterfaces(config) {
|
|
2639
|
+
liteInterfaces(config, quiet = false) {
|
|
2576
2640
|
const enabled = lite.LITE_INTERFACES.filter((iface) => config[this.ifaceTypes[iface].conf + 'Enabled']);
|
|
2577
2641
|
if (this.connectionType === 'ccu') {
|
|
2578
|
-
if (this.metaMode) {
|
|
2642
|
+
if (this.metaMode && !quiet) {
|
|
2579
2643
|
this.logger.info(
|
|
2580
2644
|
'openccu-lite: the direct connection with callbacks by choice (connection type CCU) - the interfaces are reached on their own ports',
|
|
2581
2645
|
);
|
|
@@ -2607,6 +2671,24 @@ module.exports = function (RED) {
|
|
|
2607
2671
|
return [];
|
|
2608
2672
|
}
|
|
2609
2673
|
|
|
2674
|
+
// B-44: a token without rpc:read (one made for the names, as the CCU way needed up to
|
|
2675
|
+
// 4.6) cannot follow the interfaces to lite-rpc: `auto` keeps the direct connection that
|
|
2676
|
+
// worked with it; `openccu-lite` insists, and startLite() says what the token lacks
|
|
2677
|
+
const missing = lite.missingTiers(this.liteScopes);
|
|
2678
|
+
if (missing.includes('rpc:read') && this.connectionType === 'auto') {
|
|
2679
|
+
if (!quiet) {
|
|
2680
|
+
this.logger.warn(
|
|
2681
|
+
'openccu-lite: the token in the connection node holds ' +
|
|
2682
|
+
(this.liteScopes.join(', ') || 'no scope') +
|
|
2683
|
+
' - lite-rpc needs ' +
|
|
2684
|
+
lite.describeTiers(missing) +
|
|
2685
|
+
', so the interfaces stay on the direct connection with callbacks. To use lite-rpc, give the token these scopes on the system or pair the connection node with it',
|
|
2686
|
+
);
|
|
2687
|
+
}
|
|
2688
|
+
|
|
2689
|
+
return [];
|
|
2690
|
+
}
|
|
2691
|
+
|
|
2610
2692
|
return enabled;
|
|
2611
2693
|
}
|
|
2612
2694
|
|
|
@@ -2662,6 +2744,13 @@ module.exports = function (RED) {
|
|
|
2662
2744
|
insecure: Boolean(this.inSecure),
|
|
2663
2745
|
fingerprint256: this.fingerprint256,
|
|
2664
2746
|
interfaces: ifaces,
|
|
2747
|
+
// the names from the same client (startMeta decided it): its live copy to the provider
|
|
2748
|
+
meta: Boolean(this.meta && this.meta.shared),
|
|
2749
|
+
onBox: (box) => {
|
|
2750
|
+
if (this.meta && this.meta.shared) {
|
|
2751
|
+
this.meta.attach(box);
|
|
2752
|
+
}
|
|
2753
|
+
},
|
|
2665
2754
|
initId: (iface) => this.liteInitId(iface),
|
|
2666
2755
|
logger: this.logger,
|
|
2667
2756
|
createClient: this.liteCreateClient,
|
|
@@ -2731,6 +2820,15 @@ module.exports = function (RED) {
|
|
|
2731
2820
|
|
|
2732
2821
|
const up = ifaces.filter((iface) => running(iface));
|
|
2733
2822
|
this.logger.info('lite-rpc connected: ' + (up.join(', ') || 'no interface running'));
|
|
2823
|
+
if (!this.lite.may('rpc:operate')) {
|
|
2824
|
+
// B-44: events and device lists, but every command refused
|
|
2825
|
+
this.logger.warn(
|
|
2826
|
+
'lite-rpc: the openccu-lite token lacks ' +
|
|
2827
|
+
lite.describeTiers(['rpc:operate']) +
|
|
2828
|
+
' - events arrive, but setValue, putParamset and the other commands are refused. Give the token rpc:operate on the system or pair the connection node with it',
|
|
2829
|
+
);
|
|
2830
|
+
}
|
|
2831
|
+
|
|
2734
2832
|
return Promise.all(
|
|
2735
2833
|
up.map((iface) =>
|
|
2736
2834
|
this.getDevices(iface)
|
|
@@ -2754,6 +2852,31 @@ module.exports = function (RED) {
|
|
|
2754
2852
|
return;
|
|
2755
2853
|
}
|
|
2756
2854
|
|
|
2855
|
+
if (error.kind === 'forbidden' || error.kind === 'unauthenticated') {
|
|
2856
|
+
// B-44: a refused credential is not tried again - said as an error, and
|
|
2857
|
+
// the nodes show what the token lacks instead of "waiting"
|
|
2858
|
+
const missing = error.missing || (error.scope ? [error.scope] : []);
|
|
2859
|
+
this.logger.error(
|
|
2860
|
+
'lite-rpc: ' +
|
|
2861
|
+
(error.message || error) +
|
|
2862
|
+
(error.kind === 'forbidden'
|
|
2863
|
+
? '. Give the token ' +
|
|
2864
|
+
(missing.join(' and ') || 'rpc:read and rpc:operate') +
|
|
2865
|
+
' on the system, pair the connection node with it, or set the connection type to CCU for the direct connection with callbacks'
|
|
2866
|
+
: ''),
|
|
2867
|
+
);
|
|
2868
|
+
const text =
|
|
2869
|
+
error.kind === 'forbidden'
|
|
2870
|
+
? 'token lacks ' + (missing.join(', ') || 'rpc:read')
|
|
2871
|
+
: 'token not accepted';
|
|
2872
|
+
ifaces.forEach((iface) => {
|
|
2873
|
+
this.ifaceError[iface] = text;
|
|
2874
|
+
this.ifaceStatus[iface] = null;
|
|
2875
|
+
this.setIfaceStatus(iface, false);
|
|
2876
|
+
});
|
|
2877
|
+
return;
|
|
2878
|
+
}
|
|
2879
|
+
|
|
2757
2880
|
this.logger.warn('lite-rpc: ' + (error.message || error) + ' - the client keeps trying');
|
|
2758
2881
|
ifaces.forEach((iface) => this.setIfaceStatus(iface, false, true));
|
|
2759
2882
|
});
|
package/nodes/lib/lite.js
CHANGED
|
@@ -34,6 +34,20 @@ const CALLBACK_METHODS = [
|
|
|
34
34
|
'readdedDevice',
|
|
35
35
|
];
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* B-44: the rpc tiers as the system reads them (occulited's scope table): each tier includes the
|
|
39
|
+
* ones before it, Full access (`*`) everything. The system lists only the highest tier a token
|
|
40
|
+
* holds (`rpc:operate`, no `rpc:read` beside it), so a tier is never looked up literally.
|
|
41
|
+
*/
|
|
42
|
+
const RPC_INCLUDES = {
|
|
43
|
+
'rpc:operate': ['rpc:read'],
|
|
44
|
+
'rpc:configure': ['rpc:operate', 'rpc:read'],
|
|
45
|
+
'rpc:admin': ['rpc:configure', 'rpc:operate', 'rpc:read'],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** what each tier the interfaces need is for, in the order they matter */
|
|
49
|
+
const RPC_TIERS = {'rpc:read': 'events and device lists', 'rpc:operate': 'commands'};
|
|
50
|
+
|
|
37
51
|
let modules = null;
|
|
38
52
|
|
|
39
53
|
/**
|
|
@@ -74,6 +88,81 @@ function readAddonToken(file = module.exports.ADDON_TOKEN_FILE) {
|
|
|
74
88
|
}
|
|
75
89
|
}
|
|
76
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Whether a credential holding `scopes` may do what needs `want`.
|
|
93
|
+
* @param {string[]} scopes as the system lists them
|
|
94
|
+
* @param {string} want
|
|
95
|
+
* @returns {boolean}
|
|
96
|
+
*/
|
|
97
|
+
function covers(scopes, want) {
|
|
98
|
+
return scopes.some((have) => have === want || have === '*' || (RPC_INCLUDES[have] || []).includes(want));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* B-44: the tiers lite-rpc needs that `scopes` lacks: `rpc:read` (events, device lists),
|
|
103
|
+
* `rpc:operate` (commands).
|
|
104
|
+
* @param {string[]|undefined} scopes undefined: not known, nothing is said to be missing
|
|
105
|
+
* @returns {string[]}
|
|
106
|
+
*/
|
|
107
|
+
function missingTiers(scopes) {
|
|
108
|
+
if (!Array.isArray(scopes)) {
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return Object.keys(RPC_TIERS).filter((tier) => !covers(scopes, tier));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* B-44: what a token lacking these tiers cannot do, for the log.
|
|
117
|
+
* @param {string[]} missing from missingTiers()
|
|
118
|
+
* @returns {string} e.g. "rpc:read (events and device lists) and rpc:operate (commands)"
|
|
119
|
+
*/
|
|
120
|
+
function describeTiers(missing) {
|
|
121
|
+
return missing.map((tier) => tier + ' (' + RPC_TIERS[tier] + ')').join(' and ');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* B-44: the scopes the system lists for a credential (GET /api/auth/v1/state), asked before the
|
|
126
|
+
* interfaces are moved to lite-rpc: a token made for the names (meta:read) cannot follow them
|
|
127
|
+
* there. Undefined where it cannot be told (no answer, a refused credential - the client says
|
|
128
|
+
* that itself -, an older system without the auth state): nothing is decided on it then.
|
|
129
|
+
* @param {object} options
|
|
130
|
+
* @param {string} options.url the system, scheme and authority
|
|
131
|
+
* @param {string} options.token
|
|
132
|
+
* @param {boolean} [options.insecure]
|
|
133
|
+
* @param {string} [options.fingerprint256]
|
|
134
|
+
* @param {number} [options.timeout] ms
|
|
135
|
+
* @returns {Promise<string[]|undefined>}
|
|
136
|
+
*/
|
|
137
|
+
async function tokenScopes({url, token, insecure, fingerprint256, timeout = 5000}) {
|
|
138
|
+
const controller = new AbortController();
|
|
139
|
+
const timer = setTimeout(() => controller.abort(), timeout);
|
|
140
|
+
try {
|
|
141
|
+
const transport = load().nodeTransport(trustOf({insecure, fingerprint256}));
|
|
142
|
+
const response = await transport.request(
|
|
143
|
+
'GET',
|
|
144
|
+
url + '/api/auth/v1/state',
|
|
145
|
+
{Accept: 'application/json', Authorization: 'Bearer ' + token, 'X-Occulite-Request': '1'},
|
|
146
|
+
undefined,
|
|
147
|
+
controller.signal,
|
|
148
|
+
);
|
|
149
|
+
if (response.status !== 200) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const state = JSON.parse(response.body);
|
|
154
|
+
if (!state || state.authenticated === false || !Array.isArray(state.scopes)) {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return state.scopes.map(String);
|
|
159
|
+
} catch {
|
|
160
|
+
return undefined;
|
|
161
|
+
} finally {
|
|
162
|
+
clearTimeout(timer);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
77
166
|
/**
|
|
78
167
|
* Whether an error of the client is an RPC fault of the interface process (the process answered).
|
|
79
168
|
* @param {*} error
|
|
@@ -167,42 +256,6 @@ function liteValue(value, jsonDouble) {
|
|
|
167
256
|
return value;
|
|
168
257
|
}
|
|
169
258
|
|
|
170
|
-
const RPC_TIERS = ['rpc:operate', 'rpc:configure', 'rpc:admin'];
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* occulite-client B-3: the client decides "names only" from the literal `rpc:read` in
|
|
174
|
-
* GET /api/auth/v1/state, but a token made with a higher tier (rpc:admin, say) carries that tier
|
|
175
|
-
* alone - the system includes the lower ones. The transport spells `rpc:read` out for the client
|
|
176
|
-
* where a tier implies it. Goes once the package fixes it.
|
|
177
|
-
* @param {object} transport occulite-client's Transport
|
|
178
|
-
* @returns {object} the same transport
|
|
179
|
-
*/
|
|
180
|
-
function tierAwareTransport(transport) {
|
|
181
|
-
return {
|
|
182
|
-
request: (method, url, headers, body, signal) =>
|
|
183
|
-
transport.request(method, url, headers, body, signal).then((response) => {
|
|
184
|
-
if (response.status !== 200 || !/\/api\/auth\/v1\/state(\?|$)/.test(String(url))) {
|
|
185
|
-
return response;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
let parsed;
|
|
189
|
-
try {
|
|
190
|
-
parsed = JSON.parse(response.body);
|
|
191
|
-
} catch {
|
|
192
|
-
return response;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const scopes = Array.isArray(parsed && parsed.scopes) ? parsed.scopes : [];
|
|
196
|
-
if (scopes.includes('rpc:read') || !scopes.some((scope) => RPC_TIERS.includes(scope))) {
|
|
197
|
-
return response;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return {...response, body: JSON.stringify({...parsed, scopes: [...scopes, 'rpc:read']})};
|
|
201
|
-
}),
|
|
202
|
-
stream: (url, headers, signal) => transport.stream(url, headers, signal),
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
259
|
/**
|
|
207
260
|
* The certificate trust for occulite-client's transport: the fingerprint pinned at pairing, else
|
|
208
261
|
* any certificate where the node says so, else Node's own store.
|
|
@@ -233,6 +286,10 @@ class LiteConnection {
|
|
|
233
286
|
* @param {object} options.logger {debug, info, warn, error}
|
|
234
287
|
* @param {number} [options.timeout] per request, ms
|
|
235
288
|
* @param {Function} [options.createClient] for tests, in place of new OccuLite()
|
|
289
|
+
* @param {boolean} [options.meta] the client keeps the live copy of the names too (one client for both)
|
|
290
|
+
* @param {function(object)} [options.onBox] every client this connection opens, before it connects
|
|
291
|
+
* @param {number} [options.retryMin] the first wait before a failed connect is tried again, ms
|
|
292
|
+
* @param {number} [options.retryMax] the longest wait, ms
|
|
236
293
|
*/
|
|
237
294
|
constructor(options) {
|
|
238
295
|
this.options = options;
|
|
@@ -242,6 +299,9 @@ class LiteConnection {
|
|
|
242
299
|
this.server = null;
|
|
243
300
|
this.closed = false;
|
|
244
301
|
this.ready = false;
|
|
302
|
+
this.retryTimeout = null;
|
|
303
|
+
this.retryMin = options.retryMin === undefined ? 5000 : options.retryMin;
|
|
304
|
+
this.retryMax = options.retryMax === undefined ? 60000 : options.retryMax;
|
|
245
305
|
}
|
|
246
306
|
|
|
247
307
|
/**
|
|
@@ -253,27 +313,70 @@ class LiteConnection {
|
|
|
253
313
|
}
|
|
254
314
|
|
|
255
315
|
/**
|
|
256
|
-
* Opens the client. Resolves once the first picture is loaded
|
|
257
|
-
*
|
|
258
|
-
*
|
|
316
|
+
* Opens the client. Resolves once the first picture is loaded. occulite-client (0.4) does not
|
|
317
|
+
* try a failed connect again (its streams only reconnect once connected), so a system that
|
|
318
|
+
* did not answer (unreachable, timeout, still starting) gets a new client after a wait,
|
|
319
|
+
* growing to a minute; a refused credential, or a system that is not openccu-lite, rejects.
|
|
259
320
|
* @returns {Promise<void>}
|
|
260
321
|
*/
|
|
261
322
|
start() {
|
|
323
|
+
return new Promise((resolve, reject) => {
|
|
324
|
+
let attempt = 0;
|
|
325
|
+
const connect = () => {
|
|
326
|
+
this.retryTimeout = null;
|
|
327
|
+
this.open().then(resolve, (error) => {
|
|
328
|
+
if (this.closed) {
|
|
329
|
+
reject(error);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const kind = kindOf(error);
|
|
334
|
+
if (kind === 'unauthenticated' || kind === 'forbidden' || kind === 'unsupported') {
|
|
335
|
+
reject(error);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const delay = Math.min(this.retryMin * 2 ** attempt, this.retryMax);
|
|
340
|
+
attempt += 1;
|
|
341
|
+
this.logger[attempt === 1 ? 'warn' : 'debug'](
|
|
342
|
+
'lite-rpc: ' + (error.message || error) + ' - connecting again in ' + delay / 1000 + ' s',
|
|
343
|
+
);
|
|
344
|
+
this.retryTimeout = setTimeout(connect, delay);
|
|
345
|
+
});
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
connect();
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* One client: the stream's events into the callback table, the first picture.
|
|
354
|
+
* @returns {Promise<void>}
|
|
355
|
+
*/
|
|
356
|
+
open() {
|
|
357
|
+
if (this.box) {
|
|
358
|
+
this.box.close();
|
|
359
|
+
}
|
|
360
|
+
|
|
262
361
|
const {OccuLite, callbackEvents, nodeTransport} = load();
|
|
263
362
|
const debug = (message) => this.logger.debug('occulite-client: ' + message);
|
|
264
363
|
const clientOptions = {
|
|
265
364
|
url: this.options.url,
|
|
266
365
|
auth: {token: this.options.token},
|
|
267
|
-
transport:
|
|
366
|
+
transport: nodeTransport(trustOf(this.options)),
|
|
268
367
|
interfaces: this.interfaces,
|
|
269
368
|
values: true,
|
|
270
|
-
meta:
|
|
369
|
+
meta: Boolean(this.options.meta),
|
|
271
370
|
devices: true,
|
|
272
371
|
timeout: this.options.timeout || 30000,
|
|
273
372
|
log: {debug, info: debug, warn: debug, error: debug},
|
|
274
373
|
};
|
|
275
374
|
const box = this.options.createClient ? this.options.createClient(clientOptions) : new OccuLite(clientOptions);
|
|
276
375
|
this.box = box;
|
|
376
|
+
if (this.options.onBox) {
|
|
377
|
+
this.options.onBox(box);
|
|
378
|
+
}
|
|
379
|
+
|
|
277
380
|
box.on('status', (event) => {
|
|
278
381
|
if (this.closed || this.box !== box) {
|
|
279
382
|
return;
|
|
@@ -311,6 +414,22 @@ class LiteConnection {
|
|
|
311
414
|
return;
|
|
312
415
|
}
|
|
313
416
|
|
|
417
|
+
// B-44: occulite-client runs a credential without rpc:read names-only - no interface
|
|
418
|
+
// list, no stream, every call refused. Said as that, not as "no interface running";
|
|
419
|
+
// the client stays open (a shared names provider follows its copy).
|
|
420
|
+
if (box.mode === 'names-only') {
|
|
421
|
+
const missing = missingTiers(box.scopes);
|
|
422
|
+
const refused = new Error(
|
|
423
|
+
'the openccu-lite token lacks ' +
|
|
424
|
+
describeTiers(missing.length > 0 ? missing : ['rpc:read']) +
|
|
425
|
+
' - the system refuses the interfaces to it',
|
|
426
|
+
);
|
|
427
|
+
refused.kind = 'forbidden';
|
|
428
|
+
refused.scope = 'rpc:read';
|
|
429
|
+
refused.missing = missing.length > 0 ? missing : ['rpc:read'];
|
|
430
|
+
throw refused;
|
|
431
|
+
}
|
|
432
|
+
|
|
314
433
|
this.ready = true;
|
|
315
434
|
if (this.options.onValues) {
|
|
316
435
|
this.options.onValues(box.values);
|
|
@@ -339,6 +458,19 @@ class LiteConnection {
|
|
|
339
458
|
);
|
|
340
459
|
}
|
|
341
460
|
|
|
461
|
+
/**
|
|
462
|
+
* B-44: whether the credential may do what needs `scope`, as the system lists its scopes.
|
|
463
|
+
* @param {string} scope
|
|
464
|
+
* @returns {boolean} true while not known
|
|
465
|
+
*/
|
|
466
|
+
may(scope) {
|
|
467
|
+
if (!this.box || !Array.isArray(this.box.scopes) || this.box.scopes.length === 0) {
|
|
468
|
+
return true;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
return covers(this.box.scopes, scope);
|
|
472
|
+
}
|
|
473
|
+
|
|
342
474
|
/**
|
|
343
475
|
* Whether the system says this interface runs (the client's live list).
|
|
344
476
|
* @param {string} iface
|
|
@@ -357,6 +489,8 @@ class LiteConnection {
|
|
|
357
489
|
close() {
|
|
358
490
|
this.closed = true;
|
|
359
491
|
this.ready = false;
|
|
492
|
+
clearTimeout(this.retryTimeout);
|
|
493
|
+
this.retryTimeout = null;
|
|
360
494
|
if (this.box) {
|
|
361
495
|
this.box.close();
|
|
362
496
|
this.box = null;
|
|
@@ -370,8 +504,11 @@ module.exports = {
|
|
|
370
504
|
ADDON_TOKEN_FILE,
|
|
371
505
|
CALLBACK_METHODS,
|
|
372
506
|
readAddonToken,
|
|
507
|
+
covers,
|
|
508
|
+
missingTiers,
|
|
509
|
+
describeTiers,
|
|
510
|
+
tokenScopes,
|
|
373
511
|
liteValue,
|
|
374
|
-
tierAwareTransport,
|
|
375
512
|
trustOf,
|
|
376
513
|
translateError,
|
|
377
514
|
isFault,
|