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

shellSnapshotHttp.ts

89 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 method: "GET",
39 url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, "/api/orchestration/shell"),
40 timeoutMs: input.timeoutMs ?? DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS,
41 request: ({ client, headers }) =>
42 client.orchestration.shellSnapshot({ headers: withOrchestrationProtocolHeader(headers) }),
43 });
44});
45​
46/**
47 * Loads the environment shell snapshot over HTTP, returning `Option.none()` when
48 * it cannot be loaded (so the caller falls back to the socket-embedded snapshot).
49 * Decouples the shell state machine from the underlying HTTP + DPoP details and
50 * keeps them out of test contexts.
51 */
52export class ShellSnapshotLoader extends Context.Service<
53 ShellSnapshotLoader,
54 {
55 readonly load: (
56 prepared: PreparedConnection,
57 ) => Effect.Effect<Option.Option<OrchestrationV2ShellSnapshot>>;
58 }
59>()("@t3tools/client-runtime/state/shellSnapshotHttp/ShellSnapshotLoader") {}
60​
61export const shellSnapshotLoaderLayer: Layer.Layer<
62 ShellSnapshotLoader,
63 never,
64 HttpClient.HttpClient
65> = Layer.effect(
66 ShellSnapshotLoader,
67 Effect.gen(function* () {
68 const httpClient = yield* HttpClient.HttpClient;
69 // Resolve the DPoP signer optionally: it is only needed for relay/DPoP
70 // connections, so the loader must not hard-require it.
71 const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
72 const remoteAuthorization = yield* Effect.serviceOption(RemoteEnvironmentAuthorization);
73 return ShellSnapshotLoader.of({
74 load: (prepared: PreparedConnection) =>
75 fetchEnvironmentShellSnapshot({ prepared, signer, remoteAuthorization }).pipe(
76 Effect.map(Option.some<OrchestrationV2ShellSnapshot>),
77 Effect.provideService(HttpClient.HttpClient, httpClient),
78 Effect.catchCause((cause) =>
79 Effect.logWarning(
80 "Could not load the environment shell snapshot over HTTP; using the socket snapshot instead.",
81 ).pipe(
82 Effect.annotateLogs({ cause: Cause.pretty(cause) }),
83 Effect.as(Option.none<OrchestrationV2ShellSnapshot>()),
84 ),
85 ),
86 ),
87 });
88 }),
89);