mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
fix: rest errors preventing startup
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { USER_AGENT } from "../util/constants.ts";
|
||||
|
||||
/** Creates the request body and headers that are necessary to send a request. Will handle different types of methods and everything necessary for discord. */
|
||||
export function createRequestBody(queuedRequest: QueuedRequest) {
|
||||
const headers: { [key: string]: string } = {
|
||||
@@ -6,7 +8,7 @@ export function createRequestBody(queuedRequest: QueuedRequest) {
|
||||
};
|
||||
|
||||
// GET METHODS SHOULD NOT HAVE A BODY
|
||||
if (queuedRequest.request.method === "GET") {
|
||||
if (queuedRequest.request.method.toUpperCase() === "GET") {
|
||||
queuedRequest.payload.body = undefined;
|
||||
}
|
||||
|
||||
|
||||
+45
-36
@@ -1,4 +1,5 @@
|
||||
import { DiscordHTTPResponseCodes } from "../types/codes/http_response_codes.ts";
|
||||
import { delay } from "../util/utils.ts";
|
||||
import { rest } from "./rest.ts";
|
||||
|
||||
/** Processes the queue by looping over each path separately until the queues are empty. */
|
||||
@@ -36,15 +37,21 @@ export async function processQueue(id: string) {
|
||||
|
||||
// IF THIS IS A GET REQUEST, CHANGE THE BODY TO QUERY PARAMETERS
|
||||
const query =
|
||||
queuedRequest.request.method === "GET" && queuedRequest.payload.body
|
||||
? Object.entries(queuedRequest.payload.body).map(([key, value]) =>
|
||||
`${encodeURIComponent(key)}=${encodeURIComponent(value as string)}`
|
||||
)
|
||||
.join("&")
|
||||
queuedRequest.request.method.toUpperCase() === "GET" &&
|
||||
queuedRequest.payload.body
|
||||
? Object.entries(queuedRequest.payload.body)
|
||||
.map(
|
||||
([key, value]) =>
|
||||
`${encodeURIComponent(key)}=${encodeURIComponent(
|
||||
value as string
|
||||
)}`
|
||||
)
|
||||
.join("&")
|
||||
: "";
|
||||
const urlToUse = queuedRequest.request.method === "GET" && query
|
||||
? `${queuedRequest.request.url}?${query}`
|
||||
: queuedRequest.request.url;
|
||||
const urlToUse =
|
||||
queuedRequest.request.method.toUpperCase() === "GET" && query
|
||||
? `${queuedRequest.request.url}?${query}`
|
||||
: queuedRequest.request.url;
|
||||
|
||||
// CUSTOM HANDLER FOR USER TO LOG OR WHATEVER WHENEVER A FETCH IS MADE
|
||||
rest.eventHandlers.fetching(queuedRequest.payload);
|
||||
@@ -52,45 +59,49 @@ export async function processQueue(id: string) {
|
||||
try {
|
||||
const response = await fetch(
|
||||
urlToUse,
|
||||
rest.createRequestBody(queuedRequest),
|
||||
rest.createRequestBody(queuedRequest)
|
||||
);
|
||||
|
||||
rest.eventHandlers.fetched(queuedRequest.payload);
|
||||
const bucketIdFromHeaders = rest.processRequestHeaders(
|
||||
queuedRequest.request.url,
|
||||
response.headers,
|
||||
response.headers
|
||||
);
|
||||
|
||||
if (response.status < 200 || response.status >= 400) {
|
||||
rest.eventHandlers.error(
|
||||
"httpError",
|
||||
queuedRequest.payload,
|
||||
response,
|
||||
);
|
||||
rest.eventHandlers.error("httpError", queuedRequest.payload, response);
|
||||
|
||||
let error = "REQUEST_UNKNOWN_ERROR";
|
||||
switch (response.status) {
|
||||
case DiscordHTTPResponseCodes.BadRequest:
|
||||
error =
|
||||
"The request was improperly formatted, or the server couldn't understand it.";
|
||||
break;
|
||||
case DiscordHTTPResponseCodes.Unauthorized:
|
||||
error = "The Authorization header was missing or invalid.";
|
||||
break;
|
||||
case DiscordHTTPResponseCodes.Forbidden:
|
||||
error =
|
||||
"The Authorization token you passed did not have permission to the resource.";
|
||||
break;
|
||||
case DiscordHTTPResponseCodes.NotFound:
|
||||
error = "The resource at the location specified doesn't exist.";
|
||||
break;
|
||||
case DiscordHTTPResponseCodes.MethodNotAllowed:
|
||||
error =
|
||||
"The HTTP method used is not valid for the location specified.";
|
||||
break;
|
||||
case DiscordHTTPResponseCodes.GatewayUnavailable:
|
||||
error =
|
||||
"There was not a gateway available to process your request. Wait a bit and retry.";
|
||||
break;
|
||||
}
|
||||
|
||||
queuedRequest.request.respond(
|
||||
{ status: response.status, body: JSON.stringify({ error }) },
|
||||
);
|
||||
queuedRequest.request.respond({
|
||||
status: response.status,
|
||||
body: JSON.stringify({ error }),
|
||||
});
|
||||
|
||||
queue.shift();
|
||||
continue;
|
||||
}
|
||||
@@ -112,20 +123,16 @@ export async function processQueue(id: string) {
|
||||
// IF IT HAS MAXED RETRIES SOMETHING SERIOUSLY WRONG. CANCEL OUT.
|
||||
if (
|
||||
queuedRequest.payload.retryCount >=
|
||||
queuedRequest.options.maxRetryCount
|
||||
queuedRequest.options.maxRetryCount
|
||||
) {
|
||||
rest.eventHandlers.retriesMaxed(queuedRequest.payload);
|
||||
queuedRequest.request.respond(
|
||||
{
|
||||
status: 200,
|
||||
body: JSON.stringify(
|
||||
{
|
||||
error:
|
||||
"The request was rate limited and it maxed out the retries limit.",
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
queuedRequest.request.respond({
|
||||
status: 200,
|
||||
body: JSON.stringify({
|
||||
error:
|
||||
"The request was rate limited and it maxed out the retries limit.",
|
||||
}),
|
||||
});
|
||||
// REMOVE ITEM FROM QUEUE TO PREVENT RETRY
|
||||
queue.shift();
|
||||
continue;
|
||||
@@ -142,16 +149,18 @@ export async function processQueue(id: string) {
|
||||
rest.eventHandlers.fetchSuccess(queuedRequest.payload);
|
||||
// REMOVE FROM QUEUE
|
||||
queue.shift();
|
||||
queuedRequest.request.respond(
|
||||
{ status: 200, body: JSON.stringify(json) },
|
||||
);
|
||||
queuedRequest.request.respond({
|
||||
status: 200,
|
||||
body: JSON.stringify(json),
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
// SOMETHING WENT WRONG, LOG AND RESPOND WITH ERROR
|
||||
rest.eventHandlers.fetchFailed(queuedRequest.payload, error);
|
||||
queuedRequest.request.respond(
|
||||
{ status: 404, body: JSON.stringify({ error }) },
|
||||
);
|
||||
queuedRequest.request.respond({
|
||||
status: 404,
|
||||
body: JSON.stringify({ error }),
|
||||
});
|
||||
// REMOVE FROM QUEUE
|
||||
queue.shift();
|
||||
}
|
||||
|
||||
+14
-11
@@ -1,8 +1,10 @@
|
||||
import { BASE_URL } from "../util/constants.ts";
|
||||
import { rest } from "./rest.ts";
|
||||
|
||||
/** Processes a request and assigns it to a queue or creates a queue if none exists for it. */
|
||||
export function processRequest(
|
||||
request: ServerRequest,
|
||||
payload: RunMethodOptions,
|
||||
options: RestServerOptions,
|
||||
payload: RunMethodOptions
|
||||
) {
|
||||
const route = request.url.substring(request.url.indexOf("api/"));
|
||||
const parts = route.split("/");
|
||||
@@ -11,23 +13,24 @@ export function processRequest(
|
||||
// REMOVES THE VERSION NUMBER
|
||||
if (parts[0]?.startsWith("v")) parts.shift();
|
||||
// SET THE NEW REQUEST URL
|
||||
request.url = `${BASE_URL}/v${options.apiVersion || 8}/${parts.join("/")}`;
|
||||
request.url = `${BASE_URL}/v${rest.apiVersion}/${parts.join("/")}`;
|
||||
// REMOVE THE MAJOR PARAM
|
||||
parts.shift();
|
||||
|
||||
const [id] = parts;
|
||||
|
||||
const queue = restCache.pathQueues.get(id);
|
||||
const queue = rest.pathQueues.get(id);
|
||||
// IF THE QUEUE EXISTS JUST ADD THIS TO THE QUEUE
|
||||
if (queue) {
|
||||
queue.push({ request, payload, options });
|
||||
queue.push({ request, payload });
|
||||
} else {
|
||||
// CREATES A NEW QUEUE
|
||||
restCache.pathQueues.set(id, [{
|
||||
request,
|
||||
payload,
|
||||
options,
|
||||
}]);
|
||||
processQueue(id);
|
||||
rest.pathQueues.set(id, [
|
||||
{
|
||||
request,
|
||||
payload,
|
||||
},
|
||||
]);
|
||||
rest.processQueue(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import { processRequestHeaders } from "./process_request_headers.ts";
|
||||
import { runMethod } from "./run_method.ts";
|
||||
|
||||
export const rest = {
|
||||
apiVersion: "8",
|
||||
/** The secret authorization key to confirm that this was a request made by you and not a DDOS attack. */
|
||||
authorization: "discordeno_best_lib_ever",
|
||||
pathQueues: new Map(),
|
||||
|
||||
@@ -46,8 +46,7 @@ export function runMethod(
|
||||
}
|
||||
|
||||
// No proxy so we need to handle all rate limiting and such
|
||||
// deno-lint-ignore no-async-promise-executor
|
||||
return new Promise(async (resolve, reject) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const callback = async () => {
|
||||
try {
|
||||
const rateLimitResetIn = rest.checkRateLimits(url);
|
||||
@@ -143,14 +142,10 @@ export function runMethod(
|
||||
}
|
||||
};
|
||||
|
||||
rest.addToQueue({
|
||||
rest.processRequest({ url, method, respond: (data: { status: number, body?: string; }) => resolve(JSON.parse(data.body || "{}")) }, {
|
||||
callback,
|
||||
bucketId,
|
||||
url,
|
||||
});
|
||||
if (!rest.queueInProcess) {
|
||||
rest.queueInProcess = true;
|
||||
await rest.processQueue();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user