fix(server): canonicalise media paths the same way their callers do (#9575)
Co-authored-by: Claude Code <[email protected]>
This commit is contained in:
1 parent
912276bcd9
commit
5f4c7161fa
3 files changed
+22
-11
No files matched your search
@@ -754,7 +754,7 @@ describe("AssetAccess", () => {
|
||||
projectFaviconPath: "brand/custom.svg",
|
||||
});
|
||||
|
||||
expect(result.sourcePath).toBe("brand/custom.svg");
|
||||
expect(result.sourcePath).toBe(path.join("brand", "custom.svg"));
|
||||
expect(result.relativeUrl).toMatch(/\/v[0-9a-f]{64}-custom\.svg$/);
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
@@ -813,7 +813,7 @@ describe("AssetAccess", () => {
|
||||
projectFaviconPath: "brand/saved.svg",
|
||||
});
|
||||
|
||||
expect(result.sourcePath).toBe("brand/saved.svg");
|
||||
expect(result.sourcePath).toBe(path.join("brand", "saved.svg"));
|
||||
expect(result.relativeUrl).toMatch(/\/v[0-9a-f]{64}-saved\.svg$/);
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
|
||||
@@ -37,6 +37,11 @@ export interface OpenMediaFile {
|
||||
readonly info: NodeFS.BigIntStats;
|
||||
}
|
||||
|
||||
const realpathLikeFileSystem = (filePath: string) =>
|
||||
new Promise<string>((resolve, reject) => {
|
||||
NodeFS.realpath(filePath, (error, resolved) => (error ? reject(error) : resolve(resolved)));
|
||||
});
|
||||
|
||||
/** Opens a canonical media path once. Replacements cannot change the response's source. */
|
||||
export const openMediaFile = Effect.fn("openMediaFile")(function* (
|
||||
filePath: string,
|
||||
@@ -71,7 +76,11 @@ export const openMediaFile = Effect.fn("openMediaFile")(function* (
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
if ((await NodeFSP.realpath(filePath)) !== filePath) return null;
|
||||
// Callers canonicalise with Effect's FileSystem.realPath, which is
|
||||
// Node's JS realpath. fs/promises.realpath is the native binding and
|
||||
// on Windows also expands 8.3 short names, so a path that is already
|
||||
// canonical by the caller's rules would still look swapped here.
|
||||
if ((await realpathLikeFileSystem(filePath)) !== filePath) return null;
|
||||
const after = await NodeFSP.lstat(filePath, { bigint: true });
|
||||
if (!after.isFile() || info.dev !== after.dev || info.ino !== after.ino) return null;
|
||||
accepted = true;
|
||||
|
||||
@@ -126,6 +126,7 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
it.effect("prefers a t3.json iconPath over well-known files", () =>
|
||||
Effect.gen(function* () {
|
||||
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
const path = yield* Path.Path;
|
||||
const cwd = yield* makeTempDir;
|
||||
yield* writeTextFile(cwd, "t3.json", '{ "iconPath": "brand/mark.svg" }');
|
||||
yield* writeTextFile(cwd, "brand/mark.svg", "<svg>mark</svg>");
|
||||
@@ -134,13 +135,14 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
|
||||
expect(resolved).not.toBeNull();
|
||||
expect(resolved).toContain("brand/mark.svg");
|
||||
expect(resolved).toBe(path.join(cwd, "brand", "mark.svg"));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("uses a saved project favicon override", () =>
|
||||
Effect.gen(function* () {
|
||||
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
const path = yield* Path.Path;
|
||||
const cwd = yield* makeTempDir;
|
||||
yield* writeTextFile(cwd, "brand/custom.svg", "<svg>custom</svg>");
|
||||
yield* writeTextFile(cwd, "favicon.svg", "<svg>automatic</svg>");
|
||||
@@ -148,7 +150,7 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
const resolved = yield* resolver.resolvePath(cwd, "brand/custom.svg");
|
||||
|
||||
expect(resolved).not.toBeNull();
|
||||
expect(resolved).toContain("brand/custom.svg");
|
||||
expect(resolved).toBe(path.join(cwd, "brand", "custom.svg"));
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -232,7 +234,7 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
|
||||
expect(resolved).not.toBeNull();
|
||||
expect(resolved).toContain("public/brand/logo.svg");
|
||||
expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -257,7 +259,7 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
|
||||
expect(resolved).not.toBeNull();
|
||||
expect(resolved).toContain("public/brand/logo.svg");
|
||||
expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -275,7 +277,7 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
|
||||
expect(resolved).not.toBeNull();
|
||||
expect(resolved).toContain("public/brand/logo.svg");
|
||||
expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -293,7 +295,7 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
|
||||
expect(resolved).not.toBeNull();
|
||||
expect(resolved).toContain("public/brand/logo.svg");
|
||||
expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -311,7 +313,7 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
|
||||
expect(resolved).not.toBeNull();
|
||||
expect(resolved).toContain("public/brand/logo.svg");
|
||||
expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -459,7 +461,7 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
|
||||
expect(resolved).not.toBeNull();
|
||||
expect(resolved).toContain("public/brand/logo.svg");
|
||||
expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user