Skip to content

Commit c8f7542

Browse files
vanceingallsclaude
andcommitted
fix(producer): decode percent-encoded src in HDR image probe
The HDR image probe still hand-rolled the path join the video probe had already delegated to resolveProjectRelativeSrc, so a percent-encoded non-ASCII `<img src>` (`图1.png` -> `%E5%9B%BE1.png`) never resolved: the image never entered nativeHdrImageIds, resolveEffectiveHdrMode saw no HDR sources, and the composition rendered through the SDR fallback with wrong color -- silently, unlike the video path which errored at ffmpeg. Both probes now call resolveProjectRelativeSrc directly, with no isAbsolute() pre-check. The resolver already returns an absolute path that exists and otherwise treats a leading slash as a browser origin-root URL, so a pre-check would hand back `/assets/%E5%9B%BE1.png` undecoded and re-open the same bug for root-relative srcs. This matches planHdrResources, so the two halves of the fix can no longer disagree. Widening resolution also makes previously-unresolvable files reachable for the first time, including truncated or 0-byte assets on which ffprobe exits non-zero. These probes run inside a bare Promise.all, so an unguarded throw aborted the whole render over one unreadable image; probeColorSpaceSafely now logs and treats such a source as SDR. Tests cover percent-encoded CJK, origin-root percent-encoded CJK, compiledDir-over-projectDir precedence, and existing-absolute passthrough, with distinct projectDir/compiledDir so the precedence is actually pinned. Fault-injection verified: reintroducing the isAbsolute short-circuit fails the origin-root test. Refs PRINFRA-349 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 0fcf792 commit c8f7542

2 files changed

Lines changed: 121 additions & 23 deletions

File tree

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { describe, expect, it } from "vitest";
2+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { resolveProjectRelativeSrc } from "@hyperframes/engine";
26
import { appendAutoDetectedVideoAudio, shouldCopyExtractedFrames } from "./extractVideosStage.js";
37
import type { ExtractedFrames, VideoElement } from "@hyperframes/engine";
48

@@ -82,6 +86,74 @@ describe("appendAutoDetectedVideoAudio", () => {
8286
});
8387
});
8488

89+
// The HDR probes in this stage resolve `<video>`/`<img>` srcs with
90+
// resolveProjectRelativeSrc and NO isAbsolute() pre-check. These pin the src
91+
// shapes that a pre-check would silently break — an earlier revision of the
92+
// PRINFRA-349 fix short-circuited on isAbsolute and left root-relative srcs
93+
// percent-encoded, so the HDR image never resolved and the render shipped SDR.
94+
describe("HDR probe src resolution (PRINFRA-349)", () => {
95+
it("decodes a percent-encoded CJK src to the real on-disk path", () => {
96+
const projectDir = mkdtempSync(join(tmpdir(), "hf-probe-cjk-"));
97+
const compiledDir = mkdtempSync(join(tmpdir(), "hf-probe-compiled-"));
98+
try {
99+
const realName = "图1.png";
100+
writeFileSync(join(projectDir, realName), "x");
101+
// The compiled DOM carries the URL-encoded attribute value.
102+
const encoded = encodeURIComponent(realName); // %E5%9B%BE1.png
103+
expect(resolveProjectRelativeSrc(encoded, projectDir, compiledDir)).toBe(
104+
join(projectDir, realName),
105+
);
106+
} finally {
107+
rmSync(projectDir, { recursive: true, force: true });
108+
rmSync(compiledDir, { recursive: true, force: true });
109+
}
110+
});
111+
112+
it("decodes a percent-encoded CJK src served from a browser origin-root URL", () => {
113+
// Regression guard: `isAbsolute("/assets/%E5%9B%BE1.png")` is true on POSIX,
114+
// so a pre-check would return it verbatim, existsSync would fail, and the
115+
// image would never enter nativeHdrImageIds — a silent SDR render.
116+
const projectDir = mkdtempSync(join(tmpdir(), "hf-probe-root-"));
117+
try {
118+
mkdirSync(join(projectDir, "assets"));
119+
const realName = "图1.png";
120+
writeFileSync(join(projectDir, "assets", realName), "x");
121+
const rootRelative = `/assets/${encodeURIComponent(realName)}`;
122+
expect(resolveProjectRelativeSrc(rootRelative, projectDir, projectDir)).toBe(
123+
join(projectDir, "assets", realName),
124+
);
125+
} finally {
126+
rmSync(projectDir, { recursive: true, force: true });
127+
}
128+
});
129+
130+
it("prefers compiledDir over projectDir when both hold the asset", () => {
131+
const projectDir = mkdtempSync(join(tmpdir(), "hf-probe-proj-"));
132+
const compiledDir = mkdtempSync(join(tmpdir(), "hf-probe-comp-"));
133+
try {
134+
writeFileSync(join(projectDir, "clip.mp4"), "x");
135+
writeFileSync(join(compiledDir, "clip.mp4"), "x");
136+
expect(resolveProjectRelativeSrc("clip.mp4", projectDir, compiledDir)).toBe(
137+
join(compiledDir, "clip.mp4"),
138+
);
139+
} finally {
140+
rmSync(projectDir, { recursive: true, force: true });
141+
rmSync(compiledDir, { recursive: true, force: true });
142+
}
143+
});
144+
145+
it("returns an existing absolute path unchanged", () => {
146+
const projectDir = mkdtempSync(join(tmpdir(), "hf-probe-abs-"));
147+
try {
148+
const abs = join(projectDir, "clip.mp4");
149+
writeFileSync(abs, "x");
150+
expect(resolveProjectRelativeSrc(abs, projectDir, projectDir)).toBe(abs);
151+
} finally {
152+
rmSync(projectDir, { recursive: true, force: true });
153+
}
154+
});
155+
});
156+
85157
describe("shouldCopyExtractedFrames", () => {
86158
it("copies frames on Windows (symlinkSync throws EPERM without Developer Mode)", () => {
87159
expect(shouldCopyExtractedFrames("win32")).toBe(true);

‎packages/producer/src/services/render/stages/extractVideosStage.ts‎

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030

3131
import { existsSync } from "node:fs";
32-
import { isAbsolute, join } from "node:path";
32+
import { join } from "node:path";
3333
import {
3434
type CaptureVideoMetadataHint,
3535
type EngineConfig,
@@ -108,6 +108,34 @@ export function shouldCopyExtractedFrames(platform: NodeJS.Platform): boolean {
108108
return platform === "win32";
109109
}
110110

111+
/**
112+
* Probe a media file's color space, returning null when it can't be read.
113+
*
114+
* Both HDR probes below widened which files they resolve (PRINFRA-349: the
115+
* shared resolver percent-decodes non-ASCII names, so `%E5%9B%BE1.png` now
116+
* finds `图1.png`). Files that used to silently fail to resolve are therefore
117+
* reachable for the first time — including truncated / 0-byte assets, on which
118+
* ffprobe exits non-zero and `extractMediaMetadata` throws. These probes run
119+
* inside a bare `Promise.all`, so an unguarded throw aborts the whole render
120+
* over one unreadable image. A probe that can't read a file must skip it, not
121+
* kill the render.
122+
*/
123+
async function probeColorSpaceSafely(
124+
path: string,
125+
log: ProducerLogger | undefined,
126+
): Promise<VideoColorSpace | null> {
127+
try {
128+
const meta = await extractMediaMetadata(path);
129+
return meta.colorSpace;
130+
} catch (error) {
131+
log?.warn("HDR color-space probe failed; treating source as SDR", {
132+
path,
133+
error: error instanceof Error ? error.message : String(error),
134+
});
135+
return null;
136+
}
137+
}
138+
111139
export async function runExtractVideosStage(
112140
input: ExtractVideosStageInput,
113141
): Promise<ExtractVideosStageResult> {
@@ -140,19 +168,19 @@ export async function runExtractVideosStage(
140168
log?.info("Probing video color spaces...", { videoCount: composition.videos.length });
141169
await Promise.all(
142170
composition.videos.map(async (v) => {
143-
// Use the shared resolver so a `<video src="../assets/foo">` in a
144-
// sub-composition resolves the same way the browser would (see
145-
// resolveProjectRelativeSrc in videoFrameExtractor for the full
146-
// explanation). isAbsolute (not `startsWith("/")`) so Windows
147-
// absolute paths like `C:\...` skip the join correctly.
148-
const videoPath = isAbsolute(v.src)
149-
? v.src
150-
: resolveProjectRelativeSrc(v.src, projectDir, compiledDir);
171+
// Shared resolver so a `<video src="../assets/foo">` in a sub-composition
172+
// resolves the same way the browser would, and a percent-encoded
173+
// non-ASCII name decodes to its real on-disk path. Called with no
174+
// isAbsolute() pre-check: the resolver already returns an absolute path
175+
// that exists, and otherwise treats a leading slash as a browser
176+
// origin-root URL — pre-checking would hand back `/assets/%E5%9B%BE1.png`
177+
// undecoded and re-open PRINFRA-349 for root-relative srcs.
178+
const videoPath = resolveProjectRelativeSrc(v.src, projectDir, compiledDir);
151179
if (!existsSync(videoPath)) return;
152-
const meta = await extractMediaMetadata(videoPath);
153-
if (isHdrColorSpace(meta.colorSpace)) {
180+
const colorSpace = await probeColorSpaceSafely(videoPath, log);
181+
if (isHdrColorSpace(colorSpace)) {
154182
nativeHdrVideoIds.add(v.id);
155-
videoTransfers.set(v.id, detectTransfer(meta.colorSpace));
183+
videoTransfers.set(v.id, detectTransfer(colorSpace));
156184
}
157185
}),
158186
);
@@ -170,21 +198,19 @@ export async function runExtractVideosStage(
170198
if (job.config.hdrMode !== "force-sdr" && composition.images.length > 0) {
171199
const probed = await Promise.all(
172200
composition.images.map(async (img) => {
173-
let imgPath = img.src;
174-
if (!imgPath.startsWith("/")) {
175-
const fromCompiled = existsSync(join(compiledDir, imgPath))
176-
? join(compiledDir, imgPath)
177-
: join(projectDir, imgPath);
178-
imgPath = fromCompiled;
179-
}
201+
// Same shared resolver as the video probe above — a percent-encoded
202+
// non-ASCII `<img src>` must decode to the on-disk path, or the HDR image
203+
// never enters nativeHdrImageIds and the composition silently renders
204+
// through the SDR fallback with wrong color (PRINFRA-349 symptom c).
205+
const imgPath = resolveProjectRelativeSrc(img.src, projectDir, compiledDir);
180206
if (!existsSync(imgPath)) return null;
181-
const meta = await extractMediaMetadata(imgPath);
182-
if (isHdrColorSpace(meta.colorSpace)) {
207+
const colorSpace = await probeColorSpaceSafely(imgPath, log);
208+
if (isHdrColorSpace(colorSpace)) {
183209
nativeHdrImageIds.add(img.id);
184-
imageTransfers.set(img.id, detectTransfer(meta.colorSpace));
210+
imageTransfers.set(img.id, detectTransfer(colorSpace));
185211
hdrImageSrcPaths.set(img.id, imgPath);
186212
}
187-
return meta.colorSpace;
213+
return colorSpace;
188214
}),
189215
);
190216
imageColorSpaces.push(...probed);

0 commit comments

Comments
 (0)