mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
* fix(producer): page timers during a render run on film time * test(producer): a regression film whose timer bars must follow film time * test(producer): schedule the timers regression film in CI shards * fix(producer): keep setup timers on the wall clock until the page reports ready * fix(producer): keep the renderer's own waits and deadlines on the wall clock * test(engine): fail CI on a bare timer in code the engine runs in the page * fix(core): type the wall-clock timers like the global timers they replace * chore(engine): drop the comment on the shim's window type
61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { wallSetTimeout } from "./wallClock";
|
|
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,
|
|
onAsk?: () => void,
|
|
): Record<string, string> {
|
|
onAsk?.();
|
|
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,
|
|
onAsk?: () => void,
|
|
): Promise<boolean> {
|
|
let failed = 0;
|
|
let ask = href;
|
|
while (live()) {
|
|
const headers = askHeaders(atPlayhead(), background, onAsk);
|
|
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) => wallSetTimeout(resolveWait, seconds * 1000));
|
|
}
|
|
return false;
|
|
}
|