Download web/src/net/edgeRetry.ts from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 3.42 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/net/edgeRetry.ts
- Command line
-
hf download hf://spaces/fsanyoto/loopable/web/src/net/edgeRetry.ts
-
curl -L -o edgeRetry.ts https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/net/edgeRetry.ts
3.42 kB
| // SDLC 67 close (2026-09-29): THE HOSTING EDGE DROPS REQUESTS, SO READS RETRY. | |
| // | |
| // Measured on 2026-09-29: HF's Space proxy answered 502 (its own HTML page, never reaching | |
| // uvicorn) on 12-20% of requests under a browser-like burst, on staging, on public live AND on a | |
| // third-party Space (gradio-hello-world, 3 of 24). After the app booted, `/nav` and | |
| // `/nav/applications` came back 502 and the rail read "Navigation unavailable". Nothing in the | |
| // app retried, so one dropped read was one broken panel. | |
| // | |
| // ⛔ ONLY SAFE METHODS RETRY (GET, HEAD). A POST that 502s may or may not have reached the server; | |
| // replaying it could write twice. Those keep failing loudly, exactly as before. | |
| // ⛔ ONLY SAME-ORIGIN. A third-party endpoint has its own retry semantics. | |
| // ⛔ AN ABORTED READ NEVER RETRIES: aborting is how a component says "I no longer want this". | |
| // Installed BEFORE `installActivityLog`, so the activity log wraps THIS fetch and records one | |
| // request with its final status, not every attempt. | |
| const RETRY_STATUS = new Set([502, 503, 504]); | |
| const SAFE_METHODS = new Set(["GET", "HEAD"]); | |
| /** Delays before attempts 2 and 3. Three attempts at a ~20% drop rate fail ~0.8% of reads. */ | |
| export const RETRY_DELAYS_MS = [250, 700]; | |
| let installed = false; | |
| function methodOf(input: RequestInfo | URL, init?: RequestInit): string { | |
| return String(init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase(); | |
| } | |
| function sameOrigin(input: RequestInfo | URL): boolean { | |
| try { | |
| const raw = input instanceof Request ? input.url : String(input); | |
| return new URL(raw, window.location.origin).origin === window.location.origin; | |
| } catch { | |
| return false; | |
| } | |
| } | |
| function aborted(input: RequestInfo | URL, init?: RequestInit): boolean { | |
| return Boolean(init?.signal?.aborted || (input instanceof Request && input.signal?.aborted)); | |
| } | |
| function wait(ms: number): Promise<void> { | |
| return new Promise(resolve => window.setTimeout(resolve, ms)); | |
| } | |
| /** PURE over its inputs: may this request be retried after `status` (0 = network failure)? */ | |
| export function mayRetry(method: string, isSameOrigin: boolean, status: number): boolean { | |
| return SAFE_METHODS.has(method) && isSameOrigin && (status === 0 || RETRY_STATUS.has(status)); | |
| } | |
| export function withEdgeRetry(nativeFetch: typeof fetch, delays: number[] = RETRY_DELAYS_MS): typeof fetch { | |
| return async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => { | |
| const method = methodOf(input, init); | |
| const local = sameOrigin(input); | |
| for (let attempt = 0; ; attempt++) { | |
| const last = attempt >= delays.length; | |
| try { | |
| const response = await nativeFetch(input, init); | |
| if (last || !mayRetry(method, local, response.status) || aborted(input, init)) return response; | |
| void response.body?.cancel().catch(() => undefined); // free the dropped 502 page | |
| } catch (error) { | |
| if (last || !mayRetry(method, local, 0) || aborted(input, init)) throw error; | |
| } | |
| await wait(delays[attempt]); | |
| } | |
| }; | |
| } | |
| export function installEdgeRetry(): void { | |
| if (typeof window === "undefined" || installed) return; | |
| installed = true; | |
| window.fetch = withEdgeRetry(window.fetch.bind(window)); | |
| } | |
| declare global { | |
| interface Window { | |
| /** Defined by index.html's boot watchdog; main.tsx calls it once every static chunk ran. */ | |
| __aiosBootOk?: () => void; | |
| } | |
| } | |