Kit
Functions

getConstantDecoder

getConstantDecoder<TConstant>(constant): FixedSizeDecoder<void, TConstant["length"]>

Returns a decoder that verifies a predefined constant byte sequence.

This decoder reads the next bytes and checks that they match the provided constant. If the bytes differ, it throws an error.

For more details, see getConstantCodec.

Type Parameters

Type ParameterDescription
TConstant extends ReadonlyUint8ArrayThe fixed byte sequence expected during decoding.

Parameters

ParameterTypeDescription
constantTConstantThe predefined byte array to verify.

Returns

FixedSizeDecoder<void, TConstant["length"]>

A FixedSizeDecoder<void, N> where N is the length of the constant.

Example

Decoding a constant magic number.

const decoder = getConstantDecoder(new Uint8Array([1, 2, 3]));
 
decoder.decode(new Uint8Array([1, 2, 3])); // Passes
decoder.decode(new Uint8Array([1, 2, 4])); // Throws an error

See

getConstantCodec

On this page