Use Effect file system in workspace services
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
4abf8b46c5
commit
7e7a18de0c
4 files changed
+99
-136
No files matched your search
@@ -1,4 +1,3 @@
|
||||
// @effect-diagnostics nodeBuiltinImport:off
|
||||
/**
|
||||
* WorkspaceFileSystem - Effect service contract for workspace file mutations.
|
||||
*
|
||||
@@ -7,8 +6,6 @@
|
||||
*
|
||||
* @module WorkspaceFileSystem
|
||||
*/
|
||||
import * as NodeFSP from "node:fs/promises";
|
||||
|
||||
import type {
|
||||
ProjectReadFileInput,
|
||||
ProjectReadFileResult,
|
||||
@@ -19,6 +16,7 @@ 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 Schema from "effect/Schema";
|
||||
|
||||
@@ -140,30 +138,28 @@ export const make = Effect.gen(function* () {
|
||||
relativePath: input.relativePath,
|
||||
});
|
||||
|
||||
const realWorkspaceRoot = yield* Effect.tryPromise({
|
||||
try: () => NodeFSP.realpath(input.cwd),
|
||||
catch: (cause) =>
|
||||
const toOperationError =
|
||||
(
|
||||
operation: WorkspaceFileSystemOperationError["operation"],
|
||||
operationPath: string,
|
||||
resolvedPath = target.absolutePath,
|
||||
) =>
|
||||
(cause: unknown) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: target.absolutePath,
|
||||
operationPath: input.cwd,
|
||||
operation: "realpath-workspace-root",
|
||||
resolvedPath,
|
||||
operationPath,
|
||||
operation,
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
const realTargetPath = yield* Effect.tryPromise({
|
||||
try: () => NodeFSP.realpath(target.absolutePath),
|
||||
catch: (cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: target.absolutePath,
|
||||
operationPath: target.absolutePath,
|
||||
operation: "realpath-target",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
const realWorkspaceRoot = yield* fileSystem
|
||||
.realPath(input.cwd)
|
||||
.pipe(Effect.mapError(toOperationError("realpath-workspace-root", input.cwd)));
|
||||
const realTargetPath = yield* fileSystem
|
||||
.realPath(target.absolutePath)
|
||||
.pipe(Effect.mapError(toOperationError("realpath-target", target.absolutePath)));
|
||||
const relativeRealPath = path.relative(realWorkspaceRoot, realTargetPath);
|
||||
if (
|
||||
relativeRealPath.startsWith(`..${path.sep}`) ||
|
||||
@@ -178,84 +174,45 @@ export const make = Effect.gen(function* () {
|
||||
});
|
||||
}
|
||||
|
||||
return yield* Effect.acquireUseRelease(
|
||||
Effect.tryPromise({
|
||||
try: () => NodeFSP.open(realTargetPath, "r"),
|
||||
catch: (cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
return yield* Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const handle = yield* fileSystem
|
||||
.open(realTargetPath, { flag: "r" })
|
||||
.pipe(Effect.mapError(toOperationError("open", realTargetPath, realTargetPath)));
|
||||
const stat = yield* handle.stat.pipe(
|
||||
Effect.mapError(toOperationError("stat", realTargetPath, realTargetPath)),
|
||||
);
|
||||
if (stat.type !== "File") {
|
||||
return yield* new WorkspacePathNotFileError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: realTargetPath,
|
||||
operationPath: realTargetPath,
|
||||
operation: "open",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
const byteLength = Number(stat.size);
|
||||
const bytesToRead = Math.min(byteLength, PROJECT_READ_FILE_MAX_BYTES);
|
||||
const fileBytes = Option.getOrElse(
|
||||
yield* handle
|
||||
.readAlloc(bytesToRead)
|
||||
.pipe(Effect.mapError(toOperationError("read", realTargetPath, realTargetPath))),
|
||||
() => new Uint8Array(0),
|
||||
);
|
||||
if (fileBytes.includes(0)) {
|
||||
return yield* new WorkspaceBinaryFileError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: realTargetPath,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
relativePath: target.relativePath,
|
||||
contents: new TextDecoder("utf-8").decode(fileBytes),
|
||||
byteLength,
|
||||
truncated: byteLength > PROJECT_READ_FILE_MAX_BYTES,
|
||||
};
|
||||
}),
|
||||
(handle) =>
|
||||
Effect.gen(function* () {
|
||||
const stat = yield* Effect.tryPromise({
|
||||
try: () => handle.stat(),
|
||||
catch: (cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: realTargetPath,
|
||||
operationPath: realTargetPath,
|
||||
operation: "stat",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
if (!stat.isFile()) {
|
||||
return yield* new WorkspacePathNotFileError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: realTargetPath,
|
||||
});
|
||||
}
|
||||
|
||||
const bytesToRead = Math.min(stat.size, PROJECT_READ_FILE_MAX_BYTES);
|
||||
const buffer = Buffer.alloc(bytesToRead);
|
||||
const { bytesRead } = yield* Effect.tryPromise({
|
||||
try: () => handle.read(buffer, 0, bytesToRead, 0),
|
||||
catch: (cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: realTargetPath,
|
||||
operationPath: realTargetPath,
|
||||
operation: "read",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
const fileBytes = buffer.subarray(0, bytesRead);
|
||||
if (fileBytes.includes(0)) {
|
||||
return yield* new WorkspaceBinaryFileError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: realTargetPath,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
relativePath: target.relativePath,
|
||||
contents: new TextDecoder("utf-8").decode(fileBytes),
|
||||
byteLength: stat.size,
|
||||
truncated: stat.size > PROJECT_READ_FILE_MAX_BYTES,
|
||||
};
|
||||
}),
|
||||
(handle) =>
|
||||
Effect.tryPromise({
|
||||
try: () => handle.close(),
|
||||
catch: (cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: realTargetPath,
|
||||
operationPath: realTargetPath,
|
||||
operation: "close",
|
||||
cause,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user