@@ -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
2427function 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 */
177192export 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. */
190205export function clearFfBinaryLookupCache ( ) : void {
191206 pathLookupCache . clear ( ) ;
207+ lastMissAt . clear ( ) ;
192208}
0 commit comments