@solana/codecs
TypeScript icon, indicating that this package has built-in type declarations

2.0.0-experimental.f2a2e5b • Public • Published

npm npm-downloads semantic-release
code-style-prettier

@solana/codecs

This package contains all types and helpers for encoding and decoding anything to and from a Uint8Array. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK @solana/web3.js@experimental.

No matter which serialization strategy we use, Codecs abstract away its implementation and offers a simple encode and decode interface. Codecs are also highly composable, allowing us to build complex data structures from simple building blocks.

Here's a quick example that encodes and decodes a simple Person object.

// Use composable codecs to build complex data structures.
type Person = { name: string; age: number };
const getPersonCodec = (): Codec<Person> =>
    getStructCodec([
        ['name', getStringCodec()],
        ['age', getU32Codec()],
    ]);

// Use your own codecs to encode and decode data.
const person = { name: 'John', age: 42 };
const personCodec = getPersonCodec();
const encodedPerson: Uint8Array = personCodec.encode(person);
const decodedPerson: Person = personCodec.decode(encodedPerson);

Whilst Codecs can both encode and decode, it is possible to only focus on encoding or decoding data, enabling the unused logic to be tree-shaken. For instance, here’s our previous example using Decoders only to decode a Person type.

const getPersonDecoder = (): Decoder<Person> =>
    getStructDecoder([
        ['name', getStringDecoder()],
        ['age', getU32Decoder()],
    ]);

The @solana/codecs package is composed of several smaller packages, each with its own set of responsibilities. You can learn more about codecs and how to create your own by reading their respective documentation.

Readme

Keywords

Package Sidebar

Install

npm i @solana/codecs

Weekly Downloads

36,583

Version

2.0.0-experimental.f2a2e5b

License

MIT

Unpacked Size

28.2 kB

Total Files

21

Last publish

Collaborators

  • mcintyre94
  • 2501babe
  • lorisleiva
  • buffalojoec
  • ngundotra
  • _chido
  • steveluscher
  • sebastianbor
  • seanyoung
  • mvines
  • tyera
  • joncinque
  • ryoqun
  • trent-solana
  • jordansexton