Skip to content

Commit a6b0384

Browse files
committed
fix: staging folders beside a project path no longer fail past 260 characters on Windows
1 parent eebf9fe commit a6b0384

21 files changed

Lines changed: 143 additions & 119 deletions

File tree

‎packages/cli/src/capture/captureFile.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mkdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
22
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
33
import { CaptureDirRefusedError } from "./captureErrors.js";
4-
import { createStagingDir } from "../utils/stagingDir.js";
4+
import { createStagingDir } from "@hyperframes/core/atomic-file";
55

66
type WriteOptions = Parameters<typeof writeFileSync>[2];
77

‎packages/cli/src/cloud/download.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { open } from "node:fs/promises";
1818
import { dirname, join } from "node:path";
1919
import { setTimeout as sleep } from "node:timers/promises";
2020
import { resolveWritePath } from "@hyperframes/core/atomic-file";
21-
import { createStagingDir } from "../utils/stagingDir.js";
21+
import { createStagingDir } from "@hyperframes/core/atomic-file";
2222

2323
// The first request asks for PART_BYTES; a larger file is split into up to
2424
// CONNECTIONS ranged GETs. A server that ignores Range answers 200 and is streamed.

‎packages/cli/src/registry/publication.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { lstatSync, chmodSync, renameSync, rmSync, writeFileSync } from "node:fs";
22
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
33
import { mkdirWithinProject, realpath } from "@hyperframes/core";
4-
import { createStagingDir } from "../utils/stagingDir.js";
4+
import { createStagingDir } from "@hyperframes/core/atomic-file";
55

66
export { realProjectRoot as registryRoot } from "@hyperframes/core";
77

‎packages/cli/src/utils/stagingDir.test.ts‎

Lines changed: 0 additions & 51 deletions
This file was deleted.

‎packages/cli/src/utils/stagingDir.ts‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎packages/core/src/atomicFile.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { lstatSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSyn
66
import { tmpdir } from "node:os";
77
import { join } from "node:path";
88
import {
9+
createStagingDir,
910
atomicTempPath,
1011
createFileAtomically,
1112
isAtomicTempPath,
@@ -510,3 +511,34 @@ describe.skipIf(process.platform === "win32")("resolveWritePath", () => {
510511
);
511512
});
512513
});
514+
515+
describe("createStagingDir", () => {
516+
const dirs: string[] = [];
517+
const root = () => {
518+
const dir = mkdtempSync(join(tmpdir(), "hf-staging-"));
519+
dirs.push(dir);
520+
return dir;
521+
};
522+
afterEach(() => {
523+
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
524+
});
525+
526+
it("makes a fresh private directory with a random suffix", () => {
527+
const parent = root();
528+
const first = createStagingDir(parent, ".stage-");
529+
const second = createStagingDir(parent, ".stage-");
530+
expect(first).not.toBe(second);
531+
expect(first).toMatch(/\.stage-[0-9a-f]{8}$/);
532+
if (process.platform !== "win32") expect(fs.statSync(first).mode & 0o777).toBe(0o700);
533+
});
534+
535+
it("tries another name when one is taken, and gives up after five", () => {
536+
const parent = root();
537+
expect(createStagingDir(parent, ".s-", () => "same")).toBe(join(parent, ".s-same"));
538+
const names = ["same", "same", "next"];
539+
expect(createStagingDir(parent, ".s-", () => names.shift() ?? "x")).toBe(
540+
join(parent, ".s-next"),
541+
);
542+
expect(() => createStagingDir(parent, ".s-", () => "same")).toThrow(/EEXIST/);
543+
});
544+
});

‎packages/core/src/atomicFile.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const BUSY_RENAME = new Set(["EPERM", "EBUSY", "EACCES"]);
1414
const RENAME_RETRY_DELAYS_MS = [10, 20, 30, 40];
1515
const MAX_LINK_HOPS = 40;
1616
const TEMP_NAME_TRIES = 3;
17+
const STAGING_NAME_TRIES = 5;
1718
const TEMP_SUFFIX = /\.hf[0-9a-f]{6}\.tmp$/;
1819

1920
/** A fresh temp sibling for a write that publishes `filePath`. 13 bytes, so a 242-byte name still fits. */
@@ -176,3 +177,20 @@ function renameWithRetry(
176177
function errorCode(error: unknown): string {
177178
return String((error as NodeJS.ErrnoException)?.code);
178179
}
180+
181+
/** A fresh private directory in `parent`; mkdtemp fails past 260 characters on Windows. */
182+
export function createStagingDir(
183+
parent: string,
184+
prefix: string,
185+
suffix = () => randomBytes(4).toString("hex"),
186+
): string {
187+
for (let attempt = 1; ; attempt++) {
188+
const dir = join(parent, `${prefix}${suffix()}`);
189+
try {
190+
fs.mkdirSync(dir, { mode: 0o700 });
191+
return dir;
192+
} catch (error) {
193+
if (errorCode(error) !== "EEXIST" || attempt >= STAGING_NAME_TRIES) throw error;
194+
}
195+
}
196+
}

‎packages/core/src/fonts/fontCompression.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
// @ts-expect-error -- wawoff2 ships no type declarations
22
import wawoff2 from "wawoff2";
33
import { createHash } from "node:crypto";
4-
import {
5-
existsSync,
6-
mkdirSync,
7-
mkdtempSync,
8-
readFileSync,
9-
renameSync,
10-
rmSync,
11-
writeFileSync,
12-
} from "node:fs";
4+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
135
import { homedir, tmpdir } from "node:os";
146
import { dirname, join } from "node:path";
7+
import { createStagingDir } from "../atomicFile.js";
158

169
const { compress } = wawoff2 as {
1710
compress: (input: Buffer | Uint8Array) => Promise<Uint8Array>;
@@ -71,7 +64,7 @@ function cacheCompression(path: string, compressed: Buffer): void {
7164
mkdirSync(dirname(path), { recursive: true });
7265
// Keep the write on the cache filesystem for an atomic rename, inside a
7366
// private directory that this invocation owns.
74-
tempDir = mkdtempSync(join(dirname(path), ".compression-"));
67+
tempDir = createStagingDir(dirname(path), ".compression-");
7568
const tmpPath = join(tempDir, "font.woff2");
7669
writeFileSync(tmpPath, compressed, { flag: "wx", mode: 0o644 });
7770
renameSync(tmpPath, path);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { readdirSync, readFileSync, statSync } from "node:fs";
2+
import { dirname, join, relative } from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
import { expect, it } from "vitest";
5+
6+
const packagesDir = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
7+
const SOURCE = /\.(ts|mjs)$/;
8+
const NOT_SOURCE = /\.(test|fixture)\.|[\\/](tests?|node_modules|dist)[\\/]/;
9+
const MKDTEMP = /\bmkdtemp(Sync)?\(/;
10+
11+
// Not beside a project or output path: a system temp root, a standalone skill script, a cloud function's scratch root.
12+
const ALLOWED = new Set([
13+
"cli/src/media-use/lib/lut-preset-provider.mjs",
14+
"producer/src/server.ts",
15+
"producer/src/regression-harness.ts",
16+
"producer/src/services/render/renderDirOwner.ts",
17+
"aws-lambda/src/s3PlanV2Publisher.ts",
18+
"gcp-cloud-run/src/gcsPlanV2Publisher.ts",
19+
]);
20+
21+
function sources(dir: string): string[] {
22+
return readdirSync(dir).flatMap((name) => {
23+
const path = join(dir, name);
24+
if (NOT_SOURCE.test(path)) return [];
25+
return statSync(path).isDirectory() ? sources(path) : SOURCE.test(name) ? [path] : [];
26+
});
27+
}
28+
29+
it("no package makes a staging folder beside a project path with mkdtemp, which fails past 260 characters on Windows", () => {
30+
const offenders = readdirSync(packagesDir).flatMap((pkg) => {
31+
const src = join(packagesDir, pkg, "src");
32+
try {
33+
statSync(src);
34+
} catch {
35+
return [];
36+
}
37+
return sources(src).flatMap((file) => {
38+
const rel = relative(packagesDir, file).split("\\").join("/");
39+
if (ALLOWED.has(rel)) return [];
40+
return readFileSync(file, "utf8")
41+
.split("\n")
42+
.flatMap((line, i) =>
43+
MKDTEMP.test(line) && !/tmpdir|^\s*(\/\/|\*)/.test(line) ? [`${rel}:${i + 1}`] : [],
44+
);
45+
});
46+
});
47+
expect(offenders, "use createStagingDir from @hyperframes/core/atomic-file").toEqual([]);
48+
});

‎packages/engine/src/services/aacEncode.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
*/
55

66
import { execFile } from "node:child_process";
7-
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
7+
import { readFileSync, rmSync, writeFileSync } from "node:fs";
88
import { dirname, join } from "node:path";
99
import { getFfmpegBinary } from "../utils/ffmpegBinaries.js";
1010
import { runFfmpeg, type RunFfmpegResult } from "../utils/runFfmpeg.js";
11+
import { createStagingDir } from "@hyperframes/core/atomic-file";
1112

1213
const SAMPLE_RATE = 48_000;
1314
const FRAME_SAMPLES = 1024;
@@ -101,7 +102,7 @@ export async function encodeAac(
101102
if (!(await supportsSettsDuration())) {
102103
return encodeInOnePass(input, outputPath, { signal, timeout, filter });
103104
}
104-
const dir = mkdtempSync(join(dirname(outputPath), ".aac-segments-"));
105+
const dir = createStagingDir(dirname(outputPath), ".aac-segments-");
105106
try {
106107
const segments = planSegments(totalDuration, count).map((segment, k) => ({
107108
...segment,

0 commit comments

Comments
 (0)