openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
68 lines (67 loc) • 2.05 kB
JavaScript
import { t as bindAbortRelay } from "./fetch-timeout-D62KciOm.mjs";
import { r as normalizeRequestInitHeadersForFetch } from "./fetch-headers-r34WWC5V.mjs";
//#region src/infra/fetch.ts
const wrapFetchWithAbortSignalMarker = Symbol.for("openclaw.fetch.abort-signal-wrapped");
function withDuplex(e, t) {
const n = e?.body != null;
const r = !n && typeof Request !== "undefined" && t instanceof Request && t.body != null;
if (!n && !r) return e;
if (e && "duplex" in e) return e;
return {
...e,
duplex: "half"
};
}
/**
* Wraps fetch so Node-compatible duplex bodies, normalized headers, and foreign
* AbortSignal implementations work against runtimes expecting native signals.
*/
function wrapFetchWithAbortSignal(e) {
if (e[wrapFetchWithAbortSignalMarker]) return e;
const wrapped = ((t, n) => {
const r = normalizeRequestInitHeadersForFetch(withDuplex(n, t));
const i = r?.signal;
if (!i || typeof AbortSignal !== "undefined" && i instanceof AbortSignal || typeof AbortController === "undefined" || typeof i.addEventListener !== "function") return e(t, r);
const a = new AbortController();
const o = bindAbortRelay(a);
let s = false;
if (i.aborted) a.abort();
else {
i.addEventListener("abort", o, { once: true });
s = true;
}
const cleanup = () => {
if (!s || typeof i.removeEventListener !== "function") return;
s = false;
try {
i.removeEventListener("abort", o);
} catch {}
};
try {
return e(t, {
...r,
signal: a.signal
}).finally(cleanup);
} catch (e) {
cleanup();
throw e;
}
});
const t = Object.assign(wrapped, e);
const n = e;
t.preconnect = typeof n.preconnect === "function" ? n.preconnect.bind(n) : () => {};
Object.defineProperty(t, wrapFetchWithAbortSignalMarker, {
value: true,
enumerable: false,
configurable: false,
writable: false
});
return t;
}
function resolveFetch(e) {
const t = e ?? globalThis.fetch;
if (!t) return;
return wrapFetchWithAbortSignal(t);
}
//#endregion
export { wrapFetchWithAbortSignal as n, resolveFetch as t };