openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 6.87 kB
JavaScript
import{o as getAsyncWorkSignal,t as AsyncWorkScope}from"./async-work-scope-DBcxYU4O.mjs";import{t as isAbortError}from"./abort-signal-BKkRe6pu.mjs";import{o as emitInternalDiagnosticEvent}from"./diagnostic-events-BYBrlAdF.mjs";import{h as readRuntimeConfig}from"./runtime-snapshot-Bg5K4JNu.mjs";import{m as readUserProfileAliasRevision}from"./user-profile-events-CkQlfQnE.mjs";import{r as isOperatorAuthorityUnavailable,t as GatewayOperatorAccessUnavailableError}from"./operator-authority-error-B9EU9j0W.mjs";import"./io-Di_yuX3t.mjs";import{t as ToolAuthorizationError}from"./tool-input-error-BCfbFipG.mjs";import{u as readOperatorRolePolicyRevision}from"./operator-role-policy-g-dxw0h4.mjs";import{t as isNamedOperatorRoleEnforcementEnabled}from"./operator-role-enforcement-FsaRvImR.mjs";import{n as GatewayOperatorAccessDeniedError,r as hasCurrentGatewayOperatorAccess,t as GATEWAY_OPERATOR_ACCESS_DENIED_MESSAGE}from"./operator-access-policy-lm0chPjw.mjs";import{d as runHttpConnectionRequest,l as createHttpRequestAbortSignal,p as waitForHttpRequestRejection}from"./http-body-CsPV_uT5.mjs";import{r as sourceRolePolicies}from"./operator-role-source-policy-FkG4nDbU.mjs";import{a as getRemoteControlUiIngressContext,r as assertRemoteControlUiIngressCurrent}from"./ingress-attribution-Ct_hJMPU.mjs";import{r as isGatewayAuthPolicyCurrent,t as captureGatewayAuthPolicy}from"./auth-policy-DPmo4h9a.mjs";import{t as resolveSharedGatewaySessionGeneration}from"./ws-shared-generation-BJ1DAER5.mjs";import{t as sendJson}from"./http-json-g_k96dZ_.mjs";import{h as sendUnauthorized,l as sendGatewayAuthFailure,o as prepareGatewayHttpErrorResponse}from"./http-common-kI7-c_LU.mjs";import{AsyncLocalStorage}from"node:async_hooks";import{isDeepStrictEqual}from"node:util";
//!src/gateway/http-operator-access.ts
function sendGatewayHttpAuthFailure(e,t,{retryable:n=true}={}){if(t.reason===`operator_access_unavailable`){if(prepareGatewayHttpErrorResponse(e,`Service Unavailable`))sendJson(e,503,{error:{message:n?new GatewayOperatorAccessUnavailableError().message:`Gateway access policy is unavailable. The operation may have completed; check its status before retrying.`,type:`service_unavailable`,retryable:n}});return}if(t.reason!==`operator_access_denied`){sendGatewayAuthFailure(e,t);return}if(!prepareGatewayHttpErrorResponse(e,`Forbidden`))return;sendJson(e,403,{error:{message:GATEWAY_OPERATOR_ACCESS_DENIED_MESSAGE,type:`forbidden`}})}function bindHttpOperatorAccessAuthority(e,t,n=true){if(!t)return true;let r;try{if(!hasCurrentGatewayOperatorAccess(t))r=`operator_access_denied`}catch(e){if(!isOperatorAuthorityUnavailable(e))throw e;r=`operator_access_unavailable`}if(r){if(n&&!e.writableEnded&&!e.destroyed)sendGatewayHttpAuthFailure(e,{ok:false,reason:r});return false}if(e.writableEnded||e.destroyed)return false;const release=()=>{t.signal.removeEventListener(`abort`,revoke);e.off(`finish`,release);e.off(`close`,release)};const revoke=()=>{release();if(!e.writableEnded&&!e.destroyed)e.destroy()};t.signal.addEventListener(`abort`,revoke,{once:true});e.once(`finish`,release);e.once(`close`,release);return true}
//!src/gateway/http-request-authority.ts
function runGatewayHttpRequest(e,t,n,r){if(n?.requestEntryLifetime?.signal.aborted){if(!t.writableFinished&&!t.destroyed)t.destroy();return Promise.resolve()}const run=async()=>{const i=getRemoteControlUiIngressContext(e);assertRemoteControlUiIngressCurrent(i);const a=new AsyncWorkScope;return a.run(async()=>{const o=AsyncLocalStorage.snapshot();const s=createHttpRequestAbortSignal(e,t);const c=n?.requestEntryLifetime?.signal;const l=AbortSignal.any([s.signal,...c?[c]:[],...i?[i.signal]:[]]);let u;const cancel=()=>o(()=>{u=s.signal.aborted?`client`:`shutdown`;a.beginClose(l.reason);if(u===`shutdown`&&!t.writableFinished&&!t.destroyed)t.destroy()});l.addEventListener(`abort`,cancel,{once:true});if(l.aborted)cancel();let d=false;try{await a.track(()=>runHttpConnectionRequest(e,async()=>{d=await r()===`failed`},t))}catch(e){d=true;throw e}finally{l.removeEventListener(`abort`,cancel);s.cleanup();if(u&&!d&&t.statusCode<500&&!(u===`client`&&waitForHttpRequestRejection(e)))emitInternalDiagnosticEvent({type:`gateway.http.cancelled`,source:u});await o(()=>a.drain())}})};return n?n.trackExecution(run):run()}var GatewayHttpRequestAuthorityError=class extends Error{};function finishGatewayHttpAuthorityError(e,t){const n=getAsyncWorkSignal();if(n?.aborted&&(t===n.reason||isAbortError(t))){if(!e.writableEnded&&!e.destroyed)e.destroy();return true}if(t instanceof GatewayOperatorAccessDeniedError){sendGatewayHttpAuthFailure(e,{ok:false,reason:`operator_access_denied`});return true}if(isOperatorAuthorityUnavailable(t)){sendGatewayHttpAuthFailure(e,{ok:false,reason:`operator_access_unavailable`},{retryable:false});return true}return t instanceof GatewayHttpRequestAuthorityError}function assertGatewayHttpRequestCurrent(e){if(e.hasCurrentClientAuthority?.()===false)throw new ToolAuthorizationError(`Gateway requester authority changed`)}function captureHttpRequestAuthority(e){const t=e.cfg??readRuntimeConfig();const n=captureGatewayAuthPolicy(t,null);const r=resolveSharedGatewaySessionGeneration(e.auth,e.trustedProxies??t.gateway?.trustedProxies);const i=t.gateway?.roles;const a=sourceRolePolicies(i);const o=isNamedOperatorRoleEnforcementEnabled(t)?readOperatorRolePolicyRevision():void 0;const s=readUserProfileAliasRevision();const c=getRemoteControlUiIngressContext(e.req);return()=>{if(c)try{assertRemoteControlUiIngressCurrent(c)}catch{return false}const t=e.getRuntimeConfig?.()??readRuntimeConfig();return!e.req.socket?.destroyed&&isGatewayAuthPolicyCurrent(n,t)&&(i===t.gateway?.roles||isDeepStrictEqual(a,sourceRolePolicies(t.gateway?.roles)))&&(o===void 0||o===readOperatorRolePolicyRevision())&&s===readUserProfileAliasRevision()&&r===resolveSharedGatewaySessionGeneration(e.getResolvedAuth?.()??e.auth,e.trustedProxies??t.gateway?.trustedProxies)}}function bindHttpResponseAuthority(e,t,n){const assertCurrent=()=>{if(t.writableEnded||t.destroyed)throw new GatewayHttpRequestAuthorityError(`HTTP request authority expired`);if(!hasCurrentGatewayOperatorAccess(e.operatorAccessAuthority))throw new GatewayOperatorAccessDeniedError;if(!n()||e.hasCurrentProfileAuthority?.()===false){sendUnauthorized(t);throw new GatewayHttpRequestAuthorityError(`Unauthorized`)}};return{...e,hasCurrentClientAuthority:()=>!t.writableEnded&&!t.destroyed&&n()&&e.hasCurrentProfileAuthority?.()!==false&&hasCurrentGatewayOperatorAccess(e.operatorAccessAuthority),assertCurrent,revalidate:async()=>assertCurrent()}}export{finishGatewayHttpAuthorityError as a,sendGatewayHttpAuthFailure as c,captureHttpRequestAuthority as i,assertGatewayHttpRequestCurrent as n,runGatewayHttpRequest as o,bindHttpResponseAuthority as r,bindHttpOperatorAccessAuthority as s,GatewayHttpRequestAuthorityError as t};