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。

3.3 KB
Raw Blame
import { parseKafkaBootstrapServerAddress, parseKafkaBootstrapServers, resolveKafkaSecurityProtocol } from "@/lib/connection/kafkaBootstrapServers";
import { firstZooKeeperEndpoint, normalizeZooKeeperConnectString } from "@/lib/zookeeper/zookeeperConnection";

export type MqKafkaConnectionSource = "bootstrap" | "zookeeper";

export interface MqKafkaConnectionInput {
  connectionSource: MqKafkaConnectionSource;
  bootstrapServers: string;
  zookeeperServers: string;
  securityProtocol?: string;
}

export interface MqKafkaConnectionTarget {
  host: string;
  port: number;
  ssl: boolean;
}

export function resolveMqKafkaConnectionSource(extra: Record<string, unknown>): MqKafkaConnectionSource {
  if (extra.connectionSource === "zookeeper") return "zookeeper";
  if (typeof extra.zookeeperServers === "string" && extra.zookeeperServers.trim() && !(typeof extra.bootstrapServers === "string" && extra.bootstrapServers.trim())) {
    return "zookeeper";
  }
  return "bootstrap";
}

export function normalizeMqKafkaZooKeeperServers(value: string): string {
  const normalized = normalizeZooKeeperConnectString(value.trim());
  if (!normalized) throw new Error("Kafka ZooKeeper servers are required");

  const chrootIndex = normalized.indexOf("/");
  const ensemble = chrootIndex >= 0 ? normalized.slice(0, chrootIndex) : normalized;
  const chroot = chrootIndex >= 0 ? normalized.slice(chrootIndex) : "";
  if (chroot && (!chroot.startsWith("/") || chroot.includes("//"))) {
    throw new Error("Kafka ZooKeeper servers are invalid");
  }

  for (const endpoint of ensemble.split(",")) {
    const match = endpoint.match(/^(?:\[[^\]]+\]|[^:\s/?#]+):(\d+)$/u);
    const port = match ? Number(match[1]) : Number.NaN;
    if (!match || !Number.isInteger(port) || port < 1 || port > 65535) {
      throw new Error("Kafka ZooKeeper servers are invalid");
    }
  }
  return normalized;
}

export function buildMqKafkaConnectionExtra(input: MqKafkaConnectionInput): Record<string, string> {
  let extra: Record<string, string>;
  let securityProtocol = input.securityProtocol?.trim() || "";

  if (input.connectionSource === "zookeeper") {
    extra = {
      connectionSource: "zookeeper",
      zookeeperServers: normalizeMqKafkaZooKeeperServers(input.zookeeperServers),
    };
  } else {
    const parsed = parseKafkaBootstrapServers(input.bootstrapServers);
    securityProtocol = resolveKafkaSecurityProtocol(securityProtocol, parsed.inferredSecurityProtocol);
    extra = { bootstrapServers: parsed.bootstrapServers };
  }

  if (securityProtocol) extra.securityProtocol = securityProtocol;
  return extra;
}

export function mqKafkaConnectionTarget(input: MqKafkaConnectionInput): MqKafkaConnectionTarget {
  if (input.connectionSource === "zookeeper") {
    const endpoint = firstZooKeeperEndpoint(normalizeMqKafkaZooKeeperServers(input.zookeeperServers));
    if (!endpoint) throw new Error("Kafka ZooKeeper servers are required");
    return { ...endpoint, ssl: false };
  }

  const parsed = parseKafkaBootstrapServers(input.bootstrapServers);
  const first = parseKafkaBootstrapServerAddress(parsed.bootstrapServers.split(",")[0]);
  const securityProtocol = resolveKafkaSecurityProtocol(input.securityProtocol || "", parsed.inferredSecurityProtocol);
  return {
    host: first.host,
    port: first.port,
    ssl: securityProtocol === "SSL" || securityProtocol === "SASL_SSL",
  };
}