Files
hyperframes/packages/cli/src/utils/staticProjectServer.range.chromium.test.ts
T
Miguel ÁngelandHuntsDesk c54f84859d fix(studio-server): long media clips no longer leave the host unanswered (#7002)
* fix(studio-server): more than six clips on one media file no longer starve check and preview of connections

Every file server answered `Range: bytes=N-` with the rest of the file. A
media element reads that until its buffer is full, then stops reading but
keeps the response open, so each <video> held one of Chrome's six HTTP/1.1
connections to the host. With seven or more <video> clips on the server's
host, every other request queued behind them and `hyperframes check`
stalled at 0% CPU. A long recording cut into trimmed segments, each its own
<video> on the same file, is enough.

An open-ended range of audio or video now gets at most 4 MiB
(OPEN_MEDIA_RANGE_BYTES), as a normal 206 with an exact Content-Range. The
element asks for the next range as it plays or seeks. Bounded and suffix
ranges, and files that aren't media, are served as before. The helper lives
in studio-server; its preview route and the CLI's static project server and
composition server use it.

Fixes #6029

Claude-Session: https://claude.ai/code/session_014aw1z3VrTNenka3LGDBBLa

* fix(studio-server): carry the range cap's reason in names and tests, not comments

The comment ratchet doesn't let a file's comment share rise: mediaRange.ts
was 48% comments against the package's 7%, and staticProjectServer.ts gained
a line. The cap is now MAX_OPEN_MEDIA_RANGE_BYTES, and the why and the
repro stay in the three tests and #6029.

Claude-Session: https://claude.ai/code/session_014aw1z3VrTNenka3LGDBBLa

* fix(producer): the render file server bounds open-ended media ranges too

parseRangeHeader takes the response's content type and caps an open-ended
`bytes=N-` on audio or video with openRangeEnd, like the CLI and Studio
servers. The producer imports it from a new `media-range` subpath of
@hyperframes/studio-server, as it does `encoder-options`, so a render
doesn't load the rest of the package.

Claude-Session: https://claude.ai/code/session_01DW4V8HZZx5JUgMffb3juVV

* test(cli): eight clips on one file no longer leave the host unanswered

A real Chromium page buffers eight long video clips of one file from the project server and then asks the same host for another file; with open-ended ranges unbounded the request stays unanswered, with the bounded piece it is answered at once.

* docs(producer): range comments name the open-ended form in lowercase

* docs(cli): the range test's comment says what holds now

---------

Co-authored-by: HuntsDesk <hunter@talloak.com>
2026-11-02 11:03:55 +00:00

90 lines
3.5 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, it } from "vitest";
import puppeteer, { type Browser } from "puppeteer-core";
import { execFileSync } from "node:child_process";
import { mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { MAX_OPEN_MEDIA_RANGE_BYTES } from "@hyperframes/studio-server";
import { serveStaticProjectHtml, type StaticProjectServer } from "./staticProjectServer.js";
const executablePath = process.env.PUPPETEER_EXECUTABLE_PATH;
const ffmpegPresent = (() => {
try {
execFileSync("ffmpeg", ["-version"], { stdio: "ignore" });
return true;
} catch {
return false;
}
})();
// Chromium opens at most six HTTP/1.1 connections to a host, and a <video> keeps its response open once its buffer
// is full, so eight of them on one file, each served to EOF, would pin every connection and leave the next request
// to the host unanswered.
const CLIPS = 8;
const PINNED = 6;
const ANSWER_WITHIN_MS = 10_000;
describe.runIf(executablePath && ffmpegPresent)("media ranges in Chromium", () => {
let browser: Browser;
let dir: string;
let server: StaticProjectServer;
beforeAll(async () => {
browser = await puppeteer.launch({ executablePath, args: ["--no-sandbox"] });
dir = mkdtempSync(join(tmpdir(), "media-range-chromium-"));
// Long and noisy, so about 45 MB of real media: a clip whose whole duration fits the buffer is read to its end
// and closed, which pins nothing.
const clip = join(dir, "clip.mp4");
execFileSync("ffmpeg", [
...["-v", "error", "-f", "lavfi", "-i", "color=c=black:s=640x360:d=120:r=25"],
...["-vf", "geq=random(1)*255:128:128", "-pix_fmt", "yuv420p"],
...["-c:v", "libx264", "-preset", "ultrafast", "-b:v", "3M", "-movflags", "+faststart"],
...["-y", clip],
]);
expect(statSync(clip).size, "longer than one bounded piece per clip").toBeGreaterThan(
CLIPS * MAX_OPEN_MEDIA_RANGE_BYTES,
);
writeFileSync(join(dir, "probe.txt"), "ok");
// Chromium shares one loader between elements with the same URL, so each clip gets its own query string.
const clips = Array.from(
{ length: CLIPS },
(_, index) => `<video src="clip.mp4?clip=${index}" preload="auto" muted></video>`,
).join("");
server = await serveStaticProjectHtml(dir, `<!doctype html><body>${clips}</body>`);
}, 120_000);
afterAll(async () => {
await browser?.close();
await server?.close();
if (dir) rmSync(dir, { recursive: true, force: true });
});
it("answers a new request to the host while eight clips of one file are buffering", async () => {
expect(server.port, "a real socket, so the browser's connection limit applies").toBeGreaterThan(
0,
);
const page = await browser.newPage();
try {
await page.goto(server.url, { waitUntil: "load" });
await page.waitForFunction(
(pinned) =>
[...document.querySelectorAll("video")].filter((video) => video.readyState >= 3).length >=
pinned,
{ timeout: 60_000 },
PINNED,
);
const outcome = await page.evaluate(
(limit) =>
Promise.race([
fetch(`/probe.txt?${Date.now()}`).then((response) => response.status),
new Promise<string>((resolve) => setTimeout(() => resolve("unanswered"), limit)),
]),
ANSWER_WITHIN_MS,
);
expect(outcome, "the host still answers while clips buffer").toBe(200);
} finally {
await page.close();
}
}, 120_000);
});