@@ -10,7 +10,7 @@ import {
1010import FormData from 'form-data' ;
1111import http , { ClientRequest , IncomingMessage } from 'http' ;
1212import https from 'https' ;
13- import { Readable } from 'stream' ;
13+ import { PassThrough , Readable } from 'stream' ;
1414
1515export interface BacktraceNodeRequestHandlerOptions {
1616 readonly timeout ?: number ;
@@ -238,13 +238,39 @@ export class BacktraceNodeRequestHandler implements BacktraceRequestHandler {
238238 }
239239
240240 for ( const attachment of attachments ) {
241- const data = attachment . get ( ) ;
241+ let data = attachment . get ( ) ;
242242 if ( ! data ) {
243243 continue ;
244244 }
245+
246+ if ( data instanceof Readable ) {
247+ data = this . wrapReadableSuppressErrors ( data ) ;
248+ }
249+
245250 formData . append ( `attachment_${ attachment . name } ` , data , attachment . name ) ;
246251 }
247252
248253 return formData ;
249254 }
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+ }
250276}
0 commit comments