openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
91 lines (90 loc) • 3.12 kB
JavaScript
import { n as consumeResponseBytes, t as decodeTextPrefix } from "./src-H3RMXyFL.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { n as withResponseBodyIdleTimeout, r as withResponseBodyTimeout } from "./http-response-body-timeout-BomM6jjh.mjs";
//#region src/infra/http-response-body.ts
/** Requests cancellation only when no consumer has started reading the body. */
async function cancelUnreadResponseBody(e) {
if (e && !e.bodyUsed) e.body?.cancel().catch(() => void 0);
}
async function readResponsePrefix(e, t, n, r) {
if (!Number.isFinite(t) || t < 0) throw new RangeError(`maxBytes must be a non-negative finite number: ${t}`);
let i;
try {
i = typeof r?.timeoutMs === "function" ? r.timeoutMs() : r?.timeoutMs;
} catch (t) {
e.body?.cancel(t).catch(() => void 0);
throw t;
}
const a = e.body;
const o = a && typeof a.getReader === "function" ? a.getReader() : void 0;
try {
return await withResponseBodyTimeout({
timeoutMs: i,
onTimeout: r?.onTimeout,
signal: r?.signal,
cancel: async (e) => await (o ?? a)?.cancel(e),
read: async () => {
if (!o) {
const n = Buffer.from(await e.arrayBuffer());
const r = n.length > t;
return {
materializeBuffer: () => r ? n.subarray(0, t) : n,
size: n.length,
truncated: r
};
}
const i = [];
const a = await withResponseBodyIdleTimeout(o, r?.chunkTimeoutMs || void 0, r?.onIdleTimeout, (e) => consumeResponseBytes({
maxBytes: t,
stopAtLimit: n,
read: () => {
e?.();
return o.read();
},
onChunk: (e) => i.push(e),
onLimit: () => {
o.cancel().catch(() => void 0);
}
}));
return {
materializeBuffer: () => Buffer.concat(i, Math.floor(Math.min(a.size, t))),
...a
};
}
});
} finally {
o?.releaseLock();
}
}
/** Reads and decodes a bounded text prefix while cancelling unread overflow. */
async function readResponseTextPrefix(e, t, n) {
const r = await readResponsePrefix(e, t, true, n);
return {
text: decodeTextPrefix(r.materializeBuffer(), { truncated: r.truncated }),
size: r.size,
truncated: r.truncated
};
}
/** Reads a response body under byte, idle, and overall timeout bounds. */
async function readResponseWithLimit(e, t, n) {
const r = n?.onOverflow;
const i = await readResponsePrefix(e, t, false, n);
if (i.truncated) throw r ? r({
size: i.size,
maxBytes: t,
res: e
}) : /* @__PURE__ */ new Error(`Content too large: ${i.size} bytes (limit: ${t} bytes)`);
return i.materializeBuffer();
}
/** Reads a small collapsed text prefix from a response body for diagnostics/errors. */
async function readResponseTextSnippet(e, t) {
const n = t?.maxBytes ?? 8192;
const r = t?.maxChars ?? 200;
const i = await readResponseTextPrefix(e, n, t);
const a = i.text.replace(/\s+/g, " ").trim();
if (!a) return;
if (a.length > r) return `${truncateUtf16Safe(a, r)}…`;
return i.truncated ? `${a}…` : a;
}
//#endregion
export { readResponseWithLimit as i, readResponseTextPrefix as n, readResponseTextSnippet as r, cancelUnreadResponseBody as t };