Skip to content

Commit

Permalink
fix(rest): use http agent when protocol is not https (#7309)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckohen committed Jan 20, 2022
1 parent 1c6c944 commit d8ea572
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/rest/src/lib/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 = '';

Expand Down Expand Up @@ -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<string, string>,
Expand Down

0 comments on commit d8ea572

Please sign in to comment.