|
| 1 | +/** |
| 2 | + * Base64 Encodes an arraybuffer |
| 3 | + * @param {ArrayBuffer} arraybuffer |
| 4 | + * @returns {string} |
| 5 | + */ |
| 6 | +export function encode64(arraybuffer) { |
| 7 | + const dv = new DataView(arraybuffer); |
| 8 | + let binaryString = ""; |
| 9 | + |
| 10 | + for (let i = 0; i < arraybuffer.byteLength; i++) { |
| 11 | + binaryString += String.fromCharCode(dv.getUint8(i)); |
| 12 | + } |
| 13 | + |
| 14 | + return binaryToAscii(binaryString); |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Decodes a base64 string into an arraybuffer |
| 19 | + * @param {string} string |
| 20 | + * @returns {ArrayBuffer} |
| 21 | + */ |
| 22 | +export function decode64(string) { |
| 23 | + const binaryString = asciiToBinary(string); |
| 24 | + const arraybuffer = new ArrayBuffer(binaryString.length); |
| 25 | + const dv = new DataView(arraybuffer); |
| 26 | + |
| 27 | + for (let i = 0; i < arraybuffer.byteLength; i++) { |
| 28 | + dv.setUint8(i, binaryString.charCodeAt(i)); |
| 29 | + } |
| 30 | + |
| 31 | + return arraybuffer; |
| 32 | +} |
| 33 | + |
| 34 | +const KEY_STRING = |
| 35 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 36 | + |
| 37 | +/** |
| 38 | + * Substitute for atob since it's deprecated in node. |
| 39 | + * Does not do any input validation. |
| 40 | + * |
| 41 | + * @see https://github.com/jsdom/abab/blob/master/lib/atob.js |
| 42 | + * |
| 43 | + * @param {string} data |
| 44 | + * @returns {string} |
| 45 | + */ |
| 46 | +function asciiToBinary(data) { |
| 47 | + if (data.length % 4 === 0) { |
| 48 | + data = data.replace(/==?$/, ""); |
| 49 | + } |
| 50 | + |
| 51 | + let output = ""; |
| 52 | + let buffer = 0; |
| 53 | + let accumulatedBits = 0; |
| 54 | + |
| 55 | + for (let i = 0; i < data.length; i++) { |
| 56 | + buffer <<= 6; |
| 57 | + buffer |= KEY_STRING.indexOf(data[i]); |
| 58 | + accumulatedBits += 6; |
| 59 | + if (accumulatedBits === 24) { |
| 60 | + output += String.fromCharCode((buffer & 0xff0000) >> 16); |
| 61 | + output += String.fromCharCode((buffer & 0xff00) >> 8); |
| 62 | + output += String.fromCharCode(buffer & 0xff); |
| 63 | + buffer = accumulatedBits = 0; |
| 64 | + } |
| 65 | + } |
| 66 | + if (accumulatedBits === 12) { |
| 67 | + buffer >>= 4; |
| 68 | + output += String.fromCharCode(buffer); |
| 69 | + } else if (accumulatedBits === 18) { |
| 70 | + buffer >>= 2; |
| 71 | + output += String.fromCharCode((buffer & 0xff00) >> 8); |
| 72 | + output += String.fromCharCode(buffer & 0xff); |
| 73 | + } |
| 74 | + return output; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Substitute for btoa since it's deprecated in node. |
| 79 | + * Does not do any input validation. |
| 80 | + * |
| 81 | + * @see https://github.com/jsdom/abab/blob/master/lib/btoa.js |
| 82 | + * |
| 83 | + * @param {string} str |
| 84 | + * @returns {string} |
| 85 | + */ |
| 86 | +function binaryToAscii(str) { |
| 87 | + let out = ""; |
| 88 | + for (let i = 0; i < str.length; i += 3) { |
| 89 | + /** @type {[number, number, number, number]} */ |
| 90 | + const groupsOfSix = [undefined, undefined, undefined, undefined]; |
| 91 | + groupsOfSix[0] = str.charCodeAt(i) >> 2; |
| 92 | + groupsOfSix[1] = (str.charCodeAt(i) & 0x03) << 4; |
| 93 | + if (str.length > i + 1) { |
| 94 | + groupsOfSix[1] |= str.charCodeAt(i + 1) >> 4; |
| 95 | + groupsOfSix[2] = (str.charCodeAt(i + 1) & 0x0f) << 2; |
| 96 | + } |
| 97 | + if (str.length > i + 2) { |
| 98 | + groupsOfSix[2] |= str.charCodeAt(i + 2) >> 6; |
| 99 | + groupsOfSix[3] = str.charCodeAt(i + 2) & 0x3f; |
| 100 | + } |
| 101 | + for (let j = 0; j < groupsOfSix.length; j++) { |
| 102 | + if (typeof groupsOfSix[j] === "undefined") { |
| 103 | + out += "="; |
| 104 | + } else { |
| 105 | + out += KEY_STRING[groupsOfSix[j]]; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + return out; |
| 110 | +} |
0 commit comments