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