or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-base64-js

Base64 encoding/decoding in pure JavaScript for binary data

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/base64-js@1.5.x

To install, run

npx @tessl/cli install tessl/npm-base64-js@1.5.0

index.mddocs/

base64-js

base64-js provides a pure JavaScript implementation for base64 encoding and decoding of binary data. It offers cross-platform compatibility for Node.js and browser environments, supporting both standard and URL-safe base64 encoding variants.

Package Information

  • Package Name: base64-js
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install base64-js

Core Imports

const base64js = require('base64-js');

For ES modules:

import * as base64js from 'base64-js';

For TypeScript with full type safety:

import { byteLength, toByteArray, fromByteArray } from 'base64-js';

Individual imports:

const { byteLength, toByteArray, fromByteArray } = require('base64-js');

Basic Usage

const base64js = require('base64-js');

// Convert binary data to base64 string
const bytes = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
const base64String = base64js.fromByteArray(bytes);
console.log(base64String); // "SGVsbG8="

// Convert base64 string back to binary data
const decodedBytes = base64js.toByteArray(base64String);
console.log(decodedBytes); // Uint8Array [72, 101, 108, 108, 111]

// Calculate byte length of base64 string
const length = base64js.byteLength(base64String);
console.log(length); // 5

Capabilities

Base64 String Length Calculation

Calculates the byte length of a base64 encoded string without decoding it.

/**
 * Calculate the byte length of a base64 encoded string
 * @param {string} b64 - Base64 encoded string
 * @returns {number} The byte length of the decoded data
 * @throws {Error} If string length is not a multiple of 4
 */
function byteLength(b64);

Usage Example:

const base64js = require('base64-js');

const encoded = "SGVsbG8gV29ybGQ="; // "Hello World"
const length = base64js.byteLength(encoded);
console.log(length); // 11

Base64 Decoding

Converts a base64 encoded string to a byte array. Supports both standard base64 and URL-safe base64 variants.

/**
 * Convert base64 string to byte array
 * @param {string} b64 - Base64 encoded string (supports URL-safe variants)
 * @returns {Uint8Array|Array} Decoded binary data (Uint8Array in modern environments, Array in legacy browsers)
 * @throws {Error} If string length is not a multiple of 4
 */
function toByteArray(b64);

Features:

  • Supports standard base64 characters: A-Z, a-z, 0-9, +, /
  • Supports URL-safe base64 characters: - (instead of +) and _ (instead of /)
  • Handles proper padding with = characters
  • Returns Uint8Array in modern environments, Array in legacy browsers without Uint8Array support

Usage Examples:

const base64js = require('base64-js');

// Standard base64
const standard = "SGVsbG8=";
const bytes1 = base64js.toByteArray(standard);

// URL-safe base64
const urlSafe = "SGVsbG8-"; // Using - instead of +
const bytes2 = base64js.toByteArray(urlSafe);

// Both decode to the same result: [72, 101, 108, 108, 111]

Base64 Encoding

Converts a byte array to a base64 encoded string with proper padding.

/**
 * Convert byte array to base64 string
 * @param {Uint8Array|Array<number>} uint8 - Binary data to encode (accepts both Uint8Array and regular arrays)
 * @returns {string} Base64 encoded string with proper padding
 */
function fromByteArray(uint8);

Features:

  • Accepts Uint8Array or regular Array<number> of bytes (0-255 range)
  • Produces standards-compliant base64 with proper = padding
  • Optimized for performance with chunked processing (16383-byte chunks)
  • Handles arrays of any size

Usage Examples:

const base64js = require('base64-js');

// From Uint8Array
const bytes = new Uint8Array([72, 101, 108, 108, 111]);
const encoded1 = base64js.fromByteArray(bytes);

// From regular array
const byteArray = [72, 101, 108, 108, 111];
const encoded2 = base64js.fromByteArray(byteArray);

// Both produce: "SGVsbG8="

// Large data handling
const largeData = new Uint8Array(100000).fill(42);
const encodedLarge = base64js.fromByteArray(largeData);

Error Handling

All functions throw Error if the input base64 string has invalid length:

const base64js = require('base64-js');

try {
  // Invalid base64 - length not multiple of 4
  const result = base64js.toByteArray("SGVsbG8"); // Missing padding
} catch (error) {
  console.error(error.message); // "Invalid string. Length must be a multiple of 4"
}

// The same error is thrown by byteLength for invalid strings
try {
  const length = base64js.byteLength("SGVsbG8");
} catch (error) {
  console.error(error.message); // "Invalid string. Length must be a multiple of 4"
}

Browser Compatibility

  • Node.js: Full support with Uint8Array
  • Modern Browsers: Uses native Uint8Array for optimal performance
  • Legacy Browsers: Falls back to regular Array when Uint8Array is unavailable
  • URL-Safe Base64: Automatically handles - and _ characters per RFC 4648 Section 5