Kit
Functions

getHiddenPrefixCodec

Call Signature

getHiddenPrefixCodec<TFrom, TTo>(codec, prefixedCodecs): FixedSizeCodec<TFrom, TTo>

Returns a codec that encodes and decodes values with a hidden prefix.

  • Encoding: Prefixes the value with hidden data before encoding.
  • Decoding: Skips the hidden prefix before decoding the main value.

This is useful for any implicit metadata that should be present in binary formats but omitted from the API.

Type Parameters

Type ParameterDescription
TFromThe type of the main value being encoded.
TToThe type of the main value being decoded.

Parameters

ParameterTypeDescription
codecFixedSizeCodec<TFrom, TTo>The codec for the main value.
prefixedCodecsreadonly FixedSizeCodec<void>[]A list of void codecs that produce the hidden prefix.

Returns

FixedSizeCodec<TFrom, TTo>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding values with a hidden prefix.

Example

Encoding and decoding a value with prefixed constants.

const codec = getHiddenPrefixCodec(getUtf8Codec(), [
  getConstantCodec(new Uint8Array([1, 2, 3])),
  getConstantCodec(new Uint8Array([4, 5, 6])),
]);
 
const bytes = codec.encode('Hello');
// 0x01020304050648656c6c6f
//   |     |     └-- Our encoded value ("Hello").
//   |     └-- Our second hidden prefix.
//   └-- Our first hidden prefix.
 
codec.decode(bytes);
// 'Hello'

Remarks

If all you need is padding zeroes before a value, consider using padLeftCodec instead.

Separate getHiddenPrefixEncoder and getHiddenPrefixDecoder functions are available.

const bytes = getHiddenPrefixEncoder(getUtf8Encoder(), [
  getConstantEncoder(new Uint8Array([1, 2, 3])),
  getConstantEncoder(new Uint8Array([4, 5, 6])),
]).encode('Hello');
 
const value = getHiddenPrefixDecoder(getUtf8Decoder(), [
  getConstantDecoder(new Uint8Array([1, 2, 3])),
  getConstantDecoder(new Uint8Array([4, 5, 6])),
]).decode(bytes);

See

Call Signature

getHiddenPrefixCodec<TFrom, TTo>(codec, prefixedCodecs): VariableSizeCodec<TFrom, TTo>

Returns a codec that encodes and decodes values with a hidden prefix.

  • Encoding: Prefixes the value with hidden data before encoding.
  • Decoding: Skips the hidden prefix before decoding the main value.

This is useful for any implicit metadata that should be present in binary formats but omitted from the API.

Type Parameters

Type ParameterDescription
TFromThe type of the main value being encoded.
TToThe type of the main value being decoded.

Parameters

ParameterTypeDescription
codecCodec<TFrom, TTo>The codec for the main value.
prefixedCodecsreadonly Codec<void>[]A list of void codecs that produce the hidden prefix.

Returns

VariableSizeCodec<TFrom, TTo>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding values with a hidden prefix.

Example

Encoding and decoding a value with prefixed constants.

const codec = getHiddenPrefixCodec(getUtf8Codec(), [
  getConstantCodec(new Uint8Array([1, 2, 3])),
  getConstantCodec(new Uint8Array([4, 5, 6])),
]);
 
const bytes = codec.encode('Hello');
// 0x01020304050648656c6c6f
//   |     |     └-- Our encoded value ("Hello").
//   |     └-- Our second hidden prefix.
//   └-- Our first hidden prefix.
 
codec.decode(bytes);
// 'Hello'

Remarks

If all you need is padding zeroes before a value, consider using padLeftCodec instead.

Separate getHiddenPrefixEncoder and getHiddenPrefixDecoder functions are available.

const bytes = getHiddenPrefixEncoder(getUtf8Encoder(), [
  getConstantEncoder(new Uint8Array([1, 2, 3])),
  getConstantEncoder(new Uint8Array([4, 5, 6])),
]).encode('Hello');
 
const value = getHiddenPrefixDecoder(getUtf8Decoder(), [
  getConstantDecoder(new Uint8Array([1, 2, 3])),
  getConstantDecoder(new Uint8Array([4, 5, 6])),
]).decode(bytes);

See

On this page