diff --git a/docs/guides/rendering.mdx b/docs/guides/rendering.mdx index 156445d403..ac17405c4f 100644 --- a/docs/guides/rendering.mdx +++ b/docs/guides/rendering.mdx @@ -416,7 +416,7 @@ 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. +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. The scripts and scoped styles generated for those mounts have their own 32 MB limit. Every inline script in a mounted file is wrapped in a few kilobytes of scoping code, and every style rule is prefixed with its host's `data-composition-id`. Many tiny inline scripts, or a long composition id over many style rules, can reach this limit. Merge small scripts and rules, or keep ids short. Always watch the exported file itself. Preview proves that the project can play; the output file diff --git a/packages/core/src/compiler/compositionScoping.scoper.test.ts b/packages/core/src/compiler/compositionScoping.scoper.test.ts index 1d30937919..fe60ba9528 100644 --- a/packages/core/src/compiler/compositionScoping.scoper.test.ts +++ b/packages/core/src/compiler/compositionScoping.scoper.test.ts @@ -66,6 +66,33 @@ const scopesFor = (id: string) => [ ]; describe("createCompositionCssScoper", () => { + it("reserves an upper bound for each rule before scoping it and settles to the sheet's size", () => { + const scope = createCompositionCssScoper(); + const selectorList = Array.from({ length: 200 }, (_, i) => `.s${i}`).join(", "); + const wideSeparator = `.first,${" ".repeat(5_000)}${selectorList} { color: red; }`; + for (const id of ["card", "c.a:rd", 'ca"rd', "hf-scope-placeholder"]) { + for (const css of [...randomSheets(id, 200), wideSeparator]) { + for (const authoredRootId of [undefined, "card-root"]) { + for (const selector of [...scopesFor(id), scopeOf("x".repeat(500))]) { + for (const scoper of [scope, scopeCssToComposition]) { + const charges: number[] = []; + const options = { compoundAuthoredRoot: true, scopeRootSelectors: true }; + const out = scoper(css, id, selector, authoredRootId, { + ...options, + charge: (bytes: number) => charges.push(bytes), + }); + // Every settlement after a reservation is a refund: the bound held. + expect(Math.max(0, ...charges.slice(1).filter((_, i) => i % 2 === 1))).toBe(0); + expect(charges.reduce((sum, bytes) => sum + bytes, 0)).toBeGreaterThanOrEqual( + out.length, + ); + } + } + } + } + } + }, 30_000); + it("scopes every sheet exactly as scopeCssToComposition does, whatever the mount's scope", () => { const scope = createCompositionCssScoper(); for (const id of ["card", "c.a:rd", 'ca"rd', "ca]rd", "hf-scope-placeholder"]) { diff --git a/packages/core/src/compiler/compositionScoping.ts b/packages/core/src/compiler/compositionScoping.ts index e878ec7db7..b8ed8e088a 100644 --- a/packages/core/src/compiler/compositionScoping.ts +++ b/packages/core/src/compiler/compositionScoping.ts @@ -198,7 +198,12 @@ function isNestedInsideAnotherRule(rule: Rule): boolean { return false; } -type ScopeOptions = { compoundAuthoredRoot?: boolean; scopeRootSelectors?: boolean }; +type ScopeOptions = { + compoundAuthoredRoot?: boolean; + scopeRootSelectors?: boolean; + /** Told the sheet's size, an upper bound for each rule before it is scoped, then the correction. */ + charge?: (bytes: number) => void; +}; export function scopeCssToComposition( css: string, @@ -213,7 +218,26 @@ export function scopeCssToComposition( scopeSelectorOverride || `[data-composition-id="${escapeCssAttributeValue(trimmedCompositionId)}"]`; const root = parseCss(css); - return root ? scopeParsedCss(root, scope, trimmedCompositionId, authoredRootId, options) : ""; + return root + ? scopeParsedCss(root, css.length, scope, trimmedCompositionId, authoredRootId, options) + : ""; +} + +/** Up to two forms per selector, each with the scope per id mention plus twice, and an attribute per `#`. */ +function scopedSelectorsBound( + selectors: readonly string[], + scope: string, + authoredRootId: string | null | undefined, +): number { + const authoredAttribute = 26 + 2 * (authoredRootId?.trim().length ?? 0); + let bound = 0; + for (const selector of selectors) { + const mentions = selector.split("data-composition-id").length - 1; + const hashes = selector.split("#").length - 1; + const form = selector.length + hashes * authoredAttribute + (mentions + 2) * scope.length + 64; + bound += 2 * form + 2; + } + return bound; } function parseCss(css: string): postcss.Root | null { @@ -234,13 +258,24 @@ function forEachScopedRule(root: postcss.Root, visit: (rule: Rule) => void): voi function scopeParsedCss( root: postcss.Root, + cssLength: number, scope: string, compositionId: string, authoredRootId: string | null | undefined, options: ScopeOptions | undefined, ): string { + const charge = options?.charge; + charge?.(cssLength); forEachScopedRule(root, (rule) => { - rule.selectors = rule.selectors.map((selector) => + const authored = rule.selector.length; + const selectors = rule.selectors; + // postcss re-joins the list with the first separator in the authored selector. + const separator = selectors.length > 1 ? (rule.selector.match(/,\s*/)?.[0].length ?? 1) : 0; + const reserved = charge + ? scopedSelectorsBound(selectors, scope, authoredRootId) + (selectors.length - 1) * separator + : 0; + charge?.(reserved); + rule.selectors = selectors.map((selector) => scopeSelector( selector, scope, @@ -250,6 +285,7 @@ function scopeParsedCss( options?.scopeRootSelectors, ), ); + charge?.(rule.selector.length - authored - reserved); }); return root.toResult({ map: false }).css; } @@ -295,9 +331,20 @@ export function createCompositionCssScoper(): typeof scopeCssToComposition { template = scopeTemplate(css, trimmedId, authoredRootId, options); templates.set(key, template); } - return template === null - ? scopeCssToComposition(css, compositionId, scopeSelectorOverride, authoredRootId, options) - : template.split(PLACEHOLDER_SCOPE).join(scope); + if (template === null) { + return scopeCssToComposition( + css, + compositionId, + scopeSelectorOverride, + authoredRootId, + options, + ); + } + const pieces = template.split(PLACEHOLDER_SCOPE); + options?.charge?.( + template.length + (pieces.length - 1) * (scope.length - PLACEHOLDER_SCOPE.length), + ); + return pieces.join(scope); }; } @@ -318,7 +365,8 @@ function scopeTemplate( }); return rawSelector ? null - : scopeParsedCss(root, PLACEHOLDER_SCOPE, compositionId, authoredRootId, options); + : // The cached template stays in memory for the render, so it is charged as well as each join. + scopeParsedCss(root, css.length, PLACEHOLDER_SCOPE, compositionId, authoredRootId, options); } function isFontFaceAtRule(node: { type: string; name?: string }): node is AtRule { diff --git a/packages/core/src/compiler/inlineSubCompositions.ts b/packages/core/src/compiler/inlineSubCompositions.ts index 8b79144458..783462027b 100644 --- a/packages/core/src/compiler/inlineSubCompositions.ts +++ b/packages/core/src/compiler/inlineSubCompositions.ts @@ -306,6 +306,11 @@ export interface InlineSubCompositionsOptions { /** Tag each top-level host with `data-hf-scene` and report every part's scene, for preview swaps. */ tagScenes?: boolean; readProjectScript?: (path: string) => string | null; + /** + * Told the size of each script and stylesheet generated for a mount: each wrapped script once + * built, and each scoped sheet rule by rule as `scopeCssToComposition` reports it. May throw. + */ + chargeEmitted?: (srcPath: string, bytes: number) => void; } export interface ExternalLink { @@ -401,6 +406,7 @@ export function inlineSubCompositions( assetExists, tagScenes = false, readProjectScript, + chargeEmitted, } = options; const rootStyles = [...document.querySelectorAll("style")]; @@ -574,12 +580,15 @@ export function inlineSubCompositions( css = rewriteCssAssetUrls(raw, src, assetExists); sheetsWithAssetUrls.set(sheetKey, css); } - return scopeCompId - ? scopeCssToComposition(css, scopeCompId, runtimeScope || undefined, authoredRootId, { - compoundAuthoredRoot: compoundAuthoredRoot === true, - scopeRootSelectors: true, - }) - : css; + if (!scopeCompId) { + chargeEmitted?.(src, css.length); + return css; + } + return scopeCssToComposition(css, scopeCompId, runtimeScope || undefined, authoredRootId, { + compoundAuthoredRoot: compoundAuthoredRoot === true, + scopeRootSelectors: true, + ...(chargeEmitted ? { charge: (bytes: number) => chargeEmitted(src, bytes) } : {}), + }); }; // hoisting is unconditional. A templated sub-composition's webfont @@ -647,9 +656,9 @@ export function inlineSubCompositions( const prelude = scriptCompositionId ? scopedModulePrelude(runtimeCompId || scopeCompId || scriptCompositionId, src) : ""; - moduleScripts.push( - `${prelude}${AUTHORED_SOURCE_BEGIN}\n${scriptEl.textContent || ""}\n${AUTHORED_SOURCE_END}`, - ); + const moduleScript = `${prelude}${AUTHORED_SOURCE_BEGIN}\n${scriptEl.textContent || ""}\n${AUTHORED_SOURCE_END}`; + chargeEmitted?.(src, moduleScript.length); + moduleScripts.push(moduleScript); } else if (externalSrc) { if (!externalScriptSrcs.includes(externalSrc)) { externalScriptSrcs.push(externalSrc); @@ -672,6 +681,7 @@ export function inlineSubCompositions( src, ) : wrapInlineScriptWithErrorBoundary(scriptEl.textContent || "", scriptErrorLabel); + chargeEmitted?.(src, wrappedScript.length); scripts.push(wrappedScript); scriptItems.push({ kind: "inline", content: wrappedScript, ...(scene ? { scene } : {}) }); } diff --git a/packages/producer/src/services/compositionSizeLimit.ts b/packages/producer/src/services/compositionSizeLimit.ts index a39284fe52..73edfa31c0 100644 --- a/packages/producer/src/services/compositionSizeLimit.ts +++ b/packages/producer/src/services/compositionSizeLimit.ts @@ -30,7 +30,7 @@ class CompositionTooLargeError extends Error { bytes: number; limitBytes: number; mounts?: number; - exceeded?: "bytes" | "mounts"; + exceeded?: "bytes" | "mounts" | "generated"; }>, ) { super(message); @@ -47,10 +47,24 @@ const fileTooLarge = (file: string, bytes: number) => { bytes, limitBytes: MAX_COMPOSITION_HTML_BYTES }, ); -/** Adds up every mount a render inlines and throws once the total passes either limit. */ +/** Adds up every mount a render inlines, and what it generates for them, and throws past a limit. */ export class CompositionExpansionBudget { private bytes = 0; private mounts = 0; + private generated = 0; + + /** `bytes` may be negative when a reserved upper bound is settled to the real size. */ + 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" }, + ); + } charge(file: string, bytes: number): void { this.bytes += bytes; diff --git a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts index faea1d1ac5..2d9d0be7e5 100644 --- a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts +++ b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts @@ -156,6 +156,64 @@ describe("composition expansion budget", () => { expect(compiled.html.split("

Scene

").length - 1).toBe(6); }); + it("refuses a mount whose tiny scripts would each be wrapped in kilobytes of scoping code", async () => { + const scripts = ``.repeat(3_000); + const dir = project({ + "index.html": rootMounting("scene.html", 1), + "scene.html": SCENE.replace("

Scene

", `

Scene

${scripts}`), + }); + const error = await compileError(dir); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(error).toMatchObject({ publicMetadata: { exceeded: "generated" } }); + // Charged as each wrapper is built, so building stops one wrapper past the limit. + const { bytes } = (error as { publicMetadata: { bytes: number } }).publicMetadata; + expect(bytes - MAX_EXPANDED_COMPOSITION_BYTES).toBeLessThan(64 * 1024); + }); + + it("refuses a long host id over many style rules before building the scoped sheet", async () => { + const id = `s${"x".repeat(20_000)}`; + const rules = Array.from({ length: 2_000 }, (_, i) => `.r${i}{color:red}`).join(""); + const dir = project({ + "index.html": ROOT.replace("", "").replace( + 'data-composition-id="scene"', + `data-composition-id="${id}"`, + ), + "scene.html": SCENE.replace("

Scene

", `

Scene

`), + }); + const error = await compileError(dir); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(error).toMatchObject({ publicMetadata: { exceeded: "generated" } }); + // Each rule reserves its upper bound first, so no rule is scoped past the limit. + const { bytes } = (error as { publicMetadata: { bytes: number } }).publicMetadata; + expect(bytes - MAX_EXPANDED_COMPOSITION_BYTES).toBeLessThan(10 * id.length); + }); + + it("reserves the separator a scoped selector list is re-joined with", async () => { + const selectors = Array.from({ length: 2_000 }, (_, i) => `.r${i}`).join(","); + const sheet = `.first,${" ".repeat(20_000)}${selectors}{color:red}`; + const dir = project({ + "index.html": ROOT.replace("", ""), + "scene.html": SCENE.replace("

Scene

", `

Scene

`), + }); + const error = await compileError(dir); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(error).toMatchObject({ publicMetadata: { exceeded: "generated" } }); + }); + + it("compiles scenes with ordinary scripts and styles", async () => { + const scene = SCENE.replace( + "

Scene

", + `

Scene

`, + ); + const dir = project({ + "index.html": rootMounting("wrapper.html", 3), + "wrapper.html": sceneMounting("wrapper", "scene.html", 20), + "scene.html": scene, + }); + const compiled = await compileForRender(dir, join(dir, "index.html"), dir); + expect(compiled.html.split("

Scene

").length - 1).toBe(60); + }); + /** The fan-out project with `depth` nested templates in `file`, the innermost one mounting the fan-out. */ function nestedFanOut(file: string, depth: number): Record { const nested = `${"".repeat(depth)}`; diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index a13ad183cd..eb098249ab 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -1267,6 +1267,7 @@ function inlineSubCompositions( if (compHtml) budget.charge(srcPath, Buffer.byteLength(compHtml)); return compHtml; }, + chargeEmitted: (srcPath: string, bytes: number) => budget.chargeGenerated(srcPath, bytes), parseHtml: (htmlStr: string) => parseHTML(htmlStr).document as unknown as Document, // Mirrors the preview bundler: a sub-composition's SIBLING assets resolve // against its own directory, project-root refs stay as authored. diff --git a/scripts/package-size-budgets.json b/scripts/package-size-budgets.json index 22472f74f9..19952a0535 100644 --- a/scripts/package-size-budgets.json +++ b/scripts/package-size-budgets.json @@ -8,7 +8,7 @@ "unpacked": 25500000 }, "@hyperframes/core": { - "packed": 1820000, + "packed": 1830000, "unpacked": 4639000 }, "@hyperframes/engine": {