mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(engine): keep media ids that differ only by case in separate folders (#7635)
* fix(engine): keep media ids that differ only by case in separate folders macOS and Windows disks ignore case, and APFS ignores Unicode normalization, but mediaIdPathSegment passed plain ids through as written. Two clips such as `s12-vB1` and `s12-vb1` therefore shared one `__hyperframes_video_frames` folder: whichever was staged first kept it, and the other clip read its frames. A longer clip then failed mid-render with ENOENT on a frame past the shorter clip's end; a same-length pair rendered the wrong footage silently. Only a lowercase, NFC-normalized id now maps to itself. Any other spelling gets the existing readable~digest form, so every per-id path (frame folders, the tail probe, audio work files, distributed plans) stays distinct on a case-insensitive disk. * fix(engine): name the case-folding check instead of commenting it Keeps mediaPathSegment.ts at or under its comment share at the merge base.
This commit is contained in:
@@ -24,6 +24,22 @@ describe("mediaIdPathSegment", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
["s12-vb1", "s12-vB1"],
|
||||
["clip", "CLIP"],
|
||||
["Intro", "INTRO"],
|
||||
["caf\u00e9", "cafe\u0301"],
|
||||
])("never lets %j and %j share a folder on a case-insensitive disk", (a, b) => {
|
||||
const fold = (segment: string) => segment.normalize("NFC").toLowerCase();
|
||||
expect(fold(mediaIdPathSegment(a))).not.toBe(fold(mediaIdPathSegment(b)));
|
||||
});
|
||||
|
||||
it("names an id with capitals by a digest of its exact text", () => {
|
||||
const segment = mediaIdPathSegment("s12-vB1");
|
||||
expect(segment).toMatch(/^s12-vB1~[0-9a-f]{12}$/);
|
||||
expect(mediaIdPathSegment("s12-Vb1")).not.toBe(segment);
|
||||
});
|
||||
|
||||
it("keeps distinct unsafe ids apart", () => {
|
||||
expect(mediaIdPathSegment("a/b")).not.toBe(mediaIdPathSegment("a\\b"));
|
||||
expect(mediaIdPathSegment("a/b")).not.toBe("a_b");
|
||||
|
||||
@@ -10,12 +10,21 @@ const READABLE_MAX_BYTES = 64;
|
||||
* form plus a digest of the whole id, so the two namespaces never meet.
|
||||
*/
|
||||
export function mediaIdPathSegment(id: string): string {
|
||||
if (PLAIN_SEGMENT.test(id) && !GENERATED_SUFFIX.test(id)) return id;
|
||||
if (
|
||||
PLAIN_SEGMENT.test(id) &&
|
||||
!GENERATED_SUFFIX.test(id) &&
|
||||
survivesCaseAndUnicodeFoldingOnMacAndWindowsDisks(id)
|
||||
)
|
||||
return id;
|
||||
const readable = truncateUtf8(id.replace(/[/\\:]/g, "_"), READABLE_MAX_BYTES);
|
||||
const digest = createHash("sha256").update(id).digest("hex").slice(0, 12);
|
||||
return `${readable}~${digest}`;
|
||||
}
|
||||
|
||||
function survivesCaseAndUnicodeFoldingOnMacAndWindowsDisks(id: string): boolean {
|
||||
return id === id.toLowerCase() && id === id.normalize("NFC");
|
||||
}
|
||||
|
||||
function truncateUtf8(text: string, maxBytes: number): string {
|
||||
let bytes = 0;
|
||||
let out = "";
|
||||
|
||||
@@ -1097,6 +1097,44 @@ describe("materializeExtractedFramesForCompiledDir", () => {
|
||||
expect(copies).toEqual([{ src: outputDir, dest: linkPath }]);
|
||||
expect(extracted.framePaths.get(0)).toBe(win32.join(linkPath, "frame_000001.jpg"));
|
||||
});
|
||||
|
||||
it("gives ids that differ only by case their own frames on a case-insensitive disk", () => {
|
||||
// macOS and Windows disks fold case: `s12-vB1` and `s12-vb1` named as-is are one folder.
|
||||
const entries = new Map<string, string>();
|
||||
const fold = (path: string) => path.toLowerCase();
|
||||
const clip = (videoId: string, cacheDir: string, frameCount: number) => ({
|
||||
videoId,
|
||||
outputDir: cacheDir,
|
||||
framePaths: new Map(
|
||||
Array.from({ length: frameCount }, (_, i): [number, string] => [
|
||||
i,
|
||||
join(cacheDir, `frame_${String(i + 1).padStart(5, "0")}.jpg`),
|
||||
]),
|
||||
),
|
||||
});
|
||||
const short = clip("s12-vB1", "/cache/hook", 64);
|
||||
const long = clip("s12-vb1", "/cache/sparks", 66);
|
||||
|
||||
materializeExtractedFramesForCompiledDir([short, long], "/job/compiled", {
|
||||
fileSystem: {
|
||||
existsSync: (path) => entries.has(fold(String(path))),
|
||||
mkdirSync: () => undefined,
|
||||
symlinkSync: (target, path) => {
|
||||
if (entries.has(fold(String(path)))) {
|
||||
throw Object.assign(new Error("EEXIST"), { code: "EEXIST" });
|
||||
}
|
||||
entries.set(fold(String(path)), String(target));
|
||||
},
|
||||
cpSync: () => {
|
||||
throw new Error("a symlink is expected");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const targetOf = (framePath: string) => entries.get(fold(join(framePath, "..")));
|
||||
expect(targetOf(long.framePaths.get(64)!)).toBe("/cache/sparks");
|
||||
expect(targetOf(short.framePaths.get(63)!)).toBe("/cache/hook");
|
||||
});
|
||||
});
|
||||
|
||||
describe("writeCompiledArtifacts — external assets on Windows drive-letter paths (GH #321)", () => {
|
||||
|
||||
Reference in New Issue
Block a user