Open History to see the last commit
1/**
2 * ServerConfig and NetService - Runtime configuration services.
3 *
4 * Defines process-level server configuration and networking helpers used by
5 * startup and runtime layers.
6 *
7 * @module ServerConfig
8 */
9import * as Net from "node:net";
10import { Data, Effect, FileSystem, Layer, Path, ServiceMap } from "effect";
11
12export const DEFAULT_PORT = 3773;
13
14export type RuntimeMode = "web" | "desktop";
15
16/**
17 * ServerConfigShape - Process/runtime configuration required by the server.
18 */
19export interface ServerConfigShape {
20 readonly mode: RuntimeMode;
21 readonly port: number;
22 readonly host: string | undefined;
23 readonly cwd: string;
24 readonly keybindingsConfigPath: string;
25 readonly stateDir: string;
26 readonly staticDir: string | undefined;
27 readonly devUrl: URL | undefined;
28 readonly noBrowser: boolean;
29 readonly authToken: string | undefined;
30 readonly autoBootstrapProjectFromCwd: boolean;
31 readonly logWebSocketEvents: boolean;
32}
33
34/**
35 * ServerConfig - Service tag for server runtime configuration.
36 */
37export class ServerConfig extends ServiceMap.Service<ServerConfig, ServerConfigShape>()(
38 "t3/config/ServerConfig",
39) {
40 static readonly layerTest = (cwd: string, statedir: string) =>
41 Layer.effect(
42 ServerConfig,
43 Effect.gen(function* () {
44 const path = yield* Path.Path;
45 return {
46 cwd,
47 stateDir: statedir,
48 mode: "web",
49 autoBootstrapProjectFromCwd: false,
50 logWebSocketEvents: false,
51 port: 0,
52 host: undefined,
53 authToken: undefined,
54 keybindingsConfigPath: path.join(statedir, "keybindings.json"),
55 staticDir: undefined,
56 devUrl: undefined,
57 noBrowser: false,
58 };
59 }),
60 );
61}
62
63// Helpers
64
65export class NetError extends Data.TaggedError("NetError")<{
66 readonly message: string;
67 readonly cause?: unknown;
68}> {}
69
70/**
71 * NetServiceShape - Networking helper operations used during startup.
72 */
73export interface NetServiceShape {
74 /**
75 * Resolve an available listening port, preferring the provided port first.
76 *
77 * Falls back to an ephemeral OS-assigned port when the preferred port is in use.
78 */
79 readonly findAvailablePort: (preferred: number) => Effect.Effect<number, NetError>;
80}
81
82/**
83 * NetService - Service tag for startup networking helpers.
84 */
85export class NetService extends ServiceMap.Service<NetService, NetServiceShape>()(
86 "t3/config/NetService",
87) {
88 static readonly layer = Layer.succeed(NetService, {
89 findAvailablePort: (preferred) =>
90 Effect.callback<number, NetError>((resume) => {
91 let fallbackServer: Net.Server | null = null;
92 const server = Net.createServer();
93 server.listen(preferred, () => {
94 server.close(() => resume(Effect.succeed(preferred)));
95 });
96 server.on("error", () => {
97 const fallback = Net.createServer();
98 fallback.listen(0, () => {
99 const addr = fallback.address();
100 const port = typeof addr === "object" && addr !== null ? addr.port : 0;
101 fallback.close(() => {
102 resume(
103 port > 0
104 ? Effect.succeed(port)
105 : Effect.fail(new NetError({ message: "Could not find an available port." })),
106 );
107 });
108 });
109 fallback.on("error", (cause) => {
110 resume(
111 Effect.fail(new NetError({ message: "Failed to find an available port", cause })),
112 );
113 });
114 fallbackServer = fallback;
115 });
116
117 return Effect.sync(() => {
118 server.close();
119 fallbackServer?.close();
120 });
121 }),
122 });
123}
124
125export const resolveStaticDir = Effect.fn(function* () {
126 const { join, resolve } = yield* Path.Path;
127 const { exists } = yield* FileSystem.FileSystem;
128 const bundledClient = resolve(join(import.meta.dirname, "client"));
129 const bundledStat = yield* exists(join(bundledClient, "index.html")).pipe(
130 Effect.orElseSucceed(() => false),
131 );
132 if (bundledStat) {
133 return bundledClient;
134 }
135
136 const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
137 const monorepoStat = yield* exists(join(monorepoClient, "index.html")).pipe(
138 Effect.orElseSucceed(() => false),
139 );
140 if (monorepoStat) {
141 return monorepoClient;
142 }
143 return undefined;
144});