Skip to content

Commit 07917a8

Browse files
committed
fix(media-use): stop the freeze timeout from aborting long downloads
The 10 s AbortSignal.timeout on freezeUrl also aborted the response body, so any stock video, generated video or LUT that took longer than 10 s to download failed. The timer now covers only the wait for response headers and is cleared once they arrive; the 256 MB cap still bounds the body.
1 parent b8490b4 commit 07917a8

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎packages/cli/src/media-use/lib/freeze.mjs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { dirname } from "node:path";
66
// ponytail: bound the download so a hostile/runaway URL can't fill the disk.
77
// 256MB covers any real media asset; raise if 4K video sources ever exceed it.
88
const MAX_FREEZE_BYTES = 256 * 1024 * 1024;
9+
// Bounds only the wait for response headers; a large video body may stream for minutes.
10+
const FREEZE_HEADERS_TIMEOUT_MS = 10_000;
911

1012
const isSvgPath = (destPath) => /\.svg$/i.test(destPath);
1113

@@ -20,7 +22,15 @@ function writeFrozen(destPath, buffer) {
2022

2123
export async function freezeUrl(url, destPath) {
2224
const where = String(url).slice(0, 80);
23-
const res = await fetchMedia(url, { signal: AbortSignal.timeout(10_000) });
25+
const headers = new AbortController();
26+
const timer = setTimeout(
27+
() =>
28+
headers.abort(
29+
new Error(`freeze failed: no response within ${FREEZE_HEADERS_TIMEOUT_MS} ms for ${where}`),
30+
),
31+
FREEZE_HEADERS_TIMEOUT_MS,
32+
);
33+
const res = await fetchMedia(url, { signal: headers.signal }).finally(() => clearTimeout(timer));
2434
if (!res.ok) throw new Error(`freeze failed: HTTP ${res.status} for ${where}`);
2535

2636
const body = await readCappedBody(res, MAX_FREEZE_BYTES, `freeze failed for ${where}`);

‎packages/cli/src/media-use/lib/freeze.test.mjs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,54 @@ test("freezeUrl strips <script>, on* handlers and javascript: hrefs from a fetch
6969
assert.ok(out.includes("<circle"), "benign markup survives");
7070
});
7171

72+
// Puts setTimeout and AbortSignal.timeout on node:test's fake clock.
73+
function fakeClock(t) {
74+
t.mock.timers.enable({ apis: ["setTimeout"] });
75+
t.mock.method(AbortSignal, "timeout", (ms) => {
76+
const controller = new AbortController();
77+
setTimeout(() => controller.abort(new DOMException("timed out", "TimeoutError")), ms);
78+
return controller.signal;
79+
});
80+
}
81+
82+
test("freezeUrl keeps streaming a slow body past the header timeout", async (t) => {
83+
fakeClock(t);
84+
let body;
85+
t.mock.method(globalThis, "fetch", async (_url, { signal }) => {
86+
const stream = new ReadableStream({
87+
start(controller) {
88+
body = controller;
89+
// undici aborts the body stream too when the request signal fires.
90+
signal.addEventListener("abort", () => controller.error(signal.reason));
91+
controller.enqueue(new Uint8Array([1, 2, 3]));
92+
},
93+
});
94+
return new Response(stream, { status: 200 });
95+
});
96+
const dest = join(mkdtempSync(join(tmpdir(), "media-use-freeze-")), "clip.mp4");
97+
const frozen = freezeUrl("https://cdn.example.com/clip.mp4", dest);
98+
await new Promise(setImmediate);
99+
t.mock.timers.tick(60_000);
100+
body.enqueue(new Uint8Array([4, 5]));
101+
body.close();
102+
assert.equal(await frozen, 5);
103+
assert.deepEqual([...readFileSync(dest)], [1, 2, 3, 4, 5]);
104+
});
105+
106+
test("freezeUrl times out when no response headers arrive", { timeout: 5_000 }, async (t) => {
107+
fakeClock(t);
108+
t.mock.method(
109+
globalThis,
110+
"fetch",
111+
(_url, { signal }) =>
112+
new Promise((_, reject) => signal.addEventListener("abort", () => reject(signal.reason))),
113+
);
114+
const dest = join(mkdtempSync(join(tmpdir(), "media-use-freeze-")), "clip.mp4");
115+
const frozen = freezeUrl("https://cdn.example.com/clip.mp4", dest);
116+
t.mock.timers.tick(10_000);
117+
await assert.rejects(frozen, /no response within 10000 ms/);
118+
});
119+
72120
test("freezeLocalFile strips the same hostile SVG content from a local source", () => {
73121
const srcDir = mkdtempSync(join(tmpdir(), "media-use-freeze-src-"));
74122
const src = join(srcDir, "in.svg");

0 commit comments

Comments
 (0)