@@ -6,8 +6,7 @@ import { nodeFsOrThrow, nodePathOrThrow } from './fs/utils';
66
77const EVENT_KEYLESS_ENV_DRIFT_DETECTED = 'KEYLESS_ENV_DRIFT_DETECTED' ;
88const EVENT_SAMPLING_RATE = 1 ; // 100% sampling rate
9- const TELEMETRY_FLAG_FILE = '.clerk/.tmp/telemetry.json' ;
10-
9+ const TELEMETRY_FLAG_FILE_TEMPLATE = '.clerk/.tmp/telemetry.{pk}.log' ;
1110type EventKeylessEnvDriftPayload = {
1211 publicKeyMatch : boolean ;
1312 secretKeyMatch : boolean ;
@@ -25,9 +24,9 @@ type EventKeylessEnvDriftPayload = {
2524 *
2625 * @returns The absolute path to the telemetry flag file in the project's .clerk/.tmp directory
2726 */
28- function getTelemetryFlagFilePath ( ) : string {
27+ function getTelemetryFlagFilePath ( pk : string ) : string {
2928 const path = nodePathOrThrow ( ) ;
30- return path . join ( process . cwd ( ) , TELEMETRY_FLAG_FILE ) ;
29+ return path . join ( process . cwd ( ) , TELEMETRY_FLAG_FILE_TEMPLATE . replace ( '{pk}' , pk ) ) ;
3130}
3231
3332/**
@@ -41,26 +40,22 @@ function getTelemetryFlagFilePath(): string {
4140 * the event should be fired), false if the file already exists (meaning the event was
4241 * already fired) or if there was an error creating the file
4342 */
44- function tryMarkTelemetryEventAsFired ( ) : boolean {
43+ function tryMarkTelemetryEventAsFired ( pk : string ) : boolean {
44+ if ( ! canUseKeyless ) {
45+ return false ;
46+ }
4547 try {
46- if ( canUseKeyless ) {
47- const { mkdirSync, writeFileSync } = nodeFsOrThrow ( ) ;
48- const path = nodePathOrThrow ( ) ;
49- const flagFilePath = getTelemetryFlagFilePath ( ) ;
50- const flagDirectory = path . dirname ( flagFilePath ) ;
48+ const { mkdirSync, writeFileSync } = nodeFsOrThrow ( ) ;
49+ const path = nodePathOrThrow ( ) ;
50+ const flagFilePath = getTelemetryFlagFilePath ( pk ) ;
51+ const flagDirectory = path . dirname ( flagFilePath ) ;
5152
52- // Ensure the directory exists before attempting to write the file
53- mkdirSync ( flagDirectory , { recursive : true } ) ;
53+ // Ensure the directory exists before attempting to write the file
54+ mkdirSync ( flagDirectory , { recursive : true } ) ;
5455
55- const flagData = {
56- firedAt : new Date ( ) . toISOString ( ) ,
57- event : EVENT_KEYLESS_ENV_DRIFT_DETECTED ,
58- } ;
59- writeFileSync ( flagFilePath , JSON . stringify ( flagData , null , 2 ) , { flag : 'wx' } ) ;
60- return true ;
61- } else {
62- return false ;
63- }
56+ const fileContent = `Content not important. File name is the identifier for the telemetry event.` ;
57+ writeFileSync ( flagFilePath , fileContent , { flag : 'wx' } ) ;
58+ return true ;
6459 } catch ( error : unknown ) {
6560 if ( ( error as { code ?: string } ) ?. code === 'EEXIST' ) {
6661 return false ;
@@ -96,10 +91,6 @@ export async function detectKeylessEnvDrift(): Promise<void> {
9691 if ( ! canUseKeyless ) {
9792 return ;
9893 }
99- // Only run on server side
100- if ( typeof window !== 'undefined' ) {
101- return ;
102- }
10394
10495 try {
10596 // Dynamically import server-side dependencies to avoid client-side issues
@@ -165,7 +156,7 @@ export async function detectKeylessEnvDrift(): Promise<void> {
165156 secretKeyMatch,
166157 envVarsMissing,
167158 keylessFileHasKeys,
168- keylessPublishableKey : keylessFile . publishableKey ?? '' ,
159+ keylessPublishableKey : keylessFile . publishableKey ,
169160 envPublishableKey : envPublishableKey ?? '' ,
170161 } ;
171162
@@ -178,7 +169,7 @@ export async function detectKeylessEnvDrift(): Promise<void> {
178169 } ,
179170 } ) ;
180171
181- const shouldFireEvent = tryMarkTelemetryEventAsFired ( ) ;
172+ const shouldFireEvent = tryMarkTelemetryEventAsFired ( keylessFile . publishableKey ) ;
182173
183174 if ( shouldFireEvent ) {
184175 // Fire drift detected event only if we successfully created the flag
0 commit comments