@@ -14,13 +14,18 @@ import type {
1414 OsType ,
1515} from '../types.js' ;
1616
17- const INCLUDE_PID_HEADER = process . env . INCLUDE_PID_HEADER === 'true' ;
18- const MAX_BUFFER_SIZE = parseInt ( process . env . MAX_BUFFER_SIZE ?? '' , 10 ) || 1000 ;
19- const FLUSH_INTERVAL_MS =
20- parseInt ( process . env . FORCE_FLUSH_INTERVAL ?? '' , 10 ) || 15 * 60 * 1000 ;
21- const CLEARCUT_ENDPOINT =
22- process . env . CLEARCUT_ENDPOINT ??
17+ export interface ClearcutSenderConfig {
18+ appVersion : string ;
19+ osType : OsType ;
20+ clearcutEndpoint ?: string ;
21+ forceFlushIntervalMs ?: number ;
22+ includePidHeader ?: boolean ;
23+ }
24+
25+ const MAX_BUFFER_SIZE = 1000 ;
26+ const DEFAULT_CLEARCUT_ENDPOINT =
2327 'https://play.googleapis.com/log?format=json_proto' ;
28+ const DEFAULT_FLUSH_INTERVAL_MS = 15 * 60 * 1000 ;
2429
2530const LOG_SOURCE = 2839 ;
2631const CLIENT_TYPE = 47 ;
@@ -37,16 +42,24 @@ interface BufferedEvent {
3742export class ClearcutSender {
3843 #appVersion: string ;
3944 #osType: OsType ;
45+ #clearcutEndpoint: string ;
46+ #flushIntervalMs: number ;
47+ #includePidHeader: boolean ;
4048 #sessionId: string ;
4149 #sessionCreated: number ;
4250 #buffer: BufferedEvent [ ] = [ ] ;
4351 #flushTimer: ReturnType < typeof setTimeout > | null = null ;
4452 #isFlushing = false ;
4553 #timerStarted = false ;
4654
47- constructor ( appVersion : string , osType : OsType ) {
48- this . #appVersion = appVersion ;
49- this . #osType = osType ;
55+ constructor ( config : ClearcutSenderConfig ) {
56+ this . #appVersion = config . appVersion ;
57+ this . #osType = config . osType ;
58+ this . #clearcutEndpoint =
59+ config . clearcutEndpoint ?? DEFAULT_CLEARCUT_ENDPOINT ;
60+ this . #flushIntervalMs =
61+ config . forceFlushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS ;
62+ this . #includePidHeader = config . includePidHeader ?? false ;
5063 this . #sessionId = crypto . randomUUID ( ) ;
5164 this . #sessionCreated = Date . now ( ) ;
5265 }
@@ -66,7 +79,7 @@ export class ClearcutSender {
6679
6780 if ( ! this . #timerStarted) {
6881 this . #timerStarted = true ;
69- this . #scheduleFlush( FLUSH_INTERVAL_MS ) ;
82+ this . #scheduleFlush( this . #flushIntervalMs ) ;
7083 }
7184 }
7285
@@ -97,12 +110,12 @@ export class ClearcutSender {
97110 }
98111
99112 if ( this . #buffer. length === 0 ) {
100- this . #scheduleFlush( FLUSH_INTERVAL_MS ) ;
113+ this . #scheduleFlush( this . #flushIntervalMs ) ;
101114 return ;
102115 }
103116
104117 this . #isFlushing = true ;
105- let nextDelayMs = FLUSH_INTERVAL_MS ;
118+ let nextDelayMs = this . #flushIntervalMs ;
106119
107120 // Optimistically remove events from buffer before sending.
108121 // This prevents race conditions where a simultaneous #finalFlush would include these same events.
@@ -182,12 +195,12 @@ export class ClearcutSender {
182195 const controller = new AbortController ( ) ;
183196 const timeoutId = setTimeout ( ( ) => controller . abort ( ) , REQUEST_TIMEOUT_MS ) ;
184197 try {
185- const response = await fetch ( CLEARCUT_ENDPOINT , {
198+ const response = await fetch ( this . #clearcutEndpoint , {
186199 method : 'POST' ,
187200 headers : {
188201 'Content-Type' : 'application/json' ,
189202 // Used in E2E tests to confirm that the watchdog process is killed
190- ...( INCLUDE_PID_HEADER ? { 'X-Watchdog-Pid' : process . pid . toString ( ) } : { } ) ,
203+ ...( this . #includePidHeader ? { 'X-Watchdog-Pid' : process . pid . toString ( ) } : { } ) ,
191204 } ,
192205 body : JSON . stringify ( requestBody ) ,
193206 signal : controller . signal ,
0 commit comments