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

shellSnapshotHttp.ts

90 lines3.7 KB

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