Base64 encoding/decoding (RFC 4648 § 4) with = padding, and base64url (§ 5) without padding under url. Format validation included.

Module b64 | Source packages/front/fw/src/io/codec/b64.js | Deps none | Worker-safe yes

Resolve

const b64 = runtime.resolve('b64');
// Returns: { fromBytes, toBytes, test, url }

API

Method Signature Returns
fromBytes (bytes: Uint8Array|number[]) => string Base64 string with padding
toBytes (str: string) => Uint8Array Byte array
test (str: string) => boolean true if valid Base64
url.fromBytes (bytes: Uint8Array|number[]) => string base64url (- and _), no padding
url.toBytes (str: string) => Uint8Array | false Bytes, or false unless str is the canonical unpadded form
url.test (str: string) => boolean true when url.toBytes accepts str

b64.fromBytes(bytes)

b64.fromBytes(new Uint8Array([72, 101, 108, 108, 111])); // "SGVsbG8="
b64.fromBytes(new Uint8Array([1, 2, 3]));                // "AQID"

b64.toBytes(str)

b64.toBytes('SGVsbG8='); // Uint8Array [72, 101, 108, 108, 111]
b64.toBytes('AQID');     // Uint8Array [1, 2, 3]

b64.test(str)

b64.test('SGVsbG8='); // true
b64.test('Hello!');   // false — invalid character
b64.test('SGVsbG8');  // false — missing padding
b64.test('');         // true — empty string is valid

b64.url

base64url (RFC 4648 § 5), the alphabet of JWS, JWT and URL tokens. The decoder is strict: it accepts the canonical unpadded form only — no =, no + or /, no length of 1 mod 4, and the unused low bits of the last character must be zero (§ 3.5) — so one byte string has exactly one accepted spelling, and anything else returns false without a warning.

b64.url.fromBytes(new Uint8Array([251, 255])); // "-_8"
b64.url.toBytes('-_8');                        // Uint8Array [251, 255]
b64.url.toBytes('-_8=');                       // false — padding
b64.url.toBytes('Zh');                         // false — unused bits set (canonical: "Zg")

Examples

const b64 = runtime.resolve('b64');

// Encode a text string
const { utf8 } = runtime.resolveAll(['utf8']);
const bytes   = utf8.toBytes('Hello World');
const encoded = b64.fromBytes(bytes); // "SGVsbG8gV29ybGQ="

// Validate then decode
if (b64.test(encoded)) {
    const decoded = b64.toBytes(encoded);
    console.log(utf8.fromBytes(decoded)); // "Hello World"
}

Worker Usage

const worker = fw.createWorker(
    function({ libs, args }) {
        const [raw] = args;
        const encoded = libs.b64.fromBytes(new Uint8Array(raw));
        self.postMessage(encoded);
    },
    { dependencies: ['b64'], args: [[72, 101, 108]] }
);

Notes

  • Implements standard RFC 4648 (with + and /, not URL-safe).
  • = padding is always added.
  • test validates: length multiple of 4, valid characters, correct padding.
  • Speed: fromBytes on a Uint8Array and toBytes on canonical input use the platform's Uint8Array.prototype.toBase64 / Uint8Array.fromBase64 where the realm has them (JavaScriptCore — Safari, Bun — and Firefox); elsewhere (V8: Chrome, Node) JS paths over typed arrays (TextDecoder for the output string). 1 MiB encodes in about 0.2 ms (Bun) / 1.1 ms (Node) and decodes in about 0.2 ms / 2 ms.
  • toBytes is lenient, as it always was: an input that is not canonical base64 (a character outside the alphabet, an = before the end, a length that is not a multiple of 4) goes through the original decoder, which reads an unknown character as zero bits and throws a TypeError on a missing padding. Check untrusted input with test first.

See also