fix(web): use server metadata for file chip icons (#17923)

This commit is contained in:
Yash Singh authored and GitHub committed 2026-10-10 17:32:29 -05:00
1 parent f92afe79c9
commit 0442f5bfcc
34 files changed
+1565 -33

No files matched your search

@@ -13,11 +13,16 @@ import * as NodeFS from "node:fs";
import * as NodeFSP from "node:fs/promises";
import type {
FilesystemEntryMetadata,
FilesystemGetMetadataInput,
FilesystemGetMetadataResult,
ProjectReadFileInput,
ProjectReadFileResult,
ProjectWriteFileInput,
ProjectWriteFileResult,
} from "@t3tools/contracts";
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
@@ -27,6 +32,7 @@ import * as Schema from "effect/Schema";
import * as WorkspaceEntries from "./WorkspaceEntries.ts";
import * as WorkspacePaths from "./WorkspacePaths.ts";
import { fileHeaderMimeType } from "./fileHeaderMimeType.ts";
const PROJECT_READ_FILE_MAX_BYTES = 1024 * 1024;
@@ -107,6 +113,10 @@ export type WorkspaceFileSystemError = typeof WorkspaceFileSystemError.Type;
export class WorkspaceFileSystem extends Context.Service<
WorkspaceFileSystem,
{
readonly getMetadata: (
input: FilesystemGetMetadataInput,
) => Effect.Effect<FilesystemGetMetadataResult>;
/**
* Read a UTF-8 text file relative to the workspace root, or any host file by
* absolute path.
@@ -139,6 +149,51 @@ export const make = Effect.gen(function* () {
const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
const metadataForPath = Effect.fnUntraced(function* (requestedPath: string) {
const expandedPath = expandHomePath(requestedPath, yield* HostProcess.HomeDirectory);
if (!path.isAbsolute(expandedPath)) return null;
const stat = yield* fileSystem.stat(expandedPath).pipe(Effect.option);
if (stat._tag === "None") return null;
if (stat.value.type === "Directory") return { kind: "directory" } as const;
if (stat.value.type !== "File") return { kind: "other" } as const;
let mimeType: string | undefined;
// Named extensions already choose the icon without a content read. For an
// extensionless regular file, read at most 512 bytes, never the whole file.
if (path.extname(expandedPath) === "" && stat.value.size > 0) {
mimeType = yield* Effect.tryPromise({
try: async () => {
const handle = await NodeFSP.open(
expandedPath,
NodeFS.constants.O_RDONLY | NodeFS.constants.O_NONBLOCK,
);
try {
if (!(await handle.stat()).isFile()) return undefined;
const buffer = Buffer.alloc(512);
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
return fileHeaderMimeType(buffer.subarray(0, bytesRead));
} finally {
await handle.close();
}
},
catch: () => undefined,
}).pipe(Effect.catch(() => Effect.succeed(undefined)));
}
return {
kind: "file",
byteLength: Number(stat.value.size),
...(mimeType === undefined ? {} : { mimeType }),
} satisfies FilesystemEntryMetadata;
});
const getMetadata: WorkspaceFileSystem["Service"]["getMetadata"] = Effect.fn(
"WorkspaceFileSystem.getMetadata",
)(function* (input) {
const paths = [...new Set(input.paths)];
const entries = yield* Effect.forEach(paths, metadataForPath, { concurrency: 8 });
const byPath = new Map(paths.map((value, index) => [value, entries[index] ?? null]));
return { entries: input.paths.map((value) => byPath.get(value) ?? null) };
});
/**
* Resolves the file a read targets. Workspace-relative paths must stay inside the
* root, symlinks included. An absolute path reads a host file in place, such as a
@@ -340,7 +395,7 @@ export const make = Effect.gen(function* () {
return { relativePath: target.relativePath };
});
return WorkspaceFileSystem.of({ readFile, writeFile });
return WorkspaceFileSystem.of({ getMetadata, readFile, writeFile });
});
export const layer = Layer.effect(WorkspaceFileSystem, make);