diff --git a/docs/guides/rendering.mdx b/docs/guides/rendering.mdx index 9d1a9fc432..c6faf94acd 100644 --- a/docs/guides/rendering.mdx +++ b/docs/guides/rendering.mdx @@ -414,6 +414,8 @@ npx hyperframes check Keep the first exact error rather than only the final “render failed” message. See [Troubleshooting](/help) for the next checks. +A render refuses any composition HTML file, the entry or a sub-composition, that is larger than 16 MB. The error code is `COMPOSITION_TOO_LARGE`. Keep images, fonts and data in separate files and reference them by path instead of inlining them. + Always watch the exported file itself. Preview proves that the project can play; the output file proves that the delivery is correct. diff --git a/packages/producer/src/services/compositionSizeLimit.ts b/packages/producer/src/services/compositionSizeLimit.ts new file mode 100644 index 0000000000..d4da9c1a91 --- /dev/null +++ b/packages/producer/src/services/compositionSizeLimit.ts @@ -0,0 +1,47 @@ +import { statSync } from "node:fs"; + +/** + * The largest entry or sub-composition HTML file a render compiles. Compiling and probing a page + * runs on the render server's main thread, and their cost grows with the page's size, so one huge + * page would hold up every other render in the same process. + */ +export const MAX_COMPOSITION_HTML_BYTES = 16 * 1024 * 1024; + +const COMPOSITION_TOO_LARGE = "COMPOSITION_TOO_LARGE" as const; + +const megabytes = (bytes: number) => `${(bytes / (1024 * 1024)).toFixed(1)} MB`; + +class CompositionTooLargeError extends Error { + readonly code = COMPOSITION_TOO_LARGE; + // The producer server reads these off the error for the wire envelope. + readonly owner = "user" as const; + readonly retryable = false as const; + readonly publicMetadata: Readonly<{ bytes: number; limitBytes: number }>; + + constructor(file: string, bytes: number) { + super( + `Composition file "${file}" is ${megabytes(bytes)}, over the ` + + `${megabytes(MAX_COMPOSITION_HTML_BYTES)} limit for one composition HTML file. ` + + "Keep large data such as images, fonts and JSON in separate asset files and reference " + + "them by path instead of inlining them.", + ); + this.name = "CompositionTooLargeError"; + this.publicMetadata = { bytes, limitBytes: MAX_COMPOSITION_HTML_BYTES }; + } +} + +/** + * Throws {@link CompositionTooLargeError} when `path` is a file over the limit, before anything + * reads it. A missing path or a folder passes, so the caller's own read reports it as before. + */ +export function assertCompositionFileSize(path: string, file: string): void { + let bytes: number; + try { + const stats = statSync(path); + if (!stats.isFile()) return; + bytes = stats.size; + } catch { + return; + } + if (bytes > MAX_COMPOSITION_HTML_BYTES) throw new CompositionTooLargeError(file, bytes); +} diff --git a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts new file mode 100644 index 0000000000..14b233b963 --- /dev/null +++ b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts @@ -0,0 +1,78 @@ +import { mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { describe, expect, it } from "vitest"; +import { assertCompositionFileSize, MAX_COMPOSITION_HTML_BYTES } from "./compositionSizeLimit.js"; +import { compileForRender } from "./htmlCompiler.js"; +import { extractSafeRenderErrorCode } from "./safeRenderErrorCode.js"; + +const ROOT = ` + +
+
+ +
+`; +const SCENE = ``; + +/** `html` followed by a comment that brings the file to exactly `bytes`. */ +const paddedTo = (html: string, bytes: number) => + `${html}`; + +function project(files: Record): string { + const dir = mkdtempSync(join(tmpdir(), "hf-compiler-size-")); + for (const [name, text] of Object.entries(files)) writeFileSync(join(dir, name), text); + return dir; +} + +async function compileError(dir: string): Promise { + return compileForRender(dir, join(dir, "index.html"), dir).then( + () => undefined, + (error: unknown) => error, + ); +} + +describe("composition HTML size limit", () => { + it("refuses an entry file over the limit before compiling it", async () => { + const dir = project({ + "index.html": paddedTo(ROOT, MAX_COMPOSITION_HTML_BYTES + 1), + "scene.html": SCENE, + }); + const started = performance.now(); + const error = await compileError(dir); + expect(performance.now() - started).toBeLessThan(1_000); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(String(error)).toContain('"index.html"'); + expect(String(error)).toContain("16.0 MB limit"); + }); + + it("refuses a sub-composition file over the limit", async () => { + const dir = project({ + "index.html": ROOT, + "scene.html": paddedTo(SCENE, MAX_COMPOSITION_HTML_BYTES + 1), + }); + const error = await compileError(dir); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(String(error)).toContain('"scene.html"'); + }); + + it("compiles files at the limit", async () => { + const dir = project({ + "index.html": paddedTo(ROOT, MAX_COMPOSITION_HTML_BYTES), + "scene.html": SCENE, + }); + const compiled = await compileForRender(dir, join(dir, "index.html"), dir); + expect(compiled.html).toContain("

Scene

"); + }); + + it("leaves missing paths and folders to the caller's own read", () => { + const dir = project({}); + expect(() => + assertCompositionFileSize(join(dir, "missing.html"), "missing.html"), + ).not.toThrow(); + expect(() => assertCompositionFileSize(dir, "folder")).not.toThrow(); + }); +}); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 84d309b35e..f1983fa09b 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -101,6 +101,7 @@ import { } from "@hyperframes/parsers/asset-resolution"; import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path"; import { extractMediaMetadata, extractAudioMetadata } from "../utils/ffprobe.js"; +import { assertCompositionFileSize } from "./compositionSizeLimit.js"; import { escapesAssetRoot, isPathInside, @@ -340,6 +341,7 @@ function assertSubCompositionsUsable( // silence here rather than pretend it surfaces an error somewhere else. if (visited.has(filePath)) continue; + assertCompositionFileSize(filePath, srcPath); const read = readProjectFile(filePath); if (read.kind === "missing") { problems.push({ srcPath, detail: "the file does not exist" }); @@ -880,6 +882,7 @@ async function parseSubCompositions( continue; } + assertCompositionFileSize(filePath, srcPath); const read = readProjectFile(filePath); if (read.kind !== "file") { continue; @@ -1170,6 +1173,7 @@ function inlineSubCompositions( let compHtml = subCompositions.get(srcPath) || null; if (!compHtml) { const filePath = resolveCompositionFile(projectDir, srcPath, assetRoot); + if (filePath) assertCompositionFileSize(filePath, srcPath); const read = filePath && !isPrivateCompositionFile(projectDir, filePath) ? readProjectFile(filePath) @@ -2217,6 +2221,7 @@ export async function compileForRender( if (!COMPOSITION_FILE.test(htmlPath)) { throw new Error(`Entry file ${htmlPath} is not an .html file, so it is refused`); } + assertCompositionFileSize(htmlPath, basename(htmlPath)); const entryHtml = rebaseDirectEntryAssetPaths(readTextFile(htmlPath), projectDir, htmlPath); const rawHtml = bindVariableMediaSources( normalizeCompositionSrcs(rewriteUnresolvableGsapToCdn(entryHtml, projectDir), projectDir), diff --git a/packages/producer/src/services/htmlTagScans.test.ts b/packages/producer/src/services/htmlTagScans.test.ts index 6756560d2b..ea705f3295 100644 --- a/packages/producer/src/services/htmlTagScans.test.ts +++ b/packages/producer/src/services/htmlTagScans.test.ts @@ -4,6 +4,7 @@ import { countTagsWithAttr, countedTagNames, firstCompositionIdTag, + hasClosedMediaTag, inlineScriptBlocks, remoteSrcUrls, removeDelimitedRuns, @@ -291,6 +292,12 @@ describe("htmlTagScans agree with the regexes they replace", { timeout: 60_000 } } }); + it("closed media tag test", () => { + for (const text of both(7, FOCUSED.attrs)) { + expect(hasClosedMediaTag(text), text).toBe(/<(?:video|audio)\b[^>]*>/i.test(text)); + } + }); + it("remote src urls", () => { for (const html of both(5, FOCUSED.remote)) { for (const tag of ["media", "source", "img"] as const) { @@ -340,6 +347,7 @@ describe("htmlTagScans run in linear time", () => { ], ["crossorigin", (h) => stripCrossoriginAttr(h, "video"), ["