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

    Module base64

    URL-safe base64 string codec.

    Note

    A native path via Uint8Array.prototype.toBase64({ alphabet: "base64url", omitPadding: true }) and Uint8Array.fromBase64(..., { alphabet: "base64url" }) 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.

    Provides a symmetric encoder/decoder pair (encodeBase64, decodeBase64) for safely carrying arbitrary Unicode text in URLs and application/x-www-form-urlencoded payloads. Wraps the standard btoa / atob primitives, addressing two limitations that make them unsuitable on their own for 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). The codec adopts the URL-safe variant of RFC 4648 § 5, mapping + / / to - / _ on encode (and back on decode) and stripping the trailing = padding. Decoding accepts both padded and unpadded input.

    Usage

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

    encodeBase64("hello"); // "aGVsbG8" — trailing `=` padding stripped
    encodeBase64(">>>"); // "Pj4-" — standard `+` remapped to `-`
    encodeBase64("???"); // "Pz8_" — standard `/` remapped to `_`
    encodeBase64("日"); // "5pel" — multi-byte UTF-8

    decodeBase64("aGVsbG8"); // "hello" — unpadded input accepted
    decodeBase64("aGVsbG8="); // "hello" — padded input accepted

    Functions

    encodeBase64

    Encodes a string to URL-safe base64.

    decodeBase64

    Decodes a URL-safe base64 string.