feat: support TeamSpeak server nickname connections

This commit is contained in:
yumi committed 2026-10-08 23:48:47 +08:00
1 parent 35dde81cc2
commit 60d3cfd569
21 files changed
+644 -87

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
import assert from "node:assert/strict";
import test from "node:test";
import { combineTeamSpeakTarget, isValidTeamSpeakPort, splitTeamSpeakTarget, suggestedTeamSpeakPort } from "./teamspeak-target.js";
test("nickname fields round-trip without forcing a default port", () => {
for (const value of ["team eco", "another guild", "A+B & 50%", "literal%20name", "\u4e2d\u6587\u670d\u52a1\u5668"]) {
const fields = splitTeamSpeakTarget(value);
assert.deepEqual(fields, { address: value, port: "" });
assert.equal(combineTeamSpeakTarget(fields.address, fields.port), value);
const invitation = new URL("https://webspeak.example/");
invitation.searchParams.set("server", value);
assert.equal(splitTeamSpeakTarget(new URL(invitation.href).searchParams.get("server")).address, value);
}
assert.deepEqual(splitTeamSpeakTarget("guild:9987"), { address: "guild", port: "9987" });
assert.equal(combineTeamSpeakTarget("guild", "9987"), "guild:9987");
assert.equal(combineTeamSpeakTarget("guild:9988", ""), "guild:9988");
});
test("lookup URLs are decoded before combining address and port", () => {
assert.deepEqual(splitTeamSpeakTarget("https://named.myteamspeak.com/lookup?name=A%2BB+%26+50%25"), { address: "A+B & 50%", port: "" });
assert.equal(combineTeamSpeakTarget("https://named.myteamspeak.com/lookup?name=team%20eco", ""), "team eco");
assert.equal(combineTeamSpeakTarget("https://named.myteamspeak.com/lookup?name=team%20eco", "9987"), "team eco:9987");
const value = "https://named.myteamspeak.com/lookup?name=some.name%3A12#9988";
const fields = splitTeamSpeakTarget(value);
assert.equal(combineTeamSpeakTarget(fields.address, fields.port), value);
const explicit = "https://named.myteamspeak.com/lookup?name=teamspeak";
assert.deepEqual(splitTeamSpeakTarget(explicit), { address: explicit, port: "" });
assert.equal(combineTeamSpeakTarget(explicit, ""), explicit);
});
test("IP, DNS, IPv6 and legacy port fields retain their behavior", () => {
for (const [value, address, port] of [
["8.8.8.8", "8.8.8.8", "9987"],
["voice.example.com#9988", "voice.example.com", "9988"],
["[2606:4700:4700::1111]:9988", "2606:4700:4700::1111", "9988"],
]) {
assert.deepEqual(splitTeamSpeakTarget(value), { address, port });
const canonical = combineTeamSpeakTarget(address!, port!);
assert.deepEqual(splitTeamSpeakTarget(canonical), { address, port });
}
assert.deepEqual(splitTeamSpeakTarget("voice.example.com", "9999"), { address: "voice.example.com", port: "9999" });
assert.equal(combineTeamSpeakTarget("voice.example.com", ""), "voice.example.com:9987");
assert.equal(combineTeamSpeakTarget("[2606:4700:4700::1111]:9988", ""), "[2606:4700:4700::1111]:9988");
assert.equal(isValidTeamSpeakPort(""), true);
for (const port of ["0", "65536", "abc"]) assert.equal(isValidTeamSpeakPort(port), false);
});
test("changing address kind clears only the prefilled port and restores host defaults", () => {
assert.equal(suggestedTeamSpeakPort("voice.example.com", "team eco", "9987"), "");
assert.equal(suggestedTeamSpeakPort("voice.example.com", "team eco", "9999"), "9999");
assert.equal(suggestedTeamSpeakPort("team eco", "other guild", "9987"), "9987");
assert.equal(suggestedTeamSpeakPort("team eco", "voice.example.com", ""), "9987");
assert.equal(suggestedTeamSpeakPort("voice.example.com", "guild:9988", "9987"), "9988");
});
+44 -22
View File
@@ -1,3 +1,8 @@
import {
formatTeamSpeakConnectionTarget,
parseTeamSpeakConnectionTarget,
} from "../../../src/domain/teamspeak-connection-target.js";
export const DEFAULT_TEAM_SPEAK_PORT = "9987";
export interface TeamSpeakTargetFields {
@@ -10,39 +15,56 @@ export function splitTeamSpeakTarget(value: unknown, fallbackPort = DEFAULT_TEAM
const input = typeof value === "string" ? value.trim() : "";
if (!input) return { address: "", port: fallbackPort };
const hash = input.lastIndexOf("#");
if (hash > 0 && isPort(input.slice(hash + 1))) {
return { address: stripBrackets(input.slice(0, hash)), port: input.slice(hash + 1) };
}
if (input.startsWith("[")) {
const closingBracket = input.indexOf("]");
if (closingBracket > 0) {
const address = input.slice(1, closingBracket);
const suffix = input.slice(closingBracket + 1);
const port = suffix.startsWith(":") && isPort(suffix.slice(1)) ? suffix.slice(1) : fallbackPort;
return { address, port };
try {
const target = parseTeamSpeakConnectionTarget(input);
if (target.kind === "nickname") {
return { address: formatTeamSpeakConnectionTarget({ ...target, port: undefined }), port: target.port === undefined ? "" : String(target.port) };
}
const hasPort = input.includes("#") || /^\[[^\]]+\]:/.test(input) || /^[^:]+:/.test(input);
return { address: target.target.host, port: hasPort ? String(target.target.port) : fallbackPort };
} catch {
return { address: stripBrackets(input), port: fallbackPort };
}
const colon = input.lastIndexOf(":");
if (colon > 0 && input.indexOf(":") === colon && isPort(input.slice(colon + 1))) {
return { address: input.slice(0, colon), port: input.slice(colon + 1) };
}
return { address: stripBrackets(input), port: fallbackPort };
}
/** Build the canonical API value from the two user-facing fields. */
export function combineTeamSpeakTarget(address: string, port: string): string {
const normalizedAddress = stripBrackets(address.trim());
const normalizedPort = port.trim() || DEFAULT_TEAM_SPEAK_PORT;
const normalizedPort = port.trim();
if (!normalizedAddress) return "";
return normalizedAddress.includes(":") ? `[${normalizedAddress}]:${normalizedPort}` : `${normalizedAddress}:${normalizedPort}`;
const input = !normalizedAddress.startsWith("[") && normalizedAddress.indexOf(":") !== normalizedAddress.lastIndexOf(":") && !/^https?:\/\//i.test(normalizedAddress)
? `[${normalizedAddress}]`
: normalizedAddress;
try {
const target = parseTeamSpeakConnectionTarget(input);
if (normalizedPort) {
if (!isPort(normalizedPort)) throw new Error("Invalid port");
if (target.kind === "nickname") target.port = Number(normalizedPort);
else target.target.port = Number(normalizedPort);
}
return formatTeamSpeakConnectionTarget(target);
} catch {
return normalizedPort ? `${input}:${normalizedPort}` : input;
}
}
export function isValidTeamSpeakPort(value: string): boolean {
return isPort(value.trim());
return !value.trim() || isPort(value.trim());
}
/** Drop the prefilled default when switching to a nickname, retaining custom ports. */
export function suggestedTeamSpeakPort(previousAddress: string, address: string, port: string): string {
try {
const target = parseTeamSpeakConnectionTarget(address);
let previousWasNickname = false;
try { previousWasNickname = parseTeamSpeakConnectionTarget(previousAddress).kind === "nickname"; }
catch { /* An empty or incomplete field has no previous nickname. */ }
if (target.kind === "nickname" && !previousWasNickname && port === DEFAULT_TEAM_SPEAK_PORT) {
return target.port === undefined ? "" : String(target.port);
}
if (target.kind === "address" && previousWasNickname && !port.trim()) return String(target.target.port);
} catch { /* Keep the port while the address is being edited. */ }
return port;
}
function isPort(value: string): boolean {