mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(producer): charge generated mount scripts and scoped styles to the budget (#8094)
* fix(producer): charge generated mount scripts and scoped styles to the expansion budget The expansion budget charged only the source bytes of each mounted file. The inliner wraps every inline script in kilobytes of scoping code and prefixes every style rule with the host's scope, so tiny scripts or a long composition id over many rules assembled a page hundreds of times larger than its source. The inliner now reports what it generates through a chargeEmitted option. Each wrapped script is charged once built. Each style rule reserves an upper bound before it is scoped and settles to its real size after, so no rule is built past the limit. Generated output has its own 32 MB allowance. * fix(core): reserve the separator postcss re-joins scoped selectors with The per-rule reservation summed a bound per selector but left out the separator. postcss's selectors setter re-joins the list with the first separator in the authored selector, so a wide first separator over many selectors grew the rule past its reservation before it was charged. * test(core): give the scoper bound test the same time limit as its neighbour
This commit is contained in:
@@ -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.
|
||||
|
||||
<Tip>
|
||||
Always watch the exported file itself. Preview proves that the project can play; the output file
|
||||
|
||||
@@ -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"]) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) } : {}),
|
||||
});
|
||||
};
|
||||
|
||||
// <link> 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 } : {}) });
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -156,6 +156,64 @@ describe("composition expansion budget", () => {
|
||||
expect(compiled.html.split("<h1>Scene</h1>").length - 1).toBe(6);
|
||||
});
|
||||
|
||||
it("refuses a mount whose tiny scripts would each be wrapped in kilobytes of scoping code", async () => {
|
||||
const scripts = `<script>window.n = (window.n || 0) + 1;</script>`.repeat(3_000);
|
||||
const dir = project({
|
||||
"index.html": rootMounting("scene.html", 1),
|
||||
"scene.html": SCENE.replace("<h1>Scene</h1>", `<h1>Scene</h1>${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("<html>", "<html><head></head>").replace(
|
||||
'data-composition-id="scene"',
|
||||
`data-composition-id="${id}"`,
|
||||
),
|
||||
"scene.html": SCENE.replace("<h1>Scene</h1>", `<style>${rules}</style><h1>Scene</h1>`),
|
||||
});
|
||||
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("<html>", "<html><head></head>"),
|
||||
"scene.html": SCENE.replace("<h1>Scene</h1>", `<style>${sheet}</style><h1>Scene</h1>`),
|
||||
});
|
||||
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(
|
||||
"<h1>Scene</h1>",
|
||||
`<style>h1{color:red}.a,.b{margin:0}</style><h1>Scene</h1><script>window.ready = true;</script>`,
|
||||
);
|
||||
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("<h1>Scene</h1>").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<string, string> {
|
||||
const nested = `${"<template>".repeat(depth)}<div data-composition-src="level-0.html"></div>${"</template>".repeat(depth)}`;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"unpacked": 25500000
|
||||
},
|
||||
"@hyperframes/core": {
|
||||
"packed": 1820000,
|
||||
"packed": 1830000,
|
||||
"unpacked": 4639000
|
||||
},
|
||||
"@hyperframes/engine": {
|
||||
|
||||
Reference in New Issue
Block a user