Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

filesystem.ts

92 lines3.6 KB

1import * as Schema from "effect/Schema";
2import { NonBlankString, NonNegativeInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
3​
4const FILESYSTEM_BROWSE_PATH_MAX_LENGTH = 4096;
5export const FILESYSTEM_METADATA_PATH_MAX_LENGTH = 4096;
6​
7export const FILESYSTEM_METADATA_BATCH_LIMIT = 64;
8​
9export const FilesystemGetMetadataInput = Schema.Struct({
10 // Absolute host paths, including ~/; clients must anchor relative paths.
11 paths: Schema.Array(
12 NonBlankString.check(Schema.isMaxLength(FILESYSTEM_METADATA_PATH_MAX_LENGTH)),
13 ).check(Schema.isMinLength(1), Schema.isMaxLength(FILESYSTEM_METADATA_BATCH_LIMIT)),
14});
15export type FilesystemGetMetadataInput = typeof FilesystemGetMetadataInput.Type;
16​
17export const FilesystemEntryMetadata = Schema.Struct({
18 kind: Schema.Literals(["file", "directory", "other"]),
19 byteLength: Schema.optionalKey(NonNegativeInt),
20 mimeType: Schema.optionalKey(TrimmedNonEmptyString),
21});
22export type FilesystemEntryMetadata = typeof FilesystemEntryMetadata.Type;
23​
24// Entries follow request order; null means missing, unreadable, or unsupported.
25export const FilesystemGetMetadataResult = Schema.Struct({
26 entries: Schema.Array(Schema.NullOr(FilesystemEntryMetadata)),
27});
28export type FilesystemGetMetadataResult = typeof FilesystemGetMetadataResult.Type;
29​
30export const FilesystemBrowseInput = Schema.Struct({
31 partialPath: TrimmedNonEmptyString.check(Schema.isMaxLength(FILESYSTEM_BROWSE_PATH_MAX_LENGTH)),
32 cwd: Schema.optional(NonBlankString.check(Schema.isMaxLength(FILESYSTEM_BROWSE_PATH_MAX_LENGTH))),
33});
34export type FilesystemBrowseInput = typeof FilesystemBrowseInput.Type;
35​
36export const FilesystemBrowseEntry = Schema.Struct({
37 name: Schema.NonEmptyString,
38 fullPath: Schema.NonEmptyString,
39});
40export type FilesystemBrowseEntry = typeof FilesystemBrowseEntry.Type;
41​
42export const FilesystemBrowseResult = Schema.Struct({
43 parentPath: NonBlankString,
44 entries: Schema.Array(FilesystemBrowseEntry),
45});
46export type FilesystemBrowseResult = typeof FilesystemBrowseResult.Type;
47​
48export const FilesystemBrowseFailure = Schema.Literals([
49 "windows_path_unsupported",
50 "current_project_required",
51 "read_directory_failed",
52]);
53export type FilesystemBrowseFailure = typeof FilesystemBrowseFailure.Type;
54​
55function decodedFilesystemBrowseErrorMessage(props: object): string | undefined {
56 if (!("message" in props)) return undefined;
57 return typeof props.message === "string" ? props.message : undefined;
59​
60export class FilesystemBrowseError extends Schema.TaggedError<FilesystemBrowseError>()(
61 "FilesystemBrowseError",
62 {
63 partialPath: Schema.optional(TrimmedNonEmptyString),
64 cwd: Schema.optional(NonBlankString),
65 failure: Schema.optional(FilesystemBrowseFailure),
66 parentPath: Schema.optional(NonBlankString),
67 platform: Schema.optional(TrimmedNonEmptyString),
68 /** Set when the directory does not exist; older clients ignore it. */
69 pathMissing: Schema.optional(Schema.Boolean),
70 message: TrimmedNonEmptyString,
71 cause: Schema.optional(Schema.Defect()),
72 },
73) {
74 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
75 constructor(props: {
76 readonly partialPath: string;
77 readonly cwd?: string | undefined;
78 readonly failure: FilesystemBrowseFailure;
79 readonly parentPath?: string;
80 readonly platform?: string;
81 readonly pathMissing?: boolean;
82 readonly cause?: unknown;
83 }) {
84 const cwd = props.cwd === undefined ? "" : ` from '${props.cwd}'`;
85 super({
86 ...props,
87 message:
88 decodedFilesystemBrowseErrorMessage(props) ??
89 `Failed to browse filesystem path '${props.partialPath}'${cwd}.`,
90 } as any);
91 }