From 415a88c783362ceb6935eed54d75855307ca4f92 Mon Sep 17 00:00:00 2001 From: Shino Date: Fri, 23 Jul 2021 13:51:14 -0400 Subject: [PATCH 1/5] feat(Rest): expose https.Agent options --- src/rest/APIRequest.js | 6 +++++- src/util/Options.js | 1 + typings/index.d.ts | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index d710c58724f3..0f4c263663e9 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -6,7 +6,7 @@ const AbortController = require('abort-controller'); 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) { @@ -31,6 +31,10 @@ class APIRequest { } make() { + if (!agent) { + agent = new https.Agent({ ...this.client.options.http.agent, keepAlive: true }); + } + const API = this.options.versioned === false ? this.client.options.http.api diff --git a/src/util/Options.js b/src/util/Options.js index cb366cb09be8..c54011d310ea 100644 --- a/src/util/Options.js +++ b/src/util/Options.js @@ -124,6 +124,7 @@ class Options extends null { version: 9, }, http: { + agent: {}, version: 9, api: 'https://discord.com/api', cdn: 'https://cdn.discordapp.com', diff --git a/typings/index.d.ts b/typings/index.d.ts index c99166224e38..623e3f2ea7b0 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -34,6 +34,7 @@ import { Snowflake, } from 'discord-api-types/v9'; import { EventEmitter } from 'events'; +import * as https from 'https'; import { Stream } from 'stream'; import * as WebSocket from 'ws'; import { @@ -3639,6 +3640,7 @@ export interface HTTPErrorData { } export interface HTTPOptions { + agent?: Omit; api?: string; version?: number; host?: string; From a04fc08af7a63e56bccd728633fefddaed5eed5d Mon Sep 17 00:00:00 2001 From: Shino Date: Fri, 13 Aug 2021 20:03:35 -0400 Subject: [PATCH 2/5] style: destructure https --- typings/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 623e3f2ea7b0..c87993fce64a 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -34,7 +34,7 @@ import { Snowflake, } from 'discord-api-types/v9'; import { EventEmitter } from 'events'; -import * as https from 'https'; +import { AgentOptions } from 'https'; import { Stream } from 'stream'; import * as WebSocket from 'ws'; import { @@ -3640,7 +3640,7 @@ export interface HTTPErrorData { } export interface HTTPOptions { - agent?: Omit; + agent?: Omit; api?: string; version?: number; host?: string; From 0a8119d2d5f983cbed8f4c8c5550165d8c3ab364 Mon Sep 17 00:00:00 2001 From: Shino Date: Fri, 13 Aug 2021 20:10:01 -0400 Subject: [PATCH 3/5] docs(Options): document agent options --- src/util/Options.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/util/Options.js b/src/util/Options.js index c54011d310ea..cb058436e8b3 100644 --- a/src/util/Options.js +++ b/src/util/Options.js @@ -77,10 +77,18 @@ * sent in the initial guild member list, must be between 50 and 250 */ +/** + * HTTPS Agent options. + * @typedef {import('https').AgentOptions} 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 From 22aaa7ab05c95295cbc2ff8a6b239bbdfed63c4a Mon Sep 17 00:00:00 2001 From: Shino Date: Fri, 13 Aug 2021 20:11:35 -0400 Subject: [PATCH 4/5] fix(Options): document using Object --- src/util/Options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/Options.js b/src/util/Options.js index cb058436e8b3..f05883a1a20e 100644 --- a/src/util/Options.js +++ b/src/util/Options.js @@ -79,7 +79,7 @@ /** * HTTPS Agent options. - * @typedef {import('https').AgentOptions} AgentOptions + * @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} */ From 9e02a405a0ecf945c2214a5377059a0ac4b971f4 Mon Sep 17 00:00:00 2001 From: Shino Date: Sat, 14 Aug 2021 13:18:14 -0400 Subject: [PATCH 5/5] style: use null coalescing assignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/rest/APIRequest.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index 0f4c263663e9..2e75260ad5f3 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -31,9 +31,7 @@ class APIRequest { } make() { - if (!agent) { - agent = new https.Agent({ ...this.client.options.http.agent, keepAlive: true }); - } + agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true }); const API = this.options.versioned === false