openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
148 lines (147 loc) • 6.87 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { d as normalizeRequiredSecretInput, p as normalizeSecretInputString } from "./types.secrets-BmNLLnHp.mjs";
import { o as registerSecretValueForRedaction } from "./secret-redaction-registry-Cy0acctn.mjs";
import { r as createStateDomainPublication } from "./state-domain-publication-DiGC-tAS.mjs";
import { t as materializeSecretInput } from "./resolve-secret-input-string-DjfC9izQ.mjs";
//#region src/node-host/gateway-cloudflare-access.ts
const CF_ACCESS_CLIENT_ID_ENV = "CF_ACCESS_CLIENT_ID";
const CF_ACCESS_CLIENT_SECRET_ENV = "CF_ACCESS_CLIENT_SECRET";
function normalizeNodeHostCloudflareAccessConfig(e) {
if (e === void 0 || e === null) return;
if (!isRecord(e) || Object.keys(e).length !== 2 || !("clientId" in e) || !("clientSecret" in e)) throw new Error("invalid node-host gateway.cloudflareAccess: expected clientId and clientSecret");
return {
clientId: normalizeRequiredSecretInput(e.clientId, "node-host gateway.cloudflareAccess.clientId"),
clientSecret: normalizeRequiredSecretInput(e.clientSecret, "node-host gateway.cloudflareAccess.clientSecret")
};
}
/** Persist conventional environment fallback as refs, never as copied plaintext. */
function nodeHostCloudflareAccessConfigFromEnv(e) {
const t = normalizeSecretInputString(e[CF_ACCESS_CLIENT_ID_ENV]);
const n = normalizeSecretInputString(e[CF_ACCESS_CLIENT_SECRET_ENV]);
if (!t && !n) return;
if (!t || !n) throw new Error(`${CF_ACCESS_CLIENT_ID_ENV} and ${CF_ACCESS_CLIENT_SECRET_ENV} must be configured together`);
return {
clientId: {
source: "env",
provider: "default",
id: CF_ACCESS_CLIENT_ID_ENV
},
clientSecret: {
source: "env",
provider: "default",
id: CF_ACCESS_CLIENT_SECRET_ENV
}
};
}
async function resolveNodeHostCloudflareAccess(e) {
if (!e.value) return;
const [t, n] = await Promise.all([e.value.clientId, e.value.clientSecret].map((t) => materializeSecretInput({
config: e.config,
value: t,
env: e.env
})));
if (!t || !n) throw new Error("node-host Cloudflare Access credentials resolved empty");
registerSecretValueForRedaction(t);
registerSecretValueForRedaction(n);
return {
clientId: t,
clientSecret: n
};
}
function nodeHostGatewayOrigin(e) {
const t = e.host ?? "127.0.0.1";
const n = t.includes(":") && !(t.startsWith("[") && t.endsWith("]")) ? `[${t}]` : t;
const r = e.tls ? "https:" : "http:";
const i = e.port ?? (e.tls ? 443 : 80);
return new URL(`${r}//${n}:${i}`);
}
function nodeHostGatewayMatchesUrl(e, t) {
const n = nodeHostGatewayOrigin(e);
return n.protocol === t.protocol && n.host === t.host;
}
function nodeHostGatewaysShareOrigin(e, t) {
return nodeHostGatewayMatchesUrl(e, nodeHostGatewayOrigin(t));
}
//#endregion
//#region src/node-host/config-shared.ts
function optionalNonEmptyString(e, t) {
if (e === null || e === void 0) return;
if (typeof e !== "string") throw new Error(`invalid node-host SQLite row: ${t} must be a string`);
const n = e.trim();
if (!n) throw new Error(`invalid node-host SQLite row: ${t} must not be empty`);
return n;
}
function validatePort(e, t) {
if (e === null || e === void 0) return;
if (typeof e !== "number" || !Number.isSafeInteger(e) || e <= 0 || e > 65535) throw new Error(`invalid node-host ${t}: expected an integer between 1 and 65535`);
return e;
}
function normalizeStoredNodeHostConfig(e) {
if (!isRecord(e)) throw new Error("invalid node-host SQLite row: expected a configuration object");
if (e.version !== 1) throw new Error(`invalid node-host SQLite row: unsupported version ${String(e.version)}`);
const t = typeof e.nodeId === "string" ? e.nodeId.trim() : "";
if (!t) throw new Error("invalid node-host SQLite row: node_id must not be empty");
const n = e.gateway;
if (n !== void 0 && !isRecord(n)) throw new Error("invalid node-host SQLite row: gateway must be an object");
const r = n?.tls;
if (r !== void 0 && typeof r !== "boolean") throw new Error("invalid node-host SQLite row: gateway_tls must be a boolean");
if (e.installedAppsSharing !== void 0 && typeof e.installedAppsSharing !== "boolean") throw new Error("invalid node-host SQLite row: installed_apps_sharing must be a boolean");
const i = n ? normalizeGatewayConfig({
host: optionalNonEmptyString(n.host, "gateway_host"),
port: validatePort(n.port, "SQLite gateway_port"),
tls: typeof r === "boolean" ? r : void 0,
tlsFingerprint: optionalNonEmptyString(n.tlsFingerprint, "gateway_tls_fingerprint"),
contextPath: optionalNonEmptyString(n.contextPath, "gateway_context_path"),
...cloudflareAccessEntry(normalizeNodeHostCloudflareAccessConfig(n.cloudflareAccess))
}) : void 0;
return {
version: 1,
nodeId: t,
displayName: optionalNonEmptyString(e.displayName, "display_name"),
gateway: i,
installedAppsSharing: e.installedAppsSharing === true,
...e.commands !== void 0 ? { commands: normalizeNodeHostCommands(e.commands) } : {}
};
}
function normalizeNodeHostCommands(e) {
if (!Array.isArray(e) || e.some((e) => typeof e !== "string" || !e.trim())) throw new Error("invalid node-host commands: expected an array of non-empty command ids");
return [...new Set(e.map((e) => e.trim()))].toSorted();
}
function cloudflareAccessEntry(e) {
return e ? { cloudflareAccess: e } : {};
}
function normalizeGatewayConfig(e) {
const t = {
host: normalizeOptionalString(e.host),
port: validatePort(e.port, "gateway port"),
tls: e.tls,
tlsFingerprint: normalizeOptionalString(e.tlsFingerprint),
contextPath: normalizeOptionalString(e.contextPath),
...cloudflareAccessEntry(normalizeNodeHostCloudflareAccessConfig(e.cloudflareAccess))
};
return Object.values(t).some((e) => e !== void 0) ? t : void 0;
}
//#endregion
//#region src/node-host/node-worker-prepared-workspace-publication.ts
const nodePreparedWorkspacePublication = createStateDomainPublication({
domain: "node-prepared-workspace",
keyOf: (e) => e.preparation_key,
isValue: (e) => isRecord(e) && [
"preparation_key",
"cache_key",
"gateway_namespace",
"environment_id",
"workspace_dir",
"home_dir",
"source_manifest_ref",
"prepared_manifest_ref",
"state"
].every((t) => typeof e[t] === "string") && ["session_id", "session_key"].every((t) => e[t] === null || typeof e[t] === "string") && typeof e.created_at_ms === "number" && [
"owner_epoch",
"bound_at_ms",
"retired_at_ms"
].every((t) => e[t] === null || typeof e[t] === "number")
});
//#endregion
export { nodeHostCloudflareAccessConfigFromEnv as a, normalizeNodeHostCloudflareAccessConfig as c, normalizeStoredNodeHostConfig as i, resolveNodeHostCloudflareAccess as l, normalizeGatewayConfig as n, nodeHostGatewayMatchesUrl as o, normalizeNodeHostCommands as r, nodeHostGatewaysShareOrigin as s, nodePreparedWorkspacePublication as t };