gossamer/uint8_array

JavaScript Uint8Array bindings for interop with APIs that specifically require a typed byte array. Treated as a transit type: bridge to BitArray via to_bit_array and operate on the canonical Gleam surface for transformations, then from_bit_array back when handing off to JavaScript. Non-mutating reads (length, at, to_list) stay for one-shot interop without round-tripping through BitArray.

Types

A typed array of 8-bit unsigned integers (bytes).

Uint8Array is a transit type — it exists for interop with JavaScript APIs that specifically require a Uint8Array. For byte data in Gleam, prefer BitArray. Bridge with from_bit_array / to_bit_array.

See Uint8Array on MDN.

pub type Uint8Array

Values

pub fn at(
  in array: Uint8Array,
  get index: Int,
) -> Result(Int, Nil)

Returns the byte at index, or Error(Nil) if the index is out of bounds. Negative indices count from the end.

pub fn buffer(array: Uint8Array) -> array_buffer.ArrayBuffer

The underlying ArrayBuffer of array.

pub fn from_bit_array(bit_array: BitArray) -> Uint8Array

Creates a Uint8Array from a BitArray. Un-aligned bit arrays are zero-padded to the next byte.

pub fn from_buffer(
  buffer: array_buffer.ArrayBuffer,
) -> Uint8Array

Creates a Uint8Array view over the entirety of buffer.

pub fn from_buffer_range(
  buffer: array_buffer.ArrayBuffer,
  byte_offset byte_offset: Int,
  length length: Int,
) -> Result(Uint8Array, Nil)

Creates a Uint8Array view over a slice of buffer starting at byte_offset and spanning length bytes. Returns Error(Nil) if the range falls outside buffer.

pub fn from_list(list: List(Int)) -> Uint8Array

Creates a Uint8Array from a list of byte values. Values outside 0255 are wrapped modulo 256, matching the JavaScript Uint8Array constructor — from_list([257]) yields a one-byte array containing 1.

pub fn length(array: Uint8Array) -> Int

The number of bytes in the array.

pub fn new() -> Uint8Array

Creates an empty Uint8Array.

pub fn to_bit_array(array: Uint8Array) -> BitArray

Wraps the bytes of array as a BitArray.

pub fn to_list(array: Uint8Array) -> List(Int)

Returns the bytes of array as a Gleam list.

Search Document