A robust base64 encoder/decoder that is fully compatible with atob() and btoa(), written in JavaScript.
npx @tessl/cli install tessl/npm-base-64@1.0.0base-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.
npm install base-64ES6 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>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"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=="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"The library version as a string.
/**
* Semantic version number of the library
* @type {string}
*/
base64.versionThe 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"
}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 世界"The library works across all JavaScript environments through UMD pattern:
atob() and btoa() functions