fix(cli): staging folders beside a project path no longer fail past 260 characters on Windows (#7338)

* fix(cli): staging folders beside a project path no longer fail past 260 characters on Windows

mkdtemp does not get the long-path prefix that mkdir does, so every staging folder
made beside a project or output path failed once its full path passed 260 characters.
They now come from one helper, createStagingDir in @hyperframes/core/atomic-file, and a
scan test fails on a new mkdtemp beside a path.

* fix(cli): give mkdtemp the long-path form instead of replacing it

CodeQL treats mkdtemp as the safe way to make a folder under the system temp dir and
flagged every file written into a folder from the mkdir-based helper. createStagingDir
now calls mkdtemp with the long-path form of the path on Windows, so both hold.

* fix(cli): leave non-Windows paths alone when making a staging folder

The long-path form is now chosen by the shape of the path, not process.platform,
so tests that pretend to be Windows keep working and the comment share does not rise.

* fix(core): a staging folder keeps a Windows path that already names its namespace

* fix(core): a staging folder keeps a Windows device path as the caller gave it

* fix(core): the staging folder helper's comment fits the line limit
This commit is contained in:
Miguel Ángel
2026-11-09 17:16:08 +00:00
committed by GitHub
parent c149730819
commit b59fa20833
23 changed files with 206 additions and 60 deletions
+3 -2
View File
@@ -1,7 +1,8 @@
import { mkdirSync, mkdtempSync, realpathSync, renameSync, statSync, writeFileSync } from "node:fs";
import { mkdirSync, realpathSync, renameSync, statSync, writeFileSync } from "node:fs";
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
import { CaptureDirRefusedError } from "./captureErrors.js";
import { removeTreeBestEffort } from "@hyperframes/core/remove-tree";
import { createStagingDir } from "@hyperframes/core/atomic-file";
type WriteOptions = Parameters<typeof writeFileSync>[2];
@@ -18,7 +19,7 @@ function publishCaptureFileSync(
data: string | NodeJS.ArrayBufferView,
options: Exclude<WriteOptions, string>,
): void {
const stagingDir = mkdtempSync(join(dirname(path), ".capture-"));
const stagingDir = createStagingDir(dirname(path), ".capture-");
try {
const staged = join(stagingDir, basename(path));
writeCaptureFileSync(staged, data, { ...options, flag: "wx" });
+3 -3
View File
@@ -13,12 +13,12 @@
* existing destination is replaced only after the download succeeds.
*/
import { chmodSync, mkdtempSync, renameSync, rmSync, statSync } from "node:fs";
import { chmodSync, renameSync, rmSync, statSync } from "node:fs";
import { makeOutputFolder } from "../utils/outputFolder.js";
import { open } from "node:fs/promises";
import { dirname, join } from "node:path";
import { setTimeout as sleep } from "node:timers/promises";
import { resolveWritePath } from "@hyperframes/core/atomic-file";
import { createStagingDir, resolveWritePath } from "@hyperframes/core/atomic-file";
// The first request asks for PART_BYTES; a larger file is split into up to
// CONNECTIONS ranged GETs. A server that ignores Range answers 200 and is streamed.
@@ -79,7 +79,7 @@ export async function downloadToFile(
// Writes through a caller-selected symlink, as before.
const destination = resolveWritePath(destPath);
const previous = statSync(destination, { throwIfNoEntry: false });
const stage = mkdtempSync(join(dirname(destination), ".hf-download-"));
const stage = createStagingDir(dirname(destination), ".hf-download-");
const stagedFile = join(stage, "download");
let bytes = 0;
let firstError: unknown;
+3 -2
View File
@@ -1,4 +1,4 @@
import { mkdtempSync, readdirSync, renameSync, rmSync } from "node:fs";
import { readdirSync, renameSync, rmSync } from "node:fs";
import { join } from "node:path";
import { defineCommand } from "../../utils/defineCommand.js";
import type { Example } from "../_examples.js";
@@ -28,6 +28,7 @@ import {
type Roi,
type VideoInfo,
} from "../../measure/media.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
export const examples: Example[] = [
["An overview sheet, one frame a second", "hyperframes measure frames ref.mp4 --every 1"],
@@ -84,7 +85,7 @@ function writeFrames(
prefix: string,
): string[] {
// Beside the output, so the move below stays on one filesystem (a container's /tmp often is not the project's).
const work = mkdtempSync(join(dir, ".frames-"));
const work = createStagingDir(dir, ".frames-");
try {
const raw = Buffer.concat(
f.frames
+2 -2
View File
@@ -6,7 +6,6 @@ import {
existsSync,
fstatSync,
mkdirSync,
mkdtempSync,
openSync,
readFileSync,
readdirSync,
@@ -27,6 +26,7 @@ import { fetchExplained, NetworkFailure } from "../utils/networkFailure.js";
import { defineCommand } from "../utils/defineCommand.js";
import { projectConfigPath, seedProjectAuthoringSkill } from "../utils/projectConfig.js";
import { trackSkillCommandRan } from "../telemetry/events.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
const STYLE_SAMPLING = "style-sampling";
@@ -194,7 +194,7 @@ function removeAbandonedParts(dest: string): void {
async function unpack(data: Buffer, dest: string, pack: TemplatePack): Promise<void> {
makeOutputFolder(dirname(dest));
removeAbandonedParts(dest);
const work = mkdtempSync(join(dirname(dest), `${partPrefix(dest)}${process.pid}-`));
const work = createStagingDir(dirname(dest), `${partPrefix(dest)}${process.pid}-`);
const stopWatching = removeOnStop(work);
try {
writeFileSync(join(work, PULL_MARKER), JSON.stringify({ owner: PULL_OWNER, pid: process.pid }));
+3 -2
View File
@@ -1,7 +1,8 @@
import { lstatSync, chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { lstatSync, chmodSync, rmSync, writeFileSync } from "node:fs";
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
import { mkdirWithinProject, realpath } from "@hyperframes/core";
import { renameWithRetry } from "@hyperframes/core/rename-retry";
import { createStagingDir } from "@hyperframes/core/atomic-file";
export { realProjectRoot as registryRoot } from "@hyperframes/core";
@@ -47,7 +48,7 @@ export function publishRegistryFile(
const parent = realpath(dirname(path));
assertContained(root, parent);
const destination = resolve(parent, basename(path));
const stage = mkdtempSync(join(parent, ".hf-install-"));
const stage = createStagingDir(parent, ".hf-install-");
try {
const stagedFile = join(stage, "file");
writeFileSync(stagedFile, bytes, { flag: "wx" });
+39 -1
View File
@@ -4,8 +4,9 @@ import * as crypto from "node:crypto";
import * as fs from "node:fs";
import { lstatSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { basename, join } from "node:path";
import {
createStagingDir,
atomicTempPath,
createFileAtomically,
isAtomicTempPath,
@@ -544,3 +545,40 @@ describe("HF_WRITE_TRACE", () => {
}
});
});
describe("createStagingDir", () => {
const dirs: string[] = [];
const root = () => {
const dir = mkdtempSync(join(tmpdir(), "hf-staging-"));
dirs.push(dir);
return dir;
};
afterEach(() => {
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
it("makes a fresh private directory named like mkdtemp's: the prefix and 6 letters or digits", () => {
const parent = root();
const first = createStagingDir(parent, ".stage-");
const second = createStagingDir(parent, ".stage-");
expect(first).not.toBe(second);
expect(first).toBe(join(parent, basename(first)));
expect(basename(first)).toMatch(/^\.stage-[A-Za-z0-9]{6}$/);
expect(fs.statSync(first).isDirectory()).toBe(true);
if (process.platform !== "win32") expect(fs.statSync(first).mode & 0o777).toBe(0o700);
});
it("does not create the parent", () => {
expect(() => createStagingDir(join(root(), "missing"), ".s-")).toThrow(/ENOENT/);
});
it.runIf(process.platform === "win32")("works when the full path passes 260 characters", () => {
let parent = root();
while (parent.length < 280) parent = join(parent, "d".repeat(40));
fs.mkdirSync(parent, { recursive: true });
const dir = createStagingDir(parent, ".stage-");
expect(dir.length).toBeGreaterThan(260);
expect(dir.startsWith("\\\\?\\")).toBe(false);
expect(fs.statSync(dir).isDirectory()).toBe(true);
});
});
+27
View File
@@ -175,3 +175,30 @@ function writeTempSibling(
function errorCode(error: unknown): string {
return String((error as NodeJS.ErrnoException)?.code);
}
const WIN32_LONG_PREFIX = "\\\\?\\";
const WIN32_ABSOLUTE = /^([A-Za-z]:\\|\\\\)/;
const WIN32_DOS_DEVICE = /^\\\\[.?]\\/;
function toWin32LongPath(path: string): string {
const full = resolve(path);
if (!WIN32_ABSOLUTE.test(full) || WIN32_DOS_DEVICE.test(full)) return path;
return full.startsWith("\\\\")
? `${WIN32_LONG_PREFIX}UNC\\${full.slice(2)}`
: `${WIN32_LONG_PREFIX}${full}`;
}
/** `prefix` + 6 random characters in `parent`, made private; Windows past 260 characters needs the long path. */
export function createStagingDir(parent: string, prefix: string): string {
const requested = join(parent, prefix);
const long = toWin32LongPath(requested);
const created = fs.mkdtempSync(long);
return long === requested ? created : withoutAddedLongPrefix(created);
}
function withoutAddedLongPrefix(path: string): string {
const unc = `${WIN32_LONG_PREFIX}UNC\\`;
return path.startsWith(unc)
? `\\\\${path.slice(unc.length)}`
: path.slice(WIN32_LONG_PREFIX.length);
}
@@ -0,0 +1,33 @@
// @vitest-environment node
import { describe, expect, it, vi } from "vitest";
import * as fs from "node:fs";
vi.mock(
"node:path",
async () => (await vi.importActual<typeof import("node:path")>("node:path")).win32,
);
vi.mock("node:fs", async (importOriginal) => ({
...(await importOriginal<typeof import("node:fs")>()),
mkdtempSync: vi.fn((prefix: string) => `${prefix}ABC123`),
}));
const { createStagingDir } = await import("./atomicFile");
describe("createStagingDir with Windows parents", () => {
it.each([
["a volume-GUID parent", "\\\\?\\Volume{11111111-1111-1111-1111-111111111111}\\project", ""],
["an already namespaced UNC parent", "\\\\?\\unc\\server\\share\\project", ""],
["a device drive parent", "\\\\.\\C:\\project", ""],
[
"a device volume-GUID parent",
"\\\\.\\Volume{11111111-1111-1111-1111-111111111111}\\project",
"",
],
["a drive parent", "C:\\project", "\\\\?\\C:\\project"],
["a UNC parent", "\\\\server\\share\\project", "\\\\?\\UNC\\server\\share\\project"],
])("returns the folder it made for %s", (_name, parent, longForm) => {
vi.mocked(fs.mkdtempSync).mockClear();
expect(createStagingDir(parent, ".stage-")).toBe(`${parent}\\.stage-ABC123`);
expect(vi.mocked(fs.mkdtempSync).mock.calls).toEqual([[`${longForm || parent}\\.stage-`]]);
});
});
+3 -10
View File
@@ -1,18 +1,11 @@
// @ts-expect-error -- wawoff2 ships no type declarations
import wawoff2 from "wawoff2";
import { createHash } from "node:crypto";
import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
renameSync,
rmSync,
writeFileSync,
} from "node:fs";
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { hyperframesCacheDir } from "../cacheDir.js";
import { createStagingDir } from "../atomicFile.js";
const { compress } = wawoff2 as {
compress: (input: Buffer | Uint8Array) => Promise<Uint8Array>;
@@ -72,7 +65,7 @@ function cacheCompression(path: string, compressed: Buffer): void {
mkdirSync(dirname(path), { recursive: true });
// Keep the write on the cache filesystem for an atomic rename, inside a
// private directory that this invocation owns.
tempDir = mkdtempSync(join(dirname(path), ".compression-"));
tempDir = createStagingDir(dirname(path), ".compression-");
const tmpPath = join(tempDir, "font.woff2");
writeFileSync(tmpPath, compressed, { flag: "wx", mode: 0o644 });
renameSync(tmpPath, path);
+48
View File
@@ -0,0 +1,48 @@
import { readdirSync, readFileSync, statSync } from "node:fs";
import { dirname, join, relative } from "node:path";
import { fileURLToPath } from "node:url";
import { expect, it } from "vitest";
const packagesDir = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
const SOURCE = /\.(ts|mjs)$/;
const NOT_SOURCE = /\.(test|fixture)\.|[\\/](tests?|node_modules|dist)[\\/]/;
const MKDTEMP = /\bmkdtemp(Sync)?\(/;
// Not beside a project or output path: a system temp root, a standalone skill script, a cloud function's scratch root.
const ALLOWED = new Set([
"core/src/atomicFile.ts",
"cli/src/media-use/lib/lut-preset-provider.mjs",
"producer/src/server.ts",
"producer/src/regression-harness.ts",
"aws-lambda/src/s3PlanV2Publisher.ts",
"gcp-cloud-run/src/gcsPlanV2Publisher.ts",
]);
function sources(dir: string): string[] {
return readdirSync(dir).flatMap((name) => {
const path = join(dir, name);
if (NOT_SOURCE.test(path)) return [];
return statSync(path).isDirectory() ? sources(path) : SOURCE.test(name) ? [path] : [];
});
}
it("no package makes a staging folder beside a project path with mkdtemp, which fails past 260 characters on Windows", () => {
const offenders = readdirSync(packagesDir).flatMap((pkg) => {
const src = join(packagesDir, pkg, "src");
try {
statSync(src);
} catch {
return [];
}
return sources(src).flatMap((file) => {
const rel = relative(packagesDir, file).split("\\").join("/");
if (ALLOWED.has(rel)) return [];
return readFileSync(file, "utf8")
.split("\n")
.flatMap((line, i) =>
MKDTEMP.test(line) && !/tmpdir|^\s*(\/\/|\*)/.test(line) ? [`${rel}:${i + 1}`] : [],
);
});
});
expect(offenders, "use createStagingDir from @hyperframes/core/atomic-file").toEqual([]);
});
+3 -2
View File
@@ -4,11 +4,12 @@
*/
import { execFile } from "node:child_process";
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { readFileSync, writeFileSync } from "node:fs";
import { removeScratch } from "@hyperframes/core/remove-scratch";
import { dirname, join } from "node:path";
import { getFfmpegBinary } from "../utils/ffmpegBinaries.js";
import { runFfmpeg, type RunFfmpegResult } from "../utils/runFfmpeg.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
const SAMPLE_RATE = 48_000;
const FRAME_SAMPLES = 1024;
@@ -107,7 +108,7 @@ export async function encodeAac(
if (!(await supportsSettsDuration())) {
return encodeInOnePass(input, outputPath, { signal, timeout, filter });
}
const dir = mkdtempSync(join(dirname(outputPath), ".aac-segments-"));
const dir = createStagingDir(dirname(outputPath), ".aac-segments-");
try {
const segments = planSegments(totalDuration, count).map((segment, k) => ({
...segment,
+7 -6
View File
@@ -1,6 +1,6 @@
// fallow-ignore-file complexity code-duplication
import { isSelfOrAncestorHidden, memberGroupKey, isMemberGroupHidden } from "./mediaHidden.js";
import { closeSync, existsSync, mkdirSync, mkdtempSync, openSync, rmSync, writeFileSync } from "fs";
import { closeSync, existsSync, mkdirSync, openSync, rmSync, writeFileSync } from "fs";
import { renameWithRetry } from "@hyperframes/core/rename-retry";
import { removeScratch } from "@hyperframes/core/remove-scratch";
import { join, dirname, isAbsolute, relative } from "path";
@@ -73,6 +73,7 @@ import {
import { readMasterFxChain, resolveAudioGroups } from "@hyperframes/core/audio-groups";
import { applyAudioFxChain, AudioFxRenderError } from "./audioFxRender.js";
import type { AudioVolumeKeyframe } from "./audioMixer.types.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
export type { AudioElement, MixResult } from "./audioMixer.types.js";
@@ -716,7 +717,7 @@ async function extractAudioSegment(
});
const loopDuration = (metadata?.durationSeconds ?? 0) - mediaStart;
if (options?.loop && loopDuration > 0 && sourceDuration > loopDuration) {
const cycleDir = mkdtempSync(join(outputDir, ".audio-loop-"));
const cycleDir = createStagingDir(outputDir, ".audio-loop-");
const cyclePath = join(cycleDir, "cycle.wav");
try {
const cycle = await extractAudioSegment(
@@ -975,7 +976,7 @@ async function mixAudioTracksFlat(
const runMix = async (ignoreAutomation: boolean, useNormalize: boolean) => {
const inputs: string[] = [];
tracks.forEach((track) => inputs.push("-i", track.srcPath));
const scriptDir = mkdtempSync(join(outputDir, ".filter-complex-"));
const scriptDir = createStagingDir(outputDir, ".filter-complex-");
const scriptPath = join(scriptDir, "graph.txt");
const fd = openSync(scriptPath, "wx", 0o600);
try {
@@ -1150,7 +1151,7 @@ async function mixGroupMembersFlat(
// global track count.
`${mixInputs}amix=inputs=${memberTracks.length}:duration=longest:dropout_transition=0[mixed];[mixed]volume=${formatFilterNumber(memberTracks.length)}[out]`;
const filterComplex = [...inputFilters, mixFilter].join(";");
const scriptDir = mkdtempSync(join(outputDir, ".group-filter-complex-"));
const scriptDir = createStagingDir(outputDir, ".group-filter-complex-");
const scriptPath = join(scriptDir, "graph.txt");
const fd = openSync(scriptPath, "wx", 0o600);
try {
@@ -1297,7 +1298,7 @@ async function mixGroupMembers(
return mixGroupMembersFlat(memberTracks, outputPath, totalDuration, signal, config);
}
mkdirSync(dirname(outputPath), { recursive: true });
const scratchDir = mkdtempSync(join(dirname(outputPath), ".mix-batches-"));
const scratchDir = createStagingDir(dirname(outputPath), ".mix-batches-");
try {
const summed = await sumInSlices(memberTracks, scratchDir, totalDuration, signal, config);
if (!summed.success) return summed;
@@ -1319,7 +1320,7 @@ async function mixAudioTracks(
return mixAudioTracksFlat(tracks, outputPath, totalDuration, signal, config);
}
mkdirSync(dirname(outputPath), { recursive: true });
const scratchDir = mkdtempSync(join(dirname(outputPath), ".mix-batches-"));
const scratchDir = createStagingDir(dirname(outputPath), ".mix-batches-");
try {
const summed = await sumInSlices(tracks, scratchDir, totalDuration, signal, config);
if (!summed.success) {
@@ -15,12 +15,13 @@
* the caller can fall back to the expression path rather than corrupting audio.
*/
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs";
import { readFileSync, rmSync, writeFileSync } from "fs";
import { renameWithRetry } from "@hyperframes/core/rename-retry";
import { dirname, join } from "path";
import type { AudioVolumeKeyframe } from "./audioMixer.types.js";
import { normaliseEnvelope } from "@hyperframes/core/media-volume-envelope";
import { riffChunks, wavFormatTag } from "./wavChunks.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
const PCM_FORMAT = 1; // WAVE_FORMAT_PCM
const FLOAT_FORMAT = 3; // WAVE_FORMAT_IEEE_FLOAT
@@ -177,7 +178,7 @@ export function applyVolumeEnvelopeToWav(
// A private sibling directory owns the staging file; the same-filesystem
// rename keeps a failed write from truncating the original WAV.
stagingDir = mkdtempSync(join(dirname(wavPath), ".hf-volume-"));
stagingDir = createStagingDir(dirname(wavPath), ".hf-volume-");
const tempPath = join(stagingDir, "audio.wav");
writeFileSync(tempPath, buffer, { flag: "wx" });
renameWithRetry(tempPath, wavPath);
+2 -2
View File
@@ -13,7 +13,6 @@ import {
existsSync,
ftruncateSync,
mkdirSync,
mkdtempSync,
openSync,
readFileSync,
readdirSync,
@@ -50,6 +49,7 @@ import type { EncoderOptions, EncodeResult, MuxResult } from "./chunkEncoder.typ
import { appendVp9CpuUsedArg } from "./vp9Options.js";
import { appendSoftwareEncoderThreadsArg } from "./softwareEncoderThreads.js";
import { appendRenderProvenanceArgs } from "../utils/renderProvenance.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
export type { EncoderOptions, EncodeResult, MuxResult } from "./chunkEncoder.types.js";
@@ -764,7 +764,7 @@ export async function encodeFramesChunkedConcat(
mkdirSync(dirname(outputPath), { recursive: true });
// Keep intermediates under the caller-owned output directory for its existing
// cleanup/debug policy, but never reuse another invocation's chunk files.
const chunkDir = mkdtempSync(join(dirname(outputPath), "chunk-encode-"));
const chunkDir = createStagingDir(dirname(outputPath), "chunk-encode-");
const chunkPaths: string[] = [];
for (let i = 0; i < chunkCount; i++) {
+2 -2
View File
@@ -5,7 +5,6 @@ import {
existsSync,
fsyncSync,
linkSync,
mkdtempSync,
mkdirSync,
lstatSync,
openSync,
@@ -24,6 +23,7 @@ import { isBlockedNetworkHost } from "./networkPolicy.js";
import { dirname, extname, join } from "path";
import { Readable, Transform } from "stream";
import { pipeline } from "stream/promises";
import { createStagingDir } from "@hyperframes/core/atomic-file";
const inFlightDownloads = new Map<string, Promise<string>>();
const signalScopes = new WeakMap<AbortSignal, number>();
@@ -1026,7 +1026,7 @@ async function runDownloadAttempt(
): Promise<string> {
// A private, unguessable directory prevents symlink planting and keeps the
// partial on the destination filesystem so the final rename stays atomic.
const attemptDir = mkdtempSync(join(dirname(localPath), ".hf-download-"));
const attemptDir = createStagingDir(dirname(localPath), ".hf-download-");
const partialPath = join(attemptDir, "payload");
const controller = new AbortController();
let timedOut = false;
@@ -28,14 +28,13 @@ import {
cpSync,
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
statSync,
writeFileSync,
} from "node:fs";
import { dirname, join } from "node:path";
import { basename, dirname, join } from "node:path";
import {
appendRateCapArgs,
appendRenderProvenanceArgs,
@@ -62,6 +61,7 @@ import { DISTRIBUTED_RENDER_CAPABILITIES, readPlanProtocolV1 } from "./planProto
import { writeStitchedMp4 } from "./mp4Stitch.js";
import { validatePlanV2MaterializedTarget } from "./planV2.js";
import type { DistributedFormat } from "./shared.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
/**
* Result of {@link assemble}. `fileSize` reflects the final file on disk
@@ -179,7 +179,7 @@ export async function assemble(
if (!existsSync(dirname(outputPath))) {
mkdirSync(dirname(outputPath), { recursive: true });
}
const workDir = mkdtempSync(`${outputPath}.assemble-work-`);
const workDir = createStagingDir(dirname(outputPath), `${basename(outputPath)}.assemble-work-`);
const fps = { num: plan.dimensions.fpsNum, den: plan.dimensions.fpsDen };
const stageMs: Record<string, number> = {};
const ctx: JoinContext = {
@@ -2,12 +2,13 @@
// 64-bit mdat header); the payloads follow it unchanged and in order (a separate audio file's
// first, then the chunks'), so storage can copy them into place.
import { createReadStream, createWriteStream, mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { createReadStream, createWriteStream, mkdirSync, rmSync } from "node:fs";
import { open } from "node:fs/promises";
import { renameWithRetry } from "@hyperframes/engine";
import { dirname, join } from "node:path";
import { basename, dirname, join } from "node:path";
import type { Writable } from "node:stream";
import { finished, pipeline } from "node:stream/promises";
import { createStagingDir } from "@hyperframes/core/atomic-file";
// ── Box reading ─────────────────────────────────────────────────────────────
@@ -494,7 +495,7 @@ export async function writeStitchedMp4(
? buildStitchedHeader(layouts.slice(1), layouts[0])
: buildStitchedHeader(layouts);
mkdirSync(dirname(outputPath), { recursive: true });
const workDir = mkdtempSync(`${outputPath}.stitch-`);
const workDir = createStagingDir(dirname(outputPath), `${basename(outputPath)}.stitch-`);
const out = createWriteStream(join(workDir, "out.mp4"));
try {
out.write(header);
@@ -21,7 +21,6 @@ import {
existsSync,
lstatSync,
mkdirSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
@@ -69,6 +68,7 @@ import {
PlanVideosMetadataError,
type PlanVideosJson,
} from "./shared.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
const PLAN_V2_HASH_PREFIX = "hyperframes-plan-manifest-hash-v2\x00";
// The engine's content-addressed extraction cache keeps this ownership marker
@@ -508,7 +508,7 @@ function computeManifestHash(manifest: Omit<PlanV2Manifest, "planHash">): string
function writeBlob(sourcePath: string, destinationPath: string): void {
if (existsSync(destinationPath)) return;
mkdirSync(dirname(destinationPath), { recursive: true });
const temporaryDir = mkdtempSync(join(dirname(destinationPath), ".plan-v2-blob-"));
const temporaryDir = createStagingDir(dirname(destinationPath), ".plan-v2-blob-");
const temporaryPath = join(temporaryDir, "blob");
try {
copyFileSync(sourcePath, temporaryPath);
@@ -673,7 +673,7 @@ export function createPlanV2FromExecutionPlan(
throw new PlanV2IntegrityError("a local plan v2 directory cannot reference remote files");
}
mkdirSync(dirname(planV2Dir), { recursive: true });
const tempDir = mkdtempSync(join(dirname(planV2Dir), ".plan-v2-build-"));
const tempDir = createStagingDir(dirname(planV2Dir), ".plan-v2-build-");
try {
for (const blob of publication.blobs) {
writeBlob(blob.sourcePath, planV2BlobPath(tempDir, blob.sha256));
@@ -764,7 +764,7 @@ export async function planV2WithPublisher(
publisher: PlanV2ArtifactPublisher,
options: Readonly<PlanV2WithPublisherOptions> = {},
): Promise<PlanV2Manifest> {
const stagingRoot = mkdtempSync(join(options.stagingParentDir ?? tmpdir(), ".execution-plan-"));
const stagingRoot = createStagingDir(options.stagingParentDir ?? tmpdir(), ".execution-plan-");
const digests: PlanFileDigests = new Map();
try {
await buildLocalExecutionPlan(projectDir, config, stagingRoot, {
@@ -1066,7 +1066,7 @@ export function materializePlanV2Target(
sourcePath: verifyBlob(planV2Dir, artifact),
}));
mkdirSync(dirname(destinationDir), { recursive: true });
const tempDir = mkdtempSync(join(dirname(destinationDir), ".plan-v2-materialize-"));
const tempDir = createStagingDir(dirname(destinationDir), ".plan-v2-materialize-");
try {
for (const { artifact, sourcePath } of verified) {
const destinationPath = join(tempDir, ...artifact.path.split("/"));
@@ -3,7 +3,6 @@ import {
existsSync,
linkSync,
mkdirSync,
mkdtempSync,
rmSync,
statSync,
writeFileSync,
@@ -12,6 +11,7 @@ import { renameWithRetry } from "@hyperframes/engine";
import { dirname, join } from "node:path";
import { PlanV2IntegrityError } from "./planV2Errors.js";
import { assertPlanV2Sha256, planV2BlobPath } from "./planV2Layout.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
export interface PlanV2PublishBlob {
readonly sourcePath: string;
@@ -88,7 +88,7 @@ export class LocalPlanV2ArtifactPublisher implements PlanV2ArtifactPublisher {
this.destinationDir = destinationDir;
this.#linkFile = options.linkFile ?? linkSync;
mkdirSync(dirname(destinationDir), { recursive: true });
this.temporaryDir = mkdtempSync(join(dirname(destinationDir), ".plan-v2-publish-"));
this.temporaryDir = createStagingDir(dirname(destinationDir), ".plan-v2-publish-");
}
async putBlob(blob: Readonly<PlanV2PublishBlob>): Promise<void> {
@@ -102,7 +102,7 @@ export class LocalPlanV2ArtifactPublisher implements PlanV2ArtifactPublisher {
const destinationPath = planV2BlobPath(this.temporaryDir, digest);
if (existsSync(destinationPath)) return;
mkdirSync(dirname(destinationPath), { recursive: true });
const stagingDir = mkdtempSync(join(dirname(destinationPath), ".plan-v2-blob-"));
const stagingDir = createStagingDir(dirname(destinationPath), ".plan-v2-blob-");
const temporaryPath = join(stagingDir, "blob");
try {
try {
@@ -20,7 +20,6 @@
*/
import { spawn } from "node:child_process";
import { mkdtempSync } from "node:fs";
import { dirname, join } from "node:path";
import {
DEFAULT_CONFIG,
@@ -38,6 +37,7 @@ import {
} from "@hyperframes/engine";
import { redactKnownPaths, redactTelemetryString } from "@hyperframes/core";
import { measureTruePeakDbfs } from "./audioTruePeak.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
/**
* Tolerance used to decide whether an audio file is already short enough to
@@ -467,7 +467,7 @@ async function enforceAacTruePeak(input: EnforceAacTruePeakInput): Promise<{
}> {
let scratchDir: string | undefined;
try {
scratchDir = mkdtempSync(join(dirname(input.audioPath), ".true-peak-"));
scratchDir = createStagingDir(dirname(input.audioPath), ".true-peak-");
const correctedPath = join(scratchDir, "audio.m4a");
const measurements: string[] = [];
let attenuationDb = 0;
@@ -1,7 +1,6 @@
import {
existsSync,
lstatSync,
mkdtempSync,
readFileSync,
readdirSync,
readlinkSync,
@@ -9,8 +8,9 @@ import {
writeFileSync,
} from "node:fs";
import { hostname, uptime } from "node:os";
import { dirname, join } from "node:path";
import { basename, dirname, join } from "node:path";
import { RENDER_TEMP_DIR } from "@hyperframes/core/project-folders";
import { createStagingDir } from "@hyperframes/core/atomic-file";
const OWNER_FILE = "owner.json";
/** Where a staging dir parks the previous output while swapping in the new one. */
@@ -42,7 +42,7 @@ function pidScope(): { host: string; boot?: string; pidns?: string } {
/** A render's private temp dir, stamped with the process that owns it so a later sweep can tell it was abandoned. */
export function createOwnedRenderDir(prefix: string): string {
const dir = mkdtempSync(prefix);
const dir = createStagingDir(dirname(prefix), basename(prefix));
const owner = { pid: process.pid, ...pidScope() };
writeFileSync(join(dir, OWNER_FILE), JSON.stringify(owner));
return dir;
@@ -16,8 +16,7 @@
* exist, leaving the caller with empty maps that the hot loop tolerates.
*/
import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, statfsSync } from "node:fs";
import { join } from "node:path";
import { mkdirSync, readFileSync, rmSync, statSync, statfsSync } from "node:fs";
import {
type CaptureSession,
decodePngToRgb48le,
@@ -46,6 +45,7 @@ import type { HdrDiagnostics, RenderJob } from "../../renderOrchestrator.js";
import type { CompositionMetadata } from "../shared.js";
import { createHdrFrameWindows, type HdrFrameWindows } from "../../hdrFrameWindows.js";
import { encoderFailureError } from "../encoderInterruption.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
function tempDirSafePrefix(id: string): string {
const safe = id.replace(/[^A-Za-z0-9_.-]/g, "_").slice(0, 80);
@@ -464,7 +464,7 @@ export async function extractHdrVideoFrames(args: {
if (!video || !planned) continue;
const { window, metadata } = planned;
mkdirSync(framesDir, { recursive: true });
const frameDir = mkdtempSync(join(framesDir, `hdr_${tempDirSafePrefix(videoId)}-`));
const frameDir = createStagingDir(framesDir, `hdr_${tempDirSafePrefix(videoId)}-`);
createdFrameDirs.add(frameDir);
const dims = prep.hdrExtractionDims.get(videoId) ?? { width, height };
const frameSize = dims.width * dims.height * 6;
@@ -4,7 +4,6 @@ import {
writeFileSync,
mkdirSync,
lstatSync,
mkdtempSync,
renameSync,
rmSync,
statSync,
@@ -12,6 +11,7 @@ import {
import { dirname, join } from "node:path";
import { findFfBinary } from "@hyperframes/parsers/ff-binaries";
import { probeAudioTrack } from "./mediaMetadata.js";
import { createStagingDir } from "@hyperframes/core/atomic-file";
const SAMPLE_RATE = 4000;
const PEAK_COUNT = 4000;
@@ -132,7 +132,7 @@ export function writeWaveformCache(cachePath: string, peaks: number[]): void {
if (!isWaveformCacheDirectory(cacheDir)) {
throw new Error("Waveform cache must be a directory, not a symlink");
}
const stagingDir = mkdtempSync(join(cacheDir, ".waveform-"));
const stagingDir = createStagingDir(cacheDir, ".waveform-");
try {
const stagingPath = join(stagingDir, "peaks.json");
writeFileSync(stagingPath, JSON.stringify(peaks), { flag: "wx" });