Open History to see the last commit
1import { EnvironmentId, type ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
2import * as Crypto from "effect/Crypto";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7
8import { ServerConfig } from "../../config.ts";
9import { layer as ProcessRunnerLive } from "../../processRunner.ts";
10import { ServerEnvironment, type ServerEnvironmentShape } from "../Services/ServerEnvironment.ts";
11import packageJson from "../../../package.json" with { type: "json" };
12import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
13
14function platformOs(): ExecutionEnvironmentDescriptor["platform"]["os"] {
15 switch (process.platform) {
16 case "darwin":
17 return "darwin";
18 case "linux":
19 return "linux";
20 case "win32":
21 return "windows";
22 default:
23 return "unknown";
24 }
25}
26
27function platformArch(): ExecutionEnvironmentDescriptor["platform"]["arch"] {
28 switch (process.arch) {
29 case "arm64":
30 return "arm64";
31 case "x64":
32 return "x64";
33 default:
34 return "other";
35 }
36}
37
38export const makeServerEnvironment = Effect.fn("makeServerEnvironment")(function* () {
39 const fileSystem = yield* FileSystem.FileSystem;
40 const path = yield* Path.Path;
41 const serverConfig = yield* ServerConfig;
42 const crypto = yield* Crypto.Crypto;
43
44 const readPersistedEnvironmentId = Effect.gen(function* () {
45 const exists = yield* fileSystem
46 .exists(serverConfig.environmentIdPath)
47 .pipe(Effect.orElseSucceed(() => false));
48 if (!exists) {
49 return null;
50 }
51
52 const raw = yield* fileSystem
53 .readFileString(serverConfig.environmentIdPath)
54 .pipe(Effect.map((value) => value.trim()));
55
56 return raw.length > 0 ? raw : null;
57 });
58
59 const persistEnvironmentId = (value: string) =>
60 fileSystem.writeFileString(serverConfig.environmentIdPath, `${value}\n`);
61
62 const environmentIdRaw = yield* Effect.gen(function* () {
63 const persisted = yield* readPersistedEnvironmentId;
64 if (persisted) {
65 return persisted;
66 }
67
68 const generated = yield* crypto.randomUUIDv4;
69 yield* persistEnvironmentId(generated);
70 return generated;
71 });
72
73 const environmentId = EnvironmentId.make(environmentIdRaw);
74 const cwdBaseName = path.basename(serverConfig.cwd).trim();
75 const label = yield* resolveServerEnvironmentLabel({
76 cwdBaseName,
77 });
78
79 const descriptor: ExecutionEnvironmentDescriptor = {
80 environmentId,
81 label,
82 platform: {
83 os: platformOs(),
84 arch: platformArch(),
85 },
86 serverVersion: packageJson.version,
87 capabilities: {
88 repositoryIdentity: true,
89 },
90 };
91
92 return {
93 getEnvironmentId: Effect.succeed(environmentId),
94 getDescriptor: Effect.succeed(descriptor),
95 } satisfies ServerEnvironmentShape;
96});
97
98export const ServerEnvironmentLive = Layer.effect(ServerEnvironment, makeServerEnvironment()).pipe(
99 Layer.provide(ProcessRunnerLive),
100);