@solana/codecs-data-structures 2.0.0-experimental.eb5fd16 → 2.0.0-experimental.eeb355e

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.
@@ -70,10 +70,10 @@ function arrayCodecHelper(item, size, description) {
70
70
  maxSize: getArrayLikeCodecSizeFromChildren(size, [item.maxSize])
71
71
  };
72
72
  }
73
- function getArrayEncoder(item, options = {}) {
74
- const size = options.size ?? getU32Encoder();
73
+ function getArrayEncoder(item, config = {}) {
74
+ const size = config.size ?? getU32Encoder();
75
75
  return {
76
- ...arrayCodecHelper(item, size, options.description),
76
+ ...arrayCodecHelper(item, size, config.description),
77
77
  encode: (value) => {
78
78
  if (typeof size === "number") {
79
79
  assertValidNumberOfItemsForCodec("array", size, value.length);
@@ -82,10 +82,10 @@ function getArrayEncoder(item, options = {}) {
82
82
  }
83
83
  };
84
84
  }
85
- function getArrayDecoder(item, options = {}) {
86
- const size = options.size ?? getU32Decoder();
85
+ function getArrayDecoder(item, config = {}) {
86
+ const size = config.size ?? getU32Decoder();
87
87
  return {
88
- ...arrayCodecHelper(item, size, options.description),
88
+ ...arrayCodecHelper(item, size, config.description),
89
89
  decode: (bytes, offset = 0) => {
90
90
  if (typeof size === "object" && bytes.slice(offset).length === 0) {
91
91
  return [[], offset];
@@ -102,15 +102,15 @@ function getArrayDecoder(item, options = {}) {
102
102
  }
103
103
  };
104
104
  }
105
- function getArrayCodec(item, options = {}) {
106
- return combineCodec(getArrayEncoder(item, options), getArrayDecoder(item, options));
105
+ function getArrayCodec(item, config = {}) {
106
+ return combineCodec(getArrayEncoder(item, config), getArrayDecoder(item, config));
107
107
  }
108
- var getBitArrayEncoder = (size, options = {}) => {
109
- const parsedOptions = typeof options === "boolean" ? { backward: options } : options;
110
- const backward = parsedOptions.backward ?? false;
108
+ var getBitArrayEncoder = (size, config = {}) => {
109
+ const parsedConfig = typeof config === "boolean" ? { backward: config } : config;
110
+ const backward = parsedConfig.backward ?? false;
111
111
  const backwardSuffix = backward ? "; backward" : "";
112
112
  return {
113
- description: parsedOptions.description ?? `bitArray(${size}${backwardSuffix})`,
113
+ description: parsedConfig.description ?? `bitArray(${size}${backwardSuffix})`,
114
114
  encode(value) {
115
115
  const bytes = [];
116
116
  for (let i = 0; i < size; i += 1) {
@@ -131,9 +131,9 @@ var getBitArrayEncoder = (size, options = {}) => {
131
131
  maxSize: size
132
132
  };
133
133
  };
134
- var getBitArrayDecoder = (size, options = {}) => {
135
- const parsedOptions = typeof options === "boolean" ? { backward: options } : options;
136
- const backward = parsedOptions.backward ?? false;
134
+ var getBitArrayDecoder = (size, config = {}) => {
135
+ const parsedConfig = typeof config === "boolean" ? { backward: config } : config;
136
+ const backward = parsedConfig.backward ?? false;
137
137
  const backwardSuffix = backward ? "; backward" : "";
138
138
  return {
139
139
  decode(bytes, offset = 0) {
@@ -154,24 +154,24 @@ var getBitArrayDecoder = (size, options = {}) => {
154
154
  });
155
155
  return [booleans, offset + size];
156
156
  },
157
- description: parsedOptions.description ?? `bitArray(${size}${backwardSuffix})`,
157
+ description: parsedConfig.description ?? `bitArray(${size}${backwardSuffix})`,
158
158
  fixedSize: size,
159
159
  maxSize: size
160
160
  };
161
161
  };
162
- var getBitArrayCodec = (size, options = {}) => combineCodec(getBitArrayEncoder(size, options), getBitArrayDecoder(size, options));
163
- function getBooleanEncoder(options = {}) {
164
- const size = options.size ?? getU8Encoder();
162
+ var getBitArrayCodec = (size, config = {}) => combineCodec(getBitArrayEncoder(size, config), getBitArrayDecoder(size, config));
163
+ function getBooleanEncoder(config = {}) {
164
+ const size = config.size ?? getU8Encoder();
165
165
  assertFixedSizeCodec(size, "Codec [bool] requires a fixed size.");
166
166
  return {
167
- description: options.description ?? `bool(${size.description})`,
167
+ description: config.description ?? `bool(${size.description})`,
168
168
  encode: (value) => size.encode(value ? 1 : 0),
169
169
  fixedSize: size.fixedSize,
170
170
  maxSize: size.fixedSize
171
171
  };
172
172
  }
173
- function getBooleanDecoder(options = {}) {
174
- const size = options.size ?? getU8Decoder();
173
+ function getBooleanDecoder(config = {}) {
174
+ const size = config.size ?? getU8Decoder();
175
175
  assertFixedSizeCodec(size, "Codec [bool] requires a fixed size.");
176
176
  return {
177
177
  decode: (bytes, offset = 0) => {
@@ -179,18 +179,18 @@ function getBooleanDecoder(options = {}) {
179
179
  const [value, vOffset] = size.decode(bytes, offset);
180
180
  return [value === 1, vOffset];
181
181
  },
182
- description: options.description ?? `bool(${size.description})`,
182
+ description: config.description ?? `bool(${size.description})`,
183
183
  fixedSize: size.fixedSize,
184
184
  maxSize: size.fixedSize
185
185
  };
186
186
  }
187
- function getBooleanCodec(options = {}) {
188
- return combineCodec(getBooleanEncoder(options), getBooleanDecoder(options));
187
+ function getBooleanCodec(config = {}) {
188
+ return combineCodec(getBooleanEncoder(config), getBooleanDecoder(config));
189
189
  }
190
- function getBytesEncoder(options = {}) {
191
- const size = options.size ?? "variable";
190
+ function getBytesEncoder(config = {}) {
191
+ const size = config.size ?? "variable";
192
192
  const sizeDescription = typeof size === "object" ? size.description : `${size}`;
193
- const description = options.description ?? `bytes(${sizeDescription})`;
193
+ const description = config.description ?? `bytes(${sizeDescription})`;
194
194
  const byteEncoder = {
195
195
  description,
196
196
  encode: (value) => value,
@@ -212,10 +212,10 @@ function getBytesEncoder(options = {}) {
212
212
  }
213
213
  };
214
214
  }
215
- function getBytesDecoder(options = {}) {
216
- const size = options.size ?? "variable";
215
+ function getBytesDecoder(config = {}) {
216
+ const size = config.size ?? "variable";
217
217
  const sizeDescription = typeof size === "object" ? size.description : `${size}`;
218
- const description = options.description ?? `bytes(${sizeDescription})`;
218
+ const description = config.description ?? `bytes(${sizeDescription})`;
219
219
  const byteDecoder = {
220
220
  decode: (bytes, offset = 0) => {
221
221
  const slice = bytes.slice(offset);
@@ -246,8 +246,8 @@ function getBytesDecoder(options = {}) {
246
246
  }
247
247
  };
248
248
  }
249
- function getBytesCodec(options = {}) {
250
- return combineCodec(getBytesEncoder(options), getBytesDecoder(options));
249
+ function getBytesCodec(config = {}) {
250
+ return combineCodec(getBytesEncoder(config), getBytesDecoder(config));
251
251
  }
252
252
  function dataEnumCodecHelper(variants, prefix, description) {
253
253
  const fieldDescriptions = variants.map(([name, codec]) => `${String(name)}${codec ? `: ${codec.description}` : ""}`).join(", ");
@@ -260,10 +260,10 @@ function dataEnumCodecHelper(variants, prefix, description) {
260
260
  maxSize: variants.length === 0 ? prefix.maxSize : sumCodecSizes([prefix.maxSize, maxVariantSize])
261
261
  };
262
262
  }
263
- function getDataEnumEncoder(variants, options = {}) {
264
- const prefix = options.size ?? getU8Encoder();
263
+ function getDataEnumEncoder(variants, config = {}) {
264
+ const prefix = config.size ?? getU8Encoder();
265
265
  return {
266
- ...dataEnumCodecHelper(variants, prefix, options.description),
266
+ ...dataEnumCodecHelper(variants, prefix, config.description),
267
267
  encode: (variant) => {
268
268
  const discriminator = variants.findIndex(([key]) => variant.__kind === key);
269
269
  if (discriminator < 0) {
@@ -278,10 +278,10 @@ function getDataEnumEncoder(variants, options = {}) {
278
278
  }
279
279
  };
280
280
  }
281
- function getDataEnumDecoder(variants, options = {}) {
282
- const prefix = options.size ?? getU8Decoder();
281
+ function getDataEnumDecoder(variants, config = {}) {
282
+ const prefix = config.size ?? getU8Decoder();
283
283
  return {
284
- ...dataEnumCodecHelper(variants, prefix, options.description),
284
+ ...dataEnumCodecHelper(variants, prefix, config.description),
285
285
  decode: (bytes, offset = 0) => {
286
286
  assertByteArrayIsNotEmptyForCodec("dataEnum", bytes, offset);
287
287
  const [discriminator, dOffset] = prefix.decode(bytes, offset);
@@ -298,8 +298,8 @@ function getDataEnumDecoder(variants, options = {}) {
298
298
  }
299
299
  };
300
300
  }
301
- function getDataEnumCodec(variants, options = {}) {
302
- return combineCodec(getDataEnumEncoder(variants, options), getDataEnumDecoder(variants, options));
301
+ function getDataEnumCodec(variants, config = {}) {
302
+ return combineCodec(getDataEnumEncoder(variants, config), getDataEnumDecoder(variants, config));
303
303
  }
304
304
  function mapCodecHelper(key, value, size, description) {
305
305
  if (size === "remainder" && (key.fixedSize === null || value.fixedSize === null)) {
@@ -311,10 +311,10 @@ function mapCodecHelper(key, value, size, description) {
311
311
  maxSize: getArrayLikeCodecSizeFromChildren(size, [key.maxSize, value.maxSize])
312
312
  };
313
313
  }
314
- function getMapEncoder(key, value, options = {}) {
315
- const size = options.size ?? getU32Encoder();
314
+ function getMapEncoder(key, value, config = {}) {
315
+ const size = config.size ?? getU32Encoder();
316
316
  return {
317
- ...mapCodecHelper(key, value, size, options.description),
317
+ ...mapCodecHelper(key, value, size, config.description),
318
318
  encode: (map) => {
319
319
  if (typeof size === "number") {
320
320
  assertValidNumberOfItemsForCodec("map", size, map.size);
@@ -324,10 +324,10 @@ function getMapEncoder(key, value, options = {}) {
324
324
  }
325
325
  };
326
326
  }
327
- function getMapDecoder(key, value, options = {}) {
328
- const size = options.size ?? getU32Decoder();
327
+ function getMapDecoder(key, value, config = {}) {
328
+ const size = config.size ?? getU32Decoder();
329
329
  return {
330
- ...mapCodecHelper(key, value, size, options.description),
330
+ ...mapCodecHelper(key, value, size, config.description),
331
331
  decode: (bytes, offset = 0) => {
332
332
  const map = /* @__PURE__ */ new Map();
333
333
  if (typeof size === "object" && bytes.slice(offset).length === 0) {
@@ -351,8 +351,8 @@ function getMapDecoder(key, value, options = {}) {
351
351
  }
352
352
  };
353
353
  }
354
- function getMapCodec(key, value, options = {}) {
355
- return combineCodec(getMapEncoder(key, value, options), getMapDecoder(key, value, options));
354
+ function getMapCodec(key, value, config = {}) {
355
+ return combineCodec(getMapEncoder(key, value, config), getMapDecoder(key, value, config));
356
356
  }
357
357
  function nullableCodecHelper(item, prefix, fixed, description) {
358
358
  let descriptionSuffix = `; ${prefix.description}`;
@@ -369,11 +369,11 @@ function nullableCodecHelper(item, prefix, fixed, description) {
369
369
  maxSize: sumCodecSizes([prefix.maxSize, item.maxSize])
370
370
  };
371
371
  }
372
- function getNullableEncoder(item, options = {}) {
373
- const prefix = options.prefix ?? getU8Encoder();
374
- const fixed = options.fixed ?? false;
372
+ function getNullableEncoder(item, config = {}) {
373
+ const prefix = config.prefix ?? getU8Encoder();
374
+ const fixed = config.fixed ?? false;
375
375
  return {
376
- ...nullableCodecHelper(item, prefix, fixed, options.description),
376
+ ...nullableCodecHelper(item, prefix, fixed, config.description),
377
377
  encode: (option) => {
378
378
  const prefixByte = prefix.encode(Number(option !== null));
379
379
  let itemBytes = option !== null ? item.encode(option) : new Uint8Array();
@@ -382,11 +382,11 @@ function getNullableEncoder(item, options = {}) {
382
382
  }
383
383
  };
384
384
  }
385
- function getNullableDecoder(item, options = {}) {
386
- const prefix = options.prefix ?? getU8Decoder();
387
- const fixed = options.fixed ?? false;
385
+ function getNullableDecoder(item, config = {}) {
386
+ const prefix = config.prefix ?? getU8Decoder();
387
+ const fixed = config.fixed ?? false;
388
388
  return {
389
- ...nullableCodecHelper(item, prefix, fixed, options.description),
389
+ ...nullableCodecHelper(item, prefix, fixed, config.description),
390
390
  decode: (bytes, offset = 0) => {
391
391
  if (bytes.length - offset <= 0) {
392
392
  return [null, offset];
@@ -403,8 +403,8 @@ function getNullableDecoder(item, options = {}) {
403
403
  }
404
404
  };
405
405
  }
406
- function getNullableCodec(item, options = {}) {
407
- return combineCodec(getNullableEncoder(item, options), getNullableDecoder(item, options));
406
+ function getNullableCodec(item, config = {}) {
407
+ return combineCodec(getNullableEncoder(item, config), getNullableDecoder(item, config));
408
408
  }
409
409
  function scalarEnumCoderHelper(constructor, prefix, description) {
410
410
  const enumKeys = Object.keys(constructor);
@@ -426,9 +426,9 @@ function scalarEnumCoderHelper(constructor, prefix, description) {
426
426
  stringValues
427
427
  };
428
428
  }
429
- function getScalarEnumEncoder(constructor, options = {}) {
430
- const prefix = options.size ?? getU8Encoder();
431
- const { description, fixedSize, maxSize, minRange, maxRange, stringValues, enumKeys, enumValues } = scalarEnumCoderHelper(constructor, prefix, options.description);
429
+ function getScalarEnumEncoder(constructor, config = {}) {
430
+ const prefix = config.size ?? getU8Encoder();
431
+ const { description, fixedSize, maxSize, minRange, maxRange, stringValues, enumKeys, enumValues } = scalarEnumCoderHelper(constructor, prefix, config.description);
432
432
  return {
433
433
  description,
434
434
  encode: (value) => {
@@ -450,12 +450,12 @@ function getScalarEnumEncoder(constructor, options = {}) {
450
450
  maxSize
451
451
  };
452
452
  }
453
- function getScalarEnumDecoder(constructor, options = {}) {
454
- const prefix = options.size ?? getU8Decoder();
453
+ function getScalarEnumDecoder(constructor, config = {}) {
454
+ const prefix = config.size ?? getU8Decoder();
455
455
  const { description, fixedSize, maxSize, minRange, maxRange, isNumericEnum, enumValues } = scalarEnumCoderHelper(
456
456
  constructor,
457
457
  prefix,
458
- options.description
458
+ config.description
459
459
  );
460
460
  return {
461
461
  decode: (bytes, offset = 0) => {
@@ -475,8 +475,8 @@ function getScalarEnumDecoder(constructor, options = {}) {
475
475
  maxSize
476
476
  };
477
477
  }
478
- function getScalarEnumCodec(constructor, options = {}) {
479
- return combineCodec(getScalarEnumEncoder(constructor, options), getScalarEnumDecoder(constructor, options));
478
+ function getScalarEnumCodec(constructor, config = {}) {
479
+ return combineCodec(getScalarEnumEncoder(constructor, config), getScalarEnumDecoder(constructor, config));
480
480
  }
481
481
  function setCodecHelper(item, size, description) {
482
482
  if (size === "remainder" && item.fixedSize === null) {
@@ -488,10 +488,10 @@ function setCodecHelper(item, size, description) {
488
488
  maxSize: getArrayLikeCodecSizeFromChildren(size, [item.maxSize])
489
489
  };
490
490
  }
491
- function getSetEncoder(item, options = {}) {
492
- const size = options.size ?? getU32Encoder();
491
+ function getSetEncoder(item, config = {}) {
492
+ const size = config.size ?? getU32Encoder();
493
493
  return {
494
- ...setCodecHelper(item, size, options.description),
494
+ ...setCodecHelper(item, size, config.description),
495
495
  encode: (set) => {
496
496
  if (typeof size === "number" && set.size !== size) {
497
497
  assertValidNumberOfItemsForCodec("set", size, set.size);
@@ -501,10 +501,10 @@ function getSetEncoder(item, options = {}) {
501
501
  }
502
502
  };
503
503
  }
504
- function getSetDecoder(item, options = {}) {
505
- const size = options.size ?? getU32Decoder();
504
+ function getSetDecoder(item, config = {}) {
505
+ const size = config.size ?? getU32Decoder();
506
506
  return {
507
- ...setCodecHelper(item, size, options.description),
507
+ ...setCodecHelper(item, size, config.description),
508
508
  decode: (bytes, offset = 0) => {
509
509
  const set = /* @__PURE__ */ new Set();
510
510
  if (typeof size === "object" && bytes.slice(offset).length === 0) {
@@ -521,8 +521,8 @@ function getSetDecoder(item, options = {}) {
521
521
  }
522
522
  };
523
523
  }
524
- function getSetCodec(item, options = {}) {
525
- return combineCodec(getSetEncoder(item, options), getSetDecoder(item, options));
524
+ function getSetCodec(item, config = {}) {
525
+ return combineCodec(getSetEncoder(item, config), getSetDecoder(item, config));
526
526
  }
527
527
  function structCodecHelper(fields, description) {
528
528
  const fieldDescriptions = fields.map(([name, codec]) => `${String(name)}: ${codec.description}`).join(", ");
@@ -532,18 +532,18 @@ function structCodecHelper(fields, description) {
532
532
  maxSize: sumCodecSizes(fields.map(([, field]) => field.maxSize))
533
533
  };
534
534
  }
535
- function getStructEncoder(fields, options = {}) {
535
+ function getStructEncoder(fields, config = {}) {
536
536
  return {
537
- ...structCodecHelper(fields, options.description),
537
+ ...structCodecHelper(fields, config.description),
538
538
  encode: (struct) => {
539
539
  const fieldBytes = fields.map(([key, codec]) => codec.encode(struct[key]));
540
540
  return mergeBytes(fieldBytes);
541
541
  }
542
542
  };
543
543
  }
544
- function getStructDecoder(fields, options = {}) {
544
+ function getStructDecoder(fields, config = {}) {
545
545
  return {
546
- ...structCodecHelper(fields, options.description),
546
+ ...structCodecHelper(fields, config.description),
547
547
  decode: (bytes, offset = 0) => {
548
548
  const struct = {};
549
549
  fields.forEach(([key, codec]) => {
@@ -555,8 +555,8 @@ function getStructDecoder(fields, options = {}) {
555
555
  }
556
556
  };
557
557
  }
558
- function getStructCodec(fields, options = {}) {
559
- return combineCodec(getStructEncoder(fields, options), getStructDecoder(fields, options));
558
+ function getStructCodec(fields, config = {}) {
559
+ return combineCodec(getStructEncoder(fields, config), getStructDecoder(fields, config));
560
560
  }
561
561
  function tupleCodecHelper(items, description) {
562
562
  const itemDescriptions = items.map((item) => item.description).join(", ");
@@ -566,18 +566,18 @@ function tupleCodecHelper(items, description) {
566
566
  maxSize: sumCodecSizes(items.map((item) => item.maxSize))
567
567
  };
568
568
  }
569
- function getTupleEncoder(items, options = {}) {
569
+ function getTupleEncoder(items, config = {}) {
570
570
  return {
571
- ...tupleCodecHelper(items, options.description),
571
+ ...tupleCodecHelper(items, config.description),
572
572
  encode: (value) => {
573
573
  assertValidNumberOfItemsForCodec("tuple", items.length, value.length);
574
574
  return mergeBytes(items.map((item, index) => item.encode(value[index])));
575
575
  }
576
576
  };
577
577
  }
578
- function getTupleDecoder(items, options = {}) {
578
+ function getTupleDecoder(items, config = {}) {
579
579
  return {
580
- ...tupleCodecHelper(items, options.description),
580
+ ...tupleCodecHelper(items, config.description),
581
581
  decode: (bytes, offset = 0) => {
582
582
  const values = [];
583
583
  items.forEach((codec) => {
@@ -589,30 +589,30 @@ function getTupleDecoder(items, options = {}) {
589
589
  }
590
590
  };
591
591
  }
592
- function getTupleCodec(items, options = {}) {
592
+ function getTupleCodec(items, config = {}) {
593
593
  return combineCodec(
594
- getTupleEncoder(items, options),
595
- getTupleDecoder(items, options)
594
+ getTupleEncoder(items, config),
595
+ getTupleDecoder(items, config)
596
596
  );
597
597
  }
598
- function getUnitEncoder(options = {}) {
598
+ function getUnitEncoder(config = {}) {
599
599
  return {
600
- description: options.description ?? "unit",
600
+ description: config.description ?? "unit",
601
601
  encode: () => new Uint8Array(),
602
602
  fixedSize: 0,
603
603
  maxSize: 0
604
604
  };
605
605
  }
606
- function getUnitDecoder(options = {}) {
606
+ function getUnitDecoder(config = {}) {
607
607
  return {
608
608
  decode: (_bytes, offset = 0) => [void 0, offset],
609
- description: options.description ?? "unit",
609
+ description: config.description ?? "unit",
610
610
  fixedSize: 0,
611
611
  maxSize: 0
612
612
  };
613
613
  }
614
- function getUnitCodec(options = {}) {
615
- return combineCodec(getUnitEncoder(options), getUnitDecoder(options));
614
+ function getUnitCodec(config = {}) {
615
+ return combineCodec(getUnitEncoder(config), getUnitDecoder(config));
616
616
  }
617
617
 
618
618
  export { assertValidNumberOfItemsForCodec, decodeArrayLikeCodecSize, getArrayCodec, getArrayDecoder, getArrayEncoder, getArrayLikeCodecSizeDescription, getArrayLikeCodecSizeFromChildren, getArrayLikeCodecSizePrefix, getBitArrayCodec, getBitArrayDecoder, getBitArrayEncoder, getBooleanCodec, getBooleanDecoder, getBooleanEncoder, getBytesCodec, getBytesDecoder, getBytesEncoder, getDataEnumCodec, getDataEnumDecoder, getDataEnumEncoder, getMapCodec, getMapDecoder, getMapEncoder, getNullableCodec, getNullableDecoder, getNullableEncoder, getScalarEnumCodec, getScalarEnumDecoder, getScalarEnumEncoder, getSetCodec, getSetDecoder, getSetEncoder, getStructCodec, getStructDecoder, getStructEncoder, getTupleCodec, getTupleDecoder, getTupleEncoder, getUnitCodec, getUnitDecoder, getUnitEncoder };