refactor: migrate from node-fetch to undici and clean up dependencies

This commit is contained in:
Elysia
2024-11-27 16:10:54 +07:00
parent d1df7cbca2
commit 38bff20b1b
8 changed files with 44 additions and 50 deletions

View File

@@ -4,9 +4,8 @@ const Buffer = require('node:buffer').Buffer;
const https = require('node:https');
const { setTimeout } = require('node:timers');
const makeFetchCookie = require('fetch-cookie');
const FormData = require('form-data');
const fetchOriginal = require('node-fetch');
const { CookieJar } = require('tough-cookie');
const { fetch: fetchOriginal, FormData } = require('undici');
const { ciphers } = require('../util/Constants');
const Util = require('../util/Util');
@@ -69,7 +68,6 @@ class APIRequest {
const url = API + this.path;
let headers = {
authority: 'discord.com',
accept: '*/*',
'accept-language': 'en-US',
'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108"',
@@ -84,9 +82,8 @@ class APIRequest {
'x-super-properties': `${Buffer.from(JSON.stringify(this.client.options.ws.properties), 'ascii').toString(
'base64',
)}`,
Referer: 'https://discord.com/channels/@me',
referer: 'https://discord.com/channels/@me',
origin: 'https://discord.com',
'Referrer-Policy': 'strict-origin-when-cross-origin',
...this.client.options.http.headers,
'User-Agent': this.fullUserAgent,
};
@@ -152,6 +149,7 @@ class APIRequest {
agent,
body,
signal: controller.signal,
redirect: 'follow',
}).finally(() => clearTimeout(timeout));
}
}

View File

@@ -67,6 +67,21 @@ class DiscordAPIError extends Error {
this.captcha = error?.captcha_service ? error : null;
}
/**
* A special `40333` JSON error code is returned if your request is blocked by Cloudflare.
* This may be due to a malformed request or improper user agent.
* The response resembles a normal error structure:
* @type {boolean}
* @example
* {
* "message": "internal network error",
* "code": 40333
* }
*/
get isBlockedByCloudflare() {
return this.code === 40333;
}
/**
* Flattens an errors object returned from the API into an array.
* @param {APIError} obj Discord errors object