@@ -130,31 +130,11 @@ export function withSentry(origHandler: NextApiHandler, parameterizedRoute?: str
130
130
) ;
131
131
currentScope . setSpan ( transaction ) ;
132
132
133
- if ( platformSupportsStreaming ( ) ) {
133
+ if ( platformSupportsStreaming ( ) && ! origHandler . __sentry_test_doesnt_support_streaming__ ) {
134
134
autoEndTransactionOnResponseEnd ( transaction , res ) ;
135
135
} 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().
158
138
159
139
// eslint-disable-next-line @typescript-eslint/unbound-method
160
140
const origResEnd = res . end ;
@@ -223,7 +203,7 @@ export function withSentry(origHandler: NextApiHandler, parameterizedRoute?: str
223
203
// moment they detect an error, so it's important to get this done before rethrowing the error. Apps not
224
204
// deployed serverlessly will run into this cleanup code again in `res.end(), but the transaction will already
225
205
// 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__ ) {
227
207
void finishTransaction ( transaction , res ) ;
228
208
} else {
229
209
await finishTransaction ( transaction , res ) ;
0 commit comments