fix(webui): connection-diff-loop compares ONLY level — strip detail timestamps
Adversarial review caught: HttpPostAdapter.describeStatus() embeds
HH:MM:SS in 'detail' (e.g. "上次推送 14:53:01"). Every webhook delivery
rotates that string every second under any active push — and the diff
loop's raw JSON.stringify compared everything, so it published on every
500ms tick under any non-idle webhook. Net effect: SSE pushed a full
connections frame at 2 Hz, exactly the polling the loop was meant to
replace, just on the push channel.
Add an optional pickComparable projector. server.ts caller passes one
that returns { uin, nickname, adapters[{ name, kind, status }] } — the
LEVEL only, not the live-rendered detail string. The SSE handler still
ships the FULL snapshot (including detail) to the WebUI; only the diff
key is pruned.
2 new tests pin the invariant: 10 detail-only ticks → ZERO publishes; a
real status-level transition publishes exactly once. Plus a throwing-
projector isolation test. 7/7 diff-loop tests green.
This commit is contained in:
4 files changed
+96
-3
No files matched your search
@@ -335,10 +335,31 @@ export async function initWebUI(
|
||||
// listening/connected/client-count changes, so we poll the snapshot at
|
||||
// 500ms and publish 'connections' to the StateBus when it actually
|
||||
// moves. One loop per server lifetime, lives until process exit.
|
||||
//
|
||||
// `pickComparable` strips `adapter.detail` — that's a localised human
|
||||
// string assembled with HH:MM:SS timestamps from the last webhook
|
||||
// delivery; including it in the diff would make every webhook event
|
||||
// produce a publish (defeating the diff). We compare ONLY name + kind +
|
||||
// status, which captures every transition the dashboard actually
|
||||
// renders differently. The SSE handler still ships the FULL snapshot
|
||||
// (with `detail`) — it's only the diff key that's pruned.
|
||||
if (listener.stateBus) {
|
||||
startConnectionDiffLoop({
|
||||
bus: listener.stateBus,
|
||||
getSnapshot: () => oneBotManager.getConnectionStatuses(),
|
||||
pickComparable: (snap) => {
|
||||
if (!Array.isArray(snap)) return snap;
|
||||
return (snap as Array<{ uin: string; nickname?: string; adapters?: unknown[] }>).map((acc) => ({
|
||||
uin: acc.uin,
|
||||
nickname: acc.nickname,
|
||||
adapters: Array.isArray(acc.adapters)
|
||||
? acc.adapters.map((a: unknown) => {
|
||||
const o = a as { name?: string; kind?: string; status?: string };
|
||||
return { name: o.name, kind: o.kind, status: o.status };
|
||||
})
|
||||
: acc.adapters,
|
||||
}));
|
||||
},
|
||||
intervalMs: 500,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user