openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
101 lines (100 loc) • 3.2 kB
JavaScript
import { isProxy } from "node:util/types";
//#region src/shared/immutable-data.ts
const MAX_FROZEN_PLAIN_DATA_ADMISSIONS = 262144;
let deeplyFrozenPlainData = /* @__PURE__ */ new WeakSet();
let frozenPlainDataAdmissions = 0;
function boundFrozenPlainData(e) {
if (frozenPlainDataAdmissions > MAX_FROZEN_PLAIN_DATA_ADMISSIONS) {
deeplyFrozenPlainData = new WeakSet([e]);
frozenPlainDataAdmissions = 1;
}
}
function isPlainDataObject(e) {
if (isProxy(e)) return false;
const t = Object.getPrototypeOf(e);
return t === null || t === Object.prototype || Array.isArray(e) && t === Array.prototype;
}
function inspectPlainData(e, t, n = true) {
if (!e || typeof e !== "object") return typeof e !== "function";
if (deeplyFrozenPlainData.has(e)) return true;
if (!t && n && (isProxy(e) || !Object.isFrozen(e))) return false;
let r = true;
const i = /* @__PURE__ */ new Set();
const a = [e];
while (a.length) {
const e = a.pop();
if (i.has(e)) continue;
if (!isPlainDataObject(e)) {
if (!t) return false;
r = false;
continue;
}
if (!t && n && !Object.isFrozen(e)) return false;
i.add(e);
for (const n of Reflect.ownKeys(e)) {
const i = Object.getOwnPropertyDescriptor(e, n);
const o = i.value;
if (!("value" in i) || typeof o === "function") {
if (!t) return false;
r = false;
continue;
}
if (o && typeof o === "object" && !deeplyFrozenPlainData.has(o)) a.push(o);
}
if (t) Object.freeze(e);
}
if (r && (t || n)) {
for (const e of i) deeplyFrozenPlainData.add(e);
frozenPlainDataAdmissions += i.size;
boundFrozenPlainData(e);
}
return r;
}
/** Inspect mutable plain graphs without caching them or reading accessors. */
function isPlainData(e) {
return inspectPlainData(e, false, false);
}
/** Memoized proofs use weak keys and can be evicted without changing graph immutability. */
function isDeeplyFrozenPlainData(e) {
return inspectPlainData(e, false);
}
/** Freeze an owner's cloned JSON snapshot without executing opaque members. */
function freezeJsonSnapshot(e) {
inspectPlainData(e, true);
return e;
}
/** Freeze decoded/internal JSON only; callers own the plain-data guarantee. */
function freezeTrustedJsonSnapshot(e) {
if (!e || typeof e !== "object" || deeplyFrozenPlainData.has(e)) return e;
Object.freeze(e);
deeplyFrozenPlainData.add(e);
frozenPlainDataAdmissions++;
const t = [e];
while (t.length) {
const e = t.pop();
for (const n in e) {
const r = Reflect.get(e, n);
if (r && typeof r === "object" && !deeplyFrozenPlainData.has(r)) {
Object.freeze(r);
deeplyFrozenPlainData.add(r);
frozenPlainDataAdmissions++;
t.push(r);
}
}
}
boundFrozenPlainData(e);
return e;
}
/** Reuse a pure projection only while its entire source graph is immutable. */
function memoizeFrozenPlainDataProjection(e) {
const t = /* @__PURE__ */ new WeakMap();
return (n) => {
if (t.has(n)) return t.get(n);
if (!isDeeplyFrozenPlainData(n)) return e(n);
const r = freezeJsonSnapshot(e(n));
t.set(n, r);
return r;
};
}
//#endregion
export { memoizeFrozenPlainDataProjection as a, isPlainData as i, freezeTrustedJsonSnapshot as n, isDeeplyFrozenPlainData as r, freezeJsonSnapshot as t };