mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 19:36:04 +00:00
fix(engine): resolve ffmpeg/ffprobe via a PATH scan when where/which fails
findOnPath resolved ffmpeg/ffprobe by shelling out to where (Windows) or which (POSIX). In a restricted/sandboxed shell where that helper binary itself isn't runnable, resolution failed even when the target binary was directly executable and genuinely on PATH - the caller fell back to a bare command name that a later non-shell spawn() can't resolve on its own, so render failed hard for a perfectly usable install. Added a pure-JS PATH-directory scan as a fallback when where/which fails, needing no external helper process.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
// fallow-ignore-file code-duplication
|
||||
import { resolve } from "node:path";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { delimiter, join, resolve } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
assertConfiguredFfmpegBinariesExist,
|
||||
findViaPathScan,
|
||||
getFfmpegBinary,
|
||||
getFfprobeBinary,
|
||||
} from "./ffmpegBinaries.js";
|
||||
@@ -41,3 +44,54 @@ describe("ffmpeg binary env resolution", () => {
|
||||
expect(() => assertConfiguredFfmpegBinariesExist()).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
// Regression: findOnPath used to rely entirely on shelling out to
|
||||
// `where`/`which`, so a restricted/sandboxed shell without that helper
|
||||
// binary made a genuinely-on-PATH ffmpeg/ffprobe look unresolvable, even
|
||||
// though a pure PATH-directory scan would have found it fine. findViaPathScan
|
||||
// is the fallback that needs no external helper process — tested directly
|
||||
// (bypassing findOnPath's module-level cache and the real `where`/`which`
|
||||
// call) with a real fake binary on a controlled PATH.
|
||||
describe("findViaPathScan", () => {
|
||||
it("finds a binary that exists in a PATH directory", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "ffmpeg-path-scan-"));
|
||||
const fakeFfmpeg = join(dir, "ffmpeg");
|
||||
writeFileSync(fakeFfmpeg, "#!/bin/sh\necho fake\n");
|
||||
const originalPath = process.env.PATH;
|
||||
try {
|
||||
process.env.PATH = dir;
|
||||
expect(findViaPathScan("ffmpeg")).toBe(fakeFfmpeg);
|
||||
} finally {
|
||||
process.env.PATH = originalPath;
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("returns undefined when the binary is not on any PATH directory", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "ffmpeg-path-scan-empty-"));
|
||||
const originalPath = process.env.PATH;
|
||||
try {
|
||||
process.env.PATH = dir;
|
||||
expect(findViaPathScan("ffmpeg")).toBeUndefined();
|
||||
} finally {
|
||||
process.env.PATH = originalPath;
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("checks multiple PATH directories in order", () => {
|
||||
const emptyDir = mkdtempSync(join(tmpdir(), "ffmpeg-path-scan-a-"));
|
||||
const realDir = mkdtempSync(join(tmpdir(), "ffmpeg-path-scan-b-"));
|
||||
const fakeFfprobe = join(realDir, "ffprobe");
|
||||
writeFileSync(fakeFfprobe, "#!/bin/sh\necho fake\n");
|
||||
const originalPath = process.env.PATH;
|
||||
try {
|
||||
process.env.PATH = [emptyDir, realDir].join(delimiter);
|
||||
expect(findViaPathScan("ffprobe")).toBe(fakeFfprobe);
|
||||
} finally {
|
||||
process.env.PATH = originalPath;
|
||||
rmSync(emptyDir, { recursive: true, force: true });
|
||||
rmSync(realDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
// fallow-ignore-file code-duplication
|
||||
import { execFileSync } from "child_process";
|
||||
import { existsSync } from "fs";
|
||||
import { resolve } from "path";
|
||||
import { delimiter, join, resolve } from "path";
|
||||
|
||||
export const FFMPEG_PATH_ENV = "HYPERFRAMES_FFMPEG_PATH";
|
||||
export const FFPROBE_PATH_ENV = "HYPERFRAMES_FFPROBE_PATH";
|
||||
|
||||
const pathCache = new Map<string, string | undefined>();
|
||||
|
||||
function findOnPath(name: "ffmpeg" | "ffprobe"): string | undefined {
|
||||
if (pathCache.has(name)) return pathCache.get(name);
|
||||
function findViaWhereOrWhich(name: "ffmpeg" | "ffprobe"): string | undefined {
|
||||
try {
|
||||
const command = process.platform === "win32" ? "where" : "which";
|
||||
const output = execFileSync(command, [name], {
|
||||
@@ -21,15 +20,39 @@ function findOnPath(name: "ffmpeg" | "ffprobe"): string | undefined {
|
||||
.split(/\r?\n/)
|
||||
.map((s) => s.trim())
|
||||
.find(Boolean);
|
||||
const resolved = first ? resolve(first) : undefined;
|
||||
pathCache.set(name, resolved);
|
||||
return resolved;
|
||||
return first ? resolve(first) : undefined;
|
||||
} catch {
|
||||
pathCache.set(name, undefined);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// `where`/`which` themselves can be unavailable (a restricted/sandboxed shell
|
||||
// without cmd.exe's where.exe on PATH) even when the target binary is
|
||||
// directly executable and genuinely on PATH. Scanning PATH directories
|
||||
// ourselves needs no external helper process, so it still resolves the
|
||||
// binary in that case instead of falling through to a bare command name that
|
||||
// a later non-shell `spawn()` can't resolve on its own.
|
||||
export function findViaPathScan(name: "ffmpeg" | "ffprobe"): string | undefined {
|
||||
const pathEnv = process.env.PATH ?? process.env.Path ?? process.env.path;
|
||||
if (!pathEnv) return undefined;
|
||||
const candidateNames = process.platform === "win32" ? [`${name}.exe`, name] : [name];
|
||||
for (const dir of pathEnv.split(delimiter)) {
|
||||
if (!dir) continue;
|
||||
for (const candidateName of candidateNames) {
|
||||
const candidate = join(dir, candidateName);
|
||||
if (existsSync(candidate)) return candidate;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function findOnPath(name: "ffmpeg" | "ffprobe"): string | undefined {
|
||||
if (pathCache.has(name)) return pathCache.get(name);
|
||||
const resolved = findViaWhereOrWhich(name) ?? findViaPathScan(name);
|
||||
pathCache.set(name, resolved);
|
||||
return resolved;
|
||||
}
|
||||
|
||||
function getConfiguredBinary(envName: string, binaryName: "ffmpeg" | "ffprobe"): string {
|
||||
const configured = process.env[envName]?.trim();
|
||||
if (configured) return resolve(configured);
|
||||
|
||||
Reference in New Issue
Block a user