1 | {"version":3,"file":"envelope.js","sources":["../../src/envelope.ts"],"sourcesContent":["import {\n Baggage,\n DsnComponents,\n DynamicSamplingContext,\n Event,\n EventEnvelope,\n EventEnvelopeHeaders,\n EventItem,\n SdkInfo,\n SdkMetadata,\n Session,\n SessionAggregates,\n SessionEnvelope,\n SessionItem,\n} from '@sentry/types';\nimport { createEnvelope, dropUndefinedKeys, dsnToString, getSentryBaggageItems } from '@sentry/utils';\n\n/** Extract sdk info from from the API metadata */\nfunction getSdkMetadataForEnvelopeHeader(metadata?: SdkMetadata): SdkInfo | undefined {\n if (!metadata || !metadata.sdk) {\n return;\n }\n const { name, version } = metadata.sdk;\n return { name, version };\n}\n\n/**\n * Apply SdkInfo (name, version, packages, integrations) to the corresponding event key.\n * Merge with existing data if any.\n **/\nfunction enhanceEventWithSdkInfo(event: Event, sdkInfo?: SdkInfo): Event {\n if (!sdkInfo) {\n return event;\n }\n event.sdk = event.sdk || {};\n event.sdk.name = event.sdk.name || sdkInfo.name;\n event.sdk.version = event.sdk.version || sdkInfo.version;\n event.sdk.integrations = [...(event.sdk.integrations || []), ...(sdkInfo.integrations || [])];\n event.sdk.packages = [...(event.sdk.packages || []), ...(sdkInfo.packages || [])];\n return event;\n}\n\n/** Creates an envelope from a Session */\nexport function createSessionEnvelope(\n session: Session | SessionAggregates,\n dsn: DsnComponents,\n metadata?: SdkMetadata,\n tunnel?: string,\n): SessionEnvelope {\n const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata);\n const envelopeHeaders = {\n sent_at: new Date().toISOString(),\n ...(sdkInfo && { sdk: sdkInfo }),\n ...(!!tunnel && { dsn: dsnToString(dsn) }),\n };\n\n const envelopeItem: SessionItem =\n 'aggregates' in session ? [{ type: 'sessions' }, session] : [{ type: 'session' }, session];\n\n return createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]);\n}\n\n/**\n * Create an Envelope from an event.\n */\nexport function createEventEnvelope(\n event: Event,\n dsn: DsnComponents,\n metadata?: SdkMetadata,\n tunnel?: string,\n): EventEnvelope {\n const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata);\n const eventType = event.type || 'event';\n\n const { transactionSampling } = event.sdkProcessingMetadata || {};\n const { method: samplingMethod, rate: sampleRate } = transactionSampling || {};\n\n enhanceEventWithSdkInfo(event, metadata && metadata.sdk);\n\n const envelopeHeaders = createEventEnvelopeHeaders(event, sdkInfo, tunnel, dsn);\n\n // Prevent this data (which, if it exists, was used in earlier steps in the processing pipeline) from being sent to\n // sentry. (Note: Our use of this property comes and goes with whatever we might be debugging, whatever hacks we may\n // have temporarily added, etc. Even if we don't happen to be using it at some point in the future, let's not get rid\n // of this `delete`, lest we miss putting it back in the next time the property is in use.)\n delete event.sdkProcessingMetadata;\n\n const eventItem: EventItem = [\n {\n type: eventType,\n sample_rates: [{ id: samplingMethod, rate: sampleRate }],\n },\n event,\n ];\n return createEnvelope<EventEnvelope>(envelopeHeaders, [eventItem]);\n}\n\nfunction createEventEnvelopeHeaders(\n event: Event,\n sdkInfo: SdkInfo | undefined,\n tunnel: string | undefined,\n dsn: DsnComponents,\n): EventEnvelopeHeaders {\n const baggage: Baggage | undefined = event.sdkProcessingMetadata && event.sdkProcessingMetadata.baggage;\n const dynamicSamplingContext = baggage && getSentryBaggageItems(baggage);\n\n return {\n event_id: event.event_id as string,\n sent_at: new Date().toISOString(),\n ...(sdkInfo && { sdk: sdkInfo }),\n ...(!!tunnel && { dsn: dsnToString(dsn) }),\n ...(event.type === 'transaction' &&\n dynamicSamplingContext && {\n trace: dropUndefinedKeys({ ...dynamicSamplingContext }) as DynamicSamplingContext,\n }),\n };\n}\n"],"names":[],"mappings":";;;;AAiBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;"} |