Skip to content

Commit 747f1df

Browse files
timfishAbhiPrasad
authored andcommitted
ref(core): Actually ensure attachments are added to the envelope (#5159)
This patch fixes a bug where attachments were previously not added to the envelope that would be sent to Sentry. Additionally, it adds a test that ensures that the attachments actually make it into the raw envelope.
1 parent 36599a3 commit 747f1df

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/core/src/baseclient.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,13 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
287287
*/
288288
public sendEvent(event: Event, hint: EventHint = {}): void {
289289
if (this._dsn) {
290-
const env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);
290+
let env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);
291291

292292
for (const attachment of hint.attachments || []) {
293-
addItemToEnvelope(env, createAttachmentEnvelopeItem(attachment, this._options.transportOptions?.textEncoder));
293+
env = addItemToEnvelope(
294+
env,
295+
createAttachmentEnvelopeItem(attachment, this._options.transportOptions?.textEncoder),
296+
);
294297
}
295298

296299
this._sendEnvelope(env);
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { parseEnvelope } from '@sentry/utils/test/testutils';
2+
import { TextEncoder } from 'util';
3+
4+
import { createTransport } from '../../src/transports/base';
5+
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';
6+
7+
describe('Attachments', () => {
8+
beforeEach(() => {
9+
TestClient.sendEventCalled = undefined;
10+
TestClient.instance = undefined;
11+
});
12+
13+
afterEach(() => {
14+
jest.clearAllMocks();
15+
});
16+
17+
test('actually end up in envelope', async () => {
18+
expect.assertions(4);
19+
20+
const options = getDefaultTestClientOptions({
21+
dsn: 'https://username@domain/123',
22+
enableSend: true,
23+
transport: () =>
24+
createTransport({ recordDroppedEvent: () => undefined, textEncoder: new TextEncoder() }, async req => {
25+
const [, items] = parseEnvelope(req.body);
26+
expect(items.length).toEqual(2);
27+
// Second envelope item should be the attachment
28+
expect(items[1][0]).toEqual({ type: 'attachment', length: 50000, filename: 'empty.bin' });
29+
expect(items[1][1]).toBeInstanceOf(Uint8Array);
30+
expect((items[1][1] as Uint8Array).length).toEqual(50_000);
31+
return {};
32+
}),
33+
});
34+
35+
const client = new TestClient(options);
36+
client.captureEvent({}, { attachments: [{ filename: 'empty.bin', data: new Uint8Array(50_000) }] });
37+
});
38+
});

0 commit comments

Comments
 (0)