or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-base-64

A robust base64 encoder/decoder that is fully compatible with atob() and btoa(), written in JavaScript.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/base-64@1.0.x

To install, run

npx @tessl/cli install tessl/npm-base-64@1.0.0

index.mddocs/

base-64

base-64 is a robust base64 encoder/decoder that is fully compatible with the native browser atob() and btoa() functions. It provides RFC 4648 compliant base64 encoding and decoding functionality across all JavaScript environments, including Node.js, browsers, Rhino, Narwhal, RingoJS, and other JavaScript runtime environments through the UMD pattern.

Package Information

  • Package Name: base-64
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install base-64

Core Imports

ES6 modules:

import base64 from "base-64";

CommonJS (Node.js):

const base64 = require("base-64");

AMD (RequireJS):

require(["base-64"], function(base64) {
  // base64 object available
});

Browser (global variable):

<script src="base64.js"></script>
<script>
  // base64 object available globally
</script>

Basic Usage

const base64 = require("base-64");

// Encode a string to base64
const encoded = base64.encode("Hello, World!");
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="

// Decode a base64 string
const decoded = base64.decode("SGVsbG8sIFdvcmxkIQ==");
console.log(decoded); // "Hello, World!"

// Handle Unicode strings (requires UTF-8 encoding first)
const utf8 = require("utf8"); // Additional dependency for UTF-8
const text = "foo © bar 𝌆 baz";
const bytes = utf8.encode(text);
const encodedUnicode = base64.encode(bytes);
console.log(encodedUnicode); // "Zm9vIMKpIGJhciDwnYyGIGJheg=="

// Check library version
console.log(base64.version); // "1.0.0"

Capabilities

Base64 Encoding

Encodes a byte string to base64 format, fully compatible with the native browser btoa() function.

/**
 * Encodes a byte string to base64 format
 * @param {string} input - A string containing only characters from U+0000 to U+00FF representing binary bytes
 * @returns {string} Base64 encoded representation of the input
 * @throws {InvalidCharacterError} When input contains characters outside Latin1 range (U+0000 to U+00FF)
 */
base64.encode(input)

Usage Examples:

// Basic encoding
const encoded = base64.encode("Hello");
console.log(encoded); // "SGVsbG8="

// Encoding with null bytes
const withNull = base64.encode("foo\0");  
console.log(withNull); // "Zm9vAA=="

// Binary data encoding
const binaryData = "\0\x01\x02\xFF";
const encodedBinary = base64.encode(binaryData);
console.log(encodedBinary); // "AAEC/w=="

Base64 Decoding

Decodes a base64-encoded string, fully compatible with the native browser atob() function.

/**
 * Decodes a base64-encoded string
 * @param {string} input - A valid base64-encoded string
 * @returns {string} Decoded byte string with characters from U+0000 to U+00FF
 * @throws {InvalidCharacterError} When input is not correctly base64-encoded
 */
base64.decode(input)

Usage Examples:

// Basic decoding
const decoded = base64.decode("SGVsbG8=");
console.log(decoded); // "Hello"

// Decoding with padding variations
const noPadding = base64.decode("SGVsbG8");    // Missing padding (still works)
const decoded2 = base64.decode("SGVsbG8=");    // One padding character
const decoded3 = base64.decode("SGVsbG8A");    // No padding needed

// Handles whitespace removal
const withSpaces = base64.decode("SGVs bG8=");  // Spaces are ignored
console.log(withSpaces); // "Hello"

Version Information

The library version as a string.

/**
 * Semantic version number of the library
 * @type {string}
 */
base64.version

Error Handling

The library throws InvalidCharacterError for invalid input, matching the behavior of native browser functions.

/**
 * Custom error class for invalid character errors
 * @extends Error
 */
class InvalidCharacterError extends Error {
  /**
   * @param {string} message - Error message
   */
  constructor(message)
  
  /**
   * Error name
   * @type {string}
   */
  name: "InvalidCharacterError"
  
  /**
   * Error message
   * @type {string}
   */
  message: string
}

Error Scenarios:

// Encoding errors - characters outside Latin1 range
try {
  base64.encode("Hello 🌍"); // Contains Unicode outside U+0000-U+00FF
} catch (error) {
  console.log(error.name); // "InvalidCharacterError"
  console.log(error.message); // "The string to be encoded contains characters outside of the Latin1 range."
}

// Decoding errors - invalid base64 characters
try {
  base64.decode("SGVs#G8="); // Contains invalid character '#'
} catch (error) {
  console.log(error.name); // "InvalidCharacterError"
  console.log(error.message); // "Invalid character: the string to be decoded is not correctly encoded."
}

// Decoding errors - invalid length
try {
  base64.decode("SGVs"); // Invalid length (not multiple of 4 after padding removal)
} catch (error) {
  console.log(error.name); // "InvalidCharacterError"
}

Unicode Support

For Unicode strings, you must encode to UTF-8 first before base64 encoding:

// Requires additional utf8 package: npm install utf8
const utf8 = require("utf8");

// Encoding Unicode
const text = "Hello 世界";
const utf8Bytes = utf8.encode(text);      // Convert to UTF-8 bytes
const base64Encoded = base64.encode(utf8Bytes);  // Then base64 encode

// Decoding Unicode
const base64Data = "SGVsbG8g5LiW55WM";
const utf8Bytes2 = base64.decode(base64Data);    // Base64 decode first
const unicodeText = utf8.decode(utf8Bytes2);     // Then UTF-8 decode
console.log(unicodeText); // "Hello 世界"

Platform Compatibility

The library works across all JavaScript environments through UMD pattern:

  • Node.js: v0.10.0 and higher
  • Browsers: Chrome, Firefox, Safari, Opera, Internet Explorer (all versions)
  • Server-side JS: Narwhal 0.3.2+, RingoJS 0.8-0.9, Rhino 1.7RC4+
  • Headless: PhantomJS 1.9.0+
  • Any ES3+ JavaScript environment

Compliance

  • RFC 4648: Fully compliant base64 implementation
  • HTML Standard: Compatible with native atob() and btoa() functions
  • Error Behavior: Matches browser implementations exactly