1// @ts-nocheck 2// @flow 3// Inspired by: https://github.com/davidchambers/Base64.js/blob/master/base64.js 4 5const chars = 6 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 7const Base64 = { 8 btoa: (input: string = '') => { 9 let str = input; 10 let output = ''; 11 12 for ( 13 let block = 0, charCode, i = 0, map = chars; 14 str.charAt(i | 0) || ((map = '='), i % 1); 15 output += map.charAt(63 & (block >> (8 - (i % 1) * 8))) 16 ) { 17 charCode = str.charCodeAt((i += 3 / 4)); 18 19 if (charCode > 0xff) { 20 throw new Error( 21 "'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.", 22 ); 23 } 24 25 block = (block << 8) | charCode; 26 } 27 28 return output; 29 }, 30 31 atob: (input: string = '') => { 32 let str = input.replace(/[=]+$/, ''); 33 let output = ''; 34 35 if (str.length % 4 == 1) { 36 throw new Error( 37 "'atob' failed: The string to be decoded is not correctly encoded.", 38 ); 39 } 40 for ( 41 let bc = 0, bs = 0, buffer, i = 0; 42 (buffer = str.charAt(i++)); 43 ~buffer && ((bs = bc % 4 ? bs * 64 + buffer : buffer), bc++ % 4) 44 ? (output += String.fromCharCode(255 & (bs >> ((-2 * bc) & 6)))) 45 : 0 46 ) { 47 buffer = chars.indexOf(buffer); 48 } 49 50 return output; 51 }, 52}; 53 54export default Base64; 55