@@ -17,6 +17,11 @@ interface EventProxyServerOptions {
17
17
port : number ;
18
18
/** The name for the proxy server used for referencing it with listener functions */
19
19
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 ;
20
25
}
21
26
22
27
interface SentryRequestCallbackData {
@@ -56,7 +61,9 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
56
61
57
62
const envelopeHeader : EnvelopeItem [ 0 ] = JSON . parse ( proxyRequestBody . split ( '\n' ) [ 0 ] ) ;
58
63
59
- if ( ! envelopeHeader . dsn ) {
64
+ const shouldForwardEventToSentry = options . forwardToSentry != null ? options . forwardToSentry : true ;
65
+
66
+ if ( ! envelopeHeader . dsn && shouldForwardEventToSentry ) {
60
67
// eslint-disable-next-line no-console
61
68
console . log (
62
69
'[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
69
76
return ;
70
77
}
71
78
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
+
72
96
const { origin, pathname, host } = new URL ( envelopeHeader . dsn as string ) ;
73
97
74
98
const projectId = pathname . substring ( 1 ) ;
@@ -269,7 +293,13 @@ async function registerCallbackServerPort(serverName: string, port: string): Pro
269
293
await writeFile ( tmpFilePath , port , { encoding : 'utf8' } ) ;
270
294
}
271
295
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
+ }
275
305
}
0 commit comments