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

layer.ts

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