diff --git a/packages/snaps-utils/src/base64.test.ts b/packages/snaps-utils/src/base64.test.ts index b6134e8dde..46e4024dce 100644 --- a/packages/snaps-utils/src/base64.test.ts +++ b/packages/snaps-utils/src/base64.test.ts @@ -51,6 +51,21 @@ describe('encodeBase64', () => { ); expect(await encodeBase64(vfile)).toBe('eyJmb28iOiJiYXIifQ=='); }); + + it('does not use optimization when running in React Native', async () => { + Object.defineProperty(globalThis, 'FileReader', { + value: MockFileReader, + }); + + Object.defineProperty(globalThis, 'navigator', { + value: { product: 'ReactNative' }, + }); + + const vfile = new VirtualFile( + stringToBytes(JSON.stringify({ foo: 'bar' })), + ); + expect(await encodeBase64(vfile)).toBe('eyJmb28iOiJiYXIifQ=='); + }); }); describe('decodeBase64', () => { diff --git a/packages/snaps-utils/src/base64.ts b/packages/snaps-utils/src/base64.ts index fd0ba07d54..137d10ff45 100644 --- a/packages/snaps-utils/src/base64.ts +++ b/packages/snaps-utils/src/base64.ts @@ -11,8 +11,10 @@ import type { VirtualFile } from './virtual-file'; */ export async function encodeBase64(input: Uint8Array | VirtualFile | string) { const bytes = getBytes(input); - // In the browser, FileReader is much faster than bytesToBase64. - if ('FileReader' in globalThis) { + const isReactNative = + typeof navigator !== 'undefined' && navigator.product === 'ReactNative'; + // In the browser, FileReader is much faster than bytesToBase64. This is not supported in React Native however. + if ('FileReader' in globalThis && !isReactNative) { return await new Promise((resolve, reject) => { const reader = Object.assign(new FileReader(), { onload: () =>