feat(gaussdb): support multi-host connections
This commit is contained in:
1 parent
c0a6d6f2f9
commit
336fbe094b
16 files changed
+358
-25
No files matched your search
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseGaussdbHosts, serializeGaussdbHosts } from "@/lib/connection/gaussdbHosts";
|
||||
|
||||
describe("GaussDB hosts", () => {
|
||||
it("keeps a single host and port in separate fields", () => {
|
||||
expect(serializeGaussdbHosts([{ host: "db.example.com", port: 5433 }])).toEqual({ host: "db.example.com", port: 5433 });
|
||||
});
|
||||
|
||||
it("normalizes a legacy single host:port value", () => {
|
||||
expect(parseGaussdbHosts("db.example.com:5433", 5432)).toEqual([{ host: "db.example.com", port: 5433 }]);
|
||||
});
|
||||
|
||||
it("serializes multiple hosts with embedded ports", () => {
|
||||
expect(
|
||||
serializeGaussdbHosts([
|
||||
{ host: "db1", port: 5432 },
|
||||
{ host: "db2", port: 5433 },
|
||||
]),
|
||||
).toEqual({ host: "db1:5432,db2:5433", port: 5432 });
|
||||
});
|
||||
|
||||
it("round-trips bracketed IPv6 endpoints", () => {
|
||||
const entries = parseGaussdbHosts("[2001:db8::1]:5433,[2001:db8::2]:5434", 5432);
|
||||
expect(entries).toEqual([
|
||||
{ host: "2001:db8::1", port: 5433 },
|
||||
{ host: "2001:db8::2", port: 5434 },
|
||||
]);
|
||||
expect(serializeGaussdbHosts(entries).host).toBe("[2001:db8::1]:5433,[2001:db8::2]:5434");
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { ConnectionConfig, DatabaseType } from "@/types/database";
|
||||
import { GAUSSDB_M_JDBC_DRIVER_PROFILE } from "@/lib/database/jdbcDialect";
|
||||
import { parseGaussdbHosts, serializeGaussdbHosts } from "@/lib/connection/gaussdbHosts";
|
||||
|
||||
type ConnectionPresentationConfig = Pick<ConnectionConfig, "db_type" | "driver_profile" | "driver_label" | "host" | "port" | "database">;
|
||||
type ConnectionNamePresentationConfig = ConnectionPresentationConfig & Pick<ConnectionConfig, "name">;
|
||||
@@ -22,15 +23,46 @@ export function connectionEndpointLabel(connection?: ConnectionPresentationConfi
|
||||
if (LOCAL_DATABASE_TYPES.has(connection.db_type) || (connection.db_type === "h2" && connection.port === 0)) {
|
||||
return connection.host || connection.database || "local";
|
||||
}
|
||||
if (connection.host && connection.port) return `${connection.host}:${connection.port}`;
|
||||
return connection.host || connection.database || "";
|
||||
const endpoint = normalizedPresentationEndpoint(connection);
|
||||
if (endpoint.host && endpoint.port) {
|
||||
// Multi-host format: host1:port1,host2:port2 — already includes ports
|
||||
if (endpoint.host.includes(",")) return endpoint.host;
|
||||
const endpointHost = endpoint.host.includes(":") ? `[${endpoint.host}]` : endpoint.host;
|
||||
return `${endpointHost}:${endpoint.port}`;
|
||||
}
|
||||
return endpoint.host || connection.database || "";
|
||||
}
|
||||
|
||||
function normalizedPresentationEndpoint(connection: ConnectionPresentationConfig): { host: string; port: number } {
|
||||
if (connection.db_type !== "gaussdb") return { host: connection.host, port: connection.port };
|
||||
return serializeGaussdbHosts(parseGaussdbHosts(connection.host, connection.port));
|
||||
}
|
||||
|
||||
function redactConnectionHost(host: string): string {
|
||||
const normalizedHost = host.trim();
|
||||
if (!normalizedHost) return "";
|
||||
|
||||
const unwrappedHost = normalizedHost.startsWith("[") && normalizedHost.endsWith("]") ? normalizedHost.slice(1, -1) : normalizedHost;
|
||||
// Multi-host format: host1:port1,host2:port2 — redact each host separately
|
||||
// and replace each embedded port with the redacted marker.
|
||||
if (normalizedHost.includes(",")) {
|
||||
return normalizedHost
|
||||
.split(",")
|
||||
.map((part) => {
|
||||
const trimmed = part.trim();
|
||||
const colonIdx = trimmed.lastIndexOf(":");
|
||||
if (colonIdx > 0) {
|
||||
return `${redactSingleHost(trimmed.slice(0, colonIdx))}:${REDACTED_PORT}`;
|
||||
}
|
||||
return redactSingleHost(trimmed);
|
||||
})
|
||||
.join(",");
|
||||
}
|
||||
|
||||
return redactSingleHost(normalizedHost);
|
||||
}
|
||||
|
||||
function redactSingleHost(host: string): string {
|
||||
const unwrappedHost = host.startsWith("[") && host.endsWith("]") ? host.slice(1, -1) : host;
|
||||
const separator = unwrappedHost.includes(":") ? ":" : ".";
|
||||
const segments = unwrappedHost.split(separator).filter(Boolean);
|
||||
|
||||
@@ -52,8 +84,11 @@ export function connectionRedactedEndpointLabel(connection?: ConnectionPresentat
|
||||
return connectionEndpointLabel(connection);
|
||||
}
|
||||
|
||||
const redactedHost = connection.host ? redactConnectionHost(connection.host) : "";
|
||||
if (redactedHost && connection.port) {
|
||||
const endpoint = normalizedPresentationEndpoint(connection);
|
||||
const redactedHost = endpoint.host ? redactConnectionHost(endpoint.host) : "";
|
||||
if (redactedHost && endpoint.port) {
|
||||
// Multi-host format already includes ports
|
||||
if (redactedHost.includes(",")) return redactedHost;
|
||||
const endpointHost = redactedHost.includes(":") ? `[${redactedHost}]` : redactedHost;
|
||||
return `${endpointHost}:${REDACTED_PORT}`;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
export interface GaussdbHostEntry {
|
||||
host: string;
|
||||
port: number;
|
||||
}
|
||||
|
||||
const DEFAULT_GAUSSDB_PORT = 5432;
|
||||
|
||||
function validPort(value: number, fallback: number): number {
|
||||
return Number.isInteger(value) && value > 0 && value <= 65535 ? value : fallback || DEFAULT_GAUSSDB_PORT;
|
||||
}
|
||||
|
||||
function parseEndpoint(value: string, fallbackPort: number): GaussdbHostEntry {
|
||||
const endpoint = value.trim();
|
||||
if (endpoint.startsWith("[")) {
|
||||
const close = endpoint.indexOf("]");
|
||||
if (close > 0) {
|
||||
const suffix = endpoint.slice(close + 1);
|
||||
const parsedPort = suffix.startsWith(":") ? Number(suffix.slice(1)) : fallbackPort;
|
||||
return { host: endpoint.slice(1, close), port: validPort(parsedPort, fallbackPort) };
|
||||
}
|
||||
}
|
||||
if ((endpoint.match(/:/g) ?? []).length === 1) {
|
||||
const [host, rawPort] = endpoint.split(":");
|
||||
const parsedPort = Number(rawPort);
|
||||
if (host && Number.isInteger(parsedPort) && parsedPort > 0 && parsedPort <= 65535) return { host, port: parsedPort };
|
||||
}
|
||||
return { host: endpoint, port: validPort(fallbackPort, DEFAULT_GAUSSDB_PORT) };
|
||||
}
|
||||
|
||||
export function parseGaussdbHosts(host: string, port: number): GaussdbHostEntry[] {
|
||||
const fallbackPort = validPort(port, DEFAULT_GAUSSDB_PORT);
|
||||
if (!host.trim()) return [{ host: "127.0.0.1", port: fallbackPort }];
|
||||
const entries = host
|
||||
.split(",")
|
||||
.map((part) => parseEndpoint(part, fallbackPort))
|
||||
.filter((entry) => entry.host);
|
||||
return entries.length ? entries : [{ host: "127.0.0.1", port: fallbackPort }];
|
||||
}
|
||||
|
||||
function formatEndpoint(entry: GaussdbHostEntry): string {
|
||||
const host = entry.host.trim();
|
||||
const endpointHost = host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
|
||||
return `${endpointHost}:${validPort(entry.port, DEFAULT_GAUSSDB_PORT)}`;
|
||||
}
|
||||
|
||||
export function serializeGaussdbHosts(entries: readonly GaussdbHostEntry[]): { host: string; port: number } {
|
||||
const normalized = entries.map((entry) => ({ host: entry.host.trim(), port: validPort(entry.port, DEFAULT_GAUSSDB_PORT) })).filter((entry) => entry.host);
|
||||
if (!normalized.length) return { host: "", port: DEFAULT_GAUSSDB_PORT };
|
||||
if (normalized.length === 1) return normalized[0]!;
|
||||
return { host: normalized.map(formatEndpoint).join(","), port: normalized[0]!.port };
|
||||
}
|
||||
Reference in new issue
Block a user