or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Base64 ArrayBuffer

Base64 ArrayBuffer is a lightweight utility library for encoding and decoding base64 data to and from ArrayBuffers in JavaScript and TypeScript environments. It provides high-performance conversion between binary ArrayBuffer data and base64 string representations with optimized lookup tables and proper padding handling.

Package Information

  • Package Name: base64-arraybuffer
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install base64-arraybuffer

Core Imports

import { encode, decode } from "base64-arraybuffer";

For CommonJS:

const { encode, decode } = require("base64-arraybuffer");

Basic Usage

import { encode, decode } from "base64-arraybuffer";

// Create an ArrayBuffer with some data
const buffer = new ArrayBuffer(8);
const view = new Uint8Array(buffer);
view[0] = 72;  // 'H'
view[1] = 101; // 'e'
view[2] = 108; // 'l'
view[3] = 108; // 'l'
view[4] = 111; // 'o'

// Encode ArrayBuffer to base64 string
const base64String = encode(buffer);
console.log(base64String); // "SGVsbG8AAA=="

// Decode base64 string back to ArrayBuffer
const decodedBuffer = decode(base64String);
const decodedView = new Uint8Array(decodedBuffer);
console.log(Array.from(decodedView)); // [72, 101, 108, 108, 111, 0, 0, 0]

Capabilities

Base64 Encoding

Encodes ArrayBuffer data to a base64 string representation with proper padding.

/**
 * Encodes ArrayBuffer data to base64 string
 * @param arraybuffer - The binary data to encode
 * @returns Base64 encoded string representation
 */
function encode(arraybuffer: ArrayBuffer): string;

The encode function converts binary data stored in an ArrayBuffer into a base64-encoded string. It handles all ArrayBuffer sizes correctly and applies proper base64 padding according to RFC 4648 specifications.

Usage Examples:

import { encode } from "base64-arraybuffer";

// Encode text data
const textBuffer = new TextEncoder().encode("Hello World");
const encoded = encode(textBuffer.buffer);
console.log(encoded); // "SGVsbG8gV29ybGQ="

// Encode binary data (all possible byte values)
const binaryBuffer = new ArrayBuffer(256);
const binaryView = new Uint8Array(binaryBuffer);
for (let i = 0; i < 256; i++) {
    binaryView[i] = i;
}
const encodedBinary = encode(binaryBuffer);
// Results in properly encoded base64 string with appropriate padding

Base64 Decoding

Decodes a base64 string back to ArrayBuffer binary data.

/**
 * Decodes base64 string to ArrayBuffer
 * @param base64 - The base64 string to decode
 * @returns ArrayBuffer containing the decoded binary data
 */
function decode(base64: string): ArrayBuffer;

The decode function converts a base64-encoded string back into binary data stored in an ArrayBuffer. It properly handles base64 padding and efficiently processes the entire string using optimized lookup tables.

Usage Examples:

import { decode } from "base64-arraybuffer";

// Decode a base64 string
const decoded = decode("SGVsbG8gV29ybGQ=");
const text = new TextDecoder().decode(decoded);
console.log(text); // "Hello World"

// Decode with different padding scenarios
const decoded1 = decode("TWFu");     // No padding
const decoded2 = decode("TWE=");     // Single padding
const decoded3 = decode("SGVsbG8gd29ybGRzIQ=="); // Double padding

// All decode operations return proper ArrayBuffer instances
console.log(decoded1 instanceof ArrayBuffer); // true

Implementation Details

The library uses several optimizations for performance:

  • Lookup Table: Pre-computed Uint8Array for fast character-to-index conversion during decoding
  • Character Set: Standard base64 alphabet (A-Z, a-z, 0-9, +, /) for maximum compatibility
  • Padding Handling: Automatic padding calculation and validation for both encoding and decoding
  • Memory Efficiency: Direct ArrayBuffer manipulation without intermediate string processing

Browser and Node.js Compatibility

  • Node.js: Compatible with Node.js >= 0.6.0 (as specified in package.json)
  • Browsers: Works in all modern browsers with ArrayBuffer and Uint8Array support
  • TypeScript: Native TypeScript source with complete type definitions
  • Module Systems: Supports both ESM (import/export) and CommonJS (require/module.exports)