From 9e52b0bda99c33885af18ad8deb1a5898684e101 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 31 Mar 2026 16:02:00 +0000 Subject: [PATCH] refactor: cache resolved ffmpeg path at module load time Avoid repeated filesystem checks on every play() call by resolving the ffmpeg binary path once at module load. Also verify execute permission after chmod to handle noexec mounts. https://claude.ai/code/session_01CqfKgV8GuCmWNpfx86H62X --- src/audio/player.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/audio/player.ts b/src/audio/player.ts index f013782..3b37273 100644 --- a/src/audio/player.ts +++ b/src/audio/player.ts @@ -9,14 +9,15 @@ import type { Logger } from "../logger.js"; const require = createRequire(import.meta.url); const ffmpegPath: string | null = require("ffmpeg-static"); -/** Ensure the bundled ffmpeg binary has execute permission. */ -function ensureExecutable(binPath: string): boolean { +/** Ensure the given binary has execute permission. */ +function isExecutable(binPath: string): boolean { try { accessSync(binPath, constants.X_OK); return true; } catch { try { chmodSync(binPath, 0o755); + accessSync(binPath, constants.X_OK); return true; } catch { return false; @@ -24,10 +25,13 @@ function ensureExecutable(binPath: string): boolean { } } +/** Resolved once at module load — no repeated fs checks per play(). */ +const resolvedFfmpeg: string = + ffmpegPath && isExecutable(ffmpegPath) ? ffmpegPath : "ffmpeg"; + /** Resolve ffmpeg binary: prefer bundled ffmpeg-static, fall back to system PATH. */ function getFfmpegCommand(): string { - if (ffmpegPath && ensureExecutable(ffmpegPath)) return ffmpegPath; - return "ffmpeg"; // fallback to system-installed ffmpeg + return resolvedFfmpeg; } export interface PlayerEvents {