openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
47 lines (46 loc) • 2.04 kB
JavaScript
import { a as normalizeLowercaseStringOrEmpty } from "./string-coerce-mnp54Vah.js";
import { t as isBlockedObjectKey } from "./prototype-keys-D2nJOZIy.js";
//#region src/routing/account-id.ts
const DEFAULT_ACCOUNT_ID = "default";
const VALID_ID_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/i;
const INVALID_CHARS_RE = /[^a-z0-9_-]+/g;
const LEADING_DASH_RE = /^-+/;
const TRAILING_DASH_RE = /-+$/;
const ACCOUNT_ID_CACHE_MAX = 512;
const normalizeAccountIdCache = /* @__PURE__ */ new Map();
const normalizeOptionalAccountIdCache = /* @__PURE__ */ new Map();
function canonicalizeAccountId(value) {
const normalized = normalizeLowercaseStringOrEmpty(value);
if (VALID_ID_RE.test(value)) return normalized;
return normalized.replace(INVALID_CHARS_RE, "-").replace(LEADING_DASH_RE, "").replace(TRAILING_DASH_RE, "").slice(0, 64);
}
function normalizeCanonicalAccountId(value) {
const canonical = canonicalizeAccountId(value);
if (!canonical || isBlockedObjectKey(canonical)) return;
return canonical;
}
function normalizeAccountId(value) {
const trimmed = (value ?? "").trim();
if (!trimmed) return DEFAULT_ACCOUNT_ID;
const cached = normalizeAccountIdCache.get(trimmed);
if (cached) return cached;
const normalized = normalizeCanonicalAccountId(trimmed) || "default";
setNormalizeCache(normalizeAccountIdCache, trimmed, normalized);
return normalized;
}
function normalizeOptionalAccountId(value) {
const trimmed = (value ?? "").trim();
if (!trimmed) return;
if (normalizeOptionalAccountIdCache.has(trimmed)) return normalizeOptionalAccountIdCache.get(trimmed);
const normalized = normalizeCanonicalAccountId(trimmed) || void 0;
setNormalizeCache(normalizeOptionalAccountIdCache, trimmed, normalized);
return normalized;
}
function setNormalizeCache(cache, key, value) {
cache.set(key, value);
if (cache.size <= ACCOUNT_ID_CACHE_MAX) return;
const oldest = cache.keys().next();
if (!oldest.done) cache.delete(oldest.value);
}
//#endregion
export { normalizeAccountId as n, normalizeOptionalAccountId as r, DEFAULT_ACCOUNT_ID as t };