UNPKG

@juanelas/base64

Version:

Base64 for both node.js and browser javascript. It supports URL-safe encoding and enabling/disabling padding. Buffers can be implementedd using ArrayBuffer, any TypedArray, or Buffer

58 lines (55 loc) 1.69 kB
const base64Encode = (bytes) => { const CHUNK_SIZE = 0x8000; const arr = []; for (let i = 0; i < bytes.length; i += CHUNK_SIZE) { arr.push(String.fromCharCode.apply(null, bytes.subarray(i, i + CHUNK_SIZE))); } return btoa(arr.join('')); }; const base64Decode = (encoded) => { return new Uint8Array(atob(encoded) .split('') .map((c) => c.charCodeAt(0))); }; function encode(input, urlsafe = false, padding = true) { let base64 = ''; { const bytes = (typeof input === 'string') ? (new TextEncoder()).encode(input) : new Uint8Array(input); base64 = base64Encode(bytes); } if (urlsafe) base64 = base64ToBase64url(base64); if (!padding) base64 = removeBase64Padding(base64); return base64; } function decode(base64, stringOutput = false) { { let urlsafe = false; if (/^[0-9a-zA-Z_-]+={0,2}$/.test(base64)) { urlsafe = true; } else if (!/^[0-9a-zA-Z+/]*={0,2}$/.test(base64)) { throw new Error('Not a valid base64 input'); } if (urlsafe) base64 = base64urlToBase64(base64); const bytes = base64Decode(base64); return stringOutput ? (new TextDecoder()).decode(bytes) : bytes; } } function base64ToBase64url(base64) { return base64.replace(/\+/g, '-').replace(/\//g, '_'); } function base64urlToBase64(base64url) { return base64url.replace(/-/g, '+').replace(/_/g, '/').replace(/=/g, ''); } function removeBase64Padding(str) { return str.replace(/=/g, ''); } export { decode, encode }; //# sourceMappingURL=bundle.js.map