From 0aaba0305fb29d25e4caac333fbb5be8a05fb271 Mon Sep 17 00:00:00 2001 From: Almeida Date: Wed, 3 Dec 2025 14:40:15 +0000 Subject: [PATCH] fix(undiciRequest): file uploading (#11320) * fix(undiciRequest): file uploading * fix: different approach * fix: revert to the previous approach --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/proxy/__tests__/proxyRequests.test.ts | 1 + packages/rest/__tests__/REST.test.ts | 2 ++ packages/rest/__tests__/RequestHandler.test.ts | 2 ++ packages/rest/__tests__/RequestManager.test.ts | 1 + packages/rest/__tests__/UndiciRequest.test.ts | 1 + packages/rest/src/strategies/undiciRequest.ts | 13 ++++++++++++- 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/proxy/__tests__/proxyRequests.test.ts b/packages/proxy/__tests__/proxyRequests.test.ts index 23bc8f94e..9fb4ac179 100644 --- a/packages/proxy/__tests__/proxyRequests.test.ts +++ b/packages/proxy/__tests__/proxyRequests.test.ts @@ -24,6 +24,7 @@ beforeEach(() => { setGlobalDispatcher(mockAgent); // enabled the mock client to intercept requests mockPool = mockAgent.get('https://discord.com'); + api.setAgent(mockAgent); }); afterEach(async () => { diff --git a/packages/rest/__tests__/REST.test.ts b/packages/rest/__tests__/REST.test.ts index 2f451699d..f733b8d28 100644 --- a/packages/rest/__tests__/REST.test.ts +++ b/packages/rest/__tests__/REST.test.ts @@ -37,6 +37,8 @@ beforeEach(() => { setGlobalDispatcher(mockAgent); // enabled the mock client to intercept requests mockPool = mockAgent.get('https://discord.com'); + api.setAgent(mockAgent); + fetchApi.setAgent(mockAgent); }); afterEach(async () => { diff --git a/packages/rest/__tests__/RequestHandler.test.ts b/packages/rest/__tests__/RequestHandler.test.ts index 6471a1add..16f02769c 100644 --- a/packages/rest/__tests__/RequestHandler.test.ts +++ b/packages/rest/__tests__/RequestHandler.test.ts @@ -481,6 +481,8 @@ test('perm server outage', async () => { test('server responding too slow', async () => { const api2 = new REST({ timeout: 1 }).setToken('A-Very-Really-Real-Token'); + api2.setAgent(mockAgent); + mockPool .intercept({ path: genPath('/slow'), diff --git a/packages/rest/__tests__/RequestManager.test.ts b/packages/rest/__tests__/RequestManager.test.ts index e0a6b6ced..c158663de 100644 --- a/packages/rest/__tests__/RequestManager.test.ts +++ b/packages/rest/__tests__/RequestManager.test.ts @@ -15,6 +15,7 @@ beforeEach(() => { setGlobalDispatcher(mockAgent); mockPool = mockAgent.get('https://discord.com'); + api.setAgent(mockAgent); }); afterEach(async () => { diff --git a/packages/rest/__tests__/UndiciRequest.test.ts b/packages/rest/__tests__/UndiciRequest.test.ts index 95e0f4b8d..90bcc057f 100644 --- a/packages/rest/__tests__/UndiciRequest.test.ts +++ b/packages/rest/__tests__/UndiciRequest.test.ts @@ -28,6 +28,7 @@ beforeEach(() => { setGlobalDispatcher(mockAgent); // enabled the mock client to intercept requests mockPool = mockAgent.get('https://discord.com'); + api.setAgent(mockAgent); }); afterEach(async () => { diff --git a/packages/rest/src/strategies/undiciRequest.ts b/packages/rest/src/strategies/undiciRequest.ts index 5eee90c5e..bafd05028 100644 --- a/packages/rest/src/strategies/undiciRequest.ts +++ b/packages/rest/src/strategies/undiciRequest.ts @@ -1,12 +1,14 @@ import { STATUS_CODES } from 'node:http'; import { URLSearchParams } from 'node:url'; import { types } from 'node:util'; -import { type RequestInit, request, Headers, FormData as UndiciFormData } from 'undici'; +import { type RequestInit, request, Headers, FormData as UndiciFormData, Agent } from 'undici'; import type { HeaderRecord } from 'undici/types/header.js'; import type { ResponseLike } from '../shared.js'; export type RequestOptions = Exclude[1], undefined>; +let localAgent: Agent | null = null; + export async function makeRequest(url: string, init: RequestInit): Promise { // The cast is necessary because `headers` and `method` are narrower types in `undici.request` // our request path guarantees they are of acceptable type for `undici.request` @@ -14,6 +16,15 @@ export async function makeRequest(url: string, init: RequestInit): Promise