Base64 encoding/decoding (RFC 4648) with = padding. 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 }

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

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

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.

See also