@yume-chan/struct 0.0.17 → 0.0.18

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