@@ -17,6 +17,11 @@ interface EventProxyServerOptions {
1717 port : number ;
1818 /** The name for the proxy server used for referencing it with listener functions */
1919 proxyServerName : string ;
20+ /**
21+ * Whether or not to forward the event to sentry. @default `true`
22+ * This is helpful when you can't register a tunnel in the SDK setup (e.g. lambda layer without Sentry.init call)
23+ */
24+ forwardToSentry ?: boolean ;
2025}
2126
2227interface SentryRequestCallbackData {
@@ -56,7 +61,9 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
5661
5762 const envelopeHeader : EnvelopeItem [ 0 ] = JSON . parse ( proxyRequestBody . split ( '\n' ) [ 0 ] ) ;
5863
59- if ( ! envelopeHeader . dsn ) {
64+ const shouldForwardEventToSentry = options . forwardToSentry != null ? options . forwardToSentry : true ;
65+
66+ if ( ! envelopeHeader . dsn && shouldForwardEventToSentry ) {
6067 // eslint-disable-next-line no-console
6168 console . log (
6269 '[event-proxy-server] Warn: No dsn on envelope header. Maybe a client-report was received. Proxy request body:' ,
@@ -69,6 +76,23 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
6976 return ;
7077 }
7178
79+ if ( ! shouldForwardEventToSentry ) {
80+ const data : SentryRequestCallbackData = {
81+ envelope : parseEnvelope ( proxyRequestBody ) ,
82+ rawProxyRequestBody : proxyRequestBody ,
83+ rawSentryResponseBody : '' ,
84+ sentryResponseStatusCode : 200 ,
85+ } ;
86+ eventCallbackListeners . forEach ( listener => {
87+ listener ( Buffer . from ( JSON . stringify ( data ) ) . toString ( 'base64' ) ) ;
88+ } ) ;
89+
90+ proxyResponse . writeHead ( 200 ) ;
91+ proxyResponse . write ( '{}' , 'utf-8' ) ;
92+ proxyResponse . end ( ) ;
93+ return ;
94+ }
95+
7296 const { origin, pathname, host } = new URL ( envelopeHeader . dsn as string ) ;
7397
7498 const projectId = pathname . substring ( 1 ) ;
@@ -269,7 +293,13 @@ async function registerCallbackServerPort(serverName: string, port: string): Pro
269293 await writeFile ( tmpFilePath , port , { encoding : 'utf8' } ) ;
270294}
271295
272- function retrieveCallbackServerPort ( serverName : string ) : Promise < string > {
273- const tmpFilePath = path . join ( os . tmpdir ( ) , `${ TEMP_FILE_PREFIX } ${ serverName } ` ) ;
274- return readFile ( tmpFilePath , 'utf8' ) ;
296+ async function retrieveCallbackServerPort ( serverName : string ) : Promise < string > {
297+ try {
298+ const tmpFilePath = path . join ( os . tmpdir ( ) , `${ TEMP_FILE_PREFIX } ${ serverName } ` ) ;
299+ return await readFile ( tmpFilePath , 'utf8' ) ;
300+ } catch ( e ) {
301+ // eslint-disable-next-line no-console
302+ console . log ( 'Could not read callback server port' , e ) ;
303+ throw e ;
304+ }
275305}
0 commit comments