Skip to content

Commit 23f4b29

Browse files
committed
Much improved
1 parent 93960c6 commit 23f4b29

File tree

20 files changed

+137
-123
lines changed

20 files changed

+137
-123
lines changed

packages/browser/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ const baseConfig = require('../../jest/jest.config.js');
22

33
module.exports = {
44
...baseConfig,
5-
testEnvironment: 'jsdom',
5+
testEnvironment: './jest.env.js',
66
testMatch: ['<rootDir>/test/unit/**/*.test.ts'],
77
};

packages/browser/jest.env.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const Environment = require('jest-environment-jsdom');
2+
3+
// Looks like jsdom does not support global TextEncoder/TextDecoder
4+
// https://github.com/jsdom/jsdom/issues/2524
5+
6+
module.exports = class CustomTestEnvironment extends Environment {
7+
async setup() {
8+
await super.setup();
9+
if (typeof this.global.TextEncoder === 'undefined') {
10+
const { TextEncoder, TextDecoder } = require('util');
11+
this.global.TextEncoder = TextEncoder;
12+
this.global.TextDecoder = TextDecoder;
13+
}
14+
}
15+
};

packages/browser/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, Scope, SDK_VERSION } from '@sentry/core';
2-
import { AttachmentItem, ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
2+
import { Attachment, ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
33
import { createClientReportEnvelope, dsnToString, getGlobalObject, logger, serializeEnvelope } from '@sentry/utils';
44

55
import { eventFromException, eventFromMessage } from './eventbuilder';
@@ -103,7 +103,7 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
103103
/**
104104
* @inheritDoc
105105
*/
106-
protected _sendEvent(event: Event, attachments: AttachmentItem[]): void {
106+
protected _sendEvent(event: Event, attachments: Attachment[]): void {
107107
const integration = this.getIntegration(Breadcrumbs);
108108
if (integration) {
109109
integration.addSentryBreadcrumb(event);

packages/core/src/baseclient.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */
22
import { Scope, Session } from '@sentry/hub';
33
import {
4-
AttachmentItem,
4+
Attachment,
55
Client,
66
ClientOptions,
77
DataCategory,
@@ -19,6 +19,7 @@ import {
1919
Transport,
2020
} from '@sentry/types';
2121
import {
22+
addItemToEnvelope,
2223
checkOrSetAlreadyCaught,
2324
createAttachmentEnvelopeItem,
2425
dateTimestampInSeconds,
@@ -275,9 +276,14 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
275276
/**
276277
* @inheritDoc
277278
*/
278-
public sendEvent(event: Event, attachments?: AttachmentItem[]): void {
279+
public sendEvent(event: Event, attachments?: Attachment[]): void {
279280
if (this._dsn) {
280-
const env = createEventEnvelope(event, this._dsn, attachments, this._options._metadata, this._options.tunnel);
281+
const env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);
282+
283+
for (const attachment of attachments || []) {
284+
addItemToEnvelope(env, createAttachmentEnvelopeItem(attachment));
285+
}
286+
281287
this._sendEnvelope(env);
282288
}
283289
}
@@ -543,7 +549,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
543549
* @param event The Sentry event to send
544550
*/
545551
// TODO(v7): refactor: get rid of method?
546-
protected _sendEvent(event: Event, attachments?: AttachmentItem[]): void {
552+
protected _sendEvent(event: Event, attachments?: Attachment[]): void {
547553
this.sendEvent(event, attachments);
548554
}
549555

@@ -662,10 +668,10 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
662668
}
663669

664670
/**
665-
* Loads attachment items from scope
671+
* Loads attachments from scope
666672
*/
667-
protected _attachmentsFromScope(scope: Scope | undefined): AttachmentItem[] {
668-
return scope?.getAttachments()?.map(a => createAttachmentEnvelopeItem(a)) || [];
673+
protected _attachmentsFromScope(scope: Scope | undefined): Attachment[] {
674+
return scope?.getAttachments() || [];
669675
}
670676

671677
/**

packages/core/src/envelope.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
AttachmentItem,
32
DsnComponents,
43
Event,
54
EventEnvelope,
@@ -64,7 +63,6 @@ export function createSessionEnvelope(
6463
export function createEventEnvelope(
6564
event: Event,
6665
dsn: DsnComponents,
67-
attachments: AttachmentItem[] = [],
6866
metadata?: SdkMetadata,
6967
tunnel?: string,
7068
): EventEnvelope {
@@ -116,5 +114,5 @@ export function createEventEnvelope(
116114
},
117115
event,
118116
];
119-
return createEnvelope<EventEnvelope>(envelopeHeaders, [eventItem, ...attachments]);
117+
return createEnvelope<EventEnvelope>(envelopeHeaders, [eventItem]);
120118
}

packages/core/src/transports/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function createTransport(
6969
};
7070

7171
const requestTask = (): PromiseLike<void> =>
72-
makeRequest({ body: serializeEnvelope(filteredEnvelope) }).then(
72+
makeRequest({ body: (options.serializeEnvelope || serializeEnvelope)(filteredEnvelope) }).then(
7373
({ headers }): void => {
7474
if (headers) {
7575
rateLimits = updateRateLimits(rateLimits, headers);

packages/hub/src/scope.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable max-lines */
22
import {
33
Attachment,
4-
AttachmentOptions,
54
Breadcrumb,
65
CaptureContext,
76
Context,
@@ -408,8 +407,8 @@ export class Scope implements ScopeInterface {
408407
/**
409408
* @inheritDoc
410409
*/
411-
public addAttachment(pathOrData: string | Uint8Array, options?: AttachmentOptions): this {
412-
this._attachments.push([pathOrData, options]);
410+
public addAttachment(attachment: Attachment): this {
411+
this._attachments.push(attachment);
413412
return this;
414413
}
415414

packages/node/src/client.ts

Lines changed: 7 additions & 10 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';
3-
import { AttachmentItem, Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
4-
import { basename, createAttachmentEnvelopeItem, logger, resolvedSyncPromise } from '@sentry/utils';
3+
import { Attachment, Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
4+
import { basename, logger, resolvedSyncPromise } from '@sentry/utils';
55
import { existsSync, readFileSync } from 'fs';
66

77
import { eventFromMessage, eventFromUnknownInput } from './eventbuilder';
@@ -155,18 +155,15 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
155155
/**
156156
* @inheritDoc
157157
*/
158-
protected _attachmentsFromScope(scope: Scope | undefined): AttachmentItem[] {
158+
protected _attachmentsFromScope(scope: Scope | undefined): Attachment[] {
159159
return (
160160
scope?.getAttachments()?.map(attachment => {
161-
let [pathOrData, options] = attachment;
162-
163-
if (typeof pathOrData === 'string' && existsSync(pathOrData)) {
164-
options = options || {};
165-
options.filename = basename(pathOrData);
166-
pathOrData = readFileSync(pathOrData);
161+
if (attachment.path && existsSync(attachment.path)) {
162+
attachment.filename = basename(attachment.path);
163+
attachment.data = readFileSync(attachment.path);
167164
}
168165

169-
return createAttachmentEnvelopeItem([pathOrData, options]);
166+
return attachment;
170167
}) || []
171168
);
172169
}

packages/node/src/transports/http.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
TransportRequest,
77
TransportRequestExecutor,
88
} from '@sentry/types';
9+
import { serializeEnvelope } from '@sentry/utils';
910
import * as http from 'http';
1011
import * as https from 'https';
1112
import { Readable, Writable } from 'stream';
@@ -45,6 +46,8 @@ function streamFromBody(body: Uint8Array | string): Readable {
4546
* Creates a Transport that uses native the native 'http' and 'https' modules to send events to Sentry.
4647
*/
4748
export function makeNodeTransport(options: NodeTransportOptions): Transport {
49+
options.serializeEnvelope = s => serializeEnvelope(s, () => new TextEncoder());
50+
4851
const urlSegments = new URL(options.url);
4952
const isHttps = urlSegments.protocol === 'https:';
5053

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 { AttachmentItem, Integration } from '@sentry/types';
3+
import { Attachment, 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, AttachmentItem[]?]>;
82+
let s: jest.SpyInstance<void, [Event, Attachment[]?]>;
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, AttachmentItem[]?]>;
113+
let s: jest.SpyInstance<void, [Event, Attachment[]?]>;
114114

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

packages/node/test/transports/http.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createTransport } from '@sentry/core';
22
import { EventEnvelope, EventItem } from '@sentry/types';
33
import { createEnvelope, serializeEnvelope } from '@sentry/utils';
44
import * as http from 'http';
5+
import { TextEncoder } from 'util';
56

67
import { makeNodeTransport } from '../../src/transports';
78

@@ -66,7 +67,7 @@ const EVENT_ENVELOPE = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4b
6667
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem,
6768
]);
6869

69-
const SERIALIZED_EVENT_ENVELOPE = serializeEnvelope(EVENT_ENVELOPE);
70+
const SERIALIZED_EVENT_ENVELOPE = serializeEnvelope(EVENT_ENVELOPE, () => new TextEncoder());
7071

7172
const defaultOptions = {
7273
url: TEST_SERVER_URL,

packages/node/test/transports/https.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EventEnvelope, EventItem } from '@sentry/types';
33
import { createEnvelope, serializeEnvelope } from '@sentry/utils';
44
import * as http from 'http';
55
import * as https from 'https';
6+
import { TextEncoder } from 'util';
67

78
import { makeNodeTransport } from '../../src/transports';
89
import { HTTPModule, HTTPModuleRequestIncomingMessage } from '../../src/transports/http-module';
@@ -69,7 +70,7 @@ const EVENT_ENVELOPE = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4b
6970
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem,
7071
]);
7172

72-
const SERIALIZED_EVENT_ENVELOPE = serializeEnvelope(EVENT_ENVELOPE);
73+
const SERIALIZED_EVENT_ENVELOPE = serializeEnvelope(EVENT_ENVELOPE, () => new TextEncoder());
7374

7475
const unsafeHttpsModule: HTTPModule = {
7576
request: jest

packages/types/src/attachment.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export interface AttachmentOptions {
1+
export interface Attachment {
2+
path?: string;
3+
data?: string | Uint8Array;
24
filename?: string;
35
contentType?: string;
46
attachmentType?: string;
57
}
6-
7-
export type Attachment = [string | Uint8Array, AttachmentOptions | undefined];

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type { Attachment, AttachmentOptions } from './attachment';
1+
export type { Attachment } from './attachment';
22
export type { Breadcrumb, BreadcrumbHint } from './breadcrumb';
33
export type { Client } from './client';
44
export type { ClientReport, Outcome, EventDropReason } from './clientreport';

packages/types/src/scope.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Attachment, AttachmentOptions } from './attachment';
1+
import { Attachment } from './attachment';
22
import { Breadcrumb } from './breadcrumb';
33
import { Context, Contexts } from './context';
44
import { EventProcessor } from './eventprocessor';
@@ -162,10 +162,9 @@ export interface Scope {
162162

163163
/**
164164
* Adds an attachment to the scope
165-
* @param pathOrData A Uint8Array containing the attachment bytes
166-
* @param options Attachment options
165+
* @param attachment Attachment options
167166
*/
168-
addAttachment(pathOrData: string | Uint8Array, options?: AttachmentOptions): this;
167+
addAttachment(attachment: Attachment): this;
169168

170169
/**
171170
* Returns an array of attachments on the scope

packages/types/src/transport.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type TransportMakeRequestResponse = {
1717
export interface InternalBaseTransportOptions {
1818
bufferSize?: number;
1919
recordDroppedEvent: (reason: EventDropReason, dataCategory: DataCategory) => void;
20+
serializeEnvelope?: (env: Envelope) => string | Uint8Array;
2021
}
2122

2223
export interface BaseTransportOptions extends InternalBaseTransportOptions {

0 commit comments

Comments
 (0)