আমি দেখেছি যে উপরের উত্তরগুলি থেকে শিমগুলি কাজ করেছে, তারা ডেস্কটপ ব্রাউজারগুলির btoa()এবং এর প্রয়োগগুলির আচরণের সাথে মেলে না atob():
const btoa = function(str){ return Buffer.from(str).toString('base64'); }
// returns "4pyT", yet in desktop Chrome would throw an error.
btoa('✓');
// returns "fsO1w6bCvA==", yet in desktop Chrome would return "fvXmvA=="
btoa(String.fromCharCode.apply(null, new Uint8Array([0x7e, 0xf5, 0xe6, 0xbc])));
দেখা যাচ্ছে যে, Bufferউদাহরণস্বরূপ ইউটিএফ -8 এ ডিফল্টরূপে এনকোড করা স্ট্রিংগুলি উপস্থাপন / ব্যাখ্যা করে । বিপরীতে, ডেস্কটপ ক্রোমে, আপনি এমন একটি স্ট্রিংও ইনপুট করতে পারবেন না যাতে এতে ল্যাটিন 1 ব্যাপ্তির বাইরে অক্ষর রয়েছে btoa(), কারণ এটি একটি ব্যতিক্রম ছুঁড়ে দেবে:Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
অতএব, আপনি স্পষ্টভাবে সেট করতে হবে এনকোডিং টাইপ করার latin1ডেস্কটপ Chrome এর এনকোডিং টাইপ মেলে shim আপনার Node.js জন্য অনুক্রমে:
const btoaLatin1 = function(str) { return Buffer.from(str, 'latin1').toString('base64'); }
const atobLatin1 = function(b64Encoded) {return Buffer.from(b64Encoded, 'base64').toString('latin1');}
const btoaUTF8 = function(str) { return Buffer.from(str, 'utf8').toString('base64'); }
const atobUTF8 = function(b64Encoded) {return Buffer.from(b64Encoded, 'base64').toString('utf8');}
btoaLatin1('✓'); // returns "Ew==" (would be preferable for it to throw error because this is undecodable)
atobLatin1(btoa('✓')); // returns "\u0019" (END OF MEDIUM)
btoaUTF8('✓'); // returns "4pyT"
atobUTF8(btoa('✓')); // returns "✓"
// returns "fvXmvA==", just like desktop Chrome
btoaLatin1(String.fromCharCode.apply(null, new Uint8Array([0x7e, 0xf5, 0xe6, 0xbc])));
// returns "fsO1w6bCvA=="
btoaUTF8(String.fromCharCode.apply(null, new Uint8Array([0x7e, 0xf5, 0xe6, 0xbc])));