From 1f53090be34bfbe527eaabdbb6e246e5cfd1b9a2 Mon Sep 17 00:00:00 2001 From: ignaciosantise Date: Tue, 21 May 2024 17:43:07 -0300 Subject: [PATCH 1/2] feat: added atob and btoa polyfills --- packages/react-native-compat/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/react-native-compat/index.js b/packages/react-native-compat/index.js index 9ae7d203b..45741d067 100644 --- a/packages/react-native-compat/index.js +++ b/packages/react-native-compat/index.js @@ -14,6 +14,20 @@ if (typeof Buffer === "undefined") { global.Buffer = require("buffer").Buffer; } +// Polyfill btoa +if (typeof btoa === 'undefined') { + global.btoa = function (str) { + return new Buffer(str, 'binary').toString('base64'); + }; +} + +// Polyfill atob +if (typeof atob === 'undefined') { + global.atob = function (b64Encoded) { + return new Buffer(b64Encoded, 'base64').toString('binary'); + }; +} + if (typeof global?.Linking === "undefined") { try { global.Linking = require("react-native").Linking; From 9c886026266e7efd819e1ff04a82edaa1d6f01e8 Mon Sep 17 00:00:00 2001 From: ignaciosantise Date: Tue, 21 May 2024 18:10:53 -0300 Subject: [PATCH 2/2] fix: use Buffer.alloc instead of new Buffer() --- packages/react-native-compat/index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/react-native-compat/index.js b/packages/react-native-compat/index.js index 45741d067..89e7f457e 100644 --- a/packages/react-native-compat/index.js +++ b/packages/react-native-compat/index.js @@ -15,16 +15,20 @@ if (typeof Buffer === "undefined") { } // Polyfill btoa -if (typeof btoa === 'undefined') { +if (typeof btoa === "undefined") { global.btoa = function (str) { - return new Buffer(str, 'binary').toString('base64'); + return Buffer.alloc(str.length, str, "binary").toString("base64"); }; } // Polyfill atob -if (typeof atob === 'undefined') { +if (typeof atob === "undefined") { global.atob = function (b64Encoded) { - return new Buffer(b64Encoded, 'base64').toString('binary'); + return Buffer.alloc( + Buffer.from(b64Encoded, "base64").length, + Buffer.from(b64Encoded, "base64"), + "binary", + ).toString("binary"); }; }