Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/node/src/BacktraceNodeRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import FormData from 'form-data';
import http, { ClientRequest, IncomingMessage } from 'http';
import https from 'https';
import { Readable } from 'stream';
import { PassThrough, Readable } from 'stream';

export interface BacktraceNodeRequestHandlerOptions {
readonly timeout?: number;
Expand Down Expand Up @@ -238,13 +238,39 @@ export class BacktraceNodeRequestHandler implements BacktraceRequestHandler {
}

for (const attachment of attachments) {
const data = attachment.get();
let data = attachment.get();
if (!data) {
continue;
}

if (data instanceof Readable) {
data = this.wrapReadableSuppressErrors(data);
}

formData.append(`attachment_${attachment.name}`, data, attachment.name);
}

return formData;
}

/**
* When inputStream emits an error, it will be suppressed, and the stream will be closed.
*/
private wrapReadableSuppressErrors(inputStream: Readable) {
const safeStream = new PassThrough();

inputStream.on('data', (chunk) => {
safeStream.write(chunk);
});

inputStream.on('end', () => {
safeStream.end();
});

inputStream.on('error', () => {
safeStream.end();
});

return safeStream;
}
}