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.
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.
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/atobprimitives, addressing two limitations that make them unsuitable on their own for Unicode and URL-bound text:btoa/atobaccept only binary (Latin-1) strings and throw on any code point above0xFF. The codec routes input throughTextEncoder/TextDecoderto 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 andapplication/x-www-form-urlencodedpayloads (+is interpreted as space,/as a path separator,=as the key/value separator). encodeBase64 accordingly takes aurlflag 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
A native path via
Uint8Array.prototype.toBase64({ alphabet, omitPadding })andUint8Array.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-64flag. This module will switch to the native path once Node exposes it unflagged; until then, it keeps delegating tobtoa/atobfor portable server-side support.See