import { ProxyAgent, fetch as undiciFetch } from "undici"; import { danger } from "../../../../src/globals.js"; import { wrapFetchWithAbortSignal } from "../../../../src/infra/fetch.js"; import type { RuntimeEnv } from "../../../../src/runtime.js"; export function resolveDiscordRestFetch( proxyUrl: string | undefined, runtime: RuntimeEnv, ): typeof fetch { const proxy = proxyUrl?.trim(); if (!proxy) { return fetch; } try { const agent = new ProxyAgent(proxy); const fetcher = ((input: RequestInfo | URL, init?: RequestInit) => undiciFetch(input as string | URL, { ...(init as Record), dispatcher: agent, }) as unknown as Promise) as typeof fetch; runtime.log?.("discord: rest proxy enabled"); return wrapFetchWithAbortSignal(fetcher); } catch (err) { runtime.error?.(danger(`discord: invalid rest proxy: ${String(err)}`)); return fetch; } }