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