Skip to content

Commit 64c47cf

Browse files
fix(parsers): find ffmpeg installed after a long-running preview started (#5439)
* fix(parsers): find ffmpeg installed after a long-running preview started * fix(parsers): remember a missing ffmpeg for a few seconds so per-file probes stay cheap
1 parent 721ebea commit 64c47cf

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

‎packages/parsers/src/ffBinaries.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "node:fs";
1111
import { tmpdir } from "node:os";
1212
import { join, resolve } from "node:path";
13-
import { afterEach, describe, expect, it, vi } from "vitest";
13+
import { afterEach, describe, expect, it, onTestFinished, vi } from "vitest";
1414

1515
type FfBinariesModule = typeof import("./ffBinaries.js");
1616

@@ -308,4 +308,40 @@ describe("findFfBinary", () => {
308308
expect(findFfBinary("ffmpeg")).toBeUndefined();
309309
expect(execFileSync).toHaveBeenCalledTimes(2);
310310
});
311+
312+
it("finds a binary installed in a common dir shortly after an earlier miss, then caches the hit", async () => {
313+
vi.useFakeTimers();
314+
onTestFinished(() => {
315+
vi.useRealTimers();
316+
});
317+
delete process.env.HYPERFRAMES_FFMPEG_PATH;
318+
Object.defineProperty(process, "platform", { value: "linux", configurable: true });
319+
process.env.PATH = "";
320+
let installed = false;
321+
const execFileSync = vi.fn(() => {
322+
throw new Error("not found");
323+
});
324+
vi.resetModules();
325+
vi.doMock("node:child_process", () => ({ execFileSync, default: { execFileSync } }));
326+
vi.doMock("node:fs", () => {
327+
const mocked = {
328+
existsSync: (candidate: unknown) => installed && candidate === "/opt/homebrew/bin/ffmpeg",
329+
accessSync: () => {
330+
throw new Error("not executable");
331+
},
332+
constants: { X_OK: 1 },
333+
};
334+
return { ...mocked, default: mocked };
335+
});
336+
const { findFfBinary } = await importFresh();
337+
338+
expect(findFfBinary("ffmpeg")).toBeUndefined();
339+
installed = true;
340+
expect(findFfBinary("ffmpeg")).toBeUndefined();
341+
expect(execFileSync).toHaveBeenCalledTimes(1);
342+
vi.advanceTimersByTime(5_000);
343+
expect(findFfBinary("ffmpeg")).toBe(resolve("/opt/homebrew/bin/ffmpeg"));
344+
expect(findFfBinary("ffmpeg")).toBe(resolve("/opt/homebrew/bin/ffmpeg"));
345+
expect(execFileSync).toHaveBeenCalledTimes(2);
346+
});
311347
});

‎packages/parsers/src/ffBinaries.ts‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ const ENV_BY_NAME: Record<FfBinaryName, string> = {
1919
ffprobe: FFPROBE_PATH_ENV,
2020
};
2121

22-
const pathLookupCache = new Map<FfBinaryName, string | undefined>();
22+
const pathLookupCache = new Map<FfBinaryName, string>();
23+
// A miss is trusted briefly: callers probe per media file, and an install still shows up within seconds.
24+
const MISS_TTL_MS = 5_000;
25+
const lastMissAt = new Map<FfBinaryName, number>();
2326

2427
function candidateFileName(candidate: string): string {
2528
return candidate.split(/[\\/]/).at(-1)?.toLowerCase() ?? candidate.toLowerCase();
@@ -126,8 +129,7 @@ function findInInstallerPackage(name: FfBinaryName): string | undefined {
126129
return candidate && isExecutablePathCandidate(candidate) ? candidate : undefined;
127130
}
128131

129-
function lookupOnSystem(name: FfBinaryName): string | undefined {
130-
if (pathLookupCache.has(name)) return pathLookupCache.get(name);
132+
function searchSystem(name: FfBinaryName): string | undefined {
131133
let found: string | undefined;
132134
if (process.platform === "win32") {
133135
// `where.exe` writes bytes in the active console code page, while Node
@@ -150,7 +152,19 @@ function lookupOnSystem(name: FfBinaryName): string | undefined {
150152
}
151153
found ??= findInProjectLocalBin(name);
152154
found ??= findInCommonDirsOrInstallerPackage(name);
153-
const resolved = found ? resolve(found) : undefined;
155+
return found;
156+
}
157+
158+
function lookupOnSystem(name: FfBinaryName): string | undefined {
159+
const cached = pathLookupCache.get(name);
160+
if (cached) return cached;
161+
if (Date.now() - (lastMissAt.get(name) ?? -Infinity) < MISS_TTL_MS) return undefined;
162+
const found = searchSystem(name);
163+
if (!found) {
164+
lastMissAt.set(name, Date.now());
165+
return undefined;
166+
}
167+
const resolved = resolve(found);
154168
pathLookupCache.set(name, resolved);
155169
return resolved;
156170
}
@@ -170,9 +184,10 @@ export interface FindFfBinaryOptions {
170184
* Resolve an FFmpeg-family binary: env override first, then a native
171185
* current-directory/PATH scan on Windows or `which` plus PATH scan on Unix,
172186
* then a project-local `.hyperframes/bin`, then well-known Unix install dirs, then an
173-
* `@ffmpeg-installer`/`@ffprobe-installer` package reachable from the current folder. System lookups are
174-
* cached per binary for the process lifetime; the env override is re-read on
175-
* every call.
187+
* `@ffmpeg-installer`/`@ffprobe-installer` package reachable from the current folder.
188+
* A found binary is cached for the process lifetime; a miss only for a few
189+
* seconds, so a long-running server sees an install made after it started. The env
190+
* override is re-read on every call.
176191
*/
177192
export function findFfBinary(
178193
name: FfBinaryName,
@@ -189,4 +204,5 @@ export function findFfBinary(
189204
/** Test hook: drop cached system lookups so resolution can be re-exercised. */
190205
export function clearFfBinaryLookupCache(): void {
191206
pathLookupCache.clear();
207+
lastMissAt.clear();
192208
}

0 commit comments

Comments
 (0)