24 lines
968 B
TypeScript
24 lines
968 B
TypeScript
/**
|
|
* Old Firefox cancels a credentialed POST that needed a CORS preflight and
|
|
* reports NS_BINDING_ABORTED. Fetch rejects that as a TypeError, not an
|
|
* HTTP response. A caller-owned AbortController uses AbortError instead, so
|
|
* an intentional timeout is not retried here.
|
|
*/
|
|
export function isBrowserBindingAbort(error: unknown): boolean {
|
|
if (!(error instanceof Error)) return false;
|
|
const text = `${error.name} ${error.message}`.toLowerCase();
|
|
if (text.includes("ns_binding_aborted") || text.includes("binding aborted")) return true;
|
|
if (error.name !== "TypeError") return false;
|
|
return /networkerror|failed to fetch|load failed|network error/.test(text);
|
|
}
|
|
|
|
export async function fetchWithBindingRetry(run: () => Promise<Response>): Promise<Response> {
|
|
try {
|
|
return await run();
|
|
} catch (error) {
|
|
if (!isBrowserBindingAbort(error)) throw error;
|
|
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
return await run();
|
|
}
|
|
}
|