Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

2.2 KB
Raw Blame
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 };
}