server protocol & ports UX improvement

This commit is contained in:
2026-09-03 18:40:37 +08:00
parent 0840abb466
commit 0cad78718a
11 changed files with 317 additions and 72 deletions
+64
View File
@@ -0,0 +1,64 @@
export type ServerConnectionType = "https" | "http" | "direct";
const SCHEME = /^(https?|wss?):\/\//i;
export function connectionTypeFromScheme(scheme: string): ServerConnectionType {
return scheme.toLowerCase().startsWith("https") ||
scheme.toLowerCase().startsWith("wss")
? "https"
: "http";
}
export function splitServerAddress(address: string): {
address: string;
connectionType: ServerConnectionType;
} {
const match = address.match(SCHEME);
if (!match) return { address, connectionType: "direct" };
return {
address: address.slice(match[0].length),
connectionType: connectionTypeFromScheme(match[1]),
};
}
export function storedServerAddress(
address: string,
connectionType: ServerConnectionType,
): string {
const host = address.trim();
if (connectionType === "direct") return host;
return `${connectionType}://${host}`;
}
export function containsScheme(address: string): boolean {
return SCHEME.test(address.trim());
}
export function pastedServerAddress(value: string): {
address: string;
connectionType: ServerConnectionType;
port: string;
} | null {
const trimmed = value.trim();
if (!SCHEME.test(trimmed)) return null;
try {
const url = new URL(trimmed);
if (
url.username ||
url.password ||
(url.pathname !== "/" && url.pathname !== "") ||
url.search ||
url.hash
) {
return null;
}
return {
address: url.hostname,
connectionType: connectionTypeFromScheme(url.protocol),
port: url.port,
};
} catch {
return null;
}
}