mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-14 11:39:59 -05:00
34 lines
913 B
JavaScript
34 lines
913 B
JavaScript
|
const atob = require('atob');
|
||
|
const btoa = require('btoa');
|
||
|
|
||
|
class Base64Util {
|
||
|
|
||
|
/**
|
||
|
* Convert a base64 encoded string to a Uint8Array.
|
||
|
* @param {string} base64 - a base64 encoded string.
|
||
|
* @return {Uint8Array} - a decoded Uint8Array.
|
||
|
*/
|
||
|
static base64ToUint8Array (base64) {
|
||
|
const binaryString = atob(base64);
|
||
|
const len = binaryString.length;
|
||
|
const array = new Uint8Array(len);
|
||
|
for (let i = 0; i < len; i++) {
|
||
|
array[i] = binaryString.charCodeAt(i);
|
||
|
}
|
||
|
return array;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Convert a Uint8Array to a base64 encoded string.
|
||
|
* @param {Uint8Array} array - the array to convert.
|
||
|
* @return {string} - the base64 encoded string.
|
||
|
*/
|
||
|
static uint8ArrayToBase64 (array) {
|
||
|
const base64 = btoa(String.fromCharCode.apply(null, array));
|
||
|
return base64;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
module.exports = Base64Util;
|