Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions packages/engine/src/utils/urlDownloader.limits.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { _setDownloadFetchForTests, downloadToTemp, UrlDownloadError } from "./urlDownloader.js";

const stubFetch = (replacement: unknown): void =>
_setDownloadFetchForTests(replacement as Parameters<typeof _setDownloadFetchForTests>[0]);

const dirs: string[] = [];

afterEach(() => {
vi.unstubAllEnvs();
_setDownloadFetchForTests();
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});

function tempDir(): string {
const dir = mkdtempSync(join(tmpdir(), "hf-url-limits-"));
dirs.push(dir);
return dir;
}

const MEDIA = "media-payload-bytes";

function mediaResponse(headers: Record<string, string> = {}): Response {
return new Response(MEDIA, { headers: { "content-length": `${MEDIA.length}`, ...headers } });
}

describe("downloadToTemp's Content-Encoding policy", () => {
it.each(["gzip", "br", " GZIP ", "identity, gzip"])(
"refuses a %j response without retrying or leaving a file",
async (encoding) => {
const fetchMock = vi.fn(async () => mediaResponse({ "content-encoding": encoding }));
stubFetch(fetchMock);
const dir = tempDir();

await expect(
downloadToTemp("https://cdn.example/encoded.mp4", dir, 1_000),
).rejects.toMatchObject({
kind: "http_rejected",
retryable: false,
message: "Download response is compressed; only identity Content-Encoding is accepted",
} satisfies Partial<UrlDownloadError>);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(readdirSync(dir)).toEqual([]);
},
);

it.each([
["absent", {}],
["identity", { "content-encoding": "identity" }],
["padded Identity", { "content-encoding": " Identity " }],
])("downloads a response whose Content-Encoding is %s", async (_case, headers) => {
stubFetch(vi.fn(async () => mediaResponse(headers)));

const path = await downloadToTemp("https://cdn.example/plain.mp4", tempDir(), 1_000);

expect(readFileSync(path, "utf8")).toBe(MEDIA);
});
});

describe("downloadToTemp's byte limit", () => {
it("refuses a declared Content-Length over the limit before reading the body", async () => {
vi.stubEnv("HYPERFRAMES_MAX_DOWNLOAD_BYTES", "16");
const cancel = vi.fn();
// The body never sends, so a reader would hang until the 1 s timeout instead.
const body = new ReadableStream<Uint8Array>({ cancel });
const fetchMock = vi.fn(
async () => new Response(body, { headers: { "content-length": "17" } }),
);
stubFetch(fetchMock);
const dir = tempDir();

await expect(downloadToTemp("https://cdn.example/big.mp4", dir, 1_000)).rejects.toMatchObject({
kind: "length_mismatch",
retryable: false,
message: "Download exceeded the 16-byte limit",
telemetry: expect.objectContaining({ expectedBytes: 17 }),
} satisfies Partial<UrlDownloadError>);
expect(cancel).toHaveBeenCalled();
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(readdirSync(dir)).toEqual([]);
});

it("stops an endless body with no Content-Length at the limit and removes the partial", async () => {
const limit = 1024 * 1024;
const chunkBytes = 64 * 1024;
vi.stubEnv("HYPERFRAMES_MAX_DOWNLOAD_BYTES", `${limit}`);
const cancel = vi.fn();
const fetchMock = vi.fn(
async () =>
new Response(
new ReadableStream<Uint8Array>({
pull(controller) {
controller.enqueue(new Uint8Array(chunkBytes).fill(0x61));
},
cancel,
}),
),
);
stubFetch(fetchMock);
const dir = tempDir();

const error = await downloadToTemp("https://cdn.example/endless.mp4", dir, 10_000).catch(
(caught: unknown) => caught,
);

expect(error).toMatchObject({
kind: "length_mismatch",
retryable: false,
message: `Download exceeded the ${limit}-byte limit`,
});
const receivedBytes = (error as UrlDownloadError).telemetry?.receivedBytes ?? 0;
expect(receivedBytes).toBeGreaterThan(limit);
expect(receivedBytes).toBeLessThanOrEqual(limit + chunkBytes);
expect(cancel).toHaveBeenCalled();
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(readdirSync(dir)).toEqual([]);
});

it.each(["", "abc", "0", "-5", "1.5"])(
"keeps the default limit when HYPERFRAMES_MAX_DOWNLOAD_BYTES is %j",
async (value) => {
vi.stubEnv("HYPERFRAMES_MAX_DOWNLOAD_BYTES", value);
stubFetch(vi.fn(async () => mediaResponse()));

const path = await downloadToTemp("https://cdn.example/default-limit.mp4", tempDir(), 1_000);

expect(readFileSync(path, "utf8")).toBe(MEDIA);
},
);
});
115 changes: 111 additions & 4 deletions packages/engine/src/utils/urlDownloader.progress.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { mkdtempSync, readdirSync, readFileSync, rmSync, statSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
Expand All @@ -15,6 +15,7 @@ beforeEach(() => {

afterEach(() => {
vi.useRealTimers();
vi.unstubAllEnvs();
_setDownloadFetchForTests();
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
Expand Down Expand Up @@ -60,6 +61,48 @@ function trickle(
return new Response(body, { headers });
}

/**
* A sized body that sends `chunk` `everyMs` after each read, `count` times. Waiting for
* `readerWaiting()` before advancing the clock keeps real disk writes from lagging fake time.
*/
function pacedBody(chunk: Uint8Array, count: number, everyMs: number) {
let sent = 0;
let waiting = false;
const body = new ReadableStream<Uint8Array>(
{
pull(controller) {
waiting = true;
return new Promise<void>((resolve) => {
setTimeout(() => {
waiting = false;
controller.enqueue(chunk);
sent += 1;
if (sent === count) controller.close();
resolve();
}, everyMs);
});
},
},
{ highWaterMark: 0 },
);
const response = new Response(body, {
headers: { "content-length": `${chunk.length * count}` },
});
return {
response,
readerWaiting: () => waiting || sent === count,
finished: () => sent === count,
};
}

/** Like `waitForIo`, but bounded by real time so a loaded disk cannot fall behind the fake clock. */
async function waitForRealIo(done: () => boolean, budgetMs = 10_000): Promise<void> {
const deadline = performance.now() + budgetMs;
while (!done() && performance.now() < deadline) {
await new Promise((resolve) => setImmediate(resolve));
}
}

/** Real I/O (cache lock, file writes) settles on the real event loop; the fake clock stays put. */
async function waitForIo(done: () => boolean, turns = 100_000): Promise<void> {
for (let i = 0; i < turns && !done(); i += 1) {
Expand All @@ -82,16 +125,44 @@ async function startDownload(response: () => Response) {
const fetchMock = vi.fn(async () => response());
stubFetch(fetchMock);
const outcome: { path?: string; error?: unknown } = {};
void downloadToTemp("https://cdn.example/media.mp4", tempDir()).then(
const dir = tempDir();
void downloadToTemp("https://cdn.example/media.mp4", dir).then(
(path) => (outcome.path = path),
(error: unknown) => (outcome.error = error),
);
await waitForIo(fetchCalls(fetchMock, 1));
return { fetchMock, outcome };
return { fetchMock, outcome, dir };
}

const disableThroughputFloor = (): void =>
vi.stubEnv("HYPERFRAMES_MIN_DOWNLOAD_BYTES_PER_SEC", "0");

const tooSlow = {
kind: "timeout",
retryable: true,
message: "Download too slow: under 32768 bytes/s over the last 60s",
};

describe("downloadToTemp's timeout", () => {
it("lets a sized download that sends a chunk every 100 s for 400 s finish", async () => {
it("cuts a sized download that sends a chunk every 100 s at the throughput floor", async () => {
const chunks = ["chunk-1 ", "chunk-2 ", "chunk-3 ", "chunk-4"];
const { fetchMock, outcome, dir } = await startDownload(() =>
trickle(chunks, 100_000, { contentLength: chunks.join("").length }),
);
await advanceSeconds(159);
expect(fetchMock).toHaveBeenCalledTimes(1);
await advanceSeconds(1);
await waitForIo(fetchCalls(fetchMock, 2));
expect(fetchMock).toHaveBeenCalledTimes(2);
await advanceSeconds(160);
await waitForIo(() => "error" in outcome);

expect(outcome.error).toMatchObject(tooSlow);
expect(readdirSync(dir)).toEqual([]);
});

it("lets the same 100 s trickle finish when the floor is disabled with 0", async () => {
disableThroughputFloor();
const chunks = ["chunk-1 ", "chunk-2 ", "chunk-3 ", "chunk-4"];
const { fetchMock, outcome } = await startDownload(() =>
trickle(chunks, 100_000, { contentLength: chunks.join("").length }),
Expand All @@ -104,7 +175,42 @@ describe("downloadToTemp's timeout", () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
});

it("cuts a 1 KiB/s trickle that declares a huge Content-Length and removes the partial", async () => {
const { fetchMock, outcome, dir } = await startDownload(() =>
trickle(["x".repeat(1024)], 1_000, { contentLength: 10 * 1024 ** 3, then: "repeat" }),
);
await advanceSeconds(60);
expect(fetchMock).toHaveBeenCalledTimes(1);
await advanceSeconds(1);
await waitForIo(fetchCalls(fetchMock, 2));
expect(fetchMock).toHaveBeenCalledTimes(2);
await advanceSeconds(61);
await waitForIo(() => "error" in outcome);

expect(outcome.error).toMatchObject(tooSlow);
expect(readdirSync(dir)).toEqual([]);
});

it("lets a steady sized download above the floor run past 15 minutes", async () => {
const chunk = new Uint8Array(40 * 1024).fill(0x78);
const seconds = 15 * 60 + 20;
const paced = pacedBody(chunk, seconds, 1_000);
const startedAt = Date.now();
const { fetchMock, outcome } = await startDownload(() => paced.response);
while (!paced.finished() && !("error" in outcome)) {
await waitForRealIo(paced.readerWaiting);
await vi.advanceTimersByTimeAsync(1_000);
}
await waitForRealIo(() => "path" in outcome || "error" in outcome);

expect(outcome.error).toBeUndefined();
expect(Date.now() - startedAt).toBeGreaterThan(15 * 60_000);
expect(statSync(outcome.path!).size).toBe(chunk.length * seconds);
expect(fetchMock).toHaveBeenCalledTimes(1);
}, 120_000);

it("cuts a body with no Content-Length at the fixed 300 s cap even while it streams", async () => {
disableThroughputFloor();
const { fetchMock, outcome } = await startDownload(() =>
trickle(["tick "], 10_000, { then: "repeat" }),
);
Expand Down Expand Up @@ -141,6 +247,7 @@ describe("downloadToTemp's timeout", () => {
});

it("fails a sized body 300 s after its last chunk and says it stalled", async () => {
disableThroughputFloor();
const { fetchMock, outcome } = await startDownload(() =>
trickle(["part-1 "], 100_000, { contentLength: 1_000, then: "hang" }),
);
Expand Down
Loading
Loading