mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-12 01:31:08 +00:00
Net: extract runtime fetch helper
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
24
src/infra/net/runtime-fetch.ts
Normal file
24
src/infra/net/runtime-fetch.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user