API reference › @evolu/common › Type › Uint8ArrayFromBase64
const Uint8ArrayFromBase64: transform(
"Uint8ArrayFromBase64",
Base64,
Uint8Array,
{
from: (value) => ok(base64ToUint8Array(value)),
to: uint8ArrayToBase64,
},
);
Defined in: packages/common/src/Type.ts:8892
Transforms Base64 text into bytes.
Use it to decode binary fields of JSON and other text payloads, for example inside an object. Decoding cannot fail, because Base64 accepts only the canonical encoding of some bytes. Encoding returns that canonical text, so equal bytes encode to equal strings.
Example
import {
assertEqual,
assertErr,
assertOk,
Uint8ArrayFromBase64,
} from "@evolu/common";
const bytes = Uint8ArrayFromBase64.fromUnknown("AAEC/w==");
assertOk(bytes, new Uint8Array([0, 1, 2, 255]));
assertEqual(Uint8ArrayFromBase64.to(bytes.value), "AAEC/w==");
assertErr(Uint8ArrayFromBase64.fromUnknown("AAEC/w"), {
type: "Base64",
value: "AAEC/w",
});