fix(meilisearch): preserve proxy paths and secure REST requests

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-17 01:50:11 +08:00
1 parent 47337ac048
commit 50e6476cb0
8 files changed
+303 -23

No files matched your search

@@ -1,9 +1,15 @@
import { describe, expect, it } from "vitest";
import { connectionProfileForScheme, parseConnectionUrl } from "@/lib/connection/connectionUrl";
import { applyParsedConnectionUrl, connectionProfileForScheme, parseConnectionUrl } from "@/lib/connection/connectionUrl";
import { parseConnectionDeepLink } from "@/lib/connection/connectionDeepLink";
import type { ConnectionConfig } from "@/types/database";
describe("Meilisearch connection URLs", () => {
it("parses the dedicated scheme with the default port and API key", () => {
expect(parseConnectionUrl("meilisearch://:secret@search.example.com")).toMatchObject({
it("rejects an empty host URL", () => {
expect(() => parseConnectionUrl("", "meilisearch")).toThrow("Connection URL is empty");
});
it("parses the dedicated scheme with the default port, API key, and proxy path", () => {
expect(parseConnectionUrl("meilisearch://:secret@search.example.com/gateway/meili")).toMatchObject({
dbType: "meilisearch",
driverProfile: "meilisearch",
driverLabel: "Meilisearch",
@@ -12,6 +18,7 @@ describe("Meilisearch connection URLs", () => {
username: "",
password: "secret",
ssl: false,
basePath: "/gateway/meili",
});
});
@@ -25,6 +32,76 @@ describe("Meilisearch connection URLs", () => {
});
});
it.each([
["http://search.example.com/gateway/meili/", 80, false],
["https://search.example.com/gateway/meili", 443, true],
["https://search.example.com:8443/gateway/meili", 8443, true],
])("parses proxy URL %s", (url, port, ssl) => {
expect(parseConnectionUrl(url, "meilisearch")).toMatchObject({
dbType: "meilisearch",
host: "search.example.com",
port,
ssl,
database: undefined,
basePath: "/gateway/meili",
});
});
it("stores the proxy path in canonical external config without dropping other fields", () => {
const parsed = parseConnectionUrl("http://search.example.com/gateway/meili", "meilisearch");
const config = applyParsedConnectionUrl(
{
db_type: "meilisearch",
driver_profile: "meilisearch",
driver_label: "Meilisearch",
host: "localhost",
port: 7700,
username: "",
password: "",
url_params: "",
ssl: false,
external_config: { base_path: "/legacy", custom: true },
} as Omit<ConnectionConfig, "id">,
parsed,
);
expect(config.external_config).toEqual({ basePath: "/gateway/meili", custom: true });
});
it("clears stale proxy paths when the parsed URL uses the root path", () => {
const parsed = parseConnectionUrl("http://search.example.com/", "meilisearch");
const config = applyParsedConnectionUrl(
{
db_type: "meilisearch",
driver_profile: "meilisearch",
driver_label: "Meilisearch",
host: "localhost",
port: 7700,
username: "",
password: "",
url_params: "",
ssl: false,
external_config: { basePath: "/old", base_path: "/legacy", custom: true },
} as Omit<ConnectionConfig, "id">,
parsed,
);
expect(parsed.basePath).toBe("");
expect(config.external_config).toEqual({ custom: true });
});
it("preserves the proxy path when a Meilisearch URL arrives through a deep link", () => {
const link = new URL("dbx://connection/new");
link.searchParams.set("type", "meilisearch");
link.searchParams.set("url", "https://search.example.com/gateway/meili?insecure=true");
expect(parseConnectionDeepLink(link.toString())).toMatchObject({
dbType: "meilisearch",
basePath: "/gateway/meili",
urlParams: "insecure=true",
});
});
it("exposes the Meilisearch scheme profile", () => {
expect(connectionProfileForScheme("meilisearch")).toEqual({
type: "meilisearch",
@@ -23,6 +23,7 @@ export interface ConnectionDeepLinkDraft {
password?: string;
database?: string;
urlParams?: string;
basePath?: string;
ssl?: boolean;
connectionString?: string;
oracleConnectionType?: "service_name" | "sid";
@@ -188,6 +189,7 @@ function draftFromConnectionUrl(value: string, preferredProfile?: string): Conne
password: parsed.password,
database: parsed.database,
urlParams: parsed.urlParams,
basePath: parsed.basePath,
ssl: parsed.ssl,
connectionString: parsed.connectionString,
oracleConnectionType: parsed.oracleConnectionType,
@@ -208,13 +208,15 @@ export function connectionUrlPlaceholder(dbType: DatabaseType): string {
case "elasticsearch":
case "easysearch":
case "meilisearch":
case "qdrant":
case "milvus":
case "weaviate":
case "chromadb":
return "http://user:password@host:port";
case "meilisearch":
return "http://host:port/base/path";
case "dameng":
return "dm://user:password@host:port";
@@ -19,6 +19,7 @@ export interface ParsedConnectionUrl {
useMongoUrl?: boolean;
portExplicit?: boolean;
apiPath?: string;
basePath?: string;
}
export type ConnectionProfile = {
@@ -651,20 +652,24 @@ export function parseConnectionUrl(value: string, preferredProfile?: string): Pa
};
}
const isMeilisearch = profile.type === "meilisearch";
const defaultPort = isMeilisearch && scheme === "http" ? 80 : isMeilisearch && scheme === "https" ? 443 : profile.defaultPort;
return {
...(name ? { name } : {}),
dbType: profile.type,
driverProfile: profile.profile,
driverLabel: profile.label,
host: parsed.hostname,
port: parsed.port ? Number(parsed.port) : profile.defaultPort,
port: parsed.port ? Number(parsed.port) : defaultPort,
...(profile.type === "sqlserver" && parsed.port ? { portExplicit: true } : {}),
username: mysqlCredentials?.username ?? decodeUrlPart(parsed.username),
password: mysqlCredentials?.password ?? decodeUrlPart(parsed.password),
database: profile.type === "victoriametrics" ? "metrics" : databaseFromPath(parsed.pathname),
database: profile.type === "victoriametrics" ? "metrics" : isMeilisearch ? undefined : databaseFromPath(parsed.pathname),
urlParams: effectiveUrlParams,
ssl: scheme === "rediss" || scheme === "https" || urlParamsRequireTls(profile.type, effectiveUrlParams) || (profile.type === "mysql" && isTidbCloudHost(parsed.hostname)),
...(profile.type === "victoriametrics" ? { apiPath: parsed.pathname.replace(/\/+$/, "") } : {}),
...(isMeilisearch ? { basePath: parsed.pathname === "/" ? "" : parsed.pathname.replace(/\/+$/, "") } : {}),
};
}
@@ -711,12 +716,26 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object" && !Array.isArray(value);
}
export function applyMeilisearchBasePathToExternalConfig(existing: unknown, basePath: string | undefined): unknown {
const next = isRecord(existing) ? { ...existing } : {};
delete next.base_path;
if (basePath) {
next.basePath = basePath;
} else {
delete next.basePath;
}
return Object.keys(next).length > 0 ? next : undefined;
}
function parsedExternalConfig(existing: unknown, parsed: ParsedConnectionUrl): unknown {
if (parsed.dbType === "victoriametrics") {
const next = isRecord(existing) ? { ...existing } : {};
next.apiPath = parsed.apiPath || "/prometheus";
return next;
}
if (parsed.dbType === "meilisearch") {
return applyMeilisearchBasePathToExternalConfig(existing, parsed.basePath);
}
if (parsed.dbType !== "sqlserver") return existing;
const next = isRecord(existing) ? { ...existing } : {};