Net: extract runtime fetch helper

This commit is contained in:
Gustavo Madeira Santana 2026-04-05 22:11:43 -04:00
parent d112441019
commit 19ddc325d8
3 changed files with 34 additions and 20 deletions

View File

@ -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,

View File

@ -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<Response>;
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<Response> {
const runtimeFetch = loadUndiciRuntimeDeps().fetch as unknown as (
input: RequestInfo | URL,
init?: DispatcherAwareRequestInit,
) => Promise<unknown>;
return (await runtimeFetch(input, init)) as Response;
}
export { fetchWithRuntimeDispatcher } from "./runtime-fetch.js";
export async function fetchWithSsrFGuard(params: GuardedFetchOptions): Promise<GuardedFetchResult> {
const defaultFetch: FetchLike | undefined = params.fetchImpl ?? globalThis.fetch;

View File

@ -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<Response>;
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<Response> {
const runtimeFetch = loadUndiciRuntimeDeps().fetch as unknown as (
input: RequestInfo | URL,
init?: DispatcherAwareRequestInit,
) => Promise<unknown>;
return (await runtimeFetch(input, init)) as Response;
}