Skip to content

Commit 0d30294

Browse files
committed
fix: revert to the previous approach
1 parent a409f5e commit 0d30294

File tree

6 files changed

+14
-11
lines changed

6 files changed

+14
-11
lines changed

packages/proxy/__tests__/proxyRequests.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ beforeEach(() => {
2424
setGlobalDispatcher(mockAgent); // enabled the mock client to intercept requests
2525

2626
mockPool = mockAgent.get('https://discord.com');
27+
api.setAgent(mockAgent);
2728
});
2829

2930
afterEach(async () => {

packages/rest/__tests__/REST.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ beforeEach(() => {
3737
setGlobalDispatcher(mockAgent); // enabled the mock client to intercept requests
3838

3939
mockPool = mockAgent.get('https://discord.com');
40+
api.setAgent(mockAgent);
41+
fetchApi.setAgent(mockAgent);
4042
});
4143

4244
afterEach(async () => {

packages/rest/__tests__/RequestHandler.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ test('perm server outage', async () => {
481481
test('server responding too slow', async () => {
482482
const api2 = new REST({ timeout: 1 }).setToken('A-Very-Really-Real-Token');
483483

484+
api2.setAgent(mockAgent);
485+
484486
mockPool
485487
.intercept({
486488
path: genPath('/slow'),

packages/rest/__tests__/RequestManager.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ beforeEach(() => {
1515
setGlobalDispatcher(mockAgent);
1616

1717
mockPool = mockAgent.get('https://discord.com');
18+
api.setAgent(mockAgent);
1819
});
1920

2021
afterEach(async () => {

packages/rest/__tests__/UndiciRequest.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ beforeEach(() => {
2828
setGlobalDispatcher(mockAgent); // enabled the mock client to intercept requests
2929

3030
mockPool = mockAgent.get('https://discord.com');
31+
api.setAgent(mockAgent);
3132
});
3233

3334
afterEach(async () => {

packages/rest/src/strategies/undiciRequest.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import { STATUS_CODES } from 'node:http';
22
import { URLSearchParams } from 'node:url';
33
import { types } from 'node:util';
4-
import {
5-
type RequestInit,
6-
request,
7-
Headers,
8-
FormData as UndiciFormData,
9-
Agent,
10-
getGlobalDispatcher,
11-
Dispatcher,
12-
} from 'undici';
4+
import { type RequestInit, request, Headers, FormData as UndiciFormData, Agent } from 'undici';
135
import type { HeaderRecord } from 'undici/types/header.js';
146
import type { ResponseLike } from '../shared.js';
157

168
export type RequestOptions = Exclude<Parameters<typeof request>[1], undefined>;
179

10+
let localAgent: Agent | null = null;
11+
1812
export async function makeRequest(url: string, init: RequestInit): Promise<ResponseLike> {
1913
// The cast is necessary because `headers` and `method` are narrower types in `undici.request`
2014
// our request path guarantees they are of acceptable type for `undici.request`
@@ -24,9 +18,11 @@ export async function makeRequest(url: string, init: RequestInit): Promise<Respo
2418
} as RequestOptions;
2519

2620
// Mismatched dispatchers from the Node.js-bundled undici and package-installed undici breaks file uploads.
21+
// So we ensure that we always pass an Agent to request()
2722
// https://github.com/nodejs/node/issues/59012
28-
if (!options.dispatcher && !(getGlobalDispatcher() instanceof Dispatcher)) {
29-
options.dispatcher = new Agent();
23+
if (!options.dispatcher) {
24+
localAgent ??= new Agent();
25+
options.dispatcher = localAgent;
3026
}
3127

3228
const res = await request(url, options);

0 commit comments

Comments
 (0)