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

layer.ts

106 lines4.9 KB

1import type { RelayEnvironmentStatusResponse } from "@t3tools/contracts/relay";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Stream from "effect/Stream";
5import * as Option from "effect/Option";
6import * as SubscriptionRef from "effect/SubscriptionRef";
7import { orchestrationProtocolCompatibilityError } from "./compatibility.ts";
8​
9import * as ConnectionResolver from "./resolver.ts";
10import * as ConnectionDriver from "./driver.ts";
11import * as EnvironmentRegistry from "./registry.ts";
12import * as ConnectionOnboarding from "./onboarding.ts";
13import { connectionRoutes, hasRelayRoute } from "./routes.ts";
14import * as PlatformConnectionSource from "../platform/source.ts";
15import * as RelayEnvironmentDiscovery from "../relay/discovery.ts";
16import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
17import * as RpcSession from "../rpc/session.ts";
18​
19export const watchDiscoveredCompatibility = Effect.fn("connection.watchDiscoveredCompatibility")(
20 function* () {
21 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
22 const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
23 const seenChecks = new Map<string, RelayEnvironmentStatusResponse>();
24 yield* Stream.merge(
25 SubscriptionRef.changes(discovery.state),
26 SubscriptionRef.changes(registry.entries),
27 ).pipe(
28 Stream.runForEach(() =>
29 Effect.gen(function* () {
30 const current = yield* SubscriptionRef.get(discovery.state);
31 if (!current.refreshing) {
32 for (const environmentId of seenChecks.keys()) {
33 if (!current.environments.has(environmentId)) seenChecks.delete(environmentId);
34 }
35 }
36 const registered = yield* SubscriptionRef.get(registry.entries);
37 for (const entry of current.environments.values()) {
38 const status = Option.getOrNull(entry.status);
39 const descriptor = status?.descriptor;
40 if (status === null || descriptor === undefined) continue;
41 const environmentId = entry.environment.environmentId;
42 // Discovery describes the server behind the relay route. A direct
43 // connection (the desktop's own server, a saved URL, SSH) can reach
44 // a different server with the same environment id, such as a
45 // preview app that shares the home directory. Its socket handshake
46 // already checks the protocol.
47 const saved = registered.get(environmentId);
48 if (
49 saved === undefined ||
50 connectionRoutes(saved).length !== 1 ||
51 !hasRelayRoute(saved)
52 )
53 continue;
54 const previous = seenChecks.get(environmentId);
55 const fresh =
56 previous?.checkedAt !== status.checkedAt ||
57 (previous.descriptor?.orchestrationProtocolVersion ?? 1) !==
58 (descriptor.orchestrationProtocolVersion ?? 1) ||
59 previous.descriptor?.serverVersion !== descriptor.serverVersion;
60 const error = orchestrationProtocolCompatibilityError(descriptor);
61 // A replayed health result must not clear a newer socket rejection.
62 if (error !== null || fresh) yield* registry.setCompatibility(environmentId, error);
63 seenChecks.set(environmentId, status);
64 }
65 }).pipe(
66 Effect.catch((error) =>
67 Effect.logWarning("Could not apply discovered environment compatibility.", { error }),
68 ),
69 ),
70 ),
71 );
72 },
73);
74​
75export function layerWithOptions(options: RpcSession.RpcSessionOptions) {
76 const layerDriver = ConnectionDriver.layer.pipe(
77 Layer.provide(Layer.mergeAll(ConnectionResolver.layer, RpcSession.layer(options))),
78 );
79 const layerRegistry = EnvironmentRegistry.layer.pipe(Layer.provide(layerDriver));
80 const layerOnboarding = ConnectionOnboarding.layer.pipe(Layer.provide(layerRegistry));
81 const layerConnectionServices = Layer.mergeAll(
82 layerRegistry,
83 RelayEnvironmentDiscovery.layer,
84 layerOnboarding,
85 // Exposed for updating hosts too old to connect through the driver.
86 ConnectionResolver.layer,
87 );
88 const layerConnectionStartup = Layer.effectDiscard(
89 Effect.gen(function* () {
90 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
91 const platformSource = yield* PlatformConnectionSource.PlatformConnectionSource;
92 yield* watchDiscoveredCompatibility().pipe(Effect.forkScoped);
93 yield* registry.start;
94 yield* platformSource.registrations.pipe(
95 Stream.runForEach(registry.reconcilePlatform),
96 Effect.forkScoped,
97 );
98 }).pipe(Effect.withSpan("clientRuntime.connection.application.start")),
99 );
100 return layerConnectionStartup.pipe(
101 Layer.provideMerge(layerConnectionServices),
102 Layer.provideMerge(RemoteEnvironmentAuthorization.layer),
103 );
105​
106export const layer = layerWithOptions({});