@solana/codecs-data-structures 2.0.0-experimental.fcff844 → 2.0.0-experimental.fd64233

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