Kit
Functions

fixDecoderSize

fixDecoderSize<TTo, TSize>(decoder, fixedBytes): FixedSizeDecoder<TTo, TSize>

Creates a fixed-size decoder from a given decoder.

The resulting decoder always reads exactly fixedBytes bytes from the input. If the nested decoder is also fixed-size, the bytes are truncated or padded as needed.

For more details, see fixCodecSize.

Type Parameters

Type ParameterDescription
TToThe type of the decoded value.
TSize extends numberThe fixed size of the encoded value in bytes.

Parameters

ParameterTypeDescription
decoderDecoder<TTo>The decoder to wrap into a fixed-size decoder.
fixedBytesTSizeThe fixed number of bytes to read.

Returns

FixedSizeDecoder<TTo, TSize>

A FixedSizeDecoder that ensures a consistent input size.

Example

const decoder = fixDecoderSize(getUtf8Decoder(), 4);
decoder.decode(new Uint8Array([72, 101, 108, 108, 111])); // "Hell" (truncated)
decoder.decode(new Uint8Array([72, 105, 0, 0]));          // "Hi" (zeroes ignored)
decoder.decode(new Uint8Array([72, 105, 121, 97]));       // "Hiya" (same length)

Remarks

If you need a full codec with both encoding and decoding, use fixCodecSize.

See

On this page