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

context.ts

120 lines3.9 KB

1import {
2 attachEnvironmentDescriptor,
3 createKnownEnvironment,
4 type KnownEnvironment,
5} from "@t3tools/client-runtime";
6import type { EnvironmentId, ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
7import { create } from "zustand";
8​
9import { BootstrapHttpError, retryTransientBootstrap } from "./auth";
10​
11import { readPrimaryEnvironmentTarget, resolvePrimaryEnvironmentHttpUrl } from "./target";
12​
13const SERVER_ENVIRONMENT_DESCRIPTOR_PATH = "/.well-known/t3/environment";
14​
15interface PrimaryEnvironmentBootstrapState {
16 readonly descriptor: ExecutionEnvironmentDescriptor | null;
17 readonly setDescriptor: (descriptor: ExecutionEnvironmentDescriptor | null) => void;
18 readonly reset: () => void;
20​
21const usePrimaryEnvironmentBootstrapStore = create<PrimaryEnvironmentBootstrapState>()((set) => ({
22 descriptor: null,
23 setDescriptor: (descriptor) => set({ descriptor }),
24 reset: () => set({ descriptor: null }),
25}));
26​
27let primaryEnvironmentDescriptorPromise: Promise<ExecutionEnvironmentDescriptor> | null = null;
28​
29function createPrimaryKnownEnvironment(input: {
30 readonly source: KnownEnvironment["source"];
31 readonly target: KnownEnvironment["target"];
32}): KnownEnvironment | null {
33 const descriptor = readPrimaryEnvironmentDescriptor();
34 if (!descriptor) {
35 return null;
36 }
37​
38 return attachEnvironmentDescriptor(
39 createKnownEnvironment({
40 id: descriptor.environmentId,
41 label: descriptor.label,
42 source: input.source,
43 target: input.target,
44 }),
45 descriptor,
46 );
48​
49async function fetchPrimaryEnvironmentDescriptor(): Promise<ExecutionEnvironmentDescriptor> {
50 return retryTransientBootstrap(async () => {
51 const response = await fetch(
52 resolvePrimaryEnvironmentHttpUrl(SERVER_ENVIRONMENT_DESCRIPTOR_PATH),
53 );
54 if (!response.ok) {
55 throw new BootstrapHttpError({
56 message: `Failed to load server environment descriptor (${response.status}).`,
57 status: response.status,
58 });
59 }
60​
61 const descriptor = (await response.json()) as ExecutionEnvironmentDescriptor;
62 writePrimaryEnvironmentDescriptor(descriptor);
63 return descriptor;
64 });
66​
67export function readPrimaryEnvironmentDescriptor(): ExecutionEnvironmentDescriptor | null {
68 return usePrimaryEnvironmentBootstrapStore.getState().descriptor;
70​
71export function usePrimaryEnvironmentId(): EnvironmentId | null {
72 return usePrimaryEnvironmentBootstrapStore((state) => state.descriptor?.environmentId ?? null);
74​
75export function writePrimaryEnvironmentDescriptor(
76 descriptor: ExecutionEnvironmentDescriptor | null,
77): void {
78 usePrimaryEnvironmentBootstrapStore.getState().setDescriptor(descriptor);
80​
81export function getPrimaryKnownEnvironment(): KnownEnvironment | null {
82 const primaryTarget = readPrimaryEnvironmentTarget();
83 if (!primaryTarget) {
84 return null;
85 }
86​
87 return createPrimaryKnownEnvironment({
88 source: primaryTarget.source,
89 target: primaryTarget.target,
90 });
92​
93export function resolveInitialPrimaryEnvironmentDescriptor(): Promise<ExecutionEnvironmentDescriptor> {
94 const descriptor = readPrimaryEnvironmentDescriptor();
95 if (descriptor) {
96 return Promise.resolve(descriptor);
97 }
98​
99 if (primaryEnvironmentDescriptorPromise) {
100 return primaryEnvironmentDescriptorPromise;
101 }
102​
103 const nextPromise = fetchPrimaryEnvironmentDescriptor();
104 primaryEnvironmentDescriptorPromise = nextPromise;
105 return nextPromise.finally(() => {
106 if (primaryEnvironmentDescriptorPromise === nextPromise) {
107 primaryEnvironmentDescriptorPromise = null;
108 }
109 });
111​
112export function __resetPrimaryEnvironmentBootstrapForTests(): void {
113 primaryEnvironmentDescriptorPromise = null;
114 usePrimaryEnvironmentBootstrapStore.getState().reset();
116​
117export const resetPrimaryEnvironmentDescriptorForTests = __resetPrimaryEnvironmentBootstrapForTests;
118​
119export const __resetPrimaryEnvironmentDescriptorBootstrapForTests =
120 __resetPrimaryEnvironmentBootstrapForTests;