Utility functions to make dealing with Uint8Arrays easier
npx @tessl/cli install tessl/npm-uint8arrays@5.1.0uint8arrays is a comprehensive TypeScript utility library providing memory-efficient Uint8Array manipulation functions. It offers essential operations for byte array handling with automatic Node.js Buffer optimizations when available, while maintaining full compatibility across browsers and other JavaScript environments.
npm install uint8arraysimport { compare, concat, equals, fromString, toString, xor } from "uint8arrays";Submodule imports for tree-shaking:
import { alloc, allocUnsafe } from "uint8arrays/alloc";
import { compare } from "uint8arrays/compare";
import { concat } from "uint8arrays/concat";
import { equals } from "uint8arrays/equals";
import { fromString } from "uint8arrays/from-string";
import { toString } from "uint8arrays/to-string";
import { xor } from "uint8arrays/xor";
import { xorCompare } from "uint8arrays/xor-compare";CommonJS:
const { compare, concat, equals, fromString, toString, xor } = require("uint8arrays");import { concat, fromString, toString, equals } from "uint8arrays";
import { alloc } from "uint8arrays/alloc";
// Create and manipulate arrays
const buf1 = alloc(10); // Zero-initialized 10-byte array
const buf2 = fromString("hello", "utf8"); // Convert string to bytes
const buf3 = fromString("world", "utf8");
// Concatenate arrays
const combined = concat([buf2, new Uint8Array([32]), buf3]); // "hello world"
// Convert back to string
const text = toString(combined, "utf8"); // "hello world"
// Check equality
const areEqual = equals(buf2, fromString("hello", "utf8")); // true
// Encode to different formats
const hex = toString(buf2, "hex"); // "68656c6c6f"
const base64 = toString(buf2, "base64"); // "aGVsbG8"uint8arrays is built around several key principles:
Efficient Uint8Array creation with zero-initialized and unsafe allocation options. Automatically uses Node.js Buffer when available for better performance.
function alloc(size?: number): Uint8Array;
function allocUnsafe(size?: number): Uint8Array;Core array manipulation functions including concatenation, comparison, and equality testing for Uint8Arrays.
function concat(arrays: Uint8Array[], length?: number): Uint8Array;
function compare(a: Uint8Array, b: Uint8Array): number;
function equals(a: Uint8Array, b: Uint8Array): boolean;Comprehensive string encoding and decoding with support for UTF-8, hex, base64, and all multibase formats.
function fromString(string: string, encoding?: SupportedEncodings): Uint8Array;
function toString(array: Uint8Array, encoding?: SupportedEncodings): string;
type SupportedEncodings = 'utf8' | 'utf-8' | 'hex' | 'latin1' | 'ascii' | 'binary' | keyof typeof bases;Bitwise XOR operations and distance comparison functions for cryptographic and DHT applications.
function xor(a: Uint8Array, b: Uint8Array): Uint8Array;
function xorCompare(a: Uint8Array, b: Uint8Array): -1 | 0 | 1;type SupportedEncodings =
| 'utf8'
| 'utf-8'
| 'hex'
| 'latin1'
| 'ascii'
| 'binary'
| 'base2'
| 'base8'
| 'base10'
| 'base16'
| 'base16upper'
| 'base32'
| 'base32upper'
| 'base32pad'
| 'base32padupper'
| 'base32hex'
| 'base32hexupper'
| 'base32hexpad'
| 'base32hexpadupper'
| 'base32z'
| 'base36'
| 'base36upper'
| 'base58btc'
| 'base58flickr'
| 'base64'
| 'base64pad'
| 'base64url'
| 'base64urlpad';