UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

155 lines (154 loc) • 5.83 kB
import { i as asOptionalObjectRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as isBlockedObjectKey } from "./prototype-keys-CLGi5xDj.mjs"; import { t as parseJsonWithJson5Fallback } from "./parse-json-compat-DoWUfD79.mjs"; import { t as parseConfigPathArrayIndex } from "./path-array-index-qP-D1DoI.mjs"; //#region src/shared/dot-path.ts function parseQuotedBracketPathSegment(e) { const t = e.startsWith("\"") ? "\"" : "'"; if (!e.endsWith(t)) throw new SyntaxError("Unterminated quoted path segment"); const n = e.replace(/\\(?:\r\n|[\s\S])|[\s\S]/g, (n, r) => { if (n.startsWith("\\")) { const e = n[1]; if (e && e.charCodeAt(0) < 32 && e !== "\r" && e !== "\n") throw new SyntaxError("Invalid escaped control character in path segment"); return n; } if (n === t && r !== 0 && r !== e.length - 1) throw new SyntaxError("Unexpected quote in path segment"); if (n.charCodeAt(0) < 32) throw new SyntaxError("Unescaped control character in path segment"); return n === "\u2028" ? "\\u2028" : n === "\u2029" ? "\\u2029" : n; }); return parseJsonWithJson5Fallback(n); } function parseBracketPathSegment(e, t) { const n = e.trim(); if (!n) throw new Error(`Invalid path (empty "[]"): ${t}`); if (n.startsWith("\"") || n.startsWith("'")) { try { const e = parseQuotedBracketPathSegment(n); if (typeof e === "string" && e.trim()) return e; } catch (e) { throw new Error(`Invalid path bracket string (${n}): ${t}`, { cause: e }); } throw new Error(`Invalid path bracket string (${n}): ${t}`); } return parseConfigPathArrayIndex(n) ?? n; } function assertNotWhitespaceSegment(e, t) { if (e.length > 0 && !e.trim()) throw new Error(`Invalid path (empty segment): ${t}`); } function findBracketPathClose(e, t) { let n; for (let r = t + 1; r < e.length; r += 1) { const i = e[r]; if (n) { if (i === "\\") r += 1; else if (i === n) n = void 0; continue; } if (i === "]") return r; if ((i === "\"" || i === "'") && !e.slice(t + 1, r).trim()) n = i; } return -1; } /** Reads diagnostic path identity without applying object-mutation key restrictions. */ function tokenizeConcreteConfigPath(e) { const t = e.trim(); if (!t) throw new Error("Path is empty."); const n = []; const r = /* @__PURE__ */ new Set(); let i = ""; let a = false; let o = 0; while (o < t.length) { const s = t[o]; if (s === "\\") { const n = t[o + 1]; if (n === void 0) throw new Error(`Invalid path (trailing escape): ${e}`); i += n; o += 2; continue; } if (s === ".") { assertNotWhitespaceSegment(i, e); if (!a && !i.trim()) throw new Error(`Invalid path (empty segment): ${e}`); if (i) n.push(i.trim()); i = ""; a = false; o += 1; continue; } if (s === "[") { assertNotWhitespaceSegment(i, e); if (!i.trim() && !a && n.length > 0) throw new Error(`Invalid path (empty segment): ${e}`); if (i) n.push(i.trim()); i = ""; const s = findBracketPathClose(t, o); if (s === -1) throw new Error(`Invalid path (missing "]"): ${e}`); const c = t.slice(o + 1, s).trim(); const l = parseBracketPathSegment(c, e); if (typeof l === "string" && (c.startsWith("\"") || c.startsWith("'")) && parseConfigPathArrayIndex(l) !== void 0) r.add(n.length); n.push(l); const u = t[s + 1]; if (u !== void 0 && u !== "." && u !== "[") throw new Error(`Invalid path (missing separator after bracket): ${e}`); a = true; o = s + 1; continue; } i += s; o += 1; } if (!a && !i.trim()) throw new Error(`Invalid path (empty segment): ${e}`); if (i) n.push(i.trim()); return { tokens: n, quotedNumericSegments: r }; } /** Retains quoted numeric-key provenance while rejecting unsafe object-key segments. */ function parseConcreteConfigPathWithProvenance(e) { const t = tokenizeConcreteConfigPath(e); for (const e of t.tokens) if (typeof e === "string" && isBlockedObjectKey(e)) throw new Error(`Invalid path segment: ${e}`); return t; } /** Parses one concrete path while keeping explicit array brackets distinct from quoted keys. */ function parseConcreteConfigPathTokens(e) { return parseConcreteConfigPathWithProvenance(e).tokens; } /** Parses one concrete config path into the existing string-segment CLI contract. */ function parseConcreteConfigPath(e) { return parseConcreteConfigPathTokens(e).map(String); } /** Appends one config path segment without confusing literal record keys with traversal. */ function appendConfigPathSegment(e, t) { if (typeof t === "number") return `${e}[${t}]`; if (!/^[A-Za-z_$][A-Za-z0-9_$:-]*$/.test(t)) return `${e}[${JSON.stringify(t)}]`; return e ? `${e}.${t}` : t; } /** Formats concrete tokens, recovering array indices from their actual source containers. */ function formatConcreteConfigPath(e, t) { let n = t; return e.reduce((e, t) => { const r = typeof t === "string" && Array.isArray(n) ? parseConfigPathArrayIndex(t) ?? t : t; n = n !== null && typeof n === "object" ? Reflect.get(n, String(t)) : void 0; return appendConfigPathSegment(e, r); }, ""); } /** Reads object paths, optionally restricting array keys to canonical config indexes. */ function readObjectPath(e, t, n = "raw") { let r = e; for (const e of t) { const t = asOptionalObjectRecord(r); if (!t) return; if (n !== "raw" && Array.isArray(t)) { const r = parseConfigPathArrayIndex(e); if (r === void 0 || n === "bounded" && r >= t.length) return; } r = t[e]; } return r; } /** Joins path segments into their dotted-path representation. */ function toDotPath(e) { return e.join("."); } //#endregion export { parseConcreteConfigPathWithProvenance as a, tokenizeConcreteConfigPath as c, parseConcreteConfigPathTokens as i, formatConcreteConfigPath as n, readObjectPath as o, parseConcreteConfigPath as r, toDotPath as s, appendConfigPathSegment as t };