type TypedArray = | Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array; export function b64Encode(buff: ArrayBuffer | TypedArray): string { return btoa( new Uint8Array(buff).reduce((s, b) => s + String.fromCharCode(b), "") ); } export function b64Decode(string: string): Uint8Array { // We must remove newline characters from input string const raw = window.atob(string.replace(/[\r\n]/g, "")); const array = new Uint8Array(new ArrayBuffer(raw.length)); for (let i = 0; i < raw.length; i++) { array[i] = raw.charCodeAt(i); } return array; }