Files
quickdrop/app/utils/download.ts
2026-08-07 22:13:25 -05:00

31 lines
802 B
TypeScript

export const TRANSIENT_DOWNLOAD_STATUSES = [
408, 425, 429, 500, 502, 503, 504,
] as const;
export function leaseStorageKey(id: string): string {
return `quickdrop-lease:${id}`;
}
export async function retryTransient(
operation: () => Promise<Response>,
attempts = 5,
): Promise<Response> {
for (let attempt = 0; ; attempt++) {
try {
const response = await operation();
if (
response.ok ||
!TRANSIENT_DOWNLOAD_STATUSES.includes(response.status as never) ||
attempt >= attempts - 1
)
return response;
} catch (error) {
if (attempt >= attempts - 1) throw error;
}
await new Promise((resolve) =>
setTimeout(
resolve,
Math.round(500 * 2 ** attempt * (0.8 + Math.random() * 0.4)),
),
);
}
}