summary refs log tree commit diff
path: root/src/api/middlewares/RateLimit.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/middlewares/RateLimit.ts')
-rw-r--r--src/api/middlewares/RateLimit.ts49
1 files changed, 11 insertions, 38 deletions
diff --git a/src/api/middlewares/RateLimit.ts b/src/api/middlewares/RateLimit.ts
index f5bfbb4f..14811a42 100644
--- a/src/api/middlewares/RateLimit.ts
+++ b/src/api/middlewares/RateLimit.ts
@@ -65,21 +65,14 @@ export default function rateLimit(opts: {
 			if (rights.has("BYPASS_RATE_LIMITS")) return next();
 		}
 
-		const bucket_id =
-			opts.bucket ||
-			req.originalUrl.replace(API_PREFIX_TRAILING_SLASH, "");
+		const bucket_id = opts.bucket || req.originalUrl.replace(API_PREFIX_TRAILING_SLASH, "");
 		let executor_id = getIpAdress(req);
 		if (!opts.onlyIp && req.user_id) executor_id = req.user_id;
 
 		let max_hits = opts.count;
 		if (opts.bot && req.user_bot) max_hits = opts.bot;
-		if (opts.GET && ["GET", "OPTIONS", "HEAD"].includes(req.method))
-			max_hits = opts.GET;
-		else if (
-			opts.MODIFY &&
-			["POST", "DELETE", "PATCH", "PUT"].includes(req.method)
-		)
-			max_hits = opts.MODIFY;
+		if (opts.GET && ["GET", "OPTIONS", "HEAD"].includes(req.method)) max_hits = opts.GET;
+		else if (opts.MODIFY && ["POST", "DELETE", "PATCH", "PUT"].includes(req.method)) max_hits = opts.MODIFY;
 
 		const offender = Cache.get(executor_id + bucket_id);
 
@@ -104,18 +97,13 @@ export default function rateLimit(opts: {
 			}
 
 			res.set("X-RateLimit-Reset", `${reset}`);
-			res.set(
-				"X-RateLimit-Reset-After",
-				`${Math.max(0, Math.ceil(resetAfterSec))}`,
-			);
+			res.set("X-RateLimit-Reset-After", `${Math.max(0, Math.ceil(resetAfterSec))}`);
 
 			if (offender.blocked) {
 				const global = bucket_id === "global";
 				// each block violation pushes the expiry one full window further
 				reset += opts.window * 1000;
-				offender.expires_at = new Date(
-					offender.expires_at.getTime() + opts.window * 1000,
-				);
+				offender.expires_at = new Date(offender.expires_at.getTime() + opts.window * 1000);
 				resetAfterMs = reset - Date.now();
 				resetAfterSec = Math.ceil(resetAfterMs / 1000);
 
@@ -129,10 +117,7 @@ export default function rateLimit(opts: {
 					res
 						.status(429)
 						.set("X-RateLimit-Remaining", "0")
-						.set(
-							"Retry-After",
-							`${Math.max(0, Math.ceil(resetAfterSec))}`,
-						)
+						.set("Retry-After", `${Math.max(0, Math.ceil(resetAfterSec))}`)
 						// TODO: error rate limit message translation
 						.send({
 							message: "You are being rate limited.",
@@ -156,11 +141,7 @@ export default function rateLimit(opts: {
 				// check if error and increment error rate limit
 				if (res.statusCode >= 400 && opts.error) {
 					return hitRoute(hitRouteOpts);
-				} else if (
-					res.statusCode >= 200 &&
-					res.statusCode < 300 &&
-					opts.success
-				) {
+				} else if (res.statusCode >= 200 && res.statusCode < 300 && opts.success) {
 					return hitRoute(hitRouteOpts);
 				}
 			});
@@ -198,7 +179,7 @@ export async function initRateLimits(app: Router) {
 			bucket: "global",
 			onlyIp: true,
 			...ip,
-		}),
+		})
 	);
 	app.use(rateLimit({ bucket: "global", ...global }));
 	app.use(
@@ -207,24 +188,16 @@ export async function initRateLimits(app: Router) {
 			error: true,
 			onlyIp: true,
 			...error,
-		}),
+		})
 	);
 	app.use("/guilds/:id", rateLimit(routes.guild));
 	app.use("/webhooks/:id", rateLimit(routes.webhook));
 	app.use("/channels/:id", rateLimit(routes.channel));
 	app.use("/auth/login", rateLimit(routes.auth.login));
-	app.use(
-		"/auth/register",
-		rateLimit({ onlyIp: true, success: true, ...routes.auth.register }),
-	);
+	app.use("/auth/register", rateLimit({ onlyIp: true, success: true, ...routes.auth.register }));
 }
 
-async function hitRoute(opts: {
-	executor_id: string;
-	bucket_id: string;
-	max_hits: number;
-	window: number;
-}) {
+async function hitRoute(opts: { executor_id: string; bucket_id: string; max_hits: number; window: number }) {
 	const id = opts.executor_id + opts.bucket_id;
 	let limit = Cache.get(id);
 	if (!limit) {