UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.47 kB
import{n as notifyListeners,r as registerListener}from"./listeners-awzYn4LE.mjs"; //!src/gateway/device-revocation.ts const owners=/* @__PURE__ */ new WeakMap;const captures=/* @__PURE__ */ new WeakMap;const sourceIdentities=/* @__PURE__ */ new WeakMap;function sameReconnectSource(e,t){return e&&t?e.device.deviceId===t.device.deviceId&&e.device.key===t.device.key&&e.profileId===t.profileId&&e.role===t.role&&e.scopes===t.scopes&&e.clientId===t.clientId&&e.clientMode===t.clientMode&&e.pairedClientId===t.pairedClientId&&e.transportPolicyGeneration===t.transportPolicyGeneration&&e.verifiedIdentity===t.verifiedIdentity&&e.origin===t.origin&&e.requestHost===t.requestHost&&e.isLocalClient===t.isLocalClient&&e.accessOwner===t.accessOwner:e===t}function retainSourceIdentity(e,{client:t,...n}){let r=n.reconnect?e.reconnectSources??=/* @__PURE__ */ new Set:sourceIdentities.get(t);if(!r){r=/* @__PURE__ */ new Set;sourceIdentities.set(t,r)}let i=[...r].find(e=>e.dependencies.context===n.context&&e.dependencies.authPolicyGeneration===n.authPolicyGeneration&&e.dependencies.sharedGenerationOwner===n.sharedGenerationOwner&&e.dependencies.authorityOwner===n.authorityOwner&&e.dependencies.sharedGeneration===n.sharedGeneration&&sameReconnectSource(e.dependencies.reconnect,n.reconnect));if(!i){i={dependencies:n,token:Object.freeze({}),references:0};r.add(i)}const a=i;const o=r;a.references+=1;return{token:a.token,release:()=>{if(--a.references===0)o.delete(a)}}}function getOwner(e){let t=owners.get(e);if(!t){t={closed:false,devices:/* @__PURE__ */ new Map};owners.set(e,t)}return t}function releaseHold(e){const{owner:t,state:n}=e;let r=false;return()=>{if(r)return;r=true;n.references-=1;if(n.references!==0)return;e.releaseSourceIdentity?.();e.releaseSourceIdentity=void 0;e.releaseClientRevocation?.();e.releaseClientRevocation=void 0;n.listeners?.clear();if(!n.deviceId)return;const i=t.devices.get(n.deviceId);i?.delete(n);if(i?.size===0)t.devices.delete(n.deviceId)}}function revoke(e){if(e.revoked)return;e.revoked=true;notifyListeners(e.listeners??[],void 0)}function captureGatewayDeviceRevocation(e,t,n,r,i){const a=getOwner(e);const o={deviceId:t.deviceId,role:t.role,references:1,revoked:false,sourceAccepted:false};if(o.deviceId&&!a.closed){let e=a.devices.get(o.deviceId);if(!e){e=/* @__PURE__ */ new Set;a.devices.set(o.deviceId,e)}e.add(o)}const isRevocationCurrent=()=>!a.closed&&!o.revoked&&(o.references>0||!o.sourceAccepted&&r?.aborted===false);const isCurrent=()=>isRevocationCurrent()&&(o.sourceAccepted&&i?i.isCurrent():n());const s={owner:a,state:o,isCurrent,isSourceCurrent:i?()=>isRevocationCurrent()&&i.isCurrent():isCurrent,hasSourceAuthority:i!==void 0,isRevocationCurrent,sourceIdentity:Object.freeze({})};captures.set(isCurrent,s);s.releaseClientRevocation=i?.subscribe(()=>revoke(o));if(i?.dependencies){const e=retainSourceIdentity(a,i.dependencies);s.reconnectSourceClient=i.dependencies.reconnect?i.dependencies.client:void 0;s.sourceIdentity=e.token;s.releaseSourceIdentity=e.release}return{isCurrent,release:releaseHold(s)}}function bindGatewayDeviceRevocation(e,t){const n=t?captures.get(t):void 0;if(n)captures.set(e,n);return e}function readGatewayDeviceRevocationGuard(e){return e?captures.get(e)?.isRevocationCurrent:void 0}function readGatewayDeviceCommittedSourceAuthority(e){const t=e?captures.get(e):void 0;return t?.hasSourceAuthority?t.isSourceCurrent:void 0}function readGatewayDeviceSourceAuthority(e){return e?captures.get(e)?.isSourceCurrent:void 0}function acceptGatewayDeviceSourceAuthority(e){const t=e?captures.get(e):void 0;if(!t?.hasSourceAuthority||t.state.references===0||!t.isSourceCurrent())return false;t.state.sourceAccepted=true;return true}function readAcceptedGatewayDeviceSourceAuthority(e){const t=e?captures.get(e):void 0;return t?.state.sourceAccepted?t.isSourceCurrent:void 0}function hasPreparedGatewayDeviceAuthority(e,t){const n=readAcceptedGatewayDeviceSourceAuthority(t);return(n?n():!e?.invalidated)&&readGatewayDeviceRevocationGuard(t)?.()!==false}function readGatewayDeviceSourceIdentity(e){return e?captures.get(e)?.sourceIdentity:void 0}function readGatewayDeviceReconnectSourceIdentity(e,t){const n=e?captures.get(e):void 0;return n?.reconnectSourceClient===t?n.sourceIdentity:void 0}function onGatewayDeviceSourceRevoked(e,t){const n=e?captures.get(e):void 0;if(!n)return;const r=registerListener(n.state.listeners??=/* @__PURE__ */ new Set,t);if(n.state.revoked)t();return r}function retainGatewayDeviceRevocation(e){const t=e?captures.get(e):void 0;if(!t)return;if(!t.isCurrent()||t.state.references===0)throw new Error(`Gateway caller authority is no longer active.`);t.state.references+=1;return releaseHold(t)}function invalidateGatewayDeviceRevocation(e,t,n){for(const r of owners.get(e)?.devices.get(t)??[])if(!n||r.role===n)revoke(r)}function closeGatewayDeviceRevocation(e){const t=getOwner(e);t.closed=true;t.devices.clear()}export{hasPreparedGatewayDeviceAuthority as a,readAcceptedGatewayDeviceSourceAuthority as c,readGatewayDeviceRevocationGuard as d,readGatewayDeviceSourceAuthority as f,closeGatewayDeviceRevocation as i,readGatewayDeviceCommittedSourceAuthority as l,retainGatewayDeviceRevocation as m,bindGatewayDeviceRevocation as n,invalidateGatewayDeviceRevocation as o,readGatewayDeviceSourceIdentity as p,captureGatewayDeviceRevocation as r,onGatewayDeviceSourceRevoked as s,acceptGatewayDeviceSourceAuthority as t,readGatewayDeviceReconnectSourceIdentity as u};