@solana/codecs-data-structures 2.0.0-experimental.f7d1af1 → 2.0.0-experimental.f8c941c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +482 -4
- package/dist/index.browser.cjs +370 -478
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +372 -476
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +372 -476
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +370 -478
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +372 -476
- package/dist/index.node.js.map +1 -1
- package/dist/types/array.d.ts +39 -10
- package/dist/types/array.d.ts.map +1 -1
- package/dist/types/bit-array.d.ts +9 -9
- package/dist/types/bit-array.d.ts.map +1 -1
- package/dist/types/boolean.d.ts +22 -10
- package/dist/types/boolean.d.ts.map +1 -1
- package/dist/types/bytes.d.ts +18 -9
- package/dist/types/bytes.d.ts.map +1 -1
- package/dist/types/data-enum.d.ts +20 -20
- package/dist/types/data-enum.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -14
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/map.d.ts +28 -10
- package/dist/types/map.d.ts.map +1 -1
- package/dist/types/nullable.d.ts +28 -10
- package/dist/types/nullable.d.ts.map +1 -1
- package/dist/types/scalar-enum.d.ts +22 -10
- package/dist/types/scalar-enum.d.ts.map +1 -1
- package/dist/types/set.d.ts +28 -10
- package/dist/types/set.d.ts.map +1 -1
- package/dist/types/struct.d.ts +28 -18
- package/dist/types/struct.d.ts.map +1 -1
- package/dist/types/tuple.d.ts +22 -15
- package/dist/types/tuple.d.ts.map +1 -1
- package/dist/types/unit.d.ts +4 -12
- package/dist/types/unit.d.ts.map +1 -1
- package/dist/types/utils.d.ts +10 -2
- package/dist/types/utils.d.ts.map +1 -1
- package/package.json +10 -10
- package/dist/index.development.js +0 -865
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -51
- package/dist/types/array-like-codec-size.d.ts +0 -20
- package/dist/types/array-like-codec-size.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -16,10 +16,488 @@
|
|
|
16
16
|
|
|
17
17
|
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@experimental`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library).
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
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.
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
## Array codec
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
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.
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
```ts
|
|
26
|
+
const bytes = getArrayCodec(getU8Codec()).encode([1, 2, 3]);
|
|
27
|
+
const array = getArrayCodec(getU8Codec()).decode(bytes);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
By default, the size of the array is stored as a `u32` prefix before encoding the items.
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
getArrayCodec(getU8Codec()).encode([1, 2, 3]);
|
|
34
|
+
// 0x03000000010203
|
|
35
|
+
// | └-- 3 items of 1 byte each.
|
|
36
|
+
// └-- 4-byte prefix telling us to read 3 items.
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
However, you may use the `size` option to configure this behaviour. It can be one of the following three strategies:
|
|
40
|
+
|
|
41
|
+
- `Codec<number>`: When a number codec is provided, that codec will be used to encode and decode the size prefix.
|
|
42
|
+
- `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.
|
|
43
|
+
- `"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.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
getArrayCodec(getU8Codec(), { size: getU16Codec() }).encode([1, 2, 3]);
|
|
47
|
+
// 0x0300010203
|
|
48
|
+
// | └-- 3 items of 1 byte each.
|
|
49
|
+
// └-- 2-byte prefix telling us to read 3 items.
|
|
50
|
+
|
|
51
|
+
getArrayCodec(getU8Codec(), { size: 3 }).encode([1, 2, 3]);
|
|
52
|
+
// 0x010203
|
|
53
|
+
// └-- 3 items of 1 byte each. There must always be 3 items in the array.
|
|
54
|
+
|
|
55
|
+
getArrayCodec(getU8Codec(), { size: 'remainder' }).encode([1, 2, 3]);
|
|
56
|
+
// 0x010203
|
|
57
|
+
// └-- 3 items of 1 byte each. The size is inferred from the remainder of the bytes.
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Separate `getArrayEncoder` and `getArrayDecoder` functions are also available.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
const bytes = getArrayEncoder(getU8Encoder()).encode([1, 2, 3]);
|
|
64
|
+
const array = getArrayDecoder(getU8Decoder()).decode(bytes);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Set codec
|
|
68
|
+
|
|
69
|
+
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.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
const bytes = getSetCodec(getU8Codec()).encode(new Set([1, 2, 3]));
|
|
73
|
+
const set = getSetCodec(getU8Codec()).decode(bytes);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
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.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
getSetCodec(getU8Codec(), { size: getU16Codec() }).encode(new Set([1, 2, 3]));
|
|
80
|
+
getSetCodec(getU8Codec(), { size: 3 }).encode(new Set([1, 2, 3]));
|
|
81
|
+
getSetCodec(getU8Codec(), { size: 'remainder' }).encode(new Set([1, 2, 3]));
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Separate `getSetEncoder` and `getSetDecoder` functions are also available.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const bytes = getSetEncoder(getU8Encoder()).encode(new Set([1, 2, 3]));
|
|
88
|
+
const set = getSetDecoder(getU8Decoder()).decode(bytes);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Map codec
|
|
92
|
+
|
|
93
|
+
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.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const keyCodec = getStringCodec({ size: 8 });
|
|
97
|
+
const valueCodec = getU8Codec();
|
|
98
|
+
const bytes = getMapCodec(keyCodec, valueCodec).encode(new Map([['alice', 42]]));
|
|
99
|
+
const map = getMapCodec(keyCodec, valueCodec).decode(bytes);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Just like the array codec, it uses a `u32` size prefix by default.
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
const keyCodec = getStringCodec({ size: 8 });
|
|
106
|
+
const valueCodec = getU8Codec();
|
|
107
|
+
const myMap = new Map<string, number>();
|
|
108
|
+
myMap.set('alice', 42);
|
|
109
|
+
myMap.set('bob', 5);
|
|
110
|
+
|
|
111
|
+
getMapCodec(keyCodec, valueCodec).encode(myMap);
|
|
112
|
+
// 0x02000000616c6963650000002a626f62000000000005
|
|
113
|
+
// | | | | └-- 2nd entry value (5).
|
|
114
|
+
// | | | └-- 2nd entry key ("bob").
|
|
115
|
+
// | | └-- 1st entry value (42).
|
|
116
|
+
// | └-- 1st entry key ("alice").
|
|
117
|
+
// └-- 4-byte prefix telling us to read 2 map entries.
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
However, it can be configured using the `size` option. [See the `size` option of the array codec](#array-codec) for more details.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
getMapCodec(keyCodec, valueCodec, { size: getU16Codec() }).encode(myMap);
|
|
124
|
+
getMapCodec(keyCodec, valueCodec, { size: 3 }).encode(myMap);
|
|
125
|
+
getMapCodec(keyCodec, valueCodec, { size: 'remainder' }).encode(myMap);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Separate `getMapEncoder` and `getMapDecoder` functions are also available.
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
const bytes = getMapEncoder(keyEncoder, valueEncoder).encode(myMap);
|
|
132
|
+
const map = getMapDecoder(keyDecoder, valueDecoder).decode(bytes);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Tuple codec
|
|
136
|
+
|
|
137
|
+
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.
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
const itemCodecs = [getStringCodec(), getU8Codec(), getU64Codec()];
|
|
141
|
+
const bytes = getTupleCodec(itemCodecs).encode(['alice', 42, 123]);
|
|
142
|
+
const tuple = getTupleCodec(itemCodecs).decode(bytes);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Separate `getTupleEncoder` and `getTupleDecoder` functions are also available.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const bytes = getTupleEncoder(itemEncoders).encode(['alice', 42, 123]);
|
|
149
|
+
const tuple = getTupleDecoder(itemDecoders).decode(bytes);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Struct codec
|
|
153
|
+
|
|
154
|
+
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.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
type Person = { name: string; age: number };
|
|
158
|
+
const personCodec: Codec<Person> = getStructCodec([
|
|
159
|
+
['name', getStringCodec()],
|
|
160
|
+
['age', getU8Codec()],
|
|
161
|
+
]);
|
|
162
|
+
|
|
163
|
+
const bytes = personCodec.encode({ name: 'alice', age: 42 });
|
|
164
|
+
const person = personCodec.decode(bytes);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Separate `getStructEncoder` and `getStructDecoder` functions are also available.
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
const personEncoder: Encoder<Person> = getStructEncoder([
|
|
171
|
+
['name', getStringEncoder()],
|
|
172
|
+
['age', getU8Encoder()],
|
|
173
|
+
]);
|
|
174
|
+
const personDecoder: Decoder<Person> = getStructDecoder([
|
|
175
|
+
['name', getStringDecoder()],
|
|
176
|
+
['age', getU8Decoder()],
|
|
177
|
+
]);
|
|
178
|
+
const bytes = personEncoder.encode({ name: 'alice', age: 42 });
|
|
179
|
+
const person = personDecoder.decode(bytes);
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Scalar enum codec
|
|
183
|
+
|
|
184
|
+
The `getScalarEnumCodec` function accepts a JavaScript enum constructor and returns a codec for encoding and decoding values of that enum.
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
enum Direction {
|
|
188
|
+
Left,
|
|
189
|
+
Right,
|
|
190
|
+
Up,
|
|
191
|
+
Down,
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const bytes = getScalarEnumCodec(Direction).encode(Direction.Left);
|
|
195
|
+
const direction = getScalarEnumCodec(Direction).decode(bytes);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
When encoding a scalar enum, you may pass the value as an enum value, as a number or even as a string by passing the variant’s name.
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
enum Direction {
|
|
202
|
+
Left,
|
|
203
|
+
Right,
|
|
204
|
+
Up,
|
|
205
|
+
Down,
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
getScalarEnumCodec(Direction).encode(Direction.Left); // 0x00
|
|
209
|
+
getScalarEnumCodec(Direction).encode(Direction.Right); // 0x01
|
|
210
|
+
getScalarEnumCodec(Direction).encode(0); // 0x00
|
|
211
|
+
getScalarEnumCodec(Direction).encode(1); // 0x01
|
|
212
|
+
getScalarEnumCodec(Direction).encode('Left'); // 0x00
|
|
213
|
+
getScalarEnumCodec(Direction).encode('Right'); // 0x01
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
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.
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
const u32DirectionCodec = getScalarEnumCodec(Direction, { size: getU32Codec() });
|
|
220
|
+
u32DirectionCodec.encode(Direction.Left); // 0x00000000
|
|
221
|
+
u32DirectionCodec.encode(Direction.Right); // 0x01000000
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Note that if you provide a string enum — e.g. `enum Direction { Left = 'LEFT' }` — to the `getScalarEnumCodec` function, it will only store the index of the variant. However, the string value may be used to encode that index.
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
enum Direction {
|
|
228
|
+
Left = 'LEFT',
|
|
229
|
+
Right = 'RIGHT',
|
|
230
|
+
Up = 'UP',
|
|
231
|
+
Down = 'DOWN',
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
getScalarEnumCodec(Direction).encode(Direction.Right); // 0x01
|
|
235
|
+
getScalarEnumCodec(Direction).encode(1); // 0x01
|
|
236
|
+
getScalarEnumCodec(Direction).encode('Right'); // 0x01
|
|
237
|
+
getScalarEnumCodec(Direction).encode('RIGHT'); // 0x01
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Separate `getScalarEnumEncoder` and `getScalarEnumDecoder` functions are also available.
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
const bytes = getScalarEnumEncoder(Direction).encode(Direction.Left);
|
|
244
|
+
const direction = getScalarEnumDecoder(Direction).decode(bytes);
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Data enum codec
|
|
248
|
+
|
|
249
|
+
In Rust, enums are powerful data types whose variants can be one of the following:
|
|
250
|
+
|
|
251
|
+
- An empty variant — e.g. `enum Message { Quit }`.
|
|
252
|
+
- A tuple variant — e.g. `enum Message { Write(String) }`.
|
|
253
|
+
- A struct variant — e.g. `enum Message { Move { x: i32, y: i32 } }`.
|
|
254
|
+
|
|
255
|
+
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 data enum**.
|
|
256
|
+
|
|
257
|
+
We use a special field named `__kind` to distinguish between the different variants of a data enum. Additionally, since all variants are objects, we use a `fields` property to wrap the array of tuple variants. Here is an example.
|
|
258
|
+
|
|
259
|
+
```ts
|
|
260
|
+
type Message =
|
|
261
|
+
| { __kind: 'Quit' } // Empty variant.
|
|
262
|
+
| { __kind: 'Write'; fields: [string] } // Tuple variant.
|
|
263
|
+
| { __kind: 'Move'; x: number; y: number }; // Struct variant.
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
The `getDataEnumCodec` function helps us encode and decode these data enums.
|
|
267
|
+
|
|
268
|
+
It requires the name 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 name 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.
|
|
269
|
+
|
|
270
|
+
Here is how we can create a data enum codec for our previous example.
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
const messageCodec = getDataEnumCodec<Message>([
|
|
274
|
+
// Empty variant.
|
|
275
|
+
['Quit', getUnitCodec()],
|
|
276
|
+
|
|
277
|
+
// Tuple variant.
|
|
278
|
+
['Write', getStructCodec<{ fields: [string] }>([['fields', getTupleCodec([getStringCodec()])]])],
|
|
279
|
+
|
|
280
|
+
// Struct variant.
|
|
281
|
+
[
|
|
282
|
+
'Move',
|
|
283
|
+
getStructCodec<{ x: number; y: number }>([
|
|
284
|
+
['x', getI32Codec()],
|
|
285
|
+
['y', getI32Codec()],
|
|
286
|
+
]),
|
|
287
|
+
],
|
|
288
|
+
]);
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
And here’s how we can use such a codec to encode data enums. Notice that by default, they use a `u8` number prefix to distinguish between the different types of variants.
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
messageCodec.encode({ __kind: 'Quit' });
|
|
295
|
+
// 0x00
|
|
296
|
+
// └-- 1-byte discriminator (Index 0 — the "Quit" variant).
|
|
297
|
+
|
|
298
|
+
messageCodec.encode({ __kind: 'Write', fields: ['Hi'] });
|
|
299
|
+
// 0x01020000004869
|
|
300
|
+
// | | └-- utf8 string content ("Hi").
|
|
301
|
+
// | └-- u32 string prefix (2 characters).
|
|
302
|
+
// └-- 1-byte discriminator (Index 1 — the "Write" variant).
|
|
303
|
+
|
|
304
|
+
messageCodec.encode({ __kind: 'Move', x: 5, y: 6 });
|
|
305
|
+
// 0x020500000006000000
|
|
306
|
+
// | | └-- Field y (6).
|
|
307
|
+
// | └-- Field x (5).
|
|
308
|
+
// └-- 1-byte discriminator (Index 2 — the "Move" variant).
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
However, you may provide a number codec as the `size` option of the `getDataEnumCodec` function to customise that behaviour.
|
|
312
|
+
|
|
313
|
+
```ts
|
|
314
|
+
const u32MessageCodec = getDataEnumCodec<Message>([...], {
|
|
315
|
+
size: getU32Codec(),
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
u32MessageCodec.encode({ __kind: 'Quit' });
|
|
319
|
+
// 0x00000000
|
|
320
|
+
// └------┘ 4-byte discriminator (Index 0).
|
|
321
|
+
|
|
322
|
+
u32MessageCodec.encode({ __kind: 'Write', fields: ['Hi'] });
|
|
323
|
+
// 0x01000000020000004869
|
|
324
|
+
// └------┘ 4-byte discriminator (Index 1).
|
|
325
|
+
|
|
326
|
+
u32MessageCodec.encode({ __kind: 'Move', x: 5, y: 6 });
|
|
327
|
+
// 0x020000000500000006000000
|
|
328
|
+
// └------┘ 4-byte discriminator (Index 2).
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
Separate `getDataEnumEncoder` and `getDataEnumDecoder` functions are also available.
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
const bytes = getDataEnumEncoder(variantEncoders).encode({ __kind: 'Quit' });
|
|
335
|
+
const message = getDataEnumDecoder(variantDecoders).decode(bytes);
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Boolean codec
|
|
339
|
+
|
|
340
|
+
The `getBooleanCodec` function returns a `Codec<boolean>` that stores the boolean as `0` or `1` using a `u8` number by default.
|
|
341
|
+
|
|
342
|
+
```ts
|
|
343
|
+
const bytes = getBooleanCodec().encode(true); // 0x01
|
|
344
|
+
const value = getBooleanCodec().decode(bytes); // true
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
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.
|
|
348
|
+
|
|
349
|
+
```ts
|
|
350
|
+
getBooleanCodec({ size: getU16Codec() }).encode(false); // 0x0000
|
|
351
|
+
getBooleanCodec({ size: getU16Codec() }).encode(true); // 0x0100
|
|
352
|
+
|
|
353
|
+
getBooleanCodec({ size: getU32Codec() }).encode(false); // 0x00000000
|
|
354
|
+
getBooleanCodec({ size: getU32Codec() }).encode(true); // 0x01000000
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
Separate `getBooleanEncoder` and `getBooleanDecoder` functions are also available.
|
|
358
|
+
|
|
359
|
+
```ts
|
|
360
|
+
const bytes = getBooleanEncoder().encode(true); // 0x01
|
|
361
|
+
const value = getBooleanDecoder().decode(bytes); // true
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
## Nullable codec
|
|
365
|
+
|
|
366
|
+
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.
|
|
367
|
+
|
|
368
|
+
```ts
|
|
369
|
+
getNullableCodec(getStringCodec()).encode('Hi');
|
|
370
|
+
// 0x01020000004869
|
|
371
|
+
// | | └-- utf8 string content ("Hi").
|
|
372
|
+
// | └-- u32 string prefix (2 characters).
|
|
373
|
+
// └-- 1-byte prefix (true — The item exists).
|
|
374
|
+
|
|
375
|
+
getNullableCodec(getStringCodec()).encode(null);
|
|
376
|
+
// 0x00
|
|
377
|
+
// └-- 1-byte prefix (false — The item is null).
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
You may provide a number codec as the `prefix` option of the `getNullableCodec` function to configure how to store the boolean prefix.
|
|
381
|
+
|
|
382
|
+
```ts
|
|
383
|
+
const u32NullableStringCodec = getNullableCodec(getStringCodec(), {
|
|
384
|
+
prefix: getU32Codec(),
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
u32NullableStringCodec.encode('Hi');
|
|
388
|
+
// 0x01000000020000004869
|
|
389
|
+
// └------┘ 4-byte prefix (true).
|
|
390
|
+
|
|
391
|
+
u32NullableStringCodec.encode(null);
|
|
392
|
+
// 0x00000000
|
|
393
|
+
// └------┘ 4-byte prefix (false).
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
Additionally, if the item is a `FixedSizeCodec`, you may set the `fixed` option to `true` 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.
|
|
397
|
+
|
|
398
|
+
```ts
|
|
399
|
+
const fixedNullableStringCodec = getNullableCodec(
|
|
400
|
+
getStringCodec({ size: 8 }), // Only works with fixed-size items.
|
|
401
|
+
{ fixed: true },
|
|
402
|
+
);
|
|
403
|
+
|
|
404
|
+
fixedNullableStringCodec.encode('Hi');
|
|
405
|
+
// 0x014869000000000000
|
|
406
|
+
// | └-- 8-byte utf8 string content ("Hi").
|
|
407
|
+
// └-- 1-byte prefix (true — The item exists).
|
|
408
|
+
|
|
409
|
+
fixedNullableStringCodec.encode(null);
|
|
410
|
+
// 0x000000000000000000
|
|
411
|
+
// | └-- 8-byte of padding to make a fixed-size codec.
|
|
412
|
+
// └-- 1-byte prefix (false — The item is null).
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
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).
|
|
416
|
+
|
|
417
|
+
Separate `getNullableEncoder` and `getNullableDecoder` functions are also available.
|
|
418
|
+
|
|
419
|
+
```ts
|
|
420
|
+
const bytes = getNullableEncoder(getStringEncoder()).encode('Hi');
|
|
421
|
+
const value = getNullableDecoder(getStringDecoder()).decode(bytes);
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
## Bytes codec
|
|
425
|
+
|
|
426
|
+
The `getBytesCodec` function returns a `Codec<Uint8Array>` meaning it coverts `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.
|
|
427
|
+
|
|
428
|
+
```ts
|
|
429
|
+
const bytes = getBytesCodec().encode(new Uint8Array([42])); // 0x2a
|
|
430
|
+
const value = getBytesCodec().decode(bytes); // 0x2a
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
By default, when decoding a `Uint8Array`, all the remaining bytes will be used. However, the `getBytesCodec` function accepts a `size` option that allows us to configure how many bytes should be included in our decoded `Uint8Array`. It can be one of the following three strategies:
|
|
434
|
+
|
|
435
|
+
- `Codec<number>`: When a number codec is provided, that codec will be used to encode and decode a size prefix for that `Uint8Array`. This prefix allows us to know when to stop reading the `Uint8Array` when decoding an arbitrary byte array.
|
|
436
|
+
- `number`: When a fixed number is provided, a `FixedSizeCodec` of that size will be returned such that exactly that amount of bytes will be used to encode and decode the `Uint8Array`.
|
|
437
|
+
- `"variable"`: When the string `"variable"` is passed as a size, a `VariableSizeCodec` will be returned without any size boundary. This is the default behaviour.
|
|
438
|
+
|
|
439
|
+
```ts
|
|
440
|
+
// Default behaviour: variable size.
|
|
441
|
+
getBytesCodec().encode(new Uint8Array([42]));
|
|
442
|
+
// 0x2a
|
|
443
|
+
// └-- Uint8Array content using any bytes available.
|
|
444
|
+
|
|
445
|
+
// Custom size: u16 size.
|
|
446
|
+
getBytesCodec({ size: getU16Codec() }).encode(new Uint8Array([42]));
|
|
447
|
+
// 0x01002a
|
|
448
|
+
// | └-- Uint8Array content.
|
|
449
|
+
// └-- 2-byte prefix telling us to read 1 bytes
|
|
450
|
+
|
|
451
|
+
// Custom size: 5 bytes.
|
|
452
|
+
getBytesCodec({ size: 5 }).encode(new Uint8Array([42]));
|
|
453
|
+
// 0x2a00000000
|
|
454
|
+
// └-- Uint8Array content padded to use exactly 5 bytes.
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
Separate `getBytesEncoder` and `getBytesDecoder` functions are also available.
|
|
458
|
+
|
|
459
|
+
```ts
|
|
460
|
+
const bytes = getBytesEncoder().encode(new Uint8Array([42]));
|
|
461
|
+
const value = getBytesDecoder().decode(bytes);
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
## Bit array codec
|
|
465
|
+
|
|
466
|
+
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.
|
|
467
|
+
|
|
468
|
+
```ts
|
|
469
|
+
const booleans = [true, false, true, false, true, false, true, false];
|
|
470
|
+
|
|
471
|
+
getBitArrayCodec(1).encode(booleans);
|
|
472
|
+
// 0xaa or 0b10101010
|
|
473
|
+
|
|
474
|
+
getBitArrayCodec(1, { backward: true }).encode(booleans);
|
|
475
|
+
// 0x55 or 0b01010101
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
Separate `getBitArrayEncoder` and `getBitArrayDecoder` functions are also available.
|
|
479
|
+
|
|
480
|
+
```ts
|
|
481
|
+
const bytes = getBitArrayEncoder(1).encode(booleans);
|
|
482
|
+
const decodedBooleans = getBitArrayDecoder(1).decode(bytes);
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
## Unit codec
|
|
486
|
+
|
|
487
|
+
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 data enum codecs describe the codecs of empty variants.
|
|
488
|
+
|
|
489
|
+
```ts
|
|
490
|
+
getUnitCodec().encode(undefined); // Empty Uint8Array
|
|
491
|
+
getUnitCodec().decode(anyBytes); // undefined
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
Separate `getUnitEncoder` and `getUnitDecoder` functions are also available.
|
|
495
|
+
|
|
496
|
+
```ts
|
|
497
|
+
getUnitEncoder().encode(undefined);
|
|
498
|
+
getUnitDecoder().decode(anyBytes);
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
---
|
|
502
|
+
|
|
503
|
+
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).
|