Skip to content

Commit 7388581

Browse files
committed
Fix test spy and add implementation for creating attachment item
1 parent 0dd456f commit 7388581

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

packages/core/src/baseclient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import {
1616
Transport,
1717
} from '@sentry/types';
1818
import {
19-
attachmentItemFromAttachment,
2019
checkOrSetAlreadyCaught,
20+
createAttachmentEnvelopeItem,
2121
dateTimestampInSeconds,
2222
isPlainObject,
2323
isPrimitive,
@@ -659,7 +659,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
659659

660660
/** */
661661
protected _getAttachments(scope: Scope | undefined): AttachmentItem[] {
662-
return (scope?.getAttachments() || []).map(a => attachmentItemFromAttachment(a));
662+
return (scope?.getAttachments() || []).map(a => createAttachmentEnvelopeItem(a));
663663
}
664664

665665
/**

packages/node/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
22
import { SessionFlusher } from '@sentry/hub';
33
import { AttachmentItem, Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
4-
import { attachmentItemFromAttachment, logger, resolvedSyncPromise } from '@sentry/utils';
4+
import { createAttachmentEnvelopeItem, logger, resolvedSyncPromise } from '@sentry/utils';
55

66
import { eventFromMessage, eventFromUnknownInput } from './eventbuilder';
77
import { IS_DEBUG_BUILD } from './flags';
@@ -156,6 +156,6 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
156156
*/
157157
protected _getAttachments(scope: Scope | undefined): AttachmentItem[] {
158158
// TODO: load any attachments from paths...
159-
return (scope?.getAttachments() || []).map(a => attachmentItemFromAttachment(a));
159+
return (scope?.getAttachments() || []).map(a => createAttachmentEnvelopeItem(a));
160160
}
161161
}

packages/node/test/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { initAndBind, SDK_VERSION } from '@sentry/core';
22
import { getMainCarrier } from '@sentry/hub';
3-
import { Integration } from '@sentry/types';
3+
import { AttachmentItem, Integration } from '@sentry/types';
44
import { createStackParser } from '@sentry/utils';
55
import * as domain from 'domain';
66

@@ -79,7 +79,7 @@ describe('SentryNode', () => {
7979
});
8080

8181
describe('breadcrumbs', () => {
82-
let s: jest.SpyInstance<void, Event[]>;
82+
let s: jest.SpyInstance<void, [Event, AttachmentItem[]?]>;
8383

8484
beforeEach(() => {
8585
s = jest.spyOn(NodeClient.prototype, 'sendEvent').mockImplementation(async () => Promise.resolve({ code: 200 }));
@@ -110,7 +110,7 @@ describe('SentryNode', () => {
110110
});
111111

112112
describe('capture', () => {
113-
let s: jest.SpyInstance<void, Event[]>;
113+
let s: jest.SpyInstance<void, [Event, AttachmentItem[]?]>;
114114

115115
beforeEach(() => {
116116
s = jest.spyOn(NodeClient.prototype, 'sendEvent').mockImplementation(async () => Promise.resolve({ code: 200 }));

packages/utils/src/attachment.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import { Attachment, AttachmentItem } from '@sentry/types';
22

33
/** */
4-
export function attachmentItemFromAttachment(_attachment: Attachment): AttachmentItem {
5-
throw new Error('Not implemented');
4+
export function createAttachmentEnvelopeItem(attachment: Attachment): AttachmentItem {
5+
const [pathOrData, options] = attachment;
6+
7+
const buffer = typeof pathOrData === 'string' ? new TextEncoder().encode(pathOrData) : pathOrData;
8+
9+
return [
10+
{
11+
type: 'attachment',
12+
length: buffer.length,
13+
filename: options?.filename || 'No filename',
14+
content_type: options?.contentType,
15+
attachment_type: options?.attachmentType,
16+
},
17+
buffer,
18+
];
619
}

0 commit comments

Comments
 (0)