@@ -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 = wrapReadableSuppressErrors ( data ) ;
248+ }
249+
245250 formData . append ( `attachment_${ attachment . name } ` , data , attachment . name ) ;
246251 }
247252
248253 return formData ;
249254 }
250255}
256+
257+ /**
258+ * When inputStream emits an error, it will be suppressed, and the stream will be closed.
259+ */
260+ function wrapReadableSuppressErrors ( inputStream : Readable ) {
261+ const safeStream = new PassThrough ( ) ;
262+
263+ inputStream . on ( 'data' , ( chunk ) => {
264+ safeStream . write ( chunk ) ;
265+ } ) ;
266+
267+ inputStream . on ( 'end' , ( ) => {
268+ safeStream . end ( ) ;
269+ } ) ;
270+
271+ inputStream . on ( 'error' , ( ) => {
272+ safeStream . end ( ) ;
273+ } ) ;
274+
275+ return safeStream ;
276+ }
0 commit comments