@ende/encoding

ende add jsr:@ende/encoding

Version 2026.10.15 on JSR.

Utilities for encoding and decoding common formats like hex, base64, and varint.

Basic Usage

import { encodeBase64, decodeBase64 } from "@ende/encoding";
import { assertEquals } from "@ende/assert";

const foobar = new TextEncoder().encode("foobar");
assertEquals(encodeBase64(foobar), "Zm9vYmFy");
assertEquals(decodeBase64("Zm9vYmFy"), foobar);

Various Encoding Formats

import {
  encodeHex,
  encodeBase32,
  encodeBase58,
  encodeBase64,
  encodeAscii85,
  decodeHex,
  decodeBase32,
  decodeBase58,
  decodeBase64,
  decodeAscii85,
} from "@ende/encoding";
import { assertEquals } from "@ende/assert";

// Many different encodings for different character sets
assertEquals(encodeHex("Hello world!"), "48656c6c6f20776f726c6421");
assertEquals(encodeBase32("Hello world!"), "JBSWY3DPEB3W64TMMQQQ====");
assertEquals(encodeBase58("Hello world!"), "2NEpo7TZRhna7vSvL");
assertEquals(encodeBase64("Hello world!"), "SGVsbG8gd29ybGQh");
assertEquals(encodeAscii85("Hello world!"), "87cURD]j7BEbo80");

// Decoding
assertEquals(new TextDecoder().decode(decodeHex("48656c6c6f20776f726c6421")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeBase32("JBSWY3DPEB3W64TMMQQQ====")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeBase58("2NEpo7TZRhna7vSvL")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeBase64("SGVsbG8gd29ybGQh")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeAscii85("87cURD]j7BEbo80")), "Hello world!");

URL-Safe Base64

import { encodeBase64, encodeBase64Url } from "@ende/encoding";
import { assertEquals } from "@ende/assert";

assertEquals(encodeBase64("ice creams"), "aWNlIGNyZWFtcw=="); // Not url-safe because of `=`
assertEquals(encodeBase64Url("ice creams"), "aWNlIGNyZWFtcw"); // URL-safe!

// Base64Url replaces + with - and / with _
assertEquals(encodeBase64("subjects?"), "c3ViamVjdHM/"); // slash is not URL-safe
assertEquals(encodeBase64Url("subjects?"), "c3ViamVjdHM_"); // _ is URL-safe

Binary Data Encoding

import { encodeHex, encodeBase64 } from "@ende/encoding";
import { assertEquals } from "@ende/assert";

// Working with binary data
const binaryData = new Uint8Array([0xDE, 0xAD, 0xBE, 0xEF]);
assertEquals(encodeHex(binaryData), "deadbeef");
assertEquals(encodeBase64(binaryData), "3q2+7w==");

Varint Encoding

Learn more from the protobuf Varint encoding docs.

import { encodeVarint, decodeVarint } from "@ende/encoding";
import { assertEquals } from "@ende/assert";

// Varint encoding support
assertEquals(encodeVarint(9601n), [new Uint8Array([129, 75]), 2]);

// Decode a varint
const bytes = new Uint8Array([129, 75]);
assertEquals(decodeVarint(bytes), [9601n, 2]);

Modules

Exports

  • Ascii85Standard — Supported ascii85 standards for EncodeAscii85Options and DecodeAscii85Options.
  • decodeAscii85 — Decodes a ascii85-encoded string.
  • DecodeAscii85Options — Options for decodeAscii85.
  • decodeBase32 — Decodes a base32-encoded string.
  • decodeBase58 — Decodes a base58-encoded string.
  • decodeBase64 — Decodes a base64-encoded string.
  • decodeBase64Url — Decodes a given base64url-encoded string.
  • decodeHex — Decodes the given hex-encoded string. If the input is malformed, an error is thrown.
  • decodeVarint — Given a non empty `buf`, starting at `offset` (default: 0), begin decoding bytes as Varint encoded bytes, for a maximum of 10 bytes (offset + 10). The returned tuple is of the decoded varint 32-bit number, and the new offset with which to continue decoding other data.
  • decodeVarint32 — Given a `buf`, starting at `offset` (default: 0), begin decoding bytes as Varint encoded bytes, for a maximum of 5 bytes (offset + 5). The returned tuple is of the decoded varint 32-bit number, and the new offset with which to continue decoding other data.
  • encodeAscii85 — Converts data into an ascii85-encoded string.
  • EncodeAscii85Options — Options for encodeAscii85.
  • encodeBase32 — Converts data into a base32-encoded string.
  • encodeBase58 — Converts data into a base58-encoded string.
  • encodeBase64 — Converts data into a base64-encoded string.
  • encodeBase64Url — Convert data into a base64url-encoded string.
  • encodeHex — Converts data into a hex-encoded string.
  • encodeVarint — Takes unsigned number `num` and converts it into a Varint encoded `Uint8Array`, returning a tuple consisting of a `Uint8Array` slice of the encoded Varint, and an offset where the Varint encoded bytes end within the `Uint8Array`.
  • MaxUint64 — The maximum value of an unsigned 64-bit integer. Equivalent to `2n**64n - 1n`
  • MaxVarintLen32 — The maximum length, in bytes, of a Varint encoded 32-bit integer.
  • MaxVarintLen64 — The maximum length, in bytes, of a Varint encoded 64-bit integer.
  • Uint8Array_ — Proxy type of {@code Uint8Array<ArrayBuffer>} or {@code Uint8Array} in TypeScript 5.7 or below respectively.