@solana/codecs-data-structures 2.0.0-experimental.a48ebd2 → 2.0.0-experimental.a7a613a
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 +334 -432
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +336 -430
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +338 -430
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +334 -432
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +338 -430
- package/dist/index.node.js.map +1 -1
- package/dist/types/array.d.ts +35 -6
- package/dist/types/array.d.ts.map +1 -1
- package/dist/types/bit-array.d.ts +5 -5
- package/dist/types/bit-array.d.ts.map +1 -1
- package/dist/types/boolean.d.ts +18 -6
- package/dist/types/boolean.d.ts.map +1 -1
- package/dist/types/bytes.d.ts +14 -5
- package/dist/types/bytes.d.ts.map +1 -1
- package/dist/types/data-enum.d.ts +18 -20
- package/dist/types/data-enum.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -14
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/map.d.ts +24 -6
- package/dist/types/map.d.ts.map +1 -1
- package/dist/types/nullable.d.ts +24 -6
- package/dist/types/nullable.d.ts.map +1 -1
- package/dist/types/scalar-enum.d.ts +42 -11
- package/dist/types/scalar-enum.d.ts.map +1 -1
- package/dist/types/set.d.ts +24 -6
- package/dist/types/set.d.ts.map +1 -1
- package/dist/types/struct.d.ts +16 -21
- package/dist/types/struct.d.ts.map +1 -1
- package/dist/types/tuple.d.ts +22 -15
- package/dist/types/tuple.d.ts.map +1 -1
- package/dist/types/unit.d.ts +4 -12
- package/dist/types/unit.d.ts.map +1 -1
- package/dist/types/utils.d.ts +10 -2
- package/dist/types/utils.d.ts.map +1 -1
- package/package.json +12 -34
- package/dist/index.development.js +0 -889
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -51
- package/dist/types/array-like-codec-size.d.ts +0 -20
- package/dist/types/array-like-codec-size.d.ts.map +0 -1
package/dist/index.node.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createEncoder, getEncodedSize, createDecoder, combineCodec, assertByteArrayHasEnoughBytesForCodec, assertIsFixedSize, mapEncoder, mapDecoder, fixEncoder, fixDecoder, assertByteArrayIsNotEmptyForCodec, isFixedSize } from '@solana/codecs-core';
|
|
2
2
|
import { getU32Encoder, getU32Decoder, getU8Encoder, getU8Decoder } from '@solana/codecs-numbers';
|
|
3
3
|
|
|
4
4
|
// src/array.ts
|
|
5
5
|
|
|
6
|
-
// src/
|
|
6
|
+
// src/assertions.ts
|
|
7
|
+
function assertValidNumberOfItemsForCodec(codecDescription, expected, actual) {
|
|
8
|
+
if (expected !== actual) {
|
|
9
|
+
throw new Error(`Expected [${codecDescription}] to have ${expected} items, got ${actual}.`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
7
12
|
function maxCodecSizes(sizes) {
|
|
8
13
|
return sizes.reduce(
|
|
9
14
|
(all, size) => all === null || size === null ? null : Math.max(all, size),
|
|
@@ -13,106 +18,88 @@ function maxCodecSizes(sizes) {
|
|
|
13
18
|
function sumCodecSizes(sizes) {
|
|
14
19
|
return sizes.reduce((all, size) => all === null || size === null ? null : all + size, 0);
|
|
15
20
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function decodeArrayLikeCodecSize(size, childrenSizes, bytes, offset) {
|
|
19
|
-
if (typeof size === "number") {
|
|
20
|
-
return [size, offset];
|
|
21
|
-
}
|
|
22
|
-
if (typeof size === "object") {
|
|
23
|
-
return size.decode(bytes, offset);
|
|
24
|
-
}
|
|
25
|
-
if (size === "remainder") {
|
|
26
|
-
const childrenSize = sumCodecSizes(childrenSizes);
|
|
27
|
-
if (childrenSize === null) {
|
|
28
|
-
throw new Error('Codecs of "remainder" size must have fixed-size items.');
|
|
29
|
-
}
|
|
30
|
-
const remainder = bytes.slice(offset).length;
|
|
31
|
-
if (remainder % childrenSize !== 0) {
|
|
32
|
-
throw new Error(
|
|
33
|
-
`The remainder of the byte array (${remainder} bytes) cannot be split into chunks of ${childrenSize} bytes. Codecs of "remainder" size must have a remainder that is a multiple of its item size. In other words, ${remainder} modulo ${childrenSize} should be equal to zero.`
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
return [remainder / childrenSize, offset];
|
|
37
|
-
}
|
|
38
|
-
throw new Error(`Unrecognized array-like codec size: ${JSON.stringify(size)}`);
|
|
39
|
-
}
|
|
40
|
-
function getArrayLikeCodecSizeDescription(size) {
|
|
41
|
-
return typeof size === "object" ? size.description : `${size}`;
|
|
42
|
-
}
|
|
43
|
-
function getArrayLikeCodecSizeFromChildren(size, childrenSizes) {
|
|
44
|
-
if (typeof size !== "number")
|
|
45
|
-
return null;
|
|
46
|
-
if (size === 0)
|
|
47
|
-
return 0;
|
|
48
|
-
const childrenSize = sumCodecSizes(childrenSizes);
|
|
49
|
-
return childrenSize === null ? null : childrenSize * size;
|
|
50
|
-
}
|
|
51
|
-
function getArrayLikeCodecSizePrefix(size, realSize) {
|
|
52
|
-
return typeof size === "object" ? size.encode(realSize) : new Uint8Array();
|
|
21
|
+
function getFixedSize(codec) {
|
|
22
|
+
return isFixedSize(codec) ? codec.fixedSize : null;
|
|
53
23
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
function assertValidNumberOfItemsForCodec(codecDescription, expected, actual) {
|
|
57
|
-
if (expected !== actual) {
|
|
58
|
-
throw new Error(`Expected [${codecDescription}] to have ${expected} items, got ${actual}.`);
|
|
59
|
-
}
|
|
24
|
+
function getMaxSize(codec) {
|
|
25
|
+
return isFixedSize(codec) ? codec.fixedSize : codec.maxSize ?? null;
|
|
60
26
|
}
|
|
61
27
|
|
|
62
28
|
// src/array.ts
|
|
63
|
-
function arrayCodecHelper(item, size, description) {
|
|
64
|
-
if (size === "remainder" && item.fixedSize === null) {
|
|
65
|
-
throw new Error('Codecs of "remainder" size must have fixed-size items.');
|
|
66
|
-
}
|
|
67
|
-
return {
|
|
68
|
-
description: description ?? `array(${item.description}; ${getArrayLikeCodecSizeDescription(size)})`,
|
|
69
|
-
fixedSize: getArrayLikeCodecSizeFromChildren(size, [item.fixedSize]),
|
|
70
|
-
maxSize: getArrayLikeCodecSizeFromChildren(size, [item.maxSize])
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
29
|
function getArrayEncoder(item, config = {}) {
|
|
74
30
|
const size = config.size ?? getU32Encoder();
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
31
|
+
const fixedSize = computeArrayLikeCodecSize(size, getFixedSize(item));
|
|
32
|
+
const maxSize = computeArrayLikeCodecSize(size, getMaxSize(item)) ?? void 0;
|
|
33
|
+
return createEncoder({
|
|
34
|
+
...fixedSize !== null ? { fixedSize } : {
|
|
35
|
+
getSizeFromValue: (array) => {
|
|
36
|
+
const prefixSize = typeof size === "object" ? getEncodedSize(array.length, size) : 0;
|
|
37
|
+
return prefixSize + [...array].reduce((all, value) => all + getEncodedSize(value, item), 0);
|
|
38
|
+
},
|
|
39
|
+
maxSize
|
|
40
|
+
},
|
|
41
|
+
write: (array, bytes, offset) => {
|
|
78
42
|
if (typeof size === "number") {
|
|
79
|
-
assertValidNumberOfItemsForCodec("array", size,
|
|
43
|
+
assertValidNumberOfItemsForCodec("array", size, array.length);
|
|
44
|
+
}
|
|
45
|
+
if (typeof size === "object") {
|
|
46
|
+
offset = size.write(array.length, bytes, offset);
|
|
80
47
|
}
|
|
81
|
-
|
|
48
|
+
array.forEach((value) => {
|
|
49
|
+
offset = item.write(value, bytes, offset);
|
|
50
|
+
});
|
|
51
|
+
return offset;
|
|
82
52
|
}
|
|
83
|
-
};
|
|
53
|
+
});
|
|
84
54
|
}
|
|
85
55
|
function getArrayDecoder(item, config = {}) {
|
|
86
56
|
const size = config.size ?? getU32Decoder();
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
57
|
+
const itemSize = getFixedSize(item);
|
|
58
|
+
const fixedSize = computeArrayLikeCodecSize(size, itemSize);
|
|
59
|
+
const maxSize = computeArrayLikeCodecSize(size, getMaxSize(item)) ?? void 0;
|
|
60
|
+
return createDecoder({
|
|
61
|
+
...fixedSize !== null ? { fixedSize } : { maxSize },
|
|
62
|
+
read: (bytes, offset) => {
|
|
63
|
+
const array = [];
|
|
90
64
|
if (typeof size === "object" && bytes.slice(offset).length === 0) {
|
|
91
|
-
return [
|
|
65
|
+
return [array, offset];
|
|
92
66
|
}
|
|
93
|
-
|
|
67
|
+
if (size === "remainder") {
|
|
68
|
+
while (offset < bytes.length) {
|
|
69
|
+
const [value, newOffset2] = item.read(bytes, offset);
|
|
70
|
+
offset = newOffset2;
|
|
71
|
+
array.push(value);
|
|
72
|
+
}
|
|
73
|
+
return [array, offset];
|
|
74
|
+
}
|
|
75
|
+
const [resolvedSize, newOffset] = typeof size === "number" ? [size, offset] : size.read(bytes, offset);
|
|
94
76
|
offset = newOffset;
|
|
95
|
-
const values = [];
|
|
96
77
|
for (let i = 0; i < resolvedSize; i += 1) {
|
|
97
|
-
const [value, newOffset2] = item.
|
|
98
|
-
values.push(value);
|
|
78
|
+
const [value, newOffset2] = item.read(bytes, offset);
|
|
99
79
|
offset = newOffset2;
|
|
80
|
+
array.push(value);
|
|
100
81
|
}
|
|
101
|
-
return [
|
|
82
|
+
return [array, offset];
|
|
102
83
|
}
|
|
103
|
-
};
|
|
84
|
+
});
|
|
104
85
|
}
|
|
105
86
|
function getArrayCodec(item, config = {}) {
|
|
106
87
|
return combineCodec(getArrayEncoder(item, config), getArrayDecoder(item, config));
|
|
107
88
|
}
|
|
108
|
-
|
|
89
|
+
function computeArrayLikeCodecSize(size, itemSize) {
|
|
90
|
+
if (typeof size !== "number")
|
|
91
|
+
return null;
|
|
92
|
+
if (size === 0)
|
|
93
|
+
return 0;
|
|
94
|
+
return itemSize === null ? null : itemSize * size;
|
|
95
|
+
}
|
|
96
|
+
function getBitArrayEncoder(size, config = {}) {
|
|
109
97
|
const parsedConfig = typeof config === "boolean" ? { backward: config } : config;
|
|
110
98
|
const backward = parsedConfig.backward ?? false;
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const bytes = [];
|
|
99
|
+
return createEncoder({
|
|
100
|
+
fixedSize: size,
|
|
101
|
+
write(value, bytes, offset) {
|
|
102
|
+
const bytesToAdd = [];
|
|
116
103
|
for (let i = 0; i < size; i += 1) {
|
|
117
104
|
let byte = 0;
|
|
118
105
|
for (let j = 0; j < 8; j += 1) {
|
|
@@ -120,23 +107,22 @@ var getBitArrayEncoder = (size, config = {}) => {
|
|
|
120
107
|
byte |= feature << (backward ? j : 7 - j);
|
|
121
108
|
}
|
|
122
109
|
if (backward) {
|
|
123
|
-
|
|
110
|
+
bytesToAdd.unshift(byte);
|
|
124
111
|
} else {
|
|
125
|
-
|
|
112
|
+
bytesToAdd.push(byte);
|
|
126
113
|
}
|
|
127
114
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
var getBitArrayDecoder = (size, config = {}) => {
|
|
115
|
+
bytes.set(bytesToAdd, offset);
|
|
116
|
+
return size;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
function getBitArrayDecoder(size, config = {}) {
|
|
135
121
|
const parsedConfig = typeof config === "boolean" ? { backward: config } : config;
|
|
136
122
|
const backward = parsedConfig.backward ?? false;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
123
|
+
return createDecoder({
|
|
124
|
+
fixedSize: size,
|
|
125
|
+
read(bytes, offset) {
|
|
140
126
|
assertByteArrayHasEnoughBytesForCodec("bitArray", size, bytes, offset);
|
|
141
127
|
const booleans = [];
|
|
142
128
|
let slice = bytes.slice(offset, offset + size);
|
|
@@ -153,138 +139,107 @@ var getBitArrayDecoder = (size, config = {}) => {
|
|
|
153
139
|
}
|
|
154
140
|
});
|
|
155
141
|
return [booleans, offset + size];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
var getBitArrayCodec = (size, config = {}) => combineCodec(getBitArrayEncoder(size, config), getBitArrayDecoder(size, config));
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function getBitArrayCodec(size, config = {}) {
|
|
146
|
+
return combineCodec(getBitArrayEncoder(size, config), getBitArrayDecoder(size, config));
|
|
147
|
+
}
|
|
163
148
|
function getBooleanEncoder(config = {}) {
|
|
164
149
|
const size = config.size ?? getU8Encoder();
|
|
165
|
-
|
|
166
|
-
return
|
|
167
|
-
description: config.description ?? `bool(${size.description})`,
|
|
168
|
-
encode: (value) => size.encode(value ? 1 : 0),
|
|
169
|
-
fixedSize: size.fixedSize,
|
|
170
|
-
maxSize: size.fixedSize
|
|
171
|
-
};
|
|
150
|
+
assertIsFixedSize(size, "Codec [bool] requires a fixed size.");
|
|
151
|
+
return mapEncoder(size, (value) => value ? 1 : 0);
|
|
172
152
|
}
|
|
173
153
|
function getBooleanDecoder(config = {}) {
|
|
174
154
|
const size = config.size ?? getU8Decoder();
|
|
175
|
-
|
|
176
|
-
return
|
|
177
|
-
decode: (bytes, offset = 0) => {
|
|
178
|
-
assertByteArrayIsNotEmptyForCodec("bool", bytes, offset);
|
|
179
|
-
const [value, vOffset] = size.decode(bytes, offset);
|
|
180
|
-
return [value === 1, vOffset];
|
|
181
|
-
},
|
|
182
|
-
description: config.description ?? `bool(${size.description})`,
|
|
183
|
-
fixedSize: size.fixedSize,
|
|
184
|
-
maxSize: size.fixedSize
|
|
185
|
-
};
|
|
155
|
+
assertIsFixedSize(size, "Codec [bool] requires a fixed size.");
|
|
156
|
+
return mapDecoder(size, (value) => Number(value) === 1);
|
|
186
157
|
}
|
|
187
158
|
function getBooleanCodec(config = {}) {
|
|
188
159
|
return combineCodec(getBooleanEncoder(config), getBooleanDecoder(config));
|
|
189
160
|
}
|
|
190
161
|
function getBytesEncoder(config = {}) {
|
|
191
162
|
const size = config.size ?? "variable";
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
};
|
|
163
|
+
const byteEncoder = createEncoder({
|
|
164
|
+
getSizeFromValue: (value) => value.length,
|
|
165
|
+
write: (value, bytes, offset) => {
|
|
166
|
+
bytes.set(value, offset);
|
|
167
|
+
return offset + value.length;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
200
170
|
if (size === "variable") {
|
|
201
171
|
return byteEncoder;
|
|
202
172
|
}
|
|
203
173
|
if (typeof size === "number") {
|
|
204
|
-
return fixEncoder(byteEncoder, size
|
|
174
|
+
return fixEncoder(byteEncoder, size);
|
|
205
175
|
}
|
|
206
|
-
return {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return mergeBytes([lengthBytes, contentBytes]);
|
|
176
|
+
return createEncoder({
|
|
177
|
+
getSizeFromValue: (value) => getEncodedSize(value.length, size) + value.length,
|
|
178
|
+
write: (value, bytes, offset) => {
|
|
179
|
+
offset = size.write(value.length, bytes, offset);
|
|
180
|
+
return byteEncoder.write(value, bytes, offset);
|
|
212
181
|
}
|
|
213
|
-
};
|
|
182
|
+
});
|
|
214
183
|
}
|
|
215
184
|
function getBytesDecoder(config = {}) {
|
|
216
185
|
const size = config.size ?? "variable";
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
const byteDecoder = {
|
|
220
|
-
decode: (bytes, offset = 0) => {
|
|
186
|
+
const byteDecoder = createDecoder({
|
|
187
|
+
read: (bytes, offset) => {
|
|
221
188
|
const slice = bytes.slice(offset);
|
|
222
189
|
return [slice, offset + slice.length];
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
fixedSize: null,
|
|
226
|
-
maxSize: null
|
|
227
|
-
};
|
|
190
|
+
}
|
|
191
|
+
});
|
|
228
192
|
if (size === "variable") {
|
|
229
193
|
return byteDecoder;
|
|
230
194
|
}
|
|
231
195
|
if (typeof size === "number") {
|
|
232
|
-
return fixDecoder(byteDecoder, size
|
|
196
|
+
return fixDecoder(byteDecoder, size);
|
|
233
197
|
}
|
|
234
|
-
return {
|
|
235
|
-
|
|
236
|
-
decode: (bytes, offset = 0) => {
|
|
198
|
+
return createDecoder({
|
|
199
|
+
read: (bytes, offset) => {
|
|
237
200
|
assertByteArrayIsNotEmptyForCodec("bytes", bytes, offset);
|
|
238
|
-
const [lengthBigInt, lengthOffset] = size.
|
|
201
|
+
const [lengthBigInt, lengthOffset] = size.read(bytes, offset);
|
|
239
202
|
const length = Number(lengthBigInt);
|
|
240
203
|
offset = lengthOffset;
|
|
241
204
|
const contentBytes = bytes.slice(offset, offset + length);
|
|
242
205
|
assertByteArrayHasEnoughBytesForCodec("bytes", length, contentBytes);
|
|
243
|
-
const [value, contentOffset] = byteDecoder.
|
|
206
|
+
const [value, contentOffset] = byteDecoder.read(contentBytes, 0);
|
|
244
207
|
offset += contentOffset;
|
|
245
208
|
return [value, offset];
|
|
246
209
|
}
|
|
247
|
-
};
|
|
210
|
+
});
|
|
248
211
|
}
|
|
249
212
|
function getBytesCodec(config = {}) {
|
|
250
213
|
return combineCodec(getBytesEncoder(config), getBytesDecoder(config));
|
|
251
214
|
}
|
|
252
|
-
function dataEnumCodecHelper(variants, prefix, description) {
|
|
253
|
-
const fieldDescriptions = variants.map(([name, codec]) => `${String(name)}${codec ? `: ${codec.description}` : ""}`).join(", ");
|
|
254
|
-
const allVariantHaveTheSameFixedSize = variants.every((one, _i, all) => one[1].fixedSize === all[0][1].fixedSize);
|
|
255
|
-
const fixedVariantSize = allVariantHaveTheSameFixedSize ? variants[0][1].fixedSize : null;
|
|
256
|
-
const maxVariantSize = maxCodecSizes(variants.map(([, field]) => field.maxSize));
|
|
257
|
-
return {
|
|
258
|
-
description: description ?? `dataEnum(${fieldDescriptions}; ${prefix.description})`,
|
|
259
|
-
fixedSize: variants.length === 0 ? prefix.fixedSize : sumCodecSizes([prefix.fixedSize, fixedVariantSize]),
|
|
260
|
-
maxSize: variants.length === 0 ? prefix.maxSize : sumCodecSizes([prefix.maxSize, maxVariantSize])
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
215
|
function getDataEnumEncoder(variants, config = {}) {
|
|
264
216
|
const prefix = config.size ?? getU8Encoder();
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
const
|
|
277
|
-
|
|
217
|
+
const fixedSize = getDataEnumFixedSize(variants, prefix);
|
|
218
|
+
return createEncoder({
|
|
219
|
+
...fixedSize !== null ? { fixedSize } : {
|
|
220
|
+
getSizeFromValue: (variant) => {
|
|
221
|
+
const discriminator = getVariantDiscriminator(variants, variant);
|
|
222
|
+
const variantEncoder = variants[discriminator][1];
|
|
223
|
+
return getEncodedSize(discriminator, prefix) + getEncodedSize(variant, variantEncoder);
|
|
224
|
+
},
|
|
225
|
+
maxSize: getDataEnumMaxSize(variants, prefix)
|
|
226
|
+
},
|
|
227
|
+
write: (variant, bytes, offset) => {
|
|
228
|
+
const discriminator = getVariantDiscriminator(variants, variant);
|
|
229
|
+
offset = prefix.write(discriminator, bytes, offset);
|
|
230
|
+
const variantEncoder = variants[discriminator][1];
|
|
231
|
+
return variantEncoder.write(variant, bytes, offset);
|
|
278
232
|
}
|
|
279
|
-
};
|
|
233
|
+
});
|
|
280
234
|
}
|
|
281
235
|
function getDataEnumDecoder(variants, config = {}) {
|
|
282
236
|
const prefix = config.size ?? getU8Decoder();
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
237
|
+
const fixedSize = getDataEnumFixedSize(variants, prefix);
|
|
238
|
+
return createDecoder({
|
|
239
|
+
...fixedSize !== null ? { fixedSize } : { maxSize: getDataEnumMaxSize(variants, prefix) },
|
|
240
|
+
read: (bytes, offset) => {
|
|
286
241
|
assertByteArrayIsNotEmptyForCodec("dataEnum", bytes, offset);
|
|
287
|
-
const [discriminator, dOffset] = prefix.
|
|
242
|
+
const [discriminator, dOffset] = prefix.read(bytes, offset);
|
|
288
243
|
offset = dOffset;
|
|
289
244
|
const variantField = variants[Number(discriminator)] ?? null;
|
|
290
245
|
if (!variantField) {
|
|
@@ -292,327 +247,280 @@ function getDataEnumDecoder(variants, config = {}) {
|
|
|
292
247
|
`Enum discriminator out of range. Expected a number between 0 and ${variants.length - 1}, got ${discriminator}.`
|
|
293
248
|
);
|
|
294
249
|
}
|
|
295
|
-
const [variant, vOffset] = variantField[1].
|
|
250
|
+
const [variant, vOffset] = variantField[1].read(bytes, offset);
|
|
296
251
|
offset = vOffset;
|
|
297
252
|
return [{ __kind: variantField[0], ...variant ?? {} }, offset];
|
|
298
253
|
}
|
|
299
|
-
};
|
|
254
|
+
});
|
|
300
255
|
}
|
|
301
256
|
function getDataEnumCodec(variants, config = {}) {
|
|
302
|
-
return combineCodec(
|
|
257
|
+
return combineCodec(
|
|
258
|
+
getDataEnumEncoder(variants, config),
|
|
259
|
+
getDataEnumDecoder(variants, config)
|
|
260
|
+
);
|
|
303
261
|
}
|
|
304
|
-
function
|
|
305
|
-
if (
|
|
306
|
-
|
|
262
|
+
function getDataEnumFixedSize(variants, prefix) {
|
|
263
|
+
if (variants.length === 0)
|
|
264
|
+
return isFixedSize(prefix) ? prefix.fixedSize : null;
|
|
265
|
+
if (!isFixedSize(variants[0][1]))
|
|
266
|
+
return null;
|
|
267
|
+
const variantSize = variants[0][1].fixedSize;
|
|
268
|
+
const sameSizedVariants = variants.every(
|
|
269
|
+
(variant) => isFixedSize(variant[1]) && variant[1].fixedSize === variantSize
|
|
270
|
+
);
|
|
271
|
+
if (!sameSizedVariants)
|
|
272
|
+
return null;
|
|
273
|
+
return isFixedSize(prefix) ? prefix.fixedSize + variantSize : null;
|
|
274
|
+
}
|
|
275
|
+
function getDataEnumMaxSize(variants, prefix) {
|
|
276
|
+
const maxVariantSize = maxCodecSizes(variants.map(([, codec]) => getMaxSize(codec)));
|
|
277
|
+
return sumCodecSizes([getMaxSize(prefix), maxVariantSize]) ?? void 0;
|
|
278
|
+
}
|
|
279
|
+
function getVariantDiscriminator(variants, variant) {
|
|
280
|
+
const discriminator = variants.findIndex(([key]) => variant.__kind === key);
|
|
281
|
+
if (discriminator < 0) {
|
|
282
|
+
throw new Error(
|
|
283
|
+
`Invalid data enum variant. Expected one of [${variants.map(([key]) => key).join(", ")}], got "${variant.__kind}".`
|
|
284
|
+
);
|
|
307
285
|
}
|
|
308
|
-
return
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
286
|
+
return discriminator;
|
|
287
|
+
}
|
|
288
|
+
function getTupleEncoder(items) {
|
|
289
|
+
const fixedSize = sumCodecSizes(items.map(getFixedSize));
|
|
290
|
+
const maxSize = sumCodecSizes(items.map(getMaxSize)) ?? void 0;
|
|
291
|
+
return createEncoder({
|
|
292
|
+
...fixedSize === null ? {
|
|
293
|
+
getSizeFromValue: (value) => items.map((item, index) => getEncodedSize(value[index], item)).reduce((all, one) => all + one, 0),
|
|
294
|
+
maxSize
|
|
295
|
+
} : { fixedSize },
|
|
296
|
+
write: (value, bytes, offset) => {
|
|
297
|
+
assertValidNumberOfItemsForCodec("tuple", items.length, value.length);
|
|
298
|
+
items.forEach((item, index) => {
|
|
299
|
+
offset = item.write(value[index], bytes, offset);
|
|
300
|
+
});
|
|
301
|
+
return offset;
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
function getTupleDecoder(items) {
|
|
306
|
+
const fixedSize = sumCodecSizes(items.map(getFixedSize));
|
|
307
|
+
const maxSize = sumCodecSizes(items.map(getMaxSize)) ?? void 0;
|
|
308
|
+
return createDecoder({
|
|
309
|
+
...fixedSize === null ? { maxSize } : { fixedSize },
|
|
310
|
+
read: (bytes, offset) => {
|
|
311
|
+
const values = [];
|
|
312
|
+
items.forEach((item) => {
|
|
313
|
+
const [newValue, newOffset] = item.read(bytes, offset);
|
|
314
|
+
values.push(newValue);
|
|
315
|
+
offset = newOffset;
|
|
316
|
+
});
|
|
317
|
+
return [values, offset];
|
|
318
|
+
}
|
|
319
|
+
});
|
|
313
320
|
}
|
|
321
|
+
function getTupleCodec(items) {
|
|
322
|
+
return combineCodec(
|
|
323
|
+
getTupleEncoder(items),
|
|
324
|
+
getTupleDecoder(items)
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// src/map.ts
|
|
314
329
|
function getMapEncoder(key, value, config = {}) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
if (typeof size === "number") {
|
|
320
|
-
assertValidNumberOfItemsForCodec("map", size, map.size);
|
|
321
|
-
}
|
|
322
|
-
const itemBytes = Array.from(map, ([k, v]) => mergeBytes([key.encode(k), value.encode(v)]));
|
|
323
|
-
return mergeBytes([getArrayLikeCodecSizePrefix(size, map.size), ...itemBytes]);
|
|
324
|
-
}
|
|
325
|
-
};
|
|
330
|
+
return mapEncoder(
|
|
331
|
+
getArrayEncoder(getTupleEncoder([key, value]), config),
|
|
332
|
+
(map) => [...map.entries()]
|
|
333
|
+
);
|
|
326
334
|
}
|
|
327
335
|
function getMapDecoder(key, value, config = {}) {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
const map = /* @__PURE__ */ new Map();
|
|
333
|
-
if (typeof size === "object" && bytes.slice(offset).length === 0) {
|
|
334
|
-
return [map, offset];
|
|
335
|
-
}
|
|
336
|
-
const [resolvedSize, newOffset] = decodeArrayLikeCodecSize(
|
|
337
|
-
size,
|
|
338
|
-
[key.fixedSize, value.fixedSize],
|
|
339
|
-
bytes,
|
|
340
|
-
offset
|
|
341
|
-
);
|
|
342
|
-
offset = newOffset;
|
|
343
|
-
for (let i = 0; i < resolvedSize; i += 1) {
|
|
344
|
-
const [decodedKey, kOffset] = key.decode(bytes, offset);
|
|
345
|
-
offset = kOffset;
|
|
346
|
-
const [decodedValue, vOffset] = value.decode(bytes, offset);
|
|
347
|
-
offset = vOffset;
|
|
348
|
-
map.set(decodedKey, decodedValue);
|
|
349
|
-
}
|
|
350
|
-
return [map, offset];
|
|
351
|
-
}
|
|
352
|
-
};
|
|
336
|
+
return mapDecoder(
|
|
337
|
+
getArrayDecoder(getTupleDecoder([key, value]), config),
|
|
338
|
+
(entries) => new Map(entries)
|
|
339
|
+
);
|
|
353
340
|
}
|
|
354
341
|
function getMapCodec(key, value, config = {}) {
|
|
355
342
|
return combineCodec(getMapEncoder(key, value, config), getMapDecoder(key, value, config));
|
|
356
343
|
}
|
|
357
|
-
function nullableCodecHelper(item, prefix, fixed, description) {
|
|
358
|
-
let descriptionSuffix = `; ${prefix.description}`;
|
|
359
|
-
let fixedSize = item.fixedSize === 0 ? prefix.fixedSize : null;
|
|
360
|
-
if (fixed) {
|
|
361
|
-
assertFixedSizeCodec(item, "Fixed nullables can only be used with fixed-size codecs.");
|
|
362
|
-
assertFixedSizeCodec(prefix, "Fixed nullables can only be used with fixed-size prefix.");
|
|
363
|
-
descriptionSuffix += "; fixed";
|
|
364
|
-
fixedSize = prefix.fixedSize + item.fixedSize;
|
|
365
|
-
}
|
|
366
|
-
return {
|
|
367
|
-
description: description ?? `nullable(${item.description + descriptionSuffix})`,
|
|
368
|
-
fixedSize,
|
|
369
|
-
maxSize: sumCodecSizes([prefix.maxSize, item.maxSize])
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
344
|
function getNullableEncoder(item, config = {}) {
|
|
373
345
|
const prefix = config.prefix ?? getU8Encoder();
|
|
374
346
|
const fixed = config.fixed ?? false;
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
347
|
+
const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
|
|
348
|
+
if (fixed || isZeroSizeItem) {
|
|
349
|
+
assertIsFixedSize(item, "Fixed nullables can only be used with fixed-size codecs.");
|
|
350
|
+
assertIsFixedSize(prefix, "Fixed nullables can only be used with fixed-size prefix.");
|
|
351
|
+
const fixedSize = prefix.fixedSize + item.fixedSize;
|
|
352
|
+
return createEncoder({
|
|
353
|
+
fixedSize,
|
|
354
|
+
write: (option, bytes, offset) => {
|
|
355
|
+
const prefixOffset = prefix.write(Number(option !== null), bytes, offset);
|
|
356
|
+
if (option !== null) {
|
|
357
|
+
item.write(option, bytes, prefixOffset);
|
|
358
|
+
}
|
|
359
|
+
return offset + fixedSize;
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
return createEncoder({
|
|
364
|
+
getSizeFromValue: (option) => getEncodedSize(Number(option !== null), prefix) + (option !== null ? getEncodedSize(option, item) : 0),
|
|
365
|
+
maxSize: sumCodecSizes([prefix, item].map(getMaxSize)) ?? void 0,
|
|
366
|
+
write: (option, bytes, offset) => {
|
|
367
|
+
offset = prefix.write(Number(option !== null), bytes, offset);
|
|
368
|
+
if (option !== null) {
|
|
369
|
+
offset = item.write(option, bytes, offset);
|
|
370
|
+
}
|
|
371
|
+
return offset;
|
|
382
372
|
}
|
|
383
|
-
};
|
|
373
|
+
});
|
|
384
374
|
}
|
|
385
375
|
function getNullableDecoder(item, config = {}) {
|
|
386
376
|
const prefix = config.prefix ?? getU8Decoder();
|
|
387
377
|
const fixed = config.fixed ?? false;
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
378
|
+
let fixedSize = null;
|
|
379
|
+
const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
|
|
380
|
+
if (fixed || isZeroSizeItem) {
|
|
381
|
+
assertIsFixedSize(item, "Fixed nullables can only be used with fixed-size codecs.");
|
|
382
|
+
assertIsFixedSize(prefix, "Fixed nullables can only be used with fixed-size prefix.");
|
|
383
|
+
fixedSize = prefix.fixedSize + item.fixedSize;
|
|
384
|
+
}
|
|
385
|
+
return createDecoder({
|
|
386
|
+
...fixedSize === null ? { maxSize: sumCodecSizes([prefix, item].map(getMaxSize)) ?? void 0 } : { fixedSize },
|
|
387
|
+
read: (bytes, offset) => {
|
|
391
388
|
if (bytes.length - offset <= 0) {
|
|
392
389
|
return [null, offset];
|
|
393
390
|
}
|
|
394
|
-
const
|
|
395
|
-
const [isSome, prefixOffset] = prefix.decode(bytes, offset);
|
|
396
|
-
offset = prefixOffset;
|
|
391
|
+
const [isSome, prefixOffset] = prefix.read(bytes, offset);
|
|
397
392
|
if (isSome === 0) {
|
|
398
|
-
return [null,
|
|
393
|
+
return [null, fixedSize !== null ? offset + fixedSize : prefixOffset];
|
|
399
394
|
}
|
|
400
|
-
const [value, newOffset] = item.
|
|
401
|
-
offset
|
|
402
|
-
return [value, fixed ? fixedOffset : offset];
|
|
395
|
+
const [value, newOffset] = item.read(bytes, prefixOffset);
|
|
396
|
+
return [value, fixedSize !== null ? offset + fixedSize : newOffset];
|
|
403
397
|
}
|
|
404
|
-
};
|
|
398
|
+
});
|
|
405
399
|
}
|
|
406
400
|
function getNullableCodec(item, config = {}) {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
function scalarEnumCoderHelper(constructor, prefix, description) {
|
|
410
|
-
const enumKeys = Object.keys(constructor);
|
|
411
|
-
const enumValues = Object.values(constructor);
|
|
412
|
-
const isNumericEnum = enumValues.some((v) => typeof v === "number");
|
|
413
|
-
const valueDescriptions = enumValues.filter((v) => typeof v === "string").join(", ");
|
|
414
|
-
const minRange = 0;
|
|
415
|
-
const maxRange = isNumericEnum ? enumValues.length / 2 - 1 : enumValues.length - 1;
|
|
416
|
-
const stringValues = isNumericEnum ? [...enumKeys] : [.../* @__PURE__ */ new Set([...enumKeys, ...enumValues])];
|
|
417
|
-
return {
|
|
418
|
-
description: description ?? `enum(${valueDescriptions}; ${prefix.description})`,
|
|
419
|
-
enumKeys,
|
|
420
|
-
enumValues,
|
|
421
|
-
fixedSize: prefix.fixedSize,
|
|
422
|
-
isNumericEnum,
|
|
423
|
-
maxRange,
|
|
424
|
-
maxSize: prefix.maxSize,
|
|
425
|
-
minRange,
|
|
426
|
-
stringValues
|
|
427
|
-
};
|
|
401
|
+
const configCast = config;
|
|
402
|
+
return combineCodec(getNullableEncoder(item, configCast), getNullableDecoder(item, configCast));
|
|
428
403
|
}
|
|
429
404
|
function getScalarEnumEncoder(constructor, config = {}) {
|
|
430
405
|
const prefix = config.size ?? getU8Encoder();
|
|
431
|
-
const {
|
|
432
|
-
return {
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
return prefix.encode(enumKeys.indexOf(value));
|
|
448
|
-
},
|
|
449
|
-
fixedSize,
|
|
450
|
-
maxSize
|
|
451
|
-
};
|
|
406
|
+
const { minRange, maxRange, allStringInputs, enumKeys, enumValues } = getScalarEnumStats(constructor);
|
|
407
|
+
return mapEncoder(prefix, (value) => {
|
|
408
|
+
const isInvalidNumber = typeof value === "number" && (value < minRange || value > maxRange);
|
|
409
|
+
const isInvalidString = typeof value === "string" && !allStringInputs.includes(value);
|
|
410
|
+
if (isInvalidNumber || isInvalidString) {
|
|
411
|
+
throw new Error(
|
|
412
|
+
`Invalid scalar enum variant. Expected one of [${allStringInputs.join(", ")}] or a number between ${minRange} and ${maxRange}, got "${value}".`
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
if (typeof value === "number")
|
|
416
|
+
return value;
|
|
417
|
+
const valueIndex = enumValues.indexOf(value);
|
|
418
|
+
if (valueIndex >= 0)
|
|
419
|
+
return valueIndex;
|
|
420
|
+
return enumKeys.indexOf(value);
|
|
421
|
+
});
|
|
452
422
|
}
|
|
453
423
|
function getScalarEnumDecoder(constructor, config = {}) {
|
|
454
424
|
const prefix = config.size ?? getU8Decoder();
|
|
455
|
-
const {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
offset = newOffset;
|
|
466
|
-
if (valueAsNumber < minRange || valueAsNumber > maxRange) {
|
|
467
|
-
throw new Error(
|
|
468
|
-
`Enum discriminator out of range. Expected a number between ${minRange} and ${maxRange}, got ${valueAsNumber}.`
|
|
469
|
-
);
|
|
470
|
-
}
|
|
471
|
-
return [isNumericEnum ? valueAsNumber : enumValues[valueAsNumber], offset];
|
|
472
|
-
},
|
|
473
|
-
description,
|
|
474
|
-
fixedSize,
|
|
475
|
-
maxSize
|
|
476
|
-
};
|
|
425
|
+
const { minRange, maxRange, enumKeys } = getScalarEnumStats(constructor);
|
|
426
|
+
return mapDecoder(prefix, (value) => {
|
|
427
|
+
const valueAsNumber = Number(value);
|
|
428
|
+
if (valueAsNumber < minRange || valueAsNumber > maxRange) {
|
|
429
|
+
throw new Error(
|
|
430
|
+
`Enum discriminator out of range. Expected a number between ${minRange} and ${maxRange}, got ${valueAsNumber}.`
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
return constructor[enumKeys[valueAsNumber]];
|
|
434
|
+
});
|
|
477
435
|
}
|
|
478
436
|
function getScalarEnumCodec(constructor, config = {}) {
|
|
479
437
|
return combineCodec(getScalarEnumEncoder(constructor, config), getScalarEnumDecoder(constructor, config));
|
|
480
438
|
}
|
|
481
|
-
function
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
439
|
+
function getScalarEnumStats(constructor) {
|
|
440
|
+
const numericValues = Object.values(constructor).filter((v) => typeof v === "number");
|
|
441
|
+
const deduplicatedConstructor = Object.fromEntries(
|
|
442
|
+
Object.entries(constructor).slice(numericValues.length)
|
|
443
|
+
);
|
|
444
|
+
const enumKeys = Object.keys(deduplicatedConstructor);
|
|
445
|
+
const enumValues = Object.values(deduplicatedConstructor);
|
|
446
|
+
const minRange = 0;
|
|
447
|
+
const maxRange = enumValues.length - 1;
|
|
448
|
+
const allStringInputs = [
|
|
449
|
+
.../* @__PURE__ */ new Set([...enumKeys, ...enumValues.filter((v) => typeof v === "string")])
|
|
450
|
+
];
|
|
485
451
|
return {
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
452
|
+
allStringInputs,
|
|
453
|
+
enumKeys,
|
|
454
|
+
enumValues,
|
|
455
|
+
maxRange,
|
|
456
|
+
minRange
|
|
489
457
|
};
|
|
490
458
|
}
|
|
491
459
|
function getSetEncoder(item, config = {}) {
|
|
492
|
-
|
|
493
|
-
return {
|
|
494
|
-
...setCodecHelper(item, size, config.description),
|
|
495
|
-
encode: (set) => {
|
|
496
|
-
if (typeof size === "number" && set.size !== size) {
|
|
497
|
-
assertValidNumberOfItemsForCodec("set", size, set.size);
|
|
498
|
-
}
|
|
499
|
-
const itemBytes = Array.from(set, (value) => item.encode(value));
|
|
500
|
-
return mergeBytes([getArrayLikeCodecSizePrefix(size, set.size), ...itemBytes]);
|
|
501
|
-
}
|
|
502
|
-
};
|
|
460
|
+
return mapEncoder(getArrayEncoder(item, config), (set) => [...set]);
|
|
503
461
|
}
|
|
504
462
|
function getSetDecoder(item, config = {}) {
|
|
505
|
-
|
|
506
|
-
return {
|
|
507
|
-
...setCodecHelper(item, size, config.description),
|
|
508
|
-
decode: (bytes, offset = 0) => {
|
|
509
|
-
const set = /* @__PURE__ */ new Set();
|
|
510
|
-
if (typeof size === "object" && bytes.slice(offset).length === 0) {
|
|
511
|
-
return [set, offset];
|
|
512
|
-
}
|
|
513
|
-
const [resolvedSize, newOffset] = decodeArrayLikeCodecSize(size, [item.fixedSize], bytes, offset);
|
|
514
|
-
offset = newOffset;
|
|
515
|
-
for (let i = 0; i < resolvedSize; i += 1) {
|
|
516
|
-
const [value, newOffset2] = item.decode(bytes, offset);
|
|
517
|
-
offset = newOffset2;
|
|
518
|
-
set.add(value);
|
|
519
|
-
}
|
|
520
|
-
return [set, offset];
|
|
521
|
-
}
|
|
522
|
-
};
|
|
463
|
+
return mapDecoder(getArrayDecoder(item, config), (entries) => new Set(entries));
|
|
523
464
|
}
|
|
524
465
|
function getSetCodec(item, config = {}) {
|
|
525
466
|
return combineCodec(getSetEncoder(item, config), getSetDecoder(item, config));
|
|
526
467
|
}
|
|
527
|
-
function
|
|
528
|
-
const
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
return
|
|
468
|
+
function getStructEncoder(fields) {
|
|
469
|
+
const fieldCodecs = fields.map(([, codec]) => codec);
|
|
470
|
+
const fixedSize = sumCodecSizes(fieldCodecs.map(getFixedSize));
|
|
471
|
+
const maxSize = sumCodecSizes(fieldCodecs.map(getMaxSize)) ?? void 0;
|
|
472
|
+
return createEncoder({
|
|
473
|
+
...fixedSize === null ? {
|
|
474
|
+
getSizeFromValue: (value) => fields.map(([key, codec]) => getEncodedSize(value[key], codec)).reduce((all, one) => all + one, 0),
|
|
475
|
+
maxSize
|
|
476
|
+
} : { fixedSize },
|
|
477
|
+
write: (struct, bytes, offset) => {
|
|
478
|
+
fields.forEach(([key, codec]) => {
|
|
479
|
+
offset = codec.write(struct[key], bytes, offset);
|
|
480
|
+
});
|
|
481
|
+
return offset;
|
|
541
482
|
}
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
|
-
function getStructDecoder(fields
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
function getStructDecoder(fields) {
|
|
486
|
+
const fieldCodecs = fields.map(([, codec]) => codec);
|
|
487
|
+
const fixedSize = sumCodecSizes(fieldCodecs.map(getFixedSize));
|
|
488
|
+
const maxSize = sumCodecSizes(fieldCodecs.map(getMaxSize)) ?? void 0;
|
|
489
|
+
return createDecoder({
|
|
490
|
+
...fixedSize === null ? { maxSize } : { fixedSize },
|
|
491
|
+
read: (bytes, offset) => {
|
|
548
492
|
const struct = {};
|
|
549
493
|
fields.forEach(([key, codec]) => {
|
|
550
|
-
const [value, newOffset] = codec.
|
|
494
|
+
const [value, newOffset] = codec.read(bytes, offset);
|
|
551
495
|
offset = newOffset;
|
|
552
496
|
struct[key] = value;
|
|
553
497
|
});
|
|
554
498
|
return [struct, offset];
|
|
555
499
|
}
|
|
556
|
-
};
|
|
557
|
-
}
|
|
558
|
-
function getStructCodec(fields, config = {}) {
|
|
559
|
-
return combineCodec(getStructEncoder(fields, config), getStructDecoder(fields, config));
|
|
560
|
-
}
|
|
561
|
-
function tupleCodecHelper(items, description) {
|
|
562
|
-
const itemDescriptions = items.map((item) => item.description).join(", ");
|
|
563
|
-
return {
|
|
564
|
-
description: description ?? `tuple(${itemDescriptions})`,
|
|
565
|
-
fixedSize: sumCodecSizes(items.map((item) => item.fixedSize)),
|
|
566
|
-
maxSize: sumCodecSizes(items.map((item) => item.maxSize))
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
function getTupleEncoder(items, config = {}) {
|
|
570
|
-
return {
|
|
571
|
-
...tupleCodecHelper(items, config.description),
|
|
572
|
-
encode: (value) => {
|
|
573
|
-
assertValidNumberOfItemsForCodec("tuple", items.length, value.length);
|
|
574
|
-
return mergeBytes(items.map((item, index) => item.encode(value[index])));
|
|
575
|
-
}
|
|
576
|
-
};
|
|
500
|
+
});
|
|
577
501
|
}
|
|
578
|
-
function
|
|
579
|
-
return {
|
|
580
|
-
...tupleCodecHelper(items, config.description),
|
|
581
|
-
decode: (bytes, offset = 0) => {
|
|
582
|
-
const values = [];
|
|
583
|
-
items.forEach((codec) => {
|
|
584
|
-
const [newValue, newOffset] = codec.decode(bytes, offset);
|
|
585
|
-
values.push(newValue);
|
|
586
|
-
offset = newOffset;
|
|
587
|
-
});
|
|
588
|
-
return [values, offset];
|
|
589
|
-
}
|
|
590
|
-
};
|
|
591
|
-
}
|
|
592
|
-
function getTupleCodec(items, config = {}) {
|
|
502
|
+
function getStructCodec(fields) {
|
|
593
503
|
return combineCodec(
|
|
594
|
-
|
|
595
|
-
|
|
504
|
+
getStructEncoder(fields),
|
|
505
|
+
getStructDecoder(fields)
|
|
596
506
|
);
|
|
597
507
|
}
|
|
598
|
-
function getUnitEncoder(
|
|
599
|
-
return {
|
|
600
|
-
description: config.description ?? "unit",
|
|
601
|
-
encode: () => new Uint8Array(),
|
|
508
|
+
function getUnitEncoder() {
|
|
509
|
+
return createEncoder({
|
|
602
510
|
fixedSize: 0,
|
|
603
|
-
|
|
604
|
-
};
|
|
511
|
+
write: (_value, _bytes, offset) => offset
|
|
512
|
+
});
|
|
605
513
|
}
|
|
606
|
-
function getUnitDecoder(
|
|
607
|
-
return {
|
|
608
|
-
decode: (_bytes, offset = 0) => [void 0, offset],
|
|
609
|
-
description: config.description ?? "unit",
|
|
514
|
+
function getUnitDecoder() {
|
|
515
|
+
return createDecoder({
|
|
610
516
|
fixedSize: 0,
|
|
611
|
-
|
|
612
|
-
};
|
|
517
|
+
read: (_bytes, offset) => [void 0, offset]
|
|
518
|
+
});
|
|
613
519
|
}
|
|
614
|
-
function getUnitCodec(
|
|
615
|
-
return combineCodec(getUnitEncoder(
|
|
520
|
+
function getUnitCodec() {
|
|
521
|
+
return combineCodec(getUnitEncoder(), getUnitDecoder());
|
|
616
522
|
}
|
|
617
523
|
|
|
618
|
-
export { assertValidNumberOfItemsForCodec,
|
|
524
|
+
export { assertValidNumberOfItemsForCodec, getArrayCodec, getArrayDecoder, getArrayEncoder, 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 };
|
|
525
|
+
//# sourceMappingURL=out.js.map
|
|
526
|
+
//# sourceMappingURL=index.node.js.map
|