@yume-chan/struct 0.0.9 → 0.0.13

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.
Files changed (151) hide show
  1. package/CHANGELOG.json +65 -0
  2. package/CHANGELOG.md +39 -0
  3. package/LICENSE +21 -21
  4. package/README.md +769 -709
  5. package/esm/basic/definition.d.ts +44 -0
  6. package/esm/basic/definition.d.ts.map +1 -0
  7. package/esm/basic/definition.js +13 -9
  8. package/esm/basic/definition.js.map +1 -1
  9. package/esm/basic/field-value.d.ts +38 -0
  10. package/esm/basic/field-value.d.ts.map +1 -0
  11. package/esm/basic/field-value.js +13 -7
  12. package/esm/basic/field-value.js.map +1 -1
  13. package/esm/basic/index.d.ts +6 -0
  14. package/esm/basic/index.d.ts.map +1 -0
  15. package/esm/basic/index.js +5 -4
  16. package/esm/basic/index.js.map +1 -1
  17. package/esm/basic/options.d.ts +10 -0
  18. package/esm/basic/options.d.ts.map +1 -0
  19. package/esm/basic/{context.js → options.js} +1 -1
  20. package/esm/basic/options.js.map +1 -0
  21. package/esm/basic/stream.d.ts +19 -0
  22. package/esm/basic/stream.d.ts.map +1 -0
  23. package/esm/basic/stream.js +2 -0
  24. package/esm/basic/stream.js.map +1 -0
  25. package/{dts → esm}/basic/struct-value.d.ts +3 -3
  26. package/esm/basic/struct-value.d.ts.map +1 -0
  27. package/esm/basic/struct-value.js +7 -15
  28. package/esm/basic/struct-value.js.map +1 -1
  29. package/esm/index.d.ts +14 -0
  30. package/esm/index.d.ts.map +1 -0
  31. package/esm/index.js +5 -5
  32. package/esm/index.js.map +1 -1
  33. package/{dts → esm}/struct.d.ts +25 -22
  34. package/esm/struct.d.ts.map +1 -0
  35. package/esm/struct.js +75 -52
  36. package/esm/struct.js.map +1 -1
  37. package/esm/syncbird.d.ts +65 -0
  38. package/esm/syncbird.d.ts.map +1 -0
  39. package/esm/syncbird.js +38 -0
  40. package/esm/syncbird.js.map +1 -0
  41. package/esm/types/bigint.d.ts +25 -0
  42. package/esm/types/bigint.d.ts.map +1 -0
  43. package/esm/types/bigint.js +45 -0
  44. package/esm/types/bigint.js.map +1 -0
  45. package/esm/types/buffer/base.d.ts +63 -0
  46. package/esm/types/buffer/base.d.ts.map +1 -0
  47. package/esm/types/buffer/base.js +100 -0
  48. package/esm/types/buffer/base.js.map +1 -0
  49. package/esm/types/buffer/fixed-length.d.ts +8 -0
  50. package/esm/types/buffer/fixed-length.d.ts.map +1 -0
  51. package/esm/types/buffer/fixed-length.js +8 -0
  52. package/esm/types/buffer/fixed-length.js.map +1 -0
  53. package/esm/types/buffer/index.d.ts +4 -0
  54. package/esm/types/buffer/index.d.ts.map +1 -0
  55. package/esm/types/buffer/index.js +4 -0
  56. package/esm/types/buffer/index.js.map +1 -0
  57. package/esm/types/buffer/variable-length.d.ts +43 -0
  58. package/esm/types/buffer/variable-length.d.ts.map +1 -0
  59. package/esm/types/buffer/variable-length.js +76 -0
  60. package/esm/types/buffer/variable-length.js.map +1 -0
  61. package/esm/types/index.d.ts +4 -0
  62. package/esm/types/index.d.ts.map +1 -0
  63. package/esm/types/index.js +3 -4
  64. package/esm/types/index.js.map +1 -1
  65. package/esm/types/number.d.ts +32 -0
  66. package/esm/types/number.d.ts.map +1 -0
  67. package/esm/types/number.js +26 -18
  68. package/esm/types/number.js.map +1 -1
  69. package/{dts → esm}/utils.d.ts +3 -2
  70. package/{dts → esm}/utils.d.ts.map +1 -1
  71. package/esm/utils.js +16 -0
  72. package/esm/utils.js.map +1 -1
  73. package/package.json +46 -49
  74. package/src/basic/definition.ts +70 -69
  75. package/src/basic/field-value.ts +67 -73
  76. package/src/basic/index.ts +5 -4
  77. package/src/basic/options.ts +19 -0
  78. package/src/basic/stream.ts +19 -0
  79. package/src/basic/struct-value.ts +39 -45
  80. package/src/index.ts +17 -5
  81. package/src/struct.ts +609 -575
  82. package/src/syncbird.ts +131 -0
  83. package/src/types/bigint.ts +102 -0
  84. package/src/types/buffer/base.ts +176 -0
  85. package/src/types/buffer/fixed-length.ts +17 -0
  86. package/src/types/buffer/index.ts +3 -0
  87. package/src/types/buffer/variable-length.ts +156 -0
  88. package/src/types/index.ts +3 -4
  89. package/src/types/number.ts +115 -100
  90. package/src/utils.ts +70 -51
  91. package/cjs/basic/context.js +0 -7
  92. package/cjs/basic/context.js.map +0 -1
  93. package/cjs/basic/definition.js +0 -25
  94. package/cjs/basic/definition.js.map +0 -1
  95. package/cjs/basic/field-value.js +0 -40
  96. package/cjs/basic/field-value.js.map +0 -1
  97. package/cjs/basic/index.js +0 -8
  98. package/cjs/basic/index.js.map +0 -1
  99. package/cjs/basic/struct-value.js +0 -46
  100. package/cjs/basic/struct-value.js.map +0 -1
  101. package/cjs/index.js +0 -11
  102. package/cjs/index.js.map +0 -1
  103. package/cjs/struct.js +0 -177
  104. package/cjs/struct.js.map +0 -1
  105. package/cjs/types/array-buffer.js +0 -118
  106. package/cjs/types/array-buffer.js.map +0 -1
  107. package/cjs/types/fixed-length-array-buffer.js +0 -12
  108. package/cjs/types/fixed-length-array-buffer.js.map +0 -1
  109. package/cjs/types/index.js +0 -8
  110. package/cjs/types/index.js.map +0 -1
  111. package/cjs/types/number.js +0 -52
  112. package/cjs/types/number.js.map +0 -1
  113. package/cjs/types/variable-length-array-buffer.js +0 -77
  114. package/cjs/types/variable-length-array-buffer.js.map +0 -1
  115. package/cjs/utils.js +0 -11
  116. package/cjs/utils.js.map +0 -1
  117. package/dts/basic/context.d.ts +0 -36
  118. package/dts/basic/context.d.ts.map +0 -1
  119. package/dts/basic/definition.d.ts +0 -48
  120. package/dts/basic/definition.d.ts.map +0 -1
  121. package/dts/basic/field-value.d.ts +0 -40
  122. package/dts/basic/field-value.d.ts.map +0 -1
  123. package/dts/basic/index.d.ts +0 -5
  124. package/dts/basic/index.d.ts.map +0 -1
  125. package/dts/basic/struct-value.d.ts.map +0 -1
  126. package/dts/index.d.ts +0 -6
  127. package/dts/index.d.ts.map +0 -1
  128. package/dts/struct.d.ts.map +0 -1
  129. package/dts/types/array-buffer.d.ts +0 -70
  130. package/dts/types/array-buffer.d.ts.map +0 -1
  131. package/dts/types/fixed-length-array-buffer.d.ts +0 -8
  132. package/dts/types/fixed-length-array-buffer.d.ts.map +0 -1
  133. package/dts/types/index.d.ts +0 -5
  134. package/dts/types/index.d.ts.map +0 -1
  135. package/dts/types/number.d.ts +0 -33
  136. package/dts/types/number.d.ts.map +0 -1
  137. package/dts/types/variable-length-array-buffer.d.ts +0 -32
  138. package/dts/types/variable-length-array-buffer.d.ts.map +0 -1
  139. package/esm/basic/context.js.map +0 -1
  140. package/esm/types/array-buffer.js +0 -109
  141. package/esm/types/array-buffer.js.map +0 -1
  142. package/esm/types/fixed-length-array-buffer.js +0 -8
  143. package/esm/types/fixed-length-array-buffer.js.map +0 -1
  144. package/esm/types/variable-length-array-buffer.js +0 -71
  145. package/esm/types/variable-length-array-buffer.js.map +0 -1
  146. package/src/basic/context.ts +0 -42
  147. package/src/types/array-buffer.ts +0 -184
  148. package/src/types/fixed-length-array-buffer.ts +0 -17
  149. package/src/types/variable-length-array-buffer.ts +0 -145
  150. package/tsconfig.cjs.json +0 -11
  151. package/tsconfig.esm.json +0 -15
package/src/struct.ts CHANGED
@@ -1,575 +1,609 @@
1
- import { StructDefaultOptions, StructDeserializationContext, StructFieldDefinition, StructFieldValue, StructOptions, StructSerializationContext, StructValue } from './basic';
2
- import { ArrayBufferFieldType, ArrayBufferLikeFieldType, FixedLengthArrayBufferLikeFieldDefinition, FixedLengthArrayBufferLikeFieldOptions, LengthField, NumberFieldDefinition, NumberFieldType, StringFieldType, Uint8ClampedArrayFieldType, VariableLengthArrayBufferLikeFieldDefinition, VariableLengthArrayBufferLikeFieldOptions } from './types';
3
- import { Awaited, Evaluate, Identity, Overwrite } from './utils';
4
-
5
- export interface StructLike<TValue> {
6
- deserialize(context: StructDeserializationContext): Promise<TValue>;
7
- }
8
-
9
- /**
10
- * Extract the value type of the specified `Struct`
11
- *
12
- * The lack of generic constraint is on purpose to allow `StructLike` types
13
- */
14
- export type StructValueType<T extends StructLike<any>> =
15
- Awaited<ReturnType<T['deserialize']>>;
16
-
17
- /**
18
- * Create a new `Struct` type with `TDescriptor` appended
19
- */
20
- type AddFieldDescriptor<
21
- TFields extends object,
22
- TOmitInitKey extends PropertyKey,
23
- TExtra extends object,
24
- TPostDeserialized,
25
- TFieldName extends PropertyKey,
26
- TDefinition extends StructFieldDefinition<any, any, any>
27
- > =
28
- Identity<Struct<
29
- // Merge two types
30
- // Evaluate immediately to optimize editor hover tooltip
31
- Evaluate<TFields & Record<TFieldName, TDefinition['TValue']>>,
32
- // Merge two `TOmitInitKey`s
33
- TOmitInitKey | TDefinition['TOmitInitKey'],
34
- TExtra,
35
- TPostDeserialized
36
- >>;
37
-
38
- /**
39
- * Overload methods to add an array buffer like field
40
- */
41
- interface ArrayBufferLikeFieldCreator<
42
- TFields extends object,
43
- TOmitInitKey extends PropertyKey,
44
- TExtra extends object,
45
- TPostDeserialized
46
- > {
47
- /**
48
- * Append a fixed-length array to the `Struct`
49
- *
50
- * @param name Name of the field
51
- * @param type `Array.SubType.ArrayBuffer` or `Array.SubType.String`
52
- * @param options Fixed-length array options
53
- * @param typescriptType Type of the field in TypeScript.
54
- * For example, if this field is a string, you can declare it as a string enum or literal union.
55
- */
56
- <
57
- TName extends PropertyKey,
58
- TType extends ArrayBufferLikeFieldType,
59
- TTypeScriptType = TType['TTypeScriptType'],
60
- >(
61
- name: TName,
62
- type: TType,
63
- options: FixedLengthArrayBufferLikeFieldOptions,
64
- typescriptType?: TTypeScriptType,
65
- ): AddFieldDescriptor<
66
- TFields,
67
- TOmitInitKey,
68
- TExtra,
69
- TPostDeserialized,
70
- TName,
71
- FixedLengthArrayBufferLikeFieldDefinition<
72
- TType,
73
- FixedLengthArrayBufferLikeFieldOptions
74
- >
75
- >;
76
-
77
- /**
78
- * Append a variable-length array to the `Struct`
79
- */
80
- <
81
- TName extends PropertyKey,
82
- TType extends ArrayBufferLikeFieldType,
83
- TOptions extends VariableLengthArrayBufferLikeFieldOptions<TFields>,
84
- TTypeScriptType = TType['TTypeScriptType'],
85
- >(
86
- name: TName,
87
- type: TType,
88
- options: TOptions,
89
- typescriptType?: TTypeScriptType,
90
- ): AddFieldDescriptor<
91
- TFields,
92
- TOmitInitKey,
93
- TExtra,
94
- TPostDeserialized,
95
- TName,
96
- VariableLengthArrayBufferLikeFieldDefinition<
97
- TType,
98
- TOptions
99
- >
100
- >;
101
- }
102
-
103
- /**
104
- * Similar to `ArrayBufferLikeFieldCreator`, but bind to a `ArrayBufferLikeFieldType`
105
- */
106
- interface ArrayBufferTypeFieldDefinitionCreator<
107
- TFields extends object,
108
- TOmitInitKey extends PropertyKey,
109
- TExtra extends object,
110
- TPostDeserialized,
111
- TType extends ArrayBufferLikeFieldType
112
- > {
113
- <
114
- TName extends PropertyKey,
115
- TTypeScriptType = TType['TTypeScriptType'],
116
- >(
117
- name: TName,
118
- options: FixedLengthArrayBufferLikeFieldOptions,
119
- typescriptType?: TTypeScriptType,
120
- ): AddFieldDescriptor<
121
- TFields,
122
- TOmitInitKey,
123
- TExtra,
124
- TPostDeserialized,
125
- TName,
126
- FixedLengthArrayBufferLikeFieldDefinition<
127
- TType,
128
- FixedLengthArrayBufferLikeFieldOptions
129
- >
130
- >;
131
-
132
- <
133
- TName extends PropertyKey,
134
- TLengthField extends LengthField<TFields>,
135
- TOptions extends VariableLengthArrayBufferLikeFieldOptions<TFields, TLengthField>,
136
- TTypeScriptType = TType['TTypeScriptType'],
137
- >(
138
- name: TName,
139
- options: TOptions,
140
- typescriptType?: TTypeScriptType,
141
- ): AddFieldDescriptor<
142
- TFields,
143
- TOmitInitKey,
144
- TExtra,
145
- TPostDeserialized,
146
- TName,
147
- VariableLengthArrayBufferLikeFieldDefinition<
148
- TType,
149
- TOptions
150
- >
151
- >;
152
- }
153
-
154
- export type StructPostDeserialized<TFields, TPostDeserialized> =
155
- (this: TFields, object: TFields) => TPostDeserialized;
156
-
157
- export type StructDeserializedResult<TFields extends object, TExtra extends object, TPostDeserialized> =
158
- TPostDeserialized extends undefined ? Overwrite<TExtra, TFields> : TPostDeserialized;
159
-
160
- export class Struct<
161
- TFields extends object = {},
162
- TOmitInitKey extends PropertyKey = never,
163
- TExtra extends object = {},
164
- TPostDeserialized = undefined,
165
- > implements StructLike<StructDeserializedResult<TFields, TExtra, TPostDeserialized>>{
166
- public readonly TFields!: TFields;
167
-
168
- public readonly TOmitInitKey!: TOmitInitKey;
169
-
170
- public readonly TExtra!: TExtra;
171
-
172
- public readonly TInit!: Evaluate<Omit<TFields, TOmitInitKey>>;
173
-
174
- public readonly TDeserializeResult!: StructDeserializedResult<TFields, TExtra, TPostDeserialized>;
175
-
176
- public readonly options: Readonly<StructOptions>;
177
-
178
- private _size = 0;
179
- /**
180
- * Gets the static size (exclude fields that can change size at runtime)
181
- */
182
- public get size() { return this._size; }
183
-
184
- private _fields: [name: PropertyKey, definition: StructFieldDefinition<any, any, any>][] = [];
185
-
186
- private _extra: PropertyDescriptorMap = {};
187
-
188
- private _postDeserialized?: StructPostDeserialized<any, any>;
189
-
190
- public constructor(options?: Partial<Readonly<StructOptions>>) {
191
- this.options = { ...StructDefaultOptions, ...options };
192
- }
193
-
194
- /**
195
- * Appends a `StructFieldDefinition` to the `Struct
196
- */
197
- public field<
198
- TName extends PropertyKey,
199
- TDefinition extends StructFieldDefinition<any, any, any>
200
- >(
201
- name: TName,
202
- definition: TDefinition,
203
- ): AddFieldDescriptor<
204
- TFields,
205
- TOmitInitKey,
206
- TExtra,
207
- TPostDeserialized,
208
- TName,
209
- TDefinition
210
- > {
211
- for (const field of this._fields) {
212
- if (field[0] === name) {
213
- throw new Error(`This struct already have a field with name '${name}'`);
214
- }
215
- }
216
-
217
- this._fields.push([name, definition]);
218
-
219
- const size = definition.getSize();
220
- this._size += size;
221
-
222
- // Force cast `this` to another type
223
- return this as any;
224
- }
225
-
226
- /**
227
- * Merges (flats) another `Struct`'s fields and extra fields into this one.
228
- */
229
- public fields<TOther extends Struct<any, any, any, any>>(
230
- other: TOther
231
- ): Struct<
232
- TFields & TOther['TFields'],
233
- TOmitInitKey | TOther['TOmitInitKey'],
234
- TExtra & TOther['TExtra'],
235
- TPostDeserialized
236
- > {
237
- for (const field of other._fields) {
238
- this._fields.push(field);
239
- }
240
- this._size += other._size;
241
- Object.assign(this._extra, other._extra);
242
- return this as any;
243
- }
244
-
245
- private number<
246
- TName extends PropertyKey,
247
- TType extends NumberFieldType = NumberFieldType,
248
- TTypeScriptType = TType['TTypeScriptType']
249
- >(
250
- name: TName,
251
- type: TType,
252
- _typescriptType?: TTypeScriptType,
253
- ) {
254
- return this.field(
255
- name,
256
- new NumberFieldDefinition(type, _typescriptType),
257
- );
258
- }
259
-
260
- /**
261
- * Appends an `int8` field to the `Struct`
262
- */
263
- public int8<
264
- TName extends PropertyKey,
265
- TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
266
- >(
267
- name: TName,
268
- _typescriptType?: TTypeScriptType,
269
- ) {
270
- return this.number(
271
- name,
272
- NumberFieldType.Int8,
273
- _typescriptType
274
- );
275
- }
276
-
277
- /**
278
- * Appends an `uint8` field to the `Struct`
279
- */
280
- public uint8<
281
- TName extends PropertyKey,
282
- TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
283
- >(
284
- name: TName,
285
- _typescriptType?: TTypeScriptType,
286
- ) {
287
- return this.number(
288
- name,
289
- NumberFieldType.Uint8,
290
- _typescriptType
291
- );
292
- }
293
-
294
- /**
295
- * Appends an `int16` field to the `Struct`
296
- */
297
- public int16<
298
- TName extends PropertyKey,
299
- TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
300
- >(
301
- name: TName,
302
- _typescriptType?: TTypeScriptType,
303
- ) {
304
- return this.number(
305
- name,
306
- NumberFieldType.Int16,
307
- _typescriptType
308
- );
309
- }
310
-
311
- /**
312
- * Appends an `uint16` field to the `Struct`
313
- */
314
- public uint16<
315
- TName extends PropertyKey,
316
- TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
317
- >(
318
- name: TName,
319
- _typescriptType?: TTypeScriptType,
320
- ) {
321
- return this.number(
322
- name,
323
- NumberFieldType.Uint16,
324
- _typescriptType
325
- );
326
- }
327
-
328
- /**
329
- * Appends an `int32` field to the `Struct`
330
- */
331
- public int32<
332
- TName extends PropertyKey,
333
- TTypeScriptType = (typeof NumberFieldType)['Int32']['TTypeScriptType']
334
- >(
335
- name: TName,
336
- _typescriptType?: TTypeScriptType,
337
- ) {
338
- return this.number(
339
- name,
340
- NumberFieldType.Int32,
341
- _typescriptType
342
- );
343
- }
344
-
345
- /**
346
- * Appends an `uint32` field to the `Struct`
347
- */
348
- public uint32<
349
- TName extends PropertyKey,
350
- TTypeScriptType = (typeof NumberFieldType)['Uint32']['TTypeScriptType']
351
- >(
352
- name: TName,
353
- typescriptType?: TTypeScriptType,
354
- ) {
355
- return this.number(
356
- name,
357
- NumberFieldType.Uint32,
358
- typescriptType
359
- );
360
- }
361
-
362
- /**
363
- * Appends an `int64` field to the `Struct`
364
- *
365
- * Requires native `BigInt` support
366
- */
367
- public int64<
368
- TName extends PropertyKey,
369
- TTypeScriptType = (typeof NumberFieldType)['Int64']['TTypeScriptType']
370
- >(
371
- name: TName,
372
- _typescriptType?: TTypeScriptType,
373
- ) {
374
- return this.number(
375
- name,
376
- NumberFieldType.Int64,
377
- _typescriptType
378
- );
379
- }
380
-
381
- /**
382
- * Appends an `uint64` field to the `Struct`
383
- *
384
- * Requires native `BigInt` support
385
- */
386
- public uint64<
387
- TName extends PropertyKey,
388
- TTypeScriptType = (typeof NumberFieldType)['Uint64']['TTypeScriptType']
389
- >(
390
- name: TName,
391
- _typescriptType?: TTypeScriptType,
392
- ) {
393
- return this.number(
394
- name,
395
- NumberFieldType.Uint64,
396
- _typescriptType
397
- );
398
- }
399
-
400
- private arrayBufferLike: ArrayBufferLikeFieldCreator<
401
- TFields,
402
- TOmitInitKey,
403
- TExtra,
404
- TPostDeserialized
405
- > = (
406
- name: PropertyKey,
407
- type: ArrayBufferLikeFieldType,
408
- options: FixedLengthArrayBufferLikeFieldOptions | VariableLengthArrayBufferLikeFieldOptions
409
- ): any => {
410
- if ('length' in options) {
411
- return this.field(
412
- name,
413
- new FixedLengthArrayBufferLikeFieldDefinition(type, options),
414
- );
415
- } else {
416
- return this.field(
417
- name,
418
- new VariableLengthArrayBufferLikeFieldDefinition(type, options),
419
- );
420
- }
421
- };
422
-
423
- public arrayBuffer: ArrayBufferTypeFieldDefinitionCreator<
424
- TFields,
425
- TOmitInitKey,
426
- TExtra,
427
- TPostDeserialized,
428
- ArrayBufferFieldType
429
- > = (
430
- name: PropertyKey,
431
- options: any
432
- ): any => {
433
- return this.arrayBufferLike(name, ArrayBufferFieldType.instance, options);
434
- };
435
-
436
- public uint8ClampedArray: ArrayBufferTypeFieldDefinitionCreator<
437
- TFields,
438
- TOmitInitKey,
439
- TExtra,
440
- TPostDeserialized,
441
- Uint8ClampedArrayFieldType
442
- > = (
443
- name: PropertyKey,
444
- options: any
445
- ): any => {
446
- return this.arrayBufferLike(name, Uint8ClampedArrayFieldType.instance, options);
447
- };
448
-
449
- public string: ArrayBufferTypeFieldDefinitionCreator<
450
- TFields,
451
- TOmitInitKey,
452
- TExtra,
453
- TPostDeserialized,
454
- StringFieldType
455
- > = (
456
- name: PropertyKey,
457
- options: any
458
- ): any => {
459
- return this.arrayBufferLike(name, StringFieldType.instance, options);
460
- };
461
-
462
- /**
463
- * Adds some extra fields into every Struct value.
464
- *
465
- * Extra fields will not affect serialize or deserialize process.
466
- *
467
- * Multiple calls to `extra` will merge all values together.
468
- *
469
- * @param value
470
- * An object containing anything you want to add to the result object. Accessors and methods are also allowed.
471
- */
472
- public extra<T extends Record<
473
- // This trick disallows any keys that are already in `TValue`
474
- Exclude<
475
- keyof T,
476
- Exclude<keyof T, keyof TFields>
477
- >,
478
- never
479
- >>(
480
- value: T & ThisType<Overwrite<Overwrite<TExtra, T>, TFields>>
481
- ): Struct<
482
- TFields,
483
- TOmitInitKey,
484
- Overwrite<TExtra, T>,
485
- TPostDeserialized
486
- > {
487
- Object.assign(this._extra, Object.getOwnPropertyDescriptors(value));
488
- return this as any;
489
- }
490
-
491
- /**
492
- * Registers (or replaces) a custom callback to be run after deserialized.
493
- *
494
- * A callback returning `never` (always throw an error)
495
- * will also change the return type of `deserialize` to `never`.
496
- */
497
- public postDeserialize(
498
- callback: StructPostDeserialized<TFields, never>
499
- ): Struct<TFields, TOmitInitKey, TExtra, never>;
500
- /**
501
- * Registers (or replaces) a custom callback to be run after deserialized.
502
- *
503
- * A callback returning `void` means it modify the result object in-place
504
- * (or doesn't modify it at all), so `deserialize` will still return the result object.
505
- */
506
- public postDeserialize(
507
- callback?: StructPostDeserialized<TFields, void>
508
- ): Struct<TFields, TOmitInitKey, TExtra, undefined>;
509
- /**
510
- * Registers (or replaces) a custom callback to be run after deserialized.
511
- *
512
- * A callback returning anything other than `undefined`
513
- * will `deserialize` to return that object instead.
514
- */
515
- public postDeserialize<TPostSerialize>(
516
- callback?: StructPostDeserialized<TFields, TPostSerialize>
517
- ): Struct<TFields, TOmitInitKey, TExtra, TPostSerialize>;
518
- public postDeserialize(
519
- callback?: StructPostDeserialized<TFields, any>
520
- ) {
521
- this._postDeserialized = callback;
522
- return this as any;
523
- }
524
-
525
- public async deserialize(
526
- context: StructDeserializationContext
527
- ): Promise<StructDeserializedResult<TFields, TExtra, TPostDeserialized>> {
528
- const value = new StructValue();
529
- Object.defineProperties(value.value, this._extra);
530
-
531
- for (const [name, definition] of this._fields) {
532
- const fieldValue = await definition.deserialize(this.options, context, value);
533
- value.set(name, fieldValue);
534
- }
535
-
536
- if (this._postDeserialized) {
537
- const object = value.value as TFields;
538
- const result = this._postDeserialized.call(object, object);
539
- if (result) {
540
- return result;
541
- }
542
- }
543
-
544
- return value.value as any;
545
- }
546
-
547
- public serialize(init: Evaluate<Omit<TFields, TOmitInitKey>>, context: StructSerializationContext): ArrayBuffer {
548
- const value = new StructValue();
549
-
550
- for (const [name, definition] of this._fields) {
551
- const fieldValue = definition.create(this.options, context, value, (init as any)[name]);
552
- value.set(name, fieldValue);
553
- }
554
-
555
- let structSize = 0;
556
- const fieldsInfo: { fieldValue: StructFieldValue, size: number; }[] = [];
557
-
558
- for (const [name] of this._fields) {
559
- const fieldValue = value.get(name);
560
- const size = fieldValue.getSize();
561
- fieldsInfo.push({ fieldValue, size });
562
- structSize += size;
563
- }
564
-
565
- const buffer = new ArrayBuffer(structSize);
566
- const dataView = new DataView(buffer);
567
- let offset = 0;
568
- for (const { fieldValue, size } of fieldsInfo) {
569
- fieldValue.serialize(dataView, offset, context);
570
- offset += size;
571
- }
572
-
573
- return buffer;
574
- }
575
- }
1
+ // cspell: ignore Syncbird
2
+
3
+ import type { StructAsyncDeserializeStream, StructDeserializeStream, StructFieldDefinition, StructFieldValue, StructOptions } from './basic/index.js';
4
+ import { StructDefaultOptions, StructValue } from './basic/index.js';
5
+ import { Syncbird } from "./syncbird.js";
6
+ import { BigIntFieldDefinition, BigIntFieldType, BufferFieldSubType, FixedLengthBufferLikeFieldDefinition, NumberFieldDefinition, NumberFieldType, StringBufferFieldSubType, Uint8ArrayBufferFieldSubType, VariableLengthBufferLikeFieldDefinition, type FixedLengthBufferLikeFieldOptions, type LengthField, type VariableLengthBufferLikeFieldOptions } from './types/index.js';
7
+ import type { Evaluate, Identity, Overwrite, ValueOrPromise } from "./utils.js";
8
+
9
+ export interface StructLike<TValue> {
10
+ deserialize(stream: StructDeserializeStream | StructAsyncDeserializeStream): Promise<TValue>;
11
+ }
12
+
13
+ /**
14
+ * Extract the value type of the specified `Struct`
15
+ */
16
+ export type StructValueType<T extends StructLike<any>> =
17
+ Awaited<ReturnType<T['deserialize']>>;
18
+
19
+ /**
20
+ * Create a new `Struct` type with `TDefinition` appended
21
+ */
22
+ type AddFieldDescriptor<
23
+ TFields extends object,
24
+ TOmitInitKey extends PropertyKey,
25
+ TExtra extends object,
26
+ TPostDeserialized,
27
+ TFieldName extends PropertyKey,
28
+ TDefinition extends StructFieldDefinition<any, any, any>
29
+ > =
30
+ Identity<Struct<
31
+ // Merge two types
32
+ // Evaluate immediately to optimize editor hover tooltip
33
+ Evaluate<TFields & Record<TFieldName, TDefinition['TValue']>>,
34
+ // Merge two `TOmitInitKey`s
35
+ TOmitInitKey | TDefinition['TOmitInitKey'],
36
+ TExtra,
37
+ TPostDeserialized
38
+ >>;
39
+
40
+ /**
41
+ * Overload methods to add an array buffer like field
42
+ */
43
+ interface ArrayBufferLikeFieldCreator<
44
+ TFields extends object,
45
+ TOmitInitKey extends PropertyKey,
46
+ TExtra extends object,
47
+ TPostDeserialized
48
+ > {
49
+ /**
50
+ * Append a fixed-length array buffer like field to the `Struct`
51
+ *
52
+ * @param name Name of the field
53
+ * @param type `Array.SubType.ArrayBuffer` or `Array.SubType.String`
54
+ * @param options Fixed-length array options
55
+ * @param typescriptType Type of the field in TypeScript.
56
+ * For example, if this field is a string, you can declare it as a string enum or literal union.
57
+ */
58
+ <
59
+ TName extends PropertyKey,
60
+ TType extends BufferFieldSubType<any, any>,
61
+ TTypeScriptType = TType['TTypeScriptType'],
62
+ >(
63
+ name: TName,
64
+ type: TType,
65
+ options: FixedLengthBufferLikeFieldOptions,
66
+ typescriptType?: TTypeScriptType,
67
+ ): AddFieldDescriptor<
68
+ TFields,
69
+ TOmitInitKey,
70
+ TExtra,
71
+ TPostDeserialized,
72
+ TName,
73
+ FixedLengthBufferLikeFieldDefinition<
74
+ TType,
75
+ FixedLengthBufferLikeFieldOptions
76
+ >
77
+ >;
78
+
79
+ /**
80
+ * Append a variable-length array buffer like field to the `Struct`
81
+ */
82
+ <
83
+ TName extends PropertyKey,
84
+ TType extends BufferFieldSubType<any, any>,
85
+ TOptions extends VariableLengthBufferLikeFieldOptions<TFields>,
86
+ TTypeScriptType = TType['TTypeScriptType'],
87
+ >(
88
+ name: TName,
89
+ type: TType,
90
+ options: TOptions,
91
+ typescriptType?: TTypeScriptType,
92
+ ): AddFieldDescriptor<
93
+ TFields,
94
+ TOmitInitKey,
95
+ TExtra,
96
+ TPostDeserialized,
97
+ TName,
98
+ VariableLengthBufferLikeFieldDefinition<
99
+ TType,
100
+ TOptions
101
+ >
102
+ >;
103
+ }
104
+
105
+ /**
106
+ * Similar to `ArrayBufferLikeFieldCreator`, but bind to `TType`
107
+ */
108
+ interface BoundArrayBufferLikeFieldDefinitionCreator<
109
+ TFields extends object,
110
+ TOmitInitKey extends PropertyKey,
111
+ TExtra extends object,
112
+ TPostDeserialized,
113
+ TType extends BufferFieldSubType<any, any>
114
+ > {
115
+ <
116
+ TName extends PropertyKey,
117
+ TTypeScriptType = TType['TTypeScriptType'],
118
+ >(
119
+ name: TName,
120
+ options: FixedLengthBufferLikeFieldOptions,
121
+ typescriptType?: TTypeScriptType,
122
+ ): AddFieldDescriptor<
123
+ TFields,
124
+ TOmitInitKey,
125
+ TExtra,
126
+ TPostDeserialized,
127
+ TName,
128
+ FixedLengthBufferLikeFieldDefinition<
129
+ TType,
130
+ FixedLengthBufferLikeFieldOptions
131
+ >
132
+ >;
133
+
134
+ <
135
+ TName extends PropertyKey,
136
+ TLengthField extends LengthField<TFields>,
137
+ TOptions extends VariableLengthBufferLikeFieldOptions<TFields, TLengthField>,
138
+ TTypeScriptType = TType['TTypeScriptType'],
139
+ >(
140
+ name: TName,
141
+ options: TOptions,
142
+ typescriptType?: TTypeScriptType,
143
+ ): AddFieldDescriptor<
144
+ TFields,
145
+ TOmitInitKey,
146
+ TExtra,
147
+ TPostDeserialized,
148
+ TName,
149
+ VariableLengthBufferLikeFieldDefinition<
150
+ TType,
151
+ TOptions
152
+ >
153
+ >;
154
+ }
155
+
156
+ export type StructPostDeserialized<TFields, TPostDeserialized> =
157
+ (this: TFields, object: TFields) => TPostDeserialized;
158
+
159
+ export type StructDeserializedResult<TFields extends object, TExtra extends object, TPostDeserialized> =
160
+ TPostDeserialized extends undefined ? Overwrite<TExtra, TFields> : TPostDeserialized;
161
+
162
+ export class Struct<
163
+ TFields extends object = {},
164
+ TOmitInitKey extends PropertyKey = never,
165
+ TExtra extends object = {},
166
+ TPostDeserialized = undefined,
167
+ > implements StructLike<StructDeserializedResult<TFields, TExtra, TPostDeserialized>>{
168
+ public readonly TFields!: TFields;
169
+
170
+ public readonly TOmitInitKey!: TOmitInitKey;
171
+
172
+ public readonly TExtra!: TExtra;
173
+
174
+ public readonly TInit!: Evaluate<Omit<TFields, TOmitInitKey>>;
175
+
176
+ public readonly TDeserializeResult!: StructDeserializedResult<TFields, TExtra, TPostDeserialized>;
177
+
178
+ public readonly options: Readonly<StructOptions>;
179
+
180
+ private _size = 0;
181
+ /**
182
+ * Gets the static size (exclude fields that can change size at runtime)
183
+ */
184
+ public get size() { return this._size; }
185
+
186
+ private _fields: [name: PropertyKey, definition: StructFieldDefinition<any, any, any>][] = [];
187
+
188
+ private _extra: PropertyDescriptorMap = {};
189
+
190
+ private _postDeserialized?: StructPostDeserialized<any, any> | undefined;
191
+
192
+ public constructor(options?: Partial<Readonly<StructOptions>>) {
193
+ this.options = { ...StructDefaultOptions, ...options };
194
+ }
195
+
196
+ /**
197
+ * Appends a `StructFieldDefinition` to the `Struct
198
+ */
199
+ public field<
200
+ TName extends PropertyKey,
201
+ TDefinition extends StructFieldDefinition<any, any, any>
202
+ >(
203
+ name: TName,
204
+ definition: TDefinition,
205
+ ): AddFieldDescriptor<
206
+ TFields,
207
+ TOmitInitKey,
208
+ TExtra,
209
+ TPostDeserialized,
210
+ TName,
211
+ TDefinition
212
+ > {
213
+ for (const field of this._fields) {
214
+ if (field[0] === name) {
215
+ throw new Error(`This struct already have a field with name '${String(name)}'`);
216
+ }
217
+ }
218
+
219
+ this._fields.push([name, definition]);
220
+
221
+ const size = definition.getSize();
222
+ this._size += size;
223
+
224
+ // Force cast `this` to another type
225
+ return this as any;
226
+ }
227
+
228
+ /**
229
+ * Merges (flats) another `Struct`'s fields and extra fields into this one.
230
+ */
231
+ public fields<TOther extends Struct<any, any, any, any>>(
232
+ other: TOther
233
+ ): Struct<
234
+ TFields & TOther['TFields'],
235
+ TOmitInitKey | TOther['TOmitInitKey'],
236
+ TExtra & TOther['TExtra'],
237
+ TPostDeserialized
238
+ > {
239
+ for (const field of other._fields) {
240
+ this._fields.push(field);
241
+ }
242
+ this._size += other._size;
243
+ Object.assign(this._extra, other._extra);
244
+ return this as any;
245
+ }
246
+
247
+ private number<
248
+ TName extends PropertyKey,
249
+ TType extends NumberFieldType = NumberFieldType,
250
+ TTypeScriptType = TType['TTypeScriptType']
251
+ >(
252
+ name: TName,
253
+ type: TType,
254
+ _typescriptType?: TTypeScriptType,
255
+ ) {
256
+ return this.field(
257
+ name,
258
+ new NumberFieldDefinition(type, _typescriptType),
259
+ );
260
+ }
261
+
262
+ /**
263
+ * Appends an `int8` field to the `Struct`
264
+ */
265
+ public int8<
266
+ TName extends PropertyKey,
267
+ TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
268
+ >(
269
+ name: TName,
270
+ _typescriptType?: TTypeScriptType,
271
+ ) {
272
+ return this.number(
273
+ name,
274
+ NumberFieldType.Int8,
275
+ _typescriptType
276
+ );
277
+ }
278
+
279
+ /**
280
+ * Appends an `uint8` field to the `Struct`
281
+ */
282
+ public uint8<
283
+ TName extends PropertyKey,
284
+ TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
285
+ >(
286
+ name: TName,
287
+ _typescriptType?: TTypeScriptType,
288
+ ) {
289
+ return this.number(
290
+ name,
291
+ NumberFieldType.Uint8,
292
+ _typescriptType
293
+ );
294
+ }
295
+
296
+ /**
297
+ * Appends an `int16` field to the `Struct`
298
+ */
299
+ public int16<
300
+ TName extends PropertyKey,
301
+ TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
302
+ >(
303
+ name: TName,
304
+ _typescriptType?: TTypeScriptType,
305
+ ) {
306
+ return this.number(
307
+ name,
308
+ NumberFieldType.Int16,
309
+ _typescriptType
310
+ );
311
+ }
312
+
313
+ /**
314
+ * Appends an `uint16` field to the `Struct`
315
+ */
316
+ public uint16<
317
+ TName extends PropertyKey,
318
+ TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
319
+ >(
320
+ name: TName,
321
+ _typescriptType?: TTypeScriptType,
322
+ ) {
323
+ return this.number(
324
+ name,
325
+ NumberFieldType.Uint16,
326
+ _typescriptType
327
+ );
328
+ }
329
+
330
+ /**
331
+ * Appends an `int32` field to the `Struct`
332
+ */
333
+ public int32<
334
+ TName extends PropertyKey,
335
+ TTypeScriptType = (typeof NumberFieldType)['Int32']['TTypeScriptType']
336
+ >(
337
+ name: TName,
338
+ _typescriptType?: TTypeScriptType,
339
+ ) {
340
+ return this.number(
341
+ name,
342
+ NumberFieldType.Int32,
343
+ _typescriptType
344
+ );
345
+ }
346
+
347
+ /**
348
+ * Appends an `uint32` field to the `Struct`
349
+ */
350
+ public uint32<
351
+ TName extends PropertyKey,
352
+ TTypeScriptType = (typeof NumberFieldType)['Uint32']['TTypeScriptType']
353
+ >(
354
+ name: TName,
355
+ typescriptType?: TTypeScriptType,
356
+ ) {
357
+ return this.number(
358
+ name,
359
+ NumberFieldType.Uint32,
360
+ typescriptType
361
+ );
362
+ }
363
+
364
+ private bigint<
365
+ TName extends PropertyKey,
366
+ TType extends BigIntFieldType = BigIntFieldType,
367
+ TTypeScriptType = TType['TTypeScriptType']
368
+ >(
369
+ name: TName,
370
+ type: TType,
371
+ _typescriptType?: TTypeScriptType,
372
+ ) {
373
+ return this.field(
374
+ name,
375
+ new BigIntFieldDefinition(type, _typescriptType),
376
+ );
377
+ }
378
+
379
+ /**
380
+ * Appends an `int64` field to the `Struct`
381
+ *
382
+ * Requires native `BigInt` support
383
+ */
384
+ public int64<
385
+ TName extends PropertyKey,
386
+ TTypeScriptType = BigIntFieldType['TTypeScriptType']
387
+ >(
388
+ name: TName,
389
+ _typescriptType?: TTypeScriptType,
390
+ ) {
391
+ return this.bigint(
392
+ name,
393
+ BigIntFieldType.Int64,
394
+ _typescriptType
395
+ );
396
+ }
397
+
398
+ /**
399
+ * Appends an `uint64` field to the `Struct`
400
+ *
401
+ * Requires native `BigInt` support
402
+ */
403
+ public uint64<
404
+ TName extends PropertyKey,
405
+ TTypeScriptType = BigIntFieldType['TTypeScriptType']
406
+ >(
407
+ name: TName,
408
+ _typescriptType?: TTypeScriptType,
409
+ ) {
410
+ return this.bigint(
411
+ name,
412
+ BigIntFieldType.Uint64,
413
+ _typescriptType
414
+ );
415
+ }
416
+
417
+ private arrayBufferLike: ArrayBufferLikeFieldCreator<
418
+ TFields,
419
+ TOmitInitKey,
420
+ TExtra,
421
+ TPostDeserialized
422
+ > = (
423
+ name: PropertyKey,
424
+ type: BufferFieldSubType,
425
+ options: FixedLengthBufferLikeFieldOptions | VariableLengthBufferLikeFieldOptions
426
+ ): any => {
427
+ if ('length' in options) {
428
+ return this.field(
429
+ name,
430
+ new FixedLengthBufferLikeFieldDefinition(type, options),
431
+ );
432
+ } else {
433
+ return this.field(
434
+ name,
435
+ new VariableLengthBufferLikeFieldDefinition(type, options),
436
+ );
437
+ }
438
+ };
439
+
440
+ public uint8Array: BoundArrayBufferLikeFieldDefinitionCreator<
441
+ TFields,
442
+ TOmitInitKey,
443
+ TExtra,
444
+ TPostDeserialized,
445
+ Uint8ArrayBufferFieldSubType
446
+ > = (
447
+ name: PropertyKey,
448
+ options: any
449
+ ): any => {
450
+ return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options);
451
+ };
452
+
453
+ public string: BoundArrayBufferLikeFieldDefinitionCreator<
454
+ TFields,
455
+ TOmitInitKey,
456
+ TExtra,
457
+ TPostDeserialized,
458
+ StringBufferFieldSubType
459
+ > = (
460
+ name: PropertyKey,
461
+ options: any
462
+ ): any => {
463
+ return this.arrayBufferLike(name, StringBufferFieldSubType.Instance, options);
464
+ };
465
+
466
+ /**
467
+ * Adds some extra properties into every `Struct` value.
468
+ *
469
+ * Extra properties will not affect serialize or deserialize process.
470
+ *
471
+ * Multiple calls to `extra` will merge all properties together.
472
+ *
473
+ * @param value
474
+ * An object containing properties to be added to the result value. Accessors and methods are also allowed.
475
+ */
476
+ public extra<T extends Record<
477
+ // This trick disallows any keys that are already in `TValue`
478
+ Exclude<
479
+ keyof T,
480
+ Exclude<keyof T, keyof TFields>
481
+ >,
482
+ never
483
+ >>(
484
+ value: T & ThisType<Overwrite<Overwrite<TExtra, T>, TFields>>
485
+ ): Struct<
486
+ TFields,
487
+ TOmitInitKey,
488
+ Overwrite<TExtra, T>,
489
+ TPostDeserialized
490
+ > {
491
+ Object.assign(this._extra, Object.getOwnPropertyDescriptors(value));
492
+ return this as any;
493
+ }
494
+
495
+ /**
496
+ * Registers (or replaces) a custom callback to be run after deserialized.
497
+ *
498
+ * A callback returning `never` (always throw an error)
499
+ * will also change the return type of `deserialize` to `never`.
500
+ */
501
+ public postDeserialize(
502
+ callback: StructPostDeserialized<TFields, never>
503
+ ): Struct<TFields, TOmitInitKey, TExtra, never>;
504
+ /**
505
+ * Registers (or replaces) a custom callback to be run after deserialized.
506
+ *
507
+ * A callback returning `void` means it modify the result object in-place
508
+ * (or doesn't modify it at all), so `deserialize` will still return the result object.
509
+ */
510
+ public postDeserialize(
511
+ callback?: StructPostDeserialized<TFields, void>
512
+ ): Struct<TFields, TOmitInitKey, TExtra, undefined>;
513
+ /**
514
+ * Registers (or replaces) a custom callback to be run after deserialized.
515
+ *
516
+ * A callback returning anything other than `undefined`
517
+ * will `deserialize` to return that object instead.
518
+ */
519
+ public postDeserialize<TPostSerialize>(
520
+ callback?: StructPostDeserialized<TFields, TPostSerialize>
521
+ ): Struct<TFields, TOmitInitKey, TExtra, TPostSerialize>;
522
+ public postDeserialize(
523
+ callback?: StructPostDeserialized<TFields, any>
524
+ ) {
525
+ this._postDeserialized = callback;
526
+ return this as any;
527
+ }
528
+
529
+ /**
530
+ * Deserialize a struct value from `stream`.
531
+ */
532
+ public deserialize(
533
+ stream: StructDeserializeStream,
534
+ ): StructDeserializedResult<TFields, TExtra, TPostDeserialized>;
535
+ public deserialize(
536
+ stream: StructAsyncDeserializeStream,
537
+ ): Promise<StructDeserializedResult<TFields, TExtra, TPostDeserialized>>;
538
+ public deserialize(
539
+ stream: StructDeserializeStream | StructAsyncDeserializeStream,
540
+ ): ValueOrPromise<StructDeserializedResult<TFields, TExtra, TPostDeserialized>> {
541
+ const value = new StructValue();
542
+ Object.defineProperties(value.value, this._extra);
543
+
544
+ return Syncbird
545
+ .each(this._fields, ([name, definition]) => {
546
+ return Syncbird.resolve(
547
+ definition.deserialize(this.options, stream as any, value)
548
+ ).then(fieldValue => {
549
+ value.set(name, fieldValue);
550
+ });
551
+ })
552
+ .then(() => {
553
+ const object = value.value;
554
+
555
+ // Run `postDeserialized`
556
+ if (this._postDeserialized) {
557
+ const override = this._postDeserialized.call(object, object);
558
+ // If it returns a new value, use that as result
559
+ // Otherwise it only inspects/mutates the object in place.
560
+ if (override) {
561
+ return override;
562
+ }
563
+ }
564
+
565
+ return object;
566
+ })
567
+ .valueOrPromise();
568
+ }
569
+
570
+ public serialize(init: Evaluate<Omit<TFields, TOmitInitKey>>): Uint8Array;
571
+ public serialize(init: Evaluate<Omit<TFields, TOmitInitKey>>, output: Uint8Array): number;
572
+ public serialize(init: Evaluate<Omit<TFields, TOmitInitKey>>, output?: Uint8Array): Uint8Array | number {
573
+ const value = new StructValue();
574
+
575
+ for (const [name, definition] of this._fields) {
576
+ const fieldValue = definition.create(this.options, value, (init as any)[name]);
577
+ value.set(name, fieldValue);
578
+ }
579
+
580
+ let structSize = 0;
581
+ const fieldsInfo: { fieldValue: StructFieldValue, size: number; }[] = [];
582
+
583
+ for (const [name] of this._fields) {
584
+ const fieldValue = value.get(name);
585
+ const size = fieldValue.getSize();
586
+ fieldsInfo.push({ fieldValue, size });
587
+ structSize += size;
588
+ }
589
+
590
+ let outputType = 'number';
591
+ if (!output) {
592
+ output = new Uint8Array(structSize);
593
+ outputType = 'Uint8Array';
594
+ }
595
+
596
+ const dataView = new DataView(output.buffer, output.byteOffset, output.byteLength);
597
+ let offset = 0;
598
+ for (const { fieldValue, size } of fieldsInfo) {
599
+ fieldValue.serialize(dataView, offset);
600
+ offset += size;
601
+ }
602
+
603
+ if (outputType === 'number') {
604
+ return structSize;
605
+ } else {
606
+ return output;
607
+ }
608
+ }
609
+ }