fix: make property-assessor safer for whatsapp runs
This commit is contained in:
37
skills/property-assessor/src/async-timeout.ts
Normal file
37
skills/property-assessor/src/async-timeout.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
export class TimeoutError extends Error {
|
||||
readonly timeoutMs: number;
|
||||
|
||||
constructor(operationName: string, timeoutMs: number) {
|
||||
super(`${operationName} timed out after ${timeoutMs}ms`);
|
||||
this.name = "TimeoutError";
|
||||
this.timeoutMs = timeoutMs;
|
||||
}
|
||||
}
|
||||
|
||||
export async function withTimeout<T>(
|
||||
operation: () => Promise<T>,
|
||||
{
|
||||
operationName,
|
||||
timeoutMs
|
||||
}: {
|
||||
operationName: string;
|
||||
timeoutMs: number;
|
||||
}
|
||||
): Promise<T> {
|
||||
let timer: NodeJS.Timeout | undefined;
|
||||
|
||||
try {
|
||||
return await Promise.race([
|
||||
operation(),
|
||||
new Promise<T>((_, reject) => {
|
||||
timer = setTimeout(() => {
|
||||
reject(new TimeoutError(operationName, timeoutMs));
|
||||
}, timeoutMs);
|
||||
})
|
||||
]);
|
||||
} finally {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user