diff --git a/packages/cli/src/capture/captureFile.ts b/packages/cli/src/capture/captureFile.ts index 211d513afe..25ede18e45 100644 --- a/packages/cli/src/capture/captureFile.ts +++ b/packages/cli/src/capture/captureFile.ts @@ -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[2]; @@ -18,7 +19,7 @@ function publishCaptureFileSync( data: string | NodeJS.ArrayBufferView, options: Exclude, ): 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" }); diff --git a/packages/cli/src/cloud/download.ts b/packages/cli/src/cloud/download.ts index aac21dc149..7992474a03 100644 --- a/packages/cli/src/cloud/download.ts +++ b/packages/cli/src/cloud/download.ts @@ -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; diff --git a/packages/cli/src/commands/measure/frames.ts b/packages/cli/src/commands/measure/frames.ts index de2c6a712d..66ef591a2e 100644 --- a/packages/cli/src/commands/measure/frames.ts +++ b/packages/cli/src/commands/measure/frames.ts @@ -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 diff --git a/packages/cli/src/commands/templates.ts b/packages/cli/src/commands/templates.ts index 282fc5bc27..e8b4d4bba8 100644 --- a/packages/cli/src/commands/templates.ts +++ b/packages/cli/src/commands/templates.ts @@ -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 { 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 })); diff --git a/packages/cli/src/registry/publication.ts b/packages/cli/src/registry/publication.ts index 2e8b3e7617..36ae24615d 100644 --- a/packages/cli/src/registry/publication.ts +++ b/packages/cli/src/registry/publication.ts @@ -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" }); diff --git a/packages/core/src/atomicFile.test.ts b/packages/core/src/atomicFile.test.ts index 6b82fd0b8d..d5cfb891e0 100644 --- a/packages/core/src/atomicFile.test.ts +++ b/packages/core/src/atomicFile.test.ts @@ -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); + }); +}); diff --git a/packages/core/src/atomicFile.ts b/packages/core/src/atomicFile.ts index 96b2611aac..a79103e544 100644 --- a/packages/core/src/atomicFile.ts +++ b/packages/core/src/atomicFile.ts @@ -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); +} diff --git a/packages/core/src/atomicFile.win32Paths.test.ts b/packages/core/src/atomicFile.win32Paths.test.ts new file mode 100644 index 0000000000..38b9a33b69 --- /dev/null +++ b/packages/core/src/atomicFile.win32Paths.test.ts @@ -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("node:path")).win32, +); +vi.mock("node:fs", async (importOriginal) => ({ + ...(await importOriginal()), + 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-`]]); + }); +}); diff --git a/packages/core/src/fonts/fontCompression.ts b/packages/core/src/fonts/fontCompression.ts index 8bf56309fd..668f443860 100644 --- a/packages/core/src/fonts/fontCompression.ts +++ b/packages/core/src/fonts/fontCompression.ts @@ -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; @@ -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); diff --git a/packages/core/src/noBareMkdtemp.test.ts b/packages/core/src/noBareMkdtemp.test.ts new file mode 100644 index 0000000000..5f208b1ae4 --- /dev/null +++ b/packages/core/src/noBareMkdtemp.test.ts @@ -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([]); +}); diff --git a/packages/engine/src/services/aacEncode.ts b/packages/engine/src/services/aacEncode.ts index 31c3d52259..4ad0432419 100644 --- a/packages/engine/src/services/aacEncode.ts +++ b/packages/engine/src/services/aacEncode.ts @@ -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, diff --git a/packages/engine/src/services/audioMixer.ts b/packages/engine/src/services/audioMixer.ts index 8fb5404dc9..bffc0d394d 100644 --- a/packages/engine/src/services/audioMixer.ts +++ b/packages/engine/src/services/audioMixer.ts @@ -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) { diff --git a/packages/engine/src/services/audioVolumeEnvelope.ts b/packages/engine/src/services/audioVolumeEnvelope.ts index ccd3230b84..298f97fb7d 100644 --- a/packages/engine/src/services/audioVolumeEnvelope.ts +++ b/packages/engine/src/services/audioVolumeEnvelope.ts @@ -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); diff --git a/packages/engine/src/services/chunkEncoder.ts b/packages/engine/src/services/chunkEncoder.ts index 86344140a1..b66e1c2f8a 100644 --- a/packages/engine/src/services/chunkEncoder.ts +++ b/packages/engine/src/services/chunkEncoder.ts @@ -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++) { diff --git a/packages/engine/src/utils/urlDownloader.ts b/packages/engine/src/utils/urlDownloader.ts index be12d65597..6493cbf1ce 100644 --- a/packages/engine/src/utils/urlDownloader.ts +++ b/packages/engine/src/utils/urlDownloader.ts @@ -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>(); const signalScopes = new WeakMap(); @@ -1026,7 +1026,7 @@ async function runDownloadAttempt( ): Promise { // 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; diff --git a/packages/producer/src/services/distributed/assemble.ts b/packages/producer/src/services/distributed/assemble.ts index 49fde61b9a..7a69af3133 100644 --- a/packages/producer/src/services/distributed/assemble.ts +++ b/packages/producer/src/services/distributed/assemble.ts @@ -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 = {}; const ctx: JoinContext = { diff --git a/packages/producer/src/services/distributed/mp4Stitch.ts b/packages/producer/src/services/distributed/mp4Stitch.ts index c43ed8e499..e18cb349f4 100644 --- a/packages/producer/src/services/distributed/mp4Stitch.ts +++ b/packages/producer/src/services/distributed/mp4Stitch.ts @@ -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); diff --git a/packages/producer/src/services/distributed/planV2.ts b/packages/producer/src/services/distributed/planV2.ts index 676a67523f..ee1eb872e9 100644 --- a/packages/producer/src/services/distributed/planV2.ts +++ b/packages/producer/src/services/distributed/planV2.ts @@ -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): 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 = {}, ): Promise { - 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("/")); diff --git a/packages/producer/src/services/distributed/planV2Publisher.ts b/packages/producer/src/services/distributed/planV2Publisher.ts index 57e42bb848..cc6a00915d 100644 --- a/packages/producer/src/services/distributed/planV2Publisher.ts +++ b/packages/producer/src/services/distributed/planV2Publisher.ts @@ -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): Promise { @@ -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 { diff --git a/packages/producer/src/services/render/audioPadTrim.ts b/packages/producer/src/services/render/audioPadTrim.ts index cbe9b45a5a..a743d26e78 100644 --- a/packages/producer/src/services/render/audioPadTrim.ts +++ b/packages/producer/src/services/render/audioPadTrim.ts @@ -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; diff --git a/packages/producer/src/services/render/renderDirOwner.ts b/packages/producer/src/services/render/renderDirOwner.ts index 6a36df4739..f06963531a 100644 --- a/packages/producer/src/services/render/renderDirOwner.ts +++ b/packages/producer/src/services/render/renderDirOwner.ts @@ -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; diff --git a/packages/producer/src/services/render/stages/captureHdrResources.ts b/packages/producer/src/services/render/stages/captureHdrResources.ts index c76c53df94..183eecb809 100644 --- a/packages/producer/src/services/render/stages/captureHdrResources.ts +++ b/packages/producer/src/services/render/stages/captureHdrResources.ts @@ -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; diff --git a/packages/studio-server/src/helpers/waveform.ts b/packages/studio-server/src/helpers/waveform.ts index 2ade637868..07376d0376 100644 --- a/packages/studio-server/src/helpers/waveform.ts +++ b/packages/studio-server/src/helpers/waveform.ts @@ -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" });