Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Node HTTP(S) Agent #474

Merged
merged 3 commits into from Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -422,6 +424,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 @@ -86,6 +86,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 128, 256, 512, 1024, or 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 @@ -110,7 +111,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 @@ -130,6 +132,11 @@ class Client extends EventEmitter {
if(defaultImageSize < 16 || defaultImageSize > 1024 || (defaultImageSize & (defaultImageSize - 1))) {
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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jsdoc comment for the client constructor should be updated to reflect this new option.


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