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