Skip to content

Commit 723e5c4

Browse files
committed
fix(core): a copy's retry flag is sent on the first ask only, not on every poll
1 parent 12dcdfe commit 723e5c4

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎packages/core/src/runtime/mediaProxy.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,25 @@ describe("preview quality: lighter copies of heavy files", () => {
10761076
});
10771077
});
10781078

1079+
it("sends the retry flag on the first ask only, so a copy that fails again is not remade on every poll", async () => {
1080+
vi.useFakeTimers();
1081+
const fetchMock = vi.fn(async () => new Response("no", { status: 502 }));
1082+
vi.stubGlobal("fetch", fetchMock);
1083+
const { video, setPreviewQuality, retryPreviewCopies } = await stage({ "/video.mp4": HEAVY });
1084+
video();
1085+
setPreviewQuality(0.5);
1086+
await vi.advanceTimersByTimeAsync(10_000);
1087+
1088+
fetchMock.mockClear();
1089+
retryPreviewCopies();
1090+
await vi.advanceTimersByTimeAsync(10_000);
1091+
1092+
const asked = fetchMock.mock.calls.map(([href]) => String(href));
1093+
expect(asked.length).toBeGreaterThan(1);
1094+
expect(asked[0]).toContain("hf-proxy-retry=1");
1095+
expect(asked.slice(1).some((href) => href.includes("hf-proxy-retry"))).toBe(false);
1096+
});
1097+
10791098
it("ignores a level that is not one of the four", async () => {
10801099
const { video, setPreviewQuality } = await stage({ "/video.mp4": HEAVY });
10811100
const el = video();

‎packages/core/src/runtime/proxyWait.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { PREVIEW_PROXY_PROGRESS_HEADER } from "../previewProxyBox";
1+
import { PREVIEW_PROXY_PROGRESS_HEADER, PREVIEW_PROXY_RETRY_PARAM } from "../previewProxyBox";
22

33
const MAX_FAILED_ASKS = 3;
44

5+
function withoutRetryFlag(href: string): string {
6+
const url = new URL(href, document.baseURI);
7+
url.searchParams.delete(PREVIEW_PROXY_RETRY_PARAM);
8+
return url.href;
9+
}
10+
511
/** Asks for one byte of a `?hf-proxy=` URL until it is served, after each Retry-After: while the
612
* server answers 202 (copy being made) or 503 (queue full), and after up to 3 failed asks. */
713
export async function waitForServedProxy(
@@ -10,10 +16,12 @@ export async function waitForServedProxy(
1016
onProgress?: (fraction: number) => void,
1117
): Promise<boolean> {
1218
let failed = 0;
19+
let ask = href;
1320
while (live()) {
14-
const res = await fetch(href, { headers: { Range: "bytes=0-0" }, cache: "no-store" }).catch(
21+
const res = await fetch(ask, { headers: { Range: "bytes=0-0" }, cache: "no-store" }).catch(
1522
() => null,
1623
);
24+
ask = withoutRetryFlag(href);
1725
void res?.body?.cancel().catch(() => {});
1826
const pending = res?.status === 202 || res?.status === 503;
1927
const reached = res?.status === 202 ? res.headers.get(PREVIEW_PROXY_PROGRESS_HEADER) : null;

0 commit comments

Comments
 (0)