@solana/codecs-data-structures 2.0.0-experimental.e1ca396 → 2.0.0-experimental.e374ac6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +482 -4
- package/dist/index.browser.cjs +65 -63
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +66 -64
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +66 -64
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +65 -63
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +66 -64
- package/dist/index.node.js.map +1 -1
- package/dist/types/array.d.ts +3 -18
- package/dist/types/array.d.ts.map +1 -1
- package/dist/types/assertions.d.ts.map +1 -1
- package/dist/types/data-enum.d.ts +16 -18
- package/dist/types/data-enum.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -13
- package/dist/types/map.d.ts +4 -19
- package/dist/types/map.d.ts.map +1 -1
- package/dist/types/scalar-enum.d.ts +36 -17
- package/dist/types/scalar-enum.d.ts.map +1 -1
- package/dist/types/set.d.ts +4 -19
- package/dist/types/set.d.ts.map +1 -1
- package/dist/types/struct.d.ts +15 -30
- package/dist/types/struct.d.ts.map +1 -1
- package/package.json +13 -34
- package/dist/index.development.js +0 -800
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -47
package/dist/index.browser.cjs
CHANGED
|
@@ -2,13 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
var codecsCore = require('@solana/codecs-core');
|
|
4
4
|
var codecsNumbers = require('@solana/codecs-numbers');
|
|
5
|
+
var errors = require('@solana/errors');
|
|
5
6
|
|
|
6
7
|
// src/array.ts
|
|
7
|
-
|
|
8
|
-
// src/assertions.ts
|
|
9
8
|
function assertValidNumberOfItemsForCodec(codecDescription, expected, actual) {
|
|
10
9
|
if (expected !== actual) {
|
|
11
|
-
throw new
|
|
10
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS_WRONG_NUMBER_OF_ITEMS, {
|
|
11
|
+
actual,
|
|
12
|
+
codecDescription,
|
|
13
|
+
expected
|
|
14
|
+
});
|
|
12
15
|
}
|
|
13
16
|
}
|
|
14
17
|
function maxCodecSizes(sizes) {
|
|
@@ -30,9 +33,6 @@ function getMaxSize(codec) {
|
|
|
30
33
|
// src/array.ts
|
|
31
34
|
function getArrayEncoder(item, config = {}) {
|
|
32
35
|
const size = config.size ?? codecsNumbers.getU32Encoder();
|
|
33
|
-
if (size === "remainder") {
|
|
34
|
-
codecsCore.assertIsFixedSize(item, 'Codecs of "remainder" size must have fixed-size items.');
|
|
35
|
-
}
|
|
36
36
|
const fixedSize = computeArrayLikeCodecSize(size, getFixedSize(item));
|
|
37
37
|
const maxSize = computeArrayLikeCodecSize(size, getMaxSize(item)) ?? void 0;
|
|
38
38
|
return codecsCore.createEncoder({
|
|
@@ -59,9 +59,6 @@ function getArrayEncoder(item, config = {}) {
|
|
|
59
59
|
}
|
|
60
60
|
function getArrayDecoder(item, config = {}) {
|
|
61
61
|
const size = config.size ?? codecsNumbers.getU32Decoder();
|
|
62
|
-
if (size === "remainder") {
|
|
63
|
-
codecsCore.assertIsFixedSize(item, 'Codecs of "remainder" size must have fixed-size items.');
|
|
64
|
-
}
|
|
65
62
|
const itemSize = getFixedSize(item);
|
|
66
63
|
const fixedSize = computeArrayLikeCodecSize(size, itemSize);
|
|
67
64
|
const maxSize = computeArrayLikeCodecSize(size, getMaxSize(item)) ?? void 0;
|
|
@@ -72,7 +69,15 @@ function getArrayDecoder(item, config = {}) {
|
|
|
72
69
|
if (typeof size === "object" && bytes.slice(offset).length === 0) {
|
|
73
70
|
return [array, offset];
|
|
74
71
|
}
|
|
75
|
-
|
|
72
|
+
if (size === "remainder") {
|
|
73
|
+
while (offset < bytes.length) {
|
|
74
|
+
const [value, newOffset2] = item.read(bytes, offset);
|
|
75
|
+
offset = newOffset2;
|
|
76
|
+
array.push(value);
|
|
77
|
+
}
|
|
78
|
+
return [array, offset];
|
|
79
|
+
}
|
|
80
|
+
const [resolvedSize, newOffset] = typeof size === "number" ? [size, offset] : size.read(bytes, offset);
|
|
76
81
|
offset = newOffset;
|
|
77
82
|
for (let i = 0; i < resolvedSize; i += 1) {
|
|
78
83
|
const [value, newOffset2] = item.read(bytes, offset);
|
|
@@ -86,27 +91,6 @@ function getArrayDecoder(item, config = {}) {
|
|
|
86
91
|
function getArrayCodec(item, config = {}) {
|
|
87
92
|
return codecsCore.combineCodec(getArrayEncoder(item, config), getArrayDecoder(item, config));
|
|
88
93
|
}
|
|
89
|
-
function readArrayLikeCodecSize(size, itemSize, bytes, offset) {
|
|
90
|
-
if (typeof size === "number") {
|
|
91
|
-
return [size, offset];
|
|
92
|
-
}
|
|
93
|
-
if (typeof size === "object") {
|
|
94
|
-
return size.read(bytes, offset);
|
|
95
|
-
}
|
|
96
|
-
if (size === "remainder") {
|
|
97
|
-
if (itemSize === null) {
|
|
98
|
-
throw new Error('Codecs of "remainder" size must have fixed-size items.');
|
|
99
|
-
}
|
|
100
|
-
const remainder = Math.max(0, bytes.length - offset);
|
|
101
|
-
if (remainder % itemSize !== 0) {
|
|
102
|
-
throw new Error(
|
|
103
|
-
`The remainder of the byte array (${remainder} bytes) cannot be split into chunks of ${itemSize} bytes. Codecs of "remainder" size must have a remainder that is a multiple of its item size. In other words, ${remainder} modulo ${itemSize} should be equal to zero.`
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
return [remainder / itemSize, offset];
|
|
107
|
-
}
|
|
108
|
-
throw new Error(`Unrecognized array-like codec size: ${JSON.stringify(size)}`);
|
|
109
|
-
}
|
|
110
94
|
function computeArrayLikeCodecSize(size, itemSize) {
|
|
111
95
|
if (typeof size !== "number")
|
|
112
96
|
return null;
|
|
@@ -168,12 +152,12 @@ function getBitArrayCodec(size, config = {}) {
|
|
|
168
152
|
}
|
|
169
153
|
function getBooleanEncoder(config = {}) {
|
|
170
154
|
const size = config.size ?? codecsNumbers.getU8Encoder();
|
|
171
|
-
codecsCore.assertIsFixedSize(size
|
|
155
|
+
codecsCore.assertIsFixedSize(size);
|
|
172
156
|
return codecsCore.mapEncoder(size, (value) => value ? 1 : 0);
|
|
173
157
|
}
|
|
174
158
|
function getBooleanDecoder(config = {}) {
|
|
175
159
|
const size = config.size ?? codecsNumbers.getU8Decoder();
|
|
176
|
-
codecsCore.assertIsFixedSize(size
|
|
160
|
+
codecsCore.assertIsFixedSize(size);
|
|
177
161
|
return codecsCore.mapDecoder(size, (value) => Number(value) === 1);
|
|
178
162
|
}
|
|
179
163
|
function getBooleanCodec(config = {}) {
|
|
@@ -264,9 +248,11 @@ function getDataEnumDecoder(variants, config = {}) {
|
|
|
264
248
|
offset = dOffset;
|
|
265
249
|
const variantField = variants[Number(discriminator)] ?? null;
|
|
266
250
|
if (!variantField) {
|
|
267
|
-
throw new
|
|
268
|
-
|
|
269
|
-
|
|
251
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS_ENUM_DISCRIMINATOR_OUT_OF_RANGE, {
|
|
252
|
+
discriminator,
|
|
253
|
+
maxRange: variants.length - 1,
|
|
254
|
+
minRange: 0
|
|
255
|
+
});
|
|
270
256
|
}
|
|
271
257
|
const [variant, vOffset] = variantField[1].read(bytes, offset);
|
|
272
258
|
offset = vOffset;
|
|
@@ -275,7 +261,10 @@ function getDataEnumDecoder(variants, config = {}) {
|
|
|
275
261
|
});
|
|
276
262
|
}
|
|
277
263
|
function getDataEnumCodec(variants, config = {}) {
|
|
278
|
-
return codecsCore.combineCodec(
|
|
264
|
+
return codecsCore.combineCodec(
|
|
265
|
+
getDataEnumEncoder(variants, config),
|
|
266
|
+
getDataEnumDecoder(variants, config)
|
|
267
|
+
);
|
|
279
268
|
}
|
|
280
269
|
function getDataEnumFixedSize(variants, prefix) {
|
|
281
270
|
if (variants.length === 0)
|
|
@@ -297,9 +286,10 @@ function getDataEnumMaxSize(variants, prefix) {
|
|
|
297
286
|
function getVariantDiscriminator(variants, variant) {
|
|
298
287
|
const discriminator = variants.findIndex(([key]) => variant.__kind === key);
|
|
299
288
|
if (discriminator < 0) {
|
|
300
|
-
throw new
|
|
301
|
-
|
|
302
|
-
|
|
289
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS_INVALID_DATA_ENUM_VARIANT, {
|
|
290
|
+
value: variant.__kind,
|
|
291
|
+
variants: variants.map(([key]) => key)
|
|
292
|
+
});
|
|
303
293
|
}
|
|
304
294
|
return discriminator;
|
|
305
295
|
}
|
|
@@ -364,8 +354,8 @@ function getNullableEncoder(item, config = {}) {
|
|
|
364
354
|
const fixed = config.fixed ?? false;
|
|
365
355
|
const isZeroSizeItem = codecsCore.isFixedSize(item) && codecsCore.isFixedSize(prefix) && item.fixedSize === 0;
|
|
366
356
|
if (fixed || isZeroSizeItem) {
|
|
367
|
-
codecsCore.assertIsFixedSize(item
|
|
368
|
-
codecsCore.assertIsFixedSize(prefix
|
|
357
|
+
codecsCore.assertIsFixedSize(item);
|
|
358
|
+
codecsCore.assertIsFixedSize(prefix);
|
|
369
359
|
const fixedSize = prefix.fixedSize + item.fixedSize;
|
|
370
360
|
return codecsCore.createEncoder({
|
|
371
361
|
fixedSize,
|
|
@@ -396,8 +386,8 @@ function getNullableDecoder(item, config = {}) {
|
|
|
396
386
|
let fixedSize = null;
|
|
397
387
|
const isZeroSizeItem = codecsCore.isFixedSize(item) && codecsCore.isFixedSize(prefix) && item.fixedSize === 0;
|
|
398
388
|
if (fixed || isZeroSizeItem) {
|
|
399
|
-
codecsCore.assertIsFixedSize(item
|
|
400
|
-
codecsCore.assertIsFixedSize(prefix
|
|
389
|
+
codecsCore.assertIsFixedSize(item);
|
|
390
|
+
codecsCore.assertIsFixedSize(prefix);
|
|
401
391
|
fixedSize = prefix.fixedSize + item.fixedSize;
|
|
402
392
|
}
|
|
403
393
|
return codecsCore.createDecoder({
|
|
@@ -421,14 +411,17 @@ function getNullableCodec(item, config = {}) {
|
|
|
421
411
|
}
|
|
422
412
|
function getScalarEnumEncoder(constructor, config = {}) {
|
|
423
413
|
const prefix = config.size ?? codecsNumbers.getU8Encoder();
|
|
424
|
-
const { minRange, maxRange,
|
|
414
|
+
const { minRange, maxRange, allStringInputs, enumKeys, enumValues } = getScalarEnumStats(constructor);
|
|
425
415
|
return codecsCore.mapEncoder(prefix, (value) => {
|
|
426
416
|
const isInvalidNumber = typeof value === "number" && (value < minRange || value > maxRange);
|
|
427
|
-
const isInvalidString = typeof value === "string" && !
|
|
417
|
+
const isInvalidString = typeof value === "string" && !allStringInputs.includes(value);
|
|
428
418
|
if (isInvalidNumber || isInvalidString) {
|
|
429
|
-
throw new
|
|
430
|
-
|
|
431
|
-
|
|
419
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS_INVALID_SCALAR_ENUM_VARIANT, {
|
|
420
|
+
maxRange,
|
|
421
|
+
minRange,
|
|
422
|
+
value,
|
|
423
|
+
variants: allStringInputs
|
|
424
|
+
});
|
|
432
425
|
}
|
|
433
426
|
if (typeof value === "number")
|
|
434
427
|
return value;
|
|
@@ -440,34 +433,40 @@ function getScalarEnumEncoder(constructor, config = {}) {
|
|
|
440
433
|
}
|
|
441
434
|
function getScalarEnumDecoder(constructor, config = {}) {
|
|
442
435
|
const prefix = config.size ?? codecsNumbers.getU8Decoder();
|
|
443
|
-
const { minRange, maxRange,
|
|
436
|
+
const { minRange, maxRange, enumKeys } = getScalarEnumStats(constructor);
|
|
444
437
|
return codecsCore.mapDecoder(prefix, (value) => {
|
|
445
438
|
const valueAsNumber = Number(value);
|
|
446
439
|
if (valueAsNumber < minRange || valueAsNumber > maxRange) {
|
|
447
|
-
throw new
|
|
448
|
-
|
|
449
|
-
|
|
440
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS_ENUM_DISCRIMINATOR_OUT_OF_RANGE, {
|
|
441
|
+
discriminator: valueAsNumber,
|
|
442
|
+
maxRange,
|
|
443
|
+
minRange
|
|
444
|
+
});
|
|
450
445
|
}
|
|
451
|
-
return
|
|
446
|
+
return constructor[enumKeys[valueAsNumber]];
|
|
452
447
|
});
|
|
453
448
|
}
|
|
454
449
|
function getScalarEnumCodec(constructor, config = {}) {
|
|
455
450
|
return codecsCore.combineCodec(getScalarEnumEncoder(constructor, config), getScalarEnumDecoder(constructor, config));
|
|
456
451
|
}
|
|
457
452
|
function getScalarEnumStats(constructor) {
|
|
458
|
-
const
|
|
459
|
-
const
|
|
460
|
-
|
|
453
|
+
const numericValues = Object.values(constructor).filter((v) => typeof v === "number");
|
|
454
|
+
const deduplicatedConstructor = Object.fromEntries(
|
|
455
|
+
Object.entries(constructor).slice(numericValues.length)
|
|
456
|
+
);
|
|
457
|
+
const enumKeys = Object.keys(deduplicatedConstructor);
|
|
458
|
+
const enumValues = Object.values(deduplicatedConstructor);
|
|
461
459
|
const minRange = 0;
|
|
462
|
-
const maxRange =
|
|
463
|
-
const
|
|
460
|
+
const maxRange = enumValues.length - 1;
|
|
461
|
+
const allStringInputs = [
|
|
462
|
+
.../* @__PURE__ */ new Set([...enumKeys, ...enumValues.filter((v) => typeof v === "string")])
|
|
463
|
+
];
|
|
464
464
|
return {
|
|
465
|
+
allStringInputs,
|
|
465
466
|
enumKeys,
|
|
466
467
|
enumValues,
|
|
467
|
-
isNumericEnum,
|
|
468
468
|
maxRange,
|
|
469
|
-
minRange
|
|
470
|
-
stringValues
|
|
469
|
+
minRange
|
|
471
470
|
};
|
|
472
471
|
}
|
|
473
472
|
function getSetEncoder(item, config = {}) {
|
|
@@ -514,7 +513,10 @@ function getStructDecoder(fields) {
|
|
|
514
513
|
});
|
|
515
514
|
}
|
|
516
515
|
function getStructCodec(fields) {
|
|
517
|
-
return codecsCore.combineCodec(
|
|
516
|
+
return codecsCore.combineCodec(
|
|
517
|
+
getStructEncoder(fields),
|
|
518
|
+
getStructDecoder(fields)
|
|
519
|
+
);
|
|
518
520
|
}
|
|
519
521
|
function getUnitEncoder() {
|
|
520
522
|
return codecsCore.createEncoder({
|