import noop from '@tinkoff/utils/function/noop' ; import pathEq from '@tinkoff/utils/object/pathEq' ; import type { Reporter , LogObj } from '../logger.h' ; import type { LEVELS } from '../constants' ; import { formatJson } from './utils/formatJson' ; type EmitLevels = Partial < Record < keyof typeof LEVELS , boolean > > ; type MakeRequest = ( logObj : Record < string , any > ) => Promise < any > ; export class RemoteReporter implements Reporter { private requestCount : number ; private makeRequest : MakeRequest ; private emitLevels : EmitLevels ; private reqCount = 0 ; private queue : LogObj [ ] = [ ] ; constructor ( { requestCount , makeRequest , emitLevels , } ) { this . requestCount = requestCount || 1 ; this . makeRequest = makeRequest ; this . emitLevels = emitLevels || { fatal : true } ; } checkQueue ( ) { if ( this . queue . length && this . reqCount < this . requestCount ) { this . sendRequest ( ) ; } } sendRequest ( ) { const logObj = this . queue . shift ( ) ; this . reqCount ++ ; this . makeRequest ( formatJson ( logObj ) ) . catch ( noop ) . then ( ( ) => { this . reqCount -- ; this . checkQueue ( ) ; } ) ; } remote ( logObj ) { this . queue . push ( logObj ) ; this . checkQueue ( ) ; } log ( logObj ) { const { type , remote } = logObj ; if ( remote != null ) { if ( remote === true || pathEq ( [ 'emitLevels' , type ] , true , remote ) ) { this . remote ( { ... logObj , remote : undefined } ) ; } return ; } if ( this . emitLevels [ type ] ) { this . remote ( logObj ) ; } } }