UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

124 lines (123 loc) • 4.44 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { r as resolvePathViaExistingAncestorSync } from "./boundary-path-C2gXEiXe.mjs"; import { t as SQLITE_SIDECAR_SUFFIXES } from "./sqlite-files-rWisKj_V.mjs"; import fs from "node:fs"; import path from "node:path"; //#region src/infra/client-credential-receipt.ts const CLIENT_CREDENTIAL_LEGACY_KEYS = [ "identity:primary", "identity:node", "identity:shareExtension", "auth:primary", "auth:node", "auth:shareExtension" ]; /** File replacement fences a completed orphan without opening the selected database. */ function readClientCredentialFileIdentity(e) { const t = fs.statSync(e, { bigint: true, throwIfNoEntry: false }); if (!t) { if (SQLITE_SIDECAR_SUFFIXES.some((t) => fs.existsSync(`${e}${t}`))) throw new Error("Client credential source is unavailable; restore the matching main database before using this client."); return null; } if (!t.isFile()) throw new Error("Client credential source is not a regular file."); return { path: fs.realpathSync.native(e), dev: String(t.dev), ino: String(t.ino) }; } function emptyClientCredentialReceipt(e = null) { return { format: 1, source: null, sharedFile: e, legacyImports: {} }; } function digest(e) { return typeof e === "string" && /^[a-f0-9]{64}$/u.test(e); } /** The private receipt is shared with Swift; reject unsupported formats rather than discarding claims. */ function parseClientCredentialReceipt(e, t) { const invalid = () => /* @__PURE__ */ new Error("Client credential migration receipt is invalid; run openclaw doctor --fix."); let n; try { n = JSON.parse(e); } catch { throw invalid(); } if (!isRecord(n) || n.format !== 1 || !isRecord(n.legacyImports)) throw invalid(); let r = null; if (n.source !== null) { const e = n.source; if (!isRecord(e) || e.kind !== "canonical" && e.kind !== "independent" || e.engine !== "sqlite" && e.engine !== "postgres" && e.engine !== "native-v0" || typeof e.identity !== "string" || e.identity.length === 0 || typeof e.generation !== "string" || e.generation.length === 0 || !digest(e.sha256) || e.destinationVersion !== 1) throw invalid(); r = { kind: e.kind, engine: e.engine, identity: e.identity, generation: e.generation, sha256: e.sha256, destinationVersion: e.destinationVersion }; } let i = null; if (n.sharedFile !== null) { const e = n.sharedFile; if (!isRecord(e) || typeof e.path !== "string" || typeof e.dev !== "string" || !/^\d+$/u.test(e.dev) || typeof e.ino !== "string" || !/^\d+$/u.test(e.ino)) throw invalid(); i = { path: e.path, dev: e.dev, ino: e.ino }; } if (r?.kind === "independent" && (r.engine !== "native-v0" || !i)) throw invalid(); const a = {}; for (const [e, t] of Object.entries(n.legacyImports)) { const n = CLIENT_CREDENTIAL_LEGACY_KEYS.find((t) => t === e); if (!n || !isRecord(t) || t.sha256 !== null && !digest(t.sha256)) throw invalid(); a[n] = { sha256: t.sha256 }; } return rebaseCanonicalReceipt({ format: 1, source: r, sharedFile: i, legacyImports: a }, t, false); } function rebaseCanonicalReceipt(e, t, n) { const r = e.sharedFile; if (!r || e.source?.kind === "independent") return e; const i = !path.isAbsolute(r.path); if (i && (!t || !r.path.startsWith("./") || r.path.slice(2).split("/").some((e) => !e || e === "." || e === ".." || e.includes("\\")))) throw new Error("Client credential receipt has an invalid relative shared binding."); if (!t) return e; const a = resolvePathViaExistingAncestorSync(t); const o = path.resolve(a, r.path); const s = path.relative(a, o); const c = s && !path.isAbsolute(s) && s !== ".." && !s.startsWith(`..${path.sep}`); const l = n && c ? `./${s.split(path.sep).join("/")}` : o; let u = e.source; if (u?.engine === "sqlite") { const e = `${r.path}:`; if (u.identity.startsWith(e)) u = { ...u, identity: `${l}:${u.identity.slice(e.length)}` }; else if (i) throw new Error("Client credential receipt has an invalid relative source identity."); } return { ...e, source: u, sharedFile: { ...r, path: l } }; } function serializeClientCredentialReceipt(e, t) { return JSON.stringify(rebaseCanonicalReceipt(e, t, true)); } //#endregion export { serializeClientCredentialReceipt as a, readClientCredentialFileIdentity as i, emptyClientCredentialReceipt as n, parseClientCredentialReceipt as r, CLIENT_CREDENTIAL_LEGACY_KEYS as t };