@yume-chan/struct 0.0.13 → 0.0.16
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 +33 -0
- package/CHANGELOG.md +21 -1
- package/LICENSE +21 -21
- package/README.md +767 -769
- package/esm/basic/definition.d.ts +3 -3
- package/esm/basic/definition.d.ts.map +1 -1
- package/esm/basic/definition.js +0 -1
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.d.ts +1 -0
- package/esm/basic/field-value.d.ts.map +1 -1
- package/esm/basic/field-value.js +4 -0
- package/esm/basic/field-value.js.map +1 -1
- package/esm/basic/stream.d.ts +2 -1
- package/esm/basic/stream.d.ts.map +1 -1
- package/esm/basic/struct-value.d.ts +7 -5
- package/esm/basic/struct-value.d.ts.map +1 -1
- package/esm/basic/struct-value.js +30 -14
- package/esm/basic/struct-value.js.map +1 -1
- package/esm/struct.d.ts +11 -11
- package/esm/struct.d.ts.map +1 -1
- package/esm/struct.js +55 -41
- package/esm/struct.js.map +1 -1
- package/esm/sync-promise.d.ts +13 -0
- package/esm/sync-promise.d.ts.map +1 -0
- package/esm/sync-promise.js +71 -0
- package/esm/sync-promise.js.map +1 -0
- package/esm/types/bigint.d.ts.map +1 -1
- package/esm/types/bigint.js +7 -5
- package/esm/types/bigint.js.map +1 -1
- package/esm/types/buffer/base.d.ts +4 -3
- package/esm/types/buffer/base.d.ts.map +1 -1
- package/esm/types/buffer/base.js +9 -7
- package/esm/types/buffer/base.js.map +1 -1
- package/esm/types/buffer/fixed-length.d.ts +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 +2 -2
- 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 +6 -5
- package/esm/types/number.d.ts.map +1 -1
- package/esm/types/number.js +25 -14
- package/esm/types/number.js.map +1 -1
- package/package.json +11 -11
- package/src/basic/definition.ts +68 -70
- package/src/basic/field-value.ts +72 -67
- package/src/basic/index.ts +5 -5
- package/src/basic/options.ts +19 -19
- package/src/basic/stream.ts +21 -19
- package/src/basic/struct-value.ts +61 -39
- package/src/index.ts +17 -17
- package/src/struct.ts +632 -609
- package/src/sync-promise.ts +114 -0
- package/src/types/bigint.ts +103 -102
- package/src/types/buffer/base.ts +179 -176
- package/src/types/buffer/fixed-length.ts +20 -17
- package/src/types/buffer/index.ts +3 -3
- package/src/types/buffer/variable-length.ts +158 -156
- package/src/types/index.ts +3 -3
- package/src/types/number.ts +127 -115
- package/src/utils.ts +70 -70
- package/tsconfig.build.json +3 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.test.json +8 -0
- package/esm/syncbird.d.ts +0 -65
- package/esm/syncbird.d.ts.map +0 -1
- package/esm/syncbird.js +0 -38
- package/esm/syncbird.js.map +0 -1
- package/src/syncbird.ts +0 -131
|
@@ -1,156 +1,158 @@
|
|
|
1
|
-
import { StructFieldValue, type StructFieldDefinition, type StructOptions, type StructValue } from '../../basic/index.js';
|
|
2
|
-
import type { KeysOfType } from '../../utils.js';
|
|
3
|
-
import { BufferLikeFieldDefinition, BufferLikeFieldValue, type BufferFieldSubType } from './base.js';
|
|
4
|
-
|
|
5
|
-
export type LengthField<TFields> = KeysOfType<TFields, number | string>;
|
|
6
|
-
|
|
7
|
-
export interface VariableLengthBufferLikeFieldOptions<
|
|
8
|
-
TFields = object,
|
|
9
|
-
TLengthField extends LengthField<TFields> = any,
|
|
10
|
-
> {
|
|
11
|
-
/**
|
|
12
|
-
* The name of the field that contains the length of the buffer.
|
|
13
|
-
*
|
|
14
|
-
* This field must be a `number` or `string` (can't be `bigint`) field.
|
|
15
|
-
*/
|
|
16
|
-
lengthField: TLengthField;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* If the `lengthField` refers to a string field,
|
|
20
|
-
* what radix to use when converting the string to a number.
|
|
21
|
-
*
|
|
22
|
-
* @default 10
|
|
23
|
-
*/
|
|
24
|
-
lengthFieldRadix?: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export class VariableLengthBufferLikeFieldDefinition<
|
|
28
|
-
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
29
|
-
TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
TOptions
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
protected
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
protected
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
1
|
+
import { StructFieldValue, type StructFieldDefinition, type StructOptions, type StructValue } from '../../basic/index.js';
|
|
2
|
+
import type { KeysOfType } from '../../utils.js';
|
|
3
|
+
import { BufferLikeFieldDefinition, BufferLikeFieldValue, type BufferFieldSubType } from './base.js';
|
|
4
|
+
|
|
5
|
+
export type LengthField<TFields> = KeysOfType<TFields, number | string>;
|
|
6
|
+
|
|
7
|
+
export interface VariableLengthBufferLikeFieldOptions<
|
|
8
|
+
TFields = object,
|
|
9
|
+
TLengthField extends LengthField<TFields> = any,
|
|
10
|
+
> {
|
|
11
|
+
/**
|
|
12
|
+
* The name of the field that contains the length of the buffer.
|
|
13
|
+
*
|
|
14
|
+
* This field must be a `number` or `string` (can't be `bigint`) field.
|
|
15
|
+
*/
|
|
16
|
+
lengthField: TLengthField;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* If the `lengthField` refers to a string field,
|
|
20
|
+
* what radix to use when converting the string to a number.
|
|
21
|
+
*
|
|
22
|
+
* @default 10
|
|
23
|
+
*/
|
|
24
|
+
lengthFieldRadix?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class VariableLengthBufferLikeFieldDefinition<
|
|
28
|
+
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
29
|
+
TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions,
|
|
30
|
+
TTypeScriptType = TType["TTypeScriptType"],
|
|
31
|
+
> extends BufferLikeFieldDefinition<
|
|
32
|
+
TType,
|
|
33
|
+
TOptions,
|
|
34
|
+
TOptions['lengthField'],
|
|
35
|
+
TTypeScriptType
|
|
36
|
+
> {
|
|
37
|
+
public getSize(): number {
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected override getDeserializeSize(struct: StructValue) {
|
|
42
|
+
let value = struct.value[this.options.lengthField] as number | string;
|
|
43
|
+
if (typeof value === 'string') {
|
|
44
|
+
value = Number.parseInt(value, this.options.lengthFieldRadix ?? 10);
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public override create(
|
|
50
|
+
options: Readonly<StructOptions>,
|
|
51
|
+
struct: StructValue,
|
|
52
|
+
value: TTypeScriptType,
|
|
53
|
+
array?: Uint8Array
|
|
54
|
+
): VariableLengthBufferLikeStructFieldValue<this> {
|
|
55
|
+
return new VariableLengthBufferLikeStructFieldValue(
|
|
56
|
+
this,
|
|
57
|
+
options,
|
|
58
|
+
struct,
|
|
59
|
+
value,
|
|
60
|
+
array,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class VariableLengthBufferLikeStructFieldValue<
|
|
66
|
+
TDefinition extends VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition,
|
|
67
|
+
> extends BufferLikeFieldValue<TDefinition> {
|
|
68
|
+
protected length: number | undefined;
|
|
69
|
+
|
|
70
|
+
protected lengthFieldValue: VariableLengthBufferLikeFieldLengthValue;
|
|
71
|
+
|
|
72
|
+
public constructor(
|
|
73
|
+
definition: TDefinition,
|
|
74
|
+
options: Readonly<StructOptions>,
|
|
75
|
+
struct: StructValue,
|
|
76
|
+
value: TDefinition['TValue'],
|
|
77
|
+
array?: Uint8Array,
|
|
78
|
+
) {
|
|
79
|
+
super(definition, options, struct, value, array);
|
|
80
|
+
|
|
81
|
+
if (array) {
|
|
82
|
+
this.length = array.byteLength;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Patch the associated length field.
|
|
86
|
+
const lengthField = this.definition.options.lengthField;
|
|
87
|
+
|
|
88
|
+
const originalValue = struct.get(lengthField);
|
|
89
|
+
this.lengthFieldValue = new VariableLengthBufferLikeFieldLengthValue(
|
|
90
|
+
originalValue,
|
|
91
|
+
this,
|
|
92
|
+
);
|
|
93
|
+
struct.set(lengthField, this.lengthFieldValue);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public override getSize() {
|
|
97
|
+
if (this.length === undefined) {
|
|
98
|
+
this.length = this.definition.type.getSize(this.value);
|
|
99
|
+
if (this.length === -1) {
|
|
100
|
+
this.array = this.definition.type.toBuffer(this.value);
|
|
101
|
+
this.length = this.array.byteLength;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return this.length;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public override set(value: unknown) {
|
|
109
|
+
super.set(value);
|
|
110
|
+
this.array = undefined;
|
|
111
|
+
this.length = undefined;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Not using `VariableLengthBufferLikeStructFieldValue` directly makes writing tests much easier...
|
|
116
|
+
type VariableLengthBufferLikeFieldValueLike =
|
|
117
|
+
StructFieldValue<StructFieldDefinition<VariableLengthBufferLikeFieldOptions, any, any>>;
|
|
118
|
+
|
|
119
|
+
export class VariableLengthBufferLikeFieldLengthValue
|
|
120
|
+
extends StructFieldValue {
|
|
121
|
+
protected originalField: StructFieldValue;
|
|
122
|
+
|
|
123
|
+
protected bufferField: VariableLengthBufferLikeFieldValueLike;
|
|
124
|
+
|
|
125
|
+
public constructor(
|
|
126
|
+
originalField: StructFieldValue,
|
|
127
|
+
arrayBufferField: VariableLengthBufferLikeFieldValueLike,
|
|
128
|
+
) {
|
|
129
|
+
super(originalField.definition, originalField.options, originalField.struct, 0);
|
|
130
|
+
this.originalField = originalField;
|
|
131
|
+
this.bufferField = arrayBufferField;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public override getSize() {
|
|
135
|
+
return this.originalField.getSize();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public override get() {
|
|
139
|
+
let value: string | number = this.bufferField.getSize();
|
|
140
|
+
|
|
141
|
+
const originalValue = this.originalField.get();
|
|
142
|
+
if (typeof originalValue === 'string') {
|
|
143
|
+
value = value.toString(this.bufferField.definition.options.lengthFieldRadix ?? 10);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public override set() {
|
|
150
|
+
// Ignore setting
|
|
151
|
+
// It will always be in sync with the buffer size
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
serialize(dataView: DataView, offset: number) {
|
|
155
|
+
this.originalField.set(this.get());
|
|
156
|
+
this.originalField.serialize(dataView, offset);
|
|
157
|
+
}
|
|
158
|
+
}
|
package/src/types/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./bigint.js";
|
|
2
|
-
export * from "./buffer/index.js";
|
|
3
|
-
export * from "./number.js";
|
|
1
|
+
export * from "./bigint.js";
|
|
2
|
+
export * from "./buffer/index.js";
|
|
3
|
+
export * from "./number.js";
|
package/src/types/number.ts
CHANGED
|
@@ -1,115 +1,127 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
public
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
public readonly
|
|
53
|
-
|
|
54
|
-
public
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
struct: StructValue,
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
options
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
1
|
+
import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from '../basic/index.js';
|
|
2
|
+
import { SyncPromise } from "../sync-promise.js";
|
|
3
|
+
import type { ValueOrPromise } from "../utils.js";
|
|
4
|
+
|
|
5
|
+
type NumberTypeDeserializer = (array: Uint8Array, littleEndian: boolean) => number;
|
|
6
|
+
|
|
7
|
+
const DESERIALIZERS: Record<number, NumberTypeDeserializer> = {
|
|
8
|
+
1: (array, littleEndian) =>
|
|
9
|
+
array[0]!,
|
|
10
|
+
2: (array, littleEndian) =>
|
|
11
|
+
((array[1]! << 8) | array[0]!) * (littleEndian as any) |
|
|
12
|
+
((array[0]! << 8) | array[1]!) * (!littleEndian as any),
|
|
13
|
+
4: (array, littleEndian) =>
|
|
14
|
+
((array[3]! << 24) | (array[2]! << 16) | (array[1]! << 8) | array[0]!) * (littleEndian as any) |
|
|
15
|
+
((array[0]! << 24) | (array[1]! << 16) | (array[2]! << 8) | array[3]!) * (!littleEndian as any),
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type DataViewSetters =
|
|
19
|
+
{ [TKey in keyof DataView]: TKey extends `set${string}` ? TKey : never }[keyof DataView];
|
|
20
|
+
|
|
21
|
+
export class NumberFieldType {
|
|
22
|
+
public readonly TTypeScriptType!: number;
|
|
23
|
+
|
|
24
|
+
public readonly signed: boolean;
|
|
25
|
+
|
|
26
|
+
public readonly size: number;
|
|
27
|
+
|
|
28
|
+
public readonly deserializer: NumberTypeDeserializer;
|
|
29
|
+
public readonly convertSign: (value: number) => number;
|
|
30
|
+
|
|
31
|
+
public readonly dataViewSetter: DataViewSetters;
|
|
32
|
+
|
|
33
|
+
public constructor(
|
|
34
|
+
size: number,
|
|
35
|
+
signed: boolean,
|
|
36
|
+
convertSign: (value: number) => number,
|
|
37
|
+
dataViewSetter: DataViewSetters
|
|
38
|
+
) {
|
|
39
|
+
this.size = size;
|
|
40
|
+
this.signed = signed;
|
|
41
|
+
this.deserializer = DESERIALIZERS[size]!;
|
|
42
|
+
this.convertSign = convertSign;
|
|
43
|
+
this.dataViewSetter = dataViewSetter;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public static readonly Int8 = new NumberFieldType(1, true, value => value << 24 >> 24, 'setInt8');
|
|
47
|
+
|
|
48
|
+
public static readonly Uint8 = new NumberFieldType(1, false, value => value, 'setUint8');
|
|
49
|
+
|
|
50
|
+
public static readonly Int16 = new NumberFieldType(2, true, value => value << 16 >> 16, 'setInt16');
|
|
51
|
+
|
|
52
|
+
public static readonly Uint16 = new NumberFieldType(2, false, value => value, 'setUint16');
|
|
53
|
+
|
|
54
|
+
public static readonly Int32 = new NumberFieldType(4, true, value => value, 'setInt32');
|
|
55
|
+
|
|
56
|
+
public static readonly Uint32 = new NumberFieldType(4, false, value => value >>> 0, 'setUint32');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class NumberFieldDefinition<
|
|
60
|
+
TType extends NumberFieldType = NumberFieldType,
|
|
61
|
+
TTypeScriptType = TType["TTypeScriptType"],
|
|
62
|
+
> extends StructFieldDefinition<
|
|
63
|
+
void,
|
|
64
|
+
TTypeScriptType
|
|
65
|
+
> {
|
|
66
|
+
public readonly type: TType;
|
|
67
|
+
|
|
68
|
+
public constructor(type: TType, _typescriptType?: TTypeScriptType) {
|
|
69
|
+
super();
|
|
70
|
+
this.type = type;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public getSize(): number {
|
|
74
|
+
return this.type.size;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public create(
|
|
78
|
+
options: Readonly<StructOptions>,
|
|
79
|
+
struct: StructValue,
|
|
80
|
+
value: TTypeScriptType,
|
|
81
|
+
): NumberFieldValue<this> {
|
|
82
|
+
return new NumberFieldValue(this, options, struct, value);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public override deserialize(
|
|
86
|
+
options: Readonly<StructOptions>,
|
|
87
|
+
stream: StructDeserializeStream,
|
|
88
|
+
struct: StructValue,
|
|
89
|
+
): NumberFieldValue<this>;
|
|
90
|
+
public override deserialize(
|
|
91
|
+
options: Readonly<StructOptions>,
|
|
92
|
+
stream: StructAsyncDeserializeStream,
|
|
93
|
+
struct: StructValue,
|
|
94
|
+
): Promise<NumberFieldValue<this>>;
|
|
95
|
+
public override deserialize(
|
|
96
|
+
options: Readonly<StructOptions>,
|
|
97
|
+
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
|
98
|
+
struct: StructValue,
|
|
99
|
+
): ValueOrPromise<NumberFieldValue<this>> {
|
|
100
|
+
return SyncPromise
|
|
101
|
+
.try(() => {
|
|
102
|
+
return stream.read(this.getSize());
|
|
103
|
+
})
|
|
104
|
+
.then(array => {
|
|
105
|
+
let value: number;
|
|
106
|
+
value = this.type.deserializer(array, options.littleEndian);
|
|
107
|
+
value = this.type.convertSign(value);
|
|
108
|
+
return this.create(options, struct, value as any);
|
|
109
|
+
})
|
|
110
|
+
.valueOrPromise();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export class NumberFieldValue<
|
|
115
|
+
TDefinition extends NumberFieldDefinition<NumberFieldType, any>,
|
|
116
|
+
> extends StructFieldValue<TDefinition> {
|
|
117
|
+
public serialize(dataView: DataView, offset: number): void {
|
|
118
|
+
// `setBigInt64` requires a `bigint` while others require `number`
|
|
119
|
+
// So `dataView[DataViewSetters]` requires `bigint & number`
|
|
120
|
+
// and that is, `never`
|
|
121
|
+
(dataView[this.definition.type.dataViewSetter] as any)(
|
|
122
|
+
offset,
|
|
123
|
+
this.value!,
|
|
124
|
+
this.options.littleEndian
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|