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