feat: abort Requests that takes a lot of time to resolve (#3327)

* Add Request Timeout

* Add abort controller in packages

* Fix Lint Error.

* Fix Lint Errors

* Make Timeout Customizable & use finally

* Fixed a minor issue

* Fix eslint

* Update request timeout to use d.js client timeout methods.
This commit is contained in:
Saya
2019-08-20 00:55:07 +08:00
committed by SpaceEEC
parent 3fcc862c5f
commit e4309b23d5
5 changed files with 12 additions and 1 deletions

View File

@@ -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 });
@@ -46,12 +47,15 @@ class APIRequest {
headers['Content-Type'] = 'application/json';
}
const controller = new AbortController();
const timeout = this.client.setTimeout(() => controller.abort(), this.client.options.restRequestTimeout);
return fetch(url, {
method: this.method,
headers,
agent,
body,
});
signal: controller.signal,
}).finally(() => this.client.clearTimeout(timeout));
}
}