encodeIntoBase64

import { encodeIntoBase64 } from "jsr:@ende/encoding/unstable-base64";

function ยท experimental

function encodeIntoBase64(input: string | Uint8Array_ | ArrayBuffer, output: Uint8Array_, options: Base64Options = [UNSUPPORTED]): number

encodeIntoBase64 takes an input source and encodes it as base64 into the output buffer.

input: string | Uint8Array_ | ArrayBuffer

the source to encode.

output: Uint8Array_

the buffer to write the encoded source to.

options: Base64Options = [UNSUPPORTED]

the options to use for encoding.

Returns

the number of bytes written to the buffer.

Example: Basic Usage

import { assertEquals } from "@ende/assert";
import {
  calcSizeBase64,
  encodeBase64,
  encodeIntoBase64,
} from "@ende/encoding/unstable-base64";

const prefix = "data:url/fake,";
const input = await Ende.readFile("./ende.lock");
const output = new Uint8Array(prefix.length + calcSizeBase64(input.length));

let o = new TextEncoder().encodeInto(prefix, output).written;
o += encodeIntoBase64(input, output.subarray(o), { alphabet: "base64url" });
assertEquals(
  new TextDecoder().decode(output.subarray(0, o)),
  "data:url/fake," +
    encodeBase64(
      await Ende.readFile("./ende.lock"),
      { alphabet: "base64url" },
    ),
);

Source