Spaces:
Sleeping
Sleeping
File size: 29,338 Bytes
6655e35 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | [![npm][npm-image]][npm-url]
[![npm-downloads][npm-downloads-image]][npm-url]
[![semantic-release][semantic-release-image]][semantic-release-url]
<br />
[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]
[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
[code-style-prettier-url]: https://github.com/prettier/prettier
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/codecs-data-structures/rc.svg?style=flat
[npm-image]: https://img.shields.io/npm/v/@solana/codecs-data-structures/rc.svg?style=flat
[npm-url]: https://www.npmjs.com/package/@solana/codecs-data-structures/v/rc
[semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-release-url]: https://github.com/semantic-release/semantic-release
# @solana/codecs-data-structures
This package contains codecs for various data structures such as arrays, maps, structs, tuples, enums, etc. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@rc`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library).
This package is also part of the [`@solana/codecs` package](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs) which acts as an entry point for all codec packages as well as for their documentation.
## Array codec
The `getArrayCodec` function accepts any codec of type `T` and returns a codec of type `Array<T>`. For instance, here’s how we can create a codec for arrays of numbers that each fit in a single byte.
```ts
const bytes = getArrayCodec(getU8Codec()).encode([1, 2, 3]);
const array = getArrayCodec(getU8Codec()).decode(bytes);
```
By default, the size of the array is stored as a `u32` prefix before encoding the items.
```ts
getArrayCodec(getU8Codec()).encode([1, 2, 3]);
// 0x03000000010203
// | └-- 3 items of 1 byte each.
// └-- 4-byte prefix telling us to read 3 items.
```
However, you may use the `size` option to configure this behaviour. It can be one of the following three strategies:
- `Codec<number>`: When a number codec is provided, that codec will be used to encode and decode the size prefix.
- `number`: When a number is provided, the codec will expect a fixed number of items in the array. An error will be thrown when trying to encode an array of a different length.
- `"remainder"`: When the string `"remainder"` is passed as a size, the codec will use the remainder of the bytes to encode/decode its items. This means the size is not stored or known in advance but simply inferred from the rest of the buffer. For instance, if we have an array of `u16` numbers and 10 bytes remaining, we know there are 5 items in this array.
```ts
getArrayCodec(getU8Codec(), { size: getU16Codec() }).encode([1, 2, 3]);
// 0x0300010203
// | └-- 3 items of 1 byte each.
// └-- 2-byte prefix telling us to read 3 items.
getArrayCodec(getU8Codec(), { size: 3 }).encode([1, 2, 3]);
// 0x010203
// └-- 3 items of 1 byte each. There must always be 3 items in the array.
getArrayCodec(getU8Codec(), { size: 'remainder' }).encode([1, 2, 3]);
// 0x010203
// └-- 3 items of 1 byte each. The size is inferred from the remainder of the bytes.
```
Separate `getArrayEncoder` and `getArrayDecoder` functions are also available.
```ts
const bytes = getArrayEncoder(getU8Encoder()).encode([1, 2, 3]);
const array = getArrayDecoder(getU8Decoder()).decode(bytes);
```
## Set codec
The `getSetCodec` function accepts any codec of type `T` and returns a codec of type `Set<T>`. For instance, here’s how we can create a codec for sets of numbers that each fit in a single byte.
```ts
const bytes = getSetCodec(getU8Codec()).encode(new Set([1, 2, 3]));
const set = getSetCodec(getU8Codec()).decode(bytes);
```
Just like the array codec, it uses a `u32` size prefix by default but can be configured using the `size` option. [See the array codec](#array-codec) for more details.
```ts
getSetCodec(getU8Codec(), { size: getU16Codec() }).encode(new Set([1, 2, 3]));
getSetCodec(getU8Codec(), { size: 3 }).encode(new Set([1, 2, 3]));
getSetCodec(getU8Codec(), { size: 'remainder' }).encode(new Set([1, 2, 3]));
```
Separate `getSetEncoder` and `getSetDecoder` functions are also available.
```ts
const bytes = getSetEncoder(getU8Encoder()).encode(new Set([1, 2, 3]));
const set = getSetDecoder(getU8Decoder()).decode(bytes);
```
## Map codec
The `getMapCodec` function accepts two codecs of type `K` and `V` and returns a codec of type `Map<K, V>`. For instance, here’s how we can create a codec for maps such that the keys are fixed strings of 8 bytes and the values are `u8` numbers.
```ts
const keyCodec = fixCodecSize(getUtf8Codec(), 8);
const valueCodec = getU8Codec();
const bytes = getMapCodec(keyCodec, valueCodec).encode(new Map([['alice', 42]]));
const map = getMapCodec(keyCodec, valueCodec).decode(bytes);
```
Just like the array codec, it uses a `u32` size prefix by default.
```ts
const keyCodec = fixCodecSize(getUtf8Codec(), 8);
const valueCodec = getU8Codec();
const myMap = new Map<string, number>();
myMap.set('alice', 42);
myMap.set('bob', 5);
getMapCodec(keyCodec, valueCodec).encode(myMap);
// 0x02000000616c6963650000002a626f62000000000005
// | | | | └-- 2nd entry value (5).
// | | | └-- 2nd entry key ("bob").
// | | └-- 1st entry value (42).
// | └-- 1st entry key ("alice").
// └-- 4-byte prefix telling us to read 2 map entries.
```
However, it can be configured using the `size` option. [See the `size` option of the array codec](#array-codec) for more details.
```ts
getMapCodec(keyCodec, valueCodec, { size: getU16Codec() }).encode(myMap);
getMapCodec(keyCodec, valueCodec, { size: 3 }).encode(myMap);
getMapCodec(keyCodec, valueCodec, { size: 'remainder' }).encode(myMap);
```
Separate `getMapEncoder` and `getMapDecoder` functions are also available.
```ts
const bytes = getMapEncoder(keyEncoder, valueEncoder).encode(myMap);
const map = getMapDecoder(keyDecoder, valueDecoder).decode(bytes);
```
## Tuple codec
The `getTupleCodec` function accepts any number of codecs — `T`, `U`, `V`, etc. — and returns a tuple codec of type `[T, U, V, …]` such that each item is in the order of the provided codecs.
```ts
const codec = getTupleCodec([addCodecSizePrefix(getUtf8Codec(), getU32Codec()), getU8Codec(), getU64Codec()]);
const bytes = codec.encode(['alice', 42, 123]);
const tuple = codec.decode(bytes);
```
Separate `getTupleEncoder` and `getTupleDecoder` functions are also available.
```ts
const bytes = getTupleEncoder([getU8Encoder(), getU64Encoder()]).encode([42, 123]);
const tuple = getTupleDecoder([getU8Decoder(), getU64Decoder()]).decode(bytes);
```
## Struct codec
The `getStructCodec` function accepts any number of field codecs and returns a codec for an object containing all these fields. Each provided field is an array such that the first item is the name of the field and the second item is the codec used to encode and decode that field type.
```ts
type Person = { name: string; age: number };
const personCodec: Codec<Person> = getStructCodec([
['name', addCodecSizePrefix(getUtf8Codec(), getU32Codec())],
['age', getU8Codec()],
]);
const bytes = personCodec.encode({ name: 'alice', age: 42 });
const person = personCodec.decode(bytes);
```
Separate `getStructEncoder` and `getStructDecoder` functions are also available.
```ts
const personEncoder: Encoder<Person> = getStructEncoder([
['name', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
['age', getU8Encoder()],
]);
const personDecoder: Decoder<Person> = getStructDecoder([
['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
['age', getU8Decoder()],
]);
const bytes = personEncoder.encode({ name: 'alice', age: 42 });
const person = personDecoder.decode(bytes);
```
## Enum codec
The `getEnumCodec` function accepts a JavaScript enum constructor and returns a codec for encoding and decoding values of that enum.
```ts
enum Direction {
Left,
Right,
}
const bytes = getEnumCodec(Direction).encode(Direction.Left);
const direction = getEnumCodec(Direction).decode(bytes);
```
When encoding an enum, you may either provide the value of the enum variant — e.g. `Direction.Left` — or its key — e.g. `'Left'`.
```ts
enum Direction {
Left,
Right,
}
getEnumCodec(Direction).encode(Direction.Left); // 0x00
getEnumCodec(Direction).encode(Direction.Right); // 0x01
getEnumCodec(Direction).encode('Left'); // 0x00
getEnumCodec(Direction).encode('Right'); // 0x01
```
As you can see, by default, a `u8` number is being used to store the enum value. However, a number codec may be passed as the `size` option to configure that behaviour.
```ts
const u32DirectionCodec = getEnumCodec(Direction, { size: getU32Codec() });
u32DirectionCodec.encode(Direction.Left); // 0x00000000
u32DirectionCodec.encode(Direction.Right); // 0x01000000
```
This function also works with lexical enums — e.g. `enum Direction { Left = '←' }` — explicit numerical enums — e.g. `enum Speed { Left = 50 }` — and hybrid enums with a mix of both.
```ts
enum Numbers {
One,
Five = 5,
Six,
Nine = 'nine',
}
getEnumCodec(Numbers).encode(Direction.One); // 0x00
getEnumCodec(Numbers).encode(Direction.Five); // 0x01
getEnumCodec(Numbers).encode(Direction.Six); // 0x02
getEnumCodec(Numbers).encode(Direction.Nine); // 0x03
getEnumCodec(Numbers).encode('One'); // 0x00
getEnumCodec(Numbers).encode('Five'); // 0x01
getEnumCodec(Numbers).encode('Six'); // 0x02
getEnumCodec(Numbers).encode('Nine'); // 0x03
```
Notice how, by default, the index of the enum variant is used to encode the value of the enum. For instance, in the example above, `Numbers.Five` is encoded as `0x01` even though its value is `5`. This is also true for lexical enums.
However, when dealing with numerical enums that have explicit values, you may use the `useValuesAsDiscriminators` option to encode the value of the enum variant instead of its index.
```ts
enum Numbers {
One,
Five = 5,
Six,
Nine = 9,
}
const codec = getEnumCodec(Numbers, { useValuesAsDiscriminators: true });
codec.encode(Direction.One); // 0x00
codec.encode(Direction.Five); // 0x05
codec.encode(Direction.Six); // 0x06
codec.encode(Direction.Nine); // 0x09
codec.encode('One'); // 0x00
codec.encode('Five'); // 0x05
codec.encode('Six'); // 0x06
codec.encode('Nine'); // 0x09
```
Note that when using the `useValuesAsDiscriminators` option on an enum that contains a lexical value, an error will be thrown.
```ts
enum Lexical {
One,
Two = 'two',
}
getEnumCodec(Lexical, { useValuesAsDiscriminators: true }); // Throws an error.
```
Separate `getEnumEncoder` and `getEnumDecoder` functions are also available.
```ts
const bytes = getEnumEncoder(Direction).encode(Direction.Left);
const direction = getEnumDecoder(Direction).decode(bytes);
```
## Literal union codec
The `getLiteralUnionCodec` function works similarly to the `getUnionCodec` function but does not require a JavaScript `enum` to exist.
It accepts an array of literal values — such as `string`, `number`, `boolean`, etc. — and returns a codec that encodes and decodes such values using by using their index in the array. It uses TypeScript unions to represent all the possible values.
```ts
const codec = getLiteralUnionCodec(['left', 'right', 'up', 'down']);
// ^? FixedSizeCodec<"left" | "right" | "up" | "down">
const bytes = codec.encode('left'); // 0x00
const value = codec.decode(bytes); // 'left'
```
As you can see, it uses a `u8` number by default to store the index of the value. However, you may provide a number codec as the `size` option of the `getLiteralUnionCodec` function to customise that behaviour.
```ts
const codec = getLiteralUnionCodec(['left', 'right', 'up', 'down'], {
size: getU32Codec(),
});
codec.encode('left'); // 0x00000000
codec.encode('right'); // 0x01000000
codec.encode('up'); // 0x02000000
codec.encode('down'); // 0x03000000
```
Separate `getLiteralUnionEncoder` and `getLiteralUnionDecoder` functions are also available.
```ts
const bytes = getLiteralUnionEncoder(['left', 'right']).encode('left'); // 0x00
const value = getLiteralUnionDecoder(['left', 'right']).decode(bytes); // 'left'
```
## Discriminated union codec
In Rust, enums are powerful data types whose variants can be one of the following:
- An empty variant — e.g. `enum Message { Quit }`.
- A tuple variant — e.g. `enum Message { Write(String) }`.
- A struct variant — e.g. `enum Message { Move { x: i32, y: i32 } }`.
Whilst we do not have such powerful enums in JavaScript, we can emulate them in TypeScript using a union of objects such that each object is differentiated by a specific field. **We call this a discriminated union**.
We use a special field named `__kind` to distinguish between the different variants of a discriminated union. Additionally, since all variants are objects, we can use a `fields` property to wrap the array of tuple variants. Here is an example.
```ts
type Message =
| { __kind: 'Quit' } // Empty variant.
| { __kind: 'Write'; fields: [string] } // Tuple variant.
| { __kind: 'Move'; x: number; y: number }; // Struct variant.
```
The `getDiscriminatedUnionCodec` function helps us encode and decode these discriminated unions.
It requires the discriminator and codec of each variant as a first argument. Similarly to the struct codec, these are defined as an array of variant tuples where the first item is the discriminator of the variant and the second item is its codec. Since empty variants do not have data to encode, they simply use the unit codec — documented below — which does nothing.
Here is how we can create a discriminated union codec for our previous example.
```ts
const messageCodec = getDiscriminatedUnionCodec([
// Empty variant.
['Quit', getUnitCodec()],
// Tuple variant.
['Write', getStructCodec([['fields', getTupleCodec([addCodecSizePrefix(getUtf8Codec(), getU32Codec())])]])],
// Struct variant.
[
'Move',
getStructCodec([
['x', getI32Codec()],
['y', getI32Codec()],
]),
],
]);
```
And here’s how we can use such a codec to encode discriminated unions. Notice that by default, they use a `u8` number prefix to distinguish between the different types of variants.
```ts
messageCodec.encode({ __kind: 'Quit' });
// 0x00
// └-- 1-byte discriminator (Index 0 — the "Quit" variant).
messageCodec.encode({ __kind: 'Write', fields: ['Hi'] });
// 0x01020000004869
// | | └-- utf8 string content ("Hi").
// | └-- u32 string prefix (2 characters).
// └-- 1-byte discriminator (Index 1 — the "Write" variant).
messageCodec.encode({ __kind: 'Move', x: 5, y: 6 });
// 0x020500000006000000
// | | └-- Field y (6).
// | └-- Field x (5).
// └-- 1-byte discriminator (Index 2 — the "Move" variant).
```
However, you may provide a number codec as the `size` option of the `getDiscriminatedUnionCodec` function to customise that behaviour.
```ts
const u32MessageCodec = getDiscriminatedUnionCodec([...], {
size: getU32Codec(),
});
u32MessageCodec.encode({ __kind: 'Quit' });
// 0x00000000
// └------┘ 4-byte discriminator (Index 0).
u32MessageCodec.encode({ __kind: 'Write', fields: ['Hi'] });
// 0x01000000020000004869
// └------┘ 4-byte discriminator (Index 1).
u32MessageCodec.encode({ __kind: 'Move', x: 5, y: 6 });
// 0x020000000500000006000000
// └------┘ 4-byte discriminator (Index 2).
```
You may also customize the discriminator property — which defaults to `__kind` — by providing the desired property name as the `discriminator` option like so:
```ts
const messageCodec = getDiscriminatedUnionCodec([...], {
discriminator: 'message',
});
messageCodec.encode({ message: 'Quit' });
messageCodec.encode({ message: 'Write', fields: ['Hi'] });
messageCodec.encode({ message: 'Move', x: 5, y: 6 });
```
Note that, the discriminator value of a variant may be any scalar value — such as `number`, `bigint`, `boolean`, a JavaScript `enum`, etc. For instance, the following is also valid:
```ts
enum Message {
Quit,
Write,
Move,
}
const messageCodec = getDiscriminatedUnionCodec([
[Message.Quit, getUnitCodec()],
[Message.Write, getStructCodec([...])],
[Message.Move, getStructCodec([...])],
]);
codec.encode({ __kind: Message.Quit });
codec.encode({ __kind: Message.Write, fields: ['Hi'] });
codec.encode({ __kind: Message.Move, x: 5, y: 6 });
```
Finally, note that separate `getDiscriminatedUnionEncoder` and `getDiscriminatedUnionDecoder` functions are available.
```ts
const bytes = getDiscriminatedUnionEncoder(variantEncoders).encode({ __kind: 'Quit' });
const message = getDiscriminatedUnionDecoder(variantDecoders).decode(bytes);
```
## Union codec
The `getUnionCodec` is a lower-lever codec helper that can be used to encode/decode any TypeScript union.
It accepts the following arguments:
- An array of codecs, each defining a variant of the union.
- A `getIndexFromValue` function which, given a value of the union, returns the index of the codec that should be used to encode that value.
- A `getIndexFromBytes` function which, given the byte array to decode at a given offset, returns the index of the codec that should be used to decode the next bytes.
```ts
const codec: Codec<number | boolean> = getUnionCodec(
[getU16Codec(), getBooleanCodec()],
value => (typeof value === 'number' ? 0 : 1),
(bytes, offset) => (bytes.slice(offset).length > 1 ? 0 : 1),
);
codec.encode(42); // 0x2a00
codec.encode(true); // 0x01
```
As usual, separate `getUnionEncoder` and `getUnionDecoder` functions are also available.
```ts
const bytes = getUnionEncoder(encoders, getIndexFromValue).encode(42);
const value = getUnionDecoder(decoders, getIndexFromBytes).decode(bytes);
```
## Boolean codec
The `getBooleanCodec` function returns a `Codec<boolean>` that stores the boolean as `0` or `1` using a `u8` number by default.
```ts
const bytes = getBooleanCodec().encode(true); // 0x01
const value = getBooleanCodec().decode(bytes); // true
```
You may configure that behaviour by providing an explicit number codec as the `size` option of the `getBooleanCodec` function. That number codec will then be used to encode and decode the values `0` and `1` accordingly.
```ts
getBooleanCodec({ size: getU16Codec() }).encode(false); // 0x0000
getBooleanCodec({ size: getU16Codec() }).encode(true); // 0x0100
getBooleanCodec({ size: getU32Codec() }).encode(false); // 0x00000000
getBooleanCodec({ size: getU32Codec() }).encode(true); // 0x01000000
```
Separate `getBooleanEncoder` and `getBooleanDecoder` functions are also available.
```ts
const bytes = getBooleanEncoder().encode(true); // 0x01
const value = getBooleanDecoder().decode(bytes); // true
```
## Nullable codec
The `getNullableCodec` function accepts a codec of type `T` and returns a codec of type `T | null`. It stores whether or not the item exists as a boolean prefix using a `u8` by default.
```ts
const stringCodec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
getNullableCodec(stringCodec).encode('Hi');
// 0x01020000004869
// | | └-- utf8 string content ("Hi").
// | └-- u32 string prefix (2 characters).
// └-- 1-byte prefix (true — The item exists).
getNullableCodec(stringCodec).encode(null);
// 0x00
// └-- 1-byte prefix (false — The item is null).
```
You may provide a number codec as the `prefix` option of the `getNullableCodec` function to configure how to store the boolean prefix.
```ts
const u32NullableStringCodec = getNullableCodec(stringCodec, {
prefix: getU32Codec(),
});
u32NullableStringCodec.encode('Hi');
// 0x01000000020000004869
// └------┘ 4-byte prefix (true).
u32NullableStringCodec.encode(null);
// 0x00000000
// └------┘ 4-byte prefix (false).
```
Additionally, if the item is a `FixedSizeCodec`, you may set the `noneValue` option to `"zeroes"` to also make the returned nullable codec a `FixedSizeCodec`. To do so, it will pad `null` values with zeroes to match the length of existing values.
```ts
const fixedNullableStringCodec = getNullableCodec(
fixCodecSize(getUtf8Codec(), 8), // Only works with fixed-size items.
{ noneValue: 'zeroes' },
);
fixedNullableStringCodec.encode('Hi');
// 0x014869000000000000
// | └-- 8-byte utf8 string content ("Hi").
// └-- 1-byte prefix (true — The item exists).
fixedNullableStringCodec.encode(null);
// 0x000000000000000000
// | └-- 8-byte of padding to make a fixed-size codec.
// └-- 1-byte prefix (false — The item is null).
```
The `noneValue` option can also be set to an explicit byte array to use as the padding for `null` values. Note that, in this case, the returned codec will not be a `FixedSizeCodec` as the byte array representing `null` values may be of any length.
```ts
const codec = getNullableCodec(getUtf8Codec(), {
noneValue: new Uint8Array([255]), // 0xff means null.
});
codec.encode('Hi');
// 0x014869
// | └-- 2-byte utf8 string content ("Hi").
// └-- 1-byte prefix (true — The item exists).
codec.encode(null);
// 0x00ff
// | └-- 1-byte representing null (0xff).
// └-- 1-byte prefix (false — The item is null).
```
Last but not least, the `prefix` option of the `getNullableCodec` function can also be set to `null`, meaning no prefix will be used to determine whether the item exists. In this case, the codec will rely on the `noneValue` option to determine whether the item is `null`.
```ts
const codecWithZeroNoneValue = getNullableCodec(getU16Codec(), {
noneValue: 'zeroes', // 0x0000 means null.
prefix: null,
});
codecWithZeroNoneValue.encode(42); // 0x2a00
codecWithZeroNoneValue.encode(null); // 0x0000
const codecWithCustomNoneValue = getNullableCodec(getU16Codec(), {
noneValue: new Uint8Array([255]), // 0xff means null.
prefix: null,
});
codecWithCustomNoneValue.encode(42); // 0x2a00
codecWithCustomNoneValue.encode(null); // 0xff
```
Finally, note that if `prefix` is set to `null` and no `noneValue` is provided, the codec assumes that the item exists if and only if some remaining bytes are available to decode. This could be useful to describe data structures that may or may not have additional data to the end of the buffer.
```ts
const codec = getNullableCodec(getU16Codec(), { prefix: null });
codec.encode(42); // 0x2a00
codec.encode(null); // Encodes nothing.
codec.decode(new Uint8Array([42, 0])); // 42
codec.decode(new Uint8Array([])); // null
```
To recap, here are all the possible configurations of the `getNullableCodec` function, using a `u16` codec as an example.
| `encode(42)` / `encode(null)` | No `noneValue` (default) | `noneValue: "zeroes"` | Custom `noneValue` (`0xff`) |
| ----------------------------- | ------------------------ | --------------------------- | --------------------------- |
| `u8` prefix (default) | `0x012a00` / `0x00` | `0x012a00` / `0x000000` | `0x012a00` / `0x00ff` |
| Custom `prefix` (`u16`) | `0x01002a00` / `0x0000` | `0x01002a00` / `0x00000000` | `0x01002a00` / `0x0000ff` |
| No `prefix` | `0x2a00` / `0x` | `0x2a00` / `0x0000` | `0x2a00` / `0xff` |
Note that you might be interested in the Rust-like alternative version of nullable codecs, available in [the `@solana/options` package](https://github.com/solana-labs/solana-web3.js/tree/master/packages/options).
Separate `getNullableEncoder` and `getNullableDecoder` functions are also available.
```ts
const bytes = getNullableEncoder(getU32Encoder()).encode(42);
const value = getNullableDecoder(getU32Decoder()).decode(bytes);
```
## Bytes codec
The `getBytesCodec` function returns a `Codec<Uint8Array>` meaning it converts `Uint8Arrays` to and from… `Uint8Arrays`! Whilst this might seem a bit useless, it can be useful when composed into other codecs. For example, you could use it in a struct codec to say that a particular field should be left unserialised.
```ts
const bytes = getBytesCodec().encode(new Uint8Array([42])); // 0x2a
const value = getBytesCodec().decode(bytes); // 0x2a
```
The `getBytesCodec` function will encode and decode `Uint8Arrays` using as much bytes as necessary. If you'd like to restrict the number of bytes used by this codec, you may combine it with the [`fixCodecSize`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs-core#fixing-the-size-of-codecs) or [`addCodecSizePrefix`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs-core#prefixing-the-size-of-codecs) primitives.
Here are some examples of how you might use the `getBytesCodec` function.
```ts
// Variable size.
getBytesCodec().encode(new Uint8Array([42]));
// 0x2a
// └-- Uint8Array content using any bytes available.
// Prefixing the size with a 2-byte u16.
addCodecSizePrefix(getBytesCodec(), getU16Codec()).encode(new Uint8Array([42]));
// 0x01002a
// | └-- Uint8Array content.
// └-- 2-byte prefix telling us to read 1 bytes
// Fixing the size to 5 bytes.
fixCodecSize(getBytesCodec(), 5).encode(new Uint8Array([42]));
// 0x2a00000000
// └-- Uint8Array content padded to use exactly 5 bytes.
```
Separate `getBytesEncoder` and `getBytesDecoder` functions are also available.
```ts
const bytes = getBytesEncoder().encode(new Uint8Array([42]));
const value = getBytesDecoder().decode(bytes);
```
## Bit array codec
The `getBitArrayCodec` function returns a codec that encodes and decodes an array of booleans such that each boolean is represented by a single bit. It requires the size of the codec in bytes and an optional `backward` flag that can be used to reverse the order of the bits.
```ts
const booleans = [true, false, true, false, true, false, true, false];
getBitArrayCodec(1).encode(booleans);
// 0xaa or 0b10101010
getBitArrayCodec(1, { backward: true }).encode(booleans);
// 0x55 or 0b01010101
```
Separate `getBitArrayEncoder` and `getBitArrayDecoder` functions are also available.
```ts
const bytes = getBitArrayEncoder(1).encode(booleans);
const decodedBooleans = getBitArrayDecoder(1).decode(bytes);
```
## Constant codec
The `getConstantCodec` function accepts any `Uint8Array` and returns a `Codec<void>`. When encoding, it will set the provided `Uint8Array` as-is. When decoding, it will assert that the next bytes contain the provided `Uint8Array` and move the offset forward.
```ts
const codec = getConstantCodec(new Uint8Array([1, 2, 3]));
codec.encode(undefined); // 0x010203
codec.decode(new Uint8Array([1, 2, 3])); // undefined
codec.decode(new Uint8Array([1, 2, 4])); // Throws an error.
```
Separate `getConstantEncoder` and `getConstantDecoder` functions are also available.
```ts
getConstantEncoder(new Uint8Array([1, 2, 3])).encode(undefined);
getConstantDecoder(new Uint8Array([1, 2, 3])).decode(new Uint8Array([1, 2, 3]));
```
## Unit codec
The `getUnitCodec` function returns a `Codec<void>` that encodes `undefined` into an empty `Uint8Array` and returns `undefined` without consuming any bytes when decoding. This is more of a low-level codec that can be used internally by other codecs. For instance, this is how discriminated union codecs describe the codecs of empty variants.
```ts
getUnitCodec().encode(undefined); // Empty Uint8Array
getUnitCodec().decode(anyBytes); // undefined
```
Separate `getUnitEncoder` and `getUnitDecoder` functions are also available.
```ts
getUnitEncoder().encode(undefined);
getUnitDecoder().decode(anyBytes);
```
---
To read more about the available codecs and how to use them, check out the documentation of the main [`@solana/codecs` package](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs).
## Hidden prefix and suffix codec
The `getHiddenPrefixCodec` and `getHiddenSuffixCodec` functions allow us to respectively prepend or append a list of hidden `Codec<void>` to a given codec. When encoding, the hidden codecs will be encoded before or after the main codec and the offset will be moved accordingly. When decoding, the hidden codecs will be decoded but only the result of the main codec will be returned. This is particularly helpful when creating data structures that include constant values that should not be included in the final type.
```ts
const codec: Codec<number> = getHiddenPrefixCodec(getU16Codec(), [
getConstantCodec(new Uint8Array([1, 2, 3])),
getConstantCodec(new Uint8Array([4, 5, 6])),
]);
codec.encode(42);
// 0x0102030405062a00
// | | └-- Our main u16 codec (value = 42).
// | └-- Our second hidden prefix codec.
// └-- Our first hidden prefix codec.
codec.decode(new Uint8Array([1, 2, 3, 4, 5, 6, 42, 0])); // 42
```
As usual, separate encoder and decoder functions are also available.
```ts
getHiddenPrefixEncoder(encoder, prefixedEncoders);
getHiddenPrefixEncoder(decoder, prefixedDecoders);
getHiddenSuffixEncoder(encoder, suffixedEncoders);
getHiddenSuffixEncoder(decoder, suffixedDecoders);
```
|