fix: make property-assessor safer for whatsapp runs

This commit is contained in:
2026-03-28 01:28:59 -05:00
parent 2deeb31369
commit 3d7ce7617c
15 changed files with 640 additions and 217 deletions

View 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);
}
}
}