Open History to see the last commit
1import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Random from "effect/Random";
8import * as Ref from "effect/Ref";
9
10import * as DesktopBackendManager from "./DesktopBackendManager.ts";
11import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
12import * as DesktopObservability from "../app/DesktopObservability.ts";
13import * as DesktopServerExposure from "./DesktopServerExposure.ts";
14
15export interface DesktopBackendConfigurationShape {
16 readonly resolve: Effect.Effect<DesktopBackendManager.DesktopBackendStartConfig>;
17}
18
19export class DesktopBackendConfiguration extends Context.Service<
20 DesktopBackendConfiguration,
21 DesktopBackendConfigurationShape
22>()("t3/desktop/BackendConfiguration") {}
23
24interface BackendObservabilitySettings {
25 readonly otlpTracesUrl: Option.Option<string>;
26 readonly otlpMetricsUrl: Option.Option<string>;
27}
28
29const emptyBackendObservabilitySettings: BackendObservabilitySettings = {
30 otlpTracesUrl: Option.none(),
31 otlpMetricsUrl: Option.none(),
32};
33
34const DESKTOP_BACKEND_ENV_NAMES = [
35 "T3CODE_PORT",
36 "T3CODE_MODE",
37 "T3CODE_NO_BROWSER",
38 "T3CODE_HOST",
39 "T3CODE_DESKTOP_WS_URL",
40 "T3CODE_DESKTOP_LAN_ACCESS",
41 "T3CODE_DESKTOP_LAN_HOST",
42 "T3CODE_DESKTOP_HTTPS_ENDPOINTS",
43 "T3CODE_TAILSCALE_SERVE",
44 "T3CODE_TAILSCALE_SERVE_PORT",
45] as const;
46
47const backendChildEnvPatch = (): Record<string, string | undefined> =>
48 Object.fromEntries(DESKTOP_BACKEND_ENV_NAMES.map((name) => [name, undefined]));
49
50const { logWarning: logBackendConfigurationWarning } = DesktopObservability.makeComponentLogger(
51 "desktop-backend-configuration",
52);
53
54const readPersistedBackendObservabilitySettings: Effect.Effect<
55 BackendObservabilitySettings,
56 never,
57 FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment
58> = Effect.gen(function* () {
59 const fileSystem = yield* FileSystem.FileSystem;
60 const environment = yield* DesktopEnvironment.DesktopEnvironment;
61 const exists = yield* fileSystem
62 .exists(environment.serverSettingsPath)
63 .pipe(Effect.orElseSucceed(() => false));
64 if (!exists) {
65 return emptyBackendObservabilitySettings;
66 }
67
68 const raw = yield* fileSystem.readFileString(environment.serverSettingsPath).pipe(Effect.option);
69 if (Option.isNone(raw)) {
70 yield* logBackendConfigurationWarning(
71 "failed to read persisted backend observability settings",
72 );
73 return emptyBackendObservabilitySettings;
74 }
75
76 const parsed = parsePersistedServerObservabilitySettings(raw.value);
77 return {
78 otlpTracesUrl: Option.fromNullishOr(parsed.otlpTracesUrl),
79 otlpMetricsUrl: Option.fromNullishOr(parsed.otlpMetricsUrl),
80 };
81});
82
83const getOrCreateBootstrapToken = Effect.fn("desktop.backendConfiguration.bootstrapToken")(
84 function* (tokenRef: Ref.Ref<Option.Option<string>>) {
85 const existing = yield* Ref.get(tokenRef);
86 if (Option.isSome(existing)) {
87 return existing.value;
88 }
89
90 let token = "";
91 while (token.length < 48) {
92 token += (yield* Random.nextUUIDv4).replace(/-/g, "");
93 }
94 token = token.slice(0, 48);
95 yield* Ref.set(tokenRef, Option.some(token));
96 return token;
97 },
98);
99
100const resolveBackendStartConfig = Effect.fn("desktop.backendConfiguration.resolveStartConfig")(
101 function* (input: {
102 readonly bootstrapToken: string;
103 readonly observabilitySettings: BackendObservabilitySettings;
104 }): Effect.fn.Return<
105 DesktopBackendManager.DesktopBackendStartConfig,
106 never,
107 DesktopEnvironment.DesktopEnvironment | DesktopServerExposure.DesktopServerExposure
108 > {
109 const environment = yield* DesktopEnvironment.DesktopEnvironment;
110 const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
111 const backendExposure = yield* serverExposure.backendConfig;
112
113 return {
114 executablePath: process.execPath,
115 entryPath: environment.backendEntryPath,
116 cwd: environment.backendCwd,
117 env: {
118 ...backendChildEnvPatch(),
119 ELECTRON_RUN_AS_NODE: "1",
120 },
121 bootstrap: {
122 mode: "desktop",
123 noBrowser: true,
124 port: backendExposure.port,
125 t3Home: environment.baseDir,
126 host: backendExposure.bindHost,
127 desktopBootstrapToken: input.bootstrapToken,
128 tailscaleServeEnabled: backendExposure.tailscaleServeEnabled,
129 tailscaleServePort: backendExposure.tailscaleServePort,
130 ...Option.match(input.observabilitySettings.otlpTracesUrl, {
131 onNone: () => ({}),
132 onSome: (otlpTracesUrl) => ({ otlpTracesUrl }),
133 }),
134 ...Option.match(input.observabilitySettings.otlpMetricsUrl, {
135 onNone: () => ({}),
136 onSome: (otlpMetricsUrl) => ({ otlpMetricsUrl }),
137 }),
138 },
139 httpBaseUrl: backendExposure.httpBaseUrl,
140 captureOutput: true,
141 };
142 },
143);
144
145export const layer = Layer.effect(
146 DesktopBackendConfiguration,
147 Effect.gen(function* () {
148 const environment = yield* DesktopEnvironment.DesktopEnvironment;
149 const fileSystem = yield* FileSystem.FileSystem;
150 const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
151 const tokenRef = yield* Ref.make(Option.none<string>());
152
153 return DesktopBackendConfiguration.of({
154 resolve: Effect.gen(function* () {
155 const bootstrapToken = yield* getOrCreateBootstrapToken(tokenRef);
156 const observabilitySettings = yield* readPersistedBackendObservabilitySettings.pipe(
157 Effect.provideService(FileSystem.FileSystem, fileSystem),
158 Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment),
159 );
160 return yield* resolveBackendStartConfig({
161 bootstrapToken,
162 observabilitySettings,
163 }).pipe(
164 Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment),
165 Effect.provideService(DesktopServerExposure.DesktopServerExposure, serverExposure),
166 );
167 }).pipe(Effect.withSpan("desktop.backendConfiguration.resolve")),
168 });
169 }),
170);