Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Commit

Permalink
feat: expose https agent options
Browse files Browse the repository at this point in the history
Ported from discordjs/discord.js#6177

Co-authored-by: Shino <shinotheshino@gmail.com>
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
  • Loading branch information
3 people committed Nov 14, 2021
1 parent 6df603e commit 030aa77
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/rest/src/lib/REST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import { EventEmitter } from 'node:events';
import { CDN } from './CDN';
import { InternalRequest, RequestData, RequestManager, RequestMethod, RouteLike } from './RequestManager';
import { DefaultRestOptions, RESTEvents } from './utils/constants';
import type { AgentOptions } from 'node:https';

/**
* Options to be passed when creating the REST instance
*/
export interface RESTOptions {
/**
* HTTPS Agent options
* @default {}
*/
agent: Omit<AgentOptions, 'keepAlive'>;
/**
* The base api path, without version
* @default 'https://discord.com/api'
Expand Down
4 changes: 3 additions & 1 deletion packages/rest/src/lib/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SequentialHandler } from './handlers/SequentialHandler';
import type { RESTOptions } from './REST';
import { DefaultRestOptions, DefaultUserAgent } from './utils/constants';

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

/**
* Represents an attachment to be added to the request
Expand Down Expand Up @@ -198,6 +198,8 @@ export class RequestManager extends EventEmitter {
private resolveRequest(request: InternalRequest): { url: string; fetchOptions: RequestInit } {
const { options } = this;

agent ??= new Agent({ ...options.agent, keepAlive: true });

let query = '';

// If a query option is passed, use it
Expand Down
1 change: 1 addition & 0 deletions packages/rest/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Package = require('../../../package.json');
export const DefaultUserAgent = `DiscordBot (${Package.homepage}, ${Package.version})`;

export const DefaultRestOptions: Required<RESTOptions> = {
agent: {},
api: 'https://discord.com/api',
cdn: 'https://cdn.discordapp.com',
headers: {},
Expand Down

0 comments on commit 030aa77

Please sign in to comment.