@@ -130,31 +130,11 @@ export function withSentry(origHandler: NextApiHandler, parameterizedRoute?: str
130130 ) ;
131131 currentScope . setSpan ( transaction ) ;
132132
133- if ( platformSupportsStreaming ( ) ) {
133+ if ( platformSupportsStreaming ( ) && ! origHandler . __sentry_test_doesnt_support_streaming__ ) {
134134 autoEndTransactionOnResponseEnd ( transaction , res ) ;
135135 } else {
136- // If we're not on a platform that supports streaming, we're blocking all response-ending methods until the
137- // queue is flushed.
138-
139- const origResSend = res . send ;
140- res . send = async function ( this : unknown , ...args : unknown [ ] ) {
141- if ( transaction ) {
142- await finishTransaction ( transaction , res ) ;
143- await flushQueue ( ) ;
144- }
145-
146- origResSend . apply ( this , args ) ;
147- } ;
148-
149- const origResJson = res . json ;
150- res . json = async function ( this : unknown , ...args : unknown [ ] ) {
151- if ( transaction ) {
152- await finishTransaction ( transaction , res ) ;
153- await flushQueue ( ) ;
154- }
155-
156- origResJson . apply ( this , args ) ;
157- } ;
136+ // If we're not on a platform that supports streaming, we're blocking res.end() until the queue is flushed.
137+ // res.json() and res.send() will implicitly call res.end(), so it is enough to wrap res.end().
158138
159139 // eslint-disable-next-line @typescript-eslint/unbound-method
160140 const origResEnd = res . end ;
@@ -223,7 +203,7 @@ export function withSentry(origHandler: NextApiHandler, parameterizedRoute?: str
223203 // moment they detect an error, so it's important to get this done before rethrowing the error. Apps not
224204 // deployed serverlessly will run into this cleanup code again in `res.end(), but the transaction will already
225205 // be finished and the queue will already be empty, so effectively it'll just no-op.)
226- if ( platformSupportsStreaming ( ) ) {
206+ if ( platformSupportsStreaming ( ) && ! origHandler . __sentry_test_doesnt_support_streaming__ ) {
227207 void finishTransaction ( transaction , res ) ;
228208 } else {
229209 await finishTransaction ( transaction , res ) ;
0 commit comments