Skip to content

Commit

Permalink
feat(Rest): expose https.Agent options (#6177)
Browse files Browse the repository at this point in the history
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
  • Loading branch information
memikri and kyranet committed Aug 17, 2021
1 parent 16493e6 commit 5eb3553
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/rest/APIRequest.js
Expand Up @@ -5,7 +5,7 @@ const FormData = require('@discordjs/form-data');
const fetch = require('node-fetch');
const { UserAgent } = require('../util/Constants');

const agent = new https.Agent({ keepAlive: true });
let agent = null;

class APIRequest {
constructor(rest, method, path, options) {
Expand All @@ -30,6 +30,8 @@ class APIRequest {
}

make() {
agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true });

const API =
this.options.versioned === false
? this.client.options.http.api
Expand Down
9 changes: 9 additions & 0 deletions src/util/Options.js
Expand Up @@ -80,10 +80,18 @@
* sent in the initial guild member list, must be between 50 and 250
*/

/**
* HTTPS Agent options.
* @typedef {Object} AgentOptions
* @see {@link https://nodejs.org/api/https.html#https_class_https_agent}
* @see {@link https://nodejs.org/api/http.html#http_new_agent_options}
*/

/**
* HTTP options
* @typedef {Object} HTTPOptions
* @property {number} [version=9] API version to use
* @property {AgentOptions} [agent={}] HTTPS Agent options
* @property {string} [api='https://discord.com/api'] Base url of the API
* @property {string} [cdn='https://cdn.discordapp.com'] Base url of the CDN
* @property {string} [invite='https://discord.gg'] Base url of invites
Expand Down Expand Up @@ -127,6 +135,7 @@ class Options extends null {
version: 9,
},
http: {
agent: {},
version: 9,
api: 'https://discord.com/api',
cdn: 'https://cdn.discordapp.com',
Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Expand Up @@ -47,6 +47,7 @@ import {
Snowflake,
} from 'discord-api-types/v9';
import { EventEmitter } from 'events';
import { AgentOptions } from 'https';
import { Stream } from 'stream';
import { MessagePort, Worker } from 'worker_threads';
import * as WebSocket from 'ws';
Expand Down Expand Up @@ -3893,6 +3894,7 @@ export interface HTTPErrorData {
}

export interface HTTPOptions {
agent?: Omit<AgentOptions, 'keepAlive'>;
api?: string;
version?: number;
host?: string;
Expand Down

0 comments on commit 5eb3553

Please sign in to comment.