Skip to content

Commit 8825def

Browse files
fix(studio-server): stream preview media byte ranges instead of reading the whole file (#3745)
The Studio preview asset route answered every Range request by reading the entire file into memory with readFileSync and slicing the window out of the buffer. A browser refills a playing <video> or <audio> with a fresh Range request every few hundred milliseconds and issues one per seek, so a source of a few hundred MB cost a full synchronous read per refill and per scrub step. The read also blocked the event loop, so the voice track, saves and the file-change stream all waited behind it. Sources over 2 GiB could not be served at all, because readFileSync refuses them. Stream only the requested window with createReadStream, take the size from stat instead of the buffer, and answer 416 for a range that starts past the end. Text assets keep the in-memory utf-8 round trip. The sibling static project server already did this.
1 parent 0d5d3f3 commit 8825def

2 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎packages/studio-server/src/routes/preview.test.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
// fallow-ignore-file code-duplication
22
import { afterEach, describe, expect, it, vi } from "vitest";
33
import { Hono } from "hono";
4-
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
4+
import {
5+
closeSync,
6+
ftruncateSync,
7+
mkdirSync,
8+
mkdtempSync,
9+
openSync,
10+
rmSync,
11+
symlinkSync,
12+
writeFileSync,
13+
writeSync,
14+
} from "node:fs";
515
import { tmpdir } from "node:os";
616
import { join } from "node:path";
717
import { registerPreviewRoutes } from "./preview";
@@ -1204,3 +1214,39 @@ describe("hf-proxy negotiation and media codec map injection (U3)", () => {
12041214
});
12051215
});
12061216
});
1217+
1218+
describe("preview asset byte ranges", () => {
1219+
it("streams a slice of a media file too large to read whole", async () => {
1220+
// A sparse 3 GiB file costs no disk. readFileSync refuses anything over
1221+
// 2 GiB (ERR_FS_FILE_TOO_LARGE), so a route that buffers the whole file
1222+
// cannot serve a single byte of it; streaming the window must.
1223+
const projectDir = createProjectDir();
1224+
const size = 3 * 1024 * 1024 * 1024;
1225+
const fd = openSync(join(projectDir, "clip.mp4"), "w");
1226+
writeSync(fd, "WXYZ", 5_000_000);
1227+
ftruncateSync(fd, size);
1228+
closeSync(fd);
1229+
1230+
const app = new Hono();
1231+
registerPreviewRoutes(app, createAdapter(projectDir));
1232+
const res = await app.request("http://localhost/projects/demo/preview/clip.mp4", {
1233+
headers: { Range: "bytes=5000000-5000003" },
1234+
});
1235+
expect(res.status).toBe(206);
1236+
expect(res.headers.get("Content-Range")).toBe(`bytes 5000000-5000003/${size}`);
1237+
expect(res.headers.get("Content-Length")).toBe("4");
1238+
expect(await res.text()).toBe("WXYZ");
1239+
});
1240+
1241+
it("answers 416 for a range that starts past the end of the file", async () => {
1242+
const projectDir = createProjectDir();
1243+
writeFileSync(join(projectDir, "clip.mp4"), "abc");
1244+
const app = new Hono();
1245+
registerPreviewRoutes(app, createAdapter(projectDir));
1246+
const res = await app.request("http://localhost/projects/demo/preview/clip.mp4", {
1247+
headers: { Range: "bytes=99-200" },
1248+
});
1249+
expect(res.status).toBe(416);
1250+
expect(res.headers.get("Content-Range")).toBe("bytes */3");
1251+
});
1252+
});

‎packages/studio-server/src/routes/preview.ts‎

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Hono } from "hono";
2-
import { existsSync, readFileSync, statSync } from "node:fs";
2+
import { createReadStream, existsSync, readFileSync, statSync } from "node:fs";
3+
import { Readable } from "node:stream";
34
import { join, resolve } from "node:path";
45
import { createHash } from "node:crypto";
56
import { injectScriptsIntoHtml, stripEmbeddedRuntimeScripts } from "@hyperframes/core/compiler";
@@ -580,34 +581,45 @@ export function registerPreviewRoutes(api: Hono, adapter: PreviewApiAdapter): vo
580581
servedContentType = PROXY_VARIANT_CONFIG[proxyVariant].contentType;
581582
}
582583

583-
const buffer: Buffer = isText
584-
? Buffer.from(readFileSync(file, "utf-8"), "utf-8")
585-
: readFileSync(servedPath);
586-
const totalSize = buffer.length;
584+
// Text is small and keeps its utf-8 round trip in memory. Binary media
585+
// streams only the requested window: Chrome refills a playing <video> or
586+
// <audio> with a fresh Range request every few hundred milliseconds, and a
587+
// 1KB slice of a multi-hundred-MB source must not readFileSync the whole
588+
// file on each one. The full read also blocked the event loop, so every
589+
// other Studio request (SSE, saves, the voice track) waited behind it.
590+
const textBuffer = isText ? Buffer.from(readFileSync(file, "utf-8"), "utf-8") : null;
591+
const totalSize = textBuffer ? textBuffer.length : statSync(servedPath).size;
592+
const bodyFor = (start: number, end: number): BodyInit =>
593+
textBuffer
594+
? new Uint8Array(textBuffer.subarray(start, end + 1))
595+
: (Readable.toWeb(createReadStream(servedPath, { start, end })) as ReadableStream);
587596

588597
// Support byte-range requests so browsers can seek audio/video elements.
589598
const rangeHeader = c.req.header("Range");
590-
if (rangeHeader) {
591-
const match = /bytes=(\d+)-(\d*)/.exec(rangeHeader);
592-
if (match) {
593-
const start = parseInt(match[1]!, 10);
594-
const end = match[2] ? parseInt(match[2], 10) : totalSize - 1;
595-
const safeEnd = Math.min(end, totalSize - 1);
596-
const chunkSize = safeEnd - start + 1;
597-
return new Response(new Uint8Array(buffer.slice(start, safeEnd + 1)), {
598-
status: 206,
599-
headers: {
600-
...cacheHeaders,
601-
"Content-Type": servedContentType,
602-
"Content-Range": `bytes ${start}-${safeEnd}/${totalSize}`,
603-
"Accept-Ranges": "bytes",
604-
"Content-Length": String(chunkSize),
605-
},
599+
const match = rangeHeader ? /bytes=(\d+)-(\d*)/.exec(rangeHeader) : null;
600+
if (match) {
601+
const start = parseInt(match[1]!, 10);
602+
const end = match[2] ? parseInt(match[2], 10) : totalSize - 1;
603+
const safeEnd = Math.min(end, totalSize - 1);
604+
if (start > safeEnd) {
605+
return new Response(null, {
606+
status: 416,
607+
headers: { ...cacheHeaders, "Content-Range": `bytes */${totalSize}` },
606608
});
607609
}
610+
return new Response(bodyFor(start, safeEnd), {
611+
status: 206,
612+
headers: {
613+
...cacheHeaders,
614+
"Content-Type": servedContentType,
615+
"Content-Range": `bytes ${start}-${safeEnd}/${totalSize}`,
616+
"Accept-Ranges": "bytes",
617+
"Content-Length": String(safeEnd - start + 1),
618+
},
619+
});
608620
}
609621

610-
return new Response(new Uint8Array(buffer), {
622+
return new Response(totalSize > 0 ? bodyFor(0, totalSize - 1) : null, {
611623
headers: {
612624
...cacheHeaders,
613625
"Content-Type": servedContentType,

0 commit comments

Comments
 (0)