import { Event , SdkInfo , SentryRequest , SentryRequestType , Session , SessionAggregates } from '@sentry/types' ; import { dsnToString } from '@sentry/utils' ; import { APIDetails , getEnvelopeEndpointWithUrlEncodedAuth , getStoreEndpointWithUrlEncodedAuth } from './api' ; /** Extract sdk info from from the API metadata */ function getSdkMetadataForEnvelopeHeader ( api ) { if ( ! api . metadata || ! api . metadata . sdk ) { return ; } const { name , version } = api . metadata . sdk ; return { name , version } ; } /**
 * Apply SdkInfo (name, version, packages, integrations) to the corresponding event key.
 * Merge with existing data if any.
 **/ function enhanceEventWithSdkInfo ( event , sdkInfo ? ) { if ( ! sdkInfo ) { return event ; } event . sdk = event . sdk || { } ; event . sdk . name = event . sdk . name || sdkInfo . name ; event . sdk . version = event . sdk . version || sdkInfo . version ; event . sdk . integrations = [ ... ( event . sdk . integrations || [ ] ) , ... ( sdkInfo . integrations || [ ] ) ] ; event . sdk . packages = [ ... ( event . sdk . packages || [ ] ) , ... ( sdkInfo . packages || [ ] ) ] ; return event ; } /** Creates a SentryRequest from a Session. */ export function sessionToSentryRequest ( session , api ) { const sdkInfo = getSdkMetadataForEnvelopeHeader ( api ) ; const envelopeHeaders = JSON . stringify ( { sent_at : new Date ( ) . toISOString ( ) , ... ( sdkInfo && { sdk : sdkInfo } ) , ... ( ! ! api . tunnel && { dsn : dsnToString ( api . dsn ) } ) , } ) ; const type = 'aggregates' in session ? ( 'sessions' as SentryRequestType ) : 'session' ; const itemHeaders = JSON . stringify ( { type , } ) ; return { body : `${ envelopeHeaders }\n${ itemHeaders }\n${ JSON . stringify ( session ) }` , type , url : getEnvelopeEndpointWithUrlEncodedAuth ( api . dsn , api . tunnel ) , } ; } /** Creates a SentryRequest from an event. */ export function eventToSentryRequest ( event , api ) { const sdkInfo = getSdkMetadataForEnvelopeHeader ( api ) ; const eventType = event . type || 'event' ; const useEnvelope = eventType === 'transaction' || ! ! api . tunnel ; const { transactionSampling } = event . sdkProcessingMetadata || { } ; const { method : samplingMethod , rate : sampleRate } = transactionSampling || { } ; delete event . sdkProcessingMetadata ; const req = { body : JSON . stringify ( sdkInfo ? enhanceEventWithSdkInfo ( event , api . metadata . sdk ) : event ) , type : eventType , url : useEnvelope ? getEnvelopeEndpointWithUrlEncodedAuth ( api . dsn , api . tunnel ) : getStoreEndpointWithUrlEncodedAuth ( api . dsn ) , } ; if ( useEnvelope ) { const envelopeHeaders = JSON . stringify ( { event_id : event . event_id , sent_at : new Date ( ) . toISOString ( ) , ... ( sdkInfo && { sdk : sdkInfo } ) , ... ( ! ! api . tunnel && { dsn : dsnToString ( api . dsn ) } ) , } ) ; const itemHeaders = JSON . stringify ( { type : eventType , sample_rates : [ { id : samplingMethod , rate : sampleRate } ] , } ) ; const envelope = `${ envelopeHeaders }\n${ itemHeaders }\n${ req . body }` ; req . body = envelope ; } return req ; }