Class: KeyValuePair
Represents a key-value pair for MOQT protocol parameters.
- If
typeValueis even, the value is a varint (bigint). - If
typeValueis odd, the value is a binary blob (Uint8Array) with a maximum length of 65535 bytes.
Use KeyValuePair.tryNewVarInt for varint pairs and KeyValuePair.tryNewBytes for blob pairs. A Type whose own namespace fixes the value encoding overrides the parity rule; see KeyValuePair.newBytes.
Properties
typeValue
readonlytypeValue:bigint
The key/type identifier for this pair.
- Even: value is a varint.
- Odd: value is a blob.
value
readonlyvalue:bigint|Uint8Array<ArrayBufferLike>
The value for this pair.
- If
typeValueis even: a varint (bigint). - If
typeValueis 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
Serializes this key-value pair to a frozen byte buffer. Equivalent to KeyValuePair.serializeDelta with prevType = 0n.
Returns
The serialized buffer.
serializeDelta()
serializeDelta(
prevType):FrozenByteBuffer
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
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()
staticdeserialize(buf):KeyValuePair
Deserializes a KeyValuePair from a buffer. Equivalent to KeyValuePair.deserializeDelta with prevType = 0n.
Parameters
buf
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()
staticdeserializeBytesValue(buf,typeValue):KeyValuePair
Reads a length-prefixed byte-string Value, independent of Type parity.
Parameters
buf
The buffer to read from.
typeValue
bigint
The already-decoded Type.
Returns
KeyValuePair
Throws
LengthExceedsMaxError if the length exceeds 65535 bytes.
deserializeDelta()
staticdeserializeDelta(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
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()
staticdeserializeValue(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
The buffer to read from.
typeValue
bigint
The already-decoded Type.
Returns
KeyValuePair
newBytes()
staticnewBytes(typeValue,value):KeyValuePair
Creates a blob KeyValuePair for a Type whose namespace defines a byte-string value regardless of parity. Prefer KeyValuePair.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()
staticnewVarInt(typeValue,value):KeyValuePair
Creates a varint KeyValuePair for a Type whose namespace defines a numeric value regardless of parity. Prefer KeyValuePair.tryNewVarInt.
Parameters
typeValue
number | bigint
The key/type identifier; parity is not checked.
value
number | bigint
The varint value.
Returns
KeyValuePair
tryNewBytes()
statictryNewBytes(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()
statictryNewVarInt(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.