---
url: https://docs.moqtail.dev/ts/api/model/classes/KeyValuePair.md
---
# Class: KeyValuePair

Represents a key-value pair for MOQT protocol parameters.

* If `typeValue` is **even**, the value is a varint (`bigint`).
* If `typeValue` is **odd**, the value is a binary blob (`Uint8Array`) with a maximum length of 65535 bytes.

Use [KeyValuePair.tryNewVarInt](#trynewvarint) for varint pairs and [KeyValuePair.tryNewBytes](#trynewbytes) for blob pairs.
A Type whose own namespace fixes the value encoding overrides the parity rule; see
[KeyValuePair.newBytes](#newbytes).

## Properties

### typeValue

> `readonly` **typeValue**: `bigint`

The key/type identifier for this pair.

* Even: value is a varint.
* Odd: value is a blob.

***

### value

> `readonly` **value**: `bigint` | `Uint8Array`<`ArrayBufferLike`>

The value for this pair.

* If `typeValue` is even: a varint (`bigint`).
* If `typeValue` is odd: a binary blob (`Uint8Array`).

## Methods

### equals()

> **equals**(`other`): `boolean`

Checks if this pair is equal to another.

#### Parameters

##### other

`KeyValuePair`

The other KeyValuePair.

#### Returns

`boolean`

True if both type and value are equal.

***

### serialize()

> **serialize**(): [`FrozenByteBuffer`](FrozenByteBuffer.md)

Serializes this key-value pair to a frozen byte buffer.
Equivalent to [KeyValuePair.serializeDelta](#serializedelta) with `prevType = 0n`.

#### Returns

[`FrozenByteBuffer`](FrozenByteBuffer.md)

The serialized buffer.

***

### serializeDelta()

> **serializeDelta**(`prevType`): [`FrozenByteBuffer`](FrozenByteBuffer.md)

Serializes this pair's wire form, encoding the Type as a delta from
`prevType` (the type of the previous KVP in the same list, or 0n if this
is the first entry).

#### Parameters

##### prevType

`bigint`

The type of the previous KVP in the same list.

#### Returns

[`FrozenByteBuffer`](FrozenByteBuffer.md)

#### Throws

ProtocolViolationError if this pair's type is less than prevType
(Delta Type is an unsigned varint, so the list must be sorted ascending).

***

### deserialize()

> `static` **deserialize**(`buf`): `KeyValuePair`

Deserializes a KeyValuePair from a buffer.
Equivalent to [KeyValuePair.deserializeDelta](#deserializedelta) with `prevType = 0n`.

#### Parameters

##### buf

[`BaseByteBuffer`](BaseByteBuffer.md)

The buffer to read from.

#### Returns

`KeyValuePair`

The deserialized KeyValuePair.

#### Throws

LengthExceedsMaxError if blob length exceeds 65535 bytes.

#### Throws

NotEnoughBytesError if buffer does not contain enough bytes.

***

### deserializeBytesValue()

> `static` **deserializeBytesValue**(`buf`, `typeValue`): `KeyValuePair`

Reads a length-prefixed byte-string Value, independent of Type parity.

#### Parameters

##### buf

[`BaseByteBuffer`](BaseByteBuffer.md)

The buffer to read from.

##### typeValue

`bigint`

The already-decoded Type.

#### Returns

`KeyValuePair`

#### Throws

LengthExceedsMaxError if the length exceeds 65535 bytes.

***

### deserializeDelta()

> `static` **deserializeDelta**(`buf`, `prevType`): `KeyValuePair`

Deserializes a wire-form KVP whose Type field is a delta from `prevType`
(the type of the previous KVP in the same list, or 0n if this is the
first entry).

#### Parameters

##### buf

[`BaseByteBuffer`](BaseByteBuffer.md)

The buffer to read from.

##### prevType

`bigint`

The type of the previous KVP in the same list.

#### Returns

`KeyValuePair`

#### Throws

ProtocolViolationError if prevType + delta exceeds 2^64 - 1.

***

### deserializeValue()

> `static` **deserializeValue**(`buf`, `typeValue`): `KeyValuePair`

Reads a value whose Type has already been decoded, applying the parity
rule. Callers in a type namespace where a Type uses a different encoding
read that value themselves and do not call this.

#### Parameters

##### buf

[`BaseByteBuffer`](BaseByteBuffer.md)

The buffer to read from.

##### typeValue

`bigint`

The already-decoded Type.

#### Returns

`KeyValuePair`

***

### newBytes()

> `static` **newBytes**(`typeValue`, `value`): `KeyValuePair`

Creates a blob KeyValuePair for a Type whose namespace defines a byte-string
value regardless of parity. Prefer [KeyValuePair.tryNewBytes](#trynewbytes).

#### Parameters

##### typeValue

`number` | `bigint`

The key/type identifier; parity is not checked.

##### value

`Uint8Array`

The binary blob value.

#### Returns

`KeyValuePair`

#### Throws

LengthExceedsMaxError if value length exceeds 65535 bytes.

***

### newVarInt()

> `static` **newVarInt**(`typeValue`, `value`): `KeyValuePair`

Creates a varint KeyValuePair for a Type whose namespace defines a numeric value
regardless of parity. Prefer [KeyValuePair.tryNewVarInt](#trynewvarint).

#### Parameters

##### typeValue

`number` | `bigint`

The key/type identifier; parity is not checked.

##### value

`number` | `bigint`

The varint value.

#### Returns

`KeyValuePair`

***

### tryNewBytes()

> `static` **tryNewBytes**(`typeValue`, `value`): `KeyValuePair`

Creates a new blob KeyValuePair.

#### Parameters

##### typeValue

`number` | `bigint`

Must be odd.

##### value

`Uint8Array`

The binary blob value.

#### Returns

`KeyValuePair`

A KeyValuePair with blob value.

#### Throws

KeyValueFormattingError if typeValue is not odd.

#### Throws

LengthExceedsMaxError if value length exceeds 65535 bytes.

***

### tryNewVarInt()

> `static` **tryNewVarInt**(`typeValue`, `value`): `KeyValuePair`

Creates a new varint KeyValuePair.

#### Parameters

##### typeValue

`number` | `bigint`

Must be even.

##### value

`number` | `bigint`

The varint value.

#### Returns

`KeyValuePair`

A KeyValuePair with varint value.

#### Throws

KeyValueFormattingError if typeValue is not even.
