Skip to content

Commit ab2b9f3

Browse files
committed
fix: improve fetchBlob with retry logic and error handling
1 parent 27d9a71 commit ab2b9f3

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

src/lib/player/services/ffmpeg/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,23 @@ export const getFFmpegURLs = () => ({
2727
const ensureMimeType = (blob: Blob, mimeType: string): Blob =>
2828
blob.type === mimeType ? blob : new Blob([blob], { type: mimeType });
2929

30-
const fetchBlob = async (url: string): Promise<Blob> => {
31-
const response = await fetch(url);
32-
if (!response.ok) throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
33-
return await response.blob();
30+
const MAX_FETCH_ATTEMPTS = 3;
31+
const FETCH_RETRY_DELAY_MS = 1000;
32+
33+
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
34+
35+
const fetchBlob = async (url: string, attempt = 0): Promise<Blob> => {
36+
try {
37+
const response = await fetch(url, { cache: 'no-store' });
38+
if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
39+
return await response.blob();
40+
} catch (error) {
41+
if (attempt >= MAX_FETCH_ATTEMPTS - 1) {
42+
throw new Error(`Failed to fetch ${url}: ${(error as Error).message}`);
43+
}
44+
await sleep(FETCH_RETRY_DELAY_MS * (attempt + 1));
45+
return fetchBlob(url, attempt + 1);
46+
}
3447
};
3548

3649
export const loadCachedFFmpegBlobs = async (): Promise<{ core: Blob; wasm: Blob } | null> => {

0 commit comments

Comments
 (0)