REST API speed improvement (#1577)

This commit is contained in:
Gus Caplan
2017-07-01 04:14:17 -05:00
committed by Amish Shah
parent 6bc7b3e068
commit 5ecd5f7d69
25 changed files with 114 additions and 109 deletions

View File

@@ -1,37 +1,30 @@
const util = require('util');
const methods = ['get', 'post', 'delete', 'patch', 'put'];
// Paramable exists so we don't return a function unless we actually need one #savingmemory
const paramable = [
'channels', 'users', 'guilds', 'members',
'bans', 'emojis', 'pins', 'permissions',
'reactions', 'webhooks', 'messages',
'notes', 'roles', 'applications',
'invites', 'bot',
const reflectors = [
'toString', 'valueOf', 'inspect', 'constructor',
Symbol.toPrimitive, util.inspect.custom,
];
const reflectors = ['toString', 'valueOf', 'inspect', Symbol.toPrimitive, util.inspect.custom];
module.exports = restManager => {
const handler = {
get(list, name) {
if (reflectors.includes(name)) return () => list.join('/');
if (paramable.includes(name)) {
if (name === 'opts') {
function toReturn(...args) { // eslint-disable-line no-inner-declarations
list = list.concat(name);
for (const arg of args) {
if (arg !== null && typeof arg !== 'undefined') list = list.concat(arg);
}
list.push(...args.filter(x => x !== null && typeof x !== 'undefined'));
return new Proxy(list, handler);
}
const directJoin = () => `${list.join('/')}/${name}`;
for (const r of reflectors) toReturn[r] = directJoin;
for (const method of methods) {
toReturn[method] = options => restManager.request(method, `${list.join('/')}/${name}`, options);
toReturn[method] = options => restManager.request(method, directJoin(), options);
}
return toReturn;
}
if (reflectors.includes(name)) return () => list.join('/');
if (methods.includes(name)) return options => restManager.request(name, list.join('/'), options);
return new Proxy(list.concat(name), handler);
list.push(name);
return new Proxy(list, handler);
},
};

View File

@@ -3,12 +3,18 @@
* @extends Error
*/
class DiscordAPIError extends Error {
constructor(error) {
constructor(path, error) {
super();
const flattened = error.errors ? `\n${this.constructor.flattenErrors(error.errors).join('\n')}` : '';
this.name = 'DiscordAPIError';
this.message = `${error.message}${flattened}`;
/**
* The path of the request relative to the HTTP endpoint
* @type {string}
*/
this.path = path;
/**
* HTTP error code returned by Discord
* @type {number}

View File

@@ -12,8 +12,10 @@ class RESTManager {
this.userAgentManager = new UserAgentManager(this);
this.rateLimitedEndpoints = {};
this.globallyRateLimited = false;
}
this.api = mountApi(this);
get api() {
return mountApi(this);
}
destroy() {

View File

@@ -41,7 +41,7 @@ class BurstRequestHandler extends RequestHandler {
this.resetTimeout = null;
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
} else {
item.reject(err.status === 400 ? new DiscordAPIError(res.body) : err);
item.reject(err.status === 400 ? new DiscordAPIError(res.request.path, res.body) : err);
this.handle();
}
} else {

View File

@@ -65,7 +65,7 @@ class SequentialRequestHandler extends RequestHandler {
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
} else {
item.reject(err.status >= 400 && err.status < 500 ? new DiscordAPIError(res.body) : err);
item.reject(err.status >= 400 && err.status < 500 ? new DiscordAPIError(res.request.path, res.body) : err);
resolve(err);
}
} else {