API reference@evolu/common › Bytes

Bytes: Uint8Array helpers, the Buffer codec, and byte sizes.

Sizes follow the same pattern as durations in Time.ts:

Buffer-based decoding functions throw instead of returning Result. Result is not slow, but every successful decode would allocate one, and a protocol message decodes many values. Throwing is the safe bet for that hot path until benchmarks cover the alternatives. Callers convert a thrown error into a Result once, at the boundary, as parseProtocolHeader in Protocol.ts does.

Classes

ClassDescription
BufferErrorCustom error for Buffer-related failures like premature end of data. Provides better stack traces for debugging binary protocol issues.

Functions

FunctionDescription
byteSizeToByteLengthConverts a ByteSize to a ByteLength. A ByteLength is returned unchanged.
bytesToHexConvert byte array to hex string. Uses built-in function, when available.
bytesToUtf8Converts bytes to string using UTF8 encoding.
concatBytesCopies several Uint8Arrays into one.
createBufferCreates a Buffer for efficient byte operations.
createRunLengthEncoderCreates an incremental run-length encoder.
decodeFlagsDecodes a byte into an array of boolean flags.
decodeJsonValueDecodes a JsonValue using the MessagePack format.
decodeNonNegativeIntDecodes a non-negative integer from a variable-length integer format.
decodeNumber-
decodeRleDecodes a run-length encoded sequence.
decodeStringDecodes a length-prefixed UTF-8 string.
encodeFlagsEncodes an array of boolean flags into a single byte.
encodeJsonValueEncodes a JsonValue using the MessagePack format.
encodeLengthEncodes the length of an array-like value.
encodeNonNegativeIntEncodes a non-negative integer into a variable-length integer format. It's more efficient than encoding via encodeNumber.
encodeNumberEvolu uses MessagePack to handle finite numbers except for NonNegativeInt. For NonNegativeInt, Evolu provides more efficient encoding.
encodeStringEncodes a length-prefixed UTF-8 string.
hexToBytesConvert hex string to byte array. Uses built-in function, when available.
utf8ToBytesConverts string to bytes using UTF8 encoding.

Interfaces

InterfaceDescription
BufferA Buffer is a dynamic, resizable container for binary data, optimized for scenarios where the final size is unknown. It grows exponentially (doubling its capacity) to minimize memory reallocations and uses subarray for efficient, copy-free data access in methods like unwrap and shift.
ByteLengthErrorError returned when ByteLength rejects negative zero.
ByteLengthFromStringErrorError returned when a string is neither a number of bytes nor a ByteSizeLiteral.
ByteSizeLiteralErrorError returned when ByteSizeLiteral rejects a value.
RunLengthEncoderIncrementally encodes consecutive equal values using run-length encoding.

Type Aliases

Type AliasDescription
ByteLengthNon-negative safe integer number of bytes.
ByteSizeByteSizeLiteral or ByteLength.
ByteSizeLiteralByte length literal Type with compile-time and runtime validation.
ByteSizeLiteralBytesBytes: "0B" to "1023B". See ByteSizeLiteral.
ByteSizeLiteralGiBGiB: "1GiB" to "1023GiB" or "1.5GiB" to "1023.5GiB". See ByteSizeLiteral.
ByteSizeLiteralKiBKiB: "1KiB" to "1023KiB" or "1.5KiB" to "1023.5KiB". See ByteSizeLiteral.
ByteSizeLiteralMiBMiB: "1MiB" to "1023MiB" or "1.5MiB" to "1023.5MiB". See ByteSizeLiteral.
ByteSizeLiteralTiBTiB: "1TiB" to "1023TiB" or "1.5TiB" to "1023.5TiB". See ByteSizeLiteral.

Variables

VariableDescription
ByteLengthNon-negative safe integer number of bytes.
ByteLengthFromStringTransforms a number of bytes or a ByteSizeLiteral in text into a ByteLength.
ByteSizeLiteralByte length literal Type with compile-time and runtime validation.
ByteSizeLiteralBytesBytes: "0B" to "1023B". See ByteSizeLiteral.
ByteSizeLiteralGiBGiB: "1GiB" to "1023GiB" or "1.5GiB" to "1023.5GiB". See ByteSizeLiteral.
ByteSizeLiteralKiBKiB: "1KiB" to "1023KiB" or "1.5KiB" to "1023.5KiB". See ByteSizeLiteral.
ByteSizeLiteralMiBMiB: "1MiB" to "1023MiB" or "1.5MiB" to "1023.5MiB". See ByteSizeLiteral.
ByteSizeLiteralTiBTiB: "1TiB" to "1023TiB" or "1.5TiB" to "1023.5TiB". See ByteSizeLiteral.
decodeLengthDecodes an array-like value length.