UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

117 lines (116 loc) • 3.47 kB
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs"; import { o as redactSensitiveUrlLikeString } from "./redact-sensitive-url-D-E7DGoc.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { c as resolveSafeTimeoutDelayMs } from "./timeouts-nwfOzsiL.mjs"; //#region src/utils/fetch-timeout.ts const log = createSubsystemLogger("fetch-timeout"); const LOG_URL_MAX_CHARS = 500; const URL_SECRET_SUFFIX_PATTERN = /[?#]/; /** * Relay abort without forwarding the Event argument as the abort reason. * Using .bind() avoids closure scope capture (memory leak prevention). */ function relayAbort() { this.abort(); } /** Returns a bound abort relay for use as an event listener. */ function bindAbortRelay(e) { return relayAbort.bind(e); } function sanitizeTimeoutLogUrl(e) { const t = e?.trim(); if (!t) return; try { const e = new URL(t); e.username = ""; e.password = ""; e.search = ""; e.hash = ""; const n = redactSensitiveUrlLikeString(e.toString()); return n.length > LOG_URL_MAX_CHARS ? `${truncateUtf16Safe(n, LOG_URL_MAX_CHARS)}...` : n; } catch { const e = t.split(URL_SECRET_SUFFIX_PATTERN, 1)[0] ?? ""; const n = redactSensitiveUrlLikeString(e.replace(/[\r\n\u2028\u2029]+/g, " ").replace(/\p{Cc}+/gu, " ").replace(/\s+/g, " ").trim()); if (!n) return; return n.length > LOG_URL_MAX_CHARS ? `${truncateUtf16Safe(n, LOG_URL_MAX_CHARS)}...` : n; } } function abortDueToTimeout(e, t, n, r, i, a, o = true) { if (a?.aborted ?? e.signal.aborted) return; const s = sanitizeTimeoutLogUrl(i); const c = Math.max(0, Date.now() - n); const l = Math.max(0, c - t); const u = l >= Math.max(1e3, t * .5) ? `timer delayed ${l}ms, likely event-loop starvation` : null; const d = [ `fetch timeout after ${t}ms`, `(elapsed ${c}ms)`, u, r ? `operation=${r}` : null, s ? `url=${s}` : null ].filter((e) => Boolean(e)).join(" "); if (o) log.warn("fetch timeout reached; aborting operation", { timeoutMs: t, elapsedMs: c, ...u ? { timerDelayMs: l, eventLoopDelayHint: u } : {}, consoleMessage: d, ...r ? { operation: r } : {}, ...s ? { url: s } : {} }); const f = /* @__PURE__ */ new Error("request timed out"); f.name = "TimeoutError"; e.abort(f); } /** * Builds an abort signal that combines an optional parent signal with a timeout. * Callers must run `cleanup`; `refresh` restarts only the internal timeout timer. */ function buildTimeoutAbortSignal(e) { const { timeoutMs: t, signal: n } = e; if (!t) return { signal: n, cleanup: () => {}, refresh: () => {} }; const r = new AbortController(); const i = n ? AbortSignal.any([n, r.signal]) : r.signal; const a = resolveSafeTimeoutDelayMs(t); let o = true; let s; const scheduleTimeout = () => { s = setTimeout(abortDueToTimeout, a, r, a, Date.now(), e.operation, e.url, i, e.logTimeout); }; scheduleTimeout(); return { signal: i, refresh: () => { if (!o || i.aborted) return; clearTimeout(s); scheduleTimeout(); }, cleanup: () => { o = false; clearTimeout(s); } }; } async function fetchWithTimeout(e, t, n, r = fetch) { const { signal: i, cleanup: a } = buildTimeoutAbortSignal({ timeoutMs: Math.max(1, n), signal: t.signal ?? void 0, operation: "fetchWithTimeout", url: e }); try { return await r(e, { ...t, signal: i }); } finally { a(); } } //#endregion export { buildTimeoutAbortSignal as n, fetchWithTimeout as r, bindAbortRelay as t };