mirror of
https://github.com/documenso/documenso.git
synced 2026-07-01 00:30:53 +10:00
138d663c25
Merge origin/main into feat/external-2fa-codes. Resolve formatting conflicts caused by biome rollout; preserve both feature streams: PR's external 2FA token + signing-session 2FA proof additions plus main's RateLimit/RecipientExpired/signingReminders/date-auto-insert. In complete-document-with-token.ts, drop the duplicate early field-fetching block introduced when main moved that logic later with date auto-insert support; keep the EXTERNAL_TWO_FACTOR_AUTH check using derivedRecipientActionAuth.
35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
/**
|
|
* Race a promise against a timeout. Returns `null` if the timeout
|
|
* fires before the promise settles.
|
|
*/
|
|
export const withTimeout = async <T>(promise: Promise<T>, timeoutMs: number) =>
|
|
await Promise.race<T | null>([
|
|
promise,
|
|
new Promise<null>((resolve) => {
|
|
setTimeout(() => resolve(null), timeoutMs);
|
|
}),
|
|
]);
|
|
|
|
/**
|
|
* Wrapper around `fetch` that aborts the request after `timeoutMs`.
|
|
* Throws with a descriptive message on timeout.
|
|
*/
|
|
export const fetchWithTimeout = async (input: string | URL | Request, init: RequestInit & { timeoutMs: number }) => {
|
|
const { timeoutMs, ...fetchInit } = init;
|
|
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
|
|
try {
|
|
return await fetch(input, { ...fetchInit, signal: controller.signal });
|
|
} catch (err) {
|
|
if (err instanceof DOMException && err.name === 'AbortError') {
|
|
throw new Error(`Request timed out after ${timeoutMs}ms`);
|
|
}
|
|
|
|
throw err;
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
}
|
|
};
|