1
1
/* eslint-disable max-lines */
2
-
3
2
import * as fs from 'fs' ;
4
3
import * as http from 'http' ;
5
4
import type { AddressInfo } from 'net' ;
@@ -18,11 +17,6 @@ interface EventProxyServerOptions {
18
17
port : number ;
19
18
/** The name for the proxy server used for referencing it with listener functions */
20
19
proxyServerName : string ;
21
- /**
22
- * Whether or not to forward the event to sentry. @default `false`
23
- * This is helpful when you can't register a tunnel in the SDK setup (e.g. lambda layer without Sentry.init call)
24
- */
25
- forwardToSentry ?: boolean ;
26
20
}
27
21
28
22
interface SentryRequestCallbackData {
@@ -36,12 +30,16 @@ interface EventCallbackListener {
36
30
( data : string ) : void ;
37
31
}
38
32
33
+ type SentryResponseStatusCode = number ;
34
+ type SentryResponseBody = string ;
35
+ type SentryResponseHeaders = Record < string , string > | undefined ;
36
+
39
37
type OnRequest = (
40
38
eventCallbackListeners : Set < EventCallbackListener > ,
41
39
proxyRequest : http . IncomingMessage ,
42
40
proxyRequestBody : string ,
43
41
eventBuffer : BufferedEvent [ ] ,
44
- ) => Promise < [ number , string , Record < string , string > | undefined ] > ;
42
+ ) => Promise < [ SentryResponseStatusCode , SentryResponseBody , SentryResponseHeaders ] > ;
45
43
46
44
interface BufferedEvent {
47
45
timestamp : number ;
@@ -170,83 +168,28 @@ export async function startProxyServer(
170
168
*/
171
169
export async function startEventProxyServer ( options : EventProxyServerOptions ) : Promise < void > {
172
170
await startProxyServer ( options , async ( eventCallbackListeners , proxyRequest , proxyRequestBody , eventBuffer ) => {
173
- const envelopeHeader : EnvelopeItem [ 0 ] = JSON . parse ( proxyRequestBody . split ( '\n' ) [ 0 ] as string ) ;
171
+ const data : SentryRequestCallbackData = {
172
+ envelope : parseEnvelope ( proxyRequestBody ) ,
173
+ rawProxyRequestBody : proxyRequestBody ,
174
+ rawSentryResponseBody : '' ,
175
+ sentryResponseStatusCode : 200 ,
176
+ } ;
174
177
175
- const shouldForwardEventToSentry = options . forwardToSentry || false ;
178
+ const dataString = Buffer . from ( JSON . stringify ( data ) ) . toString ( 'base64' ) ;
176
179
177
- if ( ! envelopeHeader . dsn && shouldForwardEventToSentry ) {
178
- // eslint-disable-next-line no-console
179
- console . log (
180
- '[event-proxy-server] Warn: No dsn on envelope header. Maybe a client-report was received. Proxy request body:' ,
181
- proxyRequestBody ,
182
- ) ;
183
-
184
- return [ 200 , '{}' , { } ] ;
185
- }
186
-
187
- if ( ! shouldForwardEventToSentry ) {
188
- const data : SentryRequestCallbackData = {
189
- envelope : parseEnvelope ( proxyRequestBody ) ,
190
- rawProxyRequestBody : proxyRequestBody ,
191
- rawSentryResponseBody : '' ,
192
- sentryResponseStatusCode : 200 ,
193
- } ;
194
- eventCallbackListeners . forEach ( listener => {
195
- listener ( Buffer . from ( JSON . stringify ( data ) ) . toString ( 'base64' ) ) ;
196
- } ) ;
197
-
198
- return [
199
- 200 ,
200
- '{}' ,
201
- {
202
- 'Access-Control-Allow-Origin' : '*' ,
203
- } ,
204
- ] ;
205
- }
206
-
207
- const { origin, pathname, host } = new URL ( envelopeHeader . dsn as string ) ;
208
-
209
- const projectId = pathname . substring ( 1 ) ;
210
- const sentryIngestUrl = `${ origin } /api/${ projectId } /envelope/` ;
211
-
212
- proxyRequest . headers . host = host ;
213
-
214
- const reqHeaders : Record < string , string > = { } ;
215
- for ( const [ key , value ] of Object . entries ( proxyRequest . headers ) ) {
216
- reqHeaders [ key ] = value as string ;
217
- }
218
-
219
- // Fetch does not like this
220
- delete reqHeaders [ 'transfer-encoding' ] ;
221
-
222
- return fetch ( sentryIngestUrl , {
223
- body : proxyRequestBody ,
224
- headers : reqHeaders ,
225
- method : proxyRequest . method ,
226
- } ) . then ( async res => {
227
- const rawSentryResponseBody = await res . text ( ) ;
228
- const data : SentryRequestCallbackData = {
229
- envelope : parseEnvelope ( proxyRequestBody ) ,
230
- rawProxyRequestBody : proxyRequestBody ,
231
- rawSentryResponseBody,
232
- sentryResponseStatusCode : res . status ,
233
- } ;
234
-
235
- const dataString = Buffer . from ( JSON . stringify ( data ) ) . toString ( 'base64' ) ;
236
-
237
- eventBuffer . push ( { data : dataString , timestamp : getNanosecondTimestamp ( ) } ) ;
238
-
239
- eventCallbackListeners . forEach ( listener => {
240
- listener ( dataString ) ;
241
- } ) ;
242
-
243
- const resHeaders : Record < string , string > = { } ;
244
- for ( const [ key , value ] of res . headers . entries ( ) ) {
245
- resHeaders [ key ] = value ;
246
- }
180
+ eventBuffer . push ( { data : dataString , timestamp : getNanosecondTimestamp ( ) } ) ;
247
181
248
- return [ res . status , rawSentryResponseBody , resHeaders ] ;
182
+ eventCallbackListeners . forEach ( listener => {
183
+ listener ( dataString ) ;
249
184
} ) ;
185
+
186
+ return [
187
+ 200 ,
188
+ '{}' ,
189
+ {
190
+ 'Access-Control-Allow-Origin' : '*' ,
191
+ } ,
192
+ ] ;
250
193
} ) ;
251
194
}
252
195
0 commit comments