fix(compiler): rebase composition asset paths from the file read and charge what they add (#8166)

* test(producer): a deep or query-stretched composition folder stays within the size limit

* fix(compiler): rebase composition asset paths from the file read and charge what they add

A mount's references now rebase from the project file the render read for
it, so a query or fragment in data-composition-src no longer lengthens every
rebased path. Every byte a rebased reference adds, including the entry file's
own rebase, is charged to the render's expansion budget as it is produced.

* test(producer): close the font embedding block after the rebase
This commit is contained in:
Miguel Ángel
2026-11-18 09:50:35 +00:00
committed by GitHub
parent 420f14d155
commit bb55c1ea7e
6 changed files with 140 additions and 15 deletions
@@ -216,6 +216,9 @@ export interface InlineSubCompositionsOptions {
*/
resolveHtml: (srcPath: string) => string | null;
/** The project-relative file `resolveHtml` read for a src; asset paths rebase from its folder. */
resolveFilePath?: (srcPath: string) => string | null;
/**
* Parse an HTML string into a Document. The returned object must expose
* standard DOM APIs (querySelector, querySelectorAll, body, head, etc.).
@@ -393,6 +396,7 @@ export function inlineSubCompositions(
): InlineSubCompositionsResult {
const {
resolveHtml,
resolveFilePath,
parseHtml,
hostIdentityMap,
rewriteInlineStyles = false,
@@ -564,11 +568,14 @@ export function inlineSubCompositions(
warnUnknownEnumValues(innerRoot, mergedVariables, variablesCompId);
}
const filePath = resolveFilePath?.(src) ?? src;
const chargeRebase = chargeEmitted && ((bytes: number) => chargeEmitted(src, bytes));
// `<head>` <link>/<script src> are hoisted into the ROOT document, so they
// need the same directory rewrite the body's [src]/[href] pass applies —
// without it even the documented `../` form escapes the project and 404s.
const resolveSubAssetPath = (raw: string | null): string =>
rewriteAssetPath(src, (raw || "").trim(), assetExists);
rewriteAssetPath(filePath, (raw || "").trim(), assetExists, chargeRebase);
// Scope one sub-composition <style> body. scopeRootSelectors keeps the
// sub-comp's html/body/:root rules from clobbering the host document (they
@@ -577,7 +584,7 @@ export function inlineSubCompositions(
const sheetKey = JSON.stringify([src, raw]);
let css = sheetsWithAssetUrls.get(sheetKey);
if (css === undefined) {
css = rewriteCssAssetUrls(raw, src, assetExists);
css = rewriteCssAssetUrls(raw, filePath, assetExists, chargeRebase);
sheetsWithAssetUrls.set(sheetKey, css);
}
if (!scopeCompId) {
@@ -695,12 +702,13 @@ export function inlineSubCompositions(
: contentDoc.querySelectorAll("[src], [href], [srcset]");
rewriteAssetPaths(
assetEls,
src,
filePath,
(el: Element, attr: string) => el.getAttribute(attr),
(el: Element, attr: string, val: string) => {
el.setAttribute(attr, val);
},
assetExists,
chargeRebase,
);
if (rewriteInlineStyles) {
@@ -709,12 +717,13 @@ export function inlineSubCompositions(
: contentDoc.querySelectorAll("[style]");
rewriteInlineStyleAssetUrls(
styledEls,
src,
filePath,
(el: Element) => el.getAttribute("style"),
(el: Element, val: string) => {
el.setAttribute("style", val);
},
assetExists,
chargeRebase,
);
}
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import {
rewriteAssetPath,
rewriteAssetPaths,
rewriteCssAssetUrls,
rewriteInlineStyleAssetUrls,
} from "./rewriteSubCompPaths.js";
@@ -130,3 +131,35 @@ describe("rewriteAssetPath", () => {
});
});
});
describe("rebasing charge", () => {
it("reports what each rebased reference adds, for every rewrite path", () => {
const added: number[] = [];
const charge = (bytes: number) => added.push(bytes);
rewriteAssetPath("scenes/a/scene.html", "../logo.png", undefined, charge);
rewriteCssAssetUrls(
"a { background: url(../bg.png) }",
"scenes/a/scene.html",
undefined,
charge,
);
const el = { srcset: "../x.png 1x, ../y.png 2x", style: "background: url(../s.png)" };
rewriteAssetPaths(
[el],
"scenes/a/scene.html",
(e, attr) => (attr === "srcset" ? e.srcset : null),
() => {},
undefined,
charge,
);
rewriteInlineStyleAssetUrls(
[el],
"scenes/a/scene.html",
(e) => e.style,
() => {},
undefined,
charge,
);
expect(added).toEqual([4, 4, 4, 4, 4]);
});
});
+14 -5
View File
@@ -30,6 +30,9 @@ import {
const isAbsoluteOrSpecial = isNonRelativeUrl;
/** Gets the length a rebased path added over the authored one. */
export type ChargeRebase = (addedBytes: number) => void;
/**
* Returns true only for paths that traverse up with `../`.
* Plain relative paths like `assets/foo.svg` are ambiguous: two conventions are
@@ -74,6 +77,7 @@ export function rewriteAssetPath(
compSrcPath: string,
relativePath: string,
assetExists?: AssetExists,
charge?: ChargeRebase,
): string {
if (isAbsoluteOrSpecial(relativePath)) return relativePath;
const compDir = dirname(compSrcPath);
@@ -84,7 +88,9 @@ export function rewriteAssetPath(
const filePath = decodeWellFormedEscapes(slashed);
const sibling = resolve("/", join(compDir, filePath)).slice(1);
if (!needsRewrite(filePath) && (!assetExists || !assetExists(sibling))) return relativePath;
return encodeUrlPath(sibling) + suffix;
const rebased = encodeUrlPath(sibling) + suffix;
charge?.(rebased.length - relativePath.length);
return rebased;
}
/**
@@ -103,11 +109,12 @@ export function rewriteAssetPaths<T>(
getAttr: (el: T, attr: string) => string | null | undefined,
setAttr: (el: T, attr: string, value: string) => void,
assetExists?: AssetExists,
charge?: ChargeRebase,
): void {
for (const el of elements) {
for (const attr of PATH_ATTRS) {
const val = (getAttr(el, attr) || "").trim();
const rewritten = rewriteAssetPath(compSrcPath, val, assetExists);
const rewritten = rewriteAssetPath(compSrcPath, val, assetExists, charge);
if (rewritten !== val) {
setAttr(el, attr, rewritten);
}
@@ -115,7 +122,7 @@ export function rewriteAssetPaths<T>(
const srcset = getAttr(el, "srcset");
if (!srcset) continue;
const rewritten = rewriteSrcsetUrls(srcset, (url) =>
rewriteAssetPath(compSrcPath, url, assetExists),
rewriteAssetPath(compSrcPath, url, assetExists, charge),
);
if (rewritten !== srcset) setAttr(el, "srcset", rewritten);
}
@@ -130,6 +137,7 @@ export function rewriteInlineStyleAssetUrls<T>(
getStyle: (el: T) => string | null | undefined,
setStyle: (el: T, value: string) => void,
assetExists?: AssetExists,
charge?: ChargeRebase,
): void {
const compDir = dirname(compSrcPath);
if (!compDir || compDir === ".") return;
@@ -137,7 +145,7 @@ export function rewriteInlineStyleAssetUrls<T>(
for (const el of elements) {
const style = getStyle(el);
if (!style) continue;
const rewritten = rewriteCssAssetUrls(style, compSrcPath, assetExists);
const rewritten = rewriteCssAssetUrls(style, compSrcPath, assetExists, charge);
if (rewritten !== style) {
setStyle(el, rewritten);
}
@@ -152,11 +160,12 @@ export function rewriteCssAssetUrls(
cssText: string,
compSrcPath: string,
assetExists?: AssetExists,
charge?: ChargeRebase,
): string {
if (!cssText) return cssText;
return replaceMatches(cssText, cssUrlMatches(cssText), (full, quote, rawUrl) => {
const urlValue = (rawUrl || "").trim();
const rewritten = rewriteAssetPath(compSrcPath, urlValue, assetExists);
const rewritten = rewriteAssetPath(compSrcPath, urlValue, assetExists, charge);
if (rewritten === urlValue) return full;
return `url(${quote || ""}${rewritten}${quote || ""})`;
});
@@ -63,7 +63,7 @@ function generatedTooLarge(file: string, bytes: number): string {
);
}
return (
`Scripts and styles generated for sub-composition mounts, ending at "${file}", add up to ` +
`Scripts, styles and rebased asset paths generated for compositions, 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."
@@ -1,6 +1,6 @@
import { mkdtempSync, writeFileSync } from "node:fs";
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { dirname, join } from "node:path";
import { parseHTML } from "linkedom";
import { describe, expect, it, vi } from "vitest";
import {
@@ -345,3 +345,55 @@ describe("local font embedding", () => {
expect(html).toContain('.mark { background: url("big.ttf"); }');
});
});
describe("asset paths rebased from a composition's folder", () => {
const candidates = (count: number) => Array(count).fill("../x.png").join(", ");
const scene = (count: number) =>
`<template><div data-composition-id="scene"><img srcset="${candidates(count)}"></div></template>`;
const deepFolder = Array.from({ length: 4 }, (_, i) => `${i}`.repeat(200)).join("/");
function projectWith(files: Record<string, string>): string {
const dir = mkdtempSync(join(tmpdir(), "hf-rebase-"));
for (const [name, text] of Object.entries(files)) {
mkdirSync(dirname(join(dir, name)), { recursive: true });
writeFileSync(join(dir, name), text);
}
return dir;
}
it("rebases from the mounted file, not a query in its src", async () => {
const src = `scenes/a/scene.html?${"q/".repeat(20_000)}`;
const dir = projectWith({
"index.html": ROOT.replace(
'data-composition-src="scene.html"',
`data-composition-src="${src}"`,
),
"scenes/a/scene.html": scene(200),
});
const { html } = await compileForRender(dir, join(dir, "index.html"), dir);
expect(html).toContain(`srcset="scenes/x.png, scenes/x.png`);
});
it("refuses a mount in a deep folder whose rebased references add up past the limit", async () => {
const dir = projectWith({
"index.html": ROOT.replace("scene.html", `${deepFolder}/scene.html`),
[`${deepFolder}/scene.html`]: scene(60_000),
});
const error = await compileError(dir);
expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE");
});
it("refuses an entry file in a deep folder whose rebased references add up past the limit", async () => {
const dir = projectWith({
[`${deepFolder}/index.html`]: `<!doctype html><html><body>
<div data-composition-id="root" data-width="1920" data-height="1080" data-duration="2">
<img srcset="${candidates(60_000)}">
</div>
</body></html>`,
});
const error = await compileForRender(dir, join(dir, deepFolder, "index.html"), dir).catch(
(e: unknown) => e,
);
expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE");
});
});
+25 -3
View File
@@ -1263,6 +1263,10 @@ function inlineSubCompositions(
hostIdentityMap: hostIdentityByElement,
readVariableDefaults: readDeclaredDefaults,
parseHostVariables: parseHostVariableValues,
resolveFilePath: (srcPath: string) => {
const file = resolveCompositionFile(projectDir, srcPath, assetRoot);
return file && relative(projectDir, file).split(sep).join("/");
},
resolveHtml: (srcPath: string) => {
const compHtml = resolveMountHtml(srcPath);
if (compHtml) budget.charge(srcPath, Buffer.byteLength(compHtml));
@@ -2253,19 +2257,26 @@ function rewriteUnresolvableGsapToCdn(
* it to compiled/index.html. Use the same sibling-first/project-root-fallback
* rule as mounted sub-compositions so both supported entry modes agree.
*/
function rebaseDirectEntryAssetPaths(html: string, projectDir: string, htmlPath: string): string {
function rebaseDirectEntryAssetPaths(
html: string,
projectDir: string,
htmlPath: string,
budget: CompositionExpansionBudget,
): string {
if (!isPathInside(htmlPath, projectDir)) return html;
const entryPath = relative(projectDir, htmlPath).replace(/\\/g, "/");
if (!entryPath.includes("/")) return html;
const { document } = parseHTML(html);
const assetExists = projectAssetExists(projectDir);
const charge = (bytes: number) => budget.chargeGenerated(entryPath, bytes);
rewriteAssetPaths(
document.querySelectorAll("[src], [href], [srcset]"),
entryPath,
(el: Element, attr: string) => el.getAttribute(attr),
(el: Element, attr: string, value: string) => el.setAttribute(attr, value),
assetExists,
charge,
);
rewriteInlineStyleAssetUrls(
document.querySelectorAll("[style]"),
@@ -2273,9 +2284,15 @@ function rebaseDirectEntryAssetPaths(html: string, projectDir: string, htmlPath:
(el: Element) => el.getAttribute("style"),
(el: Element, value: string) => el.setAttribute("style", value),
assetExists,
charge,
);
for (const style of document.querySelectorAll("style")) {
style.textContent = rewriteCssAssetUrls(style.textContent || "", entryPath, assetExists);
style.textContent = rewriteCssAssetUrls(
style.textContent || "",
entryPath,
assetExists,
charge,
);
}
return document.toString();
}
@@ -2352,8 +2369,13 @@ export async function compileForRender(
throw new Error(`Entry file ${htmlPath} is not an .html file, so it is refused`);
}
assertCompositionFileSize(htmlPath, basename(htmlPath));
const entryHtml = rebaseDirectEntryAssetPaths(readTextFile(htmlPath), projectDir, htmlPath);
const budget = new CompositionExpansionBudget();
const entryHtml = rebaseDirectEntryAssetPaths(
readTextFile(htmlPath),
projectDir,
htmlPath,
budget,
);
const rawHtml = bindVariableMediaSources(
normalizeCompositionSrcs(rewriteUnresolvableGsapToCdn(entryHtml, projectDir), projectDir),
options.variables ?? {},