fix complete directory sync for late web clients

This commit is contained in:
illusia committed 2026-09-01 23:32:15 +08:00
1 parent aa5c5e687f
commit d7766497e8
3 files changed
+48 -10

No files matched your search

+8 -5
View File
@@ -7,9 +7,10 @@ type DirectoryDelta =
| { type: "clientMoved"; id: number; channelID: bigint };
/**
* Merges the welcome snapshot with events received while that snapshot is in
* flight. Each TeamSpeak connection owns its own instance, so no directory
* event can leak into another browser session.
* Merges staged welcome snapshots with events received while the snapshot is
* in flight. Some TS6 welcome data arrives in more than one partial snapshot,
* so a later snapshot must not hide clients that were present in an earlier
* one. Explicit leave events remain authoritative removals.
*/
export class DirectorySynchronizer {
private snapshot: TSDirectorySnapshot | null = null;
@@ -22,8 +23,10 @@ export class DirectorySynchronizer {
applySnapshot(snapshot: TSDirectorySnapshot): void {
this.snapshot = { channels: snapshot.channels.slice(), clients: [] };
this.clients.clear();
for (const client of snapshot.clients) this.clients.set(client.id, client);
for (const client of snapshot.clients) {
const previous = this.clients.get(client.id);
this.clients.set(client.id, previous ? { ...previous, ...client } : client);
}
const pending = this.pending.splice(0);
for (const delta of pending) this.applyDelta(delta);
}
+27 -5
View File
@@ -2,6 +2,8 @@ import { EventEmitter } from "node:events";
import {
Client as TS3FullClient,
clientMove as tsClientMove,
listChannels as tsListChannels,
listClients as tsListClients,
poke as tsPoke,
sendTextMessage as tsSendTextMessage,
generateIdentity,
@@ -116,11 +118,28 @@ export class TSClient extends EventEmitter {
}, "Could not subscribe to all TeamSpeak channels");
}
// Directory snapshots are dispatched through two setImmediate layers in
// the SDK. Let both flush so the gateway's first connected state already
// contains members from every subscribed channel.
// the SDK. Let both flush before reconciling with a direct client-protocol
// snapshot so the gateway's first connected state contains every member.
await new Promise<void>((resolve) => setImmediate(resolve));
await new Promise<void>((resolve) => setImmediate(resolve));
// TS6 can finish the welcome event stream before it has exposed every
// existing client to a newly connected session. Pull one authoritative
// snapshot through this same client protocol session so users who joined
// earlier are visible immediately as well. Realtime events continue to
// keep the snapshot current after this reconciliation.
try {
const [channels, clients] = await Promise.all([
tsListChannels(client),
tsListClients(client),
]);
this.emit("directorySnapshot", { channels, clients });
} catch (error: unknown) {
this.logger.warn({
err: error instanceof Error ? error.message : String(error),
}, "Could not reconcile the TeamSpeak directory snapshot");
}
const connectedChannelId = client.channelID();
if (this.preferredChannelId === 0n) {
this.preferredChannelId = connectedChannelId;
@@ -154,12 +173,15 @@ export class TSClient extends EventEmitter {
});
// The SDK patch exposes the directory that TeamSpeak sends during the
// normal client welcome sequence. This is the same data a native client
// receives and does not require channellist/clientlist permissions.
// normal client welcome sequence. The connect path also reconciles this
// event stream with a complete client-protocol snapshot.
(client as unknown as {
on(event: "directorySnapshot", handler: (snapshot: TSDirectorySnapshot) => void): unknown;
}).on("directorySnapshot", (snapshot) => {
this.emit("directorySnapshot", snapshot);
this.emit("directorySnapshot", {
channels: snapshot.channels.slice(),
clients: snapshot.clients.slice(),
});
});
client.on("textMessage", (msg) => {
+13
View File
@@ -31,3 +31,16 @@ test("directory state and movement events remain isolated per session", () => {
assert.deepEqual(first.getSnapshot()?.clients.map((item) => [item.id, item.channelID]), [[3, 2n]]);
assert.deepEqual(second.getSnapshot()?.clients.map((item) => [item.id, item.channelID]), [[3, 1n]]);
});
test("directory synchronizer keeps clients across staged partial snapshots", () => {
const sync = new DirectorySynchronizer();
const alice = client(7, "Alice", 1n);
const bob = client(8, "Bob", 1n);
sync.applySnapshot({ channels: [root], clients: [alice, bob] });
sync.applySnapshot({ channels: [root], clients: [bob] });
assert.deepEqual(sync.getSnapshot()?.clients.map((item) => item.id), [7, 8]);
sync.applyClientLeave(alice.id);
assert.deepEqual(sync.getSnapshot()?.clients.map((item) => item.id), [8]);
});