openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
51 lines (50 loc) • 1.81 kB
JavaScript
import { y as toErrorObject } from "./paths-0xDagtub.mjs";
import "./utils-BclMknzT.mjs";
import { F as resolveTimerTimeoutMs } from "./path-guards-quCEQx1i.mjs";
import { r as racePromiseWithAbortSignal } from "./src-DIZdnDBl.mjs";
//#region src/infra/http-response-body-timeout.ts
function createResponseBodyTimeoutError(e) {
const t = new Error(e);
t.name = "TimeoutError";
return t;
}
async function withResponseBodyTimeout(e) {
if (e.timeoutMs === void 0 && !e.signal) return await e.read();
let t;
let n = e.signal;
try {
if (e.timeoutMs !== void 0) {
const r = new AbortController();
n = n ? AbortSignal.any([n, r.signal]) : r.signal;
const i = resolveTimerTimeoutMs(e.timeoutMs, 1);
t = setTimeout(() => {
r.abort(e.onTimeout?.({ timeoutMs: i }) ?? createResponseBodyTimeoutError(`Response body timed out after ${i}ms`));
}, i).unref();
}
return await racePromiseWithAbortSignal(() => Promise.resolve().then(() => {
n?.throwIfAborted();
return e.read(() => {
n?.throwIfAborted();
t?.refresh();
});
}), n, (e) => toErrorObject(e.reason, "Response body read aborted"));
} catch (t) {
const r = toErrorObject(t, "Non-Error rejection");
if (n?.aborted) e.cancel(r).catch(() => void 0);
throw r;
} finally {
clearTimeout(t);
}
}
/** Owns one refreshable idle deadline for a bounded response-body operation. */
function withResponseBodyIdleTimeout(e, t, n, r) {
if (t === void 0) return r();
return withResponseBodyTimeout({
timeoutMs: t,
onTimeout: ({ timeoutMs: e }) => n?.({ chunkTimeoutMs: e }) ?? createResponseBodyTimeoutError(`Media download stalled: no data received for ${e}ms`),
cancel: async (t) => await e.cancel(t),
read: r
});
}
//#endregion
export { withResponseBodyTimeout as n, withResponseBodyIdleTimeout as t };