From d8ea572fb8a51f2f6a902c4926e814017d115708 Mon Sep 17 00:00:00 2001 From: ckohen Date: Thu, 20 Jan 2022 03:14:12 -0800 Subject: [PATCH] fix(rest): use http agent when protocol is not https (#7309) --- packages/rest/src/lib/RequestManager.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/rest/src/lib/RequestManager.ts b/packages/rest/src/lib/RequestManager.ts index afb84d35eebb..1b582e0d34e6 100644 --- a/packages/rest/src/lib/RequestManager.ts +++ b/packages/rest/src/lib/RequestManager.ts @@ -2,15 +2,14 @@ import Collection from '@discordjs/collection'; import FormData from 'form-data'; import { DiscordSnowflake } from '@sapphire/snowflake'; import { EventEmitter } from 'node:events'; -import { Agent } from 'node:https'; +import { Agent as httpsAgent } from 'node:https'; +import { Agent as httpAgent } from 'node:http'; import type { RequestInit, BodyInit } from 'node-fetch'; import type { IHandler } from './handlers/IHandler'; import { SequentialHandler } from './handlers/SequentialHandler'; import type { RESTOptions, RestEvents } from './REST'; import { DefaultRestOptions, DefaultUserAgent, RESTEvents } from './utils/constants'; -let agent: Agent | null = null; - /** * Represents a file to be added to the request */ @@ -186,6 +185,7 @@ export class RequestManager extends EventEmitter { private hashTimer!: NodeJS.Timer; private handlerTimer!: NodeJS.Timer; + private agent: httpsAgent | httpAgent | null = null; public readonly options: RESTOptions; @@ -318,7 +318,9 @@ export class RequestManager extends EventEmitter { private resolveRequest(request: InternalRequest): { url: string; fetchOptions: RequestInit } { const { options } = this; - agent ??= new Agent({ ...options.agent, keepAlive: true }); + this.agent ??= options.api.startsWith('https') + ? new httpsAgent({ ...options.agent, keepAlive: true }) + : new httpAgent({ ...options.agent, keepAlive: true }); let query = ''; @@ -394,7 +396,7 @@ export class RequestManager extends EventEmitter { } const fetchOptions = { - agent, + agent: this.agent, body: finalBody, // eslint-disable-next-line @typescript-eslint/consistent-type-assertions headers: { ...(request.headers ?? {}), ...additionalHeaders, ...headers } as Record,