diff --git a/extensions/matrix/src/matrix/sdk/transport.ts b/extensions/matrix/src/matrix/sdk/transport.ts index 892f6a168b1..1cab2fe6004 100644 --- a/extensions/matrix/src/matrix/sdk/transport.ts +++ b/extensions/matrix/src/matrix/sdk/transport.ts @@ -1,5 +1,7 @@ -import type { PinnedDispatcherPolicy } from "openclaw/plugin-sdk/infra-runtime"; -import { fetchWithRuntimeDispatcher } from "openclaw/plugin-sdk/infra-runtime"; +import { + fetchWithRuntimeDispatcher, + type PinnedDispatcherPolicy, +} from "openclaw/plugin-sdk/infra-runtime"; import { buildTimeoutAbortSignal, closeDispatcher, diff --git a/src/infra/net/fetch-guard.ts b/src/infra/net/fetch-guard.ts index 0520c7c12cb..799d15ec604 100644 --- a/src/infra/net/fetch-guard.ts +++ b/src/infra/net/fetch-guard.ts @@ -3,6 +3,11 @@ import { logWarn } from "../../logger.js"; import { buildTimeoutAbortSignal } from "../../utils/fetch-timeout.js"; import { hasProxyEnvConfigured } from "./proxy-env.js"; import { retainSafeHeadersForCrossOriginRedirect as retainSafeRedirectHeaders } from "./redirect-headers.js"; +import { + fetchWithRuntimeDispatcher, + isMockedFetch, + type DispatcherAwareRequestInit, +} from "./runtime-fetch.js"; import { closeDispatcher, createPinnedDispatcher, @@ -15,7 +20,6 @@ import { import { loadUndiciRuntimeDeps } from "./undici-runtime.js"; type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise; -type DispatcherAwareRequestInit = RequestInit & { dispatcher?: Dispatcher }; export const GUARDED_FETCH_MODE = { STRICT: "strict", @@ -155,13 +159,6 @@ function isRedirectStatus(status: number): boolean { return status === 301 || status === 302 || status === 303 || status === 307 || status === 308; } -function isMockedFetch(fetchImpl: FetchLike | undefined): boolean { - if (typeof fetchImpl !== "function") { - return false; - } - return typeof (fetchImpl as FetchLike & { mock?: unknown }).mock === "object"; -} - function isAmbientGlobalFetch(params: { fetchImpl: FetchLike | undefined; globalFetch: FetchLike | undefined; @@ -227,16 +224,7 @@ function rewriteRedirectInitForMethod(params: { }; } -export async function fetchWithRuntimeDispatcher( - input: RequestInfo | URL, - init?: DispatcherAwareRequestInit, -): Promise { - const runtimeFetch = loadUndiciRuntimeDeps().fetch as unknown as ( - input: RequestInfo | URL, - init?: DispatcherAwareRequestInit, - ) => Promise; - return (await runtimeFetch(input, init)) as Response; -} +export { fetchWithRuntimeDispatcher } from "./runtime-fetch.js"; export async function fetchWithSsrFGuard(params: GuardedFetchOptions): Promise { const defaultFetch: FetchLike | undefined = params.fetchImpl ?? globalThis.fetch; diff --git a/src/infra/net/runtime-fetch.ts b/src/infra/net/runtime-fetch.ts new file mode 100644 index 00000000000..2cc41ea720e --- /dev/null +++ b/src/infra/net/runtime-fetch.ts @@ -0,0 +1,24 @@ +import type { Dispatcher } from "undici"; +import { loadUndiciRuntimeDeps } from "./undici-runtime.js"; + +export type DispatcherAwareRequestInit = RequestInit & { dispatcher?: Dispatcher }; + +type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise; + +export function isMockedFetch(fetchImpl: FetchLike | undefined): boolean { + if (typeof fetchImpl !== "function") { + return false; + } + return typeof (fetchImpl as FetchLike & { mock?: unknown }).mock === "object"; +} + +export async function fetchWithRuntimeDispatcher( + input: RequestInfo | URL, + init?: DispatcherAwareRequestInit, +): Promise { + const runtimeFetch = loadUndiciRuntimeDeps().fetch as unknown as ( + input: RequestInfo | URL, + init?: DispatcherAwareRequestInit, + ) => Promise; + return (await runtimeFetch(input, init)) as Response; +}