openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.38 kB
JavaScript
import{i as resolveGlobalMap}from"./global-singleton-DolL9xGt.mjs";
//!extensions/x/src/allowlist.ts
function matchesGitHubConfig(e,t){return Boolean(t?.repo&&e?.repo.toLowerCase()===t.repo.toLowerCase()&&e.minPermission===(t.minPermission??`push`))}function normalizeXUserId(e){const t=e.trim().replace(/^x:/i,``);return/^[0-9]+$/.test(t)?t:void 0}var XAllowlistChangedError=class extends Error{constructor(){super(`X allowlist changed during authorization; retrying with current policy`);this.name=`XAllowlistChangedError`}};const mutations=resolveGlobalMap(Symbol.for(`openclaw.x.allowlist-mutations`),`close-and-restart`);function readPublishedXAllowlist(e,t,n){const r=mutations.get(JSON.stringify([e.state.resolveStateDir(),t]));return r&&!r.pending?[...r.allowFrom??[],...matchesGitHubConfig(r.github,n)?r.github?.entries.map(e=>e.xUserId)??[]:[]]:[]}function openXAllowlist(e){const t=e.state.resolveStateDir();const n=e.state.openKeyedStore({namespace:`x.allowlist`,maxEntries:1e4,overflowPolicy:`reject-new`});const r=e.state.openKeyedStore({namespace:`x.verified-github`,maxEntries:1e3,overflowPolicy:`reject-new`});const accountPrefix=e=>`${encodeURIComponent(e)}:`;const mutationState=e=>{const n=JSON.stringify([t,e]);let r=mutations.get(n);if(!r){r={generation:{},pending:0};mutations.set(n,r)}return{key:n,state:r}};const mutate=async(e,t,n)=>{t?.();const{state:r}=mutationState(e);r.generation={};r.allowFrom=void 0;r.github=void 0;r.pending++;try{return await n()}finally{r.pending--;r.generation={}}};const list=async e=>{const t=accountPrefix(e);return(await n.entries()).filter(e=>e.key.startsWith(t)).map(e=>e.value).toSorted((e,t)=>e.userId.localeCompare(t.userId))};return{list,async readGitHub(e,t){if(!t?.repo)return;const n=await r.lookup(e);return matchesGitHubConfig(n,t)?n:void 0},async replaceGitHub(e,t,n){const i=await r.lookup(e);n();const ids=e=>JSON.stringify([...new Set(e.map(e=>e.xUserId))].toSorted());if((!i||i.repo===t.repo&&i.minPermission===t.minPermission)&&ids(i?.entries??[])===ids(t.entries)){await r.register(e,t,{assertCurrent:n});return}await mutate(e,n,()=>r.register(e,t,{assertCurrent:n}))},invalidate(e){const{state:t}=mutationState(e);t.generation={};t.allowFrom=void 0;t.github=void 0},async readSnapshot(e,t){const{key:n,state:i}=mutationState(e);const a=i.generation;const assertCurrent=()=>{if(mutations.get(n)!==i||i.pending||i.generation!==a)throw new XAllowlistChangedError};assertCurrent();const[o,s]=await Promise.all([list(e),t?.repo?r.lookup(e):void 0]);const c=o.map(e=>e.userId);assertCurrent();i.allowFrom=c;i.github=matchesGitHubConfig(s,t)?s:void 0;return{allowFrom:[...c,...i.github?.entries.map(e=>e.xUserId)??[]],github:i.github,assertCurrent}},async put(e,t,r){await mutate(e,r,()=>n.register(`${accountPrefix(e)}${t.userId}`,t,{assertCurrent:r}))},async remove(e,t,r){return await mutate(e,r,()=>n.delete(`${accountPrefix(e)}${t}`,{assertCurrent:r}))}}}function mergeXAllowlist(e,t){const n=/* @__PURE__ */ new Map;for(const e of t)n.set(e.userId,{...e,configured:false,editable:true});for(const t of e){const e=normalizeXUserId(t);if(e)n.set(e,{...n.get(e),userId:e,configured:true,editable:n.has(e)})}return[...n.values()].toSorted((e,t)=>e.userId.localeCompare(t.userId))}export{readPublishedXAllowlist as a,openXAllowlist as i,mergeXAllowlist as n,normalizeXUserId as r,XAllowlistChangedError as t};