History

Use Effect file system in workspace services

Cursor Agent committed 4 months ago

Commit 7e7a18d1 parent 4abf8b44 files · +96 −133Browse files

Showing the changes to apps/server/src/workspace/WorkspaceFileSystem.ts (1 of 4 files). Show all

Unified · Split

apps/server/src/workspace/WorkspaceFileSystem.ts+51−94
@@ -1,4 +1,3 @@
1−// @effect-diagnostics nodeBuiltinImport:off
21 /**
32 * WorkspaceFileSystem - Effect service contract for workspace file mutations.
43 *
@@ -7,8 +6,6 @@ · 2 unmodified lines
76 *
87 * @module WorkspaceFileSystem
98 */
10−import * as NodeFSP from "node:fs/promises";
11−
129 import type {
1310 ProjectReadFileInput,
1411 ProjectReadFileResult,
@@ -19,6 +16,7 @@ import * as Context from "effect/Context"; · 4 unmodified lines
1916 import * as Effect from "effect/Effect";
2017 import * as FileSystem from "effect/FileSystem";
2118 import * as Layer from "effect/Layer";
19+import * as Option from "effect/Option";
2220 import * as Path from "effect/Path";
2321 import * as Schema from "effect/Schema";
2422
@@ -140,30 +138,28 @@ export const make = Effect.gen(function* () { · 115 unmodified lines
140138 relativePath: input.relativePath,
141139 });
142140
143− const realWorkspaceRoot = yield* Effect.tryPromise({
144− try: () => NodeFSP.realpath(input.cwd),
145− catch: (cause) =>
146− new WorkspaceFileSystemOperationError({
147− workspaceRoot: input.cwd,
148− relativePath: input.relativePath,
149− resolvedPath: target.absolutePath,
150− operationPath: input.cwd,
151− operation: "realpath-workspace-root",
152− cause,
153− }),
154− });
155− const realTargetPath = yield* Effect.tryPromise({
156− try: () => NodeFSP.realpath(target.absolutePath),
157− catch: (cause) =>
141+ const toOperationError =
142+ (
143+ operation: WorkspaceFileSystemOperationError["operation"],
144+ operationPath: string,
145+ resolvedPath = target.absolutePath,
146+ ) =>
147+ (cause: unknown) =>
158148 new WorkspaceFileSystemOperationError({
159149 workspaceRoot: input.cwd,
160150 relativePath: input.relativePath,
161− resolvedPath: target.absolutePath,
162− operationPath: target.absolutePath,
163− operation: "realpath-target",
151+ resolvedPath,
152+ operationPath,
153+ operation,
164154 cause,
165− }),
166− });
155+ });
156+
157+ const realWorkspaceRoot = yield* fileSystem
158+ .realPath(input.cwd)
159+ .pipe(Effect.mapError(toOperationError("realpath-workspace-root", input.cwd)));
160+ const realTargetPath = yield* fileSystem
161+ .realPath(target.absolutePath)
162+ .pipe(Effect.mapError(toOperationError("realpath-target", target.absolutePath)));
167163 const relativeRealPath = path.relative(realWorkspaceRoot, realTargetPath);
168164 if (
169165 relativeRealPath.startsWith(`..${path.sep}`) ||
@@ -178,84 +174,45 @@ export const make = Effect.gen(function* () { · 8 unmodified lines
178174 });
179175 }
180176
181− return yield* Effect.acquireUseRelease(
182− Effect.tryPromise({
183− try: () => NodeFSP.open(realTargetPath, "r"),
184− catch: (cause) =>
185− new WorkspaceFileSystemOperationError({
177+ return yield* Effect.scoped(
178+ Effect.gen(function* () {
179+ const handle = yield* fileSystem
180+ .open(realTargetPath, { flag: "r" })
181+ .pipe(Effect.mapError(toOperationError("open", realTargetPath, realTargetPath)));
182+ const stat = yield* handle.stat.pipe(
183+ Effect.mapError(toOperationError("stat", realTargetPath, realTargetPath)),
184+ );
185+ if (stat.type !== "File") {
186+ return yield* new WorkspacePathNotFileError({
186187 workspaceRoot: input.cwd,
187188 relativePath: input.relativePath,
188189 resolvedPath: realTargetPath,
189− operationPath: realTargetPath,
190− operation: "open",
191− cause,
192− }),
193− }),
194− (handle) =>
195− Effect.gen(function* () {
196− const stat = yield* Effect.tryPromise({
197− try: () => handle.stat(),
198− catch: (cause) =>
199− new WorkspaceFileSystemOperationError({
200− workspaceRoot: input.cwd,
201− relativePath: input.relativePath,
202− resolvedPath: realTargetPath,
203− operationPath: realTargetPath,
204− operation: "stat",
205− cause,
206− }),
207190 });
208− if (!stat.isFile()) {
209− return yield* new WorkspacePathNotFileError({
210− workspaceRoot: input.cwd,
211− relativePath: input.relativePath,
212− resolvedPath: realTargetPath,
213− });
214− }
191+ }
215192
216− const bytesToRead = Math.min(stat.size, PROJECT_READ_FILE_MAX_BYTES);
217− const buffer = Buffer.alloc(bytesToRead);
218− const { bytesRead } = yield* Effect.tryPromise({
219− try: () => handle.read(buffer, 0, bytesToRead, 0),
220− catch: (cause) =>
221− new WorkspaceFileSystemOperationError({
222− workspaceRoot: input.cwd,
223− relativePath: input.relativePath,
224− resolvedPath: realTargetPath,
225− operationPath: realTargetPath,
226− operation: "read",
227− cause,
228− }),
193+ const byteLength = Number(stat.size);
194+ const bytesToRead = Math.min(byteLength, PROJECT_READ_FILE_MAX_BYTES);
195+ const fileBytes = Option.getOrElse(
196+ yield* handle
197+ .readAlloc(bytesToRead)
198+ .pipe(Effect.mapError(toOperationError("read", realTargetPath, realTargetPath))),
199+ () => new Uint8Array(0),
200+ );
201+ if (fileBytes.includes(0)) {
202+ return yield* new WorkspaceBinaryFileError({
203+ workspaceRoot: input.cwd,
204+ relativePath: input.relativePath,
205+ resolvedPath: realTargetPath,
229206 });
230− const fileBytes = buffer.subarray(0, bytesRead);
231− if (fileBytes.includes(0)) {
232− return yield* new WorkspaceBinaryFileError({
233− workspaceRoot: input.cwd,
234− relativePath: input.relativePath,
235− resolvedPath: realTargetPath,
236− });
237− }
207+ }
238208
239− return {
240− relativePath: target.relativePath,
241− contents: new TextDecoder("utf-8").decode(fileBytes),
242− byteLength: stat.size,
243− truncated: stat.size > PROJECT_READ_FILE_MAX_BYTES,
244− };
245− }),
246− (handle) =>
247− Effect.tryPromise({
248− try: () => handle.close(),
249− catch: (cause) =>
250− new WorkspaceFileSystemOperationError({
251− workspaceRoot: input.cwd,
252− relativePath: input.relativePath,
253− resolvedPath: realTargetPath,
254− operationPath: realTargetPath,
255− operation: "close",
256− cause,
257− }),
258− }),
209+ return {
210+ relativePath: target.relativePath,
211+ contents: new TextDecoder("utf-8").decode(fileBytes),
212+ byteLength,
213+ truncated: byteLength > PROJECT_READ_FILE_MAX_BYTES,
214+ };
215+ }),
259216 );
260217 });
261218