Skip to content

Commit 345220b

Browse files
committed
fix(producer): name host tool failures and keep a failed HEIC preview image as authored
Also: HEIC tests run in the integration lane; the primary grid is picked by default disposition.
1 parent cb913b3 commit 345220b

6 files changed

Lines changed: 95 additions & 10 deletions

File tree

‎packages/cli/src/server/studioServer.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
581581
downloadDir: gifDownloadDir,
582582
outputDir: heifOutputDir,
583583
outputSrcPrefix: ".hyperframes/prepared-assets/heif",
584+
onFailure: (error) => console.warn(`[studio] ${error.message}`),
584585
});
585586
const prepared = await prepareAnimatedGifInputs(htmlWithJpegs, {
586587
projectDir: project.dir,

‎packages/producer/scripts/test-classification.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const INTEGRATION_TEST_FILES = new Set([
1515
"src/regression-harness-audio.test.ts",
1616
"src/services/bundleScriptPolicy.test.ts",
1717
"src/services/coreRuntimeBrowser.test.ts",
18+
"src/services/heifImagePrep.test.ts",
19+
"src/services/htmlCompiler.heif.test.ts",
1820
"src/services/vfxDeterminism.test.ts",
1921
"src/services/distributed/assemble.test.ts",
2022
"src/services/distributed/chunkBoundary.test.ts",

‎packages/producer/src/server.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const SSE_KEEPALIVE_MS = 30_000;
128128
const SAFE_RENDER_ERROR_CODES = new Set<string>([
129129
"ASSET_MEDIA_TYPE_MISMATCH",
130130
"HEIF_IMAGE_CONVERSION_FAILED",
131+
"HEIF_TOOL_UNAVAILABLE",
131132
"NOT_MEDIA_PAYLOAD",
132133
"INVALID_VIDEO_METADATA",
133134
"VIDEO_SOURCE_UNRENDERABLE",

‎packages/producer/src/services/heifImagePrep.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
convertHeifToJpeg,
99
HEIF_IMAGE_CONVERSION_FAILED,
1010
HeifImageConversionError,
11+
HeifToolUnavailableError,
1112
isHeifBytes,
13+
primaryGrid,
1214
prepareHeifImageInputs,
1315
type HeifTranscodeRequest,
1416
} from "./heifImagePrep.js";
@@ -193,6 +195,52 @@ describe("prepareHeifImageInputs", () => {
193195
expect(error.message).toContain("JPEG or PNG");
194196
});
195197

198+
it("blames the host, not the photo, when ffprobe is missing or too old", async () => {
199+
const d = project({ "a.heic": Buffer.concat([ftyp("heic", "mif1"), Buffer.alloc(64)]) });
200+
for (const said of [
201+
"[FFmpeg] ffprobe not found. Please install FFmpeg.",
202+
"[FFmpeg] ffprobe exit with code 1: Option not found",
203+
]) {
204+
const transcode = async () => {
205+
throw new Error(said);
206+
};
207+
const error = await prepareHeifImageInputs('<img src="a.heic">', { ...d, transcode }).catch(
208+
(e) => e,
209+
);
210+
expect(error).toBeInstanceOf(HeifToolUnavailableError);
211+
expect(error.owner).toBe("system");
212+
expect(error.message).toContain('"a.heic"');
213+
expect(error.message).toContain("7.0 or newer");
214+
expect(error.message).not.toContain("Re-export");
215+
}
216+
});
217+
218+
it("leaves a failed image as authored and still converts the rest when told to report failures", async () => {
219+
const bad = Buffer.concat([ftyp("heic", "mif1"), Buffer.alloc(64)]);
220+
const d = project({ "bad.heic": bad, "good.heic": readFileSync(FIXTURE) });
221+
const failures: unknown[] = [];
222+
const result = await prepareHeifImageInputs('<img src="bad.heic"><img src="good.heic">', {
223+
...d,
224+
onFailure: (error) => failures.push(error),
225+
});
226+
expect(failures).toHaveLength(1);
227+
expect(result.html).toContain('src="bad.heic"');
228+
expect(result.html).toContain("_heif_images/hfheif-v1-");
229+
expect(result.preparedImages).toHaveLength(1);
230+
});
231+
232+
it("converts the primary item's grid, not an auxiliary grid listed first", () => {
233+
const grid = (width: number) => ({ components: [{ width, height: 10 }] });
234+
const probed = {
235+
stream_groups: [
236+
{ ...grid(111), disposition: { default: 0 } },
237+
{ ...grid(222), disposition: { default: 1 } },
238+
],
239+
};
240+
expect(primaryGrid(probed)?.width).toBe(222);
241+
expect(primaryGrid({ stream_groups: [grid(5)] })?.width).toBe(5);
242+
});
243+
196244
it("spawns nothing and writes nothing without a HEIC image", async () => {
197245
const png = Buffer.from(
198246
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/q842iQAAAABJRU5ErkJggg==",

‎packages/producer/src/services/heifImagePrep.ts‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const PREPARED_HEIF_SUBDIR = "_heif_images";
2727
const CACHE_SCHEMA = "hfheif-v1";
2828
const DEFAULT_TIMEOUT_MS = 120_000;
2929
export const HEIF_IMAGE_CONVERSION_FAILED = "HEIF_IMAGE_CONVERSION_FAILED" as const;
30+
const HEIF_TOOL_UNAVAILABLE = "HEIF_TOOL_UNAVAILABLE" as const;
3031

3132
/** The `ftyp` brands of a HEIF still coded with the hevc codec. */
3233
const HEIF_BRANDS = new Set(["heic", "heix", "hevc", "hevx", "heim", "heis", "mif1", "msf1"]);
@@ -47,6 +48,24 @@ export class HeifImageConversionError extends Error {
4748
}
4849
}
4950

51+
/** The host's ffmpeg or ffprobe cannot do the job; a different photo would not help. */
52+
export class HeifToolUnavailableError extends Error {
53+
readonly code = HEIF_TOOL_UNAVAILABLE;
54+
readonly owner = "system" as const;
55+
readonly retryable = false as const;
56+
57+
constructor(label: string, reason: string) {
58+
super(
59+
`Could not convert HEIC/HEIF image ${label}: ${reason}. ` +
60+
`Converting HEIC needs ffmpeg and ffprobe 7.0 or newer with the hevc decoder.`,
61+
);
62+
this.name = "HeifToolUnavailableError";
63+
}
64+
}
65+
66+
const TOOL_FAILURE_RE =
67+
/(?:ffmpeg|ffprobe) not found|ENOENT|Option not found|Unrecognized option|Unknown decoder|Decoder \S+ not found/i;
68+
5069
export interface HeifTranscodeRequest {
5170
inputPath: string;
5271
outputPath: string;
@@ -62,6 +81,8 @@ export interface PrepareHeifImageInputsOptions {
6281
sourceAssets?: Map<string, string>;
6382
timeoutMs?: number;
6483
transcode?: (input: HeifTranscodeRequest) => Promise<void>;
84+
/** Leaves an image whose conversion failed as authored and reports it, instead of throwing. */
85+
onFailure?: (error: HeifImageConversionError | HeifToolUnavailableError) => void;
6586
}
6687

6788
export interface PreparedHeifImage {
@@ -102,7 +123,7 @@ function readHead(path: string): Buffer | null {
102123
}
103124
}
104125

105-
interface ProbedStream {
126+
export interface ProbedStream {
106127
width?: number;
107128
height?: number;
108129
coded_width?: number;
@@ -116,9 +137,9 @@ interface ProbedStream {
116137
}[];
117138
}
118139

119-
interface ProbeJson {
140+
export interface ProbeJson {
120141
streams?: ProbedStream[];
121-
stream_groups?: { components?: ProbedStream[] }[];
142+
stream_groups?: { components?: ProbedStream[]; disposition?: { default?: number } }[];
122143
}
123144

124145
async function probeJson(path: string, withGroups: boolean): Promise<ProbeJson> {
@@ -138,11 +159,17 @@ const ROTATION_FILTER = {
138159
270: "transpose=1",
139160
} as const;
140161

162+
/** The primary item's grid: the group ffmpeg marks default, so an auxiliary grid listed first is skipped. */
163+
export function primaryGrid(probed: ProbeJson): ProbedStream | undefined {
164+
const groups = probed.stream_groups ?? [];
165+
return (groups.find((g) => g.disposition?.default === 1) ?? groups[0])?.components?.[0];
166+
}
167+
141168
// ffmpeg through 8.0 decodes a tiled HEIC as separate tiles and ignores its rotation, so stack, crop and rotate.
142169
// The JPEG must equal the declared size (swapped for a quarter turn); a lone tile never passes.
143170
export async function convertHeifToJpeg(request: HeifTranscodeRequest): Promise<void> {
144171
const probed = await probeJson(request.inputPath, true);
145-
const grid = probed.stream_groups?.[0]?.components?.[0];
172+
const grid = primaryGrid(probed);
146173
const rotation = readHeifPrimaryRotation(readFileSync(request.inputPath));
147174
const filters: string[] = [];
148175
let inputs: string[] = [];
@@ -253,10 +280,10 @@ async function ensurePreparedJpeg(
253280
if (!isUsableFile(tmpPath)) throw new Error("conversion produced an empty file");
254281
renameWithRetry(tmpPath, cachePath);
255282
} catch (error) {
256-
throw new HeifImageConversionError(
257-
source.label,
258-
error instanceof Error ? error.message : String(error),
259-
);
283+
const reason = error instanceof Error ? error.message : String(error);
284+
throw TOOL_FAILURE_RE.test(reason)
285+
? new HeifToolUnavailableError(source.label, reason)
286+
: new HeifImageConversionError(source.label, reason);
260287
} finally {
261288
rmSync(tmpPath, { force: true });
262289
rmSync(inlinePath, { force: true });
@@ -296,15 +323,21 @@ export async function prepareHeifImageInputs(
296323
if (found.length === 0) return { html, preparedAssets, preparedImages };
297324

298325
const unique = new Map(found.map((item) => [item.filename, item.source]));
326+
const failed = new Set<string>();
299327
await Promise.all(
300328
[...unique].map(([filename, source]) =>
301329
withMediaProbeSlot(() =>
302330
ensurePreparedJpeg(source, join(cacheDir, filename), join(outputDir, filename), options),
303-
),
331+
).catch((error: HeifImageConversionError | HeifToolUnavailableError) => {
332+
if (!options.onFailure) throw error;
333+
failed.add(filename);
334+
options.onFailure(error);
335+
}),
304336
),
305337
);
306338

307339
for (const { img, src, filename } of found) {
340+
if (failed.has(filename)) continue;
308341
const outputSrc = `${outputSrcPrefix}/${filename}`;
309342
img.setAttribute("src", outputSrc);
310343
preparedAssets.set(outputSrc, join(outputDir, filename));

‎scripts/test-reachability.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
],
4848
"sources": {
4949
"packages/producer/scripts/run-test-lane.mjs": "638885e14480d03290803389325a655f3749442167d784e07bb3af3404598af7",
50-
"packages/producer/scripts/test-classification.mjs": "47a6204722443284067bf5c464601a252091aab3670ef1626e50ce507b8214fc",
50+
"packages/producer/scripts/test-classification.mjs": "c434da94aa74aef060ef41736af798fd5a9af9236684a47d07a19661c1f78b9b",
5151
".github/workflows/ci.yml#job#producer-source-tests": "45da72aa6cf08cdfd5bcf5311e89f12af4842f9eebc6735df19da8c02f4d4864",
5252
"packages/producer/package.json#script#test:unit": "ae6beda8319d166774be958ea5593cd27dbf7f6c9bce1d787a3fe28dfc3477b1",
5353
"packages/producer/package.json#script#test:integration": "008589f37d026877595a13501cbb66abd5cbe50e21fdcac6904f45466c3f475d",

0 commit comments

Comments
 (0)