import { SpanAttributes } from '@opentelemetry/api' ; import { ExpressLayer , PatchedRequest , _LAYERS_STORE_PROPERTY , IgnoreMatcher , ExpressInstrumentationConfig , } from './types' ; import { ExpressLayerType } from './enums/ExpressLayerType' ; import { AttributeNames } from './enums/AttributeNames' ; param request param value export const storeLayerPath = ( request , value ? ) => { if ( Array . isArray ( request [ _LAYERS_STORE_PROPERTY ] ) === false ) { Object . defineProperty ( request , _LAYERS_STORE_PROPERTY , { enumerable : false , value : [ ] , } ) ; } if ( value === undefined ) return ; ( request [ _LAYERS_STORE_PROPERTY ] as string [ ] ) . push ( value ) ; } ; param layer param layerPath export const getLayerMetadata = ( layer , layerPath ? ) : { attributes : SpanAttributes ; name : string ; } => { if ( layer . name === 'router' ) { return { attributes : { [ AttributeNames . EXPRESS_NAME ] : layerPath , [ AttributeNames . EXPRESS_TYPE ] : ExpressLayerType . ROUTER , } , name : `router - ${ layerPath }` , } ; } else if ( layer . name === 'bound dispatch' ) { return { attributes : { [ AttributeNames . EXPRESS_NAME ] : layerPath ?? 'request handler' , [ AttributeNames . EXPRESS_TYPE ] : ExpressLayerType . REQUEST_HANDLER , } , name : `request handler${ layer . path ? ` - ${ layerPath }` : '' }` , } ; } else { return { attributes : { [ AttributeNames . EXPRESS_NAME ] : layer . name , [ AttributeNames . EXPRESS_TYPE ] : ExpressLayerType . MIDDLEWARE , } , name : `middleware - ${ layer . name }` , } ; } } ; param constant param obj param pattern const satisfiesPattern = ( constant , pattern ) : boolean => { if ( typeof pattern === 'string' ) { return pattern === constant ; } else if ( pattern instanceof RegExp ) { return pattern . test ( constant ) ; } else if ( typeof pattern === 'function' ) { return pattern ( constant ) ; } else { throw new TypeError ( 'Pattern is in unsupported datatype' ) ; } } ; param constant param list param onException export const isLayerIgnored = ( name , type , config ? ) : boolean => { if ( Array . isArray ( config ?. ignoreLayersType ) && config ?. ignoreLayersType ?. includes ( type ) ) { return true ; } if ( Array . isArray ( config ?. ignoreLayers ) === false ) return false ; try { for ( const pattern of config ! . ignoreLayers ! ) { if ( satisfiesPattern ( name , pattern ) ) { return true ; } } } catch ( e ) { } return false ; } ;