diff --git a/packages/core/src/compiler/htmlBundler.test.ts b/packages/core/src/compiler/htmlBundler.test.ts index 0aeddc5c45..99192fd0f5 100644 --- a/packages/core/src/compiler/htmlBundler.test.ts +++ b/packages/core/src/compiler/htmlBundler.test.ts @@ -4306,3 +4306,32 @@ describe("emitRootCompositionVariableStyles — authored definitions", () => { expect(css).not.toContain("--accent-dark"); }); }); + +describe("emitRootCompositionVariableStyles — size budget", () => { + const DECLARER = `
`; + const hostWith = (declarers: number, value: string) => + `` + + `
${DECLARER.repeat(declarers)}
` + + ``; + + it("throws COMPOSITION_TOO_LARGE past the core limit and appends no style", () => { + // 100 rules of ~1 MiB each: just over 32 MiB in total. + const { document } = parseHTML(hostWith(100, "x".repeat(1024 * 1024))); + expect(() => emitRootCompositionVariableStyles(document)).toThrow( + expect.objectContaining({ code: "COMPOSITION_TOO_LARGE" }), + ); + expect(document.querySelector("style[data-hf-composition-variables]")).toBeNull(); + }); + + it("stops building rules when the caller's charge throws", () => { + const { document } = parseHTML(hostWith(5, "x")); + const chargeBytes = vi.fn(() => { + throw new Error("over budget"); + }); + expect(() => emitRootCompositionVariableStyles(document, {}, {}, { chargeBytes })).toThrow( + "over budget", + ); + expect(chargeBytes).toHaveBeenCalledTimes(1); + expect(document.querySelector("style[data-hf-composition-variables]")).toBeNull(); + }); +}); diff --git a/packages/core/src/compiler/htmlBundler.ts b/packages/core/src/compiler/htmlBundler.ts index 3f4d3d1791..0acf4c2e19 100644 --- a/packages/core/src/compiler/htmlBundler.ts +++ b/packages/core/src/compiler/htmlBundler.ts @@ -1602,13 +1602,15 @@ export function emitRootCompositionVariableStyles( document: Document, variablesByComp: Record> = {}, overrides: Record = {}, + options: { chargeBytes?: (bytes: number) => void } = {}, ): boolean { + const charge = options.chargeBytes ?? defaultVariableStyleCharge(); const authoredDefines = authoredDefinesPredicate(document); const layerFor = makeVariableLayer(authoredDefines, overrides); const rules = [ - ...hostScopedVariableRules(variablesByComp, overrides, authoredDefines), - ...rootDeclaredVariableRules(document, layerFor), - ...declarerVariableRules(document, layerFor), + ...hostScopedVariableRules(variablesByComp, overrides, authoredDefines, charge), + ...rootDeclaredVariableRules(document, layerFor, charge), + ...declarerVariableRules(document, layerFor, charge), ]; if (rules.length === 0) return false; const style = document.createElement("style"); @@ -1618,6 +1620,32 @@ export function emitRootCompositionVariableStyles( return true; } +/** The most CSS the compile-time composition-variable rules may add up to when the caller sets no budget of its own. */ +export const MAX_COMPOSITION_VARIABLE_STYLE_BYTES = 32 * 1024 * 1024; + +class CompositionVariableStylesTooLargeError extends Error { + readonly code = "COMPOSITION_TOO_LARGE" as const; + + constructor(bytes: number) { + super( + `Composition variable styles add up to ${(bytes / (1024 * 1024)).toFixed(1)} MB, over the ` + + `${MAX_COMPOSITION_VARIABLE_STYLE_BYTES / (1024 * 1024)} MB limit. Shorten the variable ` + + "values or declare them on fewer elements.", + ); + this.name = "CompositionVariableStylesTooLargeError"; + } +} + +function defaultVariableStyleCharge(): (bytes: number) => void { + let total = 0; + return (bytes) => { + total += bytes; + if (total > MAX_COMPOSITION_VARIABLE_STYLE_BYTES) { + throw new CompositionVariableStylesTooLargeError(total); + } + }; +} + type VariableLayer = ( declared: Record, hostValues: Record, @@ -1682,6 +1710,7 @@ function hostScopedVariableRules( variablesByComp: Record>, overrides: Record, authoredDefines: (id: string) => boolean, + charge: (bytes: number) => void, ): string[] { const rules: string[] = []; for (const [compId, vars] of Object.entries(variablesByComp)) { @@ -1696,15 +1725,23 @@ function hostScopedVariableRules( withOverrides, cssAttributeSelector("data-composition-id", compId), ); - if (rule) rules.push(rule); + if (!rule) continue; + charge(rule.length); + rules.push(rule); } return rules; } -function rootDeclaredVariableRules(document: Document, layerFor: VariableLayer): string[] { +function rootDeclaredVariableRules( + document: Document, + layerFor: VariableLayer, + charge: (bytes: number) => void, +): string[] { const htmlDeclared = readDeclaredDefaults(document.documentElement); const htmlRule = compositionVariablesCssBlock(layerFor(htmlDeclared, {}), ":root"); - return htmlRule ? [htmlRule] : []; + if (!htmlRule) return []; + charge(htmlRule.length); + return [htmlRule]; } /** @@ -1714,20 +1751,32 @@ function rootDeclaredVariableRules(document: Document, layerFor: VariableLayer): * restyle instance B. The nearest ancestor host's data-variable-values * layer over the declared defaults (mirrors the runtime loader). */ -function declarerVariableRules(document: Document, layerFor: VariableLayer): string[] { +function declarerVariableRules( + document: Document, + layerFor: VariableLayer, + charge: (bytes: number) => void, +): string[] { const rules: string[] = []; + const hostValuesByEl = new Map>(); let markerSeq = 0; for (const el of [...document.querySelectorAll("[data-composition-variables]")]) { const declared = readDeclaredDefaults(el); const hostEl = typeof el.closest === "function" ? el.parentElement?.closest("[data-variable-values]") : null; - const hostValues = hostEl ? parseHostVariableValues(hostEl) : {}; + let hostValues: Record = {}; + if (hostEl) { + const cached = hostValuesByEl.get(hostEl); + hostValues = cached ?? parseHostVariableValues(hostEl); + if (!cached) hostValuesByEl.set(hostEl, hostValues); + } const vars = layerFor(declared, hostValues); if (Object.keys(vars).length === 0) continue; markerSeq += 1; el.setAttribute("data-hf-var-scope", String(markerSeq)); const rule = compositionVariablesCssBlock(vars, `[data-hf-var-scope="${markerSeq}"]`); - if (rule) rules.push(rule); + if (!rule) continue; + charge(rule.length); + rules.push(rule); } return rules; } diff --git a/packages/core/src/compiler/index.ts b/packages/core/src/compiler/index.ts index 7feda2e207..a46270371c 100644 --- a/packages/core/src/compiler/index.ts +++ b/packages/core/src/compiler/index.ts @@ -38,6 +38,7 @@ export { prepareFlattenedInnerRoot, FLATTENED_INNER_ROOT_STRIP_ATTRS, emitRootCompositionVariableStyles, + MAX_COMPOSITION_VARIABLE_STYLE_BYTES, parsesAsScript, } from "./htmlBundler"; export { readDeclaredDefaults, parseHostVariableValues } from "../runtime/getVariables"; diff --git a/packages/producer/src/services/compositionSizeLimit.ts b/packages/producer/src/services/compositionSizeLimit.ts index 73edfa31c0..b3ffa71f93 100644 --- a/packages/producer/src/services/compositionSizeLimit.ts +++ b/packages/producer/src/services/compositionSizeLimit.ts @@ -47,6 +47,29 @@ const fileTooLarge = (file: string, bytes: number) => { bytes, limitBytes: MAX_COMPOSITION_HTML_BYTES }, ); +const limitText = `One render generates at most ${megabytes(MAX_EXPANDED_COMPOSITION_BYTES)}`; + +function generatedTooLarge(file: string, bytes: number): string { + if (file === "composition variables") { + return ( + `Composition variable styles add up to ${megabytes(bytes)}. ${limitText}. ` + + "Shorten the variable values or declare them on fewer elements." + ); + } + if (file === "variable media sources") { + return ( + `Variable-bound media sources add up to ${megabytes(bytes)}. ${limitText}. ` + + "Use shorter URLs or bind fewer elements." + ); + } + return ( + `Scripts and styles generated for sub-composition mounts, ending at "${file}", add up to ` + + `${megabytes(bytes)}. One render generates at most ` + + `${megabytes(MAX_EXPANDED_COMPOSITION_BYTES)} for its mounts. Merge small inline scripts ` + + "and style rules, keep data-composition-id values short, or mount scenes fewer times." + ); +} + /** Adds up every mount a render inlines, and what it generates for them, and throws past a limit. */ export class CompositionExpansionBudget { private bytes = 0; @@ -57,13 +80,11 @@ export class CompositionExpansionBudget { chargeGenerated(file: string, bytes: number): void { this.generated += bytes; if (this.generated <= MAX_EXPANDED_COMPOSITION_BYTES) return; - throw new CompositionTooLargeError( - `Scripts and styles generated for sub-composition mounts, ending at "${file}", add up to ` + - `${megabytes(this.generated)}. One render generates at most ` + - `${megabytes(MAX_EXPANDED_COMPOSITION_BYTES)} for its mounts. Merge small inline scripts ` + - "and style rules, keep data-composition-id values short, or mount scenes fewer times.", - { bytes: this.generated, limitBytes: MAX_EXPANDED_COMPOSITION_BYTES, exceeded: "generated" }, - ); + throw new CompositionTooLargeError(generatedTooLarge(file, this.generated), { + bytes: this.generated, + limitBytes: MAX_EXPANDED_COMPOSITION_BYTES, + exceeded: "generated", + }); } charge(file: string, bytes: number): void { diff --git a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts index 2d9d0be7e5..f3fe3c569d 100644 --- a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts +++ b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts @@ -8,7 +8,7 @@ import { MAX_COMPOSITION_HTML_BYTES, MAX_EXPANDED_COMPOSITION_BYTES, } from "./compositionSizeLimit.js"; -import { compileForRender } from "./htmlCompiler.js"; +import { compileForRender, MAX_VARIABLE_MEDIA_SRC_BYTES } from "./htmlCompiler.js"; import { extractSafeRenderErrorCode } from "./safeRenderErrorCode.js"; const ROOT = ` @@ -249,3 +249,73 @@ describe("composition expansion budget", () => { }, ); }); + +describe("variable-driven composition styles and media sources", () => { + const withBody = (body: string) => + ROOT.replace("", "").replace("", `${body}`); + + it("refuses declarers that each repeat a long host value, with no sub-compositions to mount", async () => { + const declarer = `
`; + const host = `
${declarer.repeat(100)}
`; + const dir = project({ + "index.html": withBody(host).replace(/
]*><\/div>/, ""), + }); + const error = await compileError(dir); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(error).toMatchObject({ + owner: "user", + retryable: false, + publicMetadata: { exceeded: "generated" }, + }); + expect(String(error)).toContain("Composition variable styles add up to"); + expect(String(error)).toContain("declare them on fewer elements"); + }); + + it("refuses declarers inside a mounted sub-composition that repeat a long host value", async () => { + const declarer = `
`; + const host = `data-variable-values='${JSON.stringify({ v: "x".repeat(1024 * 1024) })}'`; + const dir = project({ + "index.html": ROOT.replace( + 'data-composition-id="scene"', + `data-composition-id="scene" ${host}`, + ), + "scene.html": SCENE.replace("

Scene

", declarer.repeat(100)), + }); + const error = await compileError(dir); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(error).toMatchObject({ owner: "user", publicMetadata: { exceeded: "generated" } }); + }); + + it("refuses many media elements bound to one long value", async () => { + const url = `https://example.com/${"a".repeat(MAX_VARIABLE_MEDIA_SRC_BYTES - 100)}.mp4`; + const videos = ``.repeat(4_200); + const dir = project({ "index.html": withBody(videos), "scene.html": SCENE }); + const error = await compileForRender(dir, join(dir, "index.html"), dir, { + variables: { clip: url }, + }).then( + () => undefined, + (e: unknown) => e, + ); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(error).toMatchObject({ publicMetadata: { exceeded: "generated" } }); + }); + + it.each([ + ["longer than 8 KiB", `https://example.com/${"a".repeat(MAX_VARIABLE_MEDIA_SRC_BYTES)}.mp4`], + ["holding a control character", "https://example.com/a\nb.mp4"], + ])("leaves src unchanged for a bound value %s, and binds a normal one", async (_, bad) => { + const dir = project({ + "index.html": withBody(``), + "scene.html": SCENE, + }); + const srcFor = async (clip: string) => { + const compiled = await compileForRender(dir, join(dir, "index.html"), dir, { + variables: { clip }, + }); + return parseHTML(compiled.html).document.getElementById("v1")?.getAttribute("src"); + }; + expect(await srcFor(bad)).toBe("fallback.mp4"); + expect(await srcFor("https://example.com/my clip.mp4")).toBe("https://example.com/my clip.mp4"); + expect(await srcFor("https://example.com/x.mp4\n")).toBe("https://example.com/x.mp4"); + }); +}); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index eb098249ab..ff90566ac1 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -1190,6 +1190,7 @@ function inlineSubCompositions( variableOverrides: Record, assetRoot: string | undefined, outsideFiles: OutsideFilePolicy, + budget: CompositionExpansionBudget, ): { html: string; variablesByComp: Record> } { const { document } = parseHTML(html); const head = document.querySelector("head"); @@ -1205,11 +1206,11 @@ function inlineSubCompositions( document as unknown as Document, {}, variableOverrides, + { chargeBytes: (bytes) => budget.chargeGenerated("composition variables", bytes) }, ); return { html: emitted ? document.toString() : html, variablesByComp: {} }; } - const budget = new CompositionExpansionBudget(); const readOnce = new Map(); const readMountHtml = (srcPath: string): string | null => { const filePath = resolveCompositionFile(projectDir, srcPath, assetRoot); @@ -1378,6 +1379,7 @@ function inlineSubCompositions( document as unknown as Document, result.variablesByComp, variableOverrides, + { chargeBytes: (bytes) => budget.chargeGenerated("composition variables", bytes) }, ); // Inlining is what makes element ids ambiguous: each composition file is @@ -2261,11 +2263,20 @@ function rebaseDirectEntryAssetPaths(html: string, projectDir: string, htmlPath: return document.toString(); } +export const MAX_VARIABLE_MEDIA_SRC_BYTES = 8 * 1024; +// oxlint-disable-next-line no-control-regex +const MEDIA_SRC_CONTROL_CHARS = /[\u0000-\u001f\u007f]/; + /** * Point each `data-var-src` video/audio at the clip its variable names, so the length and sound read below * are the bound clip's. Media under a per-instance `data-variable-values` host keeps the browser's answer. */ -function bindVariableMediaSources(html: string, variables: Record): string { +function bindVariableMediaSources( + html: string, + variables: Record, + budget: CompositionExpansionBudget, + log?: ProducerLogger, +): string { if (!html.includes("data-var-src")) return html; const { document } = parseHTML(html); const declared: Record = {}; @@ -2276,8 +2287,17 @@ function bindVariableMediaSources(html: string, variables: Record MAX_VARIABLE_MEDIA_SRC_BYTES || MEDIA_SRC_CONTROL_CHARS.test(src)) { + log?.warn("[Compiler] Refused a variable media source that cannot be a URL", { + length: src.length, + }); + continue; + } + if (el.getAttribute("src") === src) continue; + budget.chargeGenerated("variable media sources", src.length); el.setAttribute("src", src); changed = true; } @@ -2316,9 +2336,12 @@ export async function compileForRender( } assertCompositionFileSize(htmlPath, basename(htmlPath)); const entryHtml = rebaseDirectEntryAssetPaths(readTextFile(htmlPath), projectDir, htmlPath); + const budget = new CompositionExpansionBudget(); const rawHtml = bindVariableMediaSources( normalizeCompositionSrcs(rewriteUnresolvableGsapToCdn(entryHtml, projectDir), projectDir), options.variables ?? {}, + budget, + options.log, ); // Pre-flight: every data-composition-src reference must resolve to a @@ -2369,6 +2392,7 @@ export async function compileForRender( options.variables ?? {}, assetRoot, outsideFiles, + budget, ); // Strip preload="none" from media elements — the renderer needs to load all