@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.
@@ -34,7 +34,9 @@ function getMaxSize(codec) {
34
34
  function getArrayEncoder(item, config = {}) {
35
35
  const size = config.size ?? codecsNumbers.getU32Encoder();
36
36
  assertValidSize(size);
37
- const fixedSize = computeArrayLikeCodecSize(size, getFixedSize(item));
37
+ const itemSize = getFixedSize(item);
38
+ assertItemCodecSupportsSizeStrategy(config.description ?? "array", size, itemSize);
39
+ const fixedSize = computeArrayLikeCodecSize(size, itemSize);
38
40
  const maxSize = computeArrayLikeCodecSize(size, getMaxSize(item)) ?? void 0;
39
41
  return codecsCore.createEncoder({
40
42
  ...fixedSize !== null ? { fixedSize } : {
@@ -67,6 +69,7 @@ function getArrayDecoder(item, config = {}) {
67
69
  const size = config.size ?? codecsNumbers.getU32Decoder();
68
70
  assertValidSize(size);
69
71
  const itemSize = getFixedSize(item);
72
+ assertItemCodecSupportsSizeStrategy(config.description ?? "array", size, itemSize);
70
73
  const fixedSize = computeArrayLikeCodecSize(size, itemSize);
71
74
  const maxSize = computeArrayLikeCodecSize(size, getMaxSize(item)) ?? void 0;
72
75
  return codecsCore.createDecoder({
@@ -137,6 +140,15 @@ function assertValidSize(size) {
137
140
  throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS__SENTINEL_MUST_NOT_BE_EMPTY);
138
141
  }
139
142
  }
143
+ function assertItemCodecSupportsSizeStrategy(codecDescription, size, itemFixedSize) {
144
+ if (itemFixedSize !== 0) return;
145
+ if (size === "remainder" || isSentinelSize(size)) {
146
+ throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS__UNEXPECTED_ZERO_FIXED_SIZE_ITEM_FOR_ARRAY_LIKE_SIZE_STRATEGY, {
147
+ codecDescription,
148
+ sizeStrategy: size === "remainder" ? "remainder" : "sentinel"
149
+ });
150
+ }
151
+ }
140
152
  function hexBytes(bytes) {
141
153
  return bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
142
154
  }