diff --git a/packages/producer/src/services/render/stages/probeStage.test.ts b/packages/producer/src/services/render/stages/probeStage.test.ts
index d347b48054..49320cd339 100644
--- a/packages/producer/src/services/render/stages/probeStage.test.ts
+++ b/packages/producer/src/services/render/stages/probeStage.test.ts
@@ -1,4 +1,4 @@
-import { afterAll, describe, expect, it, mock } from "bun:test";
+import { afterAll, describe, expect, it, mock, spyOn } from "bun:test";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -14,8 +14,10 @@ import { collectRenderMedia } from "../../renderMediaCollector.js";
const workDir = mkdtempSync(join(tmpdir(), "hf-stage-test-"));
afterAll(() => rmSync(workDir, { recursive: true, force: true }));
import { createHash } from "node:crypto";
+import { parseHTML } from "linkedom";
import {
hasAutoStartVideos,
+ hasRuntimeMediaChanges,
hasScriptedAudioVolumeAutomation,
hasVariableBoundMedia,
reportedLengthSource,
@@ -445,6 +447,136 @@ describe("hasVariableBoundMedia", () => {
});
});
+describe("hasVariableBoundMedia — page size", () => {
+ it("parses a large variables attribute once, not once per bound element", () => {
+ const values = JSON.stringify(
+ Object.fromEntries(Array.from({ length: 1_000 }, (_, i) => [`k${i}`, "v"])),
+ );
+ const images = `
`.repeat(2_000);
+ const html = `
${images}
`;
+ const parse = spyOn(JSON, "parse");
+ try {
+ expect(hasVariableBoundMedia(html, undefined)).toBe(false);
+ expect(parse.mock.calls.length).toBeLessThanOrEqual(1);
+ } finally {
+ parse.mockRestore();
+ }
+ });
+
+ it("takes the conservative answer when the ancestor walks run past their budget", () => {
+ const depth = 600;
+ const images = `
`.repeat(2_000);
+ const html = `${"".repeat(depth)}${images}${"
".repeat(depth)}`;
+
+ expect(hasVariableBoundMedia(html, undefined)).toBe(true);
+ });
+});
+
+describe("hasRuntimeMediaChanges", () => {
+ const page = (body: string, script: string) =>
+ `${body}`;
+
+ it("finds a source write aimed at a video by id or by selector", () => {
+ const video = ``;
+ expect(
+ hasRuntimeMediaChanges(page(video, `document.getElementById("clip").src = "b.mp4";`)),
+ ).toBe(true);
+ expect(
+ hasRuntimeMediaChanges(page(video, `document.querySelector(".scene video").src = "b.mp4";`)),
+ ).toBe(true);
+ expect(
+ hasRuntimeMediaChanges(
+ page(
+ ``,
+ `document.getElementById("s").src = "b";`,
+ ),
+ ),
+ ).toBe(true);
+ });
+
+ it("ignores source writes aimed at other elements", () => {
+ const body = `
`;
+ const script = `document.getElementById("poster").src = "b.png";
+ document.querySelector(".still").setAttribute("src", "c.png");
+ document.querySelector("[[bad").src = "d.png";`;
+ expect(hasRuntimeMediaChanges(page(body, script))).toBe(false);
+ expect(
+ hasRuntimeMediaChanges(page(`
`, `document.getElementById("clip").src = "b";`)),
+ ).toBe(false);
+ });
+
+ it("answers id writes from one index, with no document lookup per write", () => {
+ const count = 2_000;
+ const divs = Array.from({ length: count }, (_, i) => ``);
+ const writes = Array.from(
+ { length: count },
+ (_, i) => `document.getElementById("n${i}").src = "x";`,
+ );
+ const html = page(`${divs.join("")}`, writes.join("\n"));
+ const lookups = documentLookupSpies();
+ try {
+ expect(hasRuntimeMediaChanges(html)).toBe(false);
+ expect(lookups.getElementById.mock.calls.length).toBe(0);
+ expect(lookups.querySelectorAll.mock.calls.length).toBeLessThanOrEqual(1);
+ } finally {
+ lookups.restore();
+ }
+ });
+
+ it("finds a source write on a page written without the html, head or body tags", () => {
+ const html =
+ `` +
+ ``;
+ expect(hasRuntimeMediaChanges(html)).toBe(true);
+ });
+
+ it("lets the browser probe decide when there are too many selectors to check", () => {
+ const videos = ``.repeat(50);
+ const writes = Array.from(
+ { length: 8_000 },
+ (_, i) => `document.querySelector(".q${i}").src = "x";`,
+ );
+ expect(hasRuntimeMediaChanges(page(videos, writes.join("\n")))).toBe(true);
+ });
+
+ it("lets the browser probe decide a long selector against many media elements", () => {
+ const videos = ``.repeat(100);
+ const selector = `${".nope ".repeat(2_000)}video`;
+ const script = `document.querySelector(${JSON.stringify(selector)}).src = "x";`;
+ expect(hasRuntimeMediaChanges(page(videos, script))).toBe(true);
+ expect(hasRuntimeMediaChanges(page(``, script))).toBe(false);
+ });
+
+ it("lets the browser probe decide a selector with a sibling combinator", () => {
+ const script = `document.querySelector(".nope ~ .none").src = "x";`;
+ expect(hasRuntimeMediaChanges(page(``, script))).toBe(true);
+ });
+});
+
+/** Spies on the linkedom document's own lookups, wherever on its prototype chain they live. */
+function documentLookupSpies() {
+ const owner = (name: string): object => {
+ for (
+ let proto = Object.getPrototypeOf(parseHTML("").document);
+ proto;
+ proto = Object.getPrototypeOf(proto)
+ ) {
+ if (Object.hasOwn(proto, name)) return proto;
+ }
+ throw new Error(`no ${name} on the document`);
+ };
+ const getElementById = spyOn(owner("getElementById") as Document, "getElementById");
+ const querySelectorAll = spyOn(owner("querySelectorAll") as Document, "querySelectorAll");
+ return {
+ getElementById,
+ querySelectorAll,
+ restore() {
+ getElementById.mockRestore();
+ querySelectorAll.mockRestore();
+ },
+ };
+}
+
describe("runProbeStage — forceScreenshot threading", () => {
it("runs media-type preflight after the browser-reconciliation phase", async () => {
mediaPreflightCallCount = 0;
diff --git a/packages/producer/src/services/render/stages/probeStage.ts b/packages/producer/src/services/render/stages/probeStage.ts
index 0d909fcda8..9a0e5bdc18 100644
--- a/packages/producer/src/services/render/stages/probeStage.ts
+++ b/packages/producer/src/services/render/stages/probeStage.ts
@@ -189,26 +189,58 @@ export function hasAutoStartVideos(html: string): boolean {
type VariablesByComp = Record>;
-function scopedVariablesFor(element: Element, variablesByComp: VariablesByComp) {
- const scope = element.closest("[data-composition-id]");
- const compId = scope?.getAttribute("data-composition-id")?.trim() ?? "";
- return compId && Object.hasOwn(variablesByComp, compId) ? variablesByComp[compId] : undefined;
+/** Ancestor steps one variable-media scan may take before it gives the conservative answer. */
+const MAX_VARIABLE_SCOPE_STEPS = 1_000_000;
+
+interface VariableScopeScan {
+ readonly ids: Map>;
+ steps: number;
}
+function variableIdsOn(scan: VariableScopeScan, element: Element): ReadonlySet {
+ let ids = scan.ids.get(element);
+ if (!ids) {
+ ids = new Set([
+ ...Object.keys(parseHostVariableValues(element)),
+ ...Object.keys(readDeclaredDefaults(element)),
+ ]);
+ scan.ids.set(element, ids);
+ }
+ return ids;
+}
+
+/** The compiled table of the element's composition scope; null once the scan is out of steps. */
+function scopedVariablesFor(
+ scan: VariableScopeScan,
+ element: Element,
+ variablesByComp: VariablesByComp,
+): Record | undefined | null {
+ for (let node: Element | null = element; node; node = node.parentElement) {
+ if (++scan.steps > MAX_VARIABLE_SCOPE_STEPS) return null;
+ if (!node.hasAttribute("data-composition-id")) continue;
+ const compId = node.getAttribute("data-composition-id")?.trim() ?? "";
+ return compId && Object.hasOwn(variablesByComp, compId) ? variablesByComp[compId] : undefined;
+ }
+ return undefined;
+}
+
+/** True when the variable has a value, or when the scan ran out of steps and cannot rule one out. */
function variableHasValue(
+ scan: VariableScopeScan,
element: Element,
variableId: string,
variables: Record | undefined,
variablesByComp: VariablesByComp,
): boolean {
if (variables && Object.hasOwn(variables, variableId)) return true;
- const scoped = scopedVariablesFor(element, variablesByComp);
+ const scoped = scopedVariablesFor(scan, element, variablesByComp);
+ if (scoped === null) return true;
if (scoped) return Object.hasOwn(scoped, variableId);
for (let scope = element.parentElement; scope; scope = scope.parentElement) {
- if (Object.hasOwn(parseHostVariableValues(scope), variableId)) return true;
- if (Object.hasOwn(readDeclaredDefaults(scope), variableId)) return true;
+ if (++scan.steps > MAX_VARIABLE_SCOPE_STEPS) return true;
+ if (variableIdsOn(scan, scope).has(variableId)) return true;
}
- return Object.hasOwn(readDeclaredDefaults(element.ownerDocument.documentElement), variableId);
+ return variableIdsOn(scan, element.ownerDocument.documentElement).has(variableId);
}
/**
@@ -221,13 +253,16 @@ export function hasVariableBoundMedia(
variablesByComp: VariablesByComp = {},
): boolean {
const { document } = parseHTML(html);
+ const scan: VariableScopeScan = { ids: new Map(), steps: 0 };
return Array.from(
document.querySelectorAll(
"img[data-var-src], audio[data-var-src], video[data-var-src], source[data-var-src]",
),
).some((element) => {
const variableId = element.getAttribute("data-var-src")?.trim();
- return Boolean(variableId && variableHasValue(element, variableId, variables, variablesByComp));
+ return Boolean(
+ variableId && variableHasValue(scan, element, variableId, variables, variablesByComp),
+ );
});
}
@@ -289,13 +324,33 @@ function reconcileBrowserMediaEnd(
return existingEnd <= 0 ? projectedEnd : Math.min(existingEnd, projectedEnd);
}
+/** Selector characters times media elements matched statically before the browser probe decides. */
+const MAX_MEDIA_SELECTOR_WORK = 1_000_000;
+// Sibling combinators can make one match walk the page; the browser probe answers those.
+const SIBLING_COMBINATOR = /[~+]/;
+
+function managedMediaElements(document: Document): Element[] {
+ const managed: Element[] = [];
+ const pending: Array<{ element: Element; inMedia: boolean }> = [];
+ for (const element of Array.from(document.children)) pending.push({ element, inMedia: false });
+ for (let next = pending.pop(); next; next = pending.pop()) {
+ const name = next.element.tagName.toLowerCase();
+ const isMedia = name === "video" || name === "audio";
+ if (isMedia || (next.inMedia && name === "source")) managed.push(next.element);
+ for (const child of Array.from(next.element.children)) {
+ pending.push({ element: child, inMedia: next.inMedia || isMedia });
+ }
+ }
+ return managed;
+}
+
/**
* Runtime-created media does not exist when the static compiler scans the HTML.
* Launch a browser probe so discoverMediaFromBrowser can reconcile it before
* extraction, even when the root duration is already known. External script
* sources have no inline text to inspect and remain a known heuristic gap.
*/
-function hasRuntimeMediaChanges(html: string): boolean {
+export function hasRuntimeMediaChanges(html: string): boolean {
const { document } = parseHTML(html);
const scriptBodies = [...document.querySelectorAll("script")].map(
(script) => script.textContent ?? "",
@@ -308,25 +363,30 @@ function hasRuntimeMediaChanges(html: string): boolean {
);
if (insertedMedia) return true;
- const isManagedMedia = (element: Element): boolean => {
- const name = element.tagName.toLowerCase();
- if (name === "video" || name === "audio") return true;
- return name === "source" && element.closest("video, audio") !== null;
- };
+ const managed = managedMediaElements(document);
+ if (managed.length === 0) return false;
+ const managedIds = new Set(managed.map((element) => element.getAttribute("id")).filter(Boolean));
+ const selectors = new Set();
+ let selectorChars = 0;
for (const script of scriptBodies) {
for (const mutation of extractMediaSrcMutations(script)) {
- try {
- const id = /^#[A-Za-z_][\w-]*$/.test(mutation.selector) ? mutation.selector.slice(1) : null;
- const idTarget = id ? document.getElementById(id) : null;
- const targets = id
- ? idTarget
- ? [idTarget]
- : []
- : [...document.querySelectorAll(mutation.selector)];
- if (targets.some(isManagedMedia)) return true;
- } catch {
- // Invalid selectors are diagnosed by lint and cannot prove a media target here.
+ const id = /^#[A-Za-z_][\w-]*$/.test(mutation.selector) ? mutation.selector.slice(1) : null;
+ if (id) {
+ if (managedIds.has(id)) return true;
+ continue;
}
+ if (selectors.has(mutation.selector)) continue;
+ if (SIBLING_COMBINATOR.test(mutation.selector)) return true;
+ selectors.add(mutation.selector);
+ selectorChars += mutation.selector.length;
+ if (selectorChars * managed.length > MAX_MEDIA_SELECTOR_WORK) return true;
+ }
+ }
+ for (const selector of selectors) {
+ try {
+ if (managed.some((element) => element.matches(selector))) return true;
+ } catch {
+ // Invalid selectors are diagnosed by lint and cannot prove a media target here.
}
}
return false;