UNPKG

@metamask/snaps-utils

Version:
38 lines 1.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodeBase64 = exports.encodeBase64 = void 0; const utils_1 = require("@metamask/utils"); const bytes_1 = require("./bytes.cjs"); /** * Provides fast, asynchronous base64 encoding. * * @param input - The input value, assumed to be coercable to bytes. * @returns A base64 string. */ async function encodeBase64(input) { const bytes = (0, bytes_1.getBytes)(input); // In the browser, FileReader is much faster than bytesToBase64. if ('FileReader' in globalThis) { return await new Promise((resolve, reject) => { const reader = Object.assign(new FileReader(), { onload: () => resolve(reader.result.replace('data:application/octet-stream;base64,', '')), onerror: () => reject(reader.error), }); reader.readAsDataURL(new File([bytes], '', { type: 'application/octet-stream' })); }); } return (0, utils_1.bytesToBase64)(bytes); } exports.encodeBase64 = encodeBase64; /** * Provides fast, asynchronous base64 decoding. * * @param base64 - A base64 string. * @returns A Uint8Array of bytes. */ async function decodeBase64(base64) { const response = await fetch(`data:application/octet-stream;base64,${base64}`); return new Uint8Array(await response.arrayBuffer()); } exports.decodeBase64 = decodeBase64; //# sourceMappingURL=base64.cjs.map