Yet another Base64 transcoder in pure-JS with comprehensive UTF-8 support and cross-platform compatibility
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Convert between Uint8Array and Base64 strings for handling binary data like images, files, and byte arrays with cross-platform compatibility.
Converts a Uint8Array to a Base64 string with optional URL-safe encoding.
/**
* Converts a Uint8Array to a Base64 string
* @param u8a - The Uint8Array to convert
* @param urlsafe - If true, make the result URL-safe (uses - and _ instead of + and /)
* @returns Base64 encoded string
*/
function fromUint8Array(u8a: Uint8Array, urlsafe?: boolean): string;Usage Examples:
import { fromUint8Array } from "js-base64";
// Basic conversion
const bytes = new Uint8Array([100, 97, 110, 107, 111, 103, 97, 105]);
const encoded = fromUint8Array(bytes);
console.log(encoded); // "ZGFua29nYWk="
// URL-safe conversion
const urlSafe = fromUint8Array(bytes, true);
console.log(urlSafe); // "ZGFua29nYWk" (no padding)
// Converting image data or file bytes
const imageBytes = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]); // PNG header
const imageB64 = fromUint8Array(imageBytes);
console.log(imageB64); // "iVBORw0KGgo="
// Working with typed arrays from File API or ArrayBuffer
const buffer = new ArrayBuffer(8);
const view = new Uint8Array(buffer);
view.set([72, 101, 108, 108, 111, 33, 33, 33]); // "Hello!!!"
const result = fromUint8Array(view);
console.log(result); // "SGVsbG8hISE="Converts a Base64 string to a Uint8Array for binary data processing.
/**
* Converts a Base64 string to a Uint8Array
* @param a - Base64 string (both normal and URL-safe are supported)
* @returns Uint8Array containing the decoded binary data
*/
function toUint8Array(a: string): Uint8Array;Usage Examples:
import { toUint8Array } from "js-base64";
// Basic conversion
const decoded = toUint8Array("ZGFua29nYWk=");
console.log(decoded); // Uint8Array(8) [100, 97, 110, 107, 111, 103, 97, 105]
// Convert to regular array if needed
const asArray = Array.from(decoded);
console.log(asArray); // [100, 97, 110, 107, 111, 103, 97, 105]
// Working with image data
const pngHeader = toUint8Array("iVBORw0KGgo=");
console.log(pngHeader); // Uint8Array [137, 80, 78, 71, 13, 10, 26, 10]
// URL-safe format (automatically detected)
const urlSafeDecoded = toUint8Array("ZGFua29nYWk");
console.log(urlSafeDecoded); // Same result as above
// Creating blobs from Base64 data
const b64Data = "SGVsbG8gV29ybGQh"; // "Hello World!"
const bytes = toUint8Array(b64Data);
const blob = new Blob([bytes], { type: 'text/plain' });
// Working with fetch responses
async function processB64Image(base64String) {
const imageBytes = toUint8Array(base64String);
const blob = new Blob([imageBytes], { type: 'image/png' });
const url = URL.createObjectURL(blob);
return url;
}import { fromUint8Array, toUint8Array } from "js-base64";
// Original data
const original = new Uint8Array([1, 2, 3, 4, 5, 255, 128, 0]);
// Convert to Base64 and back
const encoded = fromUint8Array(original);
const decoded = toUint8Array(encoded);
// Verify integrity
console.log(original.every((byte, i) => byte === decoded[i])); // trueimport { fromUint8Array, toUint8Array } from "js-base64";
// Reading a file as Base64
async function fileToBase64(file: File): Promise<string> {
const arrayBuffer = await file.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);
return fromUint8Array(uint8Array);
}
// Converting Base64 back to file
function base64ToFile(base64: string, filename: string, mimeType: string): File {
const uint8Array = toUint8Array(base64);
return new File([uint8Array], filename, { type: mimeType });
}import { fromUint8Array, toUint8Array } from "js-base64";
// ArrayBuffer to Base64
function arrayBufferToBase64(buffer: ArrayBuffer): string {
const uint8Array = new Uint8Array(buffer);
return fromUint8Array(uint8Array);
}
// Base64 to ArrayBuffer
function base64ToArrayBuffer(base64: string): ArrayBuffer {
const uint8Array = toUint8Array(base64);
return uint8Array.buffer.slice(uint8Array.byteOffset, uint8Array.byteOffset + uint8Array.byteLength);
}