Kit
Functions

combineCodec

Call Signature

combineCodec<TFrom, TTo, TSize>(encoder, decoder): FixedSizeCodec<TFrom, TTo, TSize>

Combines an Encoder and a Decoder into a Codec.

That is, given a Encoder<TFrom> and a Decoder<TTo>, this function returns a Codec<TFrom, TTo>.

This allows for modular composition by keeping encoding and decoding logic separate while still offering a convenient way to bundle them into a single Codec. This is particularly useful for library maintainers who want to expose Encoders, Decoders, and Codecs separately, enabling tree-shaking of unused logic.

The provided Encoder and Decoder must be compatible in terms of:

  • Fixed Size: If both are fixed-size, they must have the same fixedSize value.
  • Variable Size: If either has a maxSize attribute, it must match the other.

If these conditions are not met, a SolanaError will be thrown.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TToThe type of the decoded value.
TSize extends numberThe fixed size of the encoded value in bytes (for fixed-size codecs).

Parameters

ParameterTypeDescription
encoderFixedSizeEncoder<TFrom, TSize>The Encoder to combine.
decoderFixedSizeDecoder<TTo, TSize>The Decoder to combine.

Returns

FixedSizeCodec<TFrom, TTo, TSize>

A Codec that provides both encode and decode methods.

Throws

  • SOLANA_ERROR__CODECS__ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH Thrown if the encoder and decoder have mismatched size types (fixed vs. variable).
  • SOLANA_ERROR__CODECS__ENCODER_DECODER_FIXED_SIZE_MISMATCH Thrown if both are fixed-size but have different fixedSize values.
  • SOLANA_ERROR__CODECS__ENCODER_DECODER_MAX_SIZE_MISMATCH Thrown if the maxSize attributes do not match.

Examples

Creating a fixed-size Codec from an encoder and a decoder.

const encoder = getU32Encoder();
const decoder = getU32Decoder();
const codec = combineCodec(encoder, decoder);
 
const bytes = codec.encode(42); // 0x2a000000
const value = codec.decode(bytes); // 42

Creating a variable-size Codec from an encoder and a decoder.

const encoder = addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder());
const decoder = addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder());
const codec = combineCodec(encoder, decoder);
 
const bytes = codec.encode("hello"); // 0x0500000068656c6c6f
const value = codec.decode(bytes); // "hello"

Remarks

The recommended pattern for defining codecs in libraries is to expose separate functions for the encoder, decoder, and codec. This allows users to import only what they need, improving tree-shaking efficiency.

type MyType = /* ... */;
const getMyTypeEncoder = (): Encoder<MyType> => { /* ... */ };
const getMyTypeDecoder = (): Decoder<MyType> => { /* ... */ };
const getMyTypeCodec = (): Codec<MyType> =>
    combineCodec(getMyTypeEncoder(), getMyTypeDecoder());

See

Call Signature

combineCodec<TFrom, TTo>(encoder, decoder): VariableSizeCodec<TFrom, TTo>

Combines an Encoder and a Decoder into a Codec.

That is, given a Encoder<TFrom> and a Decoder<TTo>, this function returns a Codec<TFrom, TTo>.

This allows for modular composition by keeping encoding and decoding logic separate while still offering a convenient way to bundle them into a single Codec. This is particularly useful for library maintainers who want to expose Encoders, Decoders, and Codecs separately, enabling tree-shaking of unused logic.

The provided Encoder and Decoder must be compatible in terms of:

  • Fixed Size: If both are fixed-size, they must have the same fixedSize value.
  • Variable Size: If either has a maxSize attribute, it must match the other.

If these conditions are not met, a SolanaError will be thrown.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TToThe type of the decoded value.

Parameters

ParameterTypeDescription
encoderVariableSizeEncoder<TFrom>The Encoder to combine.
decoderVariableSizeDecoder<TTo>The Decoder to combine.

Returns

VariableSizeCodec<TFrom, TTo>

A Codec that provides both encode and decode methods.

Throws

  • SOLANA_ERROR__CODECS__ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH Thrown if the encoder and decoder have mismatched size types (fixed vs. variable).
  • SOLANA_ERROR__CODECS__ENCODER_DECODER_FIXED_SIZE_MISMATCH Thrown if both are fixed-size but have different fixedSize values.
  • SOLANA_ERROR__CODECS__ENCODER_DECODER_MAX_SIZE_MISMATCH Thrown if the maxSize attributes do not match.

Examples

Creating a fixed-size Codec from an encoder and a decoder.

const encoder = getU32Encoder();
const decoder = getU32Decoder();
const codec = combineCodec(encoder, decoder);
 
const bytes = codec.encode(42); // 0x2a000000
const value = codec.decode(bytes); // 42

Creating a variable-size Codec from an encoder and a decoder.

const encoder = addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder());
const decoder = addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder());
const codec = combineCodec(encoder, decoder);
 
const bytes = codec.encode("hello"); // 0x0500000068656c6c6f
const value = codec.decode(bytes); // "hello"

Remarks

The recommended pattern for defining codecs in libraries is to expose separate functions for the encoder, decoder, and codec. This allows users to import only what they need, improving tree-shaking efficiency.

type MyType = /* ... */;
const getMyTypeEncoder = (): Encoder<MyType> => { /* ... */ };
const getMyTypeDecoder = (): Decoder<MyType> => { /* ... */ };
const getMyTypeCodec = (): Codec<MyType> =>
    combineCodec(getMyTypeEncoder(), getMyTypeDecoder());

See

Call Signature

combineCodec<TFrom, TTo>(encoder, decoder): Codec<TFrom, TTo>

Combines an Encoder and a Decoder into a Codec.

That is, given a Encoder<TFrom> and a Decoder<TTo>, this function returns a Codec<TFrom, TTo>.

This allows for modular composition by keeping encoding and decoding logic separate while still offering a convenient way to bundle them into a single Codec. This is particularly useful for library maintainers who want to expose Encoders, Decoders, and Codecs separately, enabling tree-shaking of unused logic.

The provided Encoder and Decoder must be compatible in terms of:

  • Fixed Size: If both are fixed-size, they must have the same fixedSize value.
  • Variable Size: If either has a maxSize attribute, it must match the other.

If these conditions are not met, a SolanaError will be thrown.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TToThe type of the decoded value.

Parameters

ParameterTypeDescription
encoderEncoder<TFrom>The Encoder to combine.
decoderDecoder<TTo>The Decoder to combine.

Returns

Codec<TFrom, TTo>

A Codec that provides both encode and decode methods.

Throws

  • SOLANA_ERROR__CODECS__ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH Thrown if the encoder and decoder have mismatched size types (fixed vs. variable).
  • SOLANA_ERROR__CODECS__ENCODER_DECODER_FIXED_SIZE_MISMATCH Thrown if both are fixed-size but have different fixedSize values.
  • SOLANA_ERROR__CODECS__ENCODER_DECODER_MAX_SIZE_MISMATCH Thrown if the maxSize attributes do not match.

Examples

Creating a fixed-size Codec from an encoder and a decoder.

const encoder = getU32Encoder();
const decoder = getU32Decoder();
const codec = combineCodec(encoder, decoder);
 
const bytes = codec.encode(42); // 0x2a000000
const value = codec.decode(bytes); // 42

Creating a variable-size Codec from an encoder and a decoder.

const encoder = addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder());
const decoder = addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder());
const codec = combineCodec(encoder, decoder);
 
const bytes = codec.encode("hello"); // 0x0500000068656c6c6f
const value = codec.decode(bytes); // "hello"

Remarks

The recommended pattern for defining codecs in libraries is to expose separate functions for the encoder, decoder, and codec. This allows users to import only what they need, improving tree-shaking efficiency.

type MyType = /* ... */;
const getMyTypeEncoder = (): Encoder<MyType> => { /* ... */ };
const getMyTypeDecoder = (): Decoder<MyType> => { /* ... */ };
const getMyTypeCodec = (): Codec<MyType> =>
    combineCodec(getMyTypeEncoder(), getMyTypeDecoder());

See

On this page