@solana/codecs-data-structures 8.4.0 → 8.5.0-canary-20261006090716

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.
@@ -1,6 +1,6 @@
1
1
  import { createEncoder, getEncodedSize, createDecoder, containsBytes, combineCodec, assertByteArrayHasEnoughBytesForCodec, transformEncoder, transformDecoder, isFixedSize, assertIsFixedSize, fixEncoderSize, fixDecoderSize } from '@solana/codecs-core';
2
2
  import { getU32Encoder, getU32Decoder, getU8Encoder, getU8Decoder } from '@solana/codecs-numbers';
3
- import { SolanaError, SOLANA_ERROR__CODECS__INVALID_NUMBER_OF_ITEMS, SOLANA_ERROR__CODECS__SENTINEL_MISSING_AT_END_OF_BYTES, SOLANA_ERROR__CODECS__SENTINEL_MUST_NOT_BE_EMPTY, SOLANA_ERROR__CODECS__INVALID_CONSTANT, SOLANA_ERROR__CODECS__UNION_VARIANT_OUT_OF_RANGE, SOLANA_ERROR__CODECS__INVALID_DISCRIMINATED_UNION_VARIANT, SOLANA_ERROR__CODECS__CANNOT_USE_LEXICAL_VALUES_AS_ENUM_DISCRIMINATORS, SOLANA_ERROR__CODECS__INVALID_ENUM_VARIANT, SOLANA_ERROR__CODECS__ENUM_DISCRIMINATOR_OUT_OF_RANGE, SOLANA_ERROR__CODECS__INVALID_LITERAL_UNION_VARIANT, SOLANA_ERROR__CODECS__LITERAL_UNION_DISCRIMINATOR_OUT_OF_RANGE, SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_VALUE, SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_BYTES } from '@solana/errors';
3
+ import { SolanaError, SOLANA_ERROR__CODECS__INVALID_NUMBER_OF_ITEMS, SOLANA_ERROR__CODECS__SENTINEL_MISSING_AT_END_OF_BYTES, SOLANA_ERROR__CODECS__SENTINEL_MUST_NOT_BE_EMPTY, SOLANA_ERROR__CODECS__UNEXPECTED_ZERO_FIXED_SIZE_ITEM_FOR_ARRAY_LIKE_SIZE_STRATEGY, SOLANA_ERROR__CODECS__INVALID_CONSTANT, SOLANA_ERROR__CODECS__UNION_VARIANT_OUT_OF_RANGE, SOLANA_ERROR__CODECS__INVALID_DISCRIMINATED_UNION_VARIANT, SOLANA_ERROR__CODECS__CANNOT_USE_LEXICAL_VALUES_AS_ENUM_DISCRIMINATORS, SOLANA_ERROR__CODECS__INVALID_ENUM_VARIANT, SOLANA_ERROR__CODECS__ENUM_DISCRIMINATOR_OUT_OF_RANGE, SOLANA_ERROR__CODECS__INVALID_LITERAL_UNION_VARIANT, SOLANA_ERROR__CODECS__LITERAL_UNION_DISCRIMINATOR_OUT_OF_RANGE, SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_VALUE, SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_BYTES } from '@solana/errors';
4
4
 
5
5
  // src/array.ts
6
6
  function assertValidNumberOfItemsForCodec(codecDescription, expected, actual) {
@@ -32,7 +32,9 @@ function getMaxSize(codec) {
32
32
  function getArrayEncoder(item, config = {}) {
33
33
  const size = config.size ?? getU32Encoder();
34
34
  assertValidSize(size);
35
- const fixedSize = computeArrayLikeCodecSize(size, getFixedSize(item));
35
+ const itemSize = getFixedSize(item);
36
+ assertItemCodecSupportsSizeStrategy(config.description ?? "array", size, itemSize);
37
+ const fixedSize = computeArrayLikeCodecSize(size, itemSize);
36
38
  const maxSize = computeArrayLikeCodecSize(size, getMaxSize(item)) ?? void 0;
37
39
  return createEncoder({
38
40
  ...fixedSize !== null ? { fixedSize } : {
@@ -65,6 +67,7 @@ function getArrayDecoder(item, config = {}) {
65
67
  const size = config.size ?? getU32Decoder();
66
68
  assertValidSize(size);
67
69
  const itemSize = getFixedSize(item);
70
+ assertItemCodecSupportsSizeStrategy(config.description ?? "array", size, itemSize);
68
71
  const fixedSize = computeArrayLikeCodecSize(size, itemSize);
69
72
  const maxSize = computeArrayLikeCodecSize(size, getMaxSize(item)) ?? void 0;
70
73
  return createDecoder({
@@ -135,6 +138,15 @@ function assertValidSize(size) {
135
138
  throw new SolanaError(SOLANA_ERROR__CODECS__SENTINEL_MUST_NOT_BE_EMPTY);
136
139
  }
137
140
  }
141
+ function assertItemCodecSupportsSizeStrategy(codecDescription, size, itemFixedSize) {
142
+ if (itemFixedSize !== 0) return;
143
+ if (size === "remainder" || isSentinelSize(size)) {
144
+ throw new SolanaError(SOLANA_ERROR__CODECS__UNEXPECTED_ZERO_FIXED_SIZE_ITEM_FOR_ARRAY_LIKE_SIZE_STRATEGY, {
145
+ codecDescription,
146
+ sizeStrategy: size === "remainder" ? "remainder" : "sentinel"
147
+ });
148
+ }
149
+ }
138
150
  function hexBytes(bytes) {
139
151
  return bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
140
152
  }