mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-03 13:53:53 +02:00
Stop retrying requests when the source is disposed, log warning in possible CORS detection case (see #158)
This commit is contained in:
@@ -584,6 +584,7 @@ export const retriedFetch = async (
|
|||||||
url: string | URL | Request,
|
url: string | URL | Request,
|
||||||
requestInit: RequestInit,
|
requestInit: RequestInit,
|
||||||
getRetryDelay: (previousAttempts: number, error: unknown, url: string | URL | Request) => number | null,
|
getRetryDelay: (previousAttempts: number, error: unknown, url: string | URL | Request) => number | null,
|
||||||
|
shouldStop: () => boolean,
|
||||||
) => {
|
) => {
|
||||||
let attempts = 0;
|
let attempts = 0;
|
||||||
|
|
||||||
@@ -591,6 +592,10 @@ export const retriedFetch = async (
|
|||||||
try {
|
try {
|
||||||
return await fetchFn(url, requestInit);
|
return await fetchFn(url, requestInit);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (shouldStop()) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
attempts++;
|
attempts++;
|
||||||
const retryDelayInSeconds = getRetryDelay(attempts, error, url);
|
const retryDelayInSeconds = getRetryDelay(attempts, error, url);
|
||||||
|
|
||||||
@@ -607,6 +612,10 @@ export const retriedFetch = async (
|
|||||||
if (retryDelayInSeconds > 0) {
|
if (retryDelayInSeconds > 0) {
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000 * retryDelayInSeconds));
|
await new Promise(resolve => setTimeout(resolve, 1000 * retryDelayInSeconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shouldStop()) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -297,6 +297,10 @@ const DEFAULT_RETRY_DELAY
|
|||||||
= typeof navigator !== 'undefined' && typeof navigator.onLine === 'boolean' ? navigator.onLine : true;
|
= typeof navigator !== 'undefined' && typeof navigator.onLine === 'boolean' ? navigator.onLine : true;
|
||||||
|
|
||||||
if (isOnline && originOfSrc !== null && originOfSrc !== window.location.origin) {
|
if (isOnline && originOfSrc !== null && originOfSrc !== window.location.origin) {
|
||||||
|
console.warn(
|
||||||
|
`Request will not be retried because a CORS error was suspected due to different origins. You can`
|
||||||
|
+ ` modify this behavior by providing your own function for the 'getRetryDelay' option.`,
|
||||||
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -425,6 +429,7 @@ export class UrlSource extends Source {
|
|||||||
signal: abortController.signal,
|
signal: abortController.signal,
|
||||||
}),
|
}),
|
||||||
this._getRetryDelay,
|
this._getRetryDelay,
|
||||||
|
() => this._disposed,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -492,6 +497,7 @@ export class UrlSource extends Source {
|
|||||||
signal: abortController.signal,
|
signal: abortController.signal,
|
||||||
}),
|
}),
|
||||||
this._getRetryDelay,
|
this._getRetryDelay,
|
||||||
|
() => this._disposed,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -539,6 +545,11 @@ export class UrlSource extends Source {
|
|||||||
try {
|
try {
|
||||||
readResult = await reader.read();
|
readResult = await reader.read();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (this._disposed) {
|
||||||
|
// No need to try to retry
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
const retryDelayInSeconds = this._getRetryDelay(1, error, this._url);
|
const retryDelayInSeconds = this._getRetryDelay(1, error, this._url);
|
||||||
if (retryDelayInSeconds !== null) {
|
if (retryDelayInSeconds !== null) {
|
||||||
console.error('Error while reading response stream. Attempting to resume.', error);
|
console.error('Error while reading response stream. Attempting to resume.', error);
|
||||||
|
|||||||
Reference in New Issue
Block a user