fix(server): cache project favicon resolution (#9080)
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
261380f91f
commit
643b21edaa
2 files changed
+137
-3
No files matched your search
@@ -1,10 +1,12 @@
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { it, describe, expect } from "@effect/vitest";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Path from "effect/Path";
|
||||
import * as PlatformError from "effect/PlatformError";
|
||||
import { TestClock } from "effect/testing";
|
||||
|
||||
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
|
||||
import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts";
|
||||
@@ -49,6 +51,64 @@ const makeResolverWithFileSystem = (fileSystem: FileSystem.FileSystem) =>
|
||||
|
||||
it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
describe("resolvePath", () => {
|
||||
it.effect("serves repeated resolves from cache instead of re-walking candidates", () =>
|
||||
Effect.gen(function* () {
|
||||
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
const cwd = yield* makeTempDir;
|
||||
yield* writeTextFile(cwd, "public/favicon.svg", "<svg>public</svg>");
|
||||
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
expect(resolved?.endsWith("public/favicon.svg")).toBe(true);
|
||||
|
||||
// `favicon.svg` outranks `public/favicon.svg`, so a resolver that walked
|
||||
// the candidate list again would switch to it. Staying on the original
|
||||
// answer is only possible from cache.
|
||||
yield* writeTextFile(cwd, "favicon.svg", "<svg>root</svg>");
|
||||
|
||||
for (const _attempt of [1, 2, 3]) {
|
||||
expect(yield* resolver.resolvePath(cwd)).toBe(resolved);
|
||||
}
|
||||
|
||||
yield* TestClock.adjust(Duration.minutes(11));
|
||||
|
||||
expect((yield* resolver.resolvePath(cwd))?.endsWith("/favicon.svg")).toBe(true);
|
||||
expect(yield* resolver.resolvePath(cwd)).not.toBe(resolved);
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
);
|
||||
|
||||
it.effect("falls back at once when a cached favicon is deleted", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
const cwd = yield* makeTempDir;
|
||||
yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
|
||||
|
||||
expect(yield* resolver.resolvePath(cwd)).not.toBeNull();
|
||||
|
||||
yield* fileSystem.remove(path.join(cwd, "favicon.svg")).pipe(Effect.orDie);
|
||||
|
||||
// Still inside the positive TTL: the cached path must not be served.
|
||||
expect(yield* resolver.resolvePath(cwd)).toBeNull();
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
);
|
||||
|
||||
it.effect("re-probes for a favicon added after a miss once the negative TTL expires", () =>
|
||||
Effect.gen(function* () {
|
||||
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
const cwd = yield* makeTempDir;
|
||||
|
||||
expect(yield* resolver.resolvePath(cwd)).toBeNull();
|
||||
|
||||
yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
|
||||
expect(yield* resolver.resolvePath(cwd)).toBeNull();
|
||||
|
||||
yield* TestClock.adjust(Duration.minutes(2));
|
||||
|
||||
expect(yield* resolver.resolvePath(cwd)).not.toBeNull();
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
);
|
||||
|
||||
it.effect("prefers well-known favicon files", () =>
|
||||
Effect.gen(function* () {
|
||||
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
|
||||
@@ -6,8 +6,11 @@
|
||||
*
|
||||
* @module ProjectFaviconResolver
|
||||
*/
|
||||
import * as Cache from "effect/Cache";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
@@ -18,6 +21,30 @@ import * as Schema from "effect/Schema";
|
||||
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
|
||||
import * as T3ProjectFileLoader from "./T3ProjectFileLoader.ts";
|
||||
|
||||
// Resolution walks up to 12 well-known paths plus 7 source files, so a miss
|
||||
// costs ~20 filesystem probes. AssetAccess resolves on every project-favicon
|
||||
// asset URL, and a project's icon does not move, so the answer is cached.
|
||||
const FAVICON_CACHE_CAPACITY = 512;
|
||||
const FAVICON_POSITIVE_CACHE_TTL = Duration.minutes(10);
|
||||
const FAVICON_NEGATIVE_CACHE_TTL = Duration.minutes(1);
|
||||
|
||||
function faviconCacheKey(cwd: string, faviconPath?: string): string {
|
||||
return `${faviconPath ?? ""}\0${cwd}`;
|
||||
}
|
||||
|
||||
function parseFaviconCacheKey(key: string): {
|
||||
readonly cwd: string;
|
||||
readonly faviconPath?: string;
|
||||
} {
|
||||
const separatorIndex = key.indexOf("\0");
|
||||
if (separatorIndex === -1) {
|
||||
return { cwd: key };
|
||||
}
|
||||
const faviconPath = key.slice(0, separatorIndex);
|
||||
const cwd = key.slice(separatorIndex + 1);
|
||||
return faviconPath.length === 0 ? { cwd } : { cwd, faviconPath };
|
||||
}
|
||||
|
||||
// Well-known favicon paths checked in order.
|
||||
const FAVICON_CANDIDATES = [
|
||||
"favicon.svg",
|
||||
@@ -178,9 +205,10 @@ export const make = Effect.gen(function* () {
|
||||
return null;
|
||||
});
|
||||
|
||||
const resolvePath: ProjectFaviconResolver["Service"]["resolvePath"] = Effect.fn(
|
||||
"ProjectFaviconResolver.resolvePath",
|
||||
)(function* (cwd, faviconPath) {
|
||||
const resolvePathUncached = Effect.fn("ProjectFaviconResolver.resolvePathUncached")(function* (
|
||||
cwd: string,
|
||||
faviconPath?: string,
|
||||
): Effect.fn.Return<string | null, ProjectFaviconResolutionError> {
|
||||
const projectCwd = yield* workspacePaths.normalizeWorkspaceRoot(cwd).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
@@ -267,6 +295,52 @@ export const make = Effect.gen(function* () {
|
||||
return null;
|
||||
});
|
||||
|
||||
const faviconCache = yield* Cache.makeWith<string, string | null, ProjectFaviconResolutionError>(
|
||||
(key) => {
|
||||
const { cwd, faviconPath } = parseFaviconCacheKey(key);
|
||||
return resolvePathUncached(cwd, faviconPath);
|
||||
},
|
||||
{
|
||||
capacity: FAVICON_CACHE_CAPACITY,
|
||||
timeToLive: Exit.match({
|
||||
onSuccess: (value: string | null) =>
|
||||
value === null ? FAVICON_NEGATIVE_CACHE_TTL : FAVICON_POSITIVE_CACHE_TTL,
|
||||
onFailure: () => Duration.zero,
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
const resolvePath: ProjectFaviconResolver["Service"]["resolvePath"] = Effect.fn(
|
||||
"ProjectFaviconResolver.resolvePath",
|
||||
)(function* (cwd, faviconPath) {
|
||||
const key = faviconCacheKey(cwd, faviconPath);
|
||||
const cached = yield* Cache.get(faviconCache, key);
|
||||
if (cached === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// A hit still confirms the file with one stat rather than the ~20 probes a
|
||||
// full walk costs, so a deleted icon falls back at once instead of after
|
||||
// the TTL.
|
||||
const stats = yield* optionOnNotFound(fileSystem.stat(cached)).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProjectFaviconResolutionError({
|
||||
operation: "stat-candidate",
|
||||
workspaceRoot: cwd,
|
||||
absolutePath: cached,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (Option.isSome(stats) && stats.value.type === "File") {
|
||||
return cached;
|
||||
}
|
||||
|
||||
yield* Cache.invalidate(faviconCache, key);
|
||||
return yield* Cache.get(faviconCache, key);
|
||||
});
|
||||
|
||||
return ProjectFaviconResolver.of({ resolvePath });
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user