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

context.ts

127 lines4.1 KB

1import {
2 attachEnvironmentDescriptor,
3 createKnownEnvironment,
4 type KnownEnvironment,
5} from "@t3tools/client-runtime";
6import type { EnvironmentId, ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8import { HttpClientError } from "effect/unstable/http";
9import { create } from "zustand";
10​
11import { BootstrapHttpError, retryTransientBootstrap } from "./auth";
12import { PrimaryEnvironmentHttpClient } from "./httpClient";
13​
14import { runPrimaryHttp } from "../../lib/runtime";
15import { readPrimaryEnvironmentTarget } from "./target";
16​
17interface PrimaryEnvironmentBootstrapState {
18 readonly descriptor: ExecutionEnvironmentDescriptor | null;
19 readonly setDescriptor: (descriptor: ExecutionEnvironmentDescriptor | null) => void;
20 readonly reset: () => void;
22​
23const usePrimaryEnvironmentBootstrapStore = create<PrimaryEnvironmentBootstrapState>()((set) => ({
24 descriptor: null,
25 setDescriptor: (descriptor) => set({ descriptor }),
26 reset: () => set({ descriptor: null }),
27}));
28​
29let primaryEnvironmentDescriptorPromise: Promise<ExecutionEnvironmentDescriptor> | null = null;
30​
31function createPrimaryKnownEnvironment(input: {
32 readonly source: KnownEnvironment["source"];
33 readonly target: KnownEnvironment["target"];
34}): KnownEnvironment | null {
35 const descriptor = readPrimaryEnvironmentDescriptor();
36 if (!descriptor) {
37 return null;
38 }
39​
40 return attachEnvironmentDescriptor(
41 createKnownEnvironment({
42 id: descriptor.environmentId,
43 label: descriptor.label,
44 source: input.source,
45 target: input.target,
46 }),
47 descriptor,
48 );
50​
51async function fetchPrimaryEnvironmentDescriptor(): Promise<ExecutionEnvironmentDescriptor> {
52 return retryTransientBootstrap(async () => {
53 let descriptor: ExecutionEnvironmentDescriptor;
54 try {
55 descriptor = await runPrimaryHttp(
56 PrimaryEnvironmentHttpClient.pipe(Effect.flatMap((client) => client.metadata.descriptor())),
57 );
58 } catch (error) {
59 const status =
60 HttpClientError.isHttpClientError(error) && error.response !== undefined
61 ? error.response.status
62 : 500;
63 throw new BootstrapHttpError({
64 message: `Failed to load server environment descriptor (${status}).`,
65 status,
66 });
67 }
68​
69 writePrimaryEnvironmentDescriptor(descriptor);
70 return descriptor;
71 });
73​
74export function readPrimaryEnvironmentDescriptor(): ExecutionEnvironmentDescriptor | null {
75 return usePrimaryEnvironmentBootstrapStore.getState().descriptor;
77​
78export function usePrimaryEnvironmentId(): EnvironmentId | null {
79 return usePrimaryEnvironmentBootstrapStore((state) => state.descriptor?.environmentId ?? null);
81​
82export function writePrimaryEnvironmentDescriptor(
83 descriptor: ExecutionEnvironmentDescriptor | null,
84): void {
85 usePrimaryEnvironmentBootstrapStore.getState().setDescriptor(descriptor);
87​
88export function getPrimaryKnownEnvironment(): KnownEnvironment | null {
89 const primaryTarget = readPrimaryEnvironmentTarget();
90 if (!primaryTarget) {
91 return null;
92 }
93​
94 return createPrimaryKnownEnvironment({
95 source: primaryTarget.source,
96 target: primaryTarget.target,
97 });
99​
100export function resolveInitialPrimaryEnvironmentDescriptor(): Promise<ExecutionEnvironmentDescriptor> {
101 const descriptor = readPrimaryEnvironmentDescriptor();
102 if (descriptor) {
103 return Promise.resolve(descriptor);
104 }
105​
106 if (primaryEnvironmentDescriptorPromise) {
107 return primaryEnvironmentDescriptorPromise;
108 }
109​
110 const nextPromise = fetchPrimaryEnvironmentDescriptor();
111 primaryEnvironmentDescriptorPromise = nextPromise;
112 return nextPromise.finally(() => {
113 if (primaryEnvironmentDescriptorPromise === nextPromise) {
114 primaryEnvironmentDescriptorPromise = null;
115 }
116 });
118​
119export function __resetPrimaryEnvironmentBootstrapForTests(): void {
120 primaryEnvironmentDescriptorPromise = null;
121 usePrimaryEnvironmentBootstrapStore.getState().reset();
123​
124export const resetPrimaryEnvironmentDescriptorForTests = __resetPrimaryEnvironmentBootstrapForTests;
125​
126export const __resetPrimaryEnvironmentDescriptorBootstrapForTests =
127 __resetPrimaryEnvironmentBootstrapForTests;