The best way to control Claude Code, Codex and more from any computer or phone
fix(server): serve static files when the platform cannot open file handles (#14161)
Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com> Co-authored-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Pujitha Paladugu committed
Oct 19, 2026 at 13:24 UTC
1d172c3124a1c435dbf6e666417612d338a81419
2 files changed
+225
-19
apps/server/src/http.test.ts
+175
@@ -11,6 +11,7 @@ import * as Fiber from "effect/Fiber";
11
import * as FileSystem from "effect/FileSystem";
12
import * as Layer from "effect/Layer";
13
import * as Path from "effect/Path";
14
+import * as PlatformError from "effect/PlatformError";
15
import * as Queue from "effect/Queue";
16
import * as Tracer from "effect/Tracer";
17
import { HttpClient, HttpClientRequest, HttpRouter, HttpServerResponse } from "effect/http";
@@ -257,6 +258,180 @@ it.layer(
258
}),
259
);
260
261
+ it.effect("serves static files without handles and skips fallback reads for HEAD and 304", () =>
262
+ Effect.gen(function* () {
263
+ const fs = yield* FileSystem.FileSystem;
264
+ const path = yield* Path.Path;
265
+ const staticDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-static-no-open-" });
266
+ const filePath = path.join(staticDir, "app.txt");
267
+ const body = "packaged asset";
268
+ yield* fs.writeFileString(filePath, body);
269
+ let reads = 0;
270
+ // Electron asar files can be statted and read even when opening a handle fails.
271
+ const fileSystem = FileSystem.FileSystem.of({
272
+ ...fs,
273
+ open: (candidate, options) =>
274
+ candidate === filePath
275
+ ? Effect.fail(
276
+ PlatformError.systemError({
277
+ _tag: "NotFound",
278
+ module: "FileSystem",
279
+ method: "open",
280
+ pathOrDescriptor: candidate,
281
+ }),
282
+ )
283
+ : fs.open(candidate, options),
284
+ readFile: (candidate) =>
285
+ Effect.gen(function* () {
286
+ if (candidate === filePath) reads += 1;
287
+ return yield* fs.readFile(candidate);
288
+ }),
289
+ });
290
+ const request = yield* makeStaticRequest(staticDir).pipe(
291
+ Effect.provideService(FileSystem.FileSystem, fileSystem),
292
+ );
293
+
294
+ const get = yield* request("/app.txt");
295
+ expect(get.status).toBe(200);
296
+ expect(yield* get.text).toBe(body);
297
+ expect(reads).toBeGreaterThan(0);
298
+ const readsAfterGet = reads;
299
+ const head = yield* request("/app.txt", { method: "HEAD" });
300
+ expect(head.status).toBe(200);
301
+ expect(yield* head.text).toBe("");
302
+ expect(reads).toBe(readsAfterGet);
303
+ const unchanged = yield* request("/app.txt", {
304
+ headers: { "if-none-match": get.headers["etag"]! },
305
+ });
306
+ expect(unchanged.status).toBe(304);
307
+ expect(yield* unchanged.text).toBe("");
308
+ expect(reads).toBe(readsAfterGet);
309
+ }),
310
+ );
311
+
312
+ it.effect("returns 500 for static file open errors other than NotFound", () =>
313
+ Effect.gen(function* () {
314
+ const fs = yield* FileSystem.FileSystem;
315
+ const path = yield* Path.Path;
316
+ const staticDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-static-open-error-" });
317
+ const filePath = path.join(staticDir, "index.html");
318
+ yield* fs.writeFileString(filePath, "<html>readable</html>");
319
+ const fileSystem = FileSystem.FileSystem.of({
320
+ ...fs,
321
+ open: (candidate, options) =>
322
+ candidate === filePath
323
+ ? Effect.fail(
324
+ PlatformError.systemError({
325
+ _tag: "PermissionDenied",
326
+ module: "FileSystem",
327
+ method: "open",
328
+ pathOrDescriptor: candidate,
329
+ }),
330
+ )
331
+ : fs.open(candidate, options),
332
+ });
333
+ const request = yield* makeStaticRequest(staticDir).pipe(
334
+ Effect.provideService(FileSystem.FileSystem, fileSystem),
335
+ );
336
+
337
+ const response = yield* request("/");
338
+ expect(response.status).toBe(500);
339
+ expect(yield* response.text).toBe("Internal Server Error");
340
+ }),
341
+ );
342
+
343
+ it.effect("returns 500 when a static fallback read fails", () =>
344
+ Effect.gen(function* () {
345
+ const fs = yield* FileSystem.FileSystem;
346
+ const path = yield* Path.Path;
347
+ const staticDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-static-read-error-" });
348
+ const filePath = path.join(staticDir, "index.html");
349
+ yield* fs.writeFileString(filePath, "<html>unreadable</html>");
350
+ const fileSystem = FileSystem.FileSystem.of({
351
+ ...fs,
352
+ open: (candidate, options) =>
353
+ candidate === filePath
354
+ ? Effect.fail(
355
+ PlatformError.systemError({
356
+ _tag: "NotFound",
357
+ module: "FileSystem",
358
+ method: "open",
359
+ pathOrDescriptor: candidate,
360
+ }),
361
+ )
362
+ : fs.open(candidate, options),
363
+ readFile: (candidate) =>
364
+ candidate === filePath
365
+ ? Effect.fail(
366
+ PlatformError.systemError({
367
+ _tag: "PermissionDenied",
368
+ module: "FileSystem",
369
+ method: "readFile",
370
+ pathOrDescriptor: candidate,
371
+ }),
372
+ )
373
+ : fs.readFile(candidate),
374
+ });
375
+ const request = yield* makeStaticRequest(staticDir).pipe(
376
+ Effect.provideService(FileSystem.FileSystem, fileSystem),
377
+ );
378
+
379
+ const response = yield* request("/");
380
+ expect(response.status).toBe(500);
381
+ expect(yield* response.text).toBe("Internal Server Error");
382
+ }),
383
+ );
384
+
385
+ it.effect("serves replacement bytes with matching static fallback length and validators", () =>
386
+ Effect.gen(function* () {
387
+ const fs = yield* FileSystem.FileSystem;
388
+ const path = yield* Path.Path;
389
+ const staticDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-static-replaced-" });
390
+ const filePath = path.join(staticDir, "app.txt");
391
+ const before = DateTime.toDateUtc(DateTime.makeUnsafe("1985-10-26T08:15:00.000Z"));
392
+ const after = DateTime.toDateUtc(DateTime.makeUnsafe("2015-10-21T16:29:00.000Z"));
393
+ const replacement = "the replacement asset";
394
+ const replacementPath = path.join(staticDir, "replacement.txt");
395
+ yield* fs.writeFileString(filePath, "old");
396
+ yield* fs.utimes(filePath, before, before);
397
+ yield* fs.writeFileString(replacementPath, replacement);
398
+ yield* fs.utimes(replacementPath, after, after);
399
+ let replaceOnOpen = false;
400
+ const fileSystem = FileSystem.FileSystem.of({
401
+ ...fs,
402
+ open: (candidate, options) =>
403
+ Effect.gen(function* () {
404
+ if (candidate !== filePath) return yield* fs.open(candidate, options);
405
+ if (replaceOnOpen) {
406
+ yield* fs.rename(replacementPath, filePath);
407
+ replaceOnOpen = false;
408
+ }
409
+ return yield* PlatformError.systemError({
410
+ _tag: "NotFound",
411
+ module: "FileSystem",
412
+ method: "open",
413
+ pathOrDescriptor: candidate,
414
+ });
415
+ }),
416
+ });
417
+ const request = yield* makeStaticRequest(staticDir).pipe(
418
+ Effect.provideService(FileSystem.FileSystem, fileSystem),
419
+ );
420
+ const initial = yield* request("/app.txt", { headers: { "accept-encoding": "identity" } });
421
+ expect(yield* initial.text).toBe("old");
422
+
423
+ replaceOnOpen = true;
424
+ const response = yield* request("/app.txt", { headers: { "accept-encoding": "identity" } });
425
+ expect(response.status).toBe(200);
426
+ expect(response.headers).toMatchObject({
427
+ "content-length": String(Buffer.byteLength(replacement)),
428
+ "last-modified": after.toUTCString(),
429
+ });
430
+ expect(response.headers["etag"]).not.toBe(initial.headers["etag"]);
431
+ expect(yield* response.text).toBe(replacement);
432
+ }),
433
+ );
434
+
435
it.effect("serves changed HTML when deployments preserve its size and timestamp", () =>
436
Effect.gen(function* () {
437
const fs = yield* FileSystem.FileSystem;
apps/server/src/http.ts
+50
-19
@@ -7,6 +7,7 @@ import {
7
import { isDevProxiedPath } from "@t3tools/shared/devProxy";
8
import { injectMcpAppCookieBootstrap, injectMcpAppThemeBootstrap } from "@t3tools/shared/mcpApp";
9
import { decodeOtlpTraceRecords } from "@t3tools/shared/observability";
10
+import * as ByteSize from "effect/ByteSize";
11
import * as Data from "effect/Data";
12
import * as Effect from "effect/Effect";
13
import * as FileSystem from "effect/FileSystem";
@@ -533,8 +534,15 @@ const openStaticFile = Effect.fn("openStaticFile")(function* (filePath: string)
534
const fileSystem = yield* FileSystem.FileSystem;
535
const pathInfo = yield* fileSystem.stat(filePath).pipe(Effect.orElseSucceed(() => null));
536
if (pathInfo?.type !== "File") return null;
536
- const file = yield* fileSystem.open(filePath, { flag: "r" });
537
- const info = yield* file.stat;
537
+ const file = yield* fileSystem.open(filePath, { flag: "r" }).pipe(
538
+ Effect.catchTags({
539
+ PlatformError: (error) =>
540
+ error.reason._tag === "NotFound" ? Effect.succeed(null) : Effect.fail(error),
541
+ }),
542
+ );
543
+ // Electron asar can stat and read files even when opening a handle returns NotFound.
544
+ // Refresh path metadata because the file may have been replaced since the first stat.
545
+ const info = yield* file === null ? fileSystem.stat(filePath) : file.stat;
546
return info.type === "File" ? { file, info } : null;
547
});
548
@@ -629,18 +637,23 @@ const handleStaticAndDevRequest = Effect.fn("handleStaticAndDevRequest")(
637
!isHtml &&
638
/^assets\/.+-[\w-]{8}\.[^/]+$/.test(relativePath) &&
639
immutableBuildAssets.has(relativePath);
640
+ const cacheControl = immutable ? "public, max-age=31536000, immutable" : "no-cache";
641
+ const validators = (info: FileSystem.File.Info) => {
642
+ // Deployments can preserve HTML size and mtime while changing its bundle URLs.
643
+ const modifiedAt = isHtml ? undefined : Option.getOrUndefined(info.mtime);
644
+ return modifiedAt
645
+ ? {
646
+ ETag: `W/"${info.size.toString(16)}-${modifiedAt.getTime().toString(16)}"`,
647
+ "Last-Modified": modifiedAt.toUTCString(),
648
+ }
649
+ : {};
650
+ };
651
const headers: Record<string, string> = {
633
- "Cache-Control": immutable ? "public, max-age=31536000, immutable" : "no-cache",
652
+ "Cache-Control": cacheControl,
653
+ ...validators(fileInfo),
654
};
635
- // Deployments can preserve HTML size and mtime while changing its bundle URLs.
636
- const modifiedAt = isHtml ? undefined : Option.getOrUndefined(fileInfo.mtime);
637
- const etag = modifiedAt
638
- ? `W/"${fileInfo.size.toString(16)}-${modifiedAt.getTime().toString(16)}"`
639
- : undefined;
640
- if (etag !== undefined && modifiedAt !== undefined) {
641
- headers.ETag = etag;
642
- headers["Last-Modified"] = modifiedAt.toUTCString();
643
- }
655
+ const etag = headers.ETag;
656
+ const lastModified = headers["Last-Modified"];
657
658
// If-None-Match takes precedence over dates and uses weak comparison for
659
// GET/HEAD, including when compression changes the transferred bytes.
@@ -656,8 +669,8 @@ const handleStaticAndDevRequest = Effect.fn("handleStaticAndDevRequest")(
669
);
670
})
671
: ifModifiedSince !== undefined &&
659
- modifiedAt !== undefined &&
660
- Date.parse(modifiedAt.toUTCString()) <= Date.parse(ifModifiedSince);
672
+ lastModified !== undefined &&
673
+ Date.parse(lastModified) <= Date.parse(ifModifiedSince);
674
if (!isHtml && unchanged) {
675
return HttpServerResponse.empty({
676
status: 304,
@@ -666,11 +679,29 @@ const handleStaticAndDevRequest = Effect.fn("handleStaticAndDevRequest")(
679
}
680
681
const contentType = isHtml ? "text/html; charset=utf-8" : mimeType;
669
- return HttpServerResponse.stream(streamStaticFile(opened.file, fileInfo.size), {
670
- headers,
671
- contentType,
672
- contentLength: Number(fileInfo.size),
673
- });
682
+ if (opened.file === null && request.method !== "HEAD") {
683
+ const fileSystem = yield* FileSystem.FileSystem;
684
+ // Complete fallback reads before success headers; stream errors arrive too late for a 500.
685
+ const bytes = yield* fileSystem.readFile(filePath);
686
+ const info = yield* fileSystem.stat(filePath);
687
+ return HttpServerResponse.uint8Array(bytes, {
688
+ headers: {
689
+ "Cache-Control": cacheControl,
690
+ ...validators({ ...info, size: ByteSize.bytes(bytes.byteLength) }),
691
+ },
692
+ contentType,
693
+ });
694
+ }
695
+ // The request scope closes the handle for GET, HEAD, 304, errors, and cancellation.
696
+ // HEAD still passes through compression, which selects headers without reading the stream.
697
+ return HttpServerResponse.stream(
698
+ opened.file === null ? Stream.empty : streamStaticFile(opened.file, fileInfo.size),
699
+ {
700
+ headers,
701
+ contentType,
702
+ contentLength: Number(fileInfo.size),
703
+ },
704
+ );
705
},
706
Effect.catchTags({
707
PlatformError: () =>