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

Feat: Abort Requests that takes a lot of time to resolve #3327

Merged
merged 10 commits into from Aug 19, 2019
Merged
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -35,6 +35,7 @@
"runkitExampleFilename": "./docs/examples/ping.js",
"unpkg": "./webpack/discord.min.js",
"dependencies": {
"abort-controller": "^3.0.0",
"form-data": "^2.3.3",
"node-fetch": "^2.3.0",
"pako": "^1.0.8",
Expand Down
10 changes: 10 additions & 0 deletions src/rest/APIRequest.js
Expand Up @@ -4,6 +4,7 @@ const FormData = require('form-data');
const https = require('https');
const { browser, UserAgent } = require('../util/Constants');
const fetch = require('node-fetch');
const AbortController = require('abort-controller');

if (https.Agent) var agent = new https.Agent({ keepAlive: true });

Expand Down Expand Up @@ -46,11 +47,20 @@ class APIRequest {
headers['Content-Type'] = 'application/json';
}

const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 10000);
Deivu marked this conversation as resolved.
Show resolved Hide resolved
return fetch(url, {
method: this.method,
headers,
agent,
body,
signal: controller.signal,
}).then(res => {
Deivu marked this conversation as resolved.
Show resolved Hide resolved
clearTimeout(timeout);
return res;
}, error => {
clearTimeout(timeout);
throw error;
});
}
}
Expand Down