Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user