openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
53 lines (52 loc) • 1.72 kB
JavaScript
import { u as redactToolPayloadText } from "./redact-Mj7pppxC.mjs";
//#region src/plugin-sdk/browser-cdp.ts
/** Detect an operator-supplied port before WHATWG URL normalization drops default ports. */
function hasRawExplicitPort(e) {
const t = e.replace(/^[a-z][a-z0-9+.-]*:\/\//i, "").split(/[/?#]/, 1)[0] ?? "";
const n = t.slice(t.lastIndexOf("@") + 1);
if (n.startsWith("[")) return /^\[[^\]]+\]:\d+$/.test(n);
return /:\d+$/.test(n);
}
/**
* Parses a browser/CDP endpoint and returns both URL semantics and display-safe normalized forms.
*/
function parseBrowserHttpUrl(e, t) {
const n = e.trim();
const r = new URL(n);
if (![
"http:",
"https:",
"ws:",
"wss:"
].includes(r.protocol)) throw new Error(`${t} must be http(s) or ws(s), got: ${r.protocol.replace(":", "")}`);
const i = r.protocol === "https:" || r.protocol === "wss:";
const a = hasRawExplicitPort(n);
const o = r.port ? Number.parseInt(r.port, 10) : i ? 443 : 80;
if (Number.isNaN(o) || o <= 0 || o > 65535) throw new Error(`${t} has invalid port: ${r.port}`);
const s = r.toString().replace(/\/$/, "");
return {
parsed: r,
port: o,
hasExplicitPort: a,
normalized: s,
normalizedWithPort: a && !r.port ? s.replace(/^[^:]+:\/\/[^/?#]+/, (e) => `${e}:${o}`) : s
};
}
/**
* Redacts credentials and known sensitive tokens from CDP URLs before logs or diagnostics.
*/
function redactCdpUrl(e) {
if (typeof e !== "string") return e;
const t = e.trim();
if (!t) return t;
try {
const e = new URL(t);
e.username = "";
e.password = "";
return redactToolPayloadText(e.toString().replace(/\/$/, ""));
} catch {
return redactToolPayloadText(t);
}
}
//#endregion
export { redactCdpUrl as n, parseBrowserHttpUrl as t };