mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(compiler): bound composition variable styles and variable media sources (#8144)
* fix(compiler): bound composition variable styles and variable media sources Charge every compile-time composition-variable rule against a size budget and stop emitting once it is exceeded. The render compiler shares one CompositionExpansionBudget between mounts, variable styles and variable media sources; other callers get a 32 MiB core default. Host data-variable-values are now parsed once per host instead of once per declarer. Bound media sources over 8 KiB or holding control characters are refused, and every src written is charged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * fix(producer): trim bound media sources and name the variable budgets in errors Bind the trimmed value, as a browser does, so a trailing newline no longer makes compile-time probing disagree with the runtime; refuse only interior control characters or values over 8 KiB, and log the refusal as a warning. The generated-bytes error now says which of variable styles or variable media sources overran. Adds a with-mount test for the after-inline budget. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * fix(producer): trim comments added to htmlCompiler to keep its comment share down Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
cfea8cfff5
commit
c6e3f30bea
@@ -4306,3 +4306,32 @@ describe("emitRootCompositionVariableStyles — authored definitions", () => {
|
||||
expect(css).not.toContain("--accent-dark");
|
||||
});
|
||||
});
|
||||
|
||||
describe("emitRootCompositionVariableStyles — size budget", () => {
|
||||
const DECLARER = `<div data-composition-variables='[{"id":"v","type":"string","default":"d"}]'></div>`;
|
||||
const hostWith = (declarers: number, value: string) =>
|
||||
`<!doctype html><html><head></head><body>` +
|
||||
`<div data-variable-values='${JSON.stringify({ v: value })}'>${DECLARER.repeat(declarers)}</div>` +
|
||||
`</body></html>`;
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1602,13 +1602,15 @@ export function emitRootCompositionVariableStyles(
|
||||
document: Document,
|
||||
variablesByComp: Record<string, Record<string, unknown>> = {},
|
||||
overrides: Record<string, unknown> = {},
|
||||
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<string, unknown>,
|
||||
hostValues: Record<string, unknown>,
|
||||
@@ -1682,6 +1710,7 @@ function hostScopedVariableRules(
|
||||
variablesByComp: Record<string, Record<string, unknown>>,
|
||||
overrides: Record<string, unknown>,
|
||||
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<Element, Record<string, unknown>>();
|
||||
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<string, unknown> = {};
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 = `<!doctype html>
|
||||
@@ -249,3 +249,73 @@ describe("composition expansion budget", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("variable-driven composition styles and media sources", () => {
|
||||
const withBody = (body: string) =>
|
||||
ROOT.replace("<html>", "<html><head></head>").replace("</body>", `${body}</body>`);
|
||||
|
||||
it("refuses declarers that each repeat a long host value, with no sub-compositions to mount", async () => {
|
||||
const declarer = `<div data-composition-variables='[{"id":"v","type":"string","default":"d"}]'></div>`;
|
||||
const host = `<div data-variable-values='${JSON.stringify({ v: "x".repeat(1024 * 1024) })}'>${declarer.repeat(100)}</div>`;
|
||||
const dir = project({
|
||||
"index.html": withBody(host).replace(/<div data-composition-id="scene"[^>]*><\/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 = `<div data-composition-variables='[{"id":"v","type":"string","default":"d"}]'></div>`;
|
||||
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("<h1>Scene</h1>", 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 = `<video data-var-src="clip"></video>`.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(`<video id="v1" data-var-src="clip" src="fallback.mp4"></video>`),
|
||||
"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");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1190,6 +1190,7 @@ function inlineSubCompositions(
|
||||
variableOverrides: Record<string, unknown>,
|
||||
assetRoot: string | undefined,
|
||||
outsideFiles: OutsideFilePolicy,
|
||||
budget: CompositionExpansionBudget,
|
||||
): { html: string; variablesByComp: Record<string, Record<string, unknown>> } {
|
||||
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<string, string | null>();
|
||||
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, unknown>): string {
|
||||
function bindVariableMediaSources(
|
||||
html: string,
|
||||
variables: Record<string, unknown>,
|
||||
budget: CompositionExpansionBudget,
|
||||
log?: ProducerLogger,
|
||||
): string {
|
||||
if (!html.includes("data-var-src")) return html;
|
||||
const { document } = parseHTML(html);
|
||||
const declared: Record<string, unknown> = {};
|
||||
@@ -2276,8 +2287,17 @@ function bindVariableMediaSources(html: string, variables: Record<string, unknow
|
||||
let changed = false;
|
||||
for (const el of document.querySelectorAll("video[data-var-src], audio[data-var-src]")) {
|
||||
if (el.closest("[data-variable-values]")) continue;
|
||||
const src = boundMediaSrc(el, values);
|
||||
if (src === null || el.getAttribute("src") === src) continue;
|
||||
const bound = boundMediaSrc(el, values);
|
||||
if (bound === null) continue;
|
||||
const src = bound.trim();
|
||||
if (src.length > 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
|
||||
|
||||
Reference in New Issue
Block a user