import { config , putLogData } from "../telemetry/electron-metrics" ; export class PortingAssistantBuffer { name : string ; capacity : number ; buff : any [ ] ; size : number ; flushInterval : number ; maxCache : number ; constructor ( name , capacity , flushInterval , maxCache ) { this . name = name ; this . capacity = capacity ; this . buff = new Array < any [ ] > ( ) ; this . size = 0 ; this . flushInterval = flushInterval ; this . maxCache = maxCache ; setInterval ( ( ) => { this . flush ( ) ; } , this . flushInterval ) ; } push = async ( element ) => { try { if ( ! element ) { return ; } if ( this . size + ( element ?. length || 0 ) >= this . capacity && Number . MAX_SAFE_INTEGER - ( element ?. length || 0 ) > this . size ) { await this . flush ( ) ; } if ( this . size + element ?. length >= this . capacity ) { while ( this . size + ( element ?. length || 0 ) >= this . maxCache ) { const removed = this . buff . shift ( ) ! ; this . size -= removed ?. length || 0 ; } } this . buff . push ( element ) ; this . size += element ?. length || 0 ; } catch ( error ) { console . error ( "Error in pushing data: " , error ) ; } } ; flush = async ( ) => { if ( this . size > 0 ) { try { const success = await putLogData ( this . name , this . getString ( ) ) ; if ( success ) { this . buff . length = 0 ; this . size = 0 ; } } catch ( error ) { console . error ( "Error in flushing data: " , error ) ; } } } ; getString ( ) { return Buffer . from ( JSON . stringify ( this . buff ) , "utf8" ) . toString ( "base64" ) ; } } export const backendLogBuffer = new PortingAssistantBuffer ( "portingAssistant-backend-logs" , config . PortingAssistantMetrics . MaxBufferCapacity , config . PortingAssistantMetrics . FlushInterval , config . MaxBufferCache ) ; export const electronLogBuffer = new PortingAssistantBuffer ( "electron-logs" , config . PortingAssistantMetrics . MaxBufferCapacity , config . PortingAssistantMetrics . FlushInterval , config . MaxBufferCache ) ; export const metricsBuffer = new PortingAssistantBuffer ( "portingAssistant-metrics" , config . PortingAssistantMetrics . MaxBufferCapacity , config . PortingAssistantMetrics . FlushInterval , config . MaxBufferCache ) ; export const reactErrorBuffer = new PortingAssistantBuffer ( "react-errors" , config . PortingAssistantMetrics . MaxBufferCapacity , config . PortingAssistantMetrics . FlushInterval , config . MaxBufferCache ) ;