@camstack/server 1.2.176 → 1.2.178
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/dist/agent/agent-addon-updates.js +181 -0
- package/dist/agent/agent-http.js +5 -0
- package/dist/agent/agent-update-routes.js +101 -0
- package/dist/agent/main.js +26 -0
- package/dist/api/core/cap-providers.js +9 -2
- package/dist/api/core/logging-settings.js +332 -36
- package/dist/api/core/request-census-settings.js +11 -6
- package/dist/api/trpc/core-cap-bridge.js +64 -3
- package/dist/api/trpc/device-config-secret-redaction.js +19 -0
- package/dist/api/trpc/generated-cap-mounts.js +17 -1
- package/dist/api/trpc/generated-cap-routers.js +1103 -1054
- package/dist/api/trpc/http-request-census.js +201 -38
- package/dist/api/trpc/ws-request-census.js +300 -0
- package/dist/core/addon/addon-registry.service.js +33 -109
- package/dist/core/addon/device-meta-mirror.js +239 -0
- package/dist/core/addon/single-flight-refresh.js +29 -16
- package/dist/main.js +19 -7
- package/package.json +1 -1
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The hub-process mirror of every device's persisted `parentDeviceId`, `type`
|
|
4
|
+
* and `location` — and the rule that decides how much of the fleet a device
|
|
5
|
+
* event is allowed to make it re-read.
|
|
6
|
+
*
|
|
7
|
+
* ## Why the mirror exists at all
|
|
8
|
+
*
|
|
9
|
+
* The scope matcher's parent→child accessory inheritance (`getDeviceAncestors`
|
|
10
|
+
* in `trpc.context.ts`) needs a deviceId's ancestor chain on EVERY scoped
|
|
11
|
+
* request, synchronously. The hub `DeviceRegistry` only knows devices
|
|
12
|
+
* registered IN the hub process, so it is EMPTY for every forked provider
|
|
13
|
+
* (reolink, hikvision, …): a grant on a forked camera never reached its
|
|
14
|
+
* siren/floodlight/PIR children (measured 0/6 providers inherited). The
|
|
15
|
+
* authoritative parentage lives in the device-manager's persisted meta.
|
|
16
|
+
*
|
|
17
|
+
* ## The number this file exists to explain
|
|
18
|
+
*
|
|
19
|
+
* `FleetReadCensus` on the live hub, 120-second boot window, 2026-08-27
|
|
20
|
+
* (four consecutive boots, 4 886 / 4 904 / 4 910 / 4 960):
|
|
21
|
+
*
|
|
22
|
+
* site DeviceRowStore.list < DeviceRowStore.listFleet < listAll
|
|
23
|
+
* calls 4 904 rows 4 987 368 perMin 2 451
|
|
24
|
+
*
|
|
25
|
+
* 4 987 368 ÷ 4 904 = **1 017.0** — the whole fleet, every single call. The
|
|
26
|
+
* same census puts the boot's device count at 1 017 and the per-device chains
|
|
27
|
+
* (`allocateDeviceId`, `getChildren`) at ~1 024 calls each, so this site is
|
|
28
|
+
* ~4.8 reads per device.
|
|
29
|
+
*
|
|
30
|
+
* The refresh was already single-flighted (`single-flight-refresh.ts`) and
|
|
31
|
+
* that was not enough, because single-flight bounds CONCURRENCY, not RATE: the
|
|
32
|
+
* follow-up starts the instant the previous run ends, so a burst that keeps
|
|
33
|
+
* producing events for two minutes produces rebuilds for two minutes. The
|
|
34
|
+
* missing bound is not a timer — it is the observation that **a mirror of
|
|
35
|
+
* three fields must not re-read a thousand rows because one device's `name`
|
|
36
|
+
* changed.** Two rules, both here:
|
|
37
|
+
*
|
|
38
|
+
* 1. An event that names a meta `field` this mirror does not hold changes
|
|
39
|
+
* nothing, and asks for nothing ({@link MIRROR_IRRELEVANT_META_FIELDS}).
|
|
40
|
+
* 2. An event that does change something changes it for ONE device, so the
|
|
41
|
+
* re-read is `listAll({ deviceIds: [that one] })` — the narrow read
|
|
42
|
+
* `device-queries.ts#listAll` has supported since the linked-devices panel
|
|
43
|
+
* needed it. Only the boot warm (`SystemAddonsReady`) reads the fleet.
|
|
44
|
+
*
|
|
45
|
+
* ## Why the irrelevant-field list is a DENY list
|
|
46
|
+
*
|
|
47
|
+
* The tempting shape is an allow list of the fields the mirror holds
|
|
48
|
+
* (`location`, `type`). It is the wrong direction: a field added to
|
|
49
|
+
* `device-meta-actions.ts` later would silently stop being mirrored, and the
|
|
50
|
+
* symptom is a grant that quietly does not apply. A deny list fails the other
|
|
51
|
+
* way — an unrecognised field costs ONE row and stays correct. The list only
|
|
52
|
+
* ever needs to grow, and growing it late costs a row, not a bug.
|
|
53
|
+
*
|
|
54
|
+
* ## What a scoped refresh must still do
|
|
55
|
+
*
|
|
56
|
+
* A full rebuild removed a device that had disappeared, because it replaced
|
|
57
|
+
* the map. A scoped one must do that explicitly: an id that was asked for and
|
|
58
|
+
* did NOT come back is gone from the mirror — never a fabricated row, the same
|
|
59
|
+
* contract `DeviceRowStore.getMany` states for a fleet it does not know. A
|
|
60
|
+
* read that THROWS is not that: it changes nothing at all (D49).
|
|
61
|
+
*/
|
|
62
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
63
|
+
exports.DeviceMetaMirror = exports.MIRROR_IRRELEVANT_META_FIELDS = void 0;
|
|
64
|
+
exports.classifyDeviceEvent = classifyDeviceEvent;
|
|
65
|
+
const types_1 = require("@camstack/types");
|
|
66
|
+
const single_flight_refresh_js_1 = require("./single-flight-refresh.js");
|
|
67
|
+
/**
|
|
68
|
+
* Meta fields a `DeviceMetaChanged` can name that this mirror provably does
|
|
69
|
+
* not hold. Enumerated from every `field:` literal `device-meta-actions.ts`
|
|
70
|
+
* emits; `location` and `type` are deliberately ABSENT because those two are
|
|
71
|
+
* the mirror.
|
|
72
|
+
*/
|
|
73
|
+
exports.MIRROR_IRRELEVANT_META_FIELDS = new Set([
|
|
74
|
+
'name',
|
|
75
|
+
'disabled',
|
|
76
|
+
'metadata',
|
|
77
|
+
'display',
|
|
78
|
+
'role',
|
|
79
|
+
'integrationId',
|
|
80
|
+
'linkDeviceId',
|
|
81
|
+
'primaryChildEntityId',
|
|
82
|
+
'childLayout',
|
|
83
|
+
]);
|
|
84
|
+
/**
|
|
85
|
+
* Classify one device-lifecycle event's payload.
|
|
86
|
+
*
|
|
87
|
+
* `none` only for a payload that NAMES a field the mirror does not hold —
|
|
88
|
+
* that is a claim about the event, not a guess about the device. A payload
|
|
89
|
+
* this cannot read at all falls back to `fleet`: an unparseable event must
|
|
90
|
+
* never silently skip a refresh, because the failure mode is an authorisation
|
|
91
|
+
* mirror that quietly stops converging.
|
|
92
|
+
*/
|
|
93
|
+
function classifyDeviceEvent(data) {
|
|
94
|
+
if (data === null || typeof data !== 'object')
|
|
95
|
+
return { kind: 'fleet' };
|
|
96
|
+
const field = Reflect.get(data, 'field');
|
|
97
|
+
if (typeof field === 'string' && exports.MIRROR_IRRELEVANT_META_FIELDS.has(field)) {
|
|
98
|
+
return { kind: 'none' };
|
|
99
|
+
}
|
|
100
|
+
const deviceId = Reflect.get(data, 'deviceId');
|
|
101
|
+
if (typeof deviceId !== 'number' || !Number.isFinite(deviceId))
|
|
102
|
+
return { kind: 'fleet' };
|
|
103
|
+
return { kind: 'device', deviceId };
|
|
104
|
+
}
|
|
105
|
+
/** Max ancestor hops — defence-in-depth against a corrupt parent cycle. */
|
|
106
|
+
const MAX_ANCESTOR_HOPS = 8;
|
|
107
|
+
class DeviceMetaMirror {
|
|
108
|
+
read;
|
|
109
|
+
live;
|
|
110
|
+
logger;
|
|
111
|
+
parents = new Map();
|
|
112
|
+
meta = new Map();
|
|
113
|
+
gate;
|
|
114
|
+
constructor(read, live, logger) {
|
|
115
|
+
this.read = read;
|
|
116
|
+
this.live = live;
|
|
117
|
+
this.logger = logger;
|
|
118
|
+
this.gate = (0, single_flight_refresh_js_1.createSingleFlightRefresh)((scope) => this.refresh(scope));
|
|
119
|
+
}
|
|
120
|
+
/** Ask for a refresh covering exactly what the event changed. */
|
|
121
|
+
onDeviceEvent(data) {
|
|
122
|
+
const scope = classifyDeviceEvent(data);
|
|
123
|
+
if (scope.kind === 'none')
|
|
124
|
+
return;
|
|
125
|
+
void this.gate.request(scope.kind === 'fleet' ? null : new Set([scope.deviceId]));
|
|
126
|
+
}
|
|
127
|
+
/** Warm (or re-warm) the whole fleet — the boot sweep, and the D8 reconcile
|
|
128
|
+
* that repairs anything a dropped event would have left behind. */
|
|
129
|
+
async refreshAll() {
|
|
130
|
+
await this.gate.request(null);
|
|
131
|
+
}
|
|
132
|
+
/** Parent of a device: mirror first (covers forked devices), then the live
|
|
133
|
+
* hub registry (covers a hub-local device before the first warm). */
|
|
134
|
+
parentOf(deviceId) {
|
|
135
|
+
const mirrored = this.parents.get(deviceId);
|
|
136
|
+
if (mirrored !== undefined)
|
|
137
|
+
return mirrored;
|
|
138
|
+
return this.live.parentDeviceId(deviceId);
|
|
139
|
+
}
|
|
140
|
+
/** Persisted `DeviceType` string, or null when unknown. */
|
|
141
|
+
typeOf(deviceId) {
|
|
142
|
+
const mirrored = this.meta.get(deviceId);
|
|
143
|
+
if (mirrored !== undefined)
|
|
144
|
+
return mirrored.type;
|
|
145
|
+
return this.live.type(deviceId);
|
|
146
|
+
}
|
|
147
|
+
/** Persisted operator `location` label, or null when unset/unknown. */
|
|
148
|
+
locationOf(deviceId) {
|
|
149
|
+
const mirrored = this.meta.get(deviceId);
|
|
150
|
+
if (mirrored !== undefined)
|
|
151
|
+
return mirrored.location;
|
|
152
|
+
return this.live.location(deviceId);
|
|
153
|
+
}
|
|
154
|
+
/** The whole mirrored fleet — fuels fleet-wide selector expansion. */
|
|
155
|
+
list() {
|
|
156
|
+
const out = [];
|
|
157
|
+
for (const [id, m] of this.meta) {
|
|
158
|
+
out.push({
|
|
159
|
+
id,
|
|
160
|
+
type: m.type,
|
|
161
|
+
location: m.location,
|
|
162
|
+
parentDeviceId: this.parents.get(id) ?? null,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return out;
|
|
166
|
+
}
|
|
167
|
+
/** Ancestor chain (parent, grandparent, …), bounded. Empty for a top-level
|
|
168
|
+
* device or one the hub has never heard of. */
|
|
169
|
+
ancestorsOf(deviceId) {
|
|
170
|
+
const out = [];
|
|
171
|
+
let current = deviceId;
|
|
172
|
+
for (let hop = 0; hop < MAX_ANCESTOR_HOPS; hop++) {
|
|
173
|
+
const parent = this.parentOf(current);
|
|
174
|
+
if (parent === null || parent === current)
|
|
175
|
+
break;
|
|
176
|
+
out.push(parent);
|
|
177
|
+
current = parent;
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
async refresh(scope) {
|
|
182
|
+
try {
|
|
183
|
+
const ids = scope === null ? null : [...scope];
|
|
184
|
+
const rows = await this.read(ids);
|
|
185
|
+
if (!Array.isArray(rows))
|
|
186
|
+
return;
|
|
187
|
+
const nextParents = new Map();
|
|
188
|
+
const nextMeta = new Map();
|
|
189
|
+
for (const row of rows) {
|
|
190
|
+
if (row === null || typeof row !== 'object')
|
|
191
|
+
continue;
|
|
192
|
+
const id = Reflect.get(row, 'id');
|
|
193
|
+
if (typeof id !== 'number')
|
|
194
|
+
continue;
|
|
195
|
+
const parent = Reflect.get(row, 'parentDeviceId');
|
|
196
|
+
if (typeof parent === 'number')
|
|
197
|
+
nextParents.set(id, parent);
|
|
198
|
+
const type = Reflect.get(row, 'type');
|
|
199
|
+
const location = Reflect.get(row, 'location');
|
|
200
|
+
nextMeta.set(id, {
|
|
201
|
+
type: typeof type === 'string' ? type : '',
|
|
202
|
+
location: typeof location === 'string' ? location : null,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
if (ids === null) {
|
|
206
|
+
this.parents.clear();
|
|
207
|
+
for (const [k, v] of nextParents)
|
|
208
|
+
this.parents.set(k, v);
|
|
209
|
+
this.meta.clear();
|
|
210
|
+
for (const [k, v] of nextMeta)
|
|
211
|
+
this.meta.set(k, v);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
// Scoped: apply exactly the ids that were ASKED for. An id the fleet did
|
|
215
|
+
// not answer with is gone — the one thing the full rebuild used to do
|
|
216
|
+
// implicitly by replacing the map.
|
|
217
|
+
for (const id of ids) {
|
|
218
|
+
const m = nextMeta.get(id);
|
|
219
|
+
if (m === undefined) {
|
|
220
|
+
this.parents.delete(id);
|
|
221
|
+
this.meta.delete(id);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
this.meta.set(id, m);
|
|
225
|
+
const parent = nextParents.get(id);
|
|
226
|
+
if (parent === undefined)
|
|
227
|
+
this.parents.delete(id);
|
|
228
|
+
else
|
|
229
|
+
this.parents.set(id, parent);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
this.logger.debug('device-meta mirror refresh failed — keeping previous', {
|
|
234
|
+
meta: { error: (0, types_1.errMsg)(err), scope: scope === null ? 'fleet' : scope.size },
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
exports.DeviceMetaMirror = DeviceMetaMirror;
|
|
@@ -1,32 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createSingleFlightRefresh = createSingleFlightRefresh;
|
|
4
|
+
/** Union of two scopes. `null` (everything) absorbs. */
|
|
5
|
+
function widenScope(a, b) {
|
|
6
|
+
if (a === null || b === null)
|
|
7
|
+
return null;
|
|
8
|
+
const out = new Set(a);
|
|
9
|
+
for (const id of b)
|
|
10
|
+
out.add(id);
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
4
13
|
/**
|
|
5
|
-
* @param run the
|
|
6
|
-
* with itself.
|
|
14
|
+
* @param run the rebuild, given the scope it must cover. Never called
|
|
15
|
+
* concurrently with itself, and never called with an empty set.
|
|
7
16
|
*/
|
|
8
17
|
function createSingleFlightRefresh(run) {
|
|
9
18
|
let inFlight = null;
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
// them, because the follow-up reads the
|
|
13
|
-
|
|
19
|
+
// The union of everything requested while a run is active — `undefined` when
|
|
20
|
+
// nothing is pending. One accumulator, not a queue: N requests during one run
|
|
21
|
+
// need exactly ONE follow-up between them, because the follow-up reads the
|
|
22
|
+
// world as it is when it starts.
|
|
23
|
+
let pendingScope;
|
|
14
24
|
let pendingPromise = null;
|
|
15
25
|
let releasePending = () => { };
|
|
16
|
-
const start = () => {
|
|
26
|
+
const start = (scope) => {
|
|
17
27
|
const active = (async () => {
|
|
18
28
|
try {
|
|
19
|
-
await run();
|
|
29
|
+
await run(scope);
|
|
20
30
|
}
|
|
21
31
|
finally {
|
|
22
32
|
inFlight = null;
|
|
23
|
-
if (
|
|
24
|
-
|
|
33
|
+
if (pendingScope !== undefined) {
|
|
34
|
+
const next = pendingScope;
|
|
35
|
+
pendingScope = undefined;
|
|
25
36
|
const release = releasePending;
|
|
26
37
|
releasePending = () => { };
|
|
27
38
|
pendingPromise = null;
|
|
28
39
|
// Chain the follow-up, then settle everyone who waited for it.
|
|
29
|
-
void start().finally(release);
|
|
40
|
+
void start(next).finally(release);
|
|
30
41
|
}
|
|
31
42
|
}
|
|
32
43
|
})();
|
|
@@ -34,18 +45,20 @@ function createSingleFlightRefresh(run) {
|
|
|
34
45
|
return active;
|
|
35
46
|
};
|
|
36
47
|
return {
|
|
37
|
-
request: async () => {
|
|
48
|
+
request: async (scope) => {
|
|
49
|
+
if (scope !== null && scope.size === 0)
|
|
50
|
+
return;
|
|
38
51
|
if (inFlight === null) {
|
|
39
|
-
await start();
|
|
52
|
+
await start(scope);
|
|
40
53
|
return;
|
|
41
54
|
}
|
|
42
|
-
|
|
43
|
-
|
|
55
|
+
pendingScope = pendingScope === undefined ? scope : widenScope(pendingScope, scope);
|
|
56
|
+
if (pendingPromise === null) {
|
|
44
57
|
pendingPromise = new Promise((resolve) => {
|
|
45
58
|
releasePending = resolve;
|
|
46
59
|
});
|
|
47
60
|
}
|
|
48
|
-
// `pendingPromise` is non-null whenever
|
|
61
|
+
// `pendingPromise` is non-null whenever a scope is pending; the local
|
|
49
62
|
// binding keeps that visible to the type checker after the await.
|
|
50
63
|
const wait = pendingPromise;
|
|
51
64
|
if (wait !== null)
|
package/dist/main.js
CHANGED
|
@@ -75,6 +75,7 @@ const http_request_census_singleton_1 = require("./api/trpc/http-request-census.
|
|
|
75
75
|
const trpc_context_1 = require("./api/trpc/trpc.context");
|
|
76
76
|
const trpc_router_1 = require("./api/trpc/trpc.router");
|
|
77
77
|
const trpc_error_principal_1 = require("./api/trpc/trpc-error-principal");
|
|
78
|
+
const ws_request_census_1 = require("./api/trpc/ws-request-census");
|
|
78
79
|
const addon_route_jwt_gate_js_1 = require("./auth/addon-route-jwt-gate.js");
|
|
79
80
|
const session_cookie_js_1 = require("./auth/session-cookie.js");
|
|
80
81
|
const boot_config_1 = require("./boot/boot-config");
|
|
@@ -1087,16 +1088,27 @@ async function bootstrap() {
|
|
|
1087
1088
|
concurrencyLimit: 10,
|
|
1088
1089
|
},
|
|
1089
1090
|
});
|
|
1091
|
+
// The WS plane of the transport census, hooked PER OPERATION.
|
|
1092
|
+
//
|
|
1093
|
+
// It cannot hang off `createContext` below: that factory runs once per
|
|
1094
|
+
// CONNECTION and is reused for every frame the socket ever sends, so a
|
|
1095
|
+
// census wired there fires once every 37 connections instead of once every
|
|
1096
|
+
// call. The frame is the only place an operation is observable from
|
|
1097
|
+
// outside the adapter — see `ws-request-census.ts`. Registered before
|
|
1098
|
+
// `applyWSSHandler` so no socket can be accepted without a session.
|
|
1099
|
+
wss.on('connection', (client, req) => {
|
|
1100
|
+
(0, ws_request_census_1.attachWsCensusSession)(httpRequestCensus, client, req, app.get(logging_service_1.LoggingService).createLogger('tRPC:ws'));
|
|
1101
|
+
});
|
|
1090
1102
|
(0, ws_1.applyWSSHandler)({
|
|
1091
1103
|
wss,
|
|
1092
1104
|
router: appRouter,
|
|
1093
|
-
createContext: (opts) => {
|
|
1094
|
-
|
|
1095
|
-
//
|
|
1096
|
-
//
|
|
1097
|
-
//
|
|
1098
|
-
|
|
1099
|
-
return
|
|
1105
|
+
createContext: async (opts) => {
|
|
1106
|
+
const wsCtx = await (0, trpc_context_1.createWsTrpcContext)(opts, authService, addonRegistry, shareTokenService, app.get(logging_service_1.LoggingService).createLogger('tRPC:ws'));
|
|
1107
|
+
// The connection's identity, derived ONCE and reused for every
|
|
1108
|
+
// operation on this socket. Frames that arrived while the bearer was
|
|
1109
|
+
// still resolving are held by the session and released here.
|
|
1110
|
+
(0, ws_request_census_1.identifyWsCensusSession)(opts.res, (0, trpc_error_principal_1.describeTrpcPrincipal)(wsCtx.user));
|
|
1111
|
+
return wsCtx;
|
|
1100
1112
|
},
|
|
1101
1113
|
onError: ({ path: trpcPath, error, ctx, }) => {
|
|
1102
1114
|
const trpcLogger = app.get(logging_service_1.LoggingService).createLogger('tRPC:ws');
|