Skip to content

Commit

Permalink
Added support for Node HTTP(S) Agent (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
Minigugus authored and abalabahaha committed Apr 19, 2019
1 parent 247092d commit 0509d44
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions index.d.ts
Expand Up @@ -2,6 +2,8 @@ declare module "eris" {
// TODO good hacktoberfest PR: implement ShardManager, RequestHandler and other stuff
import { EventEmitter } from "events";
import { Readable as ReadableStream } from "stream";
import { Agent as HTTPAgent } from "http";
import { Agent as HTTPSAgent } from "https";

export const VERSION: string;
interface JSONCache { [s: string]: any; }
Expand Down Expand Up @@ -439,6 +441,7 @@ declare module "eris" {
defaultImageSize?: number;
ws?: any;
latencyThreshold?: number;
agent?: HTTPAgent | HTTPSAgent
}
interface CommandClientOptions {
defaultHelpCommand?: boolean;
Expand Down
9 changes: 8 additions & 1 deletion lib/Client.js
Expand Up @@ -87,6 +87,7 @@ class Client extends EventEmitter {
* @arg {String} [options.defaultImageFormat="jpg"] The default format to provide user avatars, guild icons, and group icons in. Can be "jpg", "png", "gif", or "webp"
* @arg {Number} [options.defaultImageSize=128] The default size to return user avatars, guild icons, and group icons as. Can be any power of two between 16 and 2048.
* @arg {Object} [options.ws] An object of WebSocket options to pass to the shard WebSocket constructors
* @arg {Object} [options.agent] A HTTP Agent used to proxy requests
*/
constructor(token, options) {
super();
Expand All @@ -111,7 +112,8 @@ class Client extends EventEmitter {
requestTimeout: 15000,
restMode: false,
seedVoiceConnections: false,
ws: {}
ws: {},
agent: null
};
if(typeof options === "object") {
for(const property of Object.keys(options)) {
Expand All @@ -135,6 +137,11 @@ class Client extends EventEmitter {
) {
throw new TypeError("Invalid default image size: " + defaultImageSize);
}
// Set HTTP Agent on Websockets if not already set
if (this.options.agent && !(this.options.ws && this.options.ws.agent)) {
this.options.ws = this.options.ws || {};
this.options.ws.agent = this.options.agent;
}

this.token = token;

Expand Down
4 changes: 3 additions & 1 deletion lib/rest/RequestHandler.js
Expand Up @@ -18,6 +18,7 @@ class RequestHandler {
this.userAgent = `DiscordBot (https://github.com/abalabahaha/eris, ${require("../../package.json").version})`;
this.ratelimits = {};
this.requestTimeout = client.options.requestTimeout;
this.agent = client.options.agent;
this.latencyRef = {
latency: 500,
offset: client.options.ratelimiterOffset,
Expand Down Expand Up @@ -146,7 +147,8 @@ class RequestHandler {
method: method,
host: "discordapp.com",
path: this.baseURL + finalURL,
headers: headers
headers: headers,
agent: this.agent
});

let reqError;
Expand Down

0 comments on commit 0509d44

Please sign in to comment.