diff --git a/docs/guides/rendering.mdx b/docs/guides/rendering.mdx index c6faf94acd..156445d403 100644 --- a/docs/guides/rendering.mdx +++ b/docs/guides/rendering.mdx @@ -416,6 +416,8 @@ Keep the first exact error rather than only the final “render failed” messag 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. +The same error refuses a render whose sub-composition mounts add up to more than 32 MB of composition HTML, or to more than 10,000 mounts. Every mount counts, including each repeat of the same file and each mount nested inside another. + 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 index d4da9c1a91..a39284fe52 100644 --- a/packages/producer/src/services/compositionSizeLimit.ts +++ b/packages/producer/src/services/compositionSizeLimit.ts @@ -11,22 +11,65 @@ const COMPOSITION_TOO_LARGE = "COMPOSITION_TOO_LARGE" as const; const megabytes = (bytes: number) => `${(bytes / (1024 * 1024)).toFixed(1)} MB`; +/** + * What one render may inline across every sub-composition mount. Each file is checked on its own + * above, but a few small files that mount each other many times assemble a page far larger. + */ +export const MAX_EXPANDED_COMPOSITION_BYTES = 2 * MAX_COMPOSITION_HTML_BYTES; +const MAX_COMPOSITION_MOUNTS = 10_000; + 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.", - ); + constructor( + message: string, + readonly publicMetadata: Readonly<{ + bytes: number; + limitBytes: number; + mounts?: number; + exceeded?: "bytes" | "mounts"; + }>, + ) { + super(message); this.name = "CompositionTooLargeError"; - this.publicMetadata = { bytes, limitBytes: MAX_COMPOSITION_HTML_BYTES }; + } +} + +const fileTooLarge = (file: string, bytes: number) => + new CompositionTooLargeError( + `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.", + { bytes, limitBytes: MAX_COMPOSITION_HTML_BYTES }, + ); + +/** Adds up every mount a render inlines and throws once the total passes either limit. */ +export class CompositionExpansionBudget { + private bytes = 0; + private mounts = 0; + + charge(file: string, bytes: number): void { + this.bytes += bytes; + this.mounts += 1; + if (this.bytes <= MAX_EXPANDED_COMPOSITION_BYTES && this.mounts <= MAX_COMPOSITION_MOUNTS) { + return; + } + throw new CompositionTooLargeError( + `Sub-composition mounts, ending at "${file}", add up to ${megabytes(this.bytes)} ` + + `across ${this.mounts} mounts. One render inlines at most ` + + `${megabytes(MAX_EXPANDED_COMPOSITION_BYTES)} of composition HTML in at most ` + + `${MAX_COMPOSITION_MOUNTS} mounts. Mount large or repeated scenes fewer times.`, + { + bytes: this.bytes, + limitBytes: MAX_EXPANDED_COMPOSITION_BYTES, + mounts: this.mounts, + exceeded: this.mounts > MAX_COMPOSITION_MOUNTS ? "mounts" : "bytes", + }, + ); } } @@ -43,5 +86,5 @@ export function assertCompositionFileSize(path: string, file: string): void { } catch { return; } - if (bytes > MAX_COMPOSITION_HTML_BYTES) throw new CompositionTooLargeError(file, bytes); + if (bytes > MAX_COMPOSITION_HTML_BYTES) throw fileTooLarge(file, bytes); } diff --git a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts index 14b233b963..974c3729aa 100644 --- a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts +++ b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts @@ -2,7 +2,11 @@ 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 { + assertCompositionFileSize, + MAX_COMPOSITION_HTML_BYTES, + MAX_EXPANDED_COMPOSITION_BYTES, +} from "./compositionSizeLimit.js"; import { compileForRender } from "./htmlCompiler.js"; import { extractSafeRenderErrorCode } from "./safeRenderErrorCode.js"; @@ -76,3 +80,78 @@ describe("composition HTML size limit", () => { expect(() => assertCompositionFileSize(dir, "folder")).not.toThrow(); }); }); + +/** A root that mounts `src` `count` times. */ +const rootMounting = (src: string, count: number) => + ROOT.replace( + /
]*><\/div>/, + `
`.repeat( + count, + ), + ); + +/** A sub-composition that mounts `src` `count` times, as a bare fragment or inside a `