Showing the changes to apps/server/src/workspace/WorkspaceFileSystem.ts (1 of 2 files). Show all
Unified · Split
| @@ -1,4 +1,3 @@ | |||
| 1 | // @effect-diagnostics nodeBuiltinImport:off | ||
| 2 | /** | 1 | /** |
| 3 | * WorkspaceFileSystem - Effect service contract for workspace file mutations. | 2 | * WorkspaceFileSystem - Effect service contract for workspace file mutations. |
| 4 | * | 3 | * |
| @@ -7,8 +6,6 @@ · 2 unmodified lines | |||
| 7 | * | 6 | * |
| 8 | * @module WorkspaceFileSystem | 7 | * @module WorkspaceFileSystem |
| 9 | */ | 8 | */ |
| 10 | import * as NodeFSP from "node:fs/promises"; | ||
| 11 | |||
| 12 | import type { | 9 | import type { |
| 13 | ProjectReadFileInput, | 10 | ProjectReadFileInput, |
| 14 | ProjectReadFileResult, | 11 | ProjectReadFileResult, |
| @@ -19,6 +16,7 @@ import * as Context from "effect/Context"; · 4 unmodified lines | |||
| 19 | import * as Effect from "effect/Effect"; | 16 | import * as Effect from "effect/Effect"; |
| 20 | import * as FileSystem from "effect/FileSystem"; | 17 | import * as FileSystem from "effect/FileSystem"; |
| 21 | import * as Layer from "effect/Layer"; | 18 | import * as Layer from "effect/Layer"; |
| 19 | import * as Option from "effect/Option"; | ||
| 22 | import * as Path from "effect/Path"; | 20 | import * as Path from "effect/Path"; |
| 23 | import * as Schema from "effect/Schema"; | 21 | import * as Schema from "effect/Schema"; |
| 24 | 22 | ||
| @@ -140,30 +138,28 @@ export const make = Effect.gen(function* () { · 115 unmodified lines | |||
| 140 | relativePath: input.relativePath, | 138 | relativePath: input.relativePath, |
| 141 | }); | 139 | }); |
| 142 | 140 | ||
| 143 | const realWorkspaceRoot = yield* Effect.tryPromise({ | 141 | const toOperationError = |
| 144 | try: () => NodeFSP.realpath(input.cwd), | 142 | ( |
| 145 | catch: (cause) => | 143 | operation: WorkspaceFileSystemOperationError["operation"], |
| 144 | operationPath: string, | ||
| 145 | resolvedPath = target.absolutePath, | ||
| 146 | ) => | ||
| 147 | (cause: unknown) => | ||
| 146 | new WorkspaceFileSystemOperationError({ | 148 | new WorkspaceFileSystemOperationError({ |
| 147 | workspaceRoot: input.cwd, | 149 | workspaceRoot: input.cwd, |
| 148 | relativePath: input.relativePath, | 150 | relativePath: input.relativePath, |
| 149 | resolvedPath: target.absolutePath, | 151 | resolvedPath, |
| 150 | operationPath: input.cwd, | 152 | operationPath, |
| 151 | operation: "realpath-workspace-root", | 153 | operation, |
| 152 | cause, | 154 | cause, |
| 153 | }), | 155 | }); |
| 154 | }); | 156 | |
| 155 | const realTargetPath = yield* Effect.tryPromise({ | 157 | const realWorkspaceRoot = yield* fileSystem.realPath(input.cwd).pipe( |
| 156 | try: () => NodeFSP.realpath(target.absolutePath), | 158 | Effect.mapError(toOperationError("realpath-workspace-root", input.cwd)), |
| 157 | catch: (cause) => | 159 | ); |
| 158 | new WorkspaceFileSystemOperationError({ | 160 | const realTargetPath = yield* fileSystem.realPath(target.absolutePath).pipe( |
| 159 | workspaceRoot: input.cwd, | 161 | Effect.mapError(toOperationError("realpath-target", target.absolutePath)), |
| 160 | relativePath: input.relativePath, | 162 | ); |
| 161 | resolvedPath: target.absolutePath, | ||
| 162 | operationPath: target.absolutePath, | ||
| 163 | operation: "realpath-target", | ||
| 164 | cause, | ||
| 165 | }), | ||
| 166 | }); | ||
| 167 | const relativeRealPath = path.relative(realWorkspaceRoot, realTargetPath); | 163 | const relativeRealPath = path.relative(realWorkspaceRoot, realTargetPath); |
| 168 | if ( | 164 | if ( |
| 169 | relativeRealPath.startsWith(`..${path.sep}`) || | 165 | relativeRealPath.startsWith(`..${path.sep}`) || |
| @@ -178,84 +174,49 @@ export const make = Effect.gen(function* () { · 8 unmodified lines | |||
| 178 | }); | 174 | }); |
| 179 | } | 175 | } |
| 180 | 176 | ||
| 181 | return yield* Effect.acquireUseRelease( | 177 | return yield* Effect.scoped( |
| 182 | Effect.tryPromise({ | 178 | Effect.gen(function* () { |
| 183 | try: () => NodeFSP.open(realTargetPath, "r"), | 179 | const file = yield* fileSystem.open(realTargetPath, { flag: "r" }).pipe( |
| 184 | catch: (cause) => | 180 | Effect.mapError(toOperationError("open", realTargetPath, realTargetPath)), |
| 185 | new WorkspaceFileSystemOperationError({ | 181 | ); |
| 182 | const stat = yield* file.stat.pipe( | ||
| 183 | Effect.mapError(toOperationError("stat", realTargetPath, realTargetPath)), | ||
| 184 | ); | ||
| 185 | if (stat.type !== "File") { | ||
| 186 | return yield* new WorkspacePathNotFileError({ | ||
| 186 | workspaceRoot: input.cwd, | 187 | workspaceRoot: input.cwd, |
| 187 | relativePath: input.relativePath, | 188 | relativePath: input.relativePath, |
| 188 | resolvedPath: realTargetPath, | 189 | 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 | }), | ||
| 207 | }); | 190 | }); |
| 208 | if (!stat.isFile()) { | 191 | } |
| 209 | return yield* new WorkspacePathNotFileError({ | ||
| 210 | workspaceRoot: input.cwd, | ||
| 211 | relativePath: input.relativePath, | ||
| 212 | resolvedPath: realTargetPath, | ||
| 213 | }); | ||
| 214 | } | ||
| 215 | 192 | ||
| 216 | const bytesToRead = Math.min(stat.size, PROJECT_READ_FILE_MAX_BYTES); | 193 | const byteLength = Number(stat.size); |
| 217 | const buffer = Buffer.alloc(bytesToRead); | 194 | const truncated = stat.size > BigInt(PROJECT_READ_FILE_MAX_BYTES); |
| 218 | const { bytesRead } = yield* Effect.tryPromise({ | 195 | const bytesToRead = truncated ? PROJECT_READ_FILE_MAX_BYTES : byteLength; |
| 219 | try: () => handle.read(buffer, 0, bytesToRead, 0), | 196 | const fileBytes = |
| 220 | catch: (cause) => | 197 | bytesToRead === 0 |
| 221 | new WorkspaceFileSystemOperationError({ | 198 | ? new Uint8Array() |
| 222 | workspaceRoot: input.cwd, | 199 | : Option.getOrElse( |
| 223 | relativePath: input.relativePath, | 200 | yield* file.readAlloc(bytesToRead).pipe( |
| 224 | resolvedPath: realTargetPath, | 201 | Effect.mapError(toOperationError("read", realTargetPath, realTargetPath)), |
| 225 | operationPath: realTargetPath, | 202 | ), |
| 226 | operation: "read", | 203 | () => new Uint8Array(), |
| 227 | cause, | 204 | ); |
| 228 | }), | 205 | if (fileBytes.includes(0)) { |
| 206 | return yield* new WorkspaceBinaryFileError({ | ||
| 207 | workspaceRoot: input.cwd, | ||
| 208 | relativePath: input.relativePath, | ||
| 209 | resolvedPath: realTargetPath, | ||
| 229 | }); | 210 | }); |
| 230 | const fileBytes = buffer.subarray(0, bytesRead); | 211 | } |
| 231 | if (fileBytes.includes(0)) { | ||
| 232 | return yield* new WorkspaceBinaryFileError({ | ||
| 233 | workspaceRoot: input.cwd, | ||
| 234 | relativePath: input.relativePath, | ||
| 235 | resolvedPath: realTargetPath, | ||
| 236 | }); | ||
| 237 | } | ||
| 238 | 212 | ||
| 239 | return { | 213 | return { |
| 240 | relativePath: target.relativePath, | 214 | relativePath: target.relativePath, |
| 241 | contents: new TextDecoder("utf-8").decode(fileBytes), | 215 | contents: new TextDecoder("utf-8").decode(fileBytes), |
| 242 | byteLength: stat.size, | 216 | byteLength, |
| 243 | truncated: stat.size > PROJECT_READ_FILE_MAX_BYTES, | 217 | truncated, |
| 244 | }; | 218 | }; |
| 245 | }), | 219 | }), |
| 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 | }), | ||
| 259 | ); | 220 | ); |
| 260 | }); | 221 | }); |
| 261 | 222 | ||