@solana/codecs-core 2.0.0-experimental.fcff844 → 2.0.0-experimental.fd64233

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 CHANGED
@@ -14,12 +14,397 @@
14
14
 
15
15
  # @solana/codecs-core
16
16
 
17
- This package contains the core types and function for encoding and decoding data structures on Solana. 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).
17
+ This package contains the core types and functions for encoding and decoding data structures on Solana. 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
- ## Types
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
- TODO
21
+ ## Composing codecs
22
22
 
23
- ## Functions
23
+ The easiest way to create your own codecs is to compose the [various codecs](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs) offered by this library. For instance, here’s how you would define a codec for a `Person` object that contains a `name` string attribute and an `age` number stored in 4 bytes.
24
24
 
25
- TODO
25
+ ```ts
26
+ type Person = { name: string; age: number };
27
+ const getPersonCodec = (): Codec<Person> =>
28
+ getStructCodec([
29
+ ['name', getStringCodec()],
30
+ ['age', getU32Codec()],
31
+ ]);
32
+ ```
33
+
34
+ This function returns a `Codec` object which contains both an `encode` and `decode` function that can be used to convert a `Person` type to and from a `Uint8Array`.
35
+
36
+ ```ts
37
+ const personCodec = getPersonCodec();
38
+ const bytes = personCodec.encode({ name: 'John', age: 42 });
39
+ const person = personCodec.decode(bytes);
40
+ ```
41
+
42
+ There is a significant library of composable codecs at your disposal, enabling you to compose complex types. You may be interested in the documentation of these other packages to learn more about them:
43
+
44
+ - [`@solana/codecs-numbers`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs-numbers) for number codecs.
45
+ - [`@solana/codecs-strings`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs-strings) for string codecs.
46
+ - [`@solana/codecs-data-structures`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs-data-structures) for many data structure codecs such as objects, arrays, tuples, sets, maps, scalar enums, data enums, booleans, etc.
47
+ - [`@solana/options`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/options) for a Rust-like `Option` type and associated codec.
48
+
49
+ You may also be interested in some of the helpers of this `@solana/codecs-core` library such as `mapCodec`, `fixCodec` or `reverseCodec` that create new codecs from existing ones.
50
+
51
+ Note that all of these libraries are included in the [`@solana/codecs` package](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs) as well as the main `@solana/web3.js` package for your convenience.
52
+
53
+ ## Composing encoders and decoders
54
+
55
+ 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 Encoders only to encode a `Person` type.
56
+
57
+ ```ts
58
+ const getPersonEncoder = (): Encoder<Person> =>
59
+ getStructEncoder([
60
+ ['name', getStringEncoder()],
61
+ ['age', getU32Encoder()],
62
+ ]);
63
+
64
+ const bytes = getPersonEncoder().encode({ name: 'John', age: 42 });
65
+ ```
66
+
67
+ The same can be done for decoding the `Person` type by using Decoders like so.
68
+
69
+ ```ts
70
+ const getPersonDecoder = (): Decoder<Person> =>
71
+ getStructDecoder([
72
+ ['name', getStringDecoder()],
73
+ ['age', getU32Decoder()],
74
+ ]);
75
+
76
+ const person = getPersonDecoder().decode(bytes);
77
+ ```
78
+
79
+ ## Combining encoders and decoders
80
+
81
+ Separating Codecs into Encoders and Decoders is particularly good practice for library maintainers as it allows their users to tree-shake any of the encoders and/or decoders they don’t need. However, we may still want to offer a codec helper for users who need both for convenience.
82
+
83
+ That’s why this library offers a `combineCodec` helper that creates a `Codec` instance from a matching `Encoder` and `Decoder`.
84
+
85
+ ```ts
86
+ const getPersonCodec = (): Codec<Person> => combineCodec(getPersonEncoder(), getPersonDecoder());
87
+ ```
88
+
89
+ This means library maintainers can offer Encoders, Decoders and Codecs for all their types whilst staying efficient and tree-shakeable. In summary, we recommend the following pattern when creating codecs for library types.
90
+
91
+ ```ts
92
+ type MyType = /* ... */;
93
+ const getMyTypeEncoder = (): Encoder<MyType> => { /* ... */ };
94
+ const getMyTypeDecoder = (): Decoder<MyType> => { /* ... */ };
95
+ const getMyTypeCodec = (): Codec<MyType> =>
96
+ combineCodec(getMyTypeEncoder(), getMyTypeDecoder());
97
+ ```
98
+
99
+ ## Different From and To types
100
+
101
+ When creating codecs, the encoded type is allowed to be looser than the decoded type. A good example of that is the u64 number codec:
102
+
103
+ ```ts
104
+ const u64Codec: Codec<number | bigint, bigint> = getU64Codec();
105
+ ```
106
+
107
+ As you can see, the first type parameter is looser since it accepts numbers or big integers, whereas the second type parameter only accepts big integers. That’s because when _encoding_ a u64 number, you may provide either a `bigint` or a `number` for convenience. However, when you decode a u64 number, you will always get a `bigint` because not all u64 values can fit in a JavaScript `number` type.
108
+
109
+ ```ts
110
+ const bytes = u64Codec.encode(42);
111
+ const value = u64Codec.decode(bytes); // BigInt(42)
112
+ ```
113
+
114
+ This relationship between the type we encode “From” and decode “To” can be generalized in TypeScript as `To extends From`.
115
+
116
+ Here’s another example using an object with default values. You can read more about the `mapEncoder` helper below.
117
+
118
+ ```ts
119
+ type Person = { name: string, age: number };
120
+ type PersonInput = { name: string, age?: number };
121
+
122
+ const getPersonEncoder = (): Encoder<PersonInput> =>
123
+ mapEncoder(
124
+ getStructEncoder([
125
+ ['name', getStringEncoder()],
126
+ ['age', getU32Encoder()],
127
+ ]),
128
+ input => { ...input, age: input.age ?? 42 }
129
+ );
130
+
131
+ const getPersonDecoder = (): Decoder<Person> =>
132
+ getStructEncoder([
133
+ ['name', getStringEncoder()],
134
+ ['age', getU32Encoder()],
135
+ ]);
136
+
137
+ const getPersonCodec = (): Codec<PersonInput, Person> =>
138
+ combineCodec(getPersonEncoder(), getPersonDecoder())
139
+ ```
140
+
141
+ ## Fixed-size and variable-size codecs
142
+
143
+ It is also worth noting that Codecs can either be of fixed size or variable size.
144
+
145
+ `FixedSizeCodecs` have a `fixedSize` number attribute that tells us exactly how big their encoded data is in bytes.
146
+
147
+ ```ts
148
+ const myCodec: FixedSizeCodec<number> = getU32Codec();
149
+ myCodec.fixedSize; // 4 bytes.
150
+ ```
151
+
152
+ On the other hand, `VariableSizeCodecs` do not know the size of their encoded data in advance. Instead, they will grab that information either from the provided encoded data or from the value to encode. For the former, we can simply access the length of the `Uint8Array`. For the latter, it provides a `getSizeFromValue` that tells us the encoded byte size of the provided value.
153
+
154
+ ```ts
155
+ const myCodec: VariableSizeCodec<string> = getStringCodec({
156
+ size: getU32Codec(),
157
+ });
158
+ myCodec.getSizeFromValue('hello world'); // 4 + 11 bytes.
159
+ ```
160
+
161
+ Also note that, if the `VariableSizeCodec` is bounded by a maximum size, it can be provided as a `maxSize` number attribute.
162
+
163
+ The following type guards are available to identify and/or assert the size of codecs: `isFixedSize`, `isVariableSize`, `assertIsFixedSize` and `assertIsVariableSize`.
164
+
165
+ Finally, note that the same is true for `Encoders` and `Decoders`.
166
+
167
+ - A `FixedSizeEncoder` has a `fixedSize` number attribute.
168
+ - A `VariableSizeEncoder` has a `getSizeFromValue` function and an optional `maxSize` number attribute.
169
+ - A `FixedSizeDecoder` has a `fixedSize` number attribute.
170
+ - A `VariableSizeDecoder` has an optional `maxSize` number attribute.
171
+
172
+ ## Creating custom codecs
173
+
174
+ If composing codecs isn’t enough for you, you may implement your own codec logic by using the `createCodec` function. This function requires an object with a `read` and a `write` function telling us how to read from and write to an existing byte array.
175
+
176
+ The `read` function accepts the `bytes` to decode from and the `offset` at each we should start reading. It returns an array with two items:
177
+
178
+ - The first item should be the decoded value.
179
+ - The second item should be the next offset to read from.
180
+
181
+ ```ts
182
+ createCodec({
183
+ read(bytes, offset) {
184
+ const value = bytes[offset];
185
+ return [value, offset + 1];
186
+ },
187
+ // ...
188
+ });
189
+ ```
190
+
191
+ Reciprocally, the `write` function accepts the `value` to encode, the array of `bytes` to write the encoded value to and the `offset` at which it should be written. It should encode the given value, insert it in the byte array, and provide the next offset to write to as the return value.
192
+
193
+ ```ts
194
+ createCodec({
195
+ write(value, bytes, offset) {
196
+ bytes.set(value, offset);
197
+ return offset + 1;
198
+ },
199
+ // ...
200
+ });
201
+ ```
202
+
203
+ Additionally, we must specify the size of the codec. If we are defining a `FixedSizeCodec`, we must simply provide the `fixedSize` number attribute. For `VariableSizeCodecs`, we must provide the `getSizeFromValue` function as described in the previous section.
204
+
205
+ ```ts
206
+ // FixedSizeCodec.
207
+ createCodec({
208
+ fixedSize: 1,
209
+ // ...
210
+ });
211
+
212
+ // VariableSizeCodec.
213
+ createCodec({
214
+ getSizeFromValue: (value: string) => value.length,
215
+ // ...
216
+ });
217
+ ```
218
+
219
+ Here’s a concrete example of a custom codec that encodes any unsigned integer in a single byte. Since a single byte can only store integers from 0 to 255, if any other integer is provided it will take its modulo 256 to ensure it fits in a single byte. Because it always requires a single byte, that codec is a `FixedSizeCodec` of size `1`.
220
+
221
+ ```ts
222
+ const getModuloU8Codec = () =>
223
+ createCodec<number>({
224
+ fixedSize: 1,
225
+ read(bytes, offset) {
226
+ const value = bytes[offset];
227
+ return [value, offset + 1];
228
+ },
229
+ write(value, bytes, offset) {
230
+ bytes.set(value % 256, offset);
231
+ return offset + 1;
232
+ },
233
+ });
234
+ ```
235
+
236
+ Note that, it is also possible to create custom encoders and decoders separately by using the `createEncoder` and `createDecoder` functions respectively and then use the `combineCodec` function on them just like we were doing with composed codecs.
237
+
238
+ This approach is recommended to library maintainers as it allows their users to tree-shake any of the encoders and/or decoders they don’t need.
239
+
240
+ Here’s our previous modulo u8 example but split into separate `Encoder`, `Decoder` and `Codec` instances.
241
+
242
+ ```ts
243
+ const getModuloU8Encoder = () =>
244
+ createEncoder<number>({
245
+ fixedSize: 1,
246
+ write(value, bytes, offset) {
247
+ bytes.set(value % 256, offset);
248
+ return offset + 1;
249
+ },
250
+ });
251
+
252
+ const getModuloU8Decoder = () =>
253
+ createDecoder<number>({
254
+ fixedSize: 1,
255
+ read(bytes, offset) {
256
+ const value = bytes[offset];
257
+ return [value, offset + 1];
258
+ },
259
+ });
260
+
261
+ const getModuloU8Codec = () => combineCodec(getModuloU8Encoder(), getModuloU8Decoder());
262
+ ```
263
+
264
+ Here’s another example returning a `VariableSizeCodec`. This one transforms a simple string composed of characters from `a` to `z` to a buffer of numbers from `1` to `26` where `0` bytes are spaces.
265
+
266
+ ```ts
267
+ const alphabet = ' abcdefghijklmnopqrstuvwxyz';
268
+
269
+ const getCipherEncoder = () =>
270
+ createEncoder<string>({
271
+ getSizeFromValue: value => value.length,
272
+ write(value, bytes, offset) {
273
+ const bytesToAdd = [...value].map(char => alphabet.indexOf(char));
274
+ bytes.set(bytesToAdd, offset);
275
+ return offset + bytesToAdd.length;
276
+ },
277
+ });
278
+
279
+ const getCipherDecoder = () =>
280
+ createDecoder<string>({
281
+ read(bytes, offset) {
282
+ const value = [...bytes.slice(offset)].map(byte => alphabet.charAt(byte)).join('');
283
+ return [value, bytes.length];
284
+ },
285
+ });
286
+
287
+ const getCipherCodec = () => combineCodec(getCipherEncoder(), getCipherDecoder());
288
+ ```
289
+
290
+ ## Mapping codecs
291
+
292
+ It is possible to transform a `Codec<T>` to a `Codec<U>` by providing two mapping functions: one that goes from `T` to `U` and one that does the opposite.
293
+
294
+ For instance, here’s how you would map a `u32` integer into a `string` representation of that number.
295
+
296
+ ```ts
297
+ const getStringU32Codec = () =>
298
+ mapCodec(
299
+ getU32Codec(),
300
+ (integerAsString: string): number => parseInt(integerAsString),
301
+ (integer: number): string => integer.toString(),
302
+ );
303
+
304
+ getStringU32Codec().encode('42'); // new Uint8Array([42])
305
+ getStringU32Codec().decode(new Uint8Array([42])); // "42"
306
+ ```
307
+
308
+ If a `Codec` has [different From and To types](#different-from-and-to-types), say `Codec<OldFrom, OldTo>`, and we want to map it to `Codec<NewFrom, NewTo>`, we must provide functions that map from `NewFrom` to `OldFrom` and from `OldTo` to `NewTo`.
309
+
310
+ To illustrate that, let’s take our previous `getStringU32Codec` example but make it use a `getU64Codec` codec instead as it returns a `Codec<number | bigint, bigint>`. Additionally, let’s make it so our `getStringU64Codec` function returns a `Codec<number | string, string>` so that it also accepts numbers when encoding values. Here’s what our mapping functions look like:
311
+
312
+ ```ts
313
+ const getStringU64Codec = () =>
314
+ mapCodec(
315
+ getU64Codec(),
316
+ (integerInput: number | string): number | bigint =>
317
+ typeof integerInput === 'string' ? BigInt(integerAsString) : integerInput,
318
+ (integer: bigint): string => integer.toString(),
319
+ );
320
+ ```
321
+
322
+ Note that the second function that maps the decoded type is optional. That means, you can omit it to simply update or loosen the type to encode whilst keeping the decoded type the same.
323
+
324
+ This is particularly useful to provide default values to object structures. For instance, here’s how we can map our `Person` codec to give a default value to its `age` attribute.
325
+
326
+ ```ts
327
+ type Person = { name: string; age: number; }
328
+ const getPersonCodec = (): Codec<Person> => { /*...*/ }
329
+
330
+ type PersonInput = { name: string; age?: number; }
331
+ const getPersonWithDefaultValueCodec = (): Codec<PersonInput, Person> =>
332
+ mapCodec(
333
+ getPersonCodec(),
334
+ (person: PersonInput): Person => { ...person, age: person.age ?? 42 }
335
+ )
336
+ ```
337
+
338
+ Similar helpers exist to map `Encoder` and `Decoder` instances allowing you to separate your codec logic into tree-shakeable functions. Here’s our `getStringU32Codec` written that way.
339
+
340
+ ```ts
341
+ const getStringU32Encoder = () =>
342
+ mapEncoder(getU32Encoder(), (integerAsString: string): number => parseInt(integerAsString));
343
+ const getStringU32Decoder = () => mapDecoder(getU32Decoder(), (integer: number): string => integer.toString());
344
+ const getStringU32Codec = () => combineCodec(getStringU32Encoder(), getStringU32Decoder());
345
+ ```
346
+
347
+ ## Fixing the size of codecs
348
+
349
+ The `fixCodec` function allows you to bind the size of a given codec to the given fixed size.
350
+
351
+ For instance, say you want to represent a base-58 string that uses exactly 32 bytes when decoded. Here’s how you can use the `fixCodec` helper to achieve that.
352
+
353
+ ```ts
354
+ const get32BytesBase58Codec = () => fixCodec(getBase58Codec(), 32);
355
+ ```
356
+
357
+ You may also use the `fixEncoder` and `fixDecoder` functions to separate your codec logic like so:
358
+
359
+ ```ts
360
+ const get32BytesBase58Encoder = () => fixEncoder(getBase58Encoder(), 32);
361
+ const get32BytesBase58Decoder = () => fixDecoder(getBase58Decoder(), 32);
362
+ const get32BytesBase58Codec = () => combineCodec(get32BytesBase58Encoder(), get32BytesBase58Codec());
363
+ ```
364
+
365
+ ## Reversing codecs
366
+
367
+ The `reverseCodec` helper reverses the bytes of the provided `FixedSizeCodec`.
368
+
369
+ ```ts
370
+ const getBigEndianU64Codec = () => reverseCodec(getU64Codec());
371
+ ```
372
+
373
+ Note that number codecs can already do that for you via their `endian` option.
374
+
375
+ ```ts
376
+ const getBigEndianU64Codec = () => getU64Codec({ endian: Endian.BIG });
377
+ ```
378
+
379
+ As usual, the `reverseEncoder` and `reverseDecoder` can also be used to achieve that.
380
+
381
+ ```ts
382
+ const getBigEndianU64Encoder = () => reverseEncoder(getU64Encoder());
383
+ const getBigEndianU64Decoder = () => reverseDecoder(getU64Decoder());
384
+ const getBigEndianU64Codec = () => combineCodec(getBigEndianU64Encoder(), getBigEndianU64Decoder());
385
+ ```
386
+
387
+ ## Byte helpers
388
+
389
+ This package also provides utility functions for managing bytes such as:
390
+
391
+ - `mergeBytes`: Concatenates an array of `Uint8Arrays` into a single `Uint8Array`.
392
+ - `padBytes`: Pads a `Uint8Array` with zeroes (to the right) to the specified length.
393
+ - `fixBytes`: Pads or truncates a `Uint8Array` so it has the specified length.
394
+
395
+ ```ts
396
+ // Merge multiple Uint8Array buffers into one.
397
+ mergeBytes([new Uint8Array([1, 2]), new Uint8Array([3, 4])]); // Uint8Array([1, 2, 3, 4])
398
+
399
+ // Pad a Uint8Array buffer to the given size.
400
+ padBytes(new Uint8Array([1, 2]), 4); // Uint8Array([1, 2, 0, 0])
401
+ padBytes(new Uint8Array([1, 2, 3, 4]), 2); // Uint8Array([1, 2, 3, 4])
402
+
403
+ // Pad and truncate a Uint8Array buffer to the given size.
404
+ fixBytes(new Uint8Array([1, 2]), 4); // Uint8Array([1, 2, 0, 0])
405
+ fixBytes(new Uint8Array([1, 2, 3, 4]), 2); // Uint8Array([1, 2])
406
+ ```
407
+
408
+ ---
409
+
410
+ 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).
@@ -1,4 +1,4 @@
1
- import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder, VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from './codec';
1
+ import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder, VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from './codec.js';
2
2
  /**
3
3
  * Combines an encoder and a decoder into a codec.
4
4
  * The encoder and decoder must have the same fixed size, max size and description.
@@ -1,4 +1,4 @@
1
- import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from './codec';
1
+ import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from './codec.js';
2
2
  /**
3
3
  * Creates a fixed-size encoder from a given encoder.
4
4
  *
@@ -1,8 +1,8 @@
1
- export * from './assertions';
2
- export * from './bytes';
3
- export * from './codec';
4
- export * from './combine-codec';
5
- export * from './fix-codec';
6
- export * from './map-codec';
7
- export * from './reverse-codec';
1
+ export * from './assertions.js';
2
+ export * from './bytes.js';
3
+ export * from './codec.js';
4
+ export * from './combine-codec.js';
5
+ export * from './fix-codec.js';
6
+ export * from './map-codec.js';
7
+ export * from './reverse-codec.js';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1,4 +1,4 @@
1
- import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder, VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from './codec';
1
+ import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder, VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from './codec.js';
2
2
  /**
3
3
  * Converts an encoder A to a encoder B by mapping their values.
4
4
  */
@@ -1,4 +1,4 @@
1
- import { FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from './codec';
1
+ import { FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from './codec.js';
2
2
  /**
3
3
  * Reverses the bytes of a fixed-size encoder.
4
4
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/codecs-core",
3
- "version": "2.0.0-experimental.fcff844",
3
+ "version": "2.0.0-experimental.fd64233",
4
4
  "description": "Core types and helpers for encoding and decoding byte arrays on Solana",
5
5
  "exports": {
6
6
  "browser": {
@@ -51,8 +51,8 @@
51
51
  "devDependencies": {
52
52
  "@solana/eslint-config-solana": "^1.0.2",
53
53
  "@swc/jest": "^0.2.29",
54
- "@types/jest": "^29.5.6",
55
- "@typescript-eslint/eslint-plugin": "^6.7.0",
54
+ "@types/jest": "^29.5.11",
55
+ "@typescript-eslint/eslint-plugin": "^6.13.2",
56
56
  "@typescript-eslint/parser": "^6.3.0",
57
57
  "agadoo": "^3.0.0",
58
58
  "eslint": "^8.45.0",
@@ -79,8 +79,8 @@
79
79
  ]
80
80
  },
81
81
  "scripts": {
82
- "compile:js": "tsup --config build-scripts/tsup.config.library.ts",
83
- "compile:typedefs": "tsc -p ./tsconfig.declarations.json",
82
+ "compile:js": "tsup --config build-scripts/tsup.config.package.ts",
83
+ "compile:typedefs": "tsc -p ./tsconfig.declarations.json && node node_modules/build-scripts/add-js-extension-to-types.mjs",
84
84
  "dev": "jest -c node_modules/test-config/jest-dev.config.ts --rootDir . --watch",
85
85
  "publish-packages": "pnpm publish --tag experimental --access public --no-git-checks",
86
86
  "style:fix": "pnpm eslint --fix src/* && pnpm prettier -w src/* package.json",
@@ -1,233 +0,0 @@
1
- this.globalThis = this.globalThis || {};
2
- this.globalThis.solanaWeb3 = (function (exports) {
3
- 'use strict';
4
-
5
- // src/assertions.ts
6
- function assertByteArrayIsNotEmptyForCodec(codecDescription, bytes, offset = 0) {
7
- if (bytes.length - offset <= 0) {
8
- throw new Error(`Codec [${codecDescription}] cannot decode empty byte arrays.`);
9
- }
10
- }
11
- function assertByteArrayHasEnoughBytesForCodec(codecDescription, expected, bytes, offset = 0) {
12
- const bytesLength = bytes.length - offset;
13
- if (bytesLength < expected) {
14
- throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);
15
- }
16
- }
17
-
18
- // src/bytes.ts
19
- var mergeBytes = (byteArrays) => {
20
- const nonEmptyByteArrays = byteArrays.filter((arr) => arr.length);
21
- if (nonEmptyByteArrays.length === 0) {
22
- return byteArrays.length ? byteArrays[0] : new Uint8Array();
23
- }
24
- if (nonEmptyByteArrays.length === 1) {
25
- return nonEmptyByteArrays[0];
26
- }
27
- const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);
28
- const result = new Uint8Array(totalLength);
29
- let offset = 0;
30
- nonEmptyByteArrays.forEach((arr) => {
31
- result.set(arr, offset);
32
- offset += arr.length;
33
- });
34
- return result;
35
- };
36
- var padBytes = (bytes, length) => {
37
- if (bytes.length >= length)
38
- return bytes;
39
- const paddedBytes = new Uint8Array(length).fill(0);
40
- paddedBytes.set(bytes);
41
- return paddedBytes;
42
- };
43
- var fixBytes = (bytes, length) => padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);
44
-
45
- // src/codec.ts
46
- function getEncodedSize(value, encoder) {
47
- return "fixedSize" in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value);
48
- }
49
- function createEncoder(encoder) {
50
- return Object.freeze({
51
- ...encoder,
52
- encode: (value) => {
53
- const bytes = new Uint8Array(getEncodedSize(value, encoder));
54
- encoder.write(value, bytes, 0);
55
- return bytes;
56
- }
57
- });
58
- }
59
- function createDecoder(decoder) {
60
- return Object.freeze({
61
- ...decoder,
62
- decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0]
63
- });
64
- }
65
- function createCodec(codec) {
66
- return Object.freeze({
67
- ...codec,
68
- decode: (bytes, offset = 0) => codec.read(bytes, offset)[0],
69
- encode: (value) => {
70
- const bytes = new Uint8Array(getEncodedSize(value, codec));
71
- codec.write(value, bytes, 0);
72
- return bytes;
73
- }
74
- });
75
- }
76
- function isFixedSize(codec) {
77
- return "fixedSize" in codec && typeof codec.fixedSize === "number";
78
- }
79
- function assertIsFixedSize(codec, message) {
80
- if (!isFixedSize(codec)) {
81
- throw new Error(message != null ? message : "Expected a fixed-size codec, got a variable-size one.");
82
- }
83
- }
84
- function isVariableSize(codec) {
85
- return !isFixedSize(codec);
86
- }
87
- function assertIsVariableSize(codec, message) {
88
- if (!isVariableSize(codec)) {
89
- throw new Error(message != null ? message : "Expected a variable-size codec, got a fixed-size one.");
90
- }
91
- }
92
-
93
- // src/combine-codec.ts
94
- function combineCodec(encoder, decoder) {
95
- if (isFixedSize(encoder) !== isFixedSize(decoder)) {
96
- throw new Error(`Encoder and decoder must either both be fixed-size or variable-size.`);
97
- }
98
- if (isFixedSize(encoder) && isFixedSize(decoder) && encoder.fixedSize !== decoder.fixedSize) {
99
- throw new Error(
100
- `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`
101
- );
102
- }
103
- if (!isFixedSize(encoder) && !isFixedSize(decoder) && encoder.maxSize !== decoder.maxSize) {
104
- throw new Error(
105
- `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`
106
- );
107
- }
108
- return {
109
- ...decoder,
110
- ...encoder,
111
- decode: decoder.decode,
112
- encode: encoder.encode,
113
- read: decoder.read,
114
- write: encoder.write
115
- };
116
- }
117
-
118
- // src/fix-codec.ts
119
- function fixEncoder(encoder, fixedBytes) {
120
- return createEncoder({
121
- fixedSize: fixedBytes,
122
- write: (value, bytes, offset) => {
123
- const variableByteArray = encoder.encode(value);
124
- const fixedByteArray = variableByteArray.length > fixedBytes ? variableByteArray.slice(0, fixedBytes) : variableByteArray;
125
- bytes.set(fixedByteArray, offset);
126
- return offset + fixedBytes;
127
- }
128
- });
129
- }
130
- function fixDecoder(decoder, fixedBytes) {
131
- return createDecoder({
132
- fixedSize: fixedBytes,
133
- read: (bytes, offset) => {
134
- assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset);
135
- if (offset > 0 || bytes.length > fixedBytes) {
136
- bytes = bytes.slice(offset, offset + fixedBytes);
137
- }
138
- if (isFixedSize(decoder)) {
139
- bytes = fixBytes(bytes, decoder.fixedSize);
140
- }
141
- const [value] = decoder.read(bytes, 0);
142
- return [value, offset + fixedBytes];
143
- }
144
- });
145
- }
146
- function fixCodec(codec, fixedBytes) {
147
- return combineCodec(fixEncoder(codec, fixedBytes), fixDecoder(codec, fixedBytes));
148
- }
149
-
150
- // src/map-codec.ts
151
- function mapEncoder(encoder, unmap) {
152
- return createEncoder({
153
- ...isVariableSize(encoder) ? { ...encoder, getSizeFromValue: (value) => encoder.getSizeFromValue(unmap(value)) } : encoder,
154
- write: (value, bytes, offset) => encoder.write(unmap(value), bytes, offset)
155
- });
156
- }
157
- function mapDecoder(decoder, map) {
158
- return createDecoder({
159
- ...decoder,
160
- read: (bytes, offset) => {
161
- const [value, newOffset] = decoder.read(bytes, offset);
162
- return [map(value, bytes, offset), newOffset];
163
- }
164
- });
165
- }
166
- function mapCodec(codec, unmap, map) {
167
- return createCodec({
168
- ...mapEncoder(codec, unmap),
169
- read: map ? mapDecoder(codec, map).read : codec.read
170
- });
171
- }
172
-
173
- // src/reverse-codec.ts
174
- function reverseEncoder(encoder) {
175
- assertIsFixedSize(encoder, "Cannot reverse a codec of variable size.");
176
- return createEncoder({
177
- ...encoder,
178
- write: (value, bytes, offset) => {
179
- const newOffset = encoder.write(value, bytes, offset);
180
- const slice = bytes.slice(offset, offset + encoder.fixedSize).reverse();
181
- bytes.set(slice, offset);
182
- return newOffset;
183
- }
184
- });
185
- }
186
- function reverseDecoder(decoder) {
187
- assertIsFixedSize(decoder, "Cannot reverse a codec of variable size.");
188
- return createDecoder({
189
- ...decoder,
190
- read: (bytes, offset) => {
191
- const reverseEnd = offset + decoder.fixedSize;
192
- if (offset === 0 && bytes.length === reverseEnd) {
193
- return decoder.read(bytes.reverse(), offset);
194
- }
195
- const reversedBytes = bytes.slice();
196
- reversedBytes.set(bytes.slice(offset, reverseEnd).reverse(), offset);
197
- return decoder.read(reversedBytes, offset);
198
- }
199
- });
200
- }
201
- function reverseCodec(codec) {
202
- return combineCodec(reverseEncoder(codec), reverseDecoder(codec));
203
- }
204
-
205
- exports.assertByteArrayHasEnoughBytesForCodec = assertByteArrayHasEnoughBytesForCodec;
206
- exports.assertByteArrayIsNotEmptyForCodec = assertByteArrayIsNotEmptyForCodec;
207
- exports.assertIsFixedSize = assertIsFixedSize;
208
- exports.assertIsVariableSize = assertIsVariableSize;
209
- exports.combineCodec = combineCodec;
210
- exports.createCodec = createCodec;
211
- exports.createDecoder = createDecoder;
212
- exports.createEncoder = createEncoder;
213
- exports.fixBytes = fixBytes;
214
- exports.fixCodec = fixCodec;
215
- exports.fixDecoder = fixDecoder;
216
- exports.fixEncoder = fixEncoder;
217
- exports.getEncodedSize = getEncodedSize;
218
- exports.isFixedSize = isFixedSize;
219
- exports.isVariableSize = isVariableSize;
220
- exports.mapCodec = mapCodec;
221
- exports.mapDecoder = mapDecoder;
222
- exports.mapEncoder = mapEncoder;
223
- exports.mergeBytes = mergeBytes;
224
- exports.padBytes = padBytes;
225
- exports.reverseCodec = reverseCodec;
226
- exports.reverseDecoder = reverseDecoder;
227
- exports.reverseEncoder = reverseEncoder;
228
-
229
- return exports;
230
-
231
- })({});
232
- //# sourceMappingURL=out.js.map
233
- //# sourceMappingURL=index.development.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/assertions.ts","../src/bytes.ts","../src/codec.ts","../src/combine-codec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Z,SAC4B;AAC5B,oBAAkB,SAAS,0CAA0C;AACrE,SAAO,cAAc;AAAA,IACjB,GAAG;AAAA,IACH,MAAM,CAAC,OAAO,WAAW;AACrB,YAAM,aAAa,SAAS,QAAQ;AACpC,UAAI,WAAW,KAAK,MAAM,WAAW,YAAY;AAC7C,eAAO,QAAQ,KAAK,MAAM,QAAQ,GAAG,MAAM;AAAA,MAC/C;AACA,YAAM,gBAAgB,MAAM,MAAM;AAClC,oBAAc,IAAI,MAAM,MAAM,QAAQ,UAAU,EAAE,QAAQ,GAAG,MAAM;AACnE,aAAO,QAAQ,KAAK,eAAe,MAAM;AAAA,IAC7C;AAAA,EACJ,CAAC;AACL;AAKO,SAAS,aACZ,OACiC;AACjC,SAAO,aAAa,eAAe,KAAK,GAAG,eAAe,KAAK,CAAC;AACpE","sourcesContent":["/**\n * Asserts that a given byte array is not empty.\n */\nexport function assertByteArrayIsNotEmptyForCodec(codecDescription: string, bytes: Uint8Array, offset = 0) {\n if (bytes.length - offset <= 0) {\n // TODO: Coded error.\n throw new Error(`Codec [${codecDescription}] cannot decode empty byte arrays.`);\n }\n}\n\n/**\n * Asserts that a given byte array has enough bytes to decode.\n */\nexport function assertByteArrayHasEnoughBytesForCodec(\n codecDescription: string,\n expected: number,\n bytes: Uint8Array,\n offset = 0,\n) {\n const bytesLength = bytes.length - offset;\n if (bytesLength < expected) {\n // TODO: Coded error.\n throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);\n }\n}\n","/**\n * Concatenates an array of `Uint8Array`s into a single `Uint8Array`.\n * Reuses the original byte array when applicable.\n */\nexport const mergeBytes = (byteArrays: Uint8Array[]): Uint8Array => {\n const nonEmptyByteArrays = byteArrays.filter(arr => arr.length);\n if (nonEmptyByteArrays.length === 0) {\n return byteArrays.length ? byteArrays[0] : new Uint8Array();\n }\n\n if (nonEmptyByteArrays.length === 1) {\n return nonEmptyByteArrays[0];\n }\n\n const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);\n const result = new Uint8Array(totalLength);\n let offset = 0;\n nonEmptyByteArrays.forEach(arr => {\n result.set(arr, offset);\n offset += arr.length;\n });\n return result;\n};\n\n/**\n * Pads a `Uint8Array` with zeroes to the specified length.\n * If the array is longer than the specified length, it is returned as-is.\n */\nexport const padBytes = (bytes: Uint8Array, length: number): Uint8Array => {\n if (bytes.length >= length) return bytes;\n const paddedBytes = new Uint8Array(length).fill(0);\n paddedBytes.set(bytes);\n return paddedBytes;\n};\n\n/**\n * Fixes a `Uint8Array` to the specified length.\n * If the array is longer than the specified length, it is truncated.\n * If the array is shorter than the specified length, it is padded with zeroes.\n */\nexport const fixBytes = (bytes: Uint8Array, length: number): Uint8Array =>\n padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);\n","/**\n * Defines an offset in bytes.\n */\nexport type Offset = number;\n\ntype BaseEncoder<TFrom> = {\n /** Encode the provided value and return the encoded bytes directly. */\n readonly encode: (value: TFrom) => Uint8Array;\n /**\n * Writes the encoded value into the provided byte array at the given offset.\n * Returns the offset of the next byte after the encoded value.\n */\n readonly write: (value: TFrom, bytes: Uint8Array, offset: Offset) => Offset;\n};\n\nexport type FixedSizeEncoder<TFrom, TSize extends number = number> = BaseEncoder<TFrom> & {\n /** The fixed size of the encoded value in bytes. */\n readonly fixedSize: TSize;\n};\n\nexport type VariableSizeEncoder<TFrom> = BaseEncoder<TFrom> & {\n /** The total size of the encoded value in bytes. */\n readonly getSizeFromValue: (value: TFrom) => number;\n /** The maximum size an encoded value can be in bytes, if applicable. */\n readonly maxSize?: number;\n};\n\n/**\n * An object that can encode a value to a `Uint8Array`.\n */\nexport type Encoder<TFrom> = FixedSizeEncoder<TFrom> | VariableSizeEncoder<TFrom>;\n\ntype BaseDecoder<TTo> = {\n /** Decodes the provided byte array at the given offset (or zero) and returns the value directly. */\n readonly decode: (bytes: Uint8Array, offset?: Offset) => TTo;\n /**\n * Reads the encoded value from the provided byte array at the given offset.\n * Returns the decoded value and the offset of the next byte after the encoded value.\n */\n readonly read: (bytes: Uint8Array, offset: Offset) => [TTo, Offset];\n};\n\nexport type FixedSizeDecoder<TTo, TSize extends number = number> = BaseDecoder<TTo> & {\n /** The fixed size of the encoded value in bytes. */\n readonly fixedSize: TSize;\n};\n\nexport type VariableSizeDecoder<TTo> = BaseDecoder<TTo> & {\n /** The maximum size an encoded value can be in bytes, if applicable. */\n readonly maxSize?: number;\n};\n\n/**\n * An object that can decode a value from a `Uint8Array`.\n */\nexport type Decoder<TTo> = FixedSizeDecoder<TTo> | VariableSizeDecoder<TTo>;\n\nexport type FixedSizeCodec<TFrom, TTo extends TFrom = TFrom, TSize extends number = number> = FixedSizeEncoder<\n TFrom,\n TSize\n> &\n FixedSizeDecoder<TTo, TSize>;\n\nexport type VariableSizeCodec<TFrom, TTo extends TFrom = TFrom> = VariableSizeEncoder<TFrom> & VariableSizeDecoder<TTo>;\n\n/**\n * An object that can encode and decode a value to and from a `Uint8Array`.\n * It supports encoding looser types than it decodes for convenience.\n * For example, a `bigint` encoder will always decode to a `bigint`\n * but can be used to encode a `number`.\n *\n * @typeParam TFrom - The type of the value to encode.\n * @typeParam TTo - The type of the decoded value. Defaults to `TFrom`.\n */\nexport type Codec<TFrom, TTo extends TFrom = TFrom> = FixedSizeCodec<TFrom, TTo> | VariableSizeCodec<TFrom, TTo>;\n\n/**\n * Get the encoded size of a given value in bytes.\n */\nexport function getEncodedSize<TFrom>(\n value: TFrom,\n encoder: { fixedSize: number } | { getSizeFromValue: (value: TFrom) => number },\n): number {\n return 'fixedSize' in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value);\n}\n\n/** Fills the missing `encode` function using the existing `write` function. */\nexport function createEncoder<TFrom, TSize extends number>(\n encoder: Omit<FixedSizeEncoder<TFrom, TSize>, 'encode'>,\n): FixedSizeEncoder<TFrom, TSize>;\nexport function createEncoder<TFrom>(encoder: Omit<VariableSizeEncoder<TFrom>, 'encode'>): VariableSizeEncoder<TFrom>;\nexport function createEncoder<TFrom>(\n encoder: Omit<FixedSizeEncoder<TFrom>, 'encode'> | Omit<VariableSizeEncoder<TFrom>, 'encode'>,\n): Encoder<TFrom>;\nexport function createEncoder<TFrom>(\n encoder: Omit<FixedSizeEncoder<TFrom>, 'encode'> | Omit<VariableSizeEncoder<TFrom>, 'encode'>,\n): Encoder<TFrom> {\n return Object.freeze({\n ...encoder,\n encode: value => {\n const bytes = new Uint8Array(getEncodedSize(value, encoder));\n encoder.write(value, bytes, 0);\n return bytes;\n },\n });\n}\n\n/** Fills the missing `decode` function using the existing `read` function. */\nexport function createDecoder<TTo, TSize extends number>(\n decoder: Omit<FixedSizeDecoder<TTo, TSize>, 'decode'>,\n): FixedSizeDecoder<TTo, TSize>;\nexport function createDecoder<TTo>(decoder: Omit<VariableSizeDecoder<TTo>, 'decode'>): VariableSizeDecoder<TTo>;\nexport function createDecoder<TTo>(\n decoder: Omit<FixedSizeDecoder<TTo>, 'decode'> | Omit<VariableSizeDecoder<TTo>, 'decode'>,\n): Decoder<TTo>;\nexport function createDecoder<TTo>(\n decoder: Omit<FixedSizeDecoder<TTo>, 'decode'> | Omit<VariableSizeDecoder<TTo>, 'decode'>,\n): Decoder<TTo> {\n return Object.freeze({\n ...decoder,\n decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0],\n });\n}\n\n/** Fills the missing `encode` and `decode` function using the existing `write` and `read` functions. */\nexport function createCodec<TFrom, TTo extends TFrom = TFrom, TSize extends number = number>(\n codec: Omit<FixedSizeCodec<TFrom, TTo, TSize>, 'encode' | 'decode'>,\n): FixedSizeCodec<TFrom, TTo, TSize>;\nexport function createCodec<TFrom, TTo extends TFrom = TFrom>(\n codec: Omit<VariableSizeCodec<TFrom, TTo>, 'encode' | 'decode'>,\n): VariableSizeCodec<TFrom, TTo>;\nexport function createCodec<TFrom, TTo extends TFrom = TFrom>(\n codec:\n | Omit<FixedSizeCodec<TFrom, TTo>, 'encode' | 'decode'>\n | Omit<VariableSizeCodec<TFrom, TTo>, 'encode' | 'decode'>,\n): Codec<TFrom, TTo>;\nexport function createCodec<TFrom, TTo extends TFrom = TFrom>(\n codec:\n | Omit<FixedSizeCodec<TFrom, TTo>, 'encode' | 'decode'>\n | Omit<VariableSizeCodec<TFrom, TTo>, 'encode' | 'decode'>,\n): Codec<TFrom, TTo> {\n return Object.freeze({\n ...codec,\n decode: (bytes, offset = 0) => codec.read(bytes, offset)[0],\n encode: value => {\n const bytes = new Uint8Array(getEncodedSize(value, codec));\n codec.write(value, bytes, 0);\n return bytes;\n },\n });\n}\n\nexport function isFixedSize<TFrom, TSize extends number>(\n encoder: FixedSizeEncoder<TFrom, TSize> | VariableSizeEncoder<TFrom>,\n): encoder is FixedSizeEncoder<TFrom, TSize>;\nexport function isFixedSize<TTo, TSize extends number>(\n decoder: FixedSizeDecoder<TTo, TSize> | VariableSizeDecoder<TTo>,\n): decoder is FixedSizeDecoder<TTo, TSize>;\nexport function isFixedSize<TFrom, TTo extends TFrom, TSize extends number>(\n codec: FixedSizeCodec<TFrom, TTo, TSize> | VariableSizeCodec<TFrom, TTo>,\n): codec is FixedSizeCodec<TFrom, TTo, TSize>;\nexport function isFixedSize<TSize extends number>(\n codec: { fixedSize: TSize } | { maxSize?: number },\n): codec is { fixedSize: TSize };\nexport function isFixedSize(codec: { fixedSize: number } | { maxSize?: number }): codec is { fixedSize: number } {\n return 'fixedSize' in codec && typeof codec.fixedSize === 'number';\n}\n\nexport function assertIsFixedSize<TFrom, TSize extends number>(\n encoder: FixedSizeEncoder<TFrom, TSize> | VariableSizeEncoder<TFrom>,\n message?: string,\n): asserts encoder is FixedSizeEncoder<TFrom, TSize>;\nexport function assertIsFixedSize<TTo, TSize extends number>(\n decoder: FixedSizeDecoder<TTo, TSize> | VariableSizeDecoder<TTo>,\n message?: string,\n): asserts decoder is FixedSizeDecoder<TTo, TSize>;\nexport function assertIsFixedSize<TFrom, TTo extends TFrom, TSize extends number>(\n codec: FixedSizeCodec<TFrom, TTo, TSize> | VariableSizeCodec<TFrom, TTo>,\n message?: string,\n): asserts codec is FixedSizeCodec<TFrom, TTo, TSize>;\nexport function assertIsFixedSize<TSize extends number>(\n codec: { fixedSize: TSize } | { maxSize?: number },\n message?: string,\n): asserts codec is { fixedSize: TSize };\nexport function assertIsFixedSize(\n codec: { fixedSize: number } | { maxSize?: number },\n message?: string,\n): asserts codec is { fixedSize: number } {\n if (!isFixedSize(codec)) {\n // TODO: Coded error.\n throw new Error(message ?? 'Expected a fixed-size codec, got a variable-size one.');\n }\n}\n\nexport function isVariableSize<TFrom>(encoder: Encoder<TFrom>): encoder is VariableSizeEncoder<TFrom>;\nexport function isVariableSize<TTo>(decoder: Decoder<TTo>): decoder is VariableSizeDecoder<TTo>;\nexport function isVariableSize<TFrom, TTo extends TFrom>(\n codec: Codec<TFrom, TTo>,\n): codec is VariableSizeCodec<TFrom, TTo>;\nexport function isVariableSize(codec: { fixedSize: number } | { maxSize?: number }): codec is { maxSize?: number };\nexport function isVariableSize(codec: { fixedSize: number } | { maxSize?: number }): codec is { maxSize?: number } {\n return !isFixedSize(codec);\n}\n\nexport function assertIsVariableSize<T>(\n encoder: Encoder<T>,\n message?: string,\n): asserts encoder is VariableSizeEncoder<T>;\nexport function assertIsVariableSize<T>(\n decoder: Decoder<T>,\n message?: string,\n): asserts decoder is VariableSizeDecoder<T>;\nexport function assertIsVariableSize<TFrom, TTo extends TFrom>(\n codec: Codec<TFrom, TTo>,\n message?: string,\n): asserts codec is VariableSizeCodec<TFrom, TTo>;\nexport function assertIsVariableSize(\n codec: { fixedSize: number } | { maxSize?: number },\n message?: string,\n): asserts codec is { maxSize?: number };\nexport function assertIsVariableSize(\n codec: { fixedSize: number } | { maxSize?: number },\n message?: string,\n): asserts codec is { maxSize?: number } {\n if (!isVariableSize(codec)) {\n // TODO: Coded error.\n throw new Error(message ?? 'Expected a variable-size codec, got a fixed-size one.');\n }\n}\n","import {\n Codec,\n Decoder,\n Encoder,\n FixedSizeCodec,\n FixedSizeDecoder,\n FixedSizeEncoder,\n isFixedSize,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from './codec';\n\n/**\n * Combines an encoder and a decoder into a codec.\n * The encoder and decoder must have the same fixed size, max size and description.\n * If a description is provided, it will override the encoder and decoder descriptions.\n */\nexport function combineCodec<TFrom, TTo extends TFrom, TSize extends number>(\n encoder: FixedSizeEncoder<TFrom, TSize>,\n decoder: FixedSizeDecoder<TTo, TSize>,\n): FixedSizeCodec<TFrom, TTo, TSize>;\nexport function combineCodec<TFrom, TTo extends TFrom>(\n encoder: VariableSizeEncoder<TFrom>,\n decoder: VariableSizeDecoder<TTo>,\n): VariableSizeCodec<TFrom, TTo>;\nexport function combineCodec<TFrom, TTo extends TFrom>(\n encoder: Encoder<TFrom>,\n decoder: Decoder<TTo>,\n): Codec<TFrom, TTo>;\nexport function combineCodec<TFrom, TTo extends TFrom>(\n encoder: Encoder<TFrom>,\n decoder: Decoder<TTo>,\n): Codec<TFrom, TTo> {\n if (isFixedSize(encoder) !== isFixedSize(decoder)) {\n // TODO: Coded error.\n throw new Error(`Encoder and decoder must either both be fixed-size or variable-size.`);\n }\n\n if (isFixedSize(encoder) && isFixedSize(decoder) && encoder.fixedSize !== decoder.fixedSize) {\n // TODO: Coded error.\n throw new Error(\n `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`,\n );\n }\n\n if (!isFixedSize(encoder) && !isFixedSize(decoder) && encoder.maxSize !== decoder.maxSize) {\n // TODO: Coded error.\n throw new Error(\n `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`,\n );\n }\n\n return {\n ...decoder,\n ...encoder,\n decode: decoder.decode,\n encode: encoder.encode,\n read: decoder.read,\n write: encoder.write,\n };\n}\n","import { assertByteArrayHasEnoughBytesForCodec } from './assertions';\nimport { fixBytes } from './bytes';\nimport {\n Codec,\n createDecoder,\n createEncoder,\n Decoder,\n Encoder,\n FixedSizeCodec,\n FixedSizeDecoder,\n FixedSizeEncoder,\n isFixedSize,\n Offset,\n} from './codec';\nimport { combineCodec } from './combine-codec';\n\n/**\n * Creates a fixed-size encoder from a given encoder.\n *\n * @param encoder - The encoder to wrap into a fixed-size encoder.\n * @param fixedBytes - The fixed number of bytes to write.\n */\nexport function fixEncoder<TFrom, TSize extends number>(\n encoder: Encoder<TFrom>,\n fixedBytes: TSize,\n): FixedSizeEncoder<TFrom, TSize> {\n return createEncoder({\n fixedSize: fixedBytes,\n write: (value: TFrom, bytes: Uint8Array, offset: Offset) => {\n // Here we exceptionally use the `encode` function instead of the `write`\n // function as using the nested `write` function on a fixed-sized byte\n // array may result in a out-of-bounds error on the nested encoder.\n const variableByteArray = encoder.encode(value);\n const fixedByteArray =\n variableByteArray.length > fixedBytes ? variableByteArray.slice(0, fixedBytes) : variableByteArray;\n bytes.set(fixedByteArray, offset);\n return offset + fixedBytes;\n },\n });\n}\n\n/**\n * Creates a fixed-size decoder from a given decoder.\n *\n * @param decoder - The decoder to wrap into a fixed-size decoder.\n * @param fixedBytes - The fixed number of bytes to read.\n */\nexport function fixDecoder<TTo, TSize extends number>(\n decoder: Decoder<TTo>,\n fixedBytes: TSize,\n): FixedSizeDecoder<TTo, TSize> {\n return createDecoder({\n fixedSize: fixedBytes,\n read: (bytes: Uint8Array, offset: Offset) => {\n assertByteArrayHasEnoughBytesForCodec('fixCodec', fixedBytes, bytes, offset);\n // Slice the byte array to the fixed size if necessary.\n if (offset > 0 || bytes.length > fixedBytes) {\n bytes = bytes.slice(offset, offset + fixedBytes);\n }\n // If the nested decoder is fixed-size, pad and truncate the byte array accordingly.\n if (isFixedSize(decoder)) {\n bytes = fixBytes(bytes, decoder.fixedSize);\n }\n // Decode the value using the nested decoder.\n const [value] = decoder.read(bytes, 0);\n return [value, offset + fixedBytes];\n },\n });\n}\n\n/**\n * Creates a fixed-size codec from a given codec.\n *\n * @param codec - The codec to wrap into a fixed-size codec.\n * @param fixedBytes - The fixed number of bytes to read/write.\n */\nexport function fixCodec<TFrom, TTo extends TFrom, TSize extends number>(\n codec: Codec<TFrom, TTo>,\n fixedBytes: TSize,\n): FixedSizeCodec<TFrom, TTo, TSize> {\n return combineCodec(fixEncoder(codec, fixedBytes), fixDecoder(codec, fixedBytes));\n}\n","import {\n Codec,\n createCodec,\n createDecoder,\n createEncoder,\n Decoder,\n Encoder,\n FixedSizeCodec,\n FixedSizeDecoder,\n FixedSizeEncoder,\n isVariableSize,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from './codec';\n\n/**\n * Converts an encoder A to a encoder B by mapping their values.\n */\nexport function mapEncoder<TOldFrom, TNewFrom, TSize extends number>(\n encoder: FixedSizeEncoder<TOldFrom, TSize>,\n unmap: (value: TNewFrom) => TOldFrom,\n): FixedSizeEncoder<TNewFrom, TSize>;\nexport function mapEncoder<TOldFrom, TNewFrom>(\n encoder: VariableSizeEncoder<TOldFrom>,\n unmap: (value: TNewFrom) => TOldFrom,\n): VariableSizeEncoder<TNewFrom>;\nexport function mapEncoder<TOldFrom, TNewFrom>(\n encoder: Encoder<TOldFrom>,\n unmap: (value: TNewFrom) => TOldFrom,\n): Encoder<TNewFrom>;\nexport function mapEncoder<TOldFrom, TNewFrom>(\n encoder: Encoder<TOldFrom>,\n unmap: (value: TNewFrom) => TOldFrom,\n): Encoder<TNewFrom> {\n return createEncoder({\n ...(isVariableSize(encoder)\n ? { ...encoder, getSizeFromValue: (value: TNewFrom) => encoder.getSizeFromValue(unmap(value)) }\n : encoder),\n write: (value: TNewFrom, bytes, offset) => encoder.write(unmap(value), bytes, offset),\n });\n}\n\n/**\n * Converts an decoder A to a decoder B by mapping their values.\n */\nexport function mapDecoder<TOldTo, TNewTo, TSize extends number>(\n decoder: FixedSizeDecoder<TOldTo, TSize>,\n map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo,\n): FixedSizeDecoder<TNewTo, TSize>;\nexport function mapDecoder<TOldTo, TNewTo>(\n decoder: VariableSizeDecoder<TOldTo>,\n map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo,\n): VariableSizeDecoder<TNewTo>;\nexport function mapDecoder<TOldTo, TNewTo>(\n decoder: Decoder<TOldTo>,\n map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo,\n): Decoder<TNewTo>;\nexport function mapDecoder<TOldTo, TNewTo>(\n decoder: Decoder<TOldTo>,\n map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo,\n): Decoder<TNewTo> {\n return createDecoder({\n ...decoder,\n read: (bytes: Uint8Array, offset) => {\n const [value, newOffset] = decoder.read(bytes, offset);\n return [map(value, bytes, offset), newOffset];\n },\n });\n}\n\n/**\n * Converts a codec A to a codec B by mapping their values.\n */\nexport function mapCodec<TOldFrom, TNewFrom, TTo extends TNewFrom & TOldFrom, TSize extends number>(\n codec: FixedSizeCodec<TOldFrom, TTo, TSize>,\n unmap: (value: TNewFrom) => TOldFrom,\n): FixedSizeCodec<TNewFrom, TTo, TSize>;\nexport function mapCodec<TOldFrom, TNewFrom, TTo extends TNewFrom & TOldFrom>(\n codec: VariableSizeCodec<TOldFrom, TTo>,\n unmap: (value: TNewFrom) => TOldFrom,\n): VariableSizeCodec<TNewFrom, TTo>;\nexport function mapCodec<TOldFrom, TNewFrom, TTo extends TNewFrom & TOldFrom>(\n codec: Codec<TOldFrom, TTo>,\n unmap: (value: TNewFrom) => TOldFrom,\n): Codec<TNewFrom, TTo>;\nexport function mapCodec<TOldFrom, TNewFrom, TOldTo extends TOldFrom, TNewTo extends TNewFrom, TSize extends number>(\n codec: FixedSizeCodec<TOldFrom, TOldTo, TSize>,\n unmap: (value: TNewFrom) => TOldFrom,\n map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo,\n): FixedSizeCodec<TNewFrom, TNewTo, TSize>;\nexport function mapCodec<TOldFrom, TNewFrom, TOldTo extends TOldFrom, TNewTo extends TNewFrom>(\n codec: VariableSizeCodec<TOldFrom, TOldTo>,\n unmap: (value: TNewFrom) => TOldFrom,\n map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo,\n): VariableSizeCodec<TNewFrom, TNewTo>;\nexport function mapCodec<TOldFrom, TNewFrom, TOldTo extends TOldFrom, TNewTo extends TNewFrom>(\n codec: Codec<TOldFrom, TOldTo>,\n unmap: (value: TNewFrom) => TOldFrom,\n map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo,\n): Codec<TNewFrom, TNewTo>;\nexport function mapCodec<TOldFrom, TNewFrom, TOldTo extends TOldFrom, TNewTo extends TNewFrom>(\n codec: Codec<TOldFrom, TOldTo>,\n unmap: (value: TNewFrom) => TOldFrom,\n map?: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo,\n): Codec<TNewFrom, TNewTo> {\n return createCodec({\n ...mapEncoder(codec, unmap),\n read: map ? mapDecoder(codec, map).read : (codec.read as unknown as Decoder<TNewTo>['read']),\n });\n}\n","import {\n assertIsFixedSize,\n createDecoder,\n createEncoder,\n FixedSizeCodec,\n FixedSizeDecoder,\n FixedSizeEncoder,\n} from './codec';\nimport { combineCodec } from './combine-codec';\n\n/**\n * Reverses the bytes of a fixed-size encoder.\n */\nexport function reverseEncoder<TFrom, TSize extends number>(\n encoder: FixedSizeEncoder<TFrom, TSize>,\n): FixedSizeEncoder<TFrom, TSize> {\n assertIsFixedSize(encoder, 'Cannot reverse a codec of variable size.');\n return createEncoder({\n ...encoder,\n write: (value: TFrom, bytes, offset) => {\n const newOffset = encoder.write(value, bytes, offset);\n const slice = bytes.slice(offset, offset + encoder.fixedSize).reverse();\n bytes.set(slice, offset);\n return newOffset;\n },\n });\n}\n\n/**\n * Reverses the bytes of a fixed-size decoder.\n */\nexport function reverseDecoder<TTo, TSize extends number>(\n decoder: FixedSizeDecoder<TTo, TSize>,\n): FixedSizeDecoder<TTo, TSize> {\n assertIsFixedSize(decoder, 'Cannot reverse a codec of variable size.');\n return createDecoder({\n ...decoder,\n read: (bytes, offset) => {\n const reverseEnd = offset + decoder.fixedSize;\n if (offset === 0 && bytes.length === reverseEnd) {\n return decoder.read(bytes.reverse(), offset);\n }\n const reversedBytes = bytes.slice();\n reversedBytes.set(bytes.slice(offset, reverseEnd).reverse(), offset);\n return decoder.read(reversedBytes, offset);\n },\n });\n}\n\n/**\n * Reverses the bytes of a fixed-size codec.\n */\nexport function reverseCodec<TFrom, TTo extends TFrom, TSize extends number>(\n codec: FixedSizeCodec<TFrom, TTo, TSize>,\n): FixedSizeCodec<TFrom, TTo, TSize> {\n return combineCodec(reverseEncoder(codec), reverseDecoder(codec));\n}\n"]}
@@ -1,33 +0,0 @@
1
- this.globalThis = this.globalThis || {};
2
- this.globalThis.solanaWeb3 = (function (exports) {
3
- 'use strict';
4
-
5
- function N(e,o,r=0){if(o.length-r<=0)throw new Error(`Codec [${e}] cannot decode empty byte arrays.`)}function S(e,o,r,i=0){let T=r.length-i;if(T<o)throw new Error(`Codec [${e}] expected ${o} bytes, got ${T}.`)}var v=e=>{let o=e.filter(d=>d.length);if(o.length===0)return e.length?e[0]:new Uint8Array;if(o.length===1)return o[0];let r=o.reduce((d,F)=>d+F.length,0),i=new Uint8Array(r),T=0;return o.forEach(d=>{i.set(d,T),T+=d.length;}),i},f=(e,o)=>{if(e.length>=o)return e;let r=new Uint8Array(o).fill(0);return r.set(e),r},s=(e,o)=>f(e.length<=o?e:e.slice(0,o),o);function u(e,o){return "fixedSize"in o?o.fixedSize:o.getSizeFromValue(e)}function c(e){return Object.freeze({...e,encode:o=>{let r=new Uint8Array(u(o,e));return e.write(o,r,0),r}})}function m(e){return Object.freeze({...e,decode:(o,r=0)=>e.read(o,r)[0]})}function l(e){return Object.freeze({...e,decode:(o,r=0)=>e.read(o,r)[0],encode:o=>{let r=new Uint8Array(u(o,e));return e.write(o,r,0),r}})}function n(e){return "fixedSize"in e&&typeof e.fixedSize=="number"}function z(e,o){if(!n(e))throw new Error(o!=null?o:"Expected a fixed-size codec, got a variable-size one.")}function x(e){return !n(e)}function h(e,o){if(!x(e))throw new Error(o!=null?o:"Expected a variable-size codec, got a fixed-size one.")}function a(e,o){if(n(e)!==n(o))throw new Error("Encoder and decoder must either both be fixed-size or variable-size.");if(n(e)&&n(o)&&e.fixedSize!==o.fixedSize)throw new Error(`Encoder and decoder must have the same fixed size, got [${e.fixedSize}] and [${o.fixedSize}].`);if(!n(e)&&!n(o)&&e.maxSize!==o.maxSize)throw new Error(`Encoder and decoder must have the same max size, got [${e.maxSize}] and [${o.maxSize}].`);return {...o,...e,decode:o.decode,encode:e.encode,read:o.read,write:e.write}}function b(e,o){return c({fixedSize:o,write:(r,i,T)=>{let d=e.encode(r),F=d.length>o?d.slice(0,o):d;return i.set(F,T),T+o}})}function p(e,o){return m({fixedSize:o,read:(r,i)=>{S("fixCodec",o,r,i),(i>0||r.length>o)&&(r=r.slice(i,i+o)),n(e)&&(r=s(r,e.fixedSize));let[T]=e.read(r,0);return [T,i+o]}})}function Te(e,o){return a(b(e,o),p(e,o))}function w(e,o){return c({...x(e)?{...e,getSizeFromValue:r=>e.getSizeFromValue(o(r))}:e,write:(r,i,T)=>e.write(o(r),i,T)})}function O(e,o){return m({...e,read:(r,i)=>{let[T,d]=e.read(r,i);return [o(T,r,i),d]}})}function le(e,o,r){return l({...w(e,o),read:r?O(e,r).read:e.read})}function E(e){return z(e,"Cannot reverse a codec of variable size."),c({...e,write:(o,r,i)=>{let T=e.write(o,r,i),d=r.slice(i,i+e.fixedSize).reverse();return r.set(d,i),T}})}function C(e){return z(e,"Cannot reverse a codec of variable size."),m({...e,read:(o,r)=>{let i=r+e.fixedSize;if(r===0&&o.length===i)return e.read(o.reverse(),r);let T=o.slice();return T.set(o.slice(r,i).reverse(),r),e.read(T,r)}})}function De(e){return a(E(e),C(e))}
6
-
7
- exports.assertByteArrayHasEnoughBytesForCodec = S;
8
- exports.assertByteArrayIsNotEmptyForCodec = N;
9
- exports.assertIsFixedSize = z;
10
- exports.assertIsVariableSize = h;
11
- exports.combineCodec = a;
12
- exports.createCodec = l;
13
- exports.createDecoder = m;
14
- exports.createEncoder = c;
15
- exports.fixBytes = s;
16
- exports.fixCodec = Te;
17
- exports.fixDecoder = p;
18
- exports.fixEncoder = b;
19
- exports.getEncodedSize = u;
20
- exports.isFixedSize = n;
21
- exports.isVariableSize = x;
22
- exports.mapCodec = le;
23
- exports.mapDecoder = O;
24
- exports.mapEncoder = w;
25
- exports.mergeBytes = v;
26
- exports.padBytes = f;
27
- exports.reverseCodec = De;
28
- exports.reverseDecoder = C;
29
- exports.reverseEncoder = E;
30
-
31
- return exports;
32
-
33
- })({});