Skip to content

Commit 1658d52

Browse files
committed
fix(server): finish short reads before returning workspace text
1 parent 17c0878 commit 1658d52

2 files changed

Lines changed: 72 additions & 13 deletions

File tree

‎apps/server/src/workspace/WorkspaceFileSystem.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @effect-diagnostics nodeBuiltinImport:off - FileSystem cannot create a FIFO.
22
import * as NodeChildProcess from "node:child_process";
3+
import * as NodeFSP from "node:fs/promises";
4+
import { vi } from "vite-plus/test";
35

46
import * as NodeServices from "@effect/platform-node/NodeServices";
57
import { it, describe, expect } from "@effect/vitest";
@@ -17,6 +19,27 @@ import * as WorkspacePaths from "./WorkspacePaths.ts";
1719
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
1820
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
1921

22+
vi.mock("node:fs/promises", async (importOriginal) => {
23+
const actual = await importOriginal<typeof NodeFSP>();
24+
return { ...actual, open: vi.fn(actual.open) };
25+
});
26+
27+
const injectShortReads = Effect.fn("injectShortReads")(function* (target: string) {
28+
const native = yield* Effect.promise(() => vi.importActual<typeof NodeFSP>("node:fs/promises"));
29+
const open = vi.mocked(NodeFSP.open).mockImplementation(async (path, flags, mode) => {
30+
const handle = await native.open(path, flags, mode);
31+
if (path === target) {
32+
const read = handle.read.bind(handle);
33+
Object.defineProperty(handle, "read", {
34+
value: (buffer: Uint8Array, offset: number, length: number, position: number) =>
35+
read(buffer, offset, Math.min(length, 2), position),
36+
});
37+
}
38+
return handle;
39+
});
40+
yield* Effect.addFinalizer(() => Effect.sync(() => open.mockImplementation(native.open)));
41+
});
42+
2043
const layerProject = WorkspaceFileSystem.layer.pipe(
2144
Layer.provide(WorkspacePaths.layer),
2245
Layer.provide(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
@@ -78,6 +101,37 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
78101
}),
79102
);
80103

104+
it.effect("finishes short reads before decoding UTF-8 text", () =>
105+
Effect.gen(function* () {
106+
const files = yield* WorkspaceFileSystem.WorkspaceFileSystem;
107+
const fs = yield* FileSystem.FileSystem;
108+
const cwd = yield* makeTempDir;
109+
const contents = "A café 🐈\n";
110+
yield* writeTextFile(cwd, "short.txt", contents);
111+
const target = yield* fs.realPath(`${cwd}/short.txt`);
112+
yield* injectShortReads(target);
113+
expect(yield* files.readFile({ cwd, relativePath: "short.txt" })).toEqual({
114+
relativePath: "short.txt",
115+
contents,
116+
byteLength: Buffer.byteLength(contents),
117+
truncated: false,
118+
});
119+
}),
120+
);
121+
122+
it.effect("rejects binary bytes delivered after an initial short read", () =>
123+
Effect.gen(function* () {
124+
const files = yield* WorkspaceFileSystem.WorkspaceFileSystem;
125+
const fs = yield* FileSystem.FileSystem;
126+
const cwd = yield* makeTempDir;
127+
yield* writeTextFile(cwd, "short.txt", "ab\0cd");
128+
const target = yield* fs.realPath(`${cwd}/short.txt`);
129+
yield* injectShortReads(target);
130+
const error = yield* files.readFile({ cwd, relativePath: "short.txt" }).pipe(Effect.flip);
131+
expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError);
132+
}),
133+
);
134+
81135
it.effect("reads host files outside the workspace root by absolute path", () =>
82136
Effect.gen(function* () {
83137
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;

‎apps/server/src/workspace/WorkspaceFileSystem.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,24 @@ export const make = Effect.gen(function* () {
258258

259259
const bytesToRead = Math.min(stat.size, PROJECT_READ_FILE_MAX_BYTES);
260260
const buffer = Buffer.alloc(bytesToRead);
261-
const { bytesRead } = yield* Effect.tryPromise({
262-
try: () => handle.read(buffer, 0, bytesToRead, 0),
263-
catch: (cause) =>
264-
new WorkspaceFileSystemOperationError({
265-
workspaceRoot: input.cwd,
266-
relativePath: input.relativePath,
267-
resolvedPath: realTargetPath,
268-
operationPath: realTargetPath,
269-
operation: "read",
270-
cause,
271-
}),
272-
});
273-
const fileBytes = buffer.subarray(0, bytesRead);
261+
let offset = 0;
262+
while (offset < bytesToRead) {
263+
const { bytesRead } = yield* Effect.tryPromise({
264+
try: () => handle.read(buffer, offset, bytesToRead - offset, offset),
265+
catch: (cause) =>
266+
new WorkspaceFileSystemOperationError({
267+
workspaceRoot: input.cwd,
268+
relativePath: input.relativePath,
269+
resolvedPath: realTargetPath,
270+
operationPath: realTargetPath,
271+
operation: "read",
272+
cause,
273+
}),
274+
});
275+
if (bytesRead <= 0) break;
276+
offset += bytesRead;
277+
}
278+
const fileBytes = buffer.subarray(0, offset);
274279
if (fileBytes.includes(0)) {
275280
return yield* new WorkspaceBinaryFileError({
276281
workspaceRoot: input.cwd,

0 commit comments

Comments
 (0)