-
-
Notifications
You must be signed in to change notification settings - Fork 361
feat(core): Use react-native-quick-base64 for envelope encoding when available #6314
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
Open
alwx
wants to merge
4
commits into
main
Choose a base branch
from
alwx/feature/base64-quick-base64
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ff9001b
feat(core): Use react-native-quick-base64 for envelope encoding when โฆ
alwx 58a6dd9
docs(changelog): Match house style for base64 entry
alwx c97c4dd
docs(changelog): Move base64 entry to Unreleased section
alwx 503dd56
fix(core): Address review feedback on quick-base64 encoder
alwx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| 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. | ||
| * 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') { | ||
| // Probe the native binding so that a broken native module falls through | ||
| // to the JS encoder instead of throwing on every envelope. | ||
| quickBase64.fromByteArray(new Uint8Array([0])); | ||
| cachedEncoder = quickBase64.fromByteArray; | ||
|
antonis marked this conversation as resolved.
|
||
| 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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { _resetBase64EncoderForTesting, encodeToBase64 } from '../../src/js/utils/base64'; | ||
|
|
||
| const quickFromByteArray = jest.fn((bytes: Uint8Array) => Buffer.from(bytes).toString('base64')); | ||
|
|
||
| jest.mock( | ||
| 'react-native-quick-base64', | ||
| () => ({ | ||
| __esModule: true, | ||
| fromByteArray: quickFromByteArray, | ||
| }), | ||
| { virtual: true }, | ||
| ); | ||
|
|
||
| describe('encodeToBase64', () => { | ||
| beforeEach(() => { | ||
| _resetBase64EncoderForTesting(); | ||
| quickFromByteArray.mockClear(); | ||
| jest.resetModules(); | ||
| }); | ||
|
|
||
| test('uses react-native-quick-base64 when available', () => { | ||
| // "sentry" => "c2VudHJ5" | ||
| const result = encodeToBase64(new Uint8Array([0x73, 0x65, 0x6e, 0x74, 0x72, 0x79])); | ||
| expect(result).toBe('c2VudHJ5'); | ||
| // Probe call during resolution + the actual encode call. | ||
| expect(quickFromByteArray).toHaveBeenCalledTimes(2); | ||
| expect(quickFromByteArray).toHaveBeenLastCalledWith(new Uint8Array([0x73, 0x65, 0x6e, 0x74, 0x72, 0x79])); | ||
| }); | ||
|
|
||
| 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'); | ||
| }); | ||
| }); | ||
|
|
||
| test('falls back to the JS encoder when the native binding throws on probe', () => { | ||
| jest.isolateModules(() => { | ||
| const brokenFromByteArray = jest.fn(() => { | ||
| throw new Error('native module not linked'); | ||
| }); | ||
| jest.doMock('react-native-quick-base64', () => ({ __esModule: true, fromByteArray: brokenFromByteArray }), { | ||
| virtual: true, | ||
| }); | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const { | ||
| encodeToBase64: isolatedEncode, | ||
| _resetBase64EncoderForTesting: isolatedReset, | ||
| } = require('../../src/js/utils/base64'); | ||
| isolatedReset(); | ||
| expect(isolatedEncode(new Uint8Array([0x73, 0x65, 0x6e, 0x74, 0x72, 0x79]))).toBe('c2VudHJ5'); | ||
| // Probe was attempted exactly once; the broken binding is not used for the real encode. | ||
| expect(brokenFromByteArray).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we do not have any peer dependencies other
react-nativeandexpo, I am still skeptical on if we should add a dependency for this improvement.Since
RNSentryis a TurboModule and could accept the envelope bytes as binary across JSI on the new architecture what do you think of pursuing our own solution without a new dependency?Looping @lucas-zimerman for more feedback on this ๐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem here is that it's a bit of reinventing the wheel.
Of course, Claude can do it easily but even the first not really working stage of it is already 1000+ lines of code (#6312) and I'm a bit sceptical about the maintenance load and the fact that a verified, well-tested and well-maintained solution which is react-native-quick-base64 works better than either we or Claude can do.
In addition, this is an optional peer dependency, people could still use the old approach if they don't want it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally agree with that. My concern is that by design we have avoided dependencies (ref1, ref2) even for core functionality.