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

shellSnapshotHttp.ts

86 lines3.6 KB

1import type { OrchestrationShellSnapshot } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import * as Context from "effect/Context";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import { HttpClient } from "effect/unstable/http";
8​
9import { RemoteEnvironmentAuthorization } from "../authorization/service.ts";
10import type { PreparedConnection } from "../connection/model.ts";
11import { environmentEndpointUrl } from "../environment/endpoint.ts";
12import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
13import { executeAuthenticatedEnvironmentHttpRequest } from "./environmentHttpAuth.ts";
14​
15// Bounded so a pathologically slow endpoint cannot block the (cheaper) socket
16// fallback for long. The cached shell renders while this runs.
17const DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS = 6_000;
18​
19/**
20 * Load the environment shell snapshot (projects + thread shells) over HTTP
21 * instead of as the WebSocket subscription's first frame. The response is
22 * gzip-compressible by the transport and keeps the (potentially large) list off
23 * the socket.
24 */
25export const fetchEnvironmentShellSnapshot = Effect.fn(
26 "clientRuntime.state.fetchEnvironmentShellSnapshot",
27)(function* (input: {
28 readonly prepared: PreparedConnection;
29 readonly signer: Option.Option<ManagedRelayDpopSigner["Service"]>;
30 readonly remoteAuthorization?: Option.Option<RemoteEnvironmentAuthorization["Service"]>;
31 readonly timeoutMs?: number;
32}) {
33 return yield* executeAuthenticatedEnvironmentHttpRequest({
34 ...input,
35 group: "orchestration",
36 method: "GET",
37 url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, "/api/orchestration/shell"),
38 timeoutMs: input.timeoutMs ?? DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS,
39 request: ({ client, headers }) => client.shellSnapshot({ headers }),
40 });
41});
42​
43/**
44 * Loads the environment shell snapshot over HTTP, returning `Option.none()` when
45 * it cannot be loaded (so the caller falls back to the socket-embedded snapshot).
46 * Decouples the shell state machine from the underlying HTTP + DPoP details and
47 * keeps them out of test contexts.
48 */
49export class ShellSnapshotLoader extends Context.Service<
50 ShellSnapshotLoader,
51 {
52 readonly load: (
53 prepared: PreparedConnection,
54 ) => Effect.Effect<Option.Option<OrchestrationShellSnapshot>>;
55 }
56>()("@t3tools/client-runtime/state/shellSnapshotHttp/ShellSnapshotLoader") {}
57​
58export const shellSnapshotLoaderLayer: Layer.Layer<
59 ShellSnapshotLoader,
60 never,
61 HttpClient.HttpClient
62> = Layer.effect(
63 ShellSnapshotLoader,
64 Effect.gen(function* () {
65 const httpClient = yield* HttpClient.HttpClient;
66 // Resolve the DPoP signer optionally: it is only needed for relay/DPoP
67 // connections, so the loader must not hard-require it.
68 const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
69 const remoteAuthorization = yield* Effect.serviceOption(RemoteEnvironmentAuthorization);
70 return ShellSnapshotLoader.of({
71 load: (prepared: PreparedConnection) =>
72 fetchEnvironmentShellSnapshot({ prepared, signer, remoteAuthorization }).pipe(
73 Effect.map(Option.some<OrchestrationShellSnapshot>),
74 Effect.provideService(HttpClient.HttpClient, httpClient),
75 Effect.catchCause((cause) =>
76 Effect.logWarning(
77 "Could not load the environment shell snapshot over HTTP; using the socket snapshot instead.",
78 ).pipe(
79 Effect.annotateLogs({ cause: Cause.pretty(cause) }),
80 Effect.as(Option.none<OrchestrationShellSnapshot>()),
81 ),
82 ),
83 ),
84 });
85 }),
86);