openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 2.23 kB
JavaScript
import{createHash as e,hash as t}from"node:crypto";import{Buffer as n}from"node:buffer";const preserveString=e=>e;function stableStringify(e,t=preserveString){return stringifyStableValue(e,/* @__PURE__ */ new WeakSet,t)}function writeStableStringify(e,t,n=preserveString){t(stringifyStableValue(e,/* @__PURE__ */ new WeakSet,n,t))}function stringifyStableValue(e,t,n,r){if(e==null)return String(e);if(typeof e==`number`&&!Number.isFinite(e))return JSON.stringify(String(e));if(typeof e==`bigint`)return JSON.stringify(e.toString());if(typeof e==`string`)return JSON.stringify(n(e));if(typeof e!=`object`)return JSON.stringify(e)??`null`;if(t.has(e))return JSON.stringify(`[Circular]`);t.add(e);try{if(e instanceof Error)return stringifyStableValue({name:e.name,message:e.message,stack:e.stack},t,n,r);if(e instanceof Uint8Array)return stringifyStableValue({type:`Uint8Array`,data:encodeBase64(e)},t,n,r);if(Array.isArray(e)){if(r){r(`[`);let i=``;for(let a of e)r(i),r(stringifyStableValue(a,t,n,r)),i=`,`;return r(`]`),``}let i=[];for(let r of e)i.push(stringifyStableValue(r,t,n));return`[${i.join(`,`)}]`}let i=e,a=Object.keys(i),o=n===preserveString?a.sort():a.map(e=>({key:e,normalizedKey:n(e)})).sort((e,t)=>compareStableStrings(e.normalizedKey,t.normalizedKey)||compareStableStrings(e.key,t.key));r?.(`{`);let s=n===preserveString?a:[],c=``,l=0;for(let e of o){let a=typeof e==`string`?e:e.key,o=`${JSON.stringify(typeof e==`string`?e:e.normalizedKey)}:`;r?(r(`${c}${o}`),r(stringifyStableValue(i[a],t,n,r)),c=`,`):s[l++]=`${o}${stringifyStableValue(i[a],t,n)}`}return r?(r(`}`),``):`{${s.join(`,`)}}`}finally{t.delete(e)}}function encodeBase64(e){let t=``;for(let n of e)t+=String.fromCharCode(n);return btoa(t)}function compareStableStrings(e,t){return e<t?-1:+(e>t)}function sha256Hex(e){return t(`sha256`,e,`hex`)}function sha256StableValue(t,r){let i,a=``,o=0;return writeStableStringify(t,t=>{a+=t,a.length>=16384&&(i??=e(`sha256`),i.update(a),o+=n.byteLength(a),a=``)},r),o+=n.byteLength(a),{digest:i?i.update(a).digest(`hex`):sha256Hex(a),byteWeight:o}}function sha256HexPrefixCore(e,t){return sha256Hex(e).slice(0,t)}export{stableStringify as i,sha256HexPrefixCore as n,sha256StableValue as r,sha256Hex as t};