Skip to content

Commit 5c44cd2

Browse files
fix(render): probe runtime media source mutations (#3729)
1 parent 9308ead commit 5c44cd2

8 files changed

Lines changed: 335 additions & 16 deletions

File tree

‎packages/lint/src/rules/media.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,62 @@ describe("media rules", () => {
379379
expect(finding).toBeUndefined();
380380
});
381381

382+
it("warns on raw src mutations targeting existing video and audio", async () => {
383+
const html = `
384+
<html><body>
385+
<div data-composition-id="c1" data-width="1920" data-height="1080">
386+
<video id="clip" src="video-a.mp4"></video>
387+
<audio id="voice" src="audio-a.wav"></audio>
388+
</div>
389+
<script>
390+
const clip = document.getElementById("clip");
391+
clip.src = "video-b.mp4";
392+
document.querySelector("#voice").setAttribute("src", "audio-b.wav");
393+
</script>
394+
</body></html>`;
395+
396+
const result = await lintHyperframeHtml(html);
397+
const findings = result.findings.filter(
398+
(finding) => finding.code === "media_runtime_src_mutation",
399+
);
400+
expect(findings).toHaveLength(2);
401+
expect(findings.map((finding) => finding.elementId).sort()).toEqual(["clip", "voice"]);
402+
expect(findings.every((finding) => finding.fixHint?.includes("data-var-src"))).toBe(true);
403+
});
404+
405+
it("does not flag img or script src mutations", async () => {
406+
const html = `
407+
<html><body>
408+
<div data-composition-id="c1" data-width="1920" data-height="1080">
409+
<img id="poster" src="a.png"><script id="loader"></script>
410+
</div>
411+
<script>
412+
document.getElementById("poster").src = "b.png";
413+
document.getElementById("loader").setAttribute("src", "loader-b.js");
414+
</script>
415+
</body></html>`;
416+
417+
const result = await lintHyperframeHtml(html);
418+
expect(
419+
result.findings.find((finding) => finding.code === "media_runtime_src_mutation"),
420+
).toBeUndefined();
421+
});
422+
423+
it("warns when a source child of existing media is mutated", async () => {
424+
const html = `
425+
<html><body>
426+
<div data-composition-id="c1" data-width="1920" data-height="1080">
427+
<video id="clip"><source id="clip-source" src="video-a.mp4"></video>
428+
</div>
429+
<script>document.getElementById("clip-source").src = "video-b.mp4";</script>
430+
</body></html>`;
431+
432+
const result = await lintHyperframeHtml(html);
433+
expect(
434+
result.findings.find((finding) => finding.code === "media_runtime_src_mutation"),
435+
).toMatchObject({ severity: "warning", elementId: "clip" });
436+
});
437+
382438
it("does not flag <video> inside a sub-composition (runtime drives nested media)", async () => {
383439
// The runtime's global media sweep (querySelectorAll("video, audio")) drives
384440
// media at any nesting depth, and startResolver re-bases each nested clip's

‎packages/lint/src/rules/media.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { LintContext, HyperframeLintFinding, OpenTag } from "../context";
22
import { readAttr, readDecodedAttr, stripJsComments, truncateSnippet, isMediaTag } from "../utils";
33
import { validateColorGradingContract } from "@hyperframes/parsers/color-grading-contract";
4+
import { extractMediaSrcMutations } from "@hyperframes/parsers";
5+
import { parseHTML } from "linkedom";
46

57
/**
68
* Does the GSAP call that names `#id` also set `volume` in the same call?
@@ -333,6 +335,46 @@ function findImperativeMediaControlFindings(ctx: LintContext): HyperframeLintFin
333335
return findings;
334336
}
335337

338+
function findRuntimeMediaSrcMutationFindings(ctx: LintContext): HyperframeLintFinding[] {
339+
const { document } = parseHTML(ctx.source);
340+
const findings: HyperframeLintFinding[] = [];
341+
for (const script of ctx.scripts) {
342+
for (const mutation of extractMediaSrcMutations(script.content)) {
343+
let targets: Element[];
344+
try {
345+
const id = /^#[A-Za-z_][\w-]*$/.test(mutation.selector) ? mutation.selector.slice(1) : null;
346+
const idTarget = id ? document.getElementById(id) : null;
347+
targets = id
348+
? idTarget
349+
? [idTarget]
350+
: []
351+
: [...document.querySelectorAll(mutation.selector)];
352+
} catch {
353+
continue;
354+
}
355+
const mediaTargets = targets
356+
.map((element) => {
357+
const name = element.tagName.toLowerCase();
358+
if (name === "video" || name === "audio") return element;
359+
return name === "source" ? element.closest("video, audio") : null;
360+
})
361+
.filter((element): element is Element => element !== null);
362+
if (mediaTargets.length === 0) continue;
363+
findings.push({
364+
code: "media_runtime_src_mutation",
365+
severity: "warning",
366+
message: `Inline script mutates the source of existing managed media via ${mutation.operation === "src_assignment" ? ".src assignment" : "setAttribute('src', ...)"}. Browser probing can reconcile synchronous writes, but external or delayed writes can still diverge between preview and extraction.`,
367+
elementId: mediaTargets[0]?.getAttribute("id") || undefined,
368+
selector: mutation.selector,
369+
fixHint:
370+
"Author the final static src, or bind data-var-src to a declared image/string variable so the selected source is applied before media discovery and extraction.",
371+
snippet: truncateSnippet(mutation.raw),
372+
});
373+
}
374+
}
375+
return findings;
376+
}
377+
336378
export const mediaRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
337379
// duplicate_media_id + duplicate_media_discovery_risk
338380
({ tags }) => {
@@ -738,6 +780,7 @@ export const mediaRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> =
738780

739781
// imperative_media_control
740782
findImperativeMediaControlFindings,
783+
findRuntimeMediaSrcMutationFindings,
741784

742785
// audio_volume_double_automation
743786
findVolumeDoubleAutomationFindings,

‎packages/parsers/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export * from "./compositionContract.js";
1414
// browser-safe.
1515
export { decodeUrlPathVariants } from "./utils/urlPath.js";
1616
export { scanVariableUsage, type VariableUsageScan } from "./variableUsage.js";
17+
export { extractMediaSrcMutations, type MediaSrcMutation } from "./mediaSrcMutation.js";
1718
export {
1819
FONT_ALIAS_MAP,
1920
FONT_ALIAS_KEYS,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import * as acorn from "acorn";
2+
import * as acornWalk from "acorn-walk";
3+
4+
export interface MediaSrcMutation {
5+
selector: string;
6+
operation: "src_assignment" | "set_attribute";
7+
raw: string;
8+
}
9+
10+
function parseProgram(script: string): any {
11+
try {
12+
return acorn.parse(script, { ecmaVersion: "latest", sourceType: "script" });
13+
} catch {
14+
return acorn.parse(script, { ecmaVersion: "latest", sourceType: "module" });
15+
}
16+
}
17+
18+
function literalString(node: any): string | undefined {
19+
if (node?.type === "Literal" && typeof node.value === "string") return node.value;
20+
if (node?.type === "TemplateLiteral" && node.expressions?.length === 0) {
21+
return node.quasis?.[0]?.value?.cooked;
22+
}
23+
return undefined;
24+
}
25+
26+
function memberName(node: any): string | undefined {
27+
if (node?.type !== "MemberExpression") return undefined;
28+
if (!node.computed && node.property?.type === "Identifier") return node.property.name;
29+
return literalString(node.property);
30+
}
31+
32+
function lookupSelector(node: any, bindings: ReadonlyMap<string, string>): string | undefined {
33+
if (node?.type === "Identifier") return bindings.get(node.name);
34+
if (node?.type !== "CallExpression" || node.callee?.type !== "MemberExpression") {
35+
return undefined;
36+
}
37+
const method = memberName(node.callee);
38+
if (method !== "getElementById" && method !== "querySelector") return undefined;
39+
const value = literalString(node.arguments?.[0]);
40+
if (!value) return undefined;
41+
return method === "getElementById" ? `#${value}` : value;
42+
}
43+
44+
/** Find literal source writes whose target is a statically resolvable DOM lookup. */
45+
export function extractMediaSrcMutations(script: string): MediaSrcMutation[] {
46+
try {
47+
const ast = parseProgram(script);
48+
const bindings = new Map<string, string>();
49+
acornWalk.simple(ast, {
50+
VariableDeclarator(node: any) {
51+
if (node.id?.type !== "Identifier") return;
52+
const selector = lookupSelector(node.init, bindings);
53+
if (selector) bindings.set(node.id.name, selector);
54+
},
55+
});
56+
57+
const mutations: MediaSrcMutation[] = [];
58+
acornWalk.simple(ast, {
59+
AssignmentExpression(node: any) {
60+
if (node.operator !== "=" || memberName(node.left) !== "src") return;
61+
const selector = lookupSelector(node.left.object, bindings);
62+
if (!selector) return;
63+
mutations.push({
64+
selector,
65+
operation: "src_assignment",
66+
raw: script.slice(node.start, node.end),
67+
});
68+
},
69+
CallExpression(node: any) {
70+
if (memberName(node.callee) !== "setAttribute") return;
71+
if (literalString(node.arguments?.[0])?.toLowerCase() !== "src") return;
72+
const selector = lookupSelector(node.callee.object, bindings);
73+
if (!selector) return;
74+
mutations.push({
75+
selector,
76+
operation: "set_attribute",
77+
raw: script.slice(node.start, node.end),
78+
});
79+
},
80+
});
81+
return mutations;
82+
} catch {
83+
return [];
84+
}
85+
}

‎packages/producer/src/services/render/observability.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export interface RenderExtractionObservability {
174174
* a coarse proxy for the ts=1784144554 field signal shape (147-clip
175175
* composition, 130 word-level caption divs authored-clip-count-scaled
176176
* failure). Static scan; dynamic script-inserted timed clips land in
177-
* the probe-stage's `hasRuntimeInsertedMedia` path (PR #2474).
177+
* the probe-stage's `hasRuntimeMediaChanges` path (PR #2474).
178178
*/
179179
authoredTimedClipCount?: number;
180180
}

‎packages/producer/src/services/render/stages/probeStage.test.ts‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, it, mock } from "bun:test";
2+
import { createHash } from "node:crypto";
23
import {
34
hasAutoStartVideos,
45
hasScriptedAudioVolumeAutomation,
@@ -583,6 +584,113 @@ describe("runProbeStage — forceScreenshot threading", () => {
583584
expect(capturedCfgs.length).toBeGreaterThan(0);
584585
});
585586

587+
it("probes and reconciles synchronous src mutations on existing video and audio", async () => {
588+
resetRetryMocks();
589+
capturedCfgs.length = 0;
590+
const hash = (value: string) => createHash("sha256").update(value).digest("hex");
591+
const videoA = `assets/video-a-${hash("sanitized-video-a")}.mp4`;
592+
const videoB = `assets/video-b-${hash("sanitized-video-b")}.mp4`;
593+
const audioA = `assets/audio-a-${hash("sanitized-audio-a")}.wav`;
594+
const audioB = `assets/audio-b-${hash("sanitized-audio-b")}.wav`;
595+
expect(hash("sanitized-video-a")).not.toBe(hash("sanitized-video-b"));
596+
expect(hash("sanitized-audio-a")).not.toBe(hash("sanitized-audio-b"));
597+
browserMediaResults = [
598+
{
599+
id: "clip",
600+
tagName: "video",
601+
src: videoB,
602+
start: 0,
603+
end: 5,
604+
duration: 5,
605+
mediaStart: 0,
606+
loop: false,
607+
hasAudio: false,
608+
volume: 1,
609+
muted: true,
610+
},
611+
{
612+
id: "voice",
613+
tagName: "audio",
614+
src: audioB,
615+
start: 0,
616+
end: 5,
617+
duration: 5,
618+
mediaStart: 0,
619+
loop: false,
620+
hasAudio: true,
621+
volume: 1,
622+
muted: false,
623+
},
624+
];
625+
const { runProbeStage } = await import("./probeStage.js");
626+
const input = makeProbeInput({});
627+
input.composition.duration = 5;
628+
input.composition.videos.push({
629+
id: "clip",
630+
src: videoA,
631+
start: 0,
632+
end: 5,
633+
mediaStart: 0,
634+
loop: false,
635+
hasAudio: false,
636+
});
637+
input.composition.audios.push({
638+
id: "voice",
639+
src: audioA,
640+
start: 0,
641+
end: 5,
642+
mediaStart: 0,
643+
layer: 0,
644+
volume: 1,
645+
type: "audio",
646+
});
647+
input.compiled.html = `<video id="clip" src="${videoA}"></video>
648+
<audio id="voice" src="${audioA}"></audio>
649+
<script>
650+
const clip = document.getElementById("clip");
651+
clip.src = ${JSON.stringify(videoB)};
652+
document.querySelector("#voice").setAttribute("src", ${JSON.stringify(audioB)});
653+
</script>`;
654+
655+
await runProbeStage(input);
656+
657+
expect(capturedCfgs.length).toBeGreaterThan(0);
658+
expect(input.composition.videos[0]?.src).toBe(videoB);
659+
expect(input.composition.audios[0]?.src).toBe(audioB);
660+
expect(mediaPreflightComposition).toBe(input.composition);
661+
});
662+
663+
it("does not probe for img or script src mutations", async () => {
664+
resetRetryMocks();
665+
capturedCfgs.length = 0;
666+
const { runProbeStage } = await import("./probeStage.js");
667+
const input = makeProbeInput({});
668+
input.composition.duration = 5;
669+
input.compiled.html = `<img id="poster" src="a.png"><script id="loader"></script>
670+
<script>
671+
document.getElementById("poster").src = "b.png";
672+
document.getElementById("loader").setAttribute("src", "loader-b.js");
673+
</script>`;
674+
675+
await runProbeStage(input);
676+
677+
expect(capturedCfgs).toHaveLength(0);
678+
});
679+
680+
it("probes when a source child of existing media is mutated", async () => {
681+
resetRetryMocks();
682+
capturedCfgs.length = 0;
683+
const { runProbeStage } = await import("./probeStage.js");
684+
const input = makeProbeInput({});
685+
input.composition.duration = 5;
686+
input.compiled.html = `<video id="clip"><source id="clip-source" src="video-a.mp4"></video>
687+
<script>document.getElementById("clip-source").src = "video-b.mp4";</script>`;
688+
689+
await runProbeStage(input);
690+
691+
expect(capturedCfgs.length).toBeGreaterThan(0);
692+
});
693+
586694
it("launches a probe when a static-duration composition uses the Audio constructor", async () => {
587695
capturedCfgs.length = 0;
588696
const { runProbeStage } = await import("./probeStage.js");

0 commit comments

Comments
 (0)