@@ -10,7 +10,7 @@ import {
10
10
import FormData from 'form-data' ;
11
11
import http , { ClientRequest , IncomingMessage } from 'http' ;
12
12
import https from 'https' ;
13
- import { Readable } from 'stream' ;
13
+ import { PassThrough , Readable } from 'stream' ;
14
14
15
15
export interface BacktraceNodeRequestHandlerOptions {
16
16
readonly timeout ?: number ;
@@ -238,13 +238,39 @@ export class BacktraceNodeRequestHandler implements BacktraceRequestHandler {
238
238
}
239
239
240
240
for ( const attachment of attachments ) {
241
- const data = attachment . get ( ) ;
241
+ let data = attachment . get ( ) ;
242
242
if ( ! data ) {
243
243
continue ;
244
244
}
245
+
246
+ if ( data instanceof Readable ) {
247
+ data = this . wrapReadableSuppressErrors ( data ) ;
248
+ }
249
+
245
250
formData . append ( `attachment_${ attachment . name } ` , data , attachment . name ) ;
246
251
}
247
252
248
253
return formData ;
249
254
}
255
+
256
+ /**
257
+ * When inputStream emits an error, it will be suppressed, and the stream will be closed.
258
+ */
259
+ private wrapReadableSuppressErrors ( inputStream : Readable ) {
260
+ const safeStream = new PassThrough ( ) ;
261
+
262
+ inputStream . on ( 'data' , ( chunk ) => {
263
+ safeStream . write ( chunk ) ;
264
+ } ) ;
265
+
266
+ inputStream . on ( 'end' , ( ) => {
267
+ safeStream . end ( ) ;
268
+ } ) ;
269
+
270
+ inputStream . on ( 'error' , ( ) => {
271
+ safeStream . end ( ) ;
272
+ } ) ;
273
+
274
+ return safeStream ;
275
+ }
250
276
}
0 commit comments