Skip to content
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- Add `nativeStackAndroid` support to `NativeLinkedErrors`, capturing the JVM stack trace of rejected native module promises as a linked exception ([#6278](https://github.com/getsentry/sentry-react-native/pull/6278))
- Record XHR request/response headers and (optionally) bodies in Mobile Session Replay. Opt in via `mobileReplayIntegration` with `networkDetailAllowUrls` to capture headers; set `networkCaptureBodies: true` to also capture bodies. Other options: `networkDetailDenyUrls`, `networkRequestHeaders`, `networkResponseHeaders`. Authorization-like headers are always stripped, bodies are capped at ~150 KB. Covers XHR-based clients like `axios`; fetch will follow. See [Network Details](https://docs.sentry.io/platforms/react-native/session-replay/#network-details) for details. ([#6288](https://github.com/getsentry/sentry-react-native/pull/6288))
- Use the optional `react-native-quick-base64` peer dependency for envelope base64 encoding when installed, to improve `captureEnvelope` performance. Falls back to the bundled JS encoder if the package is absent. ([#6314](https://github.com/getsentry/sentry-react-native/pull/6314))
- Warn during dev builds when multiple versions of Sentry JS SDK are detected ([#6269](https://github.com/getsentry/sentry-react-native/pull/6269))

### Fixes
Expand Down
6 changes: 5 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"peerDependencies": {
"expo": ">=49.0.0",
"react": ">=17.0.0",
"react-native": ">=0.65.0"
"react-native": ">=0.65.0",
"react-native-quick-base64": ">=3.0.0"
},
"dependencies": {
"@sentry/babel-plugin-component-annotate": "5.3.0",
Expand Down Expand Up @@ -130,6 +131,9 @@
"peerDependenciesMeta": {
"expo": {
"optional": true
},
"react-native-quick-base64": {
"optional": true
}
}
}
56 changes: 56 additions & 0 deletions packages/core/src/js/utils/base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { debug } from '@sentry/core';

import { base64StringFromByteArray } from '../vendor';

type FromByteArray = (bytes: Uint8Array, urlSafe?: boolean) => string;

let cachedEncoder: FromByteArray | null = null;
let resolved = false;

/**
* Resolves the base64 encoder once. If the optional peer dependency
* `react-native-quick-base64` is installed, its native JSI encoder is used
* (~10x faster than the pure-JS fallback). Otherwise the bundled JS encoder
* from `vendor/base64-js` is used.
*
* The resolution is cached so the require cost is paid at most once.
*/
function resolveEncoder(): FromByteArray {
if (resolved) {
return cachedEncoder ?? base64StringFromByteArray;
}
resolved = true;

try {
// Optional peer dependency โ€” only loaded if the consumer installed it.
// eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-extraneous-dependencies
const quickBase64 = require('react-native-quick-base64') as { fromByteArray?: FromByteArray };
if (quickBase64 && typeof quickBase64.fromByteArray === 'function') {
cachedEncoder = quickBase64.fromByteArray;
debug.log('Using react-native-quick-base64 for envelope encoding.');
return cachedEncoder;
}
} catch (_e) {
// Not installed โ€” fall through to JS encoder.
}

cachedEncoder = base64StringFromByteArray;
return cachedEncoder;
}

/**
* Encode a byte array to a base64 string. Prefers the native
* `react-native-quick-base64` encoder when available, otherwise uses the
* bundled JS implementation.
*/
export function encodeToBase64(input: Uint8Array): string {
return resolveEncoder()(input);
}

/**
* @internal Test helper. Resets the cached encoder so the next call re-resolves.
*/
export function _resetBase64EncoderForTesting(): void {
cachedEncoder = null;
resolved = false;
}
4 changes: 2 additions & 2 deletions packages/core/src/js/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import type { MobileReplayOptions } from './replay/mobilereplay';
import type { RequiredKeysUser } from './user';

import { isHardCrash } from './misc';
import { encodeToBase64 } from './utils/base64';
import { encodeUTF8 } from './utils/encode';
import { isTurboModuleEnabled } from './utils/environment';
import { convertToNormalizedObject } from './utils/normalize';
import { ReactNativeLibraries } from './utils/rnlibraries';
import { base64StringFromByteArray } from './vendor';
import { SDK_VERSION } from './version';

/**
Expand Down Expand Up @@ -231,7 +231,7 @@ export const NATIVE: SentryNativeWrapper = {
envelopeBytes = newBytes;
}

await RNSentry.captureEnvelope(base64StringFromByteArray(envelopeBytes), { hardCrashed });
await RNSentry.captureEnvelope(encodeToBase64(envelopeBytes), { hardCrashed });
},

/**
Expand Down
43 changes: 43 additions & 0 deletions packages/core/test/utils/base64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { _resetBase64EncoderForTesting, encodeToBase64 } from '../../src/js/utils/base64';

jest.mock(
'react-native-quick-base64',
() => ({
__esModule: true,
fromByteArray: jest.fn((bytes: Uint8Array) => `quick:${bytes.length}`),
}),
{ virtual: true },
);

describe('encodeToBase64', () => {
beforeEach(() => {
_resetBase64EncoderForTesting();
jest.resetModules();
});

test('uses react-native-quick-base64 when available', () => {
// The module mock above provides a fake `fromByteArray` returning a sentinel.
const result = encodeToBase64(new Uint8Array([0x73, 0x65, 0x6e, 0x74, 0x72, 0x79]));
expect(result).toBe('quick:6');
});

test('falls back to the JS encoder when react-native-quick-base64 is not installed', () => {
jest.isolateModules(() => {
jest.doMock(
'react-native-quick-base64',
() => {
throw new Error('Cannot find module');
},
{ virtual: true },
);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {
encodeToBase64: isolatedEncode,
_resetBase64EncoderForTesting: isolatedReset,
} = require('../../src/js/utils/base64');
isolatedReset();
// "sentry" => "c2VudHJ5"
expect(isolatedEncode(new Uint8Array([0x73, 0x65, 0x6e, 0x74, 0x72, 0x79]))).toBe('c2VudHJ5');
});
});
});
3 changes: 3 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10931,9 +10931,12 @@ __metadata:
expo: ">=49.0.0"
react: ">=17.0.0"
react-native: ">=0.65.0"
react-native-quick-base64: ">=3.0.0"
peerDependenciesMeta:
expo:
optional: true
react-native-quick-base64:
optional: true
bin:
sentry-eas-build-on-complete: scripts/eas-build-hook.js
sentry-eas-build-on-error: scripts/eas-build-hook.js
Expand Down
Loading