Skip to content

Commit 59e3850

Browse files
fix(studio-server): a write through a link lands on the file the system resolves (#4589)
* fix(studio-server): a write through a link lands on the file the system resolves * fix(cli): resolve catalog install paths as the system does, matching the install root * fix(cli): list installed files relative to the real project folder
1 parent 2b1bb8b commit 59e3850

12 files changed

Lines changed: 92 additions & 36 deletions

File tree

‎packages/cli/src/commands/add.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const examples: Example[] = [
1414
import { existsSync, readFileSync } from "node:fs";
1515
import { parseHTML } from "linkedom";
1616
import { resolve, relative } from "node:path";
17-
import { ITEM_TYPE_DIRS, type RegistryItem } from "@hyperframes/core";
17+
import { ITEM_TYPE_DIRS, realpath, type RegistryItem } from "@hyperframes/core";
1818
import { c } from "../ui/colors.js";
1919
import {
2020
DEFAULT_REGISTRY_URL,
@@ -510,14 +510,15 @@ export default defineCommand({
510510
}
511511
console.log("");
512512
console.log(`${c.success("✓")} Added ${c.accent(result.name)} (${result.type})`);
513+
const root = realpath(projectDir);
513514
for (const file of result.preserved) {
514515
console.log(
515-
` ${c.warn("kept")} ${relative(projectDir, file) || file} — you have edited this; --force to overwrite`,
516+
` ${c.warn("kept")} ${relative(root, file) || file} — you have edited this; --force to overwrite`,
516517
);
517518
}
518519

519520
for (const file of result.written) {
520-
console.log(` ${c.dim(relative(projectDir, file))}`);
521+
console.log(` ${c.dim(relative(root, file))}`);
521522
}
522523
if (result.variablesApplied.length > 0) {
523524
// Say it out loud. A component's values are baked into the file rather

‎packages/cli/src/commands/catalog.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ export const examples: Example[] = [
1010
];
1111

1212
import * as clack from "@clack/prompts";
13-
import { type ItemType, type RegistryItem } from "@hyperframes/core";
13+
import { realpath, type ItemType, type RegistryItem } from "@hyperframes/core";
1414
import { c } from "../ui/colors.js";
1515
import { loadAllItems } from "../registry/resolver.js";
1616
import { fetchRegistryManifest } from "../registry/remote.js";
1717
import { loadProjectConfig, DEFAULT_PROJECT_CONFIG } from "../utils/projectConfig.js";
18-
import { resolve } from "node:path";
18+
import { relative, resolve } from "node:path";
1919
import { finishCommand } from "../utils/commandResult.js";
2020
import { isAttendedTerminal } from "../utils/attendedTerminal.js";
2121
import { runAdd } from "./add.js";
@@ -472,9 +472,9 @@ export default defineCommand({
472472
}
473473
console.log("");
474474
console.log(`${c.success("✓")} Installed ${c.accent(result.name)} (${result.type})`);
475+
const root = realpath(dir);
475476
for (const file of result.written) {
476-
const rel = file.replace(dir + "/", "");
477-
console.log(` ${c.dim(rel)}`);
477+
console.log(` ${c.dim(relative(root, file))}`);
478478
}
479479
if (result.snippet) {
480480
console.log("");

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ it("publishes through an internal leaf alias to its physical target", () => {
8484
}
8585
});
8686

87+
it.skipIf(process.platform === "win32")(
88+
"publishes through a leaf alias whose target climbs out of a linked folder to the file it resolves",
89+
() => {
90+
const root = registryRoot(mkdtempSync(join(tmpdir(), "hf-publish-")));
91+
try {
92+
mkdirSync(join(root, "deep", "nested"), { recursive: true });
93+
mkdirSync(join(root, "m"));
94+
writeFileSync(join(root, "deep", "y.html"), "old");
95+
writeFileSync(join(root, "y.html"), "decoy");
96+
symlinkSync(join(root, "deep", "nested"), join(root, "sub"), "dir");
97+
symlinkSync("../sub/../y.html", join(root, "m", "x.html"), "file");
98+
publishRegistryFile(root, "m/x.html", "new");
99+
expect(readFileSync(join(root, "deep", "y.html"), "utf8")).toBe("new");
100+
expect(readFileSync(join(root, "y.html"), "utf8")).toBe("decoy");
101+
} finally {
102+
rmSync(root, { recursive: true, force: true });
103+
}
104+
},
105+
);
106+
87107
it.skipIf(process.platform === "win32")(
88108
"preserves executable mode when replacing an installed file",
89109
() => {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import {
33
chmodSync,
44
mkdirSync,
55
mkdtempSync,
6-
realpathSync,
76
renameSync,
87
rmSync,
98
writeFileSync,
109
} from "node:fs";
1110
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
11+
import { realpath } from "@hyperframes/core";
1212

1313
export function registryRoot(directory: string): string {
1414
mkdirSync(directory, { recursive: true });
15-
return realpathSync(directory);
15+
return realpath(directory);
1616
}
1717

1818
export function registryTargetPath(root: string, target: string): string {
@@ -21,7 +21,7 @@ export function registryTargetPath(root: string, target: string): string {
2121
let path = root;
2222
for (const part of parts) {
2323
path = join(path, part);
24-
if (lstatSync(path, { throwIfNoEntry: false })) path = realpathSync(path);
24+
if (lstatSync(path, { throwIfNoEntry: false })) path = realpath(path);
2525
assertContained(root, path);
2626
}
2727
return path;
@@ -54,7 +54,7 @@ export function publishRegistryFile(
5454
): string {
5555
const path = registryTargetPath(root, target);
5656
mkdirSync(dirname(path), { recursive: true });
57-
const parent = realpathSync(dirname(path));
57+
const parent = realpath(dirname(path));
5858
assertContained(root, parent);
5959
const destination = resolve(parent, basename(path));
6060
const stage = mkdtempSync(join(parent, ".hf-install-"));

‎packages/cli/src/server/studioServer.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import { Hono, type Context } from "hono";
99
import { streamSSE } from "hono/streaming";
10-
import { existsSync, readFileSync, realpathSync, writeFileSync, unlinkSync } from "node:fs";
10+
import { realpath } from "@hyperframes/core";
11+
import { existsSync, readFileSync, writeFileSync, unlinkSync } from "node:fs";
1112
import { createHash } from "node:crypto";
1213
import { fileURLToPath } from "node:url";
1314
import { resolve, join, basename, relative, sep } from "node:path";
@@ -784,7 +785,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
784785
recordRewrittenInstall(opts.project.dir, written);
785786

786787
// The item's own file first, as add recorded it, since Studio mounts the first .html it gets.
787-
const root = realpathSync(opts.project.dir);
788+
const root = realpath(opts.project.dir);
788789
const primary = primaryInstalledTarget(item);
789790
const primaryPath = registryTargetPath(root, primary);
790791
const others = written

‎packages/core/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export {
275275
type MediaVisualStyleProperty,
276276
} from "./inline-scripts/parityContract";
277277
export { redactKnownPaths, redactTelemetryString } from "./telemetryRedaction";
278-
export { isSafePath, resolveWithinProject } from "./safePath";
278+
export { isSafePath, realpath, resolveWithinProject } from "./safePath";
279279
export type {
280280
HyperframePickerApi,
281281
HyperframePickerBoundingBox,

‎packages/core/src/safePath.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { resolve, sep, join, dirname, basename } from "node:path";
22
import { lstatSync, realpathSync } from "node:fs";
33

4-
function realpath(path: string): string {
4+
export function realpath(path: string): string {
55
try {
66
return realpathSync.native(path);
77
} catch (error) {

‎packages/studio-server/src/helpers/compositionInsertion.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { existsSync, readFileSync, realpathSync } from "node:fs";
1+
import { existsSync, readFileSync } from "node:fs";
22
import { randomUUID } from "node:crypto";
33
import { dirname, relative, resolve, sep } from "node:path";
44
import { parseHTML } from "linkedom";
5-
import { isSafePath, resolveWithinProject } from "./safePath.js";
5+
import { isSafePath, realpath, resolveWithinProject } from "./safePath.js";
66

77
export class CompositionInsertionError extends Error {
88
constructor(
@@ -43,8 +43,8 @@ function canonicalProjectPath(projectDir: string, candidate: string | null): str
4343
if (!existsSync(candidate)) {
4444
throw new CompositionInsertionError("Composition source was not found", 404);
4545
}
46-
const canonical = realpathSync(candidate);
47-
if (!isSafePath(realpathSync(projectDir), canonical)) {
46+
const canonical = realpath(candidate);
47+
if (!isSafePath(realpath(projectDir), canonical)) {
4848
throw new CompositionInsertionError("Composition source escapes the project", 400);
4949
}
5050
return canonical;

‎packages/studio-server/src/helpers/mediaCodecMap.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { existsSync, realpathSync, statSync } from "node:fs";
1+
import { existsSync, statSync } from "node:fs";
2+
import { realpath } from "./safePath.js";
23
import { relative, resolve, sep } from "node:path";
34
import { rewriteAssetPath } from "@hyperframes/parsers/asset-paths";
45
import {
@@ -253,7 +254,7 @@ export async function probeAssetCodec(
253254
let target: string;
254255
try {
255256
stat = statSync(filePath);
256-
target = realpathSync(filePath);
257+
target = realpath(filePath);
257258
} catch {
258259
return null;
259260
}

‎packages/studio-server/src/helpers/proxyTranscoder.ts‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import { spawn } from "node:child_process";
22
import { createHash, randomUUID } from "node:crypto";
3-
import {
4-
existsSync,
5-
mkdirSync,
6-
realpathSync,
7-
renameSync,
8-
statSync,
9-
unlinkSync,
10-
utimesSync,
11-
} from "node:fs";
3+
import { existsSync, mkdirSync, renameSync, statSync, unlinkSync, utimesSync } from "node:fs";
124
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
135
import { findFfBinary } from "@hyperframes/parsers/ff-binaries";
146
import { probeMediaMetadata } from "./mediaMetadata.js";
157
import { cleanupProxyCache } from "./proxyCache.js";
168
import { PROXY_VARIANT_CONFIG, type ProxyVariant } from "./mediaCodecMap.js";
9+
import { realpath } from "./safePath.js";
1710

1811
/**
1912
* Transcodes browser-hostile local video sources (HEVC, ProRes, ...) into a
@@ -158,8 +151,8 @@ function canonicalizeProxySource(
158151
throw new ProxySourceOutsideProjectError();
159152
}
160153

161-
const canonicalProjectDir = realpathSync(projectDir);
162-
const canonicalSourcePath = realpathSync(absoluteSourcePath);
154+
const canonicalProjectDir = realpath(projectDir);
155+
const canonicalSourcePath = realpath(absoluteSourcePath);
163156
const canonicalRelativePath = relative(canonicalProjectDir, canonicalSourcePath);
164157
const sourceIsInsideCanonicalProject =
165158
canonicalRelativePath !== ".." &&

0 commit comments

Comments
 (0)