Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ServerEnvironment.ts

152 lines4.7 KB

1import { EnvironmentId, type ExecutionEnvironmentDescriptor } from "@v12/contracts";
2import { HostProcessArchitecture, HostProcessPlatform } from "@v12/shared/hostProcess";
3import * as Context from "effect/Context";
4import * as Crypto from "effect/Crypto";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Path from "effect/Path";
9import * as Schema from "effect/Schema";
10​
11import packageJson from "../../package.json" with { type: "json" };
12import * as ServerConfig from "../config.ts";
13import * as ProcessRunner from "../processRunner.ts";
14import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
15​
16export class ServerEnvironmentIdPersistenceError extends Schema.TaggedErrorClass<ServerEnvironmentIdPersistenceError>()(
17 "ServerEnvironmentIdPersistenceError",
18 {
19 operation: Schema.Literals(["check", "read", "write"]),
20 environmentIdPath: Schema.String,
21 cause: Schema.Defect(),
22 },
23) {
24 override get message(): string {
25 return `Server environment ID ${this.operation} failed at '${this.environmentIdPath}'.`;
26 }
28​
29export class ServerEnvironment extends Context.Service<
30 ServerEnvironment,
31 {
32 readonly getEnvironmentId: Effect.Effect<EnvironmentId>;
33 readonly getDescriptor: Effect.Effect<ExecutionEnvironmentDescriptor>;
34 }
35>()("v12/environment/ServerEnvironment") {}
36​
37function platformOs(platform: NodeJS.Platform): ExecutionEnvironmentDescriptor["platform"]["os"] {
38 switch (platform) {
39 case "darwin":
40 return "darwin";
41 case "linux":
42 return "linux";
43 case "win32":
44 return "windows";
45 default:
46 return "unknown";
47 }
49​
50function platformArch(
51 architecture: NodeJS.Architecture,
52): ExecutionEnvironmentDescriptor["platform"]["arch"] {
53 switch (architecture) {
54 case "arm64":
55 return "arm64";
56 case "x64":
57 return "x64";
58 default:
59 return "other";
60 }
62​
63export const make = Effect.gen(function* () {
64 const fileSystem = yield* FileSystem.FileSystem;
65 const path = yield* Path.Path;
66 const serverConfig = yield* ServerConfig.ServerConfig;
67 const crypto = yield* Crypto.Crypto;
68 const hostPlatform = yield* HostProcessPlatform;
69 const hostArchitecture = yield* HostProcessArchitecture;
70​
71 const readPersistedEnvironmentId = Effect.gen(function* () {
72 const exists = yield* fileSystem.exists(serverConfig.environmentIdPath).pipe(
73 Effect.mapError(
74 (cause) =>
75 new ServerEnvironmentIdPersistenceError({
76 operation: "check",
77 environmentIdPath: serverConfig.environmentIdPath,
78 cause,
79 }),
80 ),
81 );
82 if (!exists) {
83 return null;
84 }
85​
86 const raw = yield* fileSystem.readFileString(serverConfig.environmentIdPath).pipe(
87 Effect.map((value) => value.trim()),
88 Effect.mapError(
89 (cause) =>
90 new ServerEnvironmentIdPersistenceError({
91 operation: "read",
92 environmentIdPath: serverConfig.environmentIdPath,
93 cause,
94 }),
95 ),
96 );
97​
98 return raw.length > 0 ? raw : null;
99 });
100​
101 const persistEnvironmentId = (value: string) =>
102 fileSystem.writeFileString(serverConfig.environmentIdPath, `${value}\n`).pipe(
103 Effect.mapError(
104 (cause) =>
105 new ServerEnvironmentIdPersistenceError({
106 operation: "write",
107 environmentIdPath: serverConfig.environmentIdPath,
108 cause,
109 }),
110 ),
111 );
112​
113 const environmentIdRaw = yield* Effect.gen(function* () {
114 const persisted = yield* readPersistedEnvironmentId;
115 if (persisted) {
116 return persisted;
117 }
118​
119 const generated = yield* crypto.randomUUIDv4;
120 yield* persistEnvironmentId(generated);
121 return generated;
122 });
123​
124 const environmentId = EnvironmentId.make(environmentIdRaw);
125 const cwdBaseName = path.basename(serverConfig.cwd).trim();
126 const label = yield* resolveServerEnvironmentLabel({ cwdBaseName });
127​
128 const descriptor: ExecutionEnvironmentDescriptor = {
129 environmentId,
130 label,
131 platform: {
132 os: platformOs(hostPlatform),
133 arch: platformArch(hostArchitecture),
134 },
135 serverVersion: packageJson.version,
136 capabilities: {
137 repositoryIdentity: true,
138 },
139 };
140​
141 return ServerEnvironment.of({
142 getEnvironmentId: Effect.succeed(environmentId),
143 getDescriptor: Effect.succeed(descriptor),
144 });
145});
146​
147/**
148 * ServerEnvironment is acquired from persisted filesystem and host-process
149 * state. It intentionally has no fallback Layer.succeed value: callers must
150 * provide the external platform services and a ServerConfig.
151 */
152export const layer = Layer.effect(ServerEnvironment, make).pipe(Layer.provide(ProcessRunner.layer));