openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
37 lines (36 loc) • 1.56 kB
JavaScript
import { c as isRecord } from "./record-coerce-TIsquoVR.mjs";
import { t as parseConfigPathArrayIndex } from "./path-array-index-CvEcUJa-.mjs";
//#region src/secrets/json-pointer.ts
/** JSON Pointer token helpers for file-backed secret refs. */
/**
* Encodes one JSON Pointer path token using RFC 6901 escaping.
*/
function encodeJsonPointerToken(token) {
return token.replace(/~/g, "~0").replace(/\//g, "~1");
}
/**
* Reads a value from a JSON-like document using an absolute JSON Pointer.
* Missing segments throw by default; `onMissing: "undefined"` is for optional probes.
*/
function readJsonPointer(root, pointer, options = {}) {
const onMissing = options.onMissing ?? "throw";
const fail = (message) => {
if (onMissing === "throw") throw new Error(message);
};
if (!pointer.startsWith("/")) return fail("File-backed secret ids must be absolute JSON pointers (for example: \"/providers/openai/apiKey\").");
let current = root;
for (const rawToken of pointer.slice(1).split("/")) {
const token = rawToken.replace(/~1/g, "/").replace(/~0/g, "~");
if (Array.isArray(current)) {
const index = parseConfigPathArrayIndex(token);
if (index === void 0 || index >= current.length) return fail(`JSON pointer segment "${token}" is out of bounds.`);
current = current[index];
continue;
}
if (!isRecord(current) || !Object.hasOwn(current, token)) return fail(`JSON pointer segment "${token}" does not exist.`);
current = current[token];
}
return current;
}
//#endregion
export { readJsonPointer as n, encodeJsonPointerToken as t };