@yume-chan/struct 0.0.20 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +12 -0
- package/CHANGELOG.md +11 -1
- package/esm/basic/definition.d.ts.map +1 -1
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.d.ts.map +1 -1
- package/esm/basic/field-value.js.map +1 -1
- package/esm/basic/struct-value.d.ts.map +1 -1
- package/esm/basic/struct-value.js.map +1 -1
- package/esm/struct.d.ts +0 -3
- package/esm/struct.d.ts.map +1 -1
- package/esm/struct.js +13 -13
- package/esm/struct.js.map +1 -1
- package/esm/sync-promise.js.map +1 -1
- package/esm/types/bigint.d.ts.map +1 -1
- package/esm/types/bigint.js +1 -2
- package/esm/types/bigint.js.map +1 -1
- package/esm/types/buffer/base.d.ts.map +1 -1
- package/esm/types/buffer/base.js +2 -4
- package/esm/types/buffer/base.js.map +1 -1
- package/esm/types/buffer/fixed-length.d.ts.map +1 -1
- package/esm/types/buffer/fixed-length.js.map +1 -1
- package/esm/types/buffer/variable-length.d.ts.map +1 -1
- package/esm/types/buffer/variable-length.js.map +1 -1
- package/esm/types/number.d.ts.map +1 -1
- package/esm/types/number.js +1 -2
- package/esm/types/number.js.map +1 -1
- package/package.json +9 -9
- package/src/basic/definition.ts +12 -12
- package/src/basic/field-value.ts +11 -11
- package/src/basic/stream.ts +1 -1
- package/src/basic/struct-value.ts +4 -4
- package/src/struct.ts +101 -101
- package/src/sync-promise.ts +14 -14
- package/src/types/bigint.ts +27 -35
- package/src/types/buffer/base.ts +32 -32
- package/src/types/buffer/fixed-length.ts +4 -3
- package/src/types/buffer/variable-length.ts +22 -20
- package/src/types/number.ts +17 -18
- package/tsconfig.build.tsbuildinfo +1 -1
package/src/basic/field-value.ts
CHANGED
|
@@ -13,18 +13,18 @@ export abstract class StructFieldValue<
|
|
|
13
13
|
any,
|
|
14
14
|
any,
|
|
15
15
|
any
|
|
16
|
-
> = StructFieldDefinition<any, any, any
|
|
16
|
+
> = StructFieldDefinition<any, any, any>,
|
|
17
17
|
> {
|
|
18
18
|
/** Gets the definition associated with this runtime value */
|
|
19
|
-
|
|
19
|
+
readonly definition: TDefinition;
|
|
20
20
|
|
|
21
21
|
/** Gets the options of the associated `Struct` */
|
|
22
|
-
|
|
22
|
+
readonly options: Readonly<StructOptions>;
|
|
23
23
|
|
|
24
24
|
/** Gets the associated `Struct` instance */
|
|
25
|
-
|
|
25
|
+
readonly struct: StructValue;
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
get hasCustomAccessors(): boolean {
|
|
28
28
|
return (
|
|
29
29
|
this.get !== StructFieldValue.prototype.get ||
|
|
30
30
|
this.set !== StructFieldValue.prototype.set
|
|
@@ -33,11 +33,11 @@ export abstract class StructFieldValue<
|
|
|
33
33
|
|
|
34
34
|
protected value: TDefinition["TValue"];
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
constructor(
|
|
37
37
|
definition: TDefinition,
|
|
38
38
|
options: Readonly<StructOptions>,
|
|
39
39
|
struct: StructValue,
|
|
40
|
-
value: TDefinition["TValue"]
|
|
40
|
+
value: TDefinition["TValue"],
|
|
41
41
|
) {
|
|
42
42
|
this.definition = definition;
|
|
43
43
|
this.options = options;
|
|
@@ -50,26 +50,26 @@ export abstract class StructFieldValue<
|
|
|
50
50
|
*
|
|
51
51
|
* When overridden in derived classes, can have custom logic to calculate the actual size.
|
|
52
52
|
*/
|
|
53
|
-
|
|
53
|
+
getSize(): number {
|
|
54
54
|
return this.definition.getSize();
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* When implemented in derived classes, reads current field's value.
|
|
59
59
|
*/
|
|
60
|
-
|
|
60
|
+
get(): TDefinition["TValue"] {
|
|
61
61
|
return this.value;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
65
|
* When implemented in derived classes, updates current field's value.
|
|
66
66
|
*/
|
|
67
|
-
|
|
67
|
+
set(value: TDefinition["TValue"]): void {
|
|
68
68
|
this.value = value;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
72
|
* When implemented in derived classes, serializes this field into `dataView` at `offset`
|
|
73
73
|
*/
|
|
74
|
-
|
|
74
|
+
abstract serialize(dataView: DataView, offset: number): void;
|
|
75
75
|
}
|
package/src/basic/stream.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { ValueOrPromise } from "../utils.js";
|
|
|
3
3
|
// TODO: allow over reading (returning a `Uint8Array`, an `offset` and a `length`) to avoid copying
|
|
4
4
|
|
|
5
5
|
export class ExactReadableEndedError extends Error {
|
|
6
|
-
|
|
6
|
+
constructor() {
|
|
7
7
|
super("ExactReadable ended");
|
|
8
8
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
9
9
|
}
|
|
@@ -14,9 +14,9 @@ export class StructValue {
|
|
|
14
14
|
/**
|
|
15
15
|
* Gets the result struct value object
|
|
16
16
|
*/
|
|
17
|
-
|
|
17
|
+
readonly value: Record<PropertyKey, unknown>;
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
constructor(prototype: object) {
|
|
20
20
|
// PERF: `Object.create(extra)` is 50% faster
|
|
21
21
|
// than `Object.defineProperties(this.value, extra)`
|
|
22
22
|
this.value = Object.create(prototype) as Record<PropertyKey, unknown>;
|
|
@@ -35,7 +35,7 @@ export class StructValue {
|
|
|
35
35
|
* @param name The field name
|
|
36
36
|
* @param fieldValue The associated `StructFieldValue`
|
|
37
37
|
*/
|
|
38
|
-
|
|
38
|
+
set(name: PropertyKey, fieldValue: StructFieldValue): void {
|
|
39
39
|
this.fieldValues[name] = fieldValue;
|
|
40
40
|
|
|
41
41
|
// PERF: `Object.defineProperty` is slow
|
|
@@ -61,7 +61,7 @@ export class StructValue {
|
|
|
61
61
|
*
|
|
62
62
|
* @param name The field name
|
|
63
63
|
*/
|
|
64
|
-
|
|
64
|
+
get(name: PropertyKey): StructFieldValue {
|
|
65
65
|
return this.fieldValues[name]!;
|
|
66
66
|
}
|
|
67
67
|
}
|
package/src/struct.ts
CHANGED
|
@@ -50,7 +50,7 @@ type AddFieldDescriptor<
|
|
|
50
50
|
TExtra extends object,
|
|
51
51
|
TPostDeserialized,
|
|
52
52
|
TFieldName extends PropertyKey,
|
|
53
|
-
TDefinition extends StructFieldDefinition<any, any, any
|
|
53
|
+
TDefinition extends StructFieldDefinition<any, any, any>,
|
|
54
54
|
> = Identity<
|
|
55
55
|
Struct<
|
|
56
56
|
// Merge two types
|
|
@@ -70,7 +70,7 @@ interface ArrayBufferLikeFieldCreator<
|
|
|
70
70
|
TFields extends object,
|
|
71
71
|
TOmitInitKey extends PropertyKey,
|
|
72
72
|
TExtra extends object,
|
|
73
|
-
TPostDeserialized
|
|
73
|
+
TPostDeserialized,
|
|
74
74
|
> {
|
|
75
75
|
/**
|
|
76
76
|
* Append a fixed-length array buffer like field to the `Struct`
|
|
@@ -84,12 +84,12 @@ interface ArrayBufferLikeFieldCreator<
|
|
|
84
84
|
<
|
|
85
85
|
TName extends PropertyKey,
|
|
86
86
|
TType extends BufferFieldSubType<any, any>,
|
|
87
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
87
|
+
TTypeScriptType = TType["TTypeScriptType"],
|
|
88
88
|
>(
|
|
89
89
|
name: TName,
|
|
90
90
|
type: TType,
|
|
91
91
|
options: FixedLengthBufferLikeFieldOptions,
|
|
92
|
-
typeScriptType?: TTypeScriptType
|
|
92
|
+
typeScriptType?: TTypeScriptType,
|
|
93
93
|
): AddFieldDescriptor<
|
|
94
94
|
TFields,
|
|
95
95
|
TOmitInitKey,
|
|
@@ -109,12 +109,12 @@ interface ArrayBufferLikeFieldCreator<
|
|
|
109
109
|
TName extends PropertyKey,
|
|
110
110
|
TType extends BufferFieldSubType<any, any>,
|
|
111
111
|
TOptions extends VariableLengthBufferLikeFieldOptions<TFields>,
|
|
112
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
112
|
+
TTypeScriptType = TType["TTypeScriptType"],
|
|
113
113
|
>(
|
|
114
114
|
name: TName,
|
|
115
115
|
type: TType,
|
|
116
116
|
options: TOptions,
|
|
117
|
-
typeScriptType?: TTypeScriptType
|
|
117
|
+
typeScriptType?: TTypeScriptType,
|
|
118
118
|
): AddFieldDescriptor<
|
|
119
119
|
TFields,
|
|
120
120
|
TOmitInitKey,
|
|
@@ -133,12 +133,12 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
|
|
133
133
|
TOmitInitKey extends PropertyKey,
|
|
134
134
|
TExtra extends object,
|
|
135
135
|
TPostDeserialized,
|
|
136
|
-
TType extends BufferFieldSubType<any, any
|
|
136
|
+
TType extends BufferFieldSubType<any, any>,
|
|
137
137
|
> {
|
|
138
138
|
<TName extends PropertyKey, TTypeScriptType = TType["TTypeScriptType"]>(
|
|
139
139
|
name: TName,
|
|
140
140
|
options: FixedLengthBufferLikeFieldOptions,
|
|
141
|
-
typeScriptType?: TTypeScriptType
|
|
141
|
+
typeScriptType?: TTypeScriptType,
|
|
142
142
|
): AddFieldDescriptor<
|
|
143
143
|
TFields,
|
|
144
144
|
TOmitInitKey,
|
|
@@ -159,11 +159,11 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
|
|
159
159
|
TFields,
|
|
160
160
|
TLengthField
|
|
161
161
|
>,
|
|
162
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
162
|
+
TTypeScriptType = TType["TTypeScriptType"],
|
|
163
163
|
>(
|
|
164
164
|
name: TName,
|
|
165
165
|
options: TOptions,
|
|
166
|
-
typeScriptType?: TTypeScriptType
|
|
166
|
+
typeScriptType?: TTypeScriptType,
|
|
167
167
|
): AddFieldDescriptor<
|
|
168
168
|
TFields,
|
|
169
169
|
TOmitInitKey,
|
|
@@ -180,34 +180,34 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
|
|
180
180
|
|
|
181
181
|
export type StructPostDeserialized<TFields, TPostDeserialized> = (
|
|
182
182
|
this: TFields,
|
|
183
|
-
object: TFields
|
|
183
|
+
object: TFields,
|
|
184
184
|
) => TPostDeserialized;
|
|
185
185
|
|
|
186
186
|
export type StructDeserializedResult<
|
|
187
187
|
TFields extends object,
|
|
188
188
|
TExtra extends object,
|
|
189
|
-
TPostDeserialized
|
|
189
|
+
TPostDeserialized,
|
|
190
190
|
> = TPostDeserialized extends undefined
|
|
191
191
|
? Overwrite<TExtra, TFields>
|
|
192
192
|
: TPostDeserialized;
|
|
193
193
|
|
|
194
194
|
export class StructDeserializeError extends Error {
|
|
195
|
-
|
|
195
|
+
constructor(message: string) {
|
|
196
196
|
super(message);
|
|
197
197
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
200
|
|
|
201
201
|
export class StructNotEnoughDataError extends StructDeserializeError {
|
|
202
|
-
|
|
202
|
+
constructor() {
|
|
203
203
|
super(
|
|
204
|
-
"The underlying readable was ended before the struct was fully deserialized"
|
|
204
|
+
"The underlying readable was ended before the struct was fully deserialized",
|
|
205
205
|
);
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
export class StructEmptyError extends StructDeserializeError {
|
|
210
|
-
|
|
210
|
+
constructor() {
|
|
211
211
|
super("The underlying readable doesn't contain any more struct");
|
|
212
212
|
}
|
|
213
213
|
}
|
|
@@ -216,43 +216,43 @@ export class Struct<
|
|
|
216
216
|
TFields extends object = Record<never, never>,
|
|
217
217
|
TOmitInitKey extends PropertyKey = never,
|
|
218
218
|
TExtra extends object = Record<never, never>,
|
|
219
|
-
TPostDeserialized = undefined
|
|
219
|
+
TPostDeserialized = undefined,
|
|
220
220
|
> implements
|
|
221
221
|
StructLike<
|
|
222
222
|
StructDeserializedResult<TFields, TExtra, TPostDeserialized>
|
|
223
223
|
>
|
|
224
224
|
{
|
|
225
|
-
|
|
225
|
+
readonly TFields!: TFields;
|
|
226
226
|
|
|
227
|
-
|
|
227
|
+
readonly TOmitInitKey!: TOmitInitKey;
|
|
228
228
|
|
|
229
|
-
|
|
229
|
+
readonly TExtra!: TExtra;
|
|
230
230
|
|
|
231
|
-
|
|
231
|
+
readonly TInit!: Evaluate<Omit<TFields, TOmitInitKey>>;
|
|
232
232
|
|
|
233
|
-
|
|
233
|
+
readonly TDeserializeResult!: StructDeserializedResult<
|
|
234
234
|
TFields,
|
|
235
235
|
TExtra,
|
|
236
236
|
TPostDeserialized
|
|
237
237
|
>;
|
|
238
238
|
|
|
239
|
-
|
|
239
|
+
readonly options: Readonly<StructOptions>;
|
|
240
240
|
|
|
241
241
|
#size = 0;
|
|
242
242
|
/**
|
|
243
243
|
* Gets the static size (exclude fields that can change size at runtime)
|
|
244
244
|
*/
|
|
245
|
-
|
|
245
|
+
get size() {
|
|
246
246
|
return this.#size;
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
#fields: [
|
|
250
250
|
name: PropertyKey,
|
|
251
|
-
definition: StructFieldDefinition<any, any, any
|
|
251
|
+
definition: StructFieldDefinition<any, any, any>,
|
|
252
252
|
][] = [];
|
|
253
|
-
|
|
253
|
+
get fields(): readonly [
|
|
254
254
|
name: PropertyKey,
|
|
255
|
-
definition: StructFieldDefinition<any, any, any
|
|
255
|
+
definition: StructFieldDefinition<any, any, any>,
|
|
256
256
|
][] {
|
|
257
257
|
return this.#fields;
|
|
258
258
|
}
|
|
@@ -261,19 +261,19 @@ export class Struct<
|
|
|
261
261
|
|
|
262
262
|
#postDeserialized?: StructPostDeserialized<any, any> | undefined;
|
|
263
263
|
|
|
264
|
-
|
|
264
|
+
constructor(options?: Partial<Readonly<StructOptions>>) {
|
|
265
265
|
this.options = { ...StructDefaultOptions, ...options };
|
|
266
266
|
}
|
|
267
267
|
|
|
268
268
|
/**
|
|
269
269
|
* Appends a `StructFieldDefinition` to the `Struct
|
|
270
270
|
*/
|
|
271
|
-
|
|
271
|
+
field<
|
|
272
272
|
TName extends PropertyKey,
|
|
273
|
-
TDefinition extends StructFieldDefinition<any, any, any
|
|
273
|
+
TDefinition extends StructFieldDefinition<any, any, any>,
|
|
274
274
|
>(
|
|
275
275
|
name: TName,
|
|
276
|
-
definition: TDefinition
|
|
276
|
+
definition: TDefinition,
|
|
277
277
|
): AddFieldDescriptor<
|
|
278
278
|
TFields,
|
|
279
279
|
TOmitInitKey,
|
|
@@ -287,7 +287,7 @@ export class Struct<
|
|
|
287
287
|
// Convert Symbol to string
|
|
288
288
|
const nameString = String(name);
|
|
289
289
|
throw new Error(
|
|
290
|
-
`This struct already have a field with name '${nameString}'
|
|
290
|
+
`This struct already have a field with name '${nameString}'`,
|
|
291
291
|
);
|
|
292
292
|
}
|
|
293
293
|
}
|
|
@@ -304,8 +304,8 @@ export class Struct<
|
|
|
304
304
|
/**
|
|
305
305
|
* Merges (flats) another `Struct`'s fields and extra fields into this one.
|
|
306
306
|
*/
|
|
307
|
-
|
|
308
|
-
other: TOther
|
|
307
|
+
concat<TOther extends Struct<any, any, any, any>>(
|
|
308
|
+
other: TOther,
|
|
309
309
|
): Struct<
|
|
310
310
|
TFields & TOther["TFields"],
|
|
311
311
|
TOmitInitKey | TOther["TOmitInitKey"],
|
|
@@ -318,90 +318,90 @@ export class Struct<
|
|
|
318
318
|
this.#size += other.#size;
|
|
319
319
|
Object.defineProperties(
|
|
320
320
|
this.#extra,
|
|
321
|
-
Object.getOwnPropertyDescriptors(other.#extra)
|
|
321
|
+
Object.getOwnPropertyDescriptors(other.#extra),
|
|
322
322
|
);
|
|
323
323
|
return this as any;
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
|
|
326
|
+
#number<
|
|
327
327
|
TName extends PropertyKey,
|
|
328
328
|
TType extends NumberFieldType = NumberFieldType,
|
|
329
|
-
TTypeScriptType = number
|
|
329
|
+
TTypeScriptType = number,
|
|
330
330
|
>(name: TName, type: TType, typeScriptType?: TTypeScriptType) {
|
|
331
331
|
return this.field(
|
|
332
332
|
name,
|
|
333
|
-
new NumberFieldDefinition(type, typeScriptType)
|
|
333
|
+
new NumberFieldDefinition(type, typeScriptType),
|
|
334
334
|
);
|
|
335
335
|
}
|
|
336
336
|
|
|
337
337
|
/**
|
|
338
338
|
* Appends an `int8` field to the `Struct`
|
|
339
339
|
*/
|
|
340
|
-
|
|
340
|
+
int8<TName extends PropertyKey, TTypeScriptType = number>(
|
|
341
341
|
name: TName,
|
|
342
|
-
typeScriptType?: TTypeScriptType
|
|
342
|
+
typeScriptType?: TTypeScriptType,
|
|
343
343
|
) {
|
|
344
|
-
return this
|
|
344
|
+
return this.#number(name, NumberFieldType.Int8, typeScriptType);
|
|
345
345
|
}
|
|
346
346
|
|
|
347
347
|
/**
|
|
348
348
|
* Appends an `uint8` field to the `Struct`
|
|
349
349
|
*/
|
|
350
|
-
|
|
350
|
+
uint8<TName extends PropertyKey, TTypeScriptType = number>(
|
|
351
351
|
name: TName,
|
|
352
|
-
typeScriptType?: TTypeScriptType
|
|
352
|
+
typeScriptType?: TTypeScriptType,
|
|
353
353
|
) {
|
|
354
|
-
return this
|
|
354
|
+
return this.#number(name, NumberFieldType.Uint8, typeScriptType);
|
|
355
355
|
}
|
|
356
356
|
|
|
357
357
|
/**
|
|
358
358
|
* Appends an `int16` field to the `Struct`
|
|
359
359
|
*/
|
|
360
|
-
|
|
360
|
+
int16<TName extends PropertyKey, TTypeScriptType = number>(
|
|
361
361
|
name: TName,
|
|
362
|
-
typeScriptType?: TTypeScriptType
|
|
362
|
+
typeScriptType?: TTypeScriptType,
|
|
363
363
|
) {
|
|
364
|
-
return this
|
|
364
|
+
return this.#number(name, NumberFieldType.Int16, typeScriptType);
|
|
365
365
|
}
|
|
366
366
|
|
|
367
367
|
/**
|
|
368
368
|
* Appends an `uint16` field to the `Struct`
|
|
369
369
|
*/
|
|
370
|
-
|
|
370
|
+
uint16<TName extends PropertyKey, TTypeScriptType = number>(
|
|
371
371
|
name: TName,
|
|
372
|
-
typeScriptType?: TTypeScriptType
|
|
372
|
+
typeScriptType?: TTypeScriptType,
|
|
373
373
|
) {
|
|
374
|
-
return this
|
|
374
|
+
return this.#number(name, NumberFieldType.Uint16, typeScriptType);
|
|
375
375
|
}
|
|
376
376
|
|
|
377
377
|
/**
|
|
378
378
|
* Appends an `int32` field to the `Struct`
|
|
379
379
|
*/
|
|
380
|
-
|
|
380
|
+
int32<TName extends PropertyKey, TTypeScriptType = number>(
|
|
381
381
|
name: TName,
|
|
382
|
-
typeScriptType?: TTypeScriptType
|
|
382
|
+
typeScriptType?: TTypeScriptType,
|
|
383
383
|
) {
|
|
384
|
-
return this
|
|
384
|
+
return this.#number(name, NumberFieldType.Int32, typeScriptType);
|
|
385
385
|
}
|
|
386
386
|
|
|
387
387
|
/**
|
|
388
388
|
* Appends an `uint32` field to the `Struct`
|
|
389
389
|
*/
|
|
390
|
-
|
|
390
|
+
uint32<TName extends PropertyKey, TTypeScriptType = number>(
|
|
391
391
|
name: TName,
|
|
392
|
-
typeScriptType?: TTypeScriptType
|
|
392
|
+
typeScriptType?: TTypeScriptType,
|
|
393
393
|
) {
|
|
394
|
-
return this
|
|
394
|
+
return this.#number(name, NumberFieldType.Uint32, typeScriptType);
|
|
395
395
|
}
|
|
396
396
|
|
|
397
|
-
|
|
397
|
+
#bigint<
|
|
398
398
|
TName extends PropertyKey,
|
|
399
399
|
TType extends BigIntFieldType = BigIntFieldType,
|
|
400
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
400
|
+
TTypeScriptType = TType["TTypeScriptType"],
|
|
401
401
|
>(name: TName, type: TType, typeScriptType?: TTypeScriptType) {
|
|
402
402
|
return this.field(
|
|
403
403
|
name,
|
|
404
|
-
new BigIntFieldDefinition(type, typeScriptType)
|
|
404
|
+
new BigIntFieldDefinition(type, typeScriptType),
|
|
405
405
|
);
|
|
406
406
|
}
|
|
407
407
|
|
|
@@ -410,11 +410,11 @@ export class Struct<
|
|
|
410
410
|
*
|
|
411
411
|
* Requires native `BigInt` support
|
|
412
412
|
*/
|
|
413
|
-
|
|
413
|
+
int64<
|
|
414
414
|
TName extends PropertyKey,
|
|
415
|
-
TTypeScriptType = BigIntFieldType["TTypeScriptType"]
|
|
415
|
+
TTypeScriptType = BigIntFieldType["TTypeScriptType"],
|
|
416
416
|
>(name: TName, typeScriptType?: TTypeScriptType) {
|
|
417
|
-
return this
|
|
417
|
+
return this.#bigint(name, BigIntFieldType.Int64, typeScriptType);
|
|
418
418
|
}
|
|
419
419
|
|
|
420
420
|
/**
|
|
@@ -422,14 +422,14 @@ export class Struct<
|
|
|
422
422
|
*
|
|
423
423
|
* Requires native `BigInt` support
|
|
424
424
|
*/
|
|
425
|
-
|
|
425
|
+
uint64<
|
|
426
426
|
TName extends PropertyKey,
|
|
427
|
-
TTypeScriptType = BigIntFieldType["TTypeScriptType"]
|
|
427
|
+
TTypeScriptType = BigIntFieldType["TTypeScriptType"],
|
|
428
428
|
>(name: TName, typeScriptType?: TTypeScriptType) {
|
|
429
|
-
return this
|
|
429
|
+
return this.#bigint(name, BigIntFieldType.Uint64, typeScriptType);
|
|
430
430
|
}
|
|
431
431
|
|
|
432
|
-
|
|
432
|
+
#arrayBufferLike: ArrayBufferLikeFieldCreator<
|
|
433
433
|
TFields,
|
|
434
434
|
TOmitInitKey,
|
|
435
435
|
TExtra,
|
|
@@ -439,48 +439,48 @@ export class Struct<
|
|
|
439
439
|
type: BufferFieldSubType,
|
|
440
440
|
options:
|
|
441
441
|
| FixedLengthBufferLikeFieldOptions
|
|
442
|
-
| VariableLengthBufferLikeFieldOptions
|
|
442
|
+
| VariableLengthBufferLikeFieldOptions,
|
|
443
443
|
): any => {
|
|
444
444
|
if ("length" in options) {
|
|
445
445
|
return this.field(
|
|
446
446
|
name,
|
|
447
|
-
new FixedLengthBufferLikeFieldDefinition(type, options)
|
|
447
|
+
new FixedLengthBufferLikeFieldDefinition(type, options),
|
|
448
448
|
);
|
|
449
449
|
} else {
|
|
450
450
|
return this.field(
|
|
451
451
|
name,
|
|
452
|
-
new VariableLengthBufferLikeFieldDefinition(type, options)
|
|
452
|
+
new VariableLengthBufferLikeFieldDefinition(type, options),
|
|
453
453
|
);
|
|
454
454
|
}
|
|
455
455
|
};
|
|
456
456
|
|
|
457
|
-
|
|
457
|
+
uint8Array: BoundArrayBufferLikeFieldDefinitionCreator<
|
|
458
458
|
TFields,
|
|
459
459
|
TOmitInitKey,
|
|
460
460
|
TExtra,
|
|
461
461
|
TPostDeserialized,
|
|
462
462
|
Uint8ArrayBufferFieldSubType
|
|
463
463
|
> = (name: PropertyKey, options: any, typeScriptType: any): any => {
|
|
464
|
-
return this
|
|
464
|
+
return this.#arrayBufferLike(
|
|
465
465
|
name,
|
|
466
466
|
Uint8ArrayBufferFieldSubType.Instance,
|
|
467
467
|
options,
|
|
468
|
-
typeScriptType
|
|
468
|
+
typeScriptType,
|
|
469
469
|
);
|
|
470
470
|
};
|
|
471
471
|
|
|
472
|
-
|
|
472
|
+
string: BoundArrayBufferLikeFieldDefinitionCreator<
|
|
473
473
|
TFields,
|
|
474
474
|
TOmitInitKey,
|
|
475
475
|
TExtra,
|
|
476
476
|
TPostDeserialized,
|
|
477
477
|
StringBufferFieldSubType
|
|
478
478
|
> = (name: PropertyKey, options: any, typeScriptType: any): any => {
|
|
479
|
-
return this
|
|
479
|
+
return this.#arrayBufferLike(
|
|
480
480
|
name,
|
|
481
481
|
StringBufferFieldSubType.Instance,
|
|
482
482
|
options,
|
|
483
|
-
typeScriptType
|
|
483
|
+
typeScriptType,
|
|
484
484
|
);
|
|
485
485
|
};
|
|
486
486
|
|
|
@@ -494,18 +494,18 @@ export class Struct<
|
|
|
494
494
|
* @param value
|
|
495
495
|
* An object containing properties to be added to the result value. Accessors and methods are also allowed.
|
|
496
496
|
*/
|
|
497
|
-
|
|
497
|
+
extra<
|
|
498
498
|
T extends Record<
|
|
499
499
|
// This trick disallows any keys that are already in `TValue`
|
|
500
500
|
Exclude<keyof T, Exclude<keyof T, keyof TFields>>,
|
|
501
501
|
never
|
|
502
|
-
|
|
502
|
+
>,
|
|
503
503
|
>(
|
|
504
|
-
value: T & ThisType<Overwrite<Overwrite<TExtra, T>, TFields
|
|
504
|
+
value: T & ThisType<Overwrite<Overwrite<TExtra, T>, TFields>>,
|
|
505
505
|
): Struct<TFields, TOmitInitKey, Overwrite<TExtra, T>, TPostDeserialized> {
|
|
506
506
|
Object.defineProperties(
|
|
507
507
|
this.#extra,
|
|
508
|
-
Object.getOwnPropertyDescriptors(value)
|
|
508
|
+
Object.getOwnPropertyDescriptors(value),
|
|
509
509
|
);
|
|
510
510
|
return this as any;
|
|
511
511
|
}
|
|
@@ -516,8 +516,8 @@ export class Struct<
|
|
|
516
516
|
* A callback returning `never` (always throw an error)
|
|
517
517
|
* will also change the return type of `deserialize` to `never`.
|
|
518
518
|
*/
|
|
519
|
-
|
|
520
|
-
callback: StructPostDeserialized<TFields, never
|
|
519
|
+
postDeserialize(
|
|
520
|
+
callback: StructPostDeserialized<TFields, never>,
|
|
521
521
|
): Struct<TFields, TOmitInitKey, TExtra, never>;
|
|
522
522
|
/**
|
|
523
523
|
* Registers (or replaces) a custom callback to be run after deserialized.
|
|
@@ -525,8 +525,8 @@ export class Struct<
|
|
|
525
525
|
* A callback returning `void` means it modify the result object in-place
|
|
526
526
|
* (or doesn't modify it at all), so `deserialize` will still return the result object.
|
|
527
527
|
*/
|
|
528
|
-
|
|
529
|
-
callback?: StructPostDeserialized<TFields, void
|
|
528
|
+
postDeserialize(
|
|
529
|
+
callback?: StructPostDeserialized<TFields, void>,
|
|
530
530
|
): Struct<TFields, TOmitInitKey, TExtra, undefined>;
|
|
531
531
|
/**
|
|
532
532
|
* Registers (or replaces) a custom callback to be run after deserialized.
|
|
@@ -534,10 +534,10 @@ export class Struct<
|
|
|
534
534
|
* A callback returning anything other than `undefined`
|
|
535
535
|
* will `deserialize` to return that object instead.
|
|
536
536
|
*/
|
|
537
|
-
|
|
538
|
-
callback?: StructPostDeserialized<TFields, TPostSerialize
|
|
537
|
+
postDeserialize<TPostSerialize>(
|
|
538
|
+
callback?: StructPostDeserialized<TFields, TPostSerialize>,
|
|
539
539
|
): Struct<TFields, TOmitInitKey, TExtra, TPostSerialize>;
|
|
540
|
-
|
|
540
|
+
postDeserialize(callback?: StructPostDeserialized<TFields, any>) {
|
|
541
541
|
this.#postDeserialized = callback;
|
|
542
542
|
return this as any;
|
|
543
543
|
}
|
|
@@ -545,14 +545,14 @@ export class Struct<
|
|
|
545
545
|
/**
|
|
546
546
|
* Deserialize a struct value from `stream`.
|
|
547
547
|
*/
|
|
548
|
-
|
|
549
|
-
stream: ExactReadable
|
|
548
|
+
deserialize(
|
|
549
|
+
stream: ExactReadable,
|
|
550
550
|
): StructDeserializedResult<TFields, TExtra, TPostDeserialized>;
|
|
551
|
-
|
|
552
|
-
stream: AsyncExactReadable
|
|
551
|
+
deserialize(
|
|
552
|
+
stream: AsyncExactReadable,
|
|
553
553
|
): Promise<StructDeserializedResult<TFields, TExtra, TPostDeserialized>>;
|
|
554
|
-
|
|
555
|
-
stream: ExactReadable | AsyncExactReadable
|
|
554
|
+
deserialize(
|
|
555
|
+
stream: ExactReadable | AsyncExactReadable,
|
|
556
556
|
): ValueOrPromise<
|
|
557
557
|
StructDeserializedResult<TFields, TExtra, TPostDeserialized>
|
|
558
558
|
> {
|
|
@@ -564,7 +564,7 @@ export class Struct<
|
|
|
564
564
|
for (const [name, definition] of this.#fields) {
|
|
565
565
|
promise = promise
|
|
566
566
|
.then(() =>
|
|
567
|
-
definition.deserialize(this.options, stream, structValue)
|
|
567
|
+
definition.deserialize(this.options, stream, structValue),
|
|
568
568
|
)
|
|
569
569
|
.then(
|
|
570
570
|
(fieldValue) => {
|
|
@@ -580,7 +580,7 @@ export class Struct<
|
|
|
580
580
|
} else {
|
|
581
581
|
throw new StructNotEnoughDataError();
|
|
582
582
|
}
|
|
583
|
-
}
|
|
583
|
+
},
|
|
584
584
|
);
|
|
585
585
|
}
|
|
586
586
|
|
|
@@ -603,14 +603,14 @@ export class Struct<
|
|
|
603
603
|
.valueOrPromise();
|
|
604
604
|
}
|
|
605
605
|
|
|
606
|
-
|
|
607
|
-
|
|
606
|
+
serialize(init: Evaluate<Omit<TFields, TOmitInitKey>>): Uint8Array;
|
|
607
|
+
serialize(
|
|
608
608
|
init: Evaluate<Omit<TFields, TOmitInitKey>>,
|
|
609
|
-
output: Uint8Array
|
|
609
|
+
output: Uint8Array,
|
|
610
610
|
): number;
|
|
611
|
-
|
|
611
|
+
serialize(
|
|
612
612
|
init: Evaluate<Omit<TFields, TOmitInitKey>>,
|
|
613
|
-
output?: Uint8Array
|
|
613
|
+
output?: Uint8Array,
|
|
614
614
|
): Uint8Array | number {
|
|
615
615
|
let structValue: StructValue;
|
|
616
616
|
if (STRUCT_VALUE_SYMBOL in init) {
|
|
@@ -627,7 +627,7 @@ export class Struct<
|
|
|
627
627
|
const fieldValue = definition.create(
|
|
628
628
|
this.options,
|
|
629
629
|
structValue,
|
|
630
|
-
(init as any)[name]
|
|
630
|
+
(init as any)[name],
|
|
631
631
|
);
|
|
632
632
|
structValue.set(name, fieldValue);
|
|
633
633
|
}
|
|
@@ -652,7 +652,7 @@ export class Struct<
|
|
|
652
652
|
const dataView = new DataView(
|
|
653
653
|
output.buffer,
|
|
654
654
|
output.byteOffset,
|
|
655
|
-
output.byteLength
|
|
655
|
+
output.byteLength,
|
|
656
656
|
);
|
|
657
657
|
let offset = 0;
|
|
658
658
|
for (const { fieldValue, size } of fieldsInfo) {
|