npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

u8a-utils

v1.0.12

Published

A collection of utility functions for working with Uint8Arrays in TypeScript

Readme

u8a-utils

A collection of utility functions for working with Uint8Arrays in TypeScript.

Installation

npm install u8a-utils

Usage

import {
  concatUint8Arrays,
  compareUint8Arrays,
  toUint64BE,
  toUint32BE,
  writeUInt32BE,
  decodeHex,
  encodeHex,
  encodeBase64Url,
  decodeBase64Url,
  ensureUint8Array,
  isUint8Array,
  decodeBase64,
  encodeBase64,
} from 'u8a-utils';

// Concatenate multiple Uint8Arrays
const arr1 = new Uint8Array([1, 2, 3]);
const arr2 = new Uint8Array([4, 5, 6]);
const result = concatUint8Arrays(arr1, arr2);
// result: Uint8Array [1, 2, 3, 4, 5, 6]

// You can also use the spread operator with an array
const arrays = [arr1, arr2];
const result2 = concatUint8Arrays(...arrays);
// result2: Uint8Array [1, 2, 3, 4, 5, 6]

// Compare two Uint8Arrays
const isEqual = compareUint8Arrays(arr1, arr2);
// isEqual: false
const isEqual2 = compareUint8Arrays(arr1, new Uint8Array([1, 2, 3]));
// isEqual2: true

// Convert a number to a 64-bit Uint8Array in big-endian format (safe value)
const uint64Value = toUint64BE(0x12345678);
// uint64Value: Uint8Array [0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78]

// Convert a number to a 32-bit Uint8Array in big-endian format
const uint32Value = toUint32BE(0x12345678);
// uint32Value: Uint8Array [0x12, 0x34, 0x56, 0x78]

// Write a 32-bit unsigned integer to a Uint8Array in big-endian format
const buf = new Uint8Array(4);
writeUInt32BE(buf, 0x12345678);
// buf: Uint8Array [0x12, 0x34, 0x56, 0x78]

// Convert Uint8Array to hexadecimal string
const hex = encodeHex(new Uint8Array([0x0a, 0x1b, 0x2c]));
// hex: "0a1b2c"

// Convert hexadecimal string to Uint8Array
const bytes = decodeHex('0a1b2c');
// bytes: Uint8Array [10, 27, 44]

// Convert Uint8Array to URL-safe Base64 string
const b64u = encodeBase64Url(new Uint8Array([1, 2, 3, 4]));
// b64u: "AQIDBA"

// Convert URL-safe Base64 string to Uint8Array
const bytes2 = decodeBase64Url('AQIDBA');
// bytes2: Uint8Array [1, 2, 3, 4]

// Check if a value is a Uint8Array or Uint8Array-like object
const isU8a = isUint8Array(new Uint8Array([1, 2, 3]));
// isU8a: true

// Ensure a value is a Uint8Array
const encoded = new TextEncoder().encode('hello');
const ensured = ensureUint8Array(encoded);
// ensured: Uint8Array [104, 101, 108, 108, 111]

// Convert Uint8Array to standard Base64 string
const b64 = encodeBase64(new Uint8Array([1, 2, 3, 4]));
// b64: "AQIDBA=="

// Convert standard Base64 string to Uint8Array
const bytes3 = decodeBase64('AQIDBA==');
// bytes3: Uint8Array [1, 2, 3, 4]

API

concatUint8Arrays(...arrays: Uint8Array[]): Uint8Array

Concatenates multiple Uint8Arrays into a single Uint8Array.

Parameters

  • ...arrays: Variable number of Uint8Arrays to concatenate

Returns

A new Uint8Array containing all the concatenated data

compareUint8Arrays(a: Uint8Array, b: Uint8Array): boolean

Compares two Uint8Arrays for equality.

Parameters

  • a: First Uint8Array to compare
  • b: Second Uint8Array to compare

Returns

true if the arrays are equal (same length and values), false otherwise

toUint64BE(value: number): Uint8Array

Converts a 64-bit unsigned integer to a Uint8Array in big-endian format.

Parameters

  • value: The 64-bit unsigned integer to convert

Returns

A Uint8Array containing the 64-bit value in big-endian format

toUint32BE(value: number): Uint8Array

Converts a 32-bit unsigned integer to a Uint8Array in big-endian format.

Parameters

  • value: The 32-bit unsigned integer to convert

Returns

A Uint8Array containing the 32-bit value in big-endian format

Throws

  • RangeError if the value is not in the range [0, 0xffffffff]

writeUInt32BE(u8array: Uint8Array, value: number, offset?: number): void

Writes a 32-bit unsigned integer to a Uint8Array in big-endian format.

Parameters

  • u8array: The Uint8Array to write to
  • value: The 32-bit unsigned integer to write
  • offset: The offset in the Uint8Array to start writing at (default: 0)

Throws

  • RangeError if the value is not in the range [0, MAX_INT32 - 1]

encodeHex(value: Uint8Array): string

Converts a Uint8Array to a hexadecimal string.

Parameters

  • value: The Uint8Array to convert

Returns

A string representing the hexadecimal value of the input Uint8Array

decodeHex(value: string): Uint8Array

Converts a hexadecimal string to a Uint8Array.

Parameters

  • value: The hexadecimal string to convert

Returns

A Uint8Array representing the value of the input hexadecimal string

Throws

  • Error if the input string is not a valid hexadecimal string

encodeBase64Url(value: Uint8Array): string

Converts a Uint8Array to a URL-safe Base64 string.

Parameters

  • value: The Uint8Array to convert

Returns

A URL-safe Base64 string (using '-' and '_' instead of '+' and '/', and without padding)

decodeBase64Url(value: string): Uint8Array

Converts a URL-safe Base64 string to a Uint8Array.

Parameters

  • value: The URL-safe Base64 string to convert

Returns

A Uint8Array representing the value of the input URL-safe Base64 string

isUint8Array(value: unknown): value is Uint8Array

Checks if a value is a Uint8Array or Uint8Array-like object.

Parameters

  • value: The value to check

Returns

true if the value is a Uint8Array or Uint8Array-like object, false otherwise

ensureUint8Array(value: Uint8Array | ArrayLike): Uint8Array

Ensures the input value is a Uint8Array. This function is particularly useful when dealing with Uint8Array-like objects that might be returned by certain APIs (e.g., TextEncoder in jsdom environment).

Parameters

  • value: The input value to ensure as Uint8Array

Returns

A Uint8Array representing the input value

decodeBase64(value: string): Uint8Array

Converts a standard Base64 string to a Uint8Array.

Parameters

  • value: The standard Base64 string to convert

Returns

A Uint8Array representing the value of the input Base64 string

Throws

  • Error if the input contains invalid Base64 characters or has invalid padding

encodeBase64(value: Uint8Array): string

Converts a Uint8Array to a standard Base64 string.

Parameters

  • value: The Uint8Array to convert

Returns

A standard Base64 string (using '+' and '/', and with padding)

Deprecated Functions

The following functions are deprecated and will be removed in a future version. Please use the new function names instead:

  • fromHexdecodeHex
  • toHexencodeHex
  • fromB64decodeBase64
  • toB64encodeBase64
  • fromB64UdecodeBase64Url
  • toB64UencodeBase64Url

Development

# Install dependencies
npm install

# Run tests
npm test

# Build the package
npm run build

License

ISC