@metreeca/core - v0.9.22
    Preparing search index...

    Module base64

    RFC 4648 base64 encoders and decoders * Provides a symmetric encoder/decoder pair (encodeBase64, decodeBase64) for carrying arbitrary Unicode text as base64, either in the standard alphabet of RFC 4648 § 4 or in the URL-safe variant of RFC 4648 § 5. Wraps the standard btoa / atob primitives, addressing two limitations that make them unsuitable on their own for Unicode and URL-bound text:

    • btoa / atob accept only binary (Latin-1) strings and throw on any code point above 0xFF. The codec routes input through TextEncoder / TextDecoder to convert between JavaScript strings and UTF-8 byte sequences before delegating to the standard primitives, making multi-byte characters such as "日" transparently encodable.

    • The standard base64 alphabet uses +, /, and =, all of which carry special meaning in URLs and application/x-www-form-urlencoded payloads (+ is interpreted as space, / as a path separator, = as the key/value separator). encodeBase64 accordingly takes a url flag selecting the URL-safe variant of RFC 4648 § 5, which maps + / / to - / _ and strips the trailing = padding. decodeBase64 needs no such flag: it accepts either alphabet, padded or unpadded.

    Usage

    import { encodeBase64, decodeBase64 } from "@metreeca/core/base64";

    encodeBase64("hello"); // "aGVsbG8=" — standard alphabet, `=` padding retained
    encodeBase64(">>>"); // "Pj4+" — standard `+` retained
    encodeBase64("???"); // "Pz8/" — standard `/` retained
    encodeBase64("日"); // "5pel" — multi-byte UTF-8

    encodeBase64("hello", true); // "aGVsbG8" — URL-safe: trailing `=` padding stripped
    encodeBase64(">>>", true); // "Pj4-" — URL-safe: `+` remapped to `-`
    encodeBase64("???", true); // "Pz8_" — URL-safe: `/` remapped to `_`

    decodeBase64("aGVsbG8"); // "hello" — unpadded input accepted
    decodeBase64("aGVsbG8="); // "hello" — padded input accepted
    decodeBase64("Pj4+"); // ">>>" — standard alphabet accepted
    decodeBase64("Pj4-"); // ">>>" — URL-safe alphabet accepted
    Note

    A native path via Uint8Array.prototype.toBase64({ alphabet, omitPadding }) and Uint8Array.fromBase64(..., { alphabet }) covers this use case in one step and has been Baseline across evergreen browsers since September 2025 (Chrome 140, Firefox 133, Safari 18.2). On Node.js, however, V8 still gates it behind the experimental --js-base-64 flag. This module will switch to the native path once Node exposes it unflagged; until then, it keeps delegating to btoa / atob for portable server-side support.

    Functions

    encodeBase64

    Encodes a string to base64.

    decodeBase64

    Decodes a base64 string.