@solana/codecs-data-structures 2.0.0-experimental.ffeddf6 → 2.0.0-preview.1

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.
Files changed (46) hide show
  1. package/README.md +482 -4
  2. package/dist/index.browser.cjs +401 -488
  3. package/dist/index.browser.cjs.map +1 -1
  4. package/dist/index.browser.js +403 -486
  5. package/dist/index.browser.js.map +1 -1
  6. package/dist/index.native.js +403 -486
  7. package/dist/index.native.js.map +1 -1
  8. package/dist/index.node.cjs +401 -488
  9. package/dist/index.node.cjs.map +1 -1
  10. package/dist/index.node.js +403 -486
  11. package/dist/index.node.js.map +1 -1
  12. package/dist/types/array.d.ts +39 -10
  13. package/dist/types/array.d.ts.map +1 -0
  14. package/dist/types/assertions.d.ts +1 -1
  15. package/dist/types/assertions.d.ts.map +1 -0
  16. package/dist/types/bit-array.d.ts +9 -9
  17. package/dist/types/bit-array.d.ts.map +1 -0
  18. package/dist/types/boolean.d.ts +22 -10
  19. package/dist/types/boolean.d.ts.map +1 -0
  20. package/dist/types/bytes.d.ts +18 -9
  21. package/dist/types/bytes.d.ts.map +1 -0
  22. package/dist/types/data-enum.d.ts +22 -24
  23. package/dist/types/data-enum.d.ts.map +1 -0
  24. package/dist/types/index.d.ts +13 -14
  25. package/dist/types/index.d.ts.map +1 -0
  26. package/dist/types/map.d.ts +28 -10
  27. package/dist/types/map.d.ts.map +1 -0
  28. package/dist/types/nullable.d.ts +33 -15
  29. package/dist/types/nullable.d.ts.map +1 -0
  30. package/dist/types/scalar-enum.d.ts +46 -15
  31. package/dist/types/scalar-enum.d.ts.map +1 -0
  32. package/dist/types/set.d.ts +28 -10
  33. package/dist/types/set.d.ts.map +1 -0
  34. package/dist/types/struct.d.ts +16 -21
  35. package/dist/types/struct.d.ts.map +1 -0
  36. package/dist/types/tuple.d.ts +22 -15
  37. package/dist/types/tuple.d.ts.map +1 -0
  38. package/dist/types/unit.d.ts +4 -12
  39. package/dist/types/unit.d.ts.map +1 -0
  40. package/dist/types/utils.d.ts +10 -2
  41. package/dist/types/utils.d.ts.map +1 -0
  42. package/package.json +13 -34
  43. package/dist/index.development.js +0 -865
  44. package/dist/index.development.js.map +0 -1
  45. package/dist/index.production.min.js +0 -51
  46. package/dist/types/array-like-codec-size.d.ts +0 -20
@@ -1,865 +0,0 @@
1
- this.globalThis = this.globalThis || {};
2
- this.globalThis.solanaWeb3 = (function (exports) {
3
- 'use strict';
4
-
5
- // ../codecs-core/dist/index.browser.js
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
- function assertFixedSizeCodec(data, message) {
18
- if (data.fixedSize === null) {
19
- throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one.");
20
- }
21
- }
22
- var mergeBytes = (byteArrays) => {
23
- const nonEmptyByteArrays = byteArrays.filter((arr) => arr.length);
24
- if (nonEmptyByteArrays.length === 0) {
25
- return byteArrays.length ? byteArrays[0] : new Uint8Array();
26
- }
27
- if (nonEmptyByteArrays.length === 1) {
28
- return nonEmptyByteArrays[0];
29
- }
30
- const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);
31
- const result = new Uint8Array(totalLength);
32
- let offset = 0;
33
- nonEmptyByteArrays.forEach((arr) => {
34
- result.set(arr, offset);
35
- offset += arr.length;
36
- });
37
- return result;
38
- };
39
- var padBytes = (bytes, length) => {
40
- if (bytes.length >= length)
41
- return bytes;
42
- const paddedBytes = new Uint8Array(length).fill(0);
43
- paddedBytes.set(bytes);
44
- return paddedBytes;
45
- };
46
- var fixBytes = (bytes, length) => padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);
47
- function combineCodec(encoder, decoder, description) {
48
- if (encoder.fixedSize !== decoder.fixedSize) {
49
- throw new Error(
50
- `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`
51
- );
52
- }
53
- if (encoder.maxSize !== decoder.maxSize) {
54
- throw new Error(
55
- `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`
56
- );
57
- }
58
- if (description === void 0 && encoder.description !== decoder.description) {
59
- throw new Error(
60
- `Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.`
61
- );
62
- }
63
- return {
64
- decode: decoder.decode,
65
- description: description ?? encoder.description,
66
- encode: encoder.encode,
67
- fixedSize: encoder.fixedSize,
68
- maxSize: encoder.maxSize
69
- };
70
- }
71
- function fixCodecHelper(data, fixedBytes, description) {
72
- return {
73
- description: description ?? `fixed(${fixedBytes}, ${data.description})`,
74
- fixedSize: fixedBytes,
75
- maxSize: fixedBytes
76
- };
77
- }
78
- function fixEncoder(encoder, fixedBytes, description) {
79
- return {
80
- ...fixCodecHelper(encoder, fixedBytes, description),
81
- encode: (value) => fixBytes(encoder.encode(value), fixedBytes)
82
- };
83
- }
84
- function fixDecoder(decoder, fixedBytes, description) {
85
- return {
86
- ...fixCodecHelper(decoder, fixedBytes, description),
87
- decode: (bytes, offset = 0) => {
88
- assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset);
89
- if (offset > 0 || bytes.length > fixedBytes) {
90
- bytes = bytes.slice(offset, offset + fixedBytes);
91
- }
92
- if (decoder.fixedSize !== null) {
93
- bytes = fixBytes(bytes, decoder.fixedSize);
94
- }
95
- const [value] = decoder.decode(bytes, 0);
96
- return [value, offset + fixedBytes];
97
- }
98
- };
99
- }
100
-
101
- // ../codecs-numbers/dist/index.browser.js
102
- function assertNumberIsBetweenForCodec(codecDescription, min, max, value) {
103
- if (value < min || value > max) {
104
- throw new Error(
105
- `Codec [${codecDescription}] expected number to be in the range [${min}, ${max}], got ${value}.`
106
- );
107
- }
108
- }
109
- function sharedNumberFactory(input) {
110
- let littleEndian;
111
- let defaultDescription = input.name;
112
- if (input.size > 1) {
113
- littleEndian = !("endian" in input.options) || input.options.endian === 0;
114
- defaultDescription += littleEndian ? "(le)" : "(be)";
115
- }
116
- return {
117
- description: input.options.description ?? defaultDescription,
118
- fixedSize: input.size,
119
- littleEndian,
120
- maxSize: input.size
121
- };
122
- }
123
- function numberEncoderFactory(input) {
124
- const codecData = sharedNumberFactory(input);
125
- return {
126
- description: codecData.description,
127
- encode(value) {
128
- if (input.range) {
129
- assertNumberIsBetweenForCodec(input.name, input.range[0], input.range[1], value);
130
- }
131
- const arrayBuffer = new ArrayBuffer(input.size);
132
- input.set(new DataView(arrayBuffer), value, codecData.littleEndian);
133
- return new Uint8Array(arrayBuffer);
134
- },
135
- fixedSize: codecData.fixedSize,
136
- maxSize: codecData.maxSize
137
- };
138
- }
139
- function numberDecoderFactory(input) {
140
- const codecData = sharedNumberFactory(input);
141
- return {
142
- decode(bytes, offset = 0) {
143
- assertByteArrayIsNotEmptyForCodec(codecData.description, bytes, offset);
144
- assertByteArrayHasEnoughBytesForCodec(codecData.description, input.size, bytes, offset);
145
- const view = new DataView(toArrayBuffer(bytes, offset, input.size));
146
- return [input.get(view, codecData.littleEndian), offset + input.size];
147
- },
148
- description: codecData.description,
149
- fixedSize: codecData.fixedSize,
150
- maxSize: codecData.maxSize
151
- };
152
- }
153
- function toArrayBuffer(bytes, offset, length) {
154
- const bytesOffset = bytes.byteOffset + (offset ?? 0);
155
- const bytesLength = length ?? bytes.byteLength;
156
- return bytes.buffer.slice(bytesOffset, bytesOffset + bytesLength);
157
- }
158
- var getU32Encoder = (options = {}) => numberEncoderFactory({
159
- name: "u32",
160
- options,
161
- range: [0, Number("0xffffffff")],
162
- set: (view, value, le) => view.setUint32(0, value, le),
163
- size: 4
164
- });
165
- var getU32Decoder = (options = {}) => numberDecoderFactory({
166
- get: (view, le) => view.getUint32(0, le),
167
- name: "u32",
168
- options,
169
- size: 4
170
- });
171
- var getU8Encoder = (options = {}) => numberEncoderFactory({
172
- name: "u8",
173
- options,
174
- range: [0, Number("0xff")],
175
- set: (view, value) => view.setUint8(0, value),
176
- size: 1
177
- });
178
- var getU8Decoder = (options = {}) => numberDecoderFactory({
179
- get: (view) => view.getUint8(0),
180
- name: "u8",
181
- options,
182
- size: 1
183
- });
184
-
185
- // src/utils.ts
186
- function maxCodecSizes(sizes) {
187
- return sizes.reduce(
188
- (all, size) => all === null || size === null ? null : Math.max(all, size),
189
- 0
190
- );
191
- }
192
- function sumCodecSizes(sizes) {
193
- return sizes.reduce((all, size) => all === null || size === null ? null : all + size, 0);
194
- }
195
-
196
- // src/array-like-codec-size.ts
197
- function decodeArrayLikeCodecSize(size, childrenSizes, bytes, offset) {
198
- if (typeof size === "number") {
199
- return [size, offset];
200
- }
201
- if (typeof size === "object") {
202
- return size.decode(bytes, offset);
203
- }
204
- if (size === "remainder") {
205
- const childrenSize = sumCodecSizes(childrenSizes);
206
- if (childrenSize === null) {
207
- throw new Error('Codecs of "remainder" size must have fixed-size items.');
208
- }
209
- const remainder = bytes.slice(offset).length;
210
- if (remainder % childrenSize !== 0) {
211
- throw new Error(
212
- `The remainder of the byte array (${remainder} bytes) cannot be split into chunks of ${childrenSize} bytes. Codecs of "remainder" size must have a remainder that is a multiple of its item size. In other words, ${remainder} modulo ${childrenSize} should be equal to zero.`
213
- );
214
- }
215
- return [remainder / childrenSize, offset];
216
- }
217
- throw new Error(`Unrecognized array-like codec size: ${JSON.stringify(size)}`);
218
- }
219
- function getArrayLikeCodecSizeDescription(size) {
220
- return typeof size === "object" ? size.description : `${size}`;
221
- }
222
- function getArrayLikeCodecSizeFromChildren(size, childrenSizes) {
223
- if (typeof size !== "number")
224
- return null;
225
- if (size === 0)
226
- return 0;
227
- const childrenSize = sumCodecSizes(childrenSizes);
228
- return childrenSize === null ? null : childrenSize * size;
229
- }
230
- function getArrayLikeCodecSizePrefix(size, realSize) {
231
- return typeof size === "object" ? size.encode(realSize) : new Uint8Array();
232
- }
233
-
234
- // src/assertions.ts
235
- function assertValidNumberOfItemsForCodec(codecDescription, expected, actual) {
236
- if (expected !== actual) {
237
- throw new Error(`Expected [${codecDescription}] to have ${expected} items, got ${actual}.`);
238
- }
239
- }
240
-
241
- // src/array.ts
242
- function arrayCodecHelper(item, size, description) {
243
- if (size === "remainder" && item.fixedSize === null) {
244
- throw new Error('Codecs of "remainder" size must have fixed-size items.');
245
- }
246
- return {
247
- description: description ?? `array(${item.description}; ${getArrayLikeCodecSizeDescription(size)})`,
248
- fixedSize: getArrayLikeCodecSizeFromChildren(size, [item.fixedSize]),
249
- maxSize: getArrayLikeCodecSizeFromChildren(size, [item.maxSize])
250
- };
251
- }
252
- function getArrayEncoder(item, options = {}) {
253
- const size = options.size ?? getU32Encoder();
254
- return {
255
- ...arrayCodecHelper(item, size, options.description),
256
- encode: (value) => {
257
- if (typeof size === "number") {
258
- assertValidNumberOfItemsForCodec("array", size, value.length);
259
- }
260
- return mergeBytes([getArrayLikeCodecSizePrefix(size, value.length), ...value.map((v) => item.encode(v))]);
261
- }
262
- };
263
- }
264
- function getArrayDecoder(item, options = {}) {
265
- const size = options.size ?? getU32Decoder();
266
- return {
267
- ...arrayCodecHelper(item, size, options.description),
268
- decode: (bytes, offset = 0) => {
269
- if (typeof size === "object" && bytes.slice(offset).length === 0) {
270
- return [[], offset];
271
- }
272
- const [resolvedSize, newOffset] = decodeArrayLikeCodecSize(size, [item.fixedSize], bytes, offset);
273
- offset = newOffset;
274
- const values = [];
275
- for (let i = 0; i < resolvedSize; i += 1) {
276
- const [value, newOffset2] = item.decode(bytes, offset);
277
- values.push(value);
278
- offset = newOffset2;
279
- }
280
- return [values, offset];
281
- }
282
- };
283
- }
284
- function getArrayCodec(item, options = {}) {
285
- return combineCodec(getArrayEncoder(item, options), getArrayDecoder(item, options));
286
- }
287
-
288
- // src/bit-array.ts
289
- var getBitArrayEncoder = (size, options = {}) => {
290
- const parsedOptions = typeof options === "boolean" ? { backward: options } : options;
291
- const backward = parsedOptions.backward ?? false;
292
- const backwardSuffix = backward ? "; backward" : "";
293
- return {
294
- description: parsedOptions.description ?? `bitArray(${size}${backwardSuffix})`,
295
- encode(value) {
296
- const bytes = [];
297
- for (let i = 0; i < size; i += 1) {
298
- let byte = 0;
299
- for (let j = 0; j < 8; j += 1) {
300
- const feature = Number(value[i * 8 + j] ?? 0);
301
- byte |= feature << (backward ? j : 7 - j);
302
- }
303
- if (backward) {
304
- bytes.unshift(byte);
305
- } else {
306
- bytes.push(byte);
307
- }
308
- }
309
- return new Uint8Array(bytes);
310
- },
311
- fixedSize: size,
312
- maxSize: size
313
- };
314
- };
315
- var getBitArrayDecoder = (size, options = {}) => {
316
- const parsedOptions = typeof options === "boolean" ? { backward: options } : options;
317
- const backward = parsedOptions.backward ?? false;
318
- const backwardSuffix = backward ? "; backward" : "";
319
- return {
320
- decode(bytes, offset = 0) {
321
- assertByteArrayHasEnoughBytesForCodec("bitArray", size, bytes, offset);
322
- const booleans = [];
323
- let slice = bytes.slice(offset, offset + size);
324
- slice = backward ? slice.reverse() : slice;
325
- slice.forEach((byte) => {
326
- for (let i = 0; i < 8; i += 1) {
327
- if (backward) {
328
- booleans.push(Boolean(byte & 1));
329
- byte >>= 1;
330
- } else {
331
- booleans.push(Boolean(byte & 128));
332
- byte <<= 1;
333
- }
334
- }
335
- });
336
- return [booleans, offset + size];
337
- },
338
- description: parsedOptions.description ?? `bitArray(${size}${backwardSuffix})`,
339
- fixedSize: size,
340
- maxSize: size
341
- };
342
- };
343
- var getBitArrayCodec = (size, options = {}) => combineCodec(getBitArrayEncoder(size, options), getBitArrayDecoder(size, options));
344
-
345
- // src/boolean.ts
346
- function getBooleanEncoder(options = {}) {
347
- const size = options.size ?? getU8Encoder();
348
- assertFixedSizeCodec(size, "Codec [bool] requires a fixed size.");
349
- return {
350
- description: options.description ?? `bool(${size.description})`,
351
- encode: (value) => size.encode(value ? 1 : 0),
352
- fixedSize: size.fixedSize,
353
- maxSize: size.fixedSize
354
- };
355
- }
356
- function getBooleanDecoder(options = {}) {
357
- const size = options.size ?? getU8Decoder();
358
- assertFixedSizeCodec(size, "Codec [bool] requires a fixed size.");
359
- return {
360
- decode: (bytes, offset = 0) => {
361
- assertByteArrayIsNotEmptyForCodec("bool", bytes, offset);
362
- const [value, vOffset] = size.decode(bytes, offset);
363
- return [value === 1, vOffset];
364
- },
365
- description: options.description ?? `bool(${size.description})`,
366
- fixedSize: size.fixedSize,
367
- maxSize: size.fixedSize
368
- };
369
- }
370
- function getBooleanCodec(options = {}) {
371
- return combineCodec(getBooleanEncoder(options), getBooleanDecoder(options));
372
- }
373
-
374
- // src/bytes.ts
375
- function getBytesEncoder(options = {}) {
376
- const size = options.size ?? "variable";
377
- const sizeDescription = typeof size === "object" ? size.description : `${size}`;
378
- const description = options.description ?? `bytes(${sizeDescription})`;
379
- const byteEncoder = {
380
- description,
381
- encode: (value) => value,
382
- fixedSize: null,
383
- maxSize: null
384
- };
385
- if (size === "variable") {
386
- return byteEncoder;
387
- }
388
- if (typeof size === "number") {
389
- return fixEncoder(byteEncoder, size, description);
390
- }
391
- return {
392
- ...byteEncoder,
393
- encode: (value) => {
394
- const contentBytes = byteEncoder.encode(value);
395
- const lengthBytes = size.encode(contentBytes.length);
396
- return mergeBytes([lengthBytes, contentBytes]);
397
- }
398
- };
399
- }
400
- function getBytesDecoder(options = {}) {
401
- const size = options.size ?? "variable";
402
- const sizeDescription = typeof size === "object" ? size.description : `${size}`;
403
- const description = options.description ?? `bytes(${sizeDescription})`;
404
- const byteDecoder = {
405
- decode: (bytes, offset = 0) => {
406
- const slice = bytes.slice(offset);
407
- return [slice, offset + slice.length];
408
- },
409
- description,
410
- fixedSize: null,
411
- maxSize: null
412
- };
413
- if (size === "variable") {
414
- return byteDecoder;
415
- }
416
- if (typeof size === "number") {
417
- return fixDecoder(byteDecoder, size, description);
418
- }
419
- return {
420
- ...byteDecoder,
421
- decode: (bytes, offset = 0) => {
422
- assertByteArrayIsNotEmptyForCodec("bytes", bytes, offset);
423
- const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);
424
- const length = Number(lengthBigInt);
425
- offset = lengthOffset;
426
- const contentBytes = bytes.slice(offset, offset + length);
427
- assertByteArrayHasEnoughBytesForCodec("bytes", length, contentBytes);
428
- const [value, contentOffset] = byteDecoder.decode(contentBytes);
429
- offset += contentOffset;
430
- return [value, offset];
431
- }
432
- };
433
- }
434
- function getBytesCodec(options = {}) {
435
- return combineCodec(getBytesEncoder(options), getBytesDecoder(options));
436
- }
437
-
438
- // src/data-enum.ts
439
- function dataEnumCodecHelper(variants, prefix, description) {
440
- const fieldDescriptions = variants.map(([name, codec]) => `${String(name)}${codec ? `: ${codec.description}` : ""}`).join(", ");
441
- const allVariantHaveTheSameFixedSize = variants.every((one, _i, all) => one[1].fixedSize === all[0][1].fixedSize);
442
- const fixedVariantSize = allVariantHaveTheSameFixedSize ? variants[0][1].fixedSize : null;
443
- const maxVariantSize = maxCodecSizes(variants.map(([, field]) => field.maxSize));
444
- return {
445
- description: description ?? `dataEnum(${fieldDescriptions}; ${prefix.description})`,
446
- fixedSize: variants.length === 0 ? prefix.fixedSize : sumCodecSizes([prefix.fixedSize, fixedVariantSize]),
447
- maxSize: variants.length === 0 ? prefix.maxSize : sumCodecSizes([prefix.maxSize, maxVariantSize])
448
- };
449
- }
450
- function getDataEnumEncoder(variants, options = {}) {
451
- const prefix = options.size ?? getU8Encoder();
452
- return {
453
- ...dataEnumCodecHelper(variants, prefix, options.description),
454
- encode: (variant) => {
455
- const discriminator = variants.findIndex(([key]) => variant.__kind === key);
456
- if (discriminator < 0) {
457
- throw new Error(
458
- `Invalid data enum variant. Expected one of [${variants.map(([key]) => key).join(", ")}], got "${variant.__kind}".`
459
- );
460
- }
461
- const variantPrefix = prefix.encode(discriminator);
462
- const variantSerializer = variants[discriminator][1];
463
- const variantBytes = variantSerializer.encode(variant);
464
- return mergeBytes([variantPrefix, variantBytes]);
465
- }
466
- };
467
- }
468
- function getDataEnumDecoder(variants, options = {}) {
469
- const prefix = options.size ?? getU8Decoder();
470
- return {
471
- ...dataEnumCodecHelper(variants, prefix, options.description),
472
- decode: (bytes, offset = 0) => {
473
- assertByteArrayIsNotEmptyForCodec("dataEnum", bytes, offset);
474
- const [discriminator, dOffset] = prefix.decode(bytes, offset);
475
- offset = dOffset;
476
- const variantField = variants[Number(discriminator)] ?? null;
477
- if (!variantField) {
478
- throw new Error(
479
- `Enum discriminator out of range. Expected a number between 0 and ${variants.length - 1}, got ${discriminator}.`
480
- );
481
- }
482
- const [variant, vOffset] = variantField[1].decode(bytes, offset);
483
- offset = vOffset;
484
- return [{ __kind: variantField[0], ...variant ?? {} }, offset];
485
- }
486
- };
487
- }
488
- function getDataEnumCodec(variants, options = {}) {
489
- return combineCodec(getDataEnumEncoder(variants, options), getDataEnumDecoder(variants, options));
490
- }
491
-
492
- // src/map.ts
493
- function mapCodecHelper(key, value, size, description) {
494
- if (size === "remainder" && (key.fixedSize === null || value.fixedSize === null)) {
495
- throw new Error('Codecs of "remainder" size must have fixed-size items.');
496
- }
497
- return {
498
- description: description ?? `map(${key.description}, ${value.description}; ${getArrayLikeCodecSizeDescription(size)})`,
499
- fixedSize: getArrayLikeCodecSizeFromChildren(size, [key.fixedSize, value.fixedSize]),
500
- maxSize: getArrayLikeCodecSizeFromChildren(size, [key.maxSize, value.maxSize])
501
- };
502
- }
503
- function getMapEncoder(key, value, options = {}) {
504
- const size = options.size ?? getU32Encoder();
505
- return {
506
- ...mapCodecHelper(key, value, size, options.description),
507
- encode: (map) => {
508
- if (typeof size === "number") {
509
- assertValidNumberOfItemsForCodec("map", size, map.size);
510
- }
511
- const itemBytes = Array.from(map, ([k, v]) => mergeBytes([key.encode(k), value.encode(v)]));
512
- return mergeBytes([getArrayLikeCodecSizePrefix(size, map.size), ...itemBytes]);
513
- }
514
- };
515
- }
516
- function getMapDecoder(key, value, options = {}) {
517
- const size = options.size ?? getU32Decoder();
518
- return {
519
- ...mapCodecHelper(key, value, size, options.description),
520
- decode: (bytes, offset = 0) => {
521
- const map = /* @__PURE__ */ new Map();
522
- if (typeof size === "object" && bytes.slice(offset).length === 0) {
523
- return [map, offset];
524
- }
525
- const [resolvedSize, newOffset] = decodeArrayLikeCodecSize(
526
- size,
527
- [key.fixedSize, value.fixedSize],
528
- bytes,
529
- offset
530
- );
531
- offset = newOffset;
532
- for (let i = 0; i < resolvedSize; i += 1) {
533
- const [decodedKey, kOffset] = key.decode(bytes, offset);
534
- offset = kOffset;
535
- const [decodedValue, vOffset] = value.decode(bytes, offset);
536
- offset = vOffset;
537
- map.set(decodedKey, decodedValue);
538
- }
539
- return [map, offset];
540
- }
541
- };
542
- }
543
- function getMapCodec(key, value, options = {}) {
544
- return combineCodec(getMapEncoder(key, value, options), getMapDecoder(key, value, options));
545
- }
546
-
547
- // src/nullable.ts
548
- function nullableCodecHelper(item, prefix, fixed, description) {
549
- let descriptionSuffix = `; ${prefix.description}`;
550
- let fixedSize = item.fixedSize === 0 ? prefix.fixedSize : null;
551
- if (fixed) {
552
- assertFixedSizeCodec(item, "Fixed nullables can only be used with fixed-size codecs.");
553
- assertFixedSizeCodec(prefix, "Fixed nullables can only be used with fixed-size prefix.");
554
- descriptionSuffix += "; fixed";
555
- fixedSize = prefix.fixedSize + item.fixedSize;
556
- }
557
- return {
558
- description: description ?? `nullable(${item.description + descriptionSuffix})`,
559
- fixedSize,
560
- maxSize: sumCodecSizes([prefix.maxSize, item.maxSize])
561
- };
562
- }
563
- function getNullableEncoder(item, options = {}) {
564
- const prefix = options.prefix ?? getU8Encoder();
565
- const fixed = options.fixed ?? false;
566
- return {
567
- ...nullableCodecHelper(item, prefix, fixed, options.description),
568
- encode: (option) => {
569
- const prefixByte = prefix.encode(Number(option !== null));
570
- let itemBytes = option !== null ? item.encode(option) : new Uint8Array();
571
- itemBytes = fixed ? fixBytes(itemBytes, item.fixedSize) : itemBytes;
572
- return mergeBytes([prefixByte, itemBytes]);
573
- }
574
- };
575
- }
576
- function getNullableDecoder(item, options = {}) {
577
- const prefix = options.prefix ?? getU8Decoder();
578
- const fixed = options.fixed ?? false;
579
- return {
580
- ...nullableCodecHelper(item, prefix, fixed, options.description),
581
- decode: (bytes, offset = 0) => {
582
- if (bytes.length - offset <= 0) {
583
- return [null, offset];
584
- }
585
- const fixedOffset = offset + (prefix.fixedSize ?? 0) + (item.fixedSize ?? 0);
586
- const [isSome, prefixOffset] = prefix.decode(bytes, offset);
587
- offset = prefixOffset;
588
- if (isSome === 0) {
589
- return [null, fixed ? fixedOffset : offset];
590
- }
591
- const [value, newOffset] = item.decode(bytes, offset);
592
- offset = newOffset;
593
- return [value, fixed ? fixedOffset : offset];
594
- }
595
- };
596
- }
597
- function getNullableCodec(item, options = {}) {
598
- return combineCodec(getNullableEncoder(item, options), getNullableDecoder(item, options));
599
- }
600
-
601
- // src/scalar-enum.ts
602
- function scalarEnumCoderHelper(constructor, prefix, description) {
603
- const enumKeys = Object.keys(constructor);
604
- const enumValues = Object.values(constructor);
605
- const isNumericEnum = enumValues.some((v) => typeof v === "number");
606
- const valueDescriptions = enumValues.filter((v) => typeof v === "string").join(", ");
607
- const minRange = 0;
608
- const maxRange = isNumericEnum ? enumValues.length / 2 - 1 : enumValues.length - 1;
609
- const stringValues = isNumericEnum ? [...enumKeys] : [.../* @__PURE__ */ new Set([...enumKeys, ...enumValues])];
610
- return {
611
- description: description ?? `enum(${valueDescriptions}; ${prefix.description})`,
612
- enumKeys,
613
- enumValues,
614
- fixedSize: prefix.fixedSize,
615
- isNumericEnum,
616
- maxRange,
617
- maxSize: prefix.maxSize,
618
- minRange,
619
- stringValues
620
- };
621
- }
622
- function getScalarEnumEncoder(constructor, options = {}) {
623
- const prefix = options.size ?? getU8Encoder();
624
- const { description, fixedSize, maxSize, minRange, maxRange, stringValues, enumKeys, enumValues } = scalarEnumCoderHelper(constructor, prefix, options.description);
625
- return {
626
- description,
627
- encode: (value) => {
628
- const isInvalidNumber = typeof value === "number" && (value < minRange || value > maxRange);
629
- const isInvalidString = typeof value === "string" && !stringValues.includes(value);
630
- if (isInvalidNumber || isInvalidString) {
631
- throw new Error(
632
- `Invalid scalar enum variant. Expected one of [${stringValues.join(", ")}] or a number between ${minRange} and ${maxRange}, got "${value}".`
633
- );
634
- }
635
- if (typeof value === "number")
636
- return prefix.encode(value);
637
- const valueIndex = enumValues.indexOf(value);
638
- if (valueIndex >= 0)
639
- return prefix.encode(valueIndex);
640
- return prefix.encode(enumKeys.indexOf(value));
641
- },
642
- fixedSize,
643
- maxSize
644
- };
645
- }
646
- function getScalarEnumDecoder(constructor, options = {}) {
647
- const prefix = options.size ?? getU8Decoder();
648
- const { description, fixedSize, maxSize, minRange, maxRange, isNumericEnum, enumValues } = scalarEnumCoderHelper(
649
- constructor,
650
- prefix,
651
- options.description
652
- );
653
- return {
654
- decode: (bytes, offset = 0) => {
655
- assertByteArrayIsNotEmptyForCodec("enum", bytes, offset);
656
- const [value, newOffset] = prefix.decode(bytes, offset);
657
- const valueAsNumber = Number(value);
658
- offset = newOffset;
659
- if (valueAsNumber < minRange || valueAsNumber > maxRange) {
660
- throw new Error(
661
- `Enum discriminator out of range. Expected a number between ${minRange} and ${maxRange}, got ${valueAsNumber}.`
662
- );
663
- }
664
- return [isNumericEnum ? valueAsNumber : enumValues[valueAsNumber], offset];
665
- },
666
- description,
667
- fixedSize,
668
- maxSize
669
- };
670
- }
671
- function getScalarEnumCodec(constructor, options = {}) {
672
- return combineCodec(getScalarEnumEncoder(constructor, options), getScalarEnumDecoder(constructor, options));
673
- }
674
-
675
- // src/set.ts
676
- function setCodecHelper(item, size, description) {
677
- if (size === "remainder" && item.fixedSize === null) {
678
- throw new Error('Codecs of "remainder" size must have fixed-size items.');
679
- }
680
- return {
681
- description: description ?? `set(${item.description}; ${getArrayLikeCodecSizeDescription(size)})`,
682
- fixedSize: getArrayLikeCodecSizeFromChildren(size, [item.fixedSize]),
683
- maxSize: getArrayLikeCodecSizeFromChildren(size, [item.maxSize])
684
- };
685
- }
686
- function getSetEncoder(item, options = {}) {
687
- const size = options.size ?? getU32Encoder();
688
- return {
689
- ...setCodecHelper(item, size, options.description),
690
- encode: (set) => {
691
- if (typeof size === "number" && set.size !== size) {
692
- assertValidNumberOfItemsForCodec("set", size, set.size);
693
- }
694
- const itemBytes = Array.from(set, (value) => item.encode(value));
695
- return mergeBytes([getArrayLikeCodecSizePrefix(size, set.size), ...itemBytes]);
696
- }
697
- };
698
- }
699
- function getSetDecoder(item, options = {}) {
700
- const size = options.size ?? getU32Decoder();
701
- return {
702
- ...setCodecHelper(item, size, options.description),
703
- decode: (bytes, offset = 0) => {
704
- const set = /* @__PURE__ */ new Set();
705
- if (typeof size === "object" && bytes.slice(offset).length === 0) {
706
- return [set, offset];
707
- }
708
- const [resolvedSize, newOffset] = decodeArrayLikeCodecSize(size, [item.fixedSize], bytes, offset);
709
- offset = newOffset;
710
- for (let i = 0; i < resolvedSize; i += 1) {
711
- const [value, newOffset2] = item.decode(bytes, offset);
712
- offset = newOffset2;
713
- set.add(value);
714
- }
715
- return [set, offset];
716
- }
717
- };
718
- }
719
- function getSetCodec(item, options = {}) {
720
- return combineCodec(getSetEncoder(item, options), getSetDecoder(item, options));
721
- }
722
-
723
- // src/struct.ts
724
- function structCodecHelper(fields, description) {
725
- const fieldDescriptions = fields.map(([name, codec]) => `${String(name)}: ${codec.description}`).join(", ");
726
- return {
727
- description: description ?? `struct(${fieldDescriptions})`,
728
- fixedSize: sumCodecSizes(fields.map(([, field]) => field.fixedSize)),
729
- maxSize: sumCodecSizes(fields.map(([, field]) => field.maxSize))
730
- };
731
- }
732
- function getStructEncoder(fields, options = {}) {
733
- return {
734
- ...structCodecHelper(fields, options.description),
735
- encode: (struct) => {
736
- const fieldBytes = fields.map(([key, codec]) => codec.encode(struct[key]));
737
- return mergeBytes(fieldBytes);
738
- }
739
- };
740
- }
741
- function getStructDecoder(fields, options = {}) {
742
- return {
743
- ...structCodecHelper(fields, options.description),
744
- decode: (bytes, offset = 0) => {
745
- const struct = {};
746
- fields.forEach(([key, codec]) => {
747
- const [value, newOffset] = codec.decode(bytes, offset);
748
- offset = newOffset;
749
- struct[key] = value;
750
- });
751
- return [struct, offset];
752
- }
753
- };
754
- }
755
- function getStructCodec(fields, options = {}) {
756
- return combineCodec(getStructEncoder(fields, options), getStructDecoder(fields, options));
757
- }
758
-
759
- // src/tuple.ts
760
- function tupleCodecHelper(items, description) {
761
- const itemDescriptions = items.map((item) => item.description).join(", ");
762
- return {
763
- description: description ?? `tuple(${itemDescriptions})`,
764
- fixedSize: sumCodecSizes(items.map((item) => item.fixedSize)),
765
- maxSize: sumCodecSizes(items.map((item) => item.maxSize))
766
- };
767
- }
768
- function getTupleEncoder(items, options = {}) {
769
- return {
770
- ...tupleCodecHelper(items, options.description),
771
- encode: (value) => {
772
- assertValidNumberOfItemsForCodec("tuple", items.length, value.length);
773
- return mergeBytes(items.map((item, index) => item.encode(value[index])));
774
- }
775
- };
776
- }
777
- function getTupleDecoder(items, options = {}) {
778
- return {
779
- ...tupleCodecHelper(items, options.description),
780
- decode: (bytes, offset = 0) => {
781
- const values = [];
782
- items.forEach((codec) => {
783
- const [newValue, newOffset] = codec.decode(bytes, offset);
784
- values.push(newValue);
785
- offset = newOffset;
786
- });
787
- return [values, offset];
788
- }
789
- };
790
- }
791
- function getTupleCodec(items, options = {}) {
792
- return combineCodec(
793
- getTupleEncoder(items, options),
794
- getTupleDecoder(items, options)
795
- );
796
- }
797
-
798
- // src/unit.ts
799
- function getUnitEncoder(options = {}) {
800
- return {
801
- description: options.description ?? "unit",
802
- encode: () => new Uint8Array(),
803
- fixedSize: 0,
804
- maxSize: 0
805
- };
806
- }
807
- function getUnitDecoder(options = {}) {
808
- return {
809
- decode: (_bytes, offset = 0) => [void 0, offset],
810
- description: options.description ?? "unit",
811
- fixedSize: 0,
812
- maxSize: 0
813
- };
814
- }
815
- function getUnitCodec(options = {}) {
816
- return combineCodec(getUnitEncoder(options), getUnitDecoder(options));
817
- }
818
-
819
- exports.assertValidNumberOfItemsForCodec = assertValidNumberOfItemsForCodec;
820
- exports.decodeArrayLikeCodecSize = decodeArrayLikeCodecSize;
821
- exports.getArrayCodec = getArrayCodec;
822
- exports.getArrayDecoder = getArrayDecoder;
823
- exports.getArrayEncoder = getArrayEncoder;
824
- exports.getArrayLikeCodecSizeDescription = getArrayLikeCodecSizeDescription;
825
- exports.getArrayLikeCodecSizeFromChildren = getArrayLikeCodecSizeFromChildren;
826
- exports.getArrayLikeCodecSizePrefix = getArrayLikeCodecSizePrefix;
827
- exports.getBitArrayCodec = getBitArrayCodec;
828
- exports.getBitArrayDecoder = getBitArrayDecoder;
829
- exports.getBitArrayEncoder = getBitArrayEncoder;
830
- exports.getBooleanCodec = getBooleanCodec;
831
- exports.getBooleanDecoder = getBooleanDecoder;
832
- exports.getBooleanEncoder = getBooleanEncoder;
833
- exports.getBytesCodec = getBytesCodec;
834
- exports.getBytesDecoder = getBytesDecoder;
835
- exports.getBytesEncoder = getBytesEncoder;
836
- exports.getDataEnumCodec = getDataEnumCodec;
837
- exports.getDataEnumDecoder = getDataEnumDecoder;
838
- exports.getDataEnumEncoder = getDataEnumEncoder;
839
- exports.getMapCodec = getMapCodec;
840
- exports.getMapDecoder = getMapDecoder;
841
- exports.getMapEncoder = getMapEncoder;
842
- exports.getNullableCodec = getNullableCodec;
843
- exports.getNullableDecoder = getNullableDecoder;
844
- exports.getNullableEncoder = getNullableEncoder;
845
- exports.getScalarEnumCodec = getScalarEnumCodec;
846
- exports.getScalarEnumDecoder = getScalarEnumDecoder;
847
- exports.getScalarEnumEncoder = getScalarEnumEncoder;
848
- exports.getSetCodec = getSetCodec;
849
- exports.getSetDecoder = getSetDecoder;
850
- exports.getSetEncoder = getSetEncoder;
851
- exports.getStructCodec = getStructCodec;
852
- exports.getStructDecoder = getStructDecoder;
853
- exports.getStructEncoder = getStructEncoder;
854
- exports.getTupleCodec = getTupleCodec;
855
- exports.getTupleDecoder = getTupleDecoder;
856
- exports.getTupleEncoder = getTupleEncoder;
857
- exports.getUnitCodec = getUnitCodec;
858
- exports.getUnitDecoder = getUnitDecoder;
859
- exports.getUnitEncoder = getUnitEncoder;
860
-
861
- return exports;
862
-
863
- })({});
864
- //# sourceMappingURL=out.js.map
865
- //# sourceMappingURL=index.development.js.map