mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
fix: keep avatar transfers on their original SDK connection
This commit is contained in:
1 parent
3dfd25dab7
commit
2047e30984
5 files changed
+142
-8
No files matched your search
@@ -0,0 +1,110 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { EventEmitter } from "node:events";
|
||||
import { setImmediate as nextTurn } from "node:timers/promises";
|
||||
import type { Writable } from "node:stream";
|
||||
import pino from "pino";
|
||||
import { TSClient } from "./ts-client.js";
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
const promise = new Promise<T>(yes => { resolve = yes; });
|
||||
return { promise, resolve };
|
||||
}
|
||||
|
||||
const rows = [{ client_unique_identifier: "member", client_flag_avatar: "marker", client_base64HashClientUID: "abcdefgh" }];
|
||||
const transfer = { size: 6n };
|
||||
|
||||
class SdkStub extends EventEmitter {
|
||||
initialized = 0;
|
||||
downloaded = 0;
|
||||
metadata = async () => rows;
|
||||
transfer = async () => transfer;
|
||||
download = async (destination: Writable) => { destination.write(Buffer.from("GIF89a")); };
|
||||
execCommandWithResponse() { return this.metadata(); }
|
||||
async fileTransferInitDownload() { this.initialized++; return this.transfer(); }
|
||||
async downloadFileData(_host: string, _transfer: unknown, destination: Writable) { this.downloaded++; await this.download(destination); }
|
||||
}
|
||||
|
||||
function fixture() {
|
||||
const client = new TSClient({ target: { host: "voice.example.invalid", port: 9987 }, nickname: "Test" }, pino({ enabled: false }));
|
||||
const sdk = new SdkStub();
|
||||
const state = client as unknown as { client: SdkStub | null; connected: boolean; attachClientListeners(sdk: SdkStub): void };
|
||||
state.client = sdk;
|
||||
state.connected = true;
|
||||
state.attachClientListeners(sdk);
|
||||
return { client, sdk, state };
|
||||
}
|
||||
|
||||
test("an old avatar metadata response cannot start a transfer on a replacement SDK client", async () => {
|
||||
const f = fixture();
|
||||
const pending = deferred<typeof rows>();
|
||||
f.sdk.metadata = () => pending.promise;
|
||||
const result = f.client.getClientAvatar(2, "member");
|
||||
const replacement = new SdkStub();
|
||||
f.state.client = replacement;
|
||||
pending.resolve(rows);
|
||||
assert.equal(await result, null);
|
||||
assert.equal(f.sdk.initialized, 0);
|
||||
assert.equal(replacement.initialized, 0);
|
||||
});
|
||||
|
||||
test("disconnect during avatar transfer initialization prevents the subsequent download", async () => {
|
||||
const f = fixture();
|
||||
const pending = deferred<typeof transfer>();
|
||||
f.sdk.transfer = () => pending.promise;
|
||||
const result = f.client.getClientAvatar(2, "member");
|
||||
await nextTurn();
|
||||
assert.equal(f.sdk.initialized, 1);
|
||||
await f.client.disconnect();
|
||||
pending.resolve(transfer);
|
||||
assert.equal(await result, null);
|
||||
assert.equal(f.sdk.downloaded, 0);
|
||||
});
|
||||
|
||||
test("a completed avatar download after disconnect is discarded", async () => {
|
||||
const f = fixture();
|
||||
const pending = deferred<void>();
|
||||
f.sdk.download = async destination => { destination.write(Buffer.from("GIF89a")); await pending.promise; };
|
||||
const result = f.client.getClientAvatar(2, "member");
|
||||
await nextTurn();
|
||||
assert.equal(f.sdk.downloaded, 1);
|
||||
await f.client.disconnect();
|
||||
pending.resolve();
|
||||
assert.equal(await result, null);
|
||||
});
|
||||
|
||||
test("reusing the SDK client after interruption cannot revive an earlier avatar request", async () => {
|
||||
const f = fixture();
|
||||
const pending = deferred<typeof rows>();
|
||||
f.sdk.metadata = () => pending.promise;
|
||||
const result = f.client.getClientAvatar(2, "member");
|
||||
f.sdk.emit("disconnected");
|
||||
f.state.connected = true;
|
||||
pending.resolve(rows);
|
||||
assert.equal(await result, null);
|
||||
assert.equal(f.sdk.initialized, 0);
|
||||
});
|
||||
|
||||
test("a current avatar download preserves its bytes and rejects a mismatched identity", async () => {
|
||||
const f = fixture();
|
||||
assert.deepEqual(await f.client.getClientAvatar(2, "member"), { cacheKey: "marker", data: Buffer.from("GIF89a") });
|
||||
assert.equal(await f.client.getClientAvatar(2, "different-member"), null);
|
||||
assert.equal(f.sdk.initialized, 1);
|
||||
assert.equal(f.sdk.downloaded, 1);
|
||||
});
|
||||
|
||||
test("a retired SDK client's disconnect cannot invalidate a current avatar request", async () => {
|
||||
const f = fixture();
|
||||
const replacement = new SdkStub();
|
||||
const pending = deferred<typeof rows>();
|
||||
replacement.metadata = () => pending.promise;
|
||||
f.state.client = replacement;
|
||||
f.state.attachClientListeners(replacement);
|
||||
const result = f.client.getClientAvatar(2, "member");
|
||||
f.sdk.emit("disconnected");
|
||||
assert.equal(f.client.isConnected(), true);
|
||||
pending.resolve(rows);
|
||||
assert.deepEqual(await result, { cacheKey: "marker", data: Buffer.from("GIF89a") });
|
||||
assert.equal(replacement.downloaded, 1);
|
||||
});
|
||||
+15
-4
@@ -86,6 +86,7 @@ export class TSClient extends EventEmitter {
|
||||
private readonly identity: Identity;
|
||||
private clientId = 0;
|
||||
private connected = false;
|
||||
private connectionGeneration = 0;
|
||||
private preferredChannelId = 0n;
|
||||
private accelerationClient: AccelerationRelayClient | null = null;
|
||||
// Reason id of the most recent self leave, kept so the SDK `kicked` event can
|
||||
@@ -100,6 +101,7 @@ export class TSClient extends EventEmitter {
|
||||
}
|
||||
|
||||
async connect(): Promise<void> {
|
||||
this.connectionGeneration++;
|
||||
this.selfLeaveReasonId = null;
|
||||
if (!this.adapter || !this.client) {
|
||||
let transportTarget = this.options.target;
|
||||
@@ -197,9 +199,13 @@ export class TSClient extends EventEmitter {
|
||||
* keep its generated initial avatar.
|
||||
*/
|
||||
async getClientAvatar(clientId: number, expectedUid = ""): Promise<TSClientAvatar | null> {
|
||||
if (!this.client || !this.connected) throw new Error("TeamSpeak session is not ready");
|
||||
const client = this.client;
|
||||
if (!client || !this.connected) throw new Error("TeamSpeak session is not ready");
|
||||
if (!Number.isInteger(clientId) || clientId <= 0 || clientId > 65535) throw new Error("Invalid TeamSpeak client id");
|
||||
const rows = await this.client.execCommandWithResponse(`clientinfo clid=${clientId}`, 5_000);
|
||||
const generation = this.connectionGeneration;
|
||||
const isCurrent = () => this.client === client && this.connected && this.connectionGeneration === generation;
|
||||
const rows = await client.execCommandWithResponse(`clientinfo clid=${clientId}`, 5_000);
|
||||
if (!isCurrent()) return null;
|
||||
const info = rows.find((row) => {
|
||||
if (expectedUid && row.client_unique_identifier !== expectedUid) return false;
|
||||
return typeof row.client_flag_avatar === "string" && typeof row.client_base64HashClientUID === "string";
|
||||
@@ -209,7 +215,8 @@ export class TSClient extends EventEmitter {
|
||||
const avatarFileKey = info.client_base64HashClientUID?.trim() ?? "";
|
||||
if (!cacheKey || !/^[A-Za-z0-9+/=_-]{8,256}$/.test(avatarFileKey)) return null;
|
||||
|
||||
const transfer = await this.client.fileTransferInitDownload(0n, `/avatar_${avatarFileKey}`, "");
|
||||
const transfer = await client.fileTransferInitDownload(0n, `/avatar_${avatarFileKey}`, "");
|
||||
if (!isCurrent()) return null;
|
||||
const size = Number(transfer.size);
|
||||
if (!Number.isSafeInteger(size) || size <= 0 || size > MAX_CLIENT_AVATAR_BYTES) return null;
|
||||
|
||||
@@ -220,7 +227,8 @@ export class TSClient extends EventEmitter {
|
||||
callback();
|
||||
},
|
||||
});
|
||||
await this.client.downloadFileData(this.options.target.host, transfer, destination);
|
||||
await client.downloadFileData(this.options.target.host, transfer, destination);
|
||||
if (!isCurrent()) return null;
|
||||
const data = Buffer.concat(chunks);
|
||||
if (data.length === 0 || data.length > MAX_CLIENT_AVATAR_BYTES) return null;
|
||||
return { cacheKey, data };
|
||||
@@ -261,6 +269,8 @@ export class TSClient extends EventEmitter {
|
||||
});
|
||||
|
||||
client.on("disconnected", (err) => {
|
||||
if (this.client !== client) return;
|
||||
this.connectionGeneration++;
|
||||
if (err) {
|
||||
const normalized = normalizeTeamSpeakError(err);
|
||||
this.logger.warn({
|
||||
@@ -395,6 +405,7 @@ export class TSClient extends EventEmitter {
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
this.connectionGeneration++;
|
||||
this.connected = false;
|
||||
this.selfLeaveReasonId = null;
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user