Skip to content

Commit fbbd28c

Browse files
committed
node: suppress stream errors when uploading
1 parent 551328b commit fbbd28c

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

packages/node/src/BacktraceNodeRequestHandler.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import FormData from 'form-data';
1111
import http, { ClientRequest, IncomingMessage } from 'http';
1212
import https from 'https';
13-
import { Readable } from 'stream';
13+
import { PassThrough, Readable } from 'stream';
1414

1515
export 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

Comments
 (0)