Skip to content

Commit 21113a0

Browse files
committed
fix(core): Address review nits on btoa base64 encoder
- Soften the JSDoc claim about JSC: not every JSC build exposes `btoa`, only some do. - Add a test covering the JS-encoder fallback path by stashing `globalThis.btoa` so the module sees no native implementation.
1 parent 03f949e commit 21113a0

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

packages/core/src/js/utils/base64.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const CHUNK_SIZE = 0x8000;
66

77
/**
88
* Encodes a byte array to base64. Uses the runtime's native `btoa` when
9-
* available (Hermes and modern JSC both expose it), which is significantly
10-
* faster than the pure-JS encoder for the envelope payloads going through
11-
* `RNSentry.captureEnvelope`. Falls back to the bundled JS encoder when
12-
* `btoa` is missing (e.g. older JS engines).
9+
* available Hermes exposes it natively, and some JSC builds do too — which
10+
* is significantly faster than the pure-JS encoder for the envelope payloads
11+
* going through `RNSentry.captureEnvelope`. Falls back to the bundled JS
12+
* encoder when `btoa` is missing.
1313
*/
1414
export function encodeToBase64(bytes: Uint8Array): string {
1515
const nativeBtoa = (globalThis as { btoa?: (input: string) => string }).btoa;

packages/core/test/utils/base64.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,17 @@ describe('encodeToBase64', () => {
1515
}
1616
expect(encodeToBase64(bytes)).toBe(Buffer.from(bytes).toString('base64'));
1717
});
18+
19+
test('falls back to the JS encoder when `btoa` is unavailable', () => {
20+
const originalBtoa = (globalThis as { btoa?: unknown }).btoa;
21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22+
delete (globalThis as any).btoa;
23+
try {
24+
// "sentry" => "c2VudHJ5"
25+
expect(encodeToBase64(new Uint8Array([0x73, 0x65, 0x6e, 0x74, 0x72, 0x79]))).toBe('c2VudHJ5');
26+
} finally {
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28+
(globalThis as any).btoa = originalBtoa;
29+
}
30+
});
1831
});

0 commit comments

Comments
 (0)