Preserve asset access failure causes (#3342)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
57d25c934b
commit
90dc76b11e
6 files changed
+512
-69
No files matched your search
@@ -5,6 +5,7 @@ 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 * as ServerSecretStore from "../auth/ServerSecretStore.ts";
|
||||
import * as ServerConfig from "../config.ts";
|
||||
@@ -85,7 +86,58 @@ describe("AssetAccess", () => {
|
||||
},
|
||||
workspaceRoot: root,
|
||||
}).pipe(Effect.flip);
|
||||
expect(error.message).toContain("relative to the project root");
|
||||
expect(error.message).toBe("Workspace file path must be relative to the project root.");
|
||||
expect(error).toMatchObject({
|
||||
operation: "validate-workspace-path",
|
||||
resource: {
|
||||
_tag: "workspace-file",
|
||||
threadId: "thread-1",
|
||||
path: htmlPath,
|
||||
},
|
||||
});
|
||||
expect(error.cause).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError);
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
|
||||
it.effect("preserves non-missing canonical path failures when issuing asset URLs", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const root = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-asset-permission-root-",
|
||||
});
|
||||
const htmlPath = path.join(root, "report.html");
|
||||
yield* fileSystem.writeFileString(htmlPath, "<p>report</p>");
|
||||
const cause = PlatformError.systemError({
|
||||
_tag: "PermissionDenied",
|
||||
module: "FileSystem",
|
||||
method: "realPath",
|
||||
pathOrDescriptor: htmlPath,
|
||||
});
|
||||
const failingFileSystem = FileSystem.FileSystem.of({
|
||||
...fileSystem,
|
||||
realPath: () => Effect.fail(cause),
|
||||
});
|
||||
|
||||
const error = yield* issueAssetUrl({
|
||||
resource: {
|
||||
_tag: "workspace-file",
|
||||
threadId: ThreadId.make("thread-1"),
|
||||
path: htmlPath,
|
||||
},
|
||||
workspaceRoot: root,
|
||||
}).pipe(Effect.provideService(FileSystem.FileSystem, failingFileSystem), Effect.flip);
|
||||
|
||||
expect(error.message).toBe("Failed to inspect the workspace asset.");
|
||||
expect(error).toMatchObject({
|
||||
operation: "inspect-workspace-asset",
|
||||
resource: {
|
||||
_tag: "workspace-file",
|
||||
threadId: "thread-1",
|
||||
path: htmlPath,
|
||||
},
|
||||
});
|
||||
expect(error.cause).toBe(cause);
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
|
||||
@@ -186,4 +238,37 @@ describe("AssetAccess", () => {
|
||||
).toEqual({ kind: "project-favicon-fallback" });
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
|
||||
it.effect("preserves structured project favicon resolution causes", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const root = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-asset-favicon-error-",
|
||||
});
|
||||
const platformCause = PlatformError.systemError({
|
||||
_tag: "PermissionDenied",
|
||||
module: "FileSystem",
|
||||
method: "stat",
|
||||
});
|
||||
const resolutionCause = new ProjectFaviconResolver.ProjectFaviconResolutionError({
|
||||
operation: "stat-candidate",
|
||||
workspaceRoot: root,
|
||||
relativePath: "favicon.svg",
|
||||
cause: platformCause,
|
||||
});
|
||||
const resolver = ProjectFaviconResolver.ProjectFaviconResolver.of({
|
||||
resolvePath: () => Effect.fail(resolutionCause),
|
||||
});
|
||||
|
||||
const error = yield* issueAssetUrl({
|
||||
resource: { _tag: "project-favicon", cwd: root },
|
||||
}).pipe(
|
||||
Effect.provideService(ProjectFaviconResolver.ProjectFaviconResolver, resolver),
|
||||
Effect.flip,
|
||||
);
|
||||
|
||||
expect(error.message).toBe("Failed to resolve project favicon.");
|
||||
expect(error.cause).toBe(resolutionCause);
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
});
|
||||
@@ -11,6 +11,7 @@ import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Path from "effect/Path";
|
||||
import * as PlatformError from "effect/PlatformError";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import {
|
||||
@@ -97,33 +98,59 @@ function decodeRelativePath(value: string): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
const failAccess = (message: string, cause?: unknown) =>
|
||||
new AssetAccessError({ message, ...(cause === undefined ? {} : { cause }) });
|
||||
const optionOnNotFound = <A, R>(
|
||||
effect: Effect.Effect<A, PlatformError.PlatformError, R>,
|
||||
): Effect.Effect<Option.Option<A>, PlatformError.PlatformError, R> =>
|
||||
effect.pipe(
|
||||
Effect.map(Option.some),
|
||||
Effect.catchTags({
|
||||
PlatformError: (error) =>
|
||||
error.reason._tag === "NotFound" ? Effect.succeed(Option.none<A>()) : Effect.fail(error),
|
||||
}),
|
||||
);
|
||||
|
||||
const resolveCanonicalWorkspaceFile = Effect.fn("AssetAccess.resolveCanonicalWorkspaceFile")(
|
||||
function* (input: { readonly workspaceRoot: string; readonly relativePath: string }) {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
|
||||
const resolved = yield* workspacePaths
|
||||
.resolveRelativePathWithinRoot(input)
|
||||
.pipe(Effect.orElseSucceed(() => null));
|
||||
if (!resolved) return null;
|
||||
const resolved = yield* workspacePaths.resolveRelativePathWithinRoot(input).pipe(
|
||||
Effect.map(Option.some),
|
||||
Effect.catchTags({
|
||||
WorkspacePathOutsideRootError: () => Effect.succeed(Option.none()),
|
||||
}),
|
||||
);
|
||||
if (Option.isNone(resolved)) return null;
|
||||
|
||||
const [canonicalRoot, canonicalFile] = yield* Effect.all([
|
||||
fileSystem.realPath(input.workspaceRoot).pipe(Effect.orElseSucceed(() => null)),
|
||||
fileSystem.realPath(resolved.absolutePath).pipe(Effect.orElseSucceed(() => null)),
|
||||
optionOnNotFound(fileSystem.realPath(input.workspaceRoot)),
|
||||
optionOnNotFound(fileSystem.realPath(resolved.value.absolutePath)),
|
||||
]);
|
||||
if (!canonicalRoot || !canonicalFile) return null;
|
||||
if (Option.isNone(canonicalRoot) || Option.isNone(canonicalFile)) return null;
|
||||
|
||||
const path = yield* Path.Path;
|
||||
const relative = path.relative(canonicalRoot, canonicalFile);
|
||||
const relative = path.relative(canonicalRoot.value, canonicalFile.value);
|
||||
if (relative === "" || relative.startsWith("..") || path.isAbsolute(relative)) return null;
|
||||
|
||||
const info = yield* fileSystem.stat(canonicalFile).pipe(Effect.orElseSucceed(() => null));
|
||||
return info?.type === "File" ? canonicalFile : null;
|
||||
const info = yield* optionOnNotFound(fileSystem.stat(canonicalFile.value));
|
||||
return Option.isSome(info) && info.value.type === "File" ? canonicalFile.value : null;
|
||||
},
|
||||
);
|
||||
|
||||
const resolveCanonicalWorkspaceFileForRequest = (input: {
|
||||
readonly workspaceRoot: string;
|
||||
readonly relativePath: string;
|
||||
}) =>
|
||||
resolveCanonicalWorkspaceFile(input).pipe(
|
||||
Effect.tapError((cause) =>
|
||||
Effect.logError("Failed to resolve canonical asset path.", {
|
||||
workspaceRoot: input.workspaceRoot,
|
||||
relativePath: input.relativePath,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.orElseSucceed(() => null),
|
||||
);
|
||||
|
||||
export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (input: {
|
||||
readonly resource: AssetResource;
|
||||
readonly workspaceRoot?: string;
|
||||
@@ -138,30 +165,78 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
|
||||
switch (input.resource._tag) {
|
||||
case "workspace-file": {
|
||||
if (!input.workspaceRoot) {
|
||||
return yield* failAccess("Workspace context was not found.");
|
||||
return yield* new AssetAccessError({
|
||||
operation: "resolve-workspace-context",
|
||||
resource: input.resource,
|
||||
message: "Workspace context was not found.",
|
||||
});
|
||||
}
|
||||
const workspaceRoot = yield* workspacePaths
|
||||
.normalizeWorkspaceRoot(input.workspaceRoot)
|
||||
.pipe(Effect.mapError((cause) => failAccess(cause.message, cause)));
|
||||
const workspaceRoot = yield* workspacePaths.normalizeWorkspaceRoot(input.workspaceRoot).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "normalize-workspace-root",
|
||||
resource: input.resource,
|
||||
message: "Failed to normalize the workspace root.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const relativePath = path.isAbsolute(input.resource.path)
|
||||
? path.relative(workspaceRoot, input.resource.path)
|
||||
: input.resource.path;
|
||||
const resolved = yield* workspacePaths
|
||||
.resolveRelativePathWithinRoot({ workspaceRoot, relativePath })
|
||||
.pipe(Effect.mapError((cause) => failAccess(cause.message, cause)));
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "validate-workspace-path",
|
||||
resource: input.resource,
|
||||
message: "Workspace file path must be relative to the project root.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (!isWorkspacePreviewEntryPath(resolved.relativePath)) {
|
||||
return yield* failAccess("Only browser documents and images can be previewed.");
|
||||
return yield* new AssetAccessError({
|
||||
operation: "validate-preview-type",
|
||||
resource: input.resource,
|
||||
message: "Only browser documents and images can be previewed.",
|
||||
});
|
||||
}
|
||||
const canonicalFile = yield* resolveCanonicalWorkspaceFile({
|
||||
workspaceRoot,
|
||||
relativePath: resolved.relativePath,
|
||||
});
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "inspect-workspace-asset",
|
||||
resource: input.resource,
|
||||
message: "Failed to inspect the workspace asset.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (!canonicalFile) {
|
||||
return yield* failAccess("Workspace asset was not found.");
|
||||
return yield* new AssetAccessError({
|
||||
operation: "locate-workspace-asset",
|
||||
resource: input.resource,
|
||||
message: "Workspace asset was not found.",
|
||||
});
|
||||
}
|
||||
const canonicalWorkspaceRoot = yield* fileSystem
|
||||
.realPath(workspaceRoot)
|
||||
.pipe(Effect.mapError((cause) => failAccess("Failed to resolve workspace.", cause)));
|
||||
const canonicalWorkspaceRoot = yield* fileSystem.realPath(workspaceRoot).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "resolve-workspace",
|
||||
resource: input.resource,
|
||||
message: "Failed to resolve workspace.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
claims = isWorkspaceImagePreviewPath(resolved.relativePath)
|
||||
? {
|
||||
version: 1,
|
||||
@@ -187,7 +262,11 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
|
||||
attachmentId: input.resource.attachmentId,
|
||||
});
|
||||
if (!attachmentPath) {
|
||||
return yield* failAccess("Attachment was not found.");
|
||||
return yield* new AssetAccessError({
|
||||
operation: "locate-attachment",
|
||||
resource: input.resource,
|
||||
message: "Attachment was not found.",
|
||||
});
|
||||
}
|
||||
claims = {
|
||||
version: 1,
|
||||
@@ -199,24 +278,64 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
|
||||
break;
|
||||
}
|
||||
case "project-favicon": {
|
||||
const workspaceRoot = yield* workspacePaths
|
||||
.normalizeWorkspaceRoot(input.resource.cwd)
|
||||
.pipe(Effect.mapError((cause) => failAccess(cause.message, cause)));
|
||||
const workspaceRoot = yield* workspacePaths.normalizeWorkspaceRoot(input.resource.cwd).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "normalize-workspace-root",
|
||||
resource: input.resource,
|
||||
message: "Failed to normalize the workspace root.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const faviconResolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
const faviconPath = yield* faviconResolver.resolvePath(workspaceRoot);
|
||||
const faviconPath = yield* faviconResolver.resolvePath(workspaceRoot).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "resolve-project-favicon",
|
||||
resource: input.resource,
|
||||
message: "Failed to resolve project favicon.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const relativePath = faviconPath ? path.relative(workspaceRoot, faviconPath) : null;
|
||||
if (
|
||||
relativePath &&
|
||||
!(yield* resolveCanonicalWorkspaceFile({ workspaceRoot, relativePath }))
|
||||
!(yield* resolveCanonicalWorkspaceFile({ workspaceRoot, relativePath }).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "inspect-project-favicon",
|
||||
resource: input.resource,
|
||||
message: "Failed to inspect the project favicon.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
))
|
||||
) {
|
||||
return yield* failAccess("Project favicon was not found.");
|
||||
return yield* new AssetAccessError({
|
||||
operation: "locate-project-favicon",
|
||||
resource: input.resource,
|
||||
message: "Project favicon was not found.",
|
||||
});
|
||||
}
|
||||
claims = {
|
||||
version: 1,
|
||||
kind: "project-favicon",
|
||||
workspaceRoot: yield* fileSystem
|
||||
.realPath(workspaceRoot)
|
||||
.pipe(Effect.mapError((cause) => failAccess("Failed to resolve workspace.", cause))),
|
||||
workspaceRoot: yield* fileSystem.realPath(workspaceRoot).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "resolve-workspace",
|
||||
resource: input.resource,
|
||||
message: "Failed to resolve workspace.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
relativePath,
|
||||
expiresAt,
|
||||
};
|
||||
@@ -226,9 +345,17 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
|
||||
}
|
||||
|
||||
const secretStore = yield* ServerSecretStore.ServerSecretStore;
|
||||
const signingSecret = yield* secretStore
|
||||
.getOrCreateRandom(SIGNING_SECRET_NAME, 32)
|
||||
.pipe(Effect.mapError((cause) => failAccess(cause.message, cause)));
|
||||
const signingSecret = yield* secretStore.getOrCreateRandom(SIGNING_SECRET_NAME, 32).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "load-signing-key",
|
||||
resource: input.resource,
|
||||
message: "Failed to load the asset signing key.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const encodedPayload = base64UrlEncode(encodeAssetClaims(claims));
|
||||
const token = `${encodedPayload}.${signPayload(encodedPayload, signingSecret)}`;
|
||||
return {
|
||||
@@ -245,9 +372,10 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
|
||||
if (!encodedPayload || !signature) return null;
|
||||
|
||||
const secretStore = yield* ServerSecretStore.ServerSecretStore;
|
||||
const signingSecret = yield* secretStore
|
||||
.getOrCreateRandom(SIGNING_SECRET_NAME, 32)
|
||||
.pipe(Effect.orElseSucceed(() => null));
|
||||
const signingSecret = yield* secretStore.getOrCreateRandom(SIGNING_SECRET_NAME, 32).pipe(
|
||||
Effect.tapError((cause) => Effect.logError("Failed to load the asset signing key.", { cause })),
|
||||
Effect.orElseSucceed(() => null),
|
||||
);
|
||||
if (!signingSecret) return null;
|
||||
if (!timingSafeEqualBase64Url(signature, signPayload(encodedPayload, signingSecret))) return null;
|
||||
|
||||
@@ -262,8 +390,17 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
|
||||
});
|
||||
if (!attachmentPath) return null;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const info = yield* fileSystem.stat(attachmentPath).pipe(Effect.orElseSucceed(() => null));
|
||||
return info?.type === "File"
|
||||
const info = yield* optionOnNotFound(fileSystem.stat(attachmentPath)).pipe(
|
||||
Effect.tapError((cause) =>
|
||||
Effect.logError("Failed to inspect attachment asset.", {
|
||||
attachmentId: claims.attachmentId,
|
||||
path: attachmentPath,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.orElseSucceed(() => Option.none()),
|
||||
);
|
||||
return Option.isSome(info) && info.value.type === "File"
|
||||
? ({ kind: "file", path: attachmentPath } satisfies ResolvedAsset)
|
||||
: null;
|
||||
}
|
||||
@@ -272,7 +409,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
|
||||
if (claims.relativePath === null) {
|
||||
return { kind: "project-favicon-fallback" } satisfies ResolvedAsset;
|
||||
}
|
||||
const faviconPath = yield* resolveCanonicalWorkspaceFile({
|
||||
const faviconPath = yield* resolveCanonicalWorkspaceFileForRequest({
|
||||
workspaceRoot: claims.workspaceRoot,
|
||||
relativePath: claims.relativePath,
|
||||
});
|
||||
@@ -284,7 +421,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
|
||||
const path = yield* Path.Path;
|
||||
if (claims.kind === "workspace-file-exact") {
|
||||
if (decodedPath !== path.basename(claims.relativePath)) return null;
|
||||
const exactWorkspaceFile = yield* resolveCanonicalWorkspaceFile({
|
||||
const exactWorkspaceFile = yield* resolveCanonicalWorkspaceFileForRequest({
|
||||
workspaceRoot: claims.workspaceRoot,
|
||||
relativePath: claims.relativePath,
|
||||
});
|
||||
@@ -303,7 +440,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
|
||||
}
|
||||
const joinedRelativePath =
|
||||
claims.baseRelativePath === "." ? decodedPath : path.join(claims.baseRelativePath, decodedPath);
|
||||
const workspaceFile = yield* resolveCanonicalWorkspaceFile({
|
||||
const workspaceFile = yield* resolveCanonicalWorkspaceFileForRequest({
|
||||
workspaceRoot: claims.workspaceRoot,
|
||||
relativePath: joinedRelativePath,
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ 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 * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
|
||||
import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts";
|
||||
@@ -34,6 +35,12 @@ const writeTextFile = Effect.fn("writeTextFile")(function* (
|
||||
yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
|
||||
});
|
||||
|
||||
const makeResolverWithFileSystem = (fileSystem: FileSystem.FileSystem) =>
|
||||
ProjectFaviconResolver.make.pipe(
|
||||
Effect.provide(WorkspacePaths.layer),
|
||||
Effect.provideService(FileSystem.FileSystem, fileSystem),
|
||||
);
|
||||
|
||||
it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
describe("resolvePath", () => {
|
||||
it.effect("prefers well-known favicon files", () =>
|
||||
@@ -73,5 +80,118 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
|
||||
expect(resolved).toBeNull();
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("preserves workspace normalization context", () =>
|
||||
Effect.gen(function* () {
|
||||
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
const cwd = yield* makeTempDir;
|
||||
const missingCwd = `${cwd}/missing`;
|
||||
|
||||
const error = yield* resolver.resolvePath(missingCwd).pipe(Effect.flip);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
_tag: "ProjectFaviconResolutionError",
|
||||
operation: "normalize-workspace",
|
||||
workspaceRoot: missingCwd,
|
||||
});
|
||||
expect(error.cause).toBeInstanceOf(WorkspacePaths.WorkspaceRootNotExistsError);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("preserves non-missing candidate stat failures", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const cwd = yield* makeTempDir;
|
||||
const faviconPath = path.join(cwd, "favicon.svg");
|
||||
const cause = PlatformError.systemError({
|
||||
_tag: "PermissionDenied",
|
||||
module: "FileSystem",
|
||||
method: "stat",
|
||||
pathOrDescriptor: faviconPath,
|
||||
});
|
||||
const resolver = yield* makeResolverWithFileSystem(
|
||||
FileSystem.FileSystem.of({
|
||||
...fileSystem,
|
||||
stat: (filePath) =>
|
||||
filePath === faviconPath ? Effect.fail(cause) : fileSystem.stat(filePath),
|
||||
}),
|
||||
);
|
||||
|
||||
const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
_tag: "ProjectFaviconResolutionError",
|
||||
operation: "stat-candidate",
|
||||
workspaceRoot: cwd,
|
||||
relativePath: "favicon.svg",
|
||||
absolutePath: faviconPath,
|
||||
});
|
||||
expect(error.cause).toBe(cause);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("preserves icon source read failures", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const cwd = yield* makeTempDir;
|
||||
const sourcePath = path.join(cwd, "index.html");
|
||||
yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="/favicon.svg">');
|
||||
const cause = PlatformError.systemError({
|
||||
_tag: "PermissionDenied",
|
||||
module: "FileSystem",
|
||||
method: "readFileString",
|
||||
pathOrDescriptor: sourcePath,
|
||||
});
|
||||
const resolver = yield* makeResolverWithFileSystem(
|
||||
FileSystem.FileSystem.of({
|
||||
...fileSystem,
|
||||
readFileString: (filePath, options) =>
|
||||
filePath === sourcePath
|
||||
? Effect.fail(cause)
|
||||
: fileSystem.readFileString(filePath, options),
|
||||
}),
|
||||
);
|
||||
|
||||
const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
_tag: "ProjectFaviconResolutionError",
|
||||
operation: "read-source",
|
||||
workspaceRoot: cwd,
|
||||
relativePath: "index.html",
|
||||
absolutePath: sourcePath,
|
||||
});
|
||||
expect(error.cause).toBe(cause);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("skips icon metadata paths outside the workspace", () =>
|
||||
Effect.gen(function* () {
|
||||
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
const cwd = yield* makeTempDir;
|
||||
yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
|
||||
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
|
||||
expect(resolved).toBeNull();
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("continues to later sources after an outside-root icon href", () =>
|
||||
Effect.gen(function* () {
|
||||
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
|
||||
const cwd = yield* makeTempDir;
|
||||
yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
|
||||
yield* writeTextFile(cwd, "public/index.html", '<link rel="icon" href="/brand/logo.svg">');
|
||||
yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
|
||||
|
||||
const resolved = yield* resolver.resolvePath(cwd);
|
||||
|
||||
expect(resolved).not.toBeNull();
|
||||
expect(resolved).toContain("public/brand/logo.svg");
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -10,7 +10,10 @@ import * as Context from "effect/Context";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Path from "effect/Path";
|
||||
import * as PlatformError from "effect/PlatformError";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
|
||||
|
||||
@@ -56,6 +59,26 @@ const LINK_ICON_HTML_RE =
|
||||
const LINK_ICON_OBJ_RE =
|
||||
/(?=[^}]*\brel\s*:\s*["'](?:icon|shortcut icon)["'])(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*/i;
|
||||
|
||||
export class ProjectFaviconResolutionError extends Schema.TaggedErrorClass<ProjectFaviconResolutionError>()(
|
||||
"ProjectFaviconResolutionError",
|
||||
{
|
||||
operation: Schema.Literals([
|
||||
"normalize-workspace",
|
||||
"resolve-path",
|
||||
"stat-candidate",
|
||||
"read-source",
|
||||
]),
|
||||
workspaceRoot: Schema.String,
|
||||
relativePath: Schema.optional(Schema.String),
|
||||
absolutePath: Schema.optional(Schema.String),
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to resolve project favicon during ${this.operation} for workspace ${this.workspaceRoot}.`;
|
||||
}
|
||||
}
|
||||
|
||||
/** Service tag for project favicon resolution. */
|
||||
export class ProjectFaviconResolver extends Context.Service<
|
||||
ProjectFaviconResolver,
|
||||
@@ -65,7 +88,9 @@ export class ProjectFaviconResolver extends Context.Service<
|
||||
*
|
||||
* Returns `null` when no candidate icon file can be found.
|
||||
*/
|
||||
readonly resolvePath: (cwd: string) => Effect.Effect<string | null>;
|
||||
readonly resolvePath: (
|
||||
cwd: string,
|
||||
) => Effect.Effect<string | null, ProjectFaviconResolutionError>;
|
||||
}
|
||||
>()("t3/project/ProjectFaviconResolver") {}
|
||||
|
||||
@@ -77,6 +102,17 @@ function extractIconHref(source: string): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
const optionOnNotFound = <A, R>(
|
||||
effect: Effect.Effect<A, PlatformError.PlatformError, R>,
|
||||
): Effect.Effect<Option.Option<A>, PlatformError.PlatformError, R> =>
|
||||
effect.pipe(
|
||||
Effect.map(Option.some),
|
||||
Effect.catchTags({
|
||||
PlatformError: (error) =>
|
||||
error.reason._tag === "NotFound" ? Effect.succeed(Option.none<A>()) : Effect.fail(error),
|
||||
}),
|
||||
);
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
@@ -90,22 +126,39 @@ export const make = Effect.gen(function* () {
|
||||
const findExistingFile = Effect.fn("ProjectFaviconResolver.findExistingFile")(function* (
|
||||
projectCwd: string,
|
||||
relativeCandidates: ReadonlyArray<string>,
|
||||
): Effect.fn.Return<string | null> {
|
||||
): Effect.fn.Return<string | null, ProjectFaviconResolutionError> {
|
||||
for (const relativePath of relativeCandidates) {
|
||||
const candidate = yield* workspacePaths
|
||||
.resolveRelativePathWithinRoot({
|
||||
workspaceRoot: projectCwd,
|
||||
relativePath,
|
||||
})
|
||||
.pipe(Effect.orElseSucceed(() => null));
|
||||
if (!candidate) {
|
||||
.pipe(
|
||||
Effect.map(Option.some),
|
||||
Effect.catchTags({
|
||||
WorkspacePathOutsideRootError: () =>
|
||||
Effect.succeed(
|
||||
Option.none<{ readonly absolutePath: string; readonly relativePath: string }>(),
|
||||
),
|
||||
}),
|
||||
);
|
||||
if (Option.isNone(candidate)) {
|
||||
continue;
|
||||
}
|
||||
const stats = yield* fileSystem
|
||||
.stat(candidate.absolutePath)
|
||||
.pipe(Effect.orElseSucceed(() => null));
|
||||
if (stats?.type === "File") {
|
||||
return candidate.absolutePath;
|
||||
const stats = yield* optionOnNotFound(fileSystem.stat(candidate.value.absolutePath)).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProjectFaviconResolutionError({
|
||||
operation: "stat-candidate",
|
||||
workspaceRoot: projectCwd,
|
||||
relativePath,
|
||||
absolutePath: candidate.value.absolutePath,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (Option.isSome(stats) && stats.value.type === "File") {
|
||||
return candidate.value.absolutePath;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -114,12 +167,16 @@ export const make = Effect.gen(function* () {
|
||||
const resolvePath: ProjectFaviconResolver["Service"]["resolvePath"] = Effect.fn(
|
||||
"ProjectFaviconResolver.resolvePath",
|
||||
)(function* (cwd) {
|
||||
const projectCwd = yield* workspacePaths
|
||||
.normalizeWorkspaceRoot(cwd)
|
||||
.pipe(Effect.orElseSucceed(() => null));
|
||||
if (!projectCwd) {
|
||||
return null;
|
||||
}
|
||||
const projectCwd = yield* workspacePaths.normalizeWorkspaceRoot(cwd).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProjectFaviconResolutionError({
|
||||
operation: "normalize-workspace",
|
||||
workspaceRoot: cwd,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
for (const candidate of FAVICON_CANDIDATES) {
|
||||
const existing = yield* findExistingFile(projectCwd, [candidate]);
|
||||
if (existing) {
|
||||
@@ -133,17 +190,35 @@ export const make = Effect.gen(function* () {
|
||||
workspaceRoot: projectCwd,
|
||||
relativePath: sourceFile,
|
||||
})
|
||||
.pipe(Effect.orElseSucceed(() => null));
|
||||
if (!sourcePath) {
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProjectFaviconResolutionError({
|
||||
operation: "resolve-path",
|
||||
workspaceRoot: projectCwd,
|
||||
relativePath: sourceFile,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const source = yield* optionOnNotFound(
|
||||
fileSystem.readFileString(sourcePath.absolutePath),
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProjectFaviconResolutionError({
|
||||
operation: "read-source",
|
||||
workspaceRoot: projectCwd,
|
||||
relativePath: sourceFile,
|
||||
absolutePath: sourcePath.absolutePath,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (Option.isNone(source)) {
|
||||
continue;
|
||||
}
|
||||
const source = yield* fileSystem
|
||||
.readFileString(sourcePath.absolutePath)
|
||||
.pipe(Effect.orElseSucceed(() => null));
|
||||
if (!source) {
|
||||
continue;
|
||||
}
|
||||
const href = extractIconHref(source);
|
||||
const href = extractIconHref(source.value);
|
||||
if (!href) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1414,6 +1414,8 @@ const makeWsRpcLayer = (currentSession: EnvironmentAuth.AuthenticatedSession) =>
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "resolve-workspace-context",
|
||||
resource: input.resource,
|
||||
message: "Failed to resolve workspace context.",
|
||||
cause,
|
||||
}),
|
||||
@@ -1421,6 +1423,8 @@ const makeWsRpcLayer = (currentSession: EnvironmentAuth.AuthenticatedSession) =>
|
||||
);
|
||||
if (Option.isNone(thread)) {
|
||||
return yield* new AssetAccessError({
|
||||
operation: "resolve-workspace-context",
|
||||
resource: input.resource,
|
||||
message: "Workspace context was not found.",
|
||||
});
|
||||
}
|
||||
@@ -1430,6 +1434,8 @@ const makeWsRpcLayer = (currentSession: EnvironmentAuth.AuthenticatedSession) =>
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetAccessError({
|
||||
operation: "resolve-workspace-context",
|
||||
resource: input.resource,
|
||||
message: "Failed to resolve workspace context.",
|
||||
cause,
|
||||
}),
|
||||
@@ -1437,6 +1443,8 @@ const makeWsRpcLayer = (currentSession: EnvironmentAuth.AuthenticatedSession) =>
|
||||
);
|
||||
if (Option.isNone(project)) {
|
||||
return yield* new AssetAccessError({
|
||||
operation: "resolve-workspace-context",
|
||||
resource: input.resource,
|
||||
message: "Workspace context was not found.",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -29,9 +29,27 @@ export const AssetCreateUrlResult = Schema.Struct({
|
||||
});
|
||||
export type AssetCreateUrlResult = typeof AssetCreateUrlResult.Type;
|
||||
|
||||
export const AssetAccessOperation = Schema.Literals([
|
||||
"resolve-workspace-context",
|
||||
"normalize-workspace-root",
|
||||
"validate-workspace-path",
|
||||
"validate-preview-type",
|
||||
"inspect-workspace-asset",
|
||||
"locate-workspace-asset",
|
||||
"resolve-workspace",
|
||||
"locate-attachment",
|
||||
"resolve-project-favicon",
|
||||
"inspect-project-favicon",
|
||||
"locate-project-favicon",
|
||||
"load-signing-key",
|
||||
]);
|
||||
export type AssetAccessOperation = typeof AssetAccessOperation.Type;
|
||||
|
||||
export class AssetAccessError extends Schema.TaggedErrorClass<AssetAccessError>()(
|
||||
"AssetAccessError",
|
||||
{
|
||||
operation: AssetAccessOperation,
|
||||
resource: AssetResource,
|
||||
message: TrimmedNonEmptyString,
|
||||
cause: Schema.optional(Schema.Defect()),
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user