my fork of the bluesky client
at main 55 lines 1.6 kB view raw
1import 'fast-text-encoding' 2// @ts-ignore no decl -prf 3import findLast from 'array.prototype.findlast' 4export {} 5 6findLast.shim() 7 8/** 9https://github.com/MaxArt2501/base64-js 10The MIT License (MIT) 11Copyright (c) 2014 MaxArt2501 12 */ 13 14const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' 15// Regular expression to check formal correctness of base64 encoded strings 16const b64re = 17 /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/ 18 19globalThis.atob = (str: string): string => { 20 // atob can work with strings with whitespaces, even inside the encoded part, 21 // but only \t, \n, \f, \r and ' ', which can be stripped. 22 str = String(str).replace(/[\t\n\f\r ]+/g, '') 23 if (!b64re.test(str)) { 24 throw new TypeError( 25 "Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.", 26 ) 27 } 28 29 // Adding the padding if missing, for simplicity 30 str += '=='.slice(2 - (str.length & 3)) 31 var bitmap, 32 result = '', 33 r1, 34 r2, 35 i = 0 36 for (; i < str.length; ) { 37 bitmap = 38 (b64.indexOf(str.charAt(i++)) << 18) | 39 (b64.indexOf(str.charAt(i++)) << 12) | 40 ((r1 = b64.indexOf(str.charAt(i++))) << 6) | 41 (r2 = b64.indexOf(str.charAt(i++))) 42 43 result += 44 r1 === 64 45 ? String.fromCharCode((bitmap >> 16) & 255) 46 : r2 === 64 47 ? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255) 48 : String.fromCharCode( 49 (bitmap >> 16) & 255, 50 (bitmap >> 8) & 255, 51 bitmap & 255, 52 ) 53 } 54 return result 55}