Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NFS from "node:fs";
3import * as path from "node:path";
4import { execFileSync, spawn } from "node:child_process";
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import { assert, it } from "@effect/vitest";
7import * as FileSystem from "effect/FileSystem";
8import * as Schema from "effect/Schema";
9import * as Duration from "effect/Duration";
10import * as Effect from "effect/Effect";
11import * as Fiber from "effect/Fiber";
12import * as TestClock from "effect/testing/TestClock";
13import { vi } from "vitest";
14
15import { readBootstrapEnvelope, resolveFdPath } from "./bootstrap.ts";
16import { assertNone, assertSome } from "@effect/vitest/utils";
17
18const openSyncInterceptor = vi.hoisted(() => ({ failPath: null as string | null }));
19
20vi.mock("node:fs", async (importOriginal) => {
21 const actual = await importOriginal<typeof import("node:fs")>();
22 return {
23 ...actual,
24 openSync: (...args: Parameters<typeof actual.openSync>) => {
25 const [filePath, flags] = args;
26 if (
27 typeof filePath === "string" &&
28 filePath === openSyncInterceptor.failPath &&
29 flags === "r"
30 ) {
31 const error = new Error("no such device or address");
32 Object.assign(error, { code: "ENXIO" });
33 throw error;
34 }
35 return (actual.openSync as (...a: typeof args) => number)(...args);
36 },
37 };
38});
39
40const TestEnvelopeSchema = Schema.Struct({ mode: Schema.String });
41const encodeTestEnvelopeSchema = Schema.encodeEffect(Schema.fromJsonString(TestEnvelopeSchema));
42
43it.layer(NodeServices.layer)("readBootstrapEnvelope", (it) => {
44 it.effect("uses platform-specific fd paths", () =>
45 Effect.sync(() => {
46 assert.equal(resolveFdPath(3, "linux"), "/proc/self/fd/3");
47 assert.equal(resolveFdPath(3, "darwin"), "/dev/fd/3");
48 assert.equal(resolveFdPath(3, "win32"), undefined);
49 }),
50 );
51
52 it.effect("reads a bootstrap envelope from a provided fd", () =>
53 Effect.gen(function* () {
54 const fs = yield* FileSystem.FileSystem;
55 const filePath = yield* fs.makeTempFileScoped({ prefix: "t3-bootstrap-", suffix: ".ndjson" });
56
57 yield* fs.writeFileString(
58 filePath,
59 `${yield* encodeTestEnvelopeSchema({ mode: "desktop" })}\n`,
60 );
61
62 const fd = yield* Effect.acquireRelease(
63 Effect.sync(() => NFS.openSync(filePath, "r")),
64 (fd) => Effect.sync(() => NFS.closeSync(fd)),
65 );
66
67 const payload = yield* readBootstrapEnvelope(TestEnvelopeSchema, fd, { timeoutMs: 100 });
68 assertSome(payload, {
69 mode: "desktop",
70 });
71 }),
72 );
73
74 it.effect("falls back to reading the inherited fd when path duplication fails", () =>
75 Effect.gen(function* () {
76 const fs = yield* FileSystem.FileSystem;
77 const filePath = yield* fs.makeTempFileScoped({ prefix: "t3-bootstrap-", suffix: ".ndjson" });
78
79 yield* fs.writeFileString(
80 filePath,
81 `${yield* encodeTestEnvelopeSchema({ mode: "desktop" })}\n`,
82 );
83
84 // Open without acquireRelease: the direct-stream fallback uses autoClose: true,
85 // so the stream owns the fd lifecycle and closes it asynchronously on end.
86 // Attempting to also close it synchronously in a finalizer races with the
87 // stream's async close and produces an uncaught EBADF.
88 const fd = NFS.openSync(filePath, "r");
89
90 openSyncInterceptor.failPath = `/proc/self/fd/${fd}`;
91 try {
92 const payload = yield* readBootstrapEnvelope(TestEnvelopeSchema, fd, { timeoutMs: 100 });
93 assertSome(payload, {
94 mode: "desktop",
95 });
96 } finally {
97 openSyncInterceptor.failPath = null;
98 }
99 }),
100 );
101
102 it.effect("returns none when the fd is unavailable", () =>
103 Effect.gen(function* () {
104 const fd = NFS.openSync("/dev/null", "r");
105 NFS.closeSync(fd);
106
107 const payload = yield* readBootstrapEnvelope(TestEnvelopeSchema, fd, { timeoutMs: 100 });
108 assertNone(payload);
109 }),
110 );
111
112 it.effect("returns none when the bootstrap read times out before any value arrives", () =>
113 Effect.gen(function* () {
114 const fs = yield* FileSystem.FileSystem;
115 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-bootstrap-" });
116 const fifoPath = path.join(tempDir, "bootstrap.pipe");
117
118 yield* Effect.sync(() => execFileSync("mkfifo", [fifoPath]));
119
120 const _writer = yield* Effect.acquireRelease(
121 Effect.sync(() =>
122 spawn("sh", ["-c", 'exec 3>"$1"; sleep 60', "sh", fifoPath], {
123 stdio: ["ignore", "ignore", "ignore"],
124 }),
125 ),
126 (writer) =>
127 Effect.sync(() => {
128 writer.kill("SIGKILL");
129 }),
130 );
131
132 const fd = yield* Effect.acquireRelease(
133 Effect.sync(() => NFS.openSync(fifoPath, "r")),
134 (fd) => Effect.sync(() => NFS.closeSync(fd)),
135 );
136
137 const fiber = yield* readBootstrapEnvelope(TestEnvelopeSchema, fd, {
138 timeoutMs: 100,
139 }).pipe(Effect.forkScoped);
140
141 yield* Effect.yieldNow;
142 yield* TestClock.adjust(Duration.millis(100));
143
144 const payload = yield* Fiber.join(fiber);
145 assertNone(payload);
146 }).pipe(Effect.provide(TestClock.layer())),
147 );
148});