Open History to see the last commit
1import {
2 connectLoopbackRedirectUri,
3 CONNECT_OAUTH_SCOPES,
4 DEFAULT_HOSTED_APP_URL,
5} from "@t3tools/shared/connectAuth";
6import { clerkFrontendApiUrlFromPublishableKey } from "@t3tools/shared/relayAuth";
7import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
8import * as Config from "effect/Config";
9import * as ConfigProvider from "effect/ConfigProvider";
10import * as Effect from "effect/Effect";
11import * as Schema from "effect/Schema";
12import * as SchemaIssue from "effect/SchemaIssue";
13
14declare const __T3CODE_BUILD_RELAY_URL__: string | undefined;
15declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined;
16declare const __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: string | undefined;
17declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: string | undefined;
18declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: string | undefined;
19declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: string | undefined;
20
21const CLOUD_CLI_OAUTH_LOOPBACK_PORT = 34338;
22const CLOUD_CLI_OAUTH_SCOPES = CONNECT_OAUTH_SCOPES;
23
24function validateRelayUrl(value: string) {
25 const relayUrl = normalizeSecureRelayUrl(value);
26 return relayUrl === null
27 ? Effect.fail(
28 new Config.ConfigError(
29 new Schema.SchemaError(
30 new SchemaIssue.InvalidValue({
31 message: "Relay URL must be a secure absolute HTTPS origin.",
32 }),
33 ),
34 ),
35 )
36 : Effect.succeed(relayUrl);
37}
38
39function readBuildTimeValue(value: string | undefined): string {
40 return typeof value === "undefined" ? "" : value.trim();
41}
42
43function normalizeSecureUrl(value: string): string | null {
44 try {
45 const url = new URL(value);
46 return url.protocol === "https:" ? url.toString() : null;
47 } catch {
48 return null;
49 }
50}
51
52const buildTimeRelayUrl =
53 typeof __T3CODE_BUILD_RELAY_URL__ === "undefined"
54 ? ""
55 : (normalizeSecureRelayUrl(__T3CODE_BUILD_RELAY_URL__) ?? "");
56const buildTimeClerkPublishableKey = readBuildTimeValue(
57 typeof __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__ === "undefined"
58 ? undefined
59 : __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__,
60);
61const buildTimeClerkCliOAuthClientId = readBuildTimeValue(
62 typeof __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__ === "undefined"
63 ? undefined
64 : __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__,
65);
66const buildTimeRelayClientTracing = {
67 tracesUrl: readBuildTimeValue(
68 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__ === "undefined"
69 ? undefined
70 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__,
71 ),
72 tracesDataset: readBuildTimeValue(
73 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__ === "undefined"
74 ? undefined
75 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__,
76 ),
77 tracesToken: readBuildTimeValue(
78 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__ === "undefined"
79 ? undefined
80 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__,
81 ),
82} as const;
83
84export function resolveRelayClientTracingConfig(
85 env: Readonly<Record<string, string | undefined>> = process.env,
86 fallback = buildTimeRelayClientTracing,
87) {
88 const tracesUrl = env.T3CODE_RELAY_CLIENT_OTLP_TRACES_URL?.trim() || fallback.tracesUrl;
89 const tracesDataset =
90 env.T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET?.trim() || fallback.tracesDataset;
91 const tracesToken = env.T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN?.trim() || fallback.tracesToken;
92 const normalizedTracesUrl = normalizeSecureUrl(tracesUrl);
93 return normalizedTracesUrl && tracesDataset && tracesToken
94 ? { tracesUrl: normalizedTracesUrl, tracesDataset, tracesToken }
95 : null;
96}
97
98export function makeRelayUrlConfig(fallback = buildTimeRelayUrl) {
99 const runtimeConfig = Config.NonEmptyString("T3CODE_RELAY_URL");
100 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
101 Config.mapEffect(validateRelayUrl),
102 );
103}
104
105export const relayUrlConfig = makeRelayUrlConfig();
106
107export const hostedAppUrlConfig = makePublicValueConfig(
108 "T3CODE_HOSTED_APP_URL",
109 DEFAULT_HOSTED_APP_URL,
110).pipe(Config.mapEffect(validateHostedAppUrl));
111
112function validateHostedAppUrl(value: string) {
113 try {
114 const url = new URL(value);
115 const isLoopbackHttp =
116 url.protocol === "http:" &&
117 (url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]");
118 if (
119 (url.protocol !== "https:" && !isLoopbackHttp) ||
120 url.pathname !== "/" ||
121 url.search !== "" ||
122 url.hash !== ""
123 ) {
124 throw new Error("invalid hosted app origin");
125 }
126 return Effect.succeed(url.origin);
127 } catch {
128 return Effect.fail(
129 new Config.ConfigError(
130 new Schema.SchemaError(
131 new SchemaIssue.InvalidValue({
132 message: "Hosted app URL must be an absolute HTTPS origin (or HTTP loopback origin).",
133 }),
134 ),
135 ),
136 );
137 }
138}
139
140function makePublicValueConfig(name: string, fallback: string) {
141 const runtimeConfig = Config.NonEmptyString(name);
142 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
143 Config.map((value) => value.trim()),
144 );
145}
146
147export interface CloudCliOAuthConfig {
148 readonly tokenEndpoint: string;
149 readonly deviceAuthorizationEndpoint: string;
150 readonly clientId: string;
151 readonly loopbackPort: number;
152 readonly redirectUri: string;
153 readonly scopes: typeof CLOUD_CLI_OAUTH_SCOPES;
154}
155
156export function makeCloudCliOAuthConfig({
157 clerkPublishableKeyFallback = buildTimeClerkPublishableKey,
158 clerkCliOAuthClientIdFallback = buildTimeClerkCliOAuthClientId,
159}: {
160 readonly clerkPublishableKeyFallback?: string;
161 readonly clerkCliOAuthClientIdFallback?: string;
162} = {}) {
163 return Config.all({
164 clerkPublishableKey: makePublicValueConfig(
165 "T3CODE_CLERK_PUBLISHABLE_KEY",
166 clerkPublishableKeyFallback,
167 ),
168 clientId: makePublicValueConfig(
169 "T3CODE_CLERK_CLI_OAUTH_CLIENT_ID",
170 clerkCliOAuthClientIdFallback,
171 ),
172 }).pipe(
173 Config.mapEffect(({ clerkPublishableKey, clientId }) =>
174 Effect.try({
175 try: () => clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey),
176 catch: (cause) =>
177 new Config.ConfigError(
178 new ConfigProvider.SourceError({
179 message: "Failed to derive Clerk Frontend API URL from the publishable key.",
180 cause,
181 }),
182 ),
183 }).pipe(
184 Effect.map(
185 (clerkFrontendApiUrl) =>
186 ({
187 tokenEndpoint: `${clerkFrontendApiUrl}/oauth/token`,
188 deviceAuthorizationEndpoint: `${clerkFrontendApiUrl}/oauth/device_authorization`,
189 clientId,
190 loopbackPort: CLOUD_CLI_OAUTH_LOOPBACK_PORT,
191 redirectUri: connectLoopbackRedirectUri(CLOUD_CLI_OAUTH_LOOPBACK_PORT),
192 scopes: CLOUD_CLI_OAUTH_SCOPES,
193 }) satisfies CloudCliOAuthConfig,
194 ),
195 ),
196 ),
197 );
198}
199
200export const cloudCliOAuthConfig = makeCloudCliOAuthConfig();
201
202export const hasCloudPublicConfig = Boolean(
203 (normalizeSecureRelayUrl(process.env.T3CODE_RELAY_URL ?? "") ?? buildTimeRelayUrl) &&
204 (process.env.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() || buildTimeClerkPublishableKey) &&
205 (process.env.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || buildTimeClerkCliOAuthClientId),
206);