Yet another Base64 transcoder in pure-JS with comprehensive UTF-8 support and cross-platform compatibility
npx @tessl/cli install tessl/npm-js-base64@3.7.0js-base64 is a comprehensive Base64 encoding and decoding library for JavaScript that works across all environments including browsers and Node.js. It provides UTF-8 safe encoding/decoding, URL-safe Base64 variants, binary data support through Uint8Array integration, and optional prototype extensions for convenient usage patterns.
npm install js-base64import { Base64, encode, decode, fromUint8Array, toUint8Array } from "js-base64";For CommonJS:
const { Base64, encode, decode, fromUint8Array, toUint8Array } = require("js-base64");Using the namespace object:
import { Base64 } from "js-base64";
// All functions available as Base64.encode(), Base64.decode(), etc.import { encode, decode, fromUint8Array, toUint8Array } from "js-base64";
// Basic string encoding/decoding
const encoded = encode("Hello World!");
console.log(encoded); // "SGVsbG8gV29ybGQh"
const decoded = decode(encoded);
console.log(decoded); // "Hello World!"
// UTF-8 safe encoding
const utf8Text = "小飼弾"; // Japanese text
const utf8Encoded = encode(utf8Text);
console.log(utf8Encoded); // "5bCP6aO85by+"
const utf8Decoded = decode(utf8Encoded);
console.log(utf8Decoded); // "小飼弾"
// Binary data support
const uint8Array = new Uint8Array([100, 97, 110, 107, 111, 103, 97, 105]);
const binaryEncoded = fromUint8Array(uint8Array);
console.log(binaryEncoded); // "ZGFua29nYWk="
const binaryDecoded = toUint8Array(binaryEncoded);
console.log(binaryDecoded); // Uint8Array(8) [100, 97, 110, 107, 111, 103, 97, 105]js-base64 is built around several key components:
- and _ instead of + and /Core functionality for converting UTF-8 strings to/from Base64, with support for both standard and URL-safe variants.
function encode(src: string, urlsafe?: boolean): string;
function decode(src: string): string;
function encodeURI(src: string): string;Convert between Uint8Array and Base64 strings for handling binary data like images, files, and byte arrays.
function fromUint8Array(u8a: Uint8Array, urlsafe?: boolean): string;
function toUint8Array(a: string): Uint8Array;Low-level functions that replicate browser btoa/atob behavior with cross-platform support and polyfills.
function atob(asc: string): string;
function btoa(bin: string): string;
function atobPolyfill(asc: string): string;
function btoaPolyfill(bin: string): string;Validate Base64 strings and access version information.
function isValid(src: unknown): boolean;
const version: string;
const VERSION: string; // @deprecatedOptional methods that can be added to String.prototype and Uint8Array.prototype for fluent method chaining.
function extendString(): void;
function extendUint8Array(): void;
function extendBuiltins(): void;// All functions are also available in the Base64 namespace object
interface Base64Namespace {
version: string;
VERSION: string; // @deprecated
encode(src: string, urlsafe?: boolean): string;
toBase64(src: string, urlsafe?: boolean): string; // alias for encode
encodeURI(src: string): string;
encodeURL(src: string): string; // alias for encodeURI
decode(src: string): string;
fromBase64(src: string): string; // alias for decode
fromUint8Array(u8a: Uint8Array, urlsafe?: boolean): string;
toUint8Array(a: string): Uint8Array;
atob(asc: string): string;
btoa(bin: string): string;
atobPolyfill(asc: string): string;
btoaPolyfill(bin: string): string;
isValid(src: unknown): boolean;
utob(u: string): string; // @deprecated
btou(b: string): string; // @deprecated
extendString(): void;
extendUint8Array(): void;
extendBuiltins(): void;
}
declare const Base64: Base64Namespace;