Files
hyperframes/packages/core/src/runtime/proxyWait.ts
T
Miguel Ángel e5b10f8bc2 feat(core): a heavy clip's lighter copy can be made ahead of time (#6982)
* feat(core): make a heavy clip's lighter copy ahead of time when the host asks

* feat(core): keep the comment share of the proxy transcoder where it was

* feat(core): split the copy-ahead choice so each part stays simple

* feat(core): the preview route passes the copy's urgency as the proxy options expect
2026-11-02 07:15:45 +00:00

54 lines
2.0 KiB
TypeScript

import {
PREVIEW_PROXY_PRIORITY_HEADER,
PREVIEW_PROXY_PROGRESS_HEADER,
PREVIEW_PROXY_RETRY_PARAM,
} from "../previewProxyBox";
const MAX_FAILED_ASKS = 3;
function withoutRetryFlag(href: string): string {
const url = new URL(href, document.baseURI);
url.searchParams.delete(PREVIEW_PROXY_RETRY_PARAM);
return url.href;
}
function askHeaders(atPlayhead: boolean, background: boolean): Record<string, string> {
const headers: Record<string, string> = { Range: "bytes=0-0" };
if (atPlayhead) headers[PREVIEW_PROXY_PRIORITY_HEADER] = "1";
else if (background) headers[PREVIEW_PROXY_PRIORITY_HEADER] = "0";
return headers;
}
function reportProgress(res: Response | null, onProgress?: (fraction: number) => void): void {
const reached = res?.status === 202 ? res.headers.get(PREVIEW_PROXY_PROGRESS_HEADER) : null;
if (reached !== null && Number.isFinite(Number(reached))) {
onProgress?.(Math.min(1, Math.max(0, Number(reached))));
}
}
/** Asks for one byte of a `?hf-proxy=` URL until it is served, after each Retry-After: while the
* server answers 202 (copy being made) or 503 (queue full), and after up to 3 failed asks. */
export async function waitForServedProxy(
href: string,
live: () => boolean,
onProgress?: (fraction: number) => void,
atPlayhead: () => boolean = () => false,
background = false,
): Promise<boolean> {
let failed = 0;
let ask = href;
while (live()) {
const headers = askHeaders(atPlayhead(), background);
const res = await fetch(ask, { headers, cache: "no-store" }).catch(() => null);
ask = withoutRetryFlag(href);
void res?.body?.cancel().catch(() => {});
const pending = res?.status === 202 || res?.status === 503;
reportProgress(res, onProgress);
if (!pending && res?.ok) return true;
if (!pending && ++failed > MAX_FAILED_ASKS) return false;
const seconds = Math.min(Number(res?.headers.get("Retry-After")) || 2, 30);
await new Promise((resolveWait) => setTimeout(resolveWait, seconds * 1000));
}
return false;
}