Skip to content

Commit fc3303c

Browse files
committed
More simplify
1 parent 23f4b29 commit fc3303c

File tree

9 files changed

+28
-33
lines changed

9 files changed

+28
-33
lines changed

packages/core/src/baseclient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
281281
const env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);
282282

283283
for (const attachment of attachments || []) {
284-
addItemToEnvelope(env, createAttachmentEnvelopeItem(attachment));
284+
addItemToEnvelope(env, createAttachmentEnvelopeItem(attachment, this._options.transportOptions?.textEncoder));
285285
}
286286

287287
this._sendEnvelope(env);

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: (options.serializeEnvelope || serializeEnvelope)(filteredEnvelope) }).then(
72+
makeRequest({ body: serializeEnvelope(filteredEnvelope, options.textEncoder) }).then(
7373
({ headers }): void => {
7474
if (headers) {
7575
rateLimits = updateRateLimits(rateLimits, headers);

packages/node/src/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SessionFlusher } from '@sentry/hub';
33
import { Attachment, Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
44
import { basename, logger, resolvedSyncPromise } from '@sentry/utils';
55
import { existsSync, readFileSync } from 'fs';
6+
import { TextEncoder } from 'util';
67

78
import { eventFromMessage, eventFromUnknownInput } from './eventbuilder';
89
import { IS_DEBUG_BUILD } from './flags';
@@ -34,6 +35,11 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
3435
version: SDK_VERSION,
3536
};
3637

38+
options.transportOptions = {
39+
textEncoder: new TextEncoder(),
40+
...options.transportOptions,
41+
};
42+
3743
super(options);
3844
}
3945

packages/node/src/transports/http.ts

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const EVENT_ENVELOPE = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4b
6767
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem,
6868
]);
6969

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

7272
const defaultOptions = {
7373
url: TEST_SERVER_URL,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const EVENT_ENVELOPE = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4b
7070
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem,
7171
]);
7272

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

7575
const unsafeHttpsModule: HTTPModule = {
7676
request: jest

packages/types/src/envelope.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ type SessionItemHeaders = { type: 'session' };
5151
type SessionAggregatesItemHeaders = { type: 'sessions' };
5252
type ClientReportItemHeaders = { type: 'client_report' };
5353

54-
// TODO(v7): Remove the string union from `Event | string`
55-
// We have to allow this hack for now as we pre-serialize events because we support
56-
// both store and envelope endpoints.
57-
export type EventItem = BaseEnvelopeItem<EventItemHeaders, Event | string>;
58-
export type AttachmentItem = BaseEnvelopeItem<AttachmentItemHeaders, Uint8Array>;
54+
export type EventItem = BaseEnvelopeItem<EventItemHeaders, Event>;
55+
export type AttachmentItem = BaseEnvelopeItem<AttachmentItemHeaders, string | Uint8Array>;
5956
export type UserFeedbackItem = BaseEnvelopeItem<UserFeedbackItemHeaders, UserFeedback>;
6057
export type SessionItem =
6158
| BaseEnvelopeItem<SessionItemHeaders, Session>

packages/types/src/transport.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ export type TransportMakeRequestResponse = {
1414
};
1515
};
1616

17+
// Combination of global TextEncoder and Node require('util').TextEncoder
18+
interface TextEncoderInternal extends TextEncoderCommon {
19+
encode(input?: string): Uint8Array;
20+
}
21+
1722
export interface InternalBaseTransportOptions {
1823
bufferSize?: number;
1924
recordDroppedEvent: (reason: EventDropReason, dataCategory: DataCategory) => void;
20-
serializeEnvelope?: (env: Envelope) => string | Uint8Array;
25+
textEncoder?: TextEncoderInternal;
2126
}
2227

2328
export interface BaseTransportOptions extends InternalBaseTransportOptions {

packages/utils/src/envelope.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,6 @@ export function forEachEnvelopeItem<E extends Envelope>(
3434
});
3535
}
3636

37-
// Cached UTF8 string encoder
38-
let encoder: TextEncoder | undefined;
39-
40-
function getCachedEncoder(): TextEncoder {
41-
if (!encoder) {
42-
encoder = new TextEncoder();
43-
}
44-
45-
return encoder;
46-
}
47-
4837
// Combination of global TextEncoder and Node require('util').TextEncoder
4938
interface TextEncoderInternal extends TextEncoderCommon {
5039
encode(input?: string): Uint8Array;
@@ -53,11 +42,8 @@ interface TextEncoderInternal extends TextEncoderCommon {
5342
/**
5443
* Serializes an envelope.
5544
*/
56-
export function serializeEnvelope(
57-
envelope: Envelope,
58-
textEncoderOverride?: () => TextEncoderInternal,
59-
): string | Uint8Array {
60-
const textEncoder = textEncoderOverride || getCachedEncoder;
45+
export function serializeEnvelope(envelope: Envelope, textEncoder?: TextEncoderInternal): string | Uint8Array {
46+
const utf8 = textEncoder || new TextEncoder();
6147

6248
const [envHeaders, items] = envelope;
6349

@@ -69,10 +55,10 @@ export function serializeEnvelope(
6955
if (typeof next === 'string') {
7056
parts += next;
7157
} else {
72-
parts = [textEncoder().encode(parts), next];
58+
parts = [utf8.encode(parts), next];
7359
}
7460
} else {
75-
parts.push(typeof next === 'string' ? textEncoder().encode(next) : next);
61+
parts.push(typeof next === 'string' ? utf8.encode(next) : next);
7662
}
7763
}
7864

@@ -103,8 +89,12 @@ function concatBuffers(buffers: Uint8Array[]): Uint8Array {
10389
/**
10490
* Creates attachment envelope items
10591
*/
106-
export function createAttachmentEnvelopeItem(attachment: Attachment): AttachmentItem {
107-
const buffer = typeof attachment.data === 'string' ? new TextEncoder().encode(attachment.data) : attachment.data;
92+
export function createAttachmentEnvelopeItem(
93+
attachment: Attachment,
94+
textEncoder?: TextEncoderInternal,
95+
): AttachmentItem {
96+
const utf8 = textEncoder || new TextEncoder();
97+
const buffer = typeof attachment.data === 'string' ? utf8.encode(attachment.data) : attachment.data;
10898

10999
return [
110100
{

0 commit comments

Comments
 (0)