openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
55 lines (54 loc) • 2.06 kB
JavaScript
import { R as resolveTimerTimeoutMs } from "./number-coercion-D6u-4YD8.mjs";
import { p as toErrorObject } from "./error-coercion-hDrHOSGy.mjs";
import { o as racePromiseWithAbortSignal } from "./src-BPmQBLJp.mjs";
import "./abort-signal-D4Q6SEw7.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
});
}
/** Reads one chunk, rejecting and cancelling the reader after an idle timeout. */
async function readChunkWithIdleTimeout(e, t, n) {
return await withResponseBodyIdleTimeout(e, t, n, () => e.read());
}
//#endregion
export { withResponseBodyIdleTimeout as n, withResponseBodyTimeout as r, readChunkWithIdleTimeout as t };