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