@@ -0,0 +1,16 @@
|
||||
import type { NextFunction, Request, Response } from "express";
|
||||
|
||||
export function asyncHandler(handler: (req: Request, res: Response) => Promise<void>) {
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
void handler(req, res).catch(next);
|
||||
};
|
||||
}
|
||||
|
||||
export function readOptionalString(value: unknown): string | null {
|
||||
if (typeof value !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const trimmed = value.trim();
|
||||
return trimmed === "" ? null : trimmed;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
export class Semaphore {
|
||||
private current = 0;
|
||||
private readonly waiting: Array<() => void> = [];
|
||||
|
||||
constructor(private readonly limit: number) {}
|
||||
|
||||
get active(): number {
|
||||
return this.current;
|
||||
}
|
||||
|
||||
get queued(): number {
|
||||
return this.waiting.length;
|
||||
}
|
||||
|
||||
async use<T>(task: () => Promise<T>): Promise<T> {
|
||||
await this.acquire();
|
||||
|
||||
try {
|
||||
return await task();
|
||||
} finally {
|
||||
this.release();
|
||||
}
|
||||
}
|
||||
|
||||
private async acquire(): Promise<void> {
|
||||
if (this.current < this.limit) {
|
||||
this.current += 1;
|
||||
return;
|
||||
}
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
this.waiting.push(() => {
|
||||
this.current += 1;
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private release(): void {
|
||||
this.current -= 1;
|
||||
const next = this.waiting.shift();
|
||||
next?.();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user