@yume-chan/struct 0.0.23 → 1.0.0
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.md +37 -2
- package/LICENSE +1 -1
- package/README.md +85 -714
- package/esm/bipedal.d.ts +5 -0
- package/esm/bipedal.d.ts.map +1 -0
- package/esm/bipedal.js +31 -0
- package/esm/bipedal.js.map +1 -0
- package/esm/buffer.d.ts +21 -0
- package/esm/buffer.d.ts.map +1 -0
- package/esm/buffer.js +173 -0
- package/esm/buffer.js.map +1 -0
- package/esm/field.d.ts +21 -0
- package/esm/field.d.ts.map +1 -0
- package/esm/field.js +2 -0
- package/esm/field.js.map +1 -0
- package/esm/index.d.ts +6 -4
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +6 -4
- package/esm/index.js.map +1 -1
- package/esm/number.d.ts +13 -0
- package/esm/number.d.ts.map +1 -0
- package/esm/number.js +59 -0
- package/esm/number.js.map +1 -0
- package/esm/{basic/stream.d.ts → readable.d.ts} +3 -3
- package/esm/readable.d.ts.map +1 -0
- package/esm/{basic/stream.js → readable.js} +1 -2
- package/esm/readable.js.map +1 -0
- package/esm/string.d.ts +16 -0
- package/esm/string.d.ts.map +1 -0
- package/esm/string.js +14 -0
- package/esm/string.js.map +1 -0
- package/esm/struct.d.ts +24 -127
- package/esm/struct.d.ts.map +1 -1
- package/esm/struct.js +72 -214
- package/esm/struct.js.map +1 -1
- package/esm/utils.d.ts +10 -45
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +12 -11
- package/esm/utils.js.map +1 -1
- package/package.json +11 -14
- package/src/bipedal.ts +55 -0
- package/src/buffer.ts +260 -0
- package/src/field.ts +27 -0
- package/src/index.ts +6 -4
- package/src/number.ts +123 -0
- package/src/{basic/stream.ts → readable.ts} +3 -4
- package/src/string.ts +43 -0
- package/src/struct.ts +156 -628
- package/src/utils.ts +18 -69
- package/tsconfig.build.json +6 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/CHANGELOG.json +0 -161
- package/esm/basic/definition.d.ts +0 -44
- package/esm/basic/definition.d.ts.map +0 -1
- package/esm/basic/definition.js +0 -24
- package/esm/basic/definition.js.map +0 -1
- package/esm/basic/field-value.d.ts +0 -39
- package/esm/basic/field-value.d.ts.map +0 -1
- package/esm/basic/field-value.js +0 -46
- package/esm/basic/field-value.js.map +0 -1
- package/esm/basic/index.d.ts +0 -6
- package/esm/basic/index.d.ts.map +0 -1
- package/esm/basic/index.js +0 -6
- package/esm/basic/index.js.map +0 -1
- package/esm/basic/options.d.ts +0 -10
- package/esm/basic/options.d.ts.map +0 -1
- package/esm/basic/options.js +0 -4
- package/esm/basic/options.js.map +0 -1
- package/esm/basic/stream.d.ts.map +0 -1
- package/esm/basic/stream.js.map +0 -1
- package/esm/basic/struct-value.d.ts +0 -26
- package/esm/basic/struct-value.d.ts.map +0 -1
- package/esm/basic/struct-value.js +0 -57
- package/esm/basic/struct-value.js.map +0 -1
- package/esm/sync-promise.d.ts +0 -13
- package/esm/sync-promise.d.ts.map +0 -1
- package/esm/sync-promise.js +0 -71
- package/esm/sync-promise.js.map +0 -1
- package/esm/types/bigint.d.ts +0 -26
- package/esm/types/bigint.d.ts.map +0 -1
- package/esm/types/bigint.js +0 -47
- package/esm/types/bigint.js.map +0 -1
- package/esm/types/buffer/base.d.ts +0 -65
- package/esm/types/buffer/base.d.ts.map +0 -1
- package/esm/types/buffer/base.js +0 -101
- package/esm/types/buffer/base.js.map +0 -1
- package/esm/types/buffer/fixed-length.d.ts +0 -9
- package/esm/types/buffer/fixed-length.d.ts.map +0 -1
- package/esm/types/buffer/fixed-length.js +0 -7
- package/esm/types/buffer/fixed-length.js.map +0 -1
- package/esm/types/buffer/index.d.ts +0 -4
- package/esm/types/buffer/index.d.ts.map +0 -1
- package/esm/types/buffer/index.js +0 -4
- package/esm/types/buffer/index.js.map +0 -1
- package/esm/types/buffer/variable-length.d.ts +0 -45
- package/esm/types/buffer/variable-length.d.ts.map +0 -1
- package/esm/types/buffer/variable-length.js +0 -76
- package/esm/types/buffer/variable-length.js.map +0 -1
- package/esm/types/index.d.ts +0 -4
- package/esm/types/index.d.ts.map +0 -1
- package/esm/types/index.js +0 -4
- package/esm/types/index.js.map +0 -1
- package/esm/types/number.d.ts +0 -28
- package/esm/types/number.d.ts.map +0 -1
- package/esm/types/number.js +0 -111
- package/esm/types/number.js.map +0 -1
- package/src/basic/definition.ts +0 -68
- package/src/basic/field-value.ts +0 -75
- package/src/basic/index.ts +0 -5
- package/src/basic/options.ts +0 -19
- package/src/basic/struct-value.ts +0 -67
- package/src/sync-promise.ts +0 -134
- package/src/types/bigint.ts +0 -121
- package/src/types/buffer/base.ts +0 -197
- package/src/types/buffer/fixed-length.ts +0 -17
- package/src/types/buffer/index.ts +0 -3
- package/src/types/buffer/variable-length.ts +0 -174
- package/src/types/index.ts +0 -3
- package/src/types/number.ts +0 -173
package/src/types/buffer/base.ts
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AsyncExactReadable,
|
|
3
|
-
ExactReadable,
|
|
4
|
-
StructOptions,
|
|
5
|
-
StructValue,
|
|
6
|
-
} from "../../basic/index.js";
|
|
7
|
-
import { StructFieldDefinition, StructFieldValue } from "../../basic/index.js";
|
|
8
|
-
import { SyncPromise } from "../../sync-promise.js";
|
|
9
|
-
import type { ValueOrPromise } from "../../utils.js";
|
|
10
|
-
import { decodeUtf8, encodeUtf8 } from "../../utils.js";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Base class for all types that
|
|
14
|
-
* can be converted from an `Uint8Array` when deserialized,
|
|
15
|
-
* and need to be converted to an `Uint8Array` when serializing
|
|
16
|
-
*
|
|
17
|
-
* @template TValue The actual TypeScript type of this type
|
|
18
|
-
* @template TTypeScriptType Optional another type (should be compatible with `TType`)
|
|
19
|
-
* specified by user when creating field definitions.
|
|
20
|
-
*/
|
|
21
|
-
export abstract class BufferFieldSubType<
|
|
22
|
-
TValue = unknown,
|
|
23
|
-
TTypeScriptType = TValue,
|
|
24
|
-
> {
|
|
25
|
-
readonly TTypeScriptType!: TTypeScriptType;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* When implemented in derived classes, converts the type-specific `value` to an `Uint8Array`
|
|
29
|
-
*
|
|
30
|
-
* This function should be "pure", i.e.,
|
|
31
|
-
* same `value` should always be converted to `Uint8Array`s that have same content.
|
|
32
|
-
*/
|
|
33
|
-
abstract toBuffer(value: TValue): Uint8Array;
|
|
34
|
-
|
|
35
|
-
/** When implemented in derived classes, converts the `Uint8Array` to a type-specific value */
|
|
36
|
-
abstract toValue(array: Uint8Array): TValue;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* When implemented in derived classes, gets the size in byte of the type-specific `value`.
|
|
40
|
-
*
|
|
41
|
-
* If the size can't be calculated without first converting the `value` back to an `Uint8Array`,
|
|
42
|
-
* implementer can returns `-1`, so the caller will get its size by first converting it to
|
|
43
|
-
* an `Uint8Array` (and cache the result).
|
|
44
|
-
*/
|
|
45
|
-
abstract getSize(value: TValue): number;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** An `BufferFieldSubType` that's actually an `Uint8Array` */
|
|
49
|
-
export class Uint8ArrayBufferFieldSubType<
|
|
50
|
-
TTypeScriptType = Uint8Array,
|
|
51
|
-
> extends BufferFieldSubType<Uint8Array, TTypeScriptType> {
|
|
52
|
-
static readonly Instance = new Uint8ArrayBufferFieldSubType();
|
|
53
|
-
|
|
54
|
-
protected constructor() {
|
|
55
|
-
super();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
toBuffer(value: Uint8Array): Uint8Array {
|
|
59
|
-
return value;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
toValue(buffer: Uint8Array): Uint8Array {
|
|
63
|
-
return buffer;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
getSize(value: Uint8Array): number {
|
|
67
|
-
return value.byteLength;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** An `BufferFieldSubType` that converts between `Uint8Array` and `string` */
|
|
72
|
-
export class StringBufferFieldSubType<
|
|
73
|
-
TTypeScriptType = string,
|
|
74
|
-
> extends BufferFieldSubType<string, TTypeScriptType> {
|
|
75
|
-
static readonly Instance = new StringBufferFieldSubType();
|
|
76
|
-
|
|
77
|
-
toBuffer(value: string): Uint8Array {
|
|
78
|
-
return encodeUtf8(value);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
toValue(array: Uint8Array): string {
|
|
82
|
-
return decodeUtf8(array);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
getSize(): number {
|
|
86
|
-
// Return `-1`, so `BufferLikeFieldDefinition` will
|
|
87
|
-
// convert this `value` into an `Uint8Array` (and cache the result),
|
|
88
|
-
// Then get the size from that `Uint8Array`
|
|
89
|
-
return -1;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export const EMPTY_UINT8_ARRAY = new Uint8Array(0);
|
|
94
|
-
|
|
95
|
-
export abstract class BufferLikeFieldDefinition<
|
|
96
|
-
TType extends BufferFieldSubType<any, any> = BufferFieldSubType<
|
|
97
|
-
unknown,
|
|
98
|
-
unknown
|
|
99
|
-
>,
|
|
100
|
-
TOptions = void,
|
|
101
|
-
TOmitInitKey extends PropertyKey = never,
|
|
102
|
-
TTypeScriptType = TType["TTypeScriptType"],
|
|
103
|
-
> extends StructFieldDefinition<TOptions, TTypeScriptType, TOmitInitKey> {
|
|
104
|
-
readonly type: TType;
|
|
105
|
-
|
|
106
|
-
constructor(type: TType, options: TOptions) {
|
|
107
|
-
super(options);
|
|
108
|
-
this.type = type;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
protected getDeserializeSize(struct: StructValue): number {
|
|
112
|
-
void struct;
|
|
113
|
-
return this.getSize();
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* When implemented in derived classes, creates a `StructFieldValue` for the current field definition.
|
|
118
|
-
*/
|
|
119
|
-
create(
|
|
120
|
-
options: Readonly<StructOptions>,
|
|
121
|
-
struct: StructValue,
|
|
122
|
-
value: TType["TTypeScriptType"],
|
|
123
|
-
array?: Uint8Array,
|
|
124
|
-
): BufferLikeFieldValue<this> {
|
|
125
|
-
return new BufferLikeFieldValue(this, options, struct, value, array);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
override deserialize(
|
|
129
|
-
options: Readonly<StructOptions>,
|
|
130
|
-
stream: ExactReadable,
|
|
131
|
-
struct: StructValue,
|
|
132
|
-
): BufferLikeFieldValue<this>;
|
|
133
|
-
override deserialize(
|
|
134
|
-
options: Readonly<StructOptions>,
|
|
135
|
-
stream: AsyncExactReadable,
|
|
136
|
-
struct: StructValue,
|
|
137
|
-
): Promise<BufferLikeFieldValue<this>>;
|
|
138
|
-
override deserialize(
|
|
139
|
-
options: Readonly<StructOptions>,
|
|
140
|
-
stream: ExactReadable | AsyncExactReadable,
|
|
141
|
-
struct: StructValue,
|
|
142
|
-
): ValueOrPromise<BufferLikeFieldValue<this>> {
|
|
143
|
-
return SyncPromise.try(() => {
|
|
144
|
-
const size = this.getDeserializeSize(struct);
|
|
145
|
-
if (size === 0) {
|
|
146
|
-
return EMPTY_UINT8_ARRAY;
|
|
147
|
-
} else {
|
|
148
|
-
return stream.readExactly(size);
|
|
149
|
-
}
|
|
150
|
-
})
|
|
151
|
-
.then((array) => {
|
|
152
|
-
const value = this.type.toValue(array);
|
|
153
|
-
return this.create(options, struct, value, array);
|
|
154
|
-
})
|
|
155
|
-
.valueOrPromise();
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export class BufferLikeFieldValue<
|
|
160
|
-
TDefinition extends BufferLikeFieldDefinition<
|
|
161
|
-
BufferFieldSubType<unknown, unknown>,
|
|
162
|
-
any,
|
|
163
|
-
any
|
|
164
|
-
>,
|
|
165
|
-
> extends StructFieldValue<TDefinition> {
|
|
166
|
-
protected array: Uint8Array | undefined;
|
|
167
|
-
|
|
168
|
-
constructor(
|
|
169
|
-
definition: TDefinition,
|
|
170
|
-
options: Readonly<StructOptions>,
|
|
171
|
-
struct: StructValue,
|
|
172
|
-
value: TDefinition["TValue"],
|
|
173
|
-
array?: Uint8Array,
|
|
174
|
-
) {
|
|
175
|
-
super(definition, options, struct, value);
|
|
176
|
-
this.array = array;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
override set(value: TDefinition["TValue"]): void {
|
|
180
|
-
super.set(value);
|
|
181
|
-
// When value changes, clear the cached `array`
|
|
182
|
-
// It will be lazily calculated in `serialize()`
|
|
183
|
-
this.array = undefined;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
serialize(dataView: DataView, offset: number): void {
|
|
187
|
-
if (!this.array) {
|
|
188
|
-
this.array = this.definition.type.toBuffer(this.value);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
new Uint8Array(
|
|
192
|
-
dataView.buffer,
|
|
193
|
-
dataView.byteOffset,
|
|
194
|
-
dataView.byteLength,
|
|
195
|
-
).set(this.array, offset);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { BufferFieldSubType } from "./base.js";
|
|
2
|
-
import { BufferLikeFieldDefinition } from "./base.js";
|
|
3
|
-
|
|
4
|
-
export interface FixedLengthBufferLikeFieldOptions {
|
|
5
|
-
length: number;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export class FixedLengthBufferLikeFieldDefinition<
|
|
9
|
-
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
10
|
-
TOptions extends
|
|
11
|
-
FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
|
|
12
|
-
TTypeScriptType = TType["TTypeScriptType"],
|
|
13
|
-
> extends BufferLikeFieldDefinition<TType, TOptions, never, TTypeScriptType> {
|
|
14
|
-
getSize(): number {
|
|
15
|
-
return this.options.length;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
StructFieldDefinition,
|
|
3
|
-
StructOptions,
|
|
4
|
-
StructValue,
|
|
5
|
-
} from "../../basic/index.js";
|
|
6
|
-
import { StructFieldValue } from "../../basic/index.js";
|
|
7
|
-
import type { KeysOfType } from "../../utils.js";
|
|
8
|
-
|
|
9
|
-
import type { BufferFieldSubType } from "./base.js";
|
|
10
|
-
import { BufferLikeFieldDefinition, BufferLikeFieldValue } from "./base.js";
|
|
11
|
-
|
|
12
|
-
export type LengthField<TFields> = KeysOfType<TFields, number | string>;
|
|
13
|
-
|
|
14
|
-
export interface VariableLengthBufferLikeFieldOptions<
|
|
15
|
-
TFields = object,
|
|
16
|
-
TLengthField extends LengthField<TFields> = any,
|
|
17
|
-
> {
|
|
18
|
-
/**
|
|
19
|
-
* The name of the field that contains the length of the buffer.
|
|
20
|
-
*
|
|
21
|
-
* This field must be a `number` or `string` (can't be `bigint`) field.
|
|
22
|
-
*/
|
|
23
|
-
lengthField: TLengthField;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* If the `lengthField` refers to a string field,
|
|
27
|
-
* what radix to use when converting the string to a number.
|
|
28
|
-
*
|
|
29
|
-
* @default 10
|
|
30
|
-
*/
|
|
31
|
-
lengthFieldRadix?: number;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export class VariableLengthBufferLikeFieldDefinition<
|
|
35
|
-
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
36
|
-
TOptions extends
|
|
37
|
-
VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions,
|
|
38
|
-
TTypeScriptType = TType["TTypeScriptType"],
|
|
39
|
-
> extends BufferLikeFieldDefinition<
|
|
40
|
-
TType,
|
|
41
|
-
TOptions,
|
|
42
|
-
TOptions["lengthField"],
|
|
43
|
-
TTypeScriptType
|
|
44
|
-
> {
|
|
45
|
-
getSize(): number {
|
|
46
|
-
return 0;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
protected override getDeserializeSize(struct: StructValue) {
|
|
50
|
-
let value = struct.value[this.options.lengthField] as number | string;
|
|
51
|
-
if (typeof value === "string") {
|
|
52
|
-
value = Number.parseInt(value, this.options.lengthFieldRadix ?? 10);
|
|
53
|
-
}
|
|
54
|
-
return value;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
override create(
|
|
58
|
-
options: Readonly<StructOptions>,
|
|
59
|
-
struct: StructValue,
|
|
60
|
-
value: TTypeScriptType,
|
|
61
|
-
array?: Uint8Array,
|
|
62
|
-
): VariableLengthBufferLikeStructFieldValue<this> {
|
|
63
|
-
return new VariableLengthBufferLikeStructFieldValue(
|
|
64
|
-
this,
|
|
65
|
-
options,
|
|
66
|
-
struct,
|
|
67
|
-
value,
|
|
68
|
-
array,
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export class VariableLengthBufferLikeStructFieldValue<
|
|
74
|
-
TDefinition extends
|
|
75
|
-
VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition,
|
|
76
|
-
> extends BufferLikeFieldValue<TDefinition> {
|
|
77
|
-
protected length: number | undefined;
|
|
78
|
-
|
|
79
|
-
protected lengthFieldValue: VariableLengthBufferLikeFieldLengthValue;
|
|
80
|
-
|
|
81
|
-
constructor(
|
|
82
|
-
definition: TDefinition,
|
|
83
|
-
options: Readonly<StructOptions>,
|
|
84
|
-
struct: StructValue,
|
|
85
|
-
value: TDefinition["TValue"],
|
|
86
|
-
array?: Uint8Array,
|
|
87
|
-
) {
|
|
88
|
-
super(definition, options, struct, value, array);
|
|
89
|
-
|
|
90
|
-
if (array) {
|
|
91
|
-
this.length = array.byteLength;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Patch the associated length field.
|
|
95
|
-
const lengthField = this.definition.options.lengthField;
|
|
96
|
-
|
|
97
|
-
const originalValue = struct.get(lengthField);
|
|
98
|
-
this.lengthFieldValue = new VariableLengthBufferLikeFieldLengthValue(
|
|
99
|
-
originalValue,
|
|
100
|
-
this,
|
|
101
|
-
);
|
|
102
|
-
struct.set(lengthField, this.lengthFieldValue);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
override getSize() {
|
|
106
|
-
if (this.length === undefined) {
|
|
107
|
-
this.length = this.definition.type.getSize(this.value);
|
|
108
|
-
if (this.length === -1) {
|
|
109
|
-
this.array = this.definition.type.toBuffer(this.value);
|
|
110
|
-
this.length = this.array.byteLength;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return this.length;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
override set(value: unknown) {
|
|
118
|
-
super.set(value);
|
|
119
|
-
this.array = undefined;
|
|
120
|
-
this.length = undefined;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Not using `VariableLengthBufferLikeStructFieldValue` directly makes writing tests much easier...
|
|
125
|
-
type VariableLengthBufferLikeFieldValueLike = StructFieldValue<
|
|
126
|
-
StructFieldDefinition<VariableLengthBufferLikeFieldOptions, any, any>
|
|
127
|
-
>;
|
|
128
|
-
|
|
129
|
-
export class VariableLengthBufferLikeFieldLengthValue extends StructFieldValue {
|
|
130
|
-
protected originalField: StructFieldValue;
|
|
131
|
-
|
|
132
|
-
protected bufferField: VariableLengthBufferLikeFieldValueLike;
|
|
133
|
-
|
|
134
|
-
constructor(
|
|
135
|
-
originalField: StructFieldValue,
|
|
136
|
-
arrayBufferField: VariableLengthBufferLikeFieldValueLike,
|
|
137
|
-
) {
|
|
138
|
-
super(
|
|
139
|
-
originalField.definition,
|
|
140
|
-
originalField.options,
|
|
141
|
-
originalField.struct,
|
|
142
|
-
0,
|
|
143
|
-
);
|
|
144
|
-
this.originalField = originalField;
|
|
145
|
-
this.bufferField = arrayBufferField;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
override getSize() {
|
|
149
|
-
return this.originalField.getSize();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
override get() {
|
|
153
|
-
let value: string | number = this.bufferField.getSize();
|
|
154
|
-
|
|
155
|
-
const originalValue = this.originalField.get();
|
|
156
|
-
if (typeof originalValue === "string") {
|
|
157
|
-
value = value.toString(
|
|
158
|
-
this.bufferField.definition.options.lengthFieldRadix ?? 10,
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return value;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
override set() {
|
|
166
|
-
// Ignore setting
|
|
167
|
-
// It will always be in sync with the buffer size
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
serialize(dataView: DataView, offset: number) {
|
|
171
|
-
this.originalField.set(this.get());
|
|
172
|
-
this.originalField.serialize(dataView, offset);
|
|
173
|
-
}
|
|
174
|
-
}
|
package/src/types/index.ts
DELETED
package/src/types/number.ts
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AsyncExactReadable,
|
|
3
|
-
ExactReadable,
|
|
4
|
-
StructOptions,
|
|
5
|
-
StructValue,
|
|
6
|
-
} from "../basic/index.js";
|
|
7
|
-
import { StructFieldDefinition, StructFieldValue } from "../basic/index.js";
|
|
8
|
-
import { SyncPromise } from "../sync-promise.js";
|
|
9
|
-
import type { ValueOrPromise } from "../utils.js";
|
|
10
|
-
|
|
11
|
-
export interface NumberFieldType {
|
|
12
|
-
signed: boolean;
|
|
13
|
-
size: number;
|
|
14
|
-
deserialize(array: Uint8Array, littleEndian: boolean): number;
|
|
15
|
-
serialize(
|
|
16
|
-
dataView: DataView,
|
|
17
|
-
offset: number,
|
|
18
|
-
value: number,
|
|
19
|
-
littleEndian: boolean,
|
|
20
|
-
): void;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export namespace NumberFieldType {
|
|
24
|
-
export const Uint8: NumberFieldType = {
|
|
25
|
-
signed: false,
|
|
26
|
-
size: 1,
|
|
27
|
-
deserialize(array) {
|
|
28
|
-
return array[0]!;
|
|
29
|
-
},
|
|
30
|
-
serialize(dataView, offset, value) {
|
|
31
|
-
dataView.setUint8(offset, value);
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export const Int8: NumberFieldType = {
|
|
36
|
-
signed: true,
|
|
37
|
-
size: 1,
|
|
38
|
-
deserialize(array) {
|
|
39
|
-
const value = Uint8.deserialize(array, false);
|
|
40
|
-
return (value << 24) >> 24;
|
|
41
|
-
},
|
|
42
|
-
serialize(dataView, offset, value) {
|
|
43
|
-
dataView.setInt8(offset, value);
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export const Uint16: NumberFieldType = {
|
|
48
|
-
signed: false,
|
|
49
|
-
size: 2,
|
|
50
|
-
deserialize(array, littleEndian) {
|
|
51
|
-
// PERF: Creating many `DataView`s over small buffers is 90% slower
|
|
52
|
-
// than this. Even if the `DataView` is cached, `DataView#getUint16`
|
|
53
|
-
// is still 1% slower than this.
|
|
54
|
-
const a = (array[1]! << 8) | array[0]!;
|
|
55
|
-
const b = (array[0]! << 8) | array[1]!;
|
|
56
|
-
return littleEndian ? a : b;
|
|
57
|
-
},
|
|
58
|
-
serialize(dataView, offset, value, littleEndian) {
|
|
59
|
-
dataView.setUint16(offset, value, littleEndian);
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export const Int16: NumberFieldType = {
|
|
64
|
-
signed: true,
|
|
65
|
-
size: 2,
|
|
66
|
-
deserialize(array, littleEndian) {
|
|
67
|
-
const value = Uint16.deserialize(array, littleEndian);
|
|
68
|
-
return (value << 16) >> 16;
|
|
69
|
-
},
|
|
70
|
-
serialize(dataView, offset, value, littleEndian) {
|
|
71
|
-
dataView.setInt16(offset, value, littleEndian);
|
|
72
|
-
},
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
export const Uint32: NumberFieldType = {
|
|
76
|
-
signed: false,
|
|
77
|
-
size: 4,
|
|
78
|
-
deserialize(array, littleEndian) {
|
|
79
|
-
const value = Int32.deserialize(array, littleEndian);
|
|
80
|
-
return value >>> 0;
|
|
81
|
-
},
|
|
82
|
-
serialize(dataView, offset, value, littleEndian) {
|
|
83
|
-
dataView.setUint32(offset, value, littleEndian);
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export const Int32: NumberFieldType = {
|
|
88
|
-
signed: true,
|
|
89
|
-
size: 4,
|
|
90
|
-
deserialize(array, littleEndian) {
|
|
91
|
-
const a =
|
|
92
|
-
(array[3]! << 24) |
|
|
93
|
-
(array[2]! << 16) |
|
|
94
|
-
(array[1]! << 8) |
|
|
95
|
-
array[0]!;
|
|
96
|
-
const b =
|
|
97
|
-
(array[0]! << 24) |
|
|
98
|
-
(array[1]! << 16) |
|
|
99
|
-
(array[2]! << 8) |
|
|
100
|
-
array[3]!;
|
|
101
|
-
return littleEndian ? a : b;
|
|
102
|
-
},
|
|
103
|
-
serialize(dataView, offset, value, littleEndian) {
|
|
104
|
-
dataView.setInt32(offset, value, littleEndian);
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export class NumberFieldDefinition<
|
|
110
|
-
TType extends NumberFieldType = NumberFieldType,
|
|
111
|
-
TTypeScriptType = number,
|
|
112
|
-
> extends StructFieldDefinition<void, TTypeScriptType> {
|
|
113
|
-
readonly type: TType;
|
|
114
|
-
|
|
115
|
-
constructor(type: TType, typescriptType?: TTypeScriptType) {
|
|
116
|
-
void typescriptType;
|
|
117
|
-
super();
|
|
118
|
-
this.type = type;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
getSize(): number {
|
|
122
|
-
return this.type.size;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
create(
|
|
126
|
-
options: Readonly<StructOptions>,
|
|
127
|
-
struct: StructValue,
|
|
128
|
-
value: TTypeScriptType,
|
|
129
|
-
): NumberFieldValue<this> {
|
|
130
|
-
return new NumberFieldValue(this, options, struct, value);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
override deserialize(
|
|
134
|
-
options: Readonly<StructOptions>,
|
|
135
|
-
stream: ExactReadable,
|
|
136
|
-
struct: StructValue,
|
|
137
|
-
): NumberFieldValue<this>;
|
|
138
|
-
override deserialize(
|
|
139
|
-
options: Readonly<StructOptions>,
|
|
140
|
-
stream: AsyncExactReadable,
|
|
141
|
-
struct: StructValue,
|
|
142
|
-
): Promise<NumberFieldValue<this>>;
|
|
143
|
-
override deserialize(
|
|
144
|
-
options: Readonly<StructOptions>,
|
|
145
|
-
stream: ExactReadable | AsyncExactReadable,
|
|
146
|
-
struct: StructValue,
|
|
147
|
-
): ValueOrPromise<NumberFieldValue<this>> {
|
|
148
|
-
return SyncPromise.try(() => {
|
|
149
|
-
return stream.readExactly(this.getSize());
|
|
150
|
-
})
|
|
151
|
-
.then((array) => {
|
|
152
|
-
const value = this.type.deserialize(
|
|
153
|
-
array,
|
|
154
|
-
options.littleEndian,
|
|
155
|
-
);
|
|
156
|
-
return this.create(options, struct, value as any);
|
|
157
|
-
})
|
|
158
|
-
.valueOrPromise();
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export class NumberFieldValue<
|
|
163
|
-
TDefinition extends NumberFieldDefinition<NumberFieldType, any>,
|
|
164
|
-
> extends StructFieldValue<TDefinition> {
|
|
165
|
-
serialize(dataView: DataView, offset: number): void {
|
|
166
|
-
this.definition.type.serialize(
|
|
167
|
-
dataView,
|
|
168
|
-
offset,
|
|
169
|
-
this.value,
|
|
170
|
-
this.options.littleEndian,
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
}
|