feat(nacos): add r-nacos compatibility
This commit is contained in:
31 files changed
+2481
-168
No files matched your search
@@ -10,12 +10,31 @@ import {
|
||||
nacosConfigFileExtension,
|
||||
parseNacosRawBody,
|
||||
parseNacosRawQuery,
|
||||
normalizeNacosEndpoint,
|
||||
resolveRNacosOpenApiFallback,
|
||||
resolveNacosConfigCopyText,
|
||||
sanitizeNacosConfigFileNameSegment,
|
||||
summarizeNacosConfigDiff,
|
||||
} from "@/lib/nacos/nacosAdmin";
|
||||
|
||||
describe("nacosAdmin helpers", () => {
|
||||
it("normalizes Nacos profile URLs without losing proxy prefixes", () => {
|
||||
expect(normalizeNacosEndpoint("https://[2001:db8::1]:9443/gateway/nacos/", { implementation: "nacos", versionMode: "v2" })).toMatchObject({
|
||||
serverAddr: "https://[2001:db8::1]:9443",
|
||||
contextPath: "/gateway/nacos",
|
||||
detectedVersion: "v2",
|
||||
});
|
||||
expect(normalizeNacosEndpoint("https://nacos.example/gateway/next/index.html", { implementation: "nacos", versionMode: "v3" })).toMatchObject({
|
||||
serverAddr: "https://nacos.example",
|
||||
contextPath: "/gateway",
|
||||
detectedVersion: "v3",
|
||||
});
|
||||
expect(normalizeNacosEndpoint("http://rnacos.example:8848/nacos", { implementation: "rnacos" })).toMatchObject({
|
||||
serverAddr: "http://rnacos.example:8848",
|
||||
contextPath: "/nacos",
|
||||
});
|
||||
expect(() => normalizeNacosEndpoint("http://user:secret@nacos.example", { implementation: "nacos" })).toThrow(/embedded credentials/i);
|
||||
});
|
||||
it("parses raw query and body text", () => {
|
||||
expect(parseNacosRawQuery("?dataId=a&group=DEFAULT_GROUP")).toEqual({ dataId: "a", group: "DEFAULT_GROUP" });
|
||||
expect(parseNacosRawQuery("")).toBeUndefined();
|
||||
@@ -30,6 +49,23 @@ describe("nacosAdmin helpers", () => {
|
||||
expect(isNacosRawMutation("DELETE")).toBe(true);
|
||||
});
|
||||
|
||||
it("redirects r-nacos console settings to the compatible OpenAPI endpoint", () => {
|
||||
expect(resolveRNacosOpenApiFallback("http://rnacos.example:10848", "/rnacos")).toEqual({
|
||||
serverAddr: "http://rnacos.example:8848",
|
||||
contextPath: "/nacos",
|
||||
});
|
||||
expect(resolveRNacosOpenApiFallback("https://rnacos.example/gateway", "rnacos/")).toEqual({
|
||||
serverAddr: "https://rnacos.example/gateway",
|
||||
contextPath: "/nacos",
|
||||
});
|
||||
expect(resolveRNacosOpenApiFallback("http://nacos.example:10848", "/nacos")).toBeNull();
|
||||
expect(resolveRNacosOpenApiFallback("http://rnacos.example:10848", "", { allowConsolePortInference: true })).toEqual({
|
||||
serverAddr: "http://rnacos.example:8848",
|
||||
contextPath: "/nacos",
|
||||
});
|
||||
expect(resolveRNacosOpenApiFallback("http://rnacos.example:8848", "/nacos")).toBeNull();
|
||||
});
|
||||
|
||||
it("summarizes config diffs", () => {
|
||||
const diff = summarizeNacosConfigDiff("a\nb", "a\nc\nd");
|
||||
expect(diff.changed).toBe(true);
|
||||
|
||||
@@ -326,6 +326,8 @@ export const nacosDeleteConfig = forward("nacosDeleteConfig");
|
||||
export const nacosListConfigHistory = forward("nacosListConfigHistory");
|
||||
export const nacosGetConfigHistory = forward("nacosGetConfigHistory");
|
||||
export const nacosRollbackConfig = forward("nacosRollbackConfig");
|
||||
export const nacosGetRNacosConsoleCaptcha = forward("nacosGetRNacosConsoleCaptcha");
|
||||
export const nacosLoginRNacosConsole = forward("nacosLoginRNacosConsole");
|
||||
export const nacosListServices = forward("nacosListServices");
|
||||
export const nacosListInstances = forward("nacosListInstances");
|
||||
export const nacosUpdateInstance = forward("nacosUpdateInstance");
|
||||
|
||||
@@ -150,6 +150,7 @@ import type {
|
||||
NacosConfigRollbackRequest,
|
||||
NacosConfigUpsert,
|
||||
NacosConnectionInfo,
|
||||
NacosRNacosConsoleCaptcha,
|
||||
NacosInstanceInfo,
|
||||
NacosInstanceQuery,
|
||||
NacosInstanceUpdate,
|
||||
@@ -2101,6 +2102,14 @@ export async function nacosRollbackConfig(connectionId: string, req: NacosConfig
|
||||
return post("/api/nacos/configs/history/rollback", { connectionId, req });
|
||||
}
|
||||
|
||||
export async function nacosGetRNacosConsoleCaptcha(connectionId: string): Promise<NacosRNacosConsoleCaptcha> {
|
||||
return post("/api/nacos/rnacos-console/captcha", { connectionId });
|
||||
}
|
||||
|
||||
export async function nacosLoginRNacosConsole(connectionId: string, captcha?: string): Promise<void> {
|
||||
return post("/api/nacos/rnacos-console/login", { connectionId, captcha });
|
||||
}
|
||||
|
||||
export async function nacosListServices(connectionId: string, query: NacosServiceQuery): Promise<NacosServiceList> {
|
||||
return post("/api/nacos/services/list", { connectionId, query });
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
NacosConfigRollbackRequest,
|
||||
NacosConfigUpsert,
|
||||
NacosConnectionInfo,
|
||||
NacosRNacosConsoleCaptcha,
|
||||
NacosInstanceInfo,
|
||||
NacosInstanceQuery,
|
||||
NacosInstanceUpdate,
|
||||
@@ -66,6 +67,14 @@ export async function nacosRollbackConfig(connectionId: string, req: NacosConfig
|
||||
return invoke("nacos_rollback_config", { connectionId, req });
|
||||
}
|
||||
|
||||
export async function nacosGetRNacosConsoleCaptcha(connectionId: string): Promise<NacosRNacosConsoleCaptcha> {
|
||||
return invoke("nacos_get_rnacos_console_captcha", { connectionId });
|
||||
}
|
||||
|
||||
export async function nacosLoginRNacosConsole(connectionId: string, captcha?: string): Promise<void> {
|
||||
return invoke("nacos_login_rnacos_console", { connectionId, captcha });
|
||||
}
|
||||
|
||||
export async function nacosListServices(connectionId: string, query: NacosServiceQuery): Promise<NacosServiceList> {
|
||||
return invoke("nacos_list_services", { connectionId, query });
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { NacosConfigHistoryItem, NacosConfigItem, NacosInstanceInfo, NacosRawRequest, NacosServiceInfo } from "@/types/nacos";
|
||||
import type { NacosConfigHistoryItem, NacosConfigItem, NacosImplementation, NacosInstanceInfo, NacosRawRequest, NacosServiceInfo, NacosVersionMode } from "@/types/nacos";
|
||||
import { diffChars, diffLines } from "diff";
|
||||
|
||||
export type NacosRawTemplateKey = "serverState" | "namespaceList" | "configDetail" | "serviceList" | "instanceList";
|
||||
@@ -37,6 +37,103 @@ export const NACOS_RAW_TEMPLATES: NacosRawTemplate[] = [
|
||||
},
|
||||
];
|
||||
|
||||
export interface RNacosOpenApiFallback {
|
||||
serverAddr: string;
|
||||
contextPath: string;
|
||||
}
|
||||
|
||||
export interface RNacosOpenApiFallbackOptions {
|
||||
/** Treat the well-known r-nacos console port as a candidate after the original connection fails. */
|
||||
allowConsolePortInference?: boolean;
|
||||
}
|
||||
|
||||
export interface NacosEndpointNormalization {
|
||||
serverAddr: string;
|
||||
contextPath: string;
|
||||
detectedImplementation?: NacosImplementation;
|
||||
detectedVersion?: Exclude<NacosVersionMode, "auto">;
|
||||
warnings: string[];
|
||||
}
|
||||
|
||||
export interface NacosEndpointNormalizationOptions {
|
||||
implementation?: NacosImplementation;
|
||||
versionMode?: NacosVersionMode;
|
||||
contextPath?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a pasted browser/API URL into the persisted origin and API context.
|
||||
* We only strip documented Nacos 3 UI routes; every other prefix remains an
|
||||
* explicit context path so reverse proxies are not silently broken.
|
||||
*/
|
||||
export function normalizeNacosEndpoint(input: string, options: NacosEndpointNormalizationOptions = {}): NacosEndpointNormalization {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(input.trim());
|
||||
} catch {
|
||||
throw new Error("Nacos address must be a valid absolute URL");
|
||||
}
|
||||
if (url.username || url.password) throw new Error("Nacos address must not contain embedded credentials");
|
||||
|
||||
const rawPath = url.pathname.replace(/\/+$/, "");
|
||||
const implementation = options.implementation;
|
||||
const versionMode = options.versionMode || "auto";
|
||||
const warnings: string[] = [];
|
||||
const hasRNacosSuffix = /\/rnacos$/i.test(rawPath);
|
||||
const hasNacosSuffix = /\/nacos$/i.test(rawPath);
|
||||
const hasNacos3UiSuffix = /\/(?:next(?:\/index\.html)?|index\.html)$/i.test(rawPath);
|
||||
const detectedImplementation: NacosImplementation | undefined = implementation || (hasRNacosSuffix || url.port === "10848" ? "rnacos" : "nacos");
|
||||
const detectedVersion: Exclude<NacosVersionMode, "auto"> | undefined = detectedImplementation === "rnacos" ? undefined : versionMode === "auto" ? (hasNacos3UiSuffix ? "v3" : hasNacosSuffix ? "v2" : undefined) : versionMode;
|
||||
let contextPath = rawPath;
|
||||
|
||||
if (detectedImplementation === "rnacos") {
|
||||
if (hasRNacosSuffix || url.port === "10848") warnings.push("This looks like an r-nacos console URL; use the compatible API address as the primary endpoint.");
|
||||
contextPath = hasNacosSuffix ? rawPath : options.contextPath?.trim() || "/nacos";
|
||||
} else if (detectedVersion === "v3" || hasNacos3UiSuffix) {
|
||||
contextPath = rawPath.replace(/\/(?:next(?:\/index\.html)?|index\.html)$/i, "");
|
||||
if (hasNacos3UiSuffix) warnings.push("The Nacos 3 console route was removed from the API context.");
|
||||
} else if (!contextPath) {
|
||||
contextPath = options.contextPath?.trim() || (detectedVersion === "v2" ? "/nacos" : "");
|
||||
}
|
||||
url.pathname = "/";
|
||||
url.search = "";
|
||||
url.hash = "";
|
||||
return {
|
||||
serverAddr: url.toString().replace(/\/$/, ""),
|
||||
contextPath: contextPath ? `/${contextPath.replace(/^\/+|\/+$/g, "")}` : "",
|
||||
detectedImplementation,
|
||||
detectedVersion,
|
||||
warnings,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* r-nacos exposes its Nacos-compatible OpenAPI on the service port (8848) at
|
||||
* `/nacos`; `/rnacos` on 10848 is the separate web console and rejects these
|
||||
* OpenAPI POST requests. Port-only detection is deliberately opt-in: 10848
|
||||
* may also be a legitimate user mapping for a normal Nacos OpenAPI, so callers
|
||||
* must only use that weaker signal as a tested fallback candidate.
|
||||
*/
|
||||
export function resolveRNacosOpenApiFallback(serverAddr: string, contextPath: string, options: RNacosOpenApiFallbackOptions = {}): RNacosOpenApiFallback | null {
|
||||
const normalizedContextPath = `/${contextPath.trim().replace(/^\/+|\/+$/g, "")}`.replace(/\/$/, "");
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(serverAddr.trim());
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
const explicitRNacosContext = normalizedContextPath === "/rnacos";
|
||||
const inferredRNacosConsolePort = options.allowConsolePortInference === true && parsed.port === "10848";
|
||||
if (!explicitRNacosContext && !inferredRNacosConsolePort) return null;
|
||||
if (parsed.port === "10848") parsed.port = "8848";
|
||||
|
||||
return {
|
||||
serverAddr: parsed.toString().replace(/\/$/, ""),
|
||||
contextPath: "/nacos",
|
||||
};
|
||||
}
|
||||
|
||||
export function parseNacosRawQuery(text: string): Record<string, string> | undefined {
|
||||
const trimmed = text.trim().replace(/^\?/, "");
|
||||
if (!trimmed) return undefined;
|
||||
|
||||
Reference in new issue
Block a user