API reference › @evolu/common › Type › Base64

const Base64: brand(
  "Base64",
  String,
  (value: string) => {
    const decoded = trySync(() => base64StringToUint8Array(value, "base64"));

    return decoded.ok &&
      uint8ArrayToBase64String(decoded.value, "base64") === value
      ? ok()
      : err<Base64Error>({ type: "Base64", value });
  },
  formatBase64Error,
);

Defined in: packages/common/src/Type.ts:8810

Base64 text with padding, as defined by RFC 4648 section 4.

Accepts only the canonical encoding of some bytes: the standard alphabet with + and /, the required = padding, and unused trailing bits set to zero. Whitespace, line breaks, and the URL-safe - and _ are rejected. Use Base64Url for text in URLs and file names.

Every 4 characters encode up to 3 bytes, so the length of padded text does not determine the byte length: length(44)(Base64) accepts 31 to 33 bytes.

Convert bytes to Base64 with uint8ArrayToBase64 and convert Base64 to bytes with base64ToUint8Array.

Example

import { assertErr, assertOk, Base64 } from "@evolu/common";

assertOk(Base64.fromUnknown("AAEC/w=="), "AAEC/w==");
assertErr(Base64.fromUnknown("AAEC/w"));
assertErr(Base64.fromUnknown("AAEC_w=="));
assertErr(Base64.fromUnknown("AB=="));