openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
100 lines (99 loc) • 3.25 kB
JavaScript
import { a as throwIfSettledRejected } from "./error-graph-internal-DdjdM90N.mjs";
import { r as buildCloudflareAccessHeaders } from "./cloudflare-access-D5ntQryq.mjs";
import { i as loadDeviceAuthTokenReadOnly } from "./device-auth-store-B3F61F1r.mjs";
import { n as gatewayOriginScope } from "./gateway-origin-scope-SP9yyLnR.mjs";
import { t as GatewayClient } from "./client-cXg8kULv.mjs";
//#region src/node-host/gateway-candidate-connection.ts
function formatGatewayCandidateUrl(e) {
const t = e.host ?? "127.0.0.1";
const n = t.includes(":") && !(t.startsWith("[") && t.endsWith("]")) ? `[${t}]` : t;
const r = e.port ?? 18789;
return `${e.tls ? "wss" : "ws"}://${n}:${r}${e.contextPath ? e.contextPath.startsWith("/") ? e.contextPath : `/${e.contextPath}` : ""}`;
}
function createNodeHostGatewayCandidateConnection(e) {
if (e.candidates.length === 0) throw new Error("node host gateway candidate list cannot be empty");
let t = 0;
let n = false;
let r;
const i = [];
let a = e.candidates.length === 1;
let o;
let s = createCandidateClient(t);
function createCandidateClient(r) {
const c = e.candidates[r];
if (!c) throw new Error(`node host gateway candidate ${r} is unavailable`);
const l = formatGatewayCandidateUrl(c);
const u = e.cloudflareAccessByCandidate?.get(c);
const d = new GatewayClient({
...e.clientOptions,
url: l,
tlsFingerprint: c.tlsFingerprint,
...u ? { edgeAuthHeaders: buildCloudflareAccessHeaders(u) } : {},
onEvent: (n) => {
if (t === r) e.onEvent(n);
},
onHelloOk: (n) => {
if (t !== r) return;
if (!a) {
a = true;
e.onWinningCandidate(c);
}
e.onHelloOk(n, l, c.tlsFingerprint, u);
},
onConnectError: (n) => {
if (t === r) e.onConnectError(n);
},
onReconnectPaused: (n) => {
if (t === r) e.onReconnectPaused(n);
},
onClose: (i, o, c) => {
if (n || t !== r) return;
e.onClose(i, o, c);
const l = r + 1;
if (n || a || l >= e.candidates.length || c?.phase !== "pre-hello" || c.connectRequestSent !== false) return;
t = l;
d.stop();
queueMicrotask(() => {
if (n || t !== l) return;
s = createCandidateClient(l);
s.start();
});
}
});
if (o) d.updateNodeManifest(o);
i.push(d);
return d;
}
return {
start() {
if (!n) s.start();
},
stop() {
n = true;
r ??= Promise.resolve().then(async () => {
const e = await Promise.allSettled(i.map((e) => e.stopAndWait()));
throwIfSettledRejected(e, "node host gateway cleanup failed");
});
return r;
},
request(...e) {
return s.request(...e);
},
updateNodeManifest(e) {
o = e;
s.updateNodeManifest(e);
}
};
}
//#endregion
//#region src/node-host/gateway-auth.ts
async function canReuseNodeHostDeviceToken(e) {
const t = e.savedGateway ? gatewayOriginScope(formatGatewayCandidateUrl(e.savedGateway)) : void 0;
return Boolean(t && e.gatewayCandidates.every((e) => gatewayOriginScope(formatGatewayCandidateUrl(e)) === t) && (await loadDeviceAuthTokenReadOnly({
deviceId: e.deviceId,
role: "node",
env: e.env ?? process.env
}))?.token);
}
//#endregion
export { createNodeHostGatewayCandidateConnection as n, formatGatewayCandidateUrl as r, canReuseNodeHostDeviceToken as t };