[API reference](https://evolu.dev/docs/api-reference) › [@evolu/common](https://evolu.dev/docs/api-reference/common) › Bytes

Bytes: `Uint8Array` helpers, the [Buffer](https://evolu.dev/docs/api-reference/common/Bytes/interfaces/Buffer) codec, and byte sizes.

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

- [ByteLength](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteLength) is the canonical unit, a validated number of bytes.
- [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral) is the human-readable form, such as `"10MiB"`,
  validated at compile time and runtime.
- [ByteSize](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteSize) is what APIs accept: `ByteSizeLiteral | ByteLength`.
- [byteSizeToByteLength](https://evolu.dev/docs/api-reference/common/Bytes/functions/byteSizeToByteLength) normalizes a `ByteSize` to a `ByteLength`.
- [ByteLengthFromString](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteLengthFromString) parses either form from text.

Buffer-based decoding functions throw instead of returning [Result](https://evolu.dev/docs/api-reference/common/Result/type-aliases/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

| Class                                                                   | Description                                                                                                                                                                                       |
| ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [BufferError](https://evolu.dev/docs/api-reference/common/Bytes/classes/BufferError) | Custom error for [Buffer](https://evolu.dev/docs/api-reference/common/Bytes/interfaces/Buffer)-related failures like premature end of data. Provides better stack traces for debugging binary protocol issues. |

## Functions

| Function                                                                                        | Description                                                                                                                                                                                                                                                      |
| ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [byteSizeToByteLength](https://evolu.dev/docs/api-reference/common/Bytes/functions/byteSizeToByteLength)     | Converts a [ByteSize](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteSize) to a [ByteLength](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteLength). A [ByteLength](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteLength) is returned unchanged. |
| [bytesToHex](https://evolu.dev/docs/api-reference/common/Bytes/functions/bytesToHex)                         | Convert byte array to hex string. Uses built-in function, when available.                                                                                                                                                                                        |
| [bytesToUtf8](https://evolu.dev/docs/api-reference/common/Bytes/functions/bytesToUtf8)                       | Converts bytes to string using UTF8 encoding.                                                                                                                                                                                                                    |
| [concatBytes](https://evolu.dev/docs/api-reference/common/Bytes/functions/concatBytes)                       | Copies several Uint8Arrays into one.                                                                                                                                                                                                                             |
| [createBuffer](https://evolu.dev/docs/api-reference/common/Bytes/functions/createBuffer)                     | Creates a [Buffer](https://evolu.dev/docs/api-reference/common/Bytes/interfaces/Buffer) for efficient byte operations.                                                                                                                                                        |
| [createRunLengthEncoder](https://evolu.dev/docs/api-reference/common/Bytes/functions/createRunLengthEncoder) | Creates an incremental run-length encoder.                                                                                                                                                                                                                       |
| [decodeFlags](https://evolu.dev/docs/api-reference/common/Bytes/functions/decodeFlags)                       | Decodes a byte into an array of boolean flags.                                                                                                                                                                                                                   |
| [decodeJsonValue](https://evolu.dev/docs/api-reference/common/Bytes/functions/decodeJsonValue)               | Decodes a [JsonValue](https://evolu.dev/docs/api-reference/common/Type/variables/JsonValue) using the MessagePack format.                                                                                                                                                     |
| [decodeNonNegativeInt](https://evolu.dev/docs/api-reference/common/Bytes/functions/decodeNonNegativeInt)     | Decodes a non-negative integer from a variable-length integer format.                                                                                                                                                                                            |
| [decodeNumber](https://evolu.dev/docs/api-reference/common/Bytes/functions/decodeNumber)                     | -                                                                                                                                                                                                                                                                |
| [decodeRle](https://evolu.dev/docs/api-reference/common/Bytes/functions/decodeRle)                           | Decodes a run-length encoded sequence.                                                                                                                                                                                                                           |
| [decodeString](https://evolu.dev/docs/api-reference/common/Bytes/functions/decodeString)                     | Decodes a length-prefixed UTF-8 string.                                                                                                                                                                                                                          |
| [encodeFlags](https://evolu.dev/docs/api-reference/common/Bytes/functions/encodeFlags)                       | Encodes an array of boolean flags into a single byte.                                                                                                                                                                                                            |
| [encodeJsonValue](https://evolu.dev/docs/api-reference/common/Bytes/functions/encodeJsonValue)               | Encodes a [JsonValue](https://evolu.dev/docs/api-reference/common/Type/variables/JsonValue) using the MessagePack format.                                                                                                                                                     |
| [encodeLength](https://evolu.dev/docs/api-reference/common/Bytes/functions/encodeLength)                     | Encodes the length of an array-like value.                                                                                                                                                                                                                       |
| [encodeNonNegativeInt](https://evolu.dev/docs/api-reference/common/Bytes/functions/encodeNonNegativeInt)     | Encodes a non-negative integer into a variable-length integer format. It's more efficient than encoding via [encodeNumber](https://evolu.dev/docs/api-reference/common/Bytes/functions/encodeNumber).                                                                         |
| [encodeNumber](https://evolu.dev/docs/api-reference/common/Bytes/functions/encodeNumber)                     | Evolu uses MessagePack to handle finite numbers except for NonNegativeInt. For NonNegativeInt, Evolu provides more efficient encoding.                                                                                                                           |
| [encodeString](https://evolu.dev/docs/api-reference/common/Bytes/functions/encodeString)                     | Encodes a length-prefixed UTF-8 string.                                                                                                                                                                                                                          |
| [hexToBytes](https://evolu.dev/docs/api-reference/common/Bytes/functions/hexToBytes)                         | Convert hex string to byte array. Uses built-in function, when available.                                                                                                                                                                                        |
| [utf8ToBytes](https://evolu.dev/docs/api-reference/common/Bytes/functions/utf8ToBytes)                       | Converts string to bytes using UTF8 encoding.                                                                                                                                                                                                                    |

## Interfaces

| Interface                                                                                              | Description                                                                                                                                                                                                                                                                                         |
| ------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Buffer](https://evolu.dev/docs/api-reference/common/Bytes/interfaces/Buffer)                                       | A 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`. |
| [ByteLengthError](https://evolu.dev/docs/api-reference/common/Bytes/interfaces/ByteLengthError)                     | Error returned when [ByteLength](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteLength) rejects negative zero.                                                                                                                                                                                  |
| [ByteLengthFromStringError](https://evolu.dev/docs/api-reference/common/Bytes/interfaces/ByteLengthFromStringError) | Error returned when a string is neither a number of bytes nor a [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).                                                                                                                                                  |
| [ByteSizeLiteralError](https://evolu.dev/docs/api-reference/common/Bytes/interfaces/ByteSizeLiteralError)           | Error returned when [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral) rejects a value.                                                                                                                                                                              |
| [RunLengthEncoder](https://evolu.dev/docs/api-reference/common/Bytes/interfaces/RunLengthEncoder)                   | Incrementally encodes consecutive equal values using run-length encoding.                                                                                                                                                                                                                           |

## Type Aliases

| Type Alias                                                                                     | Description                                                                                                                                                   |
| ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [ByteLength](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteLength)                     | Non-negative safe integer number of bytes.                                                                                                                    |
| [ByteSize](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteSize)                         | [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral) or [ByteLength](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteLength). |
| [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteSizeLiteral)           | Byte length literal Type with compile-time and runtime validation.                                                                                            |
| [ByteSizeLiteralBytes](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteSizeLiteralBytes) | Bytes: `"0B"` to `"1023B"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).                                            |
| [ByteSizeLiteralGiB](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteSizeLiteralGiB)     | GiB: `"1GiB"` to `"1023GiB"` or `"1.5GiB"` to `"1023.5GiB"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).           |
| [ByteSizeLiteralKiB](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteSizeLiteralKiB)     | KiB: `"1KiB"` to `"1023KiB"` or `"1.5KiB"` to `"1023.5KiB"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).           |
| [ByteSizeLiteralMiB](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteSizeLiteralMiB)     | MiB: `"1MiB"` to `"1023MiB"` or `"1.5MiB"` to `"1023.5MiB"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).           |
| [ByteSizeLiteralTiB](https://evolu.dev/docs/api-reference/common/Bytes/type-aliases/ByteSizeLiteralTiB)     | TiB: `"1TiB"` to `"1023TiB"` or `"1.5TiB"` to `"1023.5TiB"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).           |

## Variables

| Variable                                                                                    | Description                                                                                                                                                                                                 |
| ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [ByteLength](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteLength)                     | Non-negative safe integer number of bytes.                                                                                                                                                                  |
| [ByteLengthFromString](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteLengthFromString) | Transforms a number of bytes or a [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral) in text into a [ByteLength](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteLength). |
| [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral)           | Byte length literal Type with compile-time and runtime validation.                                                                                                                                          |
| [ByteSizeLiteralBytes](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteralBytes) | Bytes: `"0B"` to `"1023B"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).                                                                                          |
| [ByteSizeLiteralGiB](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteralGiB)     | GiB: `"1GiB"` to `"1023GiB"` or `"1.5GiB"` to `"1023.5GiB"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).                                                         |
| [ByteSizeLiteralKiB](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteralKiB)     | KiB: `"1KiB"` to `"1023KiB"` or `"1.5KiB"` to `"1023.5KiB"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).                                                         |
| [ByteSizeLiteralMiB](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteralMiB)     | MiB: `"1MiB"` to `"1023MiB"` or `"1.5MiB"` to `"1023.5MiB"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).                                                         |
| [ByteSizeLiteralTiB](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteralTiB)     | TiB: `"1TiB"` to `"1023TiB"` or `"1.5TiB"` to `"1023.5TiB"`. See [ByteSizeLiteral](https://evolu.dev/docs/api-reference/common/Bytes/variables/ByteSizeLiteral).                                                         |
| [decodeLength](https://evolu.dev/docs/api-reference/common/Bytes/variables/decodeLength)                 | Decodes an array-like value length.                                                                                                                                                                         |