|
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 = ""; |
| 1 | +/* Baseline 2025 runtimes */ |
9 | 2 |
|
10 | | - for (let i = 0; i < arraybuffer.byteLength; i++) { |
11 | | - binaryString += String.fromCharCode(dv.getUint8(i)); |
12 | | - } |
| 3 | +/** @type {(array_buffer: ArrayBuffer) => string} */ |
| 4 | +export function encode_native(array_buffer) { |
| 5 | + return new Uint8Array(array_buffer).toBase64(); |
| 6 | +} |
13 | 7 |
|
14 | | - return binaryToAscii(binaryString); |
| 8 | +/** @type {(base64: string) => ArrayBuffer} */ |
| 9 | +export function decode_native(base64) { |
| 10 | + return Uint8Array.fromBase64(base64).buffer; |
15 | 11 | } |
16 | 12 |
|
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); |
| 13 | +/* Node-compatible runtimes */ |
26 | 14 |
|
27 | | - for (let i = 0; i < arraybuffer.byteLength; i++) { |
28 | | - dv.setUint8(i, binaryString.charCodeAt(i)); |
29 | | - } |
| 15 | +/** @type {(array_buffer: ArrayBuffer) => string} */ |
| 16 | +export function encode_buffer(array_buffer) { |
| 17 | + return Buffer.from(array_buffer).toString('base64'); |
| 18 | +} |
30 | 19 |
|
31 | | - return arraybuffer; |
| 20 | +/** @type {(base64: string) => ArrayBuffer} */ |
| 21 | +export function decode_buffer(base64) { |
| 22 | + return Uint8Array.from(Buffer.from(base64, 'base64')).buffer; |
32 | 23 | } |
33 | 24 |
|
34 | | -const KEY_STRING = |
35 | | - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 25 | +/* Legacy runtimes */ |
36 | 26 |
|
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 | | - } |
| 27 | +/** @type {(array_buffer: ArrayBuffer) => string} */ |
| 28 | +export function encode_legacy(array_buffer) { |
| 29 | + const array = new Uint8Array(array_buffer); |
| 30 | + let binary = ''; |
50 | 31 |
|
51 | | - let output = ""; |
52 | | - let buffer = 0; |
53 | | - let accumulatedBits = 0; |
| 32 | + // the maximum number of arguments to String.fromCharCode.apply |
| 33 | + // should be around 0xFFFF in modern engines |
| 34 | + const chunk_size = 0x8000; |
| 35 | + for (let i = 0; i < array.length; i += chunk_size) { |
| 36 | + const chunk = array.subarray(i, i + chunk_size); |
| 37 | + binary += String.fromCharCode.apply(null, chunk); |
| 38 | + } |
54 | 39 |
|
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; |
| 40 | + return btoa(binary); |
75 | 41 | } |
76 | 42 |
|
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; |
| 43 | +/** @type {(base64: string) => ArrayBuffer} */ |
| 44 | +export function decode_legacy(base64) { |
| 45 | + const binary_string = atob(base64); |
| 46 | + const len = binary_string.length; |
| 47 | + const array = new Uint8Array(len); |
| 48 | + |
| 49 | + for (let i = 0; i < len; i++) { |
| 50 | + array[i] = binary_string.charCodeAt(i); |
| 51 | + } |
| 52 | + |
| 53 | + return array.buffer; |
110 | 54 | } |
| 55 | + |
| 56 | +const native = typeof Uint8Array.fromBase64 === 'function'; |
| 57 | +const buffer = |
| 58 | + typeof process === 'object' && process.versions?.node !== undefined; |
| 59 | + |
| 60 | +export const encode64 = native |
| 61 | + ? encode_native |
| 62 | + : buffer |
| 63 | + ? encode_buffer |
| 64 | + : encode_legacy; |
| 65 | +export const decode64 = native |
| 66 | + ? decode_native |
| 67 | + : buffer |
| 68 | + ? decode_buffer |
| 69 | + : decode_legacy; |
0 commit comments