UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

34 lines (33 loc) 1.76 kB
import { c as normalizeOptionalString } from "./string-coerce-mnp54Vah.js"; import { r as canonicalizePathVariant } from "./security-path-D9ZaiTJ5.js"; //#region src/plugins/http-path.ts /** Normalizes plugin HTTP paths to leading-slash form with optional fallback. */ function normalizePluginHttpPath(path, fallback) { const trimmed = normalizeOptionalString(path); if (!trimmed) { const fallbackTrimmed = normalizeOptionalString(fallback); if (!fallbackTrimmed) return null; return fallbackTrimmed.startsWith("/") ? fallbackTrimmed : `/${fallbackTrimmed}`; } return trimmed.startsWith("/") ? trimmed : `/${trimmed}`; } //#endregion //#region src/plugins/http-route-overlap.ts /** Detects conflicting plugin HTTP routes before Gateway registration accepts them. */ function prefixMatchPath(pathname, prefix) { return pathname === prefix || pathname.startsWith(`${prefix}/`) || pathname.startsWith(`${prefix}%`); } function doPluginHttpRoutesOverlap(a, b) { const aPath = canonicalizePathVariant(a.path); const bPath = canonicalizePathVariant(b.path); if (a.match === "exact" && b.match === "exact") return aPath === bPath; if (a.match === "prefix" && b.match === "prefix") return prefixMatchPath(aPath, bPath) || prefixMatchPath(bPath, aPath); const prefixRoute = a.match === "prefix" ? a : b; return prefixMatchPath(canonicalizePathVariant((a.match === "exact" ? a : b).path), canonicalizePathVariant(prefixRoute.path)); } /** Finds the first existing route whose exact/prefix match space overlaps a candidate. */ function findOverlappingPluginHttpRoute(routes, candidate) { return routes.find((route) => doPluginHttpRoutesOverlap(route, candidate)); } //#endregion export { normalizePluginHttpPath as n, findOverlappingPluginHttpRoute as t };