Skip to content

Commit fc0a5de

Browse files
committed
fix: handle windows extracted frame paths
1 parent 3ca71c1 commit fc0a5de

4 files changed

Lines changed: 178 additions & 31 deletions

File tree

‎packages/producer/src/services/renderOrchestrator.test.ts‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
22
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3-
import { join } from "node:path";
3+
import { join, win32 } from "node:path";
44
import { tmpdir } from "node:os";
5-
import type { EngineConfig } from "@hyperframes/engine";
5+
import type { EngineConfig, ExtractedFrames } from "@hyperframes/engine";
66
import type { CompiledComposition } from "./htmlCompiler.js";
77

88
import {
@@ -18,6 +18,7 @@ import {
1818
findMissingFrameRanges,
1919
getNextRetryWorkerCount,
2020
isRecoverableParallelCaptureError,
21+
materializeExtractedFramesForCompiledDir,
2122
projectBrowserEndToCompositionTimeline,
2223
resolveRenderWorkerCount,
2324
selectCaptureCalibrationFrames,
@@ -164,6 +165,69 @@ describe("createCompiledFrameSrcResolver", () => {
164165
});
165166
});
166167

168+
describe("materializeExtractedFramesForCompiledDir", () => {
169+
function createExtractedFrames(
170+
outputDir: string,
171+
framePath: string,
172+
): Pick<ExtractedFrames, "videoId" | "outputDir" | "framePaths"> {
173+
return {
174+
videoId: "video-1",
175+
outputDir,
176+
framePaths: new Map([[0, framePath]]),
177+
};
178+
}
179+
180+
it("leaves Windows frame paths already under compiledDir unchanged", () => {
181+
const compiledDir = win32.resolve("C:\\compiled");
182+
const outputDir = win32.join(compiledDir, "__hyperframes_video_frames", "video-1");
183+
const framePath = win32.join(outputDir, "frame_000001.jpg");
184+
const extracted = createExtractedFrames(outputDir, framePath);
185+
186+
materializeExtractedFramesForCompiledDir([extracted], compiledDir, {
187+
pathModule: win32,
188+
fileSystem: {
189+
existsSync: () => {
190+
throw new Error("inside compiledDir should not touch the filesystem");
191+
},
192+
mkdirSync: () => {
193+
throw new Error("inside compiledDir should not mkdir");
194+
},
195+
symlinkSync: () => {
196+
throw new Error("inside compiledDir should not symlink");
197+
},
198+
},
199+
});
200+
201+
expect(extracted.outputDir).toBe(outputDir);
202+
expect(extracted.framePaths.get(0)).toBe(framePath);
203+
});
204+
205+
it("remaps Windows cache frames under compiledDir using only the frame basename", () => {
206+
const compiledDir = win32.resolve("C:\\compiled");
207+
const outputDir = win32.resolve("D:\\cache\\abc123");
208+
const framePath = win32.join(outputDir, "frame_000001.jpg");
209+
const extracted = createExtractedFrames(outputDir, framePath);
210+
const symlinks: Array<{ target: string; path: string }> = [];
211+
212+
materializeExtractedFramesForCompiledDir([extracted], compiledDir, {
213+
pathModule: win32,
214+
fileSystem: {
215+
existsSync: () => false,
216+
mkdirSync: () => undefined,
217+
symlinkSync: (target, path) => {
218+
symlinks.push({ target, path });
219+
},
220+
},
221+
});
222+
223+
const linkPath = win32.join(compiledDir, "__hyperframes_video_frames", "video-1");
224+
expect(extracted.outputDir).toBe(linkPath);
225+
expect(extracted.framePaths.get(0)).toBe(win32.join(linkPath, "frame_000001.jpg"));
226+
expect(extracted.framePaths.get(0)).not.toContain(outputDir);
227+
expect(symlinks).toEqual([{ target: outputDir, path: linkPath }]);
228+
});
229+
});
230+
167231
describe("writeCompiledArtifacts — external assets on Windows drive-letter paths (GH #321)", () => {
168232
const tempDirs: string[] = [];
169233
afterEach(() => {

‎packages/producer/src/services/renderOrchestrator.ts‎

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
type EngineConfig,
3434
resolveConfig,
3535
extractAllVideoFrames,
36+
type ExtractedFrames,
3637
type ExtractionPhaseBreakdown,
3738
createFrameLookupTable,
3839
type VideoElement,
@@ -94,7 +95,7 @@ import {
9495
type ElementStackingInfo,
9596
type HfTransitionMeta,
9697
} from "@hyperframes/engine";
97-
import { join, dirname, resolve, relative, isAbsolute } from "path";
98+
import { join, dirname, resolve, relative, isAbsolute, basename } from "path";
9899
import { randomUUID } from "crypto";
99100
import { freemem } from "os";
100101
import { fileURLToPath } from "url";
@@ -686,6 +687,72 @@ export function createCompiledFrameSrcResolver(
686687
};
687688
}
688689

690+
type MaterializedExtractedFrames = Pick<ExtractedFrames, "videoId" | "outputDir" | "framePaths">;
691+
692+
type MaterializePathModule = {
693+
resolve: (...segments: string[]) => string;
694+
join: (...segments: string[]) => string;
695+
dirname: (path: string) => string;
696+
basename: (path: string) => string;
697+
relative: (from: string, to: string) => string;
698+
isAbsolute: (path: string) => boolean;
699+
};
700+
701+
type MaterializeFileSystem = {
702+
existsSync: (path: string) => boolean;
703+
mkdirSync: (path: string, options: { recursive: true }) => unknown;
704+
symlinkSync: (target: string, path: string) => unknown;
705+
};
706+
707+
type MaterializeExtractedFramesOptions = {
708+
pathModule?: MaterializePathModule;
709+
fileSystem?: MaterializeFileSystem;
710+
};
711+
712+
const materializePathModule: MaterializePathModule = {
713+
resolve,
714+
join,
715+
dirname,
716+
basename,
717+
relative,
718+
isAbsolute,
719+
};
720+
721+
const materializeFileSystem: MaterializeFileSystem = {
722+
existsSync,
723+
mkdirSync,
724+
symlinkSync,
725+
};
726+
727+
export function materializeExtractedFramesForCompiledDir(
728+
extracted: MaterializedExtractedFrames[],
729+
compiledDir: string,
730+
options: MaterializeExtractedFramesOptions = {},
731+
): void {
732+
const pathModule = options.pathModule ?? materializePathModule;
733+
const fileSystem = options.fileSystem ?? materializeFileSystem;
734+
const resolvedCompiledDir = pathModule.resolve(compiledDir);
735+
const compiledFrameRoot = pathModule.join(resolvedCompiledDir, "__hyperframes_video_frames");
736+
737+
for (const ext of extracted) {
738+
const resolvedOut = pathModule.resolve(ext.outputDir);
739+
if (isPathInside(resolvedOut, resolvedCompiledDir, { pathModule })) continue;
740+
741+
const linkPath = pathModule.join(compiledFrameRoot, ext.videoId);
742+
if (!fileSystem.existsSync(linkPath)) {
743+
fileSystem.mkdirSync(pathModule.dirname(linkPath), { recursive: true });
744+
fileSystem.symlinkSync(resolvedOut, linkPath);
745+
}
746+
747+
const remapped = new Map<number, string>();
748+
for (const [idx, framePath] of ext.framePaths) {
749+
remapped.set(idx, pathModule.join(linkPath, pathModule.basename(framePath)));
750+
}
751+
ext.framePaths = remapped;
752+
ext.outputDir = linkPath;
753+
}
754+
}
755+
689756
export function applyRenderModeHints(
690757
cfg: EngineConfig,
691758
compiled: CompiledComposition,
@@ -2295,24 +2362,7 @@ export async function executeRenderJob(
22952362
);
22962363
assertNotAborted();
22972364

2298-
const compiledFrameRoot = join(compiledDir, "__hyperframes_video_frames");
2299-
for (const ext of extractionResult.extracted) {
2300-
const resolvedOut = resolve(ext.outputDir);
2301-
if (!resolvedOut.startsWith(resolve(compiledDir) + "/")) {
2302-
const linkPath = join(compiledFrameRoot, ext.videoId);
2303-
if (!existsSync(linkPath)) {
2304-
mkdirSync(dirname(linkPath), { recursive: true });
2305-
symlinkSync(resolvedOut, linkPath);
2306-
}
2307-
const remapped = new Map<number, string>();
2308-
for (const [idx, framePath] of ext.framePaths) {
2309-
const basename = framePath.slice(framePath.lastIndexOf("/") + 1);
2310-
remapped.set(idx, join(linkPath, basename));
2311-
}
2312-
ext.framePaths = remapped;
2313-
ext.outputDir = linkPath;
2314-
}
2315-
}
2365+
materializeExtractedFramesForCompiledDir(extractionResult.extracted, compiledDir);
23162366

23172367
if (extractionResult.extracted.length > 0) {
23182368
frameLookup = createFrameLookupTable(composition.videos, extractionResult.extracted);

‎packages/producer/src/utils/paths.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313

1414
import { describe, expect, it } from "vitest";
15-
import { resolve } from "node:path";
15+
import { resolve, win32 } from "node:path";
1616

1717
import { isPathInside, toExternalAssetKey } from "./paths.js";
1818

@@ -47,6 +47,16 @@ describe("isPathInside", () => {
4747
it("normalises trailing slashes on parent", () => {
4848
expect(isPathInside(resolve("/foo/bar/baz"), resolve("/foo/bar/"))).toBe(true);
4949
});
50+
51+
it("handles Windows paths under the parent directory", () => {
52+
expect(
53+
isPathInside(
54+
win32.resolve("C:\\compiled\\__hyperframes_video_frames\\video\\frame_000001.jpg"),
55+
win32.resolve("C:\\compiled"),
56+
{ pathModule: win32 },
57+
),
58+
).toBe(true);
59+
});
5060
});
5161

5262
describe("toExternalAssetKey", () => {

‎packages/producer/src/utils/paths.ts‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
* Path resolution utilities for the render pipeline.
33
*/
44

5-
import { resolve, basename, join, relative, isAbsolute } from "node:path";
5+
import {
6+
basename,
7+
join,
8+
resolve as nodeResolve,
9+
relative as nodeRelative,
10+
isAbsolute as nodeIsAbsolute,
11+
} from "node:path";
612

713
export interface RenderPaths {
814
absoluteProjectDir: string;
@@ -11,7 +17,17 @@ export interface RenderPaths {
1117

1218
const DEFAULT_RENDERS_DIR =
1319
process.env.PRODUCER_RENDERS_DIR ??
14-
resolve(new URL(import.meta.url).pathname, "../../..", "renders");
20+
nodeResolve(new URL(import.meta.url).pathname, "../../..", "renders");
21+
22+
type PathModuleLike = {
23+
resolve: (...segments: string[]) => string;
24+
relative: (from: string, to: string) => string;
25+
isAbsolute: (path: string) => boolean;
26+
};
27+
28+
type IsPathInsideOptions = {
29+
pathModule?: PathModuleLike;
30+
};
1531

1632
/**
1733
* Cross-platform containment check.
@@ -25,15 +41,22 @@ const DEFAULT_RENDERS_DIR =
2541
* Both inputs are normalised via `resolve()` so callers don't need to.
2642
* Equality counts as "inside" (a directory contains itself).
2743
*/
28-
export function isPathInside(childPath: string, parentPath: string): boolean {
29-
const absChild = resolve(childPath);
30-
const absParent = resolve(parentPath);
44+
export function isPathInside(
45+
childPath: string,
46+
parentPath: string,
47+
options: IsPathInsideOptions = {},
48+
): boolean {
49+
const resolvePath = options.pathModule?.resolve ?? nodeResolve;
50+
const relativePath = options.pathModule?.relative ?? nodeRelative;
51+
const isPathAbsolute = options.pathModule?.isAbsolute ?? nodeIsAbsolute;
52+
const absChild = resolvePath(childPath);
53+
const absParent = resolvePath(parentPath);
3154
if (absChild === absParent) return true;
32-
const rel = relative(absParent, absChild);
55+
const rel = relativePath(absParent, absChild);
3356
// `relative()` returns "" when paths are equal, ".." or "..\\foo" when child
3457
// is above the parent, and an absolute path when they live on different
3558
// drives/volumes (Windows) — none of which count as "inside".
36-
return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel);
59+
return rel !== "" && !rel.startsWith("..") && !isPathAbsolute(rel);
3760
}
3861

3962
/**
@@ -91,10 +114,10 @@ export function resolveRenderPaths(
91114
outputPath: string | null | undefined,
92115
rendersDir: string = DEFAULT_RENDERS_DIR,
93116
): RenderPaths {
94-
const absoluteProjectDir = resolve(projectDir);
117+
const absoluteProjectDir = nodeResolve(projectDir);
95118
const projectName = basename(absoluteProjectDir);
96119
const resolvedOutputPath = outputPath ?? join(rendersDir, `${projectName}.mp4`);
97-
const absoluteOutputPath = resolve(resolvedOutputPath);
120+
const absoluteOutputPath = nodeResolve(resolvedOutputPath);
98121

99122
return { absoluteProjectDir, absoluteOutputPath };
100123
}

0 commit comments

Comments
 (0)