Open History to see the last commit
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 for (const entry of current.environments.values()) {
36 const status = Option.getOrNull(entry.status);
37 const descriptor = status?.descriptor;
38 if (status === null || descriptor === undefined) continue;
39 const environmentId = entry.environment.environmentId;
40 const previous = seenChecks.get(environmentId);
41 const fresh =
42 previous?.checkedAt !== status.checkedAt ||
43 (previous.descriptor?.orchestrationProtocolVersion ?? 1) !==
44 (descriptor.orchestrationProtocolVersion ?? 1) ||
45 previous.descriptor?.serverVersion !== descriptor.serverVersion;
46 const error = orchestrationProtocolCompatibilityError(descriptor);
47 // A replayed health result must not clear a newer socket rejection.
48 if (error !== null || fresh) yield* registry.setCompatibility(environmentId, error);
49 seenChecks.set(environmentId, status);
50 }
51 }).pipe(
52 Effect.catch((error) =>
53 Effect.logWarning("Could not apply discovered environment compatibility.", { error }),
54 ),
55 ),
56 ),
57 );
58 },
59);
60
61export function layerWithOptions(options: RpcSession.RpcSessionOptions) {
62 const driverLayer = ConnectionDriver.layer.pipe(
63 Layer.provide(Layer.mergeAll(ConnectionResolver.layer, RpcSession.layerWithOptions(options))),
64 );
65 const registryLayer = EnvironmentRegistry.layer.pipe(Layer.provide(driverLayer));
66 const onboardingLayer = ConnectionOnboarding.layer.pipe(Layer.provide(registryLayer));
67 const connectionServicesLayer = Layer.mergeAll(
68 registryLayer,
69 RelayEnvironmentDiscovery.layer,
70 onboardingLayer,
71 );
72 const connectionStartupLayer = Layer.effectDiscard(
73 Effect.gen(function* () {
74 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
75 const platformSource = yield* PlatformConnectionSource.PlatformConnectionSource;
76 yield* watchDiscoveredCompatibility().pipe(Effect.forkScoped);
77 yield* registry.start;
78 yield* platformSource.registrations.pipe(
79 Stream.runForEach(registry.reconcilePlatform),
80 Effect.forkScoped,
81 );
82 }).pipe(Effect.withSpan("clientRuntime.connection.application.start")),
83 );
84 return connectionStartupLayer.pipe(
85 Layer.provideMerge(connectionServicesLayer),
86 Layer.provideMerge(RemoteEnvironmentAuthorization.layer),
87 );
88}
89
90export const layer = layerWithOptions({});