CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-js-base64

Yet another Base64 transcoder in pure-JS with comprehensive UTF-8 support and cross-platform compatibility

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

js-base64

js-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.

Package Information

  • Package Name: js-base64
  • Package Type: npm
  • Language: TypeScript (compiled to JavaScript)
  • Installation: npm install js-base64

Core Imports

import { 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.

Basic Usage

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]

Architecture

js-base64 is built around several key components:

  • Cross-platform Compatibility: Automatically detects environment capabilities (Buffer, TextEncoder/TextDecoder) and uses native functions when available, falling back to pure JavaScript polyfills
  • Multiple Export Patterns: Provides both individual function exports and a complete namespace object (Base64) for different usage preferences
  • UTF-8 Safety: Handles Unicode characters properly, unlike native btoa/atob which only work with binary strings
  • URL-Safe Variants: Supports RFC4648 §5 URL-safe Base64 encoding with - and _ instead of + and /
  • Optional Extensions: Prototype extensions can be added to String and Uint8Array for fluent method chaining

Capabilities

String Encoding and Decoding

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;

String Operations

Binary Data Operations

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;

Binary Data

Browser Compatibility Functions

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;

Browser Compatibility

Validation and Utility Functions

Validate Base64 strings and access version information.

function isValid(src: unknown): boolean;
const version: string;
const VERSION: string; // @deprecated

Validation and Utilities

Prototype Extensions

Optional methods that can be added to String.prototype and Uint8Array.prototype for fluent method chaining.

function extendString(): void;
function extendUint8Array(): void;
function extendBuiltins(): void;

Prototype Extensions

Types

// 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;
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/js-base64@3.7.x
Publish Source
CLI
Badge
tessl/npm-js-base64 badge