import { EventProcessor , Hub , Integration } from '@sentry/types' ; import { getGlobalObject , supportsReportingObserver } from '@sentry/utils' ; /** JSDoc */ interface Report { [ key ] : any ; type : ReportTypes ; url : string ; body ? : ReportBody ; } /** JSDoc */ const enum ReportTypes { /** JSDoc */ Crash = 'crash' , /** JSDoc */ Deprecation = 'deprecation' , /** JSDoc */ Intervention = 'intervention' } /** JSDoc */ type ReportBody = CrashReportBody | DeprecationReportBody | InterventionReportBody ; /** JSDoc */ interface CrashReportBody { [ key ] : any ; crashId : string ; reason ? : string ; } /** JSDoc */ interface DeprecationReportBody { [ key ] : any ; id : string ; anticipatedRemoval ? : Date ; message : string ; sourceFile ? : string ; lineNumber ? : number ; columnNumber ? : number ; } /** JSDoc */ interface InterventionReportBody { [ key ] : any ; id : string ; message : string ; sourceFile ? : string ; lineNumber ? : number ; columnNumber ? : number ; } /** Reporting API integration - https://w3c.github.io/reporting/ */ export class ReportingObserver implements Integration { inheritDoc public static id : string = 'ReportingObserver' ; inheritDoc public readonly name : string = ReportingObserver . id ; /**
     * Returns current hub.
     */ private _getCurrentHub ? : ( ) => Hub ; inheritDoc public constructor ( private readonly _options = { types : [ ReportTypes . Crash , ReportTypes . Deprecation , ReportTypes . Intervention ] , } ) { } inheritDoc public setupOnce ( _ , getCurrentHub ) { if ( ! supportsReportingObserver ( ) ) { return ; } this . _getCurrentHub = getCurrentHub ; const observer = new ( getGlobalObject < any > ( ) . ReportingObserver ) ( this . handler . bind ( this ) , { buffered : true , types : this . _options . types , } ) ; observer . observe ( ) ; } inheritDoc public handler ( reports ) { const hub = this . _getCurrentHub && this . _getCurrentHub ( ) ; if ( ! hub || ! hub . getIntegration ( ReportingObserver ) ) { return ; } for ( const report of reports ) { hub . withScope ( scope => { scope . setExtra ( 'url' , report . url ) ; const label = `ReportingObserver [${ report . type }]` ; let details = 'No details available' ; if ( report . body ) { const plainBody = { } ; for ( const prop in report . body ) { plainBody [ prop ] = report . body [ prop ] ; } scope . setExtra ( 'body' , plainBody ) ; if ( report . type === ReportTypes . Crash ) { const body = report . body as CrashReportBody ; details = [ body . crashId || '' , body . reason || '' ] . join ( ' ' ) . trim ( ) || details ; } else { const body = report . body as DeprecationReportBody | InterventionReportBody ; details = body . message || details ; } } hub . captureMessage ( `${ label }: ${ details }` ) ; } ) ; } } }