Open History to see the last commit
1/**
2 * ServerConfig - 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 { Effect, FileSystem, Layer, Path, ServiceMap } from "effect";
10
11export const DEFAULT_PORT = 3773;
12
13export type RuntimeMode = "web" | "desktop";
14
15const LOOPBACK_HOSTS = new Set(["127.0.0.1", "::1", "[::1]", "localhost"]);
16
17/**
18 * ServerConfigShape - Process/runtime configuration required by the server.
19 */
20export interface ServerConfigShape {
21 readonly mode: RuntimeMode;
22 readonly port: number;
23 readonly host: string | undefined;
24 readonly cwd: string;
25 readonly keybindingsConfigPath: string;
26 readonly stateDir: string;
27 readonly staticDir: string | undefined;
28 readonly devUrl: URL | undefined;
29 readonly noBrowser: boolean;
30 readonly authToken: string | undefined;
31 readonly autoBootstrapProjectFromCwd: boolean;
32 readonly logWebSocketEvents: boolean;
33}
34
35/**
36 * ServerConfig - Service tag for server runtime configuration.
37 */
38export class ServerConfig extends ServiceMap.Service<ServerConfig, ServerConfigShape>()(
39 "t3/config/ServerConfig",
40) {
41 static readonly layerTest = (cwd: string, statedir: string) =>
42 Layer.effect(
43 ServerConfig,
44 Effect.gen(function* () {
45 const path = yield* Path.Path;
46 return {
47 cwd,
48 stateDir: statedir,
49 mode: "web",
50 autoBootstrapProjectFromCwd: false,
51 logWebSocketEvents: false,
52 port: 0,
53 host: undefined,
54 authToken: undefined,
55 keybindingsConfigPath: path.join(statedir, "keybindings.json"),
56 staticDir: undefined,
57 devUrl: undefined,
58 noBrowser: false,
59 };
60 }),
61 );
62}
63
64export const isWildcardHost = (host: string | undefined): boolean =>
65 host === "0.0.0.0" || host === "::" || host === "[::]";
66
67export const isLoopbackHost = (host: string | undefined): boolean =>
68 host !== undefined && LOOPBACK_HOSTS.has(host.trim().toLowerCase());
69
70export const formatHostForUrl = (host: string): string =>
71 host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
72
73export const resolveStaticDir = Effect.fn(function* () {
74 const { join, resolve } = yield* Path.Path;
75 const { exists } = yield* FileSystem.FileSystem;
76 const bundledClient = resolve(join(import.meta.dirname, "client"));
77 const bundledStat = yield* exists(join(bundledClient, "index.html")).pipe(
78 Effect.orElseSucceed(() => false),
79 );
80 if (bundledStat) {
81 return bundledClient;
82 }
83
84 const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
85 const monorepoStat = yield* exists(join(monorepoClient, "index.html")).pipe(
86 Effect.orElseSucceed(() => false),
87 );
88 if (monorepoStat) {
89 return monorepoClient;
90 }
91 return undefined;
92});