openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
64 lines (63 loc) • 2.27 kB
JavaScript
import { t as isPromiseLike } from "./promise-like-aMoWEh5w.mjs";
import { t as AsyncContext } from "./async-context-BYDAvG9A.mjs";
//#region src/infra/net/fetch-request-authority.ts
const requestAuthority = new AsyncContext(Symbol.for("openclaw.guardedFetchRequestAuthority"));
/** Egress-only checks require managed fetch; lifetime assertions may also guard native transports. */
function hasGuardedFetchBeforeRequest() {
return requestAuthority.getStore()?.beforeRequest !== void 0;
}
/** Capture before transport preparation so redirects retain the original caller. */
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.");
}
}
/**
* Add a synchronous egress check to guarded requests started by run, including
* requests a returned stream sends later. It narrows inherited authority only.
*/
function withGuardedFetchBeforeRequest(e, t) {
if (!e) return t();
const n = requestAuthority.getStore();
return requestAuthority.run({
assertCurrent: () => n?.assertCurrent(),
beforeRequest: () => {
n?.beforeRequest?.();
invokeSynchronousGuard(e);
}
}, t);
}
/** Carry a host-owned assertion through provider preparation without granting new authority. */
async function withGuardedFetchRequestAuthority(e, t, n) {
const r = requestAuthority.getStore();
if (!e && !n && !r) return await t(void 0);
let i = true;
const assertAuthority = () => {
r?.assertCurrent();
if (!i) throw new Error("Guarded request authority is no longer active.");
invokeSynchronousGuard(e);
};
try {
assertAuthority();
return await requestAuthority.run({
assertCurrent: assertAuthority,
beforeRequest: n || r?.beforeRequest ? () => {
r?.beforeRequest?.();
invokeSynchronousGuard(n);
} : void 0
}, () => t(assertAuthority));
} finally {
i = false;
}
}
//#endregion
export { withGuardedFetchRequestAuthority as i, hasGuardedFetchBeforeRequest as n, withGuardedFetchBeforeRequest as r, captureGuardedFetchRequestAuthority as t };