forked from discordeno/discordeno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.rest.ts
138 lines (119 loc) · 3.83 KB
/
debug.rest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// START FILE FOR REST PROCESS
import { config as dotenv } from "https://deno.land/x/dotenv@v3.2.0/mod.ts";
import { BASE_URL, Collection, createRestManager } from "./mod.ts";
dotenv({ export: true, path: `${Deno.cwd()}/.env` });
const col = new Collection<string, number>();
const token = Deno.env.get("GAMER_TOKEN");
if (!token) throw new Error("Token was not provided.");
const REST_AUTHORIZATION_KEY = Deno.env.get("PROXY_REST_SECRET");
const PROXY_REST_URL = Deno.env.get("PROXY_REST_URL");
const REST_PORT = Number(PROXY_REST_URL?.substring(PROXY_REST_URL.lastIndexOf(":") + 1)) ?? 8080;
// CREATES THE FUNCTIONALITY FOR MANAGING THE REST REQUESTS
const rest = createRestManager({
token,
secretKey: REST_AUTHORIZATION_KEY,
customUrl: PROXY_REST_URL,
debug(text) {
if (text.startsWith("[REST - RequestCreate]")) {
const aaa = text.split(" ");
const method = aaa[4];
const url = aaa[7];
col.set(method + url, Date.now());
// console.log("[DEBUG]", method, url);
}
if (text.startsWith("[REST - processGlobalQueue] rate limited, running setTimeout.")) {
console.log("[POSSIBLE BUCKET ISSUE]");
}
},
fetching(options) {
// console.log("[FETCHING]", options.method, options.url, Date.now() - col.get(options.method + options.url)!);
},
});
// START LISTENING TO THE URL(localhost)
const server = Deno.listen({ port: REST_PORT });
console.log(
`HTTP webserver running. Access it at: ${PROXY_REST_URL}`,
);
// Connections to the server will be yielded up as an async iterable.
for await (const conn of server) {
// In order to not be blocking, we need to handle each connection individually
// in its own async function.
handleRequest(conn);
}
async function handleRequest(conn: Deno.Conn) {
// This "upgrades" a network connection into an HTTP connection.
const httpConn = Deno.serveHttp(conn);
// Each request sent over the HTTP connection will be yielded as an async
// iterator from the HTTP connection.
for await (const requestEvent of httpConn) {
if (
!REST_AUTHORIZATION_KEY ||
REST_AUTHORIZATION_KEY !==
requestEvent.request.headers.get("AUTHORIZATION")
) {
return requestEvent.respondWith(
new Response(JSON.stringify({ error: "Invalid authorization key." }), {
status: 401,
}),
);
}
try {
const text = await requestEvent.request.text();
const json = text ? JSON.parse(text) : undefined;
if (json?.file) {
json.file = await Promise.all(json.file.map(async (f: any) => ({
name: f.name,
blob: await (await fetch(f.blob)).blob(),
})));
}
const result = await rest.runMethod(
rest,
requestEvent.request.method as RequestMethod,
`${BASE_URL}${
requestEvent.request.url.substring(
`http://localhost:${REST_PORT}`.length,
)
}`,
json,
);
if (result) {
requestEvent.respondWith(
new Response(JSON.stringify(result), {
status: 200,
}),
);
} else {
requestEvent.respondWith(
new Response(undefined, {
status: 204,
}),
);
}
} catch (error) {
console.log(
"CATCH",
requestEvent.request.url,
requestEvent.request.method,
requestEvent.request.body,
error.code,
error,
);
requestEvent.respondWith(
new Response(
JSON.stringify({
message: error.message,
}),
{
status: error.code ?? 469,
},
),
);
}
}
}
type RequestMethod = "POST" | "PUT" | "DELETE" | "PATCH";
// // @ts-ignore
// rest.convertRestError = (errorStack, data) => {
// return data;
// };
// console.log(`Giveaway Boat REST Started At: ${new Date().toUTCString()}`);