Refactor code structure for improved readability and maintainability

This commit is contained in:
2026-10-19 19:36:19 +03:00
parent 170a44c377
commit 5f7bffd6e4
17 changed files with 454 additions and 85 deletions

View File

@@ -1,3 +1,5 @@
import { apiError } from "./errors.js";
/**
* Limits for the expensive endpoints: parsing an uploaded Word file can take a LibreOffice
* conversion and seconds of CPU, so one client must not be able to occupy the server.
@@ -15,7 +17,7 @@ export function rateLimit({ max, windowMs, message }) {
windows.set(req.ip, w);
if (w.count <= max) return next();
res.set("Retry-After", String(Math.ceil((w.start + windowMs - now) / 1000)));
res.status(429).json({ error: message });
res.status(429).json({ error: message, code: "too_many_requests" });
};
}
@@ -29,7 +31,7 @@ export function semaphore({ limit, queue, message }) {
return async function run(task) {
if (active < limit) active++;
else if (waiting.length < queue) await new Promise((resolve) => waiting.push(resolve));
else throw Object.assign(new Error(message), { status: 429 });
else throw apiError(429, "busy", message);
try {
return await task();
} finally {