Skip to content

Commit aed2ce6

Browse files
authored
fix(replay): Fix incorrect uncompressed recording size due to encoding (#6740)
For uncompressed payloads, we were potentially sending the incorrect size due to encoding issues. Instead encode to UTF8 when determining the payload size. Note that `TextEncoder` mostly overlaps with `MutationObserver` support *except* for IE11. So we will not be supporting IE11.
1 parent 4365c66 commit aed2ce6

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

packages/replay/jest.setup.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
import { getCurrentHub } from '@sentry/core';
33
import type { ReplayRecordingData, Transport } from '@sentry/types';
4+
import {TextEncoder} from 'util';
45

56
import type { ReplayContainer, Session } from './src/types';
67

78
// @ts-ignore TS error, this is replaced in prod builds bc of rollup
89
global.__SENTRY_REPLAY_VERSION__ = 'version:Test';
910

11+
(global as any).TextEncoder = TextEncoder;
12+
1013
type MockTransport = jest.MockedFunction<Transport['send']>;
1114

1215
jest.mock('./src/util/isBrowser', () => {

packages/replay/src/util/createReplayEnvelope.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export function createReplayEnvelope(
1818
[
1919
{
2020
type: 'replay_recording',
21-
length: recordingData.length,
21+
// If string then we need to encode to UTF8, otherwise will have
22+
// wrong size. TextEncoder has similar browser support to
23+
// MutationObserver, although it does not accept IE11.
24+
length:
25+
typeof recordingData === 'string' ? new TextEncoder().encode(recordingData).length : recordingData.length,
2226
},
2327
recordingData,
2428
],

packages/utils/src/envelope.ts

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export function forEachEnvelopeItem<E extends Envelope>(
5353
});
5454
}
5555

56+
/**
57+
* Encode a string to UTF8.
58+
*/
5659
function encodeUTF8(input: string, textEncoder?: TextEncoderInternal): Uint8Array {
5760
const utf8 = textEncoder || new TextEncoder();
5861
return utf8.encode(input);

0 commit comments

Comments
 (0)