import { Event , EventProcessor , Hub , Integration , StackFrame , Stacktrace } from '@sentry/types' ; import { basename , relative } from '@sentry/utils' ; type StackFrameIteratee = ( frame : StackFrame ) => StackFrame ; /** Rewrite event frames paths */ export class RewriteFrames implements Integration { inheritDoc public static id : string = 'RewriteFrames' ; inheritDoc public name : string = RewriteFrames . id ; inheritDoc private readonly _root ? : string ; inheritDoc private readonly _prefix : string = 'app:///' ; inheritDoc public constructor ( options = { } ) { if ( options . root ) { this . _root = options . root ; } if ( options . prefix ) { this . _prefix = options . prefix ; } if ( options . iteratee ) { this . _iteratee = options . iteratee ; } } inheritDoc public setupOnce ( addGlobalEventProcessor , getCurrentHub ) { addGlobalEventProcessor ( event => { const self = getCurrentHub ( ) . getIntegration ( RewriteFrames ) ; if ( self ) { return self . process ( event ) ; } return event ; } ) ; } /** JSDoc */ public process ( originalEvent ) { let processedEvent = originalEvent ; if ( originalEvent . exception && Array . isArray ( originalEvent . exception . values ) ) { processedEvent = this . _processExceptionsEvent ( processedEvent ) ; } if ( originalEvent . stacktrace ) { processedEvent = this . _processStacktraceEvent ( processedEvent ) ; } return processedEvent ; } inheritDoc private readonly _iteratee : StackFrameIteratee = ( frame ) => { if ( ! frame . filename ) { return frame ; } const isWindowsFrame = /^[A-Z]:\\/ . test ( frame . filename ) ; const startsWithSlash = /^\// . test ( frame . filename ) ; if ( isWindowsFrame || startsWithSlash ) { const filename = isWindowsFrame ? frame . filename . replace ( /^[A-Z]:/ , '' ) . replace ( /\\/g , '/' ) : frame . filename ; const base = this . _root ? relative ( this . _root , filename ) : basename ( filename ) ; frame . filename = `${ this . _prefix }${ base }` ; } return frame ; } ; /** JSDoc */ private _processExceptionsEvent ( event ) { try { return { ... event , exception : { ... event . exception , values : event . exception ! . values ! . map ( value => ( { ... value , stacktrace : this . _processStacktrace ( value . stacktrace ) , } ) ) , } , } ; } catch ( _oO ) { return event ; } } /** JSDoc */ private _processStacktraceEvent ( event ) { try { return { ... event , stacktrace : this . _processStacktrace ( event . stacktrace ) , } ; } catch ( _oO ) { return event ; } } /** JSDoc */ private _processStacktrace ( stacktrace ? ) { return { ... stacktrace , frames : stacktrace && stacktrace . frames && stacktrace . frames . map ( f => this . _iteratee ( f ) ) , } ; } }