Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions packages/utils/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
import { dsnToString } from './dsn';
import { normalize } from './normalize';
import { dropUndefinedKeys } from './object';
import { GLOBAL_OBJ } from './worldwide';

/**
* Creates an envelope.
Expand Down Expand Up @@ -71,14 +72,18 @@ export function envelopeContainsItemType(envelope: Envelope, types: EnvelopeItem
* Encode a string to UTF8 array.
*/
function encodeUTF8(input: string): Uint8Array {
return new TextEncoder().encode(input);
return GLOBAL_OBJ.__SENTRY__ && GLOBAL_OBJ.__SENTRY__.encodePolyfill
? GLOBAL_OBJ.__SENTRY__.encodePolyfill(input)
: new TextEncoder().encode(input);
}

/**
* Decode a UTF8 array to string.
*/
function decodeUTF8(input: Uint8Array): string {
return new TextDecoder().decode(input);
return GLOBAL_OBJ.__SENTRY__ && GLOBAL_OBJ.__SENTRY__.decodePolyfill
? GLOBAL_OBJ.__SENTRY__.decodePolyfill(input)
: new TextDecoder().decode(input);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export interface InternalGlobal {
defaultCurrentScope: Scope | undefined;
defaultIsolationScope: Scope | undefined;
globalMetricsAggregators: WeakMap<Client, MetricsAggregator> | undefined;
/** Overwrites TextEncoder used in `@sentry/utils`, need for `[email protected]` and older */
encodePolyfill?: (input: string) => Uint8Array;
/** Overwrites TextDecoder used in `@sentry/utils`, need for `[email protected]` and older */
decodePolyfill?: (input: Uint8Array) => string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave a comment about what these should be used for!

};
/**
* Raw module metadata that is injected by bundler plugins.
Expand Down
34 changes: 31 additions & 3 deletions packages/utils/test/envelope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
parseEnvelope,
serializeEnvelope,
} from '../src/envelope';
import type { InternalGlobal } from '../src/worldwide';
import { GLOBAL_OBJ } from '../src/worldwide';

describe('envelope', () => {
describe('createEnvelope()', () => {
Expand All @@ -23,6 +25,10 @@ describe('envelope', () => {
});

describe('serializeEnvelope and parseEnvelope', () => {
afterEach(() => {
delete (GLOBAL_OBJ as Partial<InternalGlobal>).__SENTRY__;
});

it('serializes an envelope', () => {
const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, []);
const serializedEnvelope = serializeEnvelope(env);
Expand All @@ -32,7 +38,29 @@ describe('envelope', () => {
expect(headers).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
});

it('serializes an envelope with attachments', () => {
test.each([
{
name: 'with TextEncoder/Decoder polyfill',
before: () => {
GLOBAL_OBJ.__SENTRY__ = {} as InternalGlobal['__SENTRY__'];
GLOBAL_OBJ.__SENTRY__.encodePolyfill = jest.fn<Uint8Array, [string]>((input: string) =>
new TextEncoder().encode(input),
);
GLOBAL_OBJ.__SENTRY__.decodePolyfill = jest.fn<string, [Uint8Array]>((input: Uint8Array) =>
new TextDecoder().decode(input),
);
},
after: () => {
expect(GLOBAL_OBJ.__SENTRY__.encodePolyfill).toHaveBeenCalled();
expect(GLOBAL_OBJ.__SENTRY__.decodePolyfill).toHaveBeenCalled();
},
},
{
name: 'with default TextEncoder/Decoder',
},
])('serializes an envelope with attachments $name', ({ before, after }) => {
before?.();

const items: EventEnvelope[1] = [
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
[{ type: 'attachment', filename: 'bar.txt', length: 6 }, Uint8Array.from([1, 2, 3, 4, 5, 6])],
Expand All @@ -44,8 +72,6 @@ describe('envelope', () => {
items,
);

expect.assertions(6);

const serializedEnvelope = serializeEnvelope(env);
expect(serializedEnvelope).toBeInstanceOf(Uint8Array);

Expand All @@ -61,6 +87,8 @@ describe('envelope', () => {
{ type: 'attachment', filename: 'foo.txt', length: 6 },
Uint8Array.from([7, 8, 9, 10, 11, 12]),
]);

after?.();
});

it("doesn't throw when being passed a an envelope that contains a circular item payload", () => {
Expand Down