@@ -7,11 +7,13 @@ import {
77 getCurrentScope ,
88 normalizeUrlToBase ,
99 Profile ,
10+ ProfileChunk ,
1011 ReplayEvent ,
1112} from '@sentry/core' ;
1213import { createGetModuleFromFilename } from '@sentry/node' ;
1314import { app } from 'electron' ;
1415import { ElectronMainOptionsInternal } from './sdk.js' ;
16+ import { SDK_VERSION } from './version.js' ;
1517
1618const getModuleFromFilename = createGetModuleFromFilename ( app . getAppPath ( ) ) ;
1719
@@ -120,3 +122,70 @@ export function normaliseProfile(profile: Profile, basePath: string): void {
120122 }
121123 }
122124}
125+
126+ /**
127+ * Normalizes all URLs in a profile chunk and sets release/environment
128+ */
129+ export function normaliseProfileChunk (
130+ profileChunk : ProfileChunk ,
131+ basePath : string ,
132+ options : ElectronMainOptionsInternal ,
133+ ) : void {
134+ // Override release and environment with main process values
135+ if ( options . release ) {
136+ profileChunk . release = options . release ;
137+ }
138+ if ( options . environment ) {
139+ profileChunk . environment = options . environment ;
140+ }
141+
142+ // Normalize paths in profile frames
143+ const profile = profileChunk . profile as { frames ?: Array < { abs_path ?: string ; filename ?: string ; module ?: string } > } ;
144+ if ( profile ?. frames ) {
145+ for ( const frame of profile . frames ) {
146+ if ( frame . abs_path ) {
147+ frame . abs_path = normalizeUrlToBase ( frame . abs_path , basePath ) ;
148+ }
149+
150+ if ( frame . filename ) {
151+ frame . filename = normalizeUrlToBase ( frame . filename , basePath ) ;
152+ }
153+
154+ if ( frame . module ) {
155+ frame . module = getModuleFromFilename ( frame . abs_path ) ;
156+ }
157+ }
158+ }
159+ }
160+
161+ /**
162+ * Normalizes profile_chunk envelope items and returns the modified envelope
163+ */
164+ export function normalizeProfileChunkEnvelope (
165+ options : ElectronMainOptionsInternal ,
166+ envelope : Envelope ,
167+ basePath : string ,
168+ ) : Envelope {
169+ // Override the SDK info in the envelope header to use Electron SDK
170+ const [ originalHeader ] = envelope ;
171+ const modifiedHeader = {
172+ ...originalHeader ,
173+ sdk : { name : 'sentry.javascript.electron' , version : SDK_VERSION } ,
174+ } ;
175+
176+ let modifiedEnvelope = createEnvelope ( modifiedHeader ) ;
177+ let isProfileChunk = false ;
178+
179+ forEachEnvelopeItem ( envelope , ( item , type ) => {
180+ if ( type === 'profile_chunk' ) {
181+ isProfileChunk = true ;
182+ const [ headers , chunk ] = item as [ { type : 'profile_chunk' } , ProfileChunk ] ;
183+
184+ normaliseProfileChunk ( chunk , basePath , options ) ;
185+
186+ modifiedEnvelope = addItemToEnvelope ( modifiedEnvelope , [ headers , chunk ] ) ;
187+ }
188+ } ) ;
189+
190+ return isProfileChunk ? modifiedEnvelope : envelope ;
191+ }
0 commit comments