Open History to see the last commit
1import { clerkFrontendApiUrlFromPublishableKey } from "@v12/shared/relayAuth";
2import { normalizeSecureRelayUrl } from "@v12/shared/relayUrl";
3import * as Config from "effect/Config";
4import * as ConfigProvider from "effect/ConfigProvider";
5import * as Effect from "effect/Effect";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8import * as SchemaIssue from "effect/SchemaIssue";
9
10declare const __V12_BUILD_RELAY_URL__: string | undefined;
11declare const __V12_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined;
12declare const __V12_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: string | undefined;
13declare const __V12_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: string | undefined;
14declare const __V12_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: string | undefined;
15declare const __V12_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: string | undefined;
16
17const CLOUD_CLI_OAUTH_REDIRECT_URI = "http://127.0.0.1:34338/callback";
18const CLOUD_CLI_OAUTH_SCOPES = ["openid", "profile", "email"] as const;
19
20function validateRelayUrl(value: string) {
21 const relayUrl = normalizeSecureRelayUrl(value);
22 return relayUrl === null
23 ? Effect.fail(
24 new Config.ConfigError(
25 new Schema.SchemaError(
26 new SchemaIssue.InvalidValue(Option.some(value), {
27 message: "Relay URL must be a secure absolute HTTPS origin.",
28 }),
29 ),
30 ),
31 )
32 : Effect.succeed(relayUrl);
33}
34
35function readBuildTimeValue(value: string | undefined): string {
36 return typeof value === "undefined" ? "" : value.trim();
37}
38
39function normalizeSecureUrl(value: string): string | null {
40 try {
41 const url = new URL(value);
42 return url.protocol === "https:" ? url.toString() : null;
43 } catch {
44 return null;
45 }
46}
47
48export const buildTimeRelayUrl =
49 typeof __V12_BUILD_RELAY_URL__ === "undefined"
50 ? ""
51 : (normalizeSecureRelayUrl(__V12_BUILD_RELAY_URL__) ?? "");
52export const buildTimeClerkPublishableKey = readBuildTimeValue(
53 typeof __V12_BUILD_CLERK_PUBLISHABLE_KEY__ === "undefined"
54 ? undefined
55 : __V12_BUILD_CLERK_PUBLISHABLE_KEY__,
56);
57export const buildTimeClerkCliOAuthClientId = readBuildTimeValue(
58 typeof __V12_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__ === "undefined"
59 ? undefined
60 : __V12_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__,
61);
62export const buildTimeRelayClientTracing = {
63 tracesUrl: readBuildTimeValue(
64 typeof __V12_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__ === "undefined"
65 ? undefined
66 : __V12_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__,
67 ),
68 tracesDataset: readBuildTimeValue(
69 typeof __V12_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__ === "undefined"
70 ? undefined
71 : __V12_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__,
72 ),
73 tracesToken: readBuildTimeValue(
74 typeof __V12_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__ === "undefined"
75 ? undefined
76 : __V12_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__,
77 ),
78} as const;
79
80export function resolveRelayClientTracingConfig(
81 env: Readonly<Record<string, string | undefined>> = process.env,
82 fallback = buildTimeRelayClientTracing,
83) {
84 const tracesUrl = env.V12_RELAY_CLIENT_OTLP_TRACES_URL?.trim() || fallback.tracesUrl;
85 const tracesDataset = env.V12_RELAY_CLIENT_OTLP_TRACES_DATASET?.trim() || fallback.tracesDataset;
86 const tracesToken = env.V12_RELAY_CLIENT_OTLP_TRACES_TOKEN?.trim() || fallback.tracesToken;
87 const normalizedTracesUrl = normalizeSecureUrl(tracesUrl);
88 return normalizedTracesUrl && tracesDataset && tracesToken
89 ? { tracesUrl: normalizedTracesUrl, tracesDataset, tracesToken }
90 : null;
91}
92
93export function makeRelayUrlConfig(fallback = buildTimeRelayUrl) {
94 const runtimeConfig = Config.nonEmptyString("V12_RELAY_URL");
95 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
96 Config.mapOrFail(validateRelayUrl),
97 );
98}
99
100export const relayUrlConfig = makeRelayUrlConfig();
101
102function makePublicValueConfig(name: string, fallback: string) {
103 const runtimeConfig = Config.nonEmptyString(name);
104 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
105 Config.map((value) => value.trim()),
106 );
107}
108
109export interface CloudCliOAuthConfig {
110 readonly authorizationEndpoint: string;
111 readonly tokenEndpoint: string;
112 readonly clientId: string;
113 readonly redirectUri: string;
114 readonly scopes: typeof CLOUD_CLI_OAUTH_SCOPES;
115}
116
117export function makeCloudCliOAuthConfig({
118 clerkPublishableKeyFallback = buildTimeClerkPublishableKey,
119 clerkCliOAuthClientIdFallback = buildTimeClerkCliOAuthClientId,
120}: {
121 readonly clerkPublishableKeyFallback?: string;
122 readonly clerkCliOAuthClientIdFallback?: string;
123} = {}) {
124 return Config.all({
125 clerkPublishableKey: makePublicValueConfig(
126 "V12_CLERK_PUBLISHABLE_KEY",
127 clerkPublishableKeyFallback,
128 ),
129 clientId: makePublicValueConfig("V12_CLERK_CLI_OAUTH_CLIENT_ID", clerkCliOAuthClientIdFallback),
130 }).pipe(
131 Config.mapOrFail(({ clerkPublishableKey, clientId }) =>
132 Effect.try({
133 try: () => clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey),
134 catch: (cause) =>
135 new Config.ConfigError(
136 new ConfigProvider.SourceError({
137 message: "Failed to derive Clerk Frontend API URL from the publishable key.",
138 cause,
139 }),
140 ),
141 }).pipe(
142 Effect.map(
143 (clerkFrontendApiUrl) =>
144 ({
145 authorizationEndpoint: `${clerkFrontendApiUrl}/oauth/authorize`,
146 tokenEndpoint: `${clerkFrontendApiUrl}/oauth/token`,
147 clientId,
148 redirectUri: CLOUD_CLI_OAUTH_REDIRECT_URI,
149 scopes: CLOUD_CLI_OAUTH_SCOPES,
150 }) satisfies CloudCliOAuthConfig,
151 ),
152 ),
153 ),
154 );
155}
156
157export const cloudCliOAuthConfig = makeCloudCliOAuthConfig();
158
159export const hasCloudPublicConfig = Boolean(
160 (normalizeSecureRelayUrl(process.env.V12_RELAY_URL ?? "") ?? buildTimeRelayUrl) &&
161 (process.env.V12_CLERK_PUBLISHABLE_KEY?.trim() || buildTimeClerkPublishableKey) &&
162 (process.env.V12_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || buildTimeClerkCliOAuthClientId),
163);