Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(replay): Fix incorrect uncompressed recording size due to encoding #6740

Merged
merged 8 commits into from
Jan 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/replay/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { getCurrentHub } from '@sentry/core';
import type { ReplayRecordingData, Transport } from '@sentry/types';
import {TextEncoder} from 'util';

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

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

(global as any).TextEncoder = TextEncoder;

type MockTransport = jest.MockedFunction<Transport['send']>;

jest.mock('./src/util/isBrowser', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/replay/src/util/createReplayEnvelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export function createReplayEnvelope(
[
{
type: 'replay_recording',
length: recordingData.length,
// If string then we need to encode to UTF8, otherwise will have
// wrong size. TextEncoder has similar browser support to
// MutationObserver, although it does not accept IE11.
length:
typeof recordingData === 'string' ? new TextEncoder().encode(recordingData).length : recordingData.length,
},
recordingData,
],
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export function forEachEnvelopeItem<E extends Envelope>(
});
}

/**
* Encode a string to UTF8.
*/
function encodeUTF8(input: string, textEncoder?: TextEncoderInternal): Uint8Array {
const utf8 = textEncoder || new TextEncoder();
return utf8.encode(input);
Expand Down