mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 02:23:31 +01:00
switch to node-fetch (#2587)
* switch to node-fetch
* remove useless var declaration
* remove method uppercasing
* rework concurrency
* Revert "rework concurrency"
This reverts commit ef6aa2697e.
* fix headers
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
const querystring = require('querystring');
|
||||
const snekfetch = require('snekfetch');
|
||||
const FormData = require('form-data');
|
||||
const https = require('https');
|
||||
const { browser, UserAgent } = require('../util/Constants');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
if (https.Agent) var agent = new https.Agent({ keepAlive: true });
|
||||
|
||||
@@ -17,24 +18,34 @@ class APIRequest {
|
||||
this.path = `${path}${queryString ? `?${queryString}` : ''}`;
|
||||
}
|
||||
|
||||
gen() {
|
||||
make() {
|
||||
const API = this.options.versioned === false ? this.client.options.http.api :
|
||||
`${this.client.options.http.api}/v${this.client.options.http.version}`;
|
||||
const url = API + this.path;
|
||||
let headers = {};
|
||||
|
||||
const request = snekfetch[this.method](`${API}${this.path}`, { agent });
|
||||
|
||||
if (this.options.auth !== false) request.set('Authorization', this.rest.getAuth());
|
||||
if (this.options.reason) request.set('X-Audit-Log-Reason', encodeURIComponent(this.options.reason));
|
||||
if (!browser) request.set('User-Agent', UserAgent);
|
||||
if (this.options.headers) request.set(this.options.headers);
|
||||
if (this.options.auth !== false) headers.Authorization = this.rest.getAuth();
|
||||
if (this.options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(this.options.reason);
|
||||
if (!browser) headers['User-Agent'] = UserAgent;
|
||||
if (this.options.headers) headers = Object.assign(headers, this.options.headers);
|
||||
|
||||
let body;
|
||||
if (this.options.files) {
|
||||
for (const file of this.options.files) if (file && file.file) request.attach(file.name, file.file, file.name);
|
||||
if (typeof this.options.data !== 'undefined') request.attach('payload_json', JSON.stringify(this.options.data));
|
||||
} else if (typeof this.options.data !== 'undefined') {
|
||||
request.send(this.options.data);
|
||||
body = new FormData();
|
||||
for (const file of this.options.files) if (file && file.file) body.append(file.name, file.file, file.name);
|
||||
if (typeof this.options.data !== 'undefined') body.append('payload_json', JSON.stringify(this.options.data));
|
||||
if (!browser) headers = Object.assign(headers, body.getHeaders());
|
||||
} else if (this.options.data != null) { // eslint-disable-line eqeqeq
|
||||
body = JSON.stringify(this.options.data);
|
||||
headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
return request;
|
||||
|
||||
return fetch(url, {
|
||||
method: this.method,
|
||||
headers,
|
||||
agent,
|
||||
body,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,34 +68,37 @@ class RequestHandler {
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
item.request.gen().end((err, res) => {
|
||||
item.request.make().then(res => {
|
||||
if (res && res.headers) {
|
||||
if (res.headers['x-ratelimit-global']) this.manager.globallyRateLimited = true;
|
||||
this.limit = Number(res.headers['x-ratelimit-limit']);
|
||||
this.resetTime = Date.now() + Number(res.headers['retry-after']);
|
||||
this.remaining = Number(res.headers['x-ratelimit-remaining']);
|
||||
if (res.headers.get('x-ratelimit-global')) this.manager.globallyRateLimited = true;
|
||||
this.limit = Number(res.headers.get('x-ratelimit-limit') || Infinity);
|
||||
this.resetTime = Number(res.headers.get('x-ratelimit-reset') || 0);
|
||||
this.remaining = Number(res.headers.get('x-ratelimit-remaining') || 1);
|
||||
}
|
||||
if (err) {
|
||||
if (err.status === 429) {
|
||||
this.queue.unshift(item);
|
||||
finish(Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
||||
} else if (err.status >= 500 && err.status < 600) {
|
||||
if (item.retried) {
|
||||
item.reject(err);
|
||||
finish();
|
||||
} else {
|
||||
item.retried = true;
|
||||
this.queue.unshift(item);
|
||||
finish(1e3 + this.client.options.restTimeOffset);
|
||||
}
|
||||
} else {
|
||||
item.reject(err.status >= 400 && err.status < 500 ?
|
||||
new DiscordAPIError(res.request.path, res.body, res.request.method) : err);
|
||||
|
||||
if (res.ok) {
|
||||
res.json().then(item.resolve, item.reject);
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.status === 429) {
|
||||
this.queue.unshift(item);
|
||||
finish(Number(res.headers.get('retry-after')) + this.client.options.restTimeOffset);
|
||||
} else if (res.status >= 500 && res.status < 600) {
|
||||
if (item.retried) {
|
||||
item.reject(res);
|
||||
finish();
|
||||
} else {
|
||||
item.retried = true;
|
||||
this.queue.unshift(item);
|
||||
finish(1e3 + this.client.options.restTimeOffset);
|
||||
}
|
||||
} else {
|
||||
const data = res && res.body ? res.body : {};
|
||||
item.resolve(data);
|
||||
res.json().then(data => {
|
||||
item.reject(res.status >= 400 && res.status < 500 ?
|
||||
new DiscordAPIError(item.path, data, item.method) : res);
|
||||
}, item.reject);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user