Open History to see the last commit
1import {
2 clerkFrontendApiHostnameFromPublishableKey,
3 relayClerkTokenOptions,
4} from "@t3tools/shared/relayAuth";
5import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
6import * as Schema from "effect/Schema";
7
8export class CloudPublicConfigMissingError extends Schema.TaggedError<CloudPublicConfigMissingError>()(
9 "CloudPublicConfigMissingError",
10 {
11 key: Schema.Literal("T3CODE_CLERK_JWT_TEMPLATE"),
12 },
13) {
14 override get message(): string {
15 return `${this.key} is not configured.`;
16 }
17}
18
19export interface CloudPublicConfig {
20 readonly clerkPublishableKey: string | null;
21 readonly clerkJwtTemplate: string | null;
22 readonly relayUrl: string | null;
23 readonly relayTracing: {
24 readonly tracesUrl: string | null;
25 readonly tracesDataset: string | null;
26 readonly tracesToken: string | null;
27 };
28}
29
30export function trimNonEmpty(value: string | undefined): string | null {
31 return value?.trim() || null;
32}
33
34function normalizeSecureUrl(value: string): string | null {
35 try {
36 const url = new URL(value);
37 return url.protocol === "https:" ? url.toString() : null;
38 } catch {
39 return null;
40 }
41}
42
43export function resolveCloudPublicConfig(): CloudPublicConfig {
44 return {
45 clerkPublishableKey: trimNonEmpty(
46 import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined,
47 ),
48 clerkJwtTemplate: trimNonEmpty(import.meta.env.VITE_CLERK_JWT_TEMPLATE as string | undefined),
49 relayUrl: normalizeSecureRelayUrl(
50 (import.meta.env.VITE_T3CODE_RELAY_URL as string | undefined) ?? "",
51 ),
52 relayTracing: {
53 tracesUrl: normalizeSecureUrl(
54 (import.meta.env.VITE_RELAY_OTLP_TRACES_URL as string | undefined) ?? "",
55 ),
56 tracesDataset: trimNonEmpty(
57 import.meta.env.VITE_RELAY_OTLP_TRACES_DATASET as string | undefined,
58 ),
59 tracesToken: trimNonEmpty(import.meta.env.VITE_RELAY_OTLP_TRACES_TOKEN as string | undefined),
60 },
61 };
62}
63
64export function resolveRelayTracingConfig() {
65 // The relay exporter is created during module initialization, so this must be synchronous.
66 if (
67 typeof window !== "undefined" &&
68 window.desktopBridge?.getRelayTelemetryEnabled?.() === false
69 ) {
70 return null;
71 }
72 const { relayTracing } = resolveCloudPublicConfig();
73 return relayTracing.tracesUrl && relayTracing.tracesDataset && relayTracing.tracesToken
74 ? {
75 tracesUrl: relayTracing.tracesUrl,
76 tracesDataset: relayTracing.tracesDataset,
77 tracesToken: relayTracing.tracesToken,
78 }
79 : null;
80}
81
82export function hasCloudPublicConfig(): boolean {
83 const config = resolveCloudPublicConfig();
84 if (!config.clerkPublishableKey || !config.clerkJwtTemplate || !config.relayUrl) {
85 return false;
86 }
87
88 // Electron has its own auth runtime; development keys also support private browser origins.
89 if (
90 typeof window === "undefined" ||
91 window.desktopBridge !== undefined ||
92 !config.clerkPublishableKey.startsWith("pk_live_")
93 ) {
94 return true;
95 }
96
97 try {
98 // Custom Frontend API hosts use clerk.<production domain>.
99 const domain = clerkFrontendApiHostnameFromPublishableKey(config.clerkPublishableKey).replace(
100 /^clerk\./u,
101 "",
102 );
103 const { hostname, protocol, port } = window.location;
104 return (
105 protocol === "https:" &&
106 port === "" &&
107 (hostname === domain || hostname.endsWith(`.${domain}`))
108 );
109 } catch {
110 return false;
111 }
112}
113
114export function resolveRelayClerkTokenOptions() {
115 const { clerkJwtTemplate } = resolveCloudPublicConfig();
116 if (!clerkJwtTemplate) {
117 throw new CloudPublicConfigMissingError({ key: "T3CODE_CLERK_JWT_TEMPLATE" });
118 }
119 return relayClerkTokenOptions(clerkJwtTemplate);
120}