openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 1.85 kB
JavaScript
import{t as isPromiseLike}from"./promise-like-OKzBwzLG.mjs";import{t as AsyncContext}from"./async-context-D4i8WbBE.mjs";
//!src/infra/net/fetch-request-authority.ts
const requestAuthority=new AsyncContext(Symbol.for(`openclaw.guardedFetchRequestAuthority`));function hasGuardedFetchBeforeRequest(){return requestAuthority.getStore()?.beforeRequest!==void 0}function captureGuardedFetchRequestAuthority(){const e=requestAuthority.getStore();return e?()=>{e.assertCurrent();e.beforeRequest?.()}:void 0}function invokeSynchronousGuard(e){const t=e?.();if(isPromiseLike(t)){Promise.resolve(t).catch(()=>void 0);throw new TypeError(`Guarded request authority must be synchronous.`)}}function withGuardedFetchBeforeRequest(e,t){if(!e)return t();const n=requestAuthority.getStore();return requestAuthority.run({assertCurrent:()=>n?.assertCurrent(),beforeRequest:()=>{n?.beforeRequest?.();invokeSynchronousGuard(e)}},t)}function createRequestAuthority(e){let t=true;const assertAuthority=()=>{e.inherited?.assertCurrent();if(!t)throw new Error(`Guarded request authority is no longer active.`);invokeSynchronousGuard(e.assertCurrent)};return{assertCurrent:assertAuthority,beforeRequest:e.beforeRequest||e.inherited?.beforeRequest?()=>{e.inherited?.beforeRequest?.();invokeSynchronousGuard(e.beforeRequest)}:void 0,close(){t=false;e.inherited=void 0;e.assertCurrent=void 0;e.beforeRequest=void 0}}}async function withGuardedFetchRequestAuthority(e,t,n){const r=requestAuthority.getStore();if(!e&&!n&&!r)return await t(void 0);const i=createRequestAuthority({assertCurrent:e,beforeRequest:n,inherited:r});try{i.assertCurrent();return await requestAuthority.run(i,()=>t(i.assertCurrent))}finally{i.close()}}export{withGuardedFetchRequestAuthority as i,hasGuardedFetchBeforeRequest as n,withGuardedFetchBeforeRequest as r,captureGuardedFetchRequestAuthority as t};