@yume-chan/struct 0.0.24 → 2.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.
Files changed (150) hide show
  1. package/CHANGELOG.md +40 -1
  2. package/LICENSE +1 -1
  3. package/README.md +85 -714
  4. package/esm/bipedal.d.ts +6 -0
  5. package/esm/bipedal.d.ts.map +1 -0
  6. package/esm/bipedal.js +38 -0
  7. package/esm/bipedal.js.map +1 -0
  8. package/esm/buffer.d.ts +30 -0
  9. package/esm/buffer.d.ts.map +1 -0
  10. package/esm/buffer.js +170 -0
  11. package/esm/buffer.js.map +1 -0
  12. package/esm/concat.d.ts +13 -0
  13. package/esm/concat.d.ts.map +1 -0
  14. package/esm/concat.js +10 -0
  15. package/esm/concat.js.map +1 -0
  16. package/esm/extend.d.ts +6 -0
  17. package/esm/extend.d.ts.map +1 -0
  18. package/esm/extend.js +10 -0
  19. package/esm/extend.js.map +1 -0
  20. package/esm/field/factory.d.ts +13 -0
  21. package/esm/field/factory.d.ts.map +1 -0
  22. package/esm/field/factory.js +21 -0
  23. package/esm/field/factory.js.map +1 -0
  24. package/esm/field/index.d.ts +4 -0
  25. package/esm/field/index.d.ts.map +1 -0
  26. package/esm/field/index.js +4 -0
  27. package/esm/field/index.js.map +1 -0
  28. package/esm/field/serialize.d.ts +8 -0
  29. package/esm/field/serialize.d.ts.map +1 -0
  30. package/esm/field/serialize.js +33 -0
  31. package/esm/field/serialize.js.map +1 -0
  32. package/esm/field/types.d.ts +33 -0
  33. package/esm/field/types.d.ts.map +1 -0
  34. package/esm/field/types.js +2 -0
  35. package/esm/field/types.js.map +1 -0
  36. package/esm/index.d.ts +9 -4
  37. package/esm/index.d.ts.map +1 -1
  38. package/esm/index.js +9 -4
  39. package/esm/index.js.map +1 -1
  40. package/esm/number.d.ts +13 -0
  41. package/esm/number.d.ts.map +1 -0
  42. package/esm/number.js +57 -0
  43. package/esm/number.js.map +1 -0
  44. package/esm/{basic/stream.d.ts → readable.d.ts} +9 -3
  45. package/esm/readable.d.ts.map +1 -0
  46. package/esm/readable.js +26 -0
  47. package/esm/readable.js.map +1 -0
  48. package/esm/string.d.ts +16 -0
  49. package/esm/string.d.ts.map +1 -0
  50. package/esm/string.js +13 -0
  51. package/esm/string.js.map +1 -0
  52. package/esm/struct.d.ts +22 -140
  53. package/esm/struct.d.ts.map +1 -1
  54. package/esm/struct.js +109 -227
  55. package/esm/struct.js.map +1 -1
  56. package/esm/types.d.ts +20 -0
  57. package/esm/types.d.ts.map +1 -0
  58. package/esm/types.js +2 -0
  59. package/esm/types.js.map +1 -0
  60. package/esm/utils.d.ts +10 -45
  61. package/esm/utils.d.ts.map +1 -1
  62. package/esm/utils.js +8 -9
  63. package/esm/utils.js.map +1 -1
  64. package/package.json +11 -12
  65. package/src/bipedal.ts +65 -0
  66. package/src/buffer.ts +334 -0
  67. package/src/concat.ts +78 -0
  68. package/src/extend.ts +37 -0
  69. package/src/field/factory.ts +64 -0
  70. package/src/field/index.ts +3 -0
  71. package/src/field/serialize.ts +58 -0
  72. package/src/field/types.ts +48 -0
  73. package/src/index.ts +9 -4
  74. package/src/number.ts +134 -0
  75. package/src/{basic/stream.ts → readable.ts} +31 -4
  76. package/src/string.ts +41 -0
  77. package/src/struct.ts +200 -670
  78. package/src/types.ts +37 -0
  79. package/src/utils.ts +14 -67
  80. package/tsconfig.build.json +6 -1
  81. package/tsconfig.build.tsbuildinfo +1 -1
  82. package/CHANGELOG.json +0 -173
  83. package/esm/basic/definition.d.ts +0 -44
  84. package/esm/basic/definition.d.ts.map +0 -1
  85. package/esm/basic/definition.js +0 -24
  86. package/esm/basic/definition.js.map +0 -1
  87. package/esm/basic/field-value.d.ts +0 -39
  88. package/esm/basic/field-value.d.ts.map +0 -1
  89. package/esm/basic/field-value.js +0 -46
  90. package/esm/basic/field-value.js.map +0 -1
  91. package/esm/basic/index.d.ts +0 -6
  92. package/esm/basic/index.d.ts.map +0 -1
  93. package/esm/basic/index.js +0 -6
  94. package/esm/basic/index.js.map +0 -1
  95. package/esm/basic/options.d.ts +0 -10
  96. package/esm/basic/options.d.ts.map +0 -1
  97. package/esm/basic/options.js +0 -4
  98. package/esm/basic/options.js.map +0 -1
  99. package/esm/basic/stream.d.ts.map +0 -1
  100. package/esm/basic/stream.js +0 -8
  101. package/esm/basic/stream.js.map +0 -1
  102. package/esm/basic/struct-value.d.ts +0 -30
  103. package/esm/basic/struct-value.d.ts.map +0 -1
  104. package/esm/basic/struct-value.js +0 -62
  105. package/esm/basic/struct-value.js.map +0 -1
  106. package/esm/sync-promise.d.ts +0 -13
  107. package/esm/sync-promise.d.ts.map +0 -1
  108. package/esm/sync-promise.js +0 -71
  109. package/esm/sync-promise.js.map +0 -1
  110. package/esm/types/bigint.d.ts +0 -25
  111. package/esm/types/bigint.d.ts.map +0 -1
  112. package/esm/types/bigint.js +0 -46
  113. package/esm/types/bigint.js.map +0 -1
  114. package/esm/types/buffer/base.d.ts +0 -69
  115. package/esm/types/buffer/base.d.ts.map +0 -1
  116. package/esm/types/buffer/base.js +0 -100
  117. package/esm/types/buffer/base.js.map +0 -1
  118. package/esm/types/buffer/fixed-length.d.ts +0 -9
  119. package/esm/types/buffer/fixed-length.d.ts.map +0 -1
  120. package/esm/types/buffer/fixed-length.js +0 -7
  121. package/esm/types/buffer/fixed-length.js.map +0 -1
  122. package/esm/types/buffer/index.d.ts +0 -4
  123. package/esm/types/buffer/index.d.ts.map +0 -1
  124. package/esm/types/buffer/index.js +0 -4
  125. package/esm/types/buffer/index.js.map +0 -1
  126. package/esm/types/buffer/variable-length.d.ts +0 -46
  127. package/esm/types/buffer/variable-length.d.ts.map +0 -1
  128. package/esm/types/buffer/variable-length.js +0 -87
  129. package/esm/types/buffer/variable-length.js.map +0 -1
  130. package/esm/types/index.d.ts +0 -4
  131. package/esm/types/index.d.ts.map +0 -1
  132. package/esm/types/index.js +0 -4
  133. package/esm/types/index.js.map +0 -1
  134. package/esm/types/number.d.ts +0 -28
  135. package/esm/types/number.d.ts.map +0 -1
  136. package/esm/types/number.js +0 -99
  137. package/esm/types/number.js.map +0 -1
  138. package/src/basic/definition.ts +0 -68
  139. package/src/basic/field-value.ts +0 -75
  140. package/src/basic/index.ts +0 -5
  141. package/src/basic/options.ts +0 -19
  142. package/src/basic/struct-value.ts +0 -85
  143. package/src/sync-promise.ts +0 -134
  144. package/src/types/bigint.ts +0 -124
  145. package/src/types/buffer/base.ts +0 -198
  146. package/src/types/buffer/fixed-length.ts +0 -22
  147. package/src/types/buffer/index.ts +0 -3
  148. package/src/types/buffer/variable-length.ts +0 -198
  149. package/src/types/index.ts +0 -3
  150. package/src/types/number.ts +0 -166
@@ -1,124 +0,0 @@
1
- import {
2
- getInt64,
3
- getUint64,
4
- setInt64,
5
- setUint64,
6
- } from "@yume-chan/no-data-view";
7
-
8
- import type {
9
- AsyncExactReadable,
10
- ExactReadable,
11
- StructOptions,
12
- StructValue,
13
- } from "../basic/index.js";
14
- import { StructFieldDefinition, StructFieldValue } from "../basic/index.js";
15
- import { SyncPromise } from "../sync-promise.js";
16
- import type { ValueOrPromise } from "../utils.js";
17
-
18
- export type BigIntDeserializer = (
19
- array: Uint8Array,
20
- byteOffset: number,
21
- littleEndian: boolean,
22
- ) => bigint;
23
-
24
- export type BigIntSerializer = (
25
- array: Uint8Array,
26
- byteOffset: number,
27
- value: bigint,
28
- littleEndian: boolean,
29
- ) => void;
30
-
31
- export class BigIntFieldVariant {
32
- readonly TTypeScriptType!: bigint;
33
-
34
- readonly size: number;
35
-
36
- readonly deserialize: BigIntDeserializer;
37
-
38
- readonly serialize: BigIntSerializer;
39
-
40
- constructor(
41
- size: number,
42
- deserialize: BigIntDeserializer,
43
- serialize: BigIntSerializer,
44
- ) {
45
- this.size = size;
46
- this.deserialize = deserialize;
47
- this.serialize = serialize;
48
- }
49
-
50
- static readonly Int64 = new BigIntFieldVariant(8, getInt64, setInt64);
51
-
52
- static readonly Uint64 = new BigIntFieldVariant(8, getUint64, setUint64);
53
- }
54
-
55
- export class BigIntFieldDefinition<
56
- TVariant extends BigIntFieldVariant = BigIntFieldVariant,
57
- TTypeScriptType = TVariant["TTypeScriptType"],
58
- > extends StructFieldDefinition<void, TTypeScriptType> {
59
- readonly variant: TVariant;
60
-
61
- constructor(variant: TVariant, typescriptType?: TTypeScriptType) {
62
- void typescriptType;
63
- super();
64
- this.variant = variant;
65
- }
66
-
67
- getSize(): number {
68
- return this.variant.size;
69
- }
70
-
71
- create(
72
- options: Readonly<StructOptions>,
73
- struct: StructValue,
74
- value: TTypeScriptType,
75
- ): BigIntFieldValue<this> {
76
- return new BigIntFieldValue(this, options, struct, value);
77
- }
78
-
79
- override deserialize(
80
- options: Readonly<StructOptions>,
81
- stream: ExactReadable,
82
- struct: StructValue,
83
- ): BigIntFieldValue<this>;
84
- override deserialize(
85
- options: Readonly<StructOptions>,
86
- stream: AsyncExactReadable,
87
- struct: StructValue,
88
- ): Promise<BigIntFieldValue<this>>;
89
- override deserialize(
90
- options: Readonly<StructOptions>,
91
- stream: ExactReadable | AsyncExactReadable,
92
- struct: StructValue,
93
- ): ValueOrPromise<BigIntFieldValue<this>> {
94
- return SyncPromise.try(() => {
95
- return stream.readExactly(this.getSize());
96
- })
97
- .then((array) => {
98
- const value = this.variant.deserialize(
99
- array,
100
- 0,
101
- options.littleEndian,
102
- );
103
- return this.create(options, struct, value as never);
104
- })
105
- .valueOrPromise();
106
- }
107
- }
108
-
109
- export class BigIntFieldValue<
110
- TDefinition extends BigIntFieldDefinition<BigIntFieldVariant, unknown>,
111
- > extends StructFieldValue<TDefinition> {
112
- override serialize(
113
- dataView: DataView,
114
- array: Uint8Array,
115
- offset: number,
116
- ): void {
117
- this.definition.variant.serialize(
118
- array,
119
- offset,
120
- this.value as never,
121
- this.options.littleEndian,
122
- );
123
- }
124
- }
@@ -1,198 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import type {
3
- AsyncExactReadable,
4
- ExactReadable,
5
- StructOptions,
6
- StructValue,
7
- } from "../../basic/index.js";
8
- import { StructFieldDefinition, StructFieldValue } from "../../basic/index.js";
9
- import { SyncPromise } from "../../sync-promise.js";
10
- import type { ValueOrPromise } from "../../utils.js";
11
- import { decodeUtf8, encodeUtf8 } from "../../utils.js";
12
-
13
- /**
14
- * A converter for buffer-like fields.
15
- * It converts `Uint8Array`s to custom-typed values when deserializing,
16
- * and convert values back to `Uint8Array`s when serializing.
17
- *
18
- * @template TValue The type of the value that the converter converts to/from `Uint8Array`.
19
- * @template TTypeScriptType Optionally another type to refine `TValue`.
20
- * For example, `TValue` is `string`, and `TTypeScriptType` is `"foo" | "bar"`.
21
- * `TValue` is specified by the developer when creating an converter implementation,
22
- * `TTypeScriptType` is specified by the user when creating a field.
23
- */
24
- export abstract class BufferFieldConverter<
25
- TValue = unknown,
26
- TTypeScriptType = TValue,
27
- > {
28
- readonly TTypeScriptType!: TTypeScriptType;
29
-
30
- /**
31
- * When implemented in derived classes, converts the custom `value` to an `Uint8Array`
32
- *
33
- * This function should be "pure", i.e.,
34
- * same `value` should always be converted to `Uint8Array`s that have same content.
35
- */
36
- abstract toBuffer(value: TValue): Uint8Array;
37
-
38
- /** When implemented in derived classes, converts the `Uint8Array` to a custom value */
39
- abstract toValue(array: Uint8Array): TValue;
40
-
41
- /**
42
- * When implemented in derived classes, gets the size in byte of the custom `value`.
43
- *
44
- * If the size can't be determined without first converting the `value` back to an `Uint8Array`,
45
- * the implementer should return `undefined`. In which case, the caller will call `toBuffer` to
46
- * convert the value to a `Uint8Array`, then read the length of the `Uint8Array`. The caller can
47
- * cache the result so the serialization process doesn't need to call `toBuffer` again.
48
- */
49
- abstract getSize(value: TValue): number | undefined;
50
- }
51
-
52
- /** An identity converter, doesn't convert to anything else. */
53
- export class Uint8ArrayBufferFieldConverter<
54
- TTypeScriptType = Uint8Array,
55
- > extends BufferFieldConverter<Uint8Array, TTypeScriptType> {
56
- static readonly Instance = new Uint8ArrayBufferFieldConverter();
57
-
58
- protected constructor() {
59
- super();
60
- }
61
-
62
- toBuffer(value: Uint8Array): Uint8Array {
63
- return value;
64
- }
65
-
66
- toValue(buffer: Uint8Array): Uint8Array {
67
- return buffer;
68
- }
69
-
70
- getSize(value: Uint8Array): number {
71
- return value.length;
72
- }
73
- }
74
-
75
- /** An `BufferFieldSubType` that converts between `Uint8Array` and `string` */
76
- export class StringBufferFieldConverter<
77
- TTypeScriptType = string,
78
- > extends BufferFieldConverter<string, TTypeScriptType> {
79
- static readonly Instance = new StringBufferFieldConverter();
80
-
81
- toBuffer(value: string): Uint8Array {
82
- return encodeUtf8(value);
83
- }
84
-
85
- toValue(array: Uint8Array): string {
86
- return decodeUtf8(array);
87
- }
88
-
89
- getSize(): number | undefined {
90
- // See the note in `BufferFieldConverter.getSize`
91
- return undefined;
92
- }
93
- }
94
-
95
- export const EMPTY_UINT8_ARRAY = new Uint8Array(0);
96
-
97
- export abstract class BufferLikeFieldDefinition<
98
- TConverter extends BufferFieldConverter<
99
- unknown,
100
- unknown
101
- > = BufferFieldConverter<unknown, unknown>,
102
- TOptions = void,
103
- TOmitInitKey extends PropertyKey = never,
104
- TTypeScriptType = TConverter["TTypeScriptType"],
105
- > extends StructFieldDefinition<TOptions, TTypeScriptType, TOmitInitKey> {
106
- readonly converter: TConverter;
107
- readonly TTypeScriptType!: TTypeScriptType;
108
-
109
- constructor(converter: TConverter, options: TOptions) {
110
- super(options);
111
- this.converter = converter;
112
- }
113
-
114
- protected getDeserializeSize(struct: StructValue): number {
115
- void struct;
116
- return this.getSize();
117
- }
118
-
119
- /**
120
- * When implemented in derived classes, creates a `StructFieldValue` for the current field definition.
121
- */
122
- create(
123
- options: Readonly<StructOptions>,
124
- struct: StructValue,
125
- value: TTypeScriptType,
126
- array?: Uint8Array,
127
- ): BufferLikeFieldValue<this> {
128
- return new BufferLikeFieldValue(this, options, struct, value, array);
129
- }
130
-
131
- override deserialize(
132
- options: Readonly<StructOptions>,
133
- stream: ExactReadable,
134
- struct: StructValue,
135
- ): BufferLikeFieldValue<this>;
136
- override deserialize(
137
- options: Readonly<StructOptions>,
138
- stream: AsyncExactReadable,
139
- struct: StructValue,
140
- ): Promise<BufferLikeFieldValue<this>>;
141
- override deserialize(
142
- options: Readonly<StructOptions>,
143
- stream: ExactReadable | AsyncExactReadable,
144
- struct: StructValue,
145
- ): ValueOrPromise<BufferLikeFieldValue<this>> {
146
- return SyncPromise.try(() => {
147
- const size = this.getDeserializeSize(struct);
148
- if (size === 0) {
149
- return EMPTY_UINT8_ARRAY;
150
- } else {
151
- return stream.readExactly(size);
152
- }
153
- })
154
- .then((array) => {
155
- const value = this.converter.toValue(array) as TTypeScriptType;
156
- return this.create(options, struct, value, array);
157
- })
158
- .valueOrPromise();
159
- }
160
- }
161
-
162
- export class BufferLikeFieldValue<
163
- TDefinition extends BufferLikeFieldDefinition<
164
- BufferFieldConverter<unknown, unknown>,
165
- any,
166
- any,
167
- any
168
- >,
169
- > extends StructFieldValue<TDefinition> {
170
- protected array: Uint8Array | undefined;
171
-
172
- constructor(
173
- definition: TDefinition,
174
- options: Readonly<StructOptions>,
175
- struct: StructValue,
176
- value: TDefinition["TTypeScriptType"],
177
- array?: Uint8Array,
178
- ) {
179
- super(definition, options, struct, value);
180
- this.array = array;
181
- }
182
-
183
- override set(value: TDefinition["TValue"]): void {
184
- super.set(value);
185
- // When value changes, clear the cached `array`
186
- // It will be lazily calculated in `serialize()`
187
- this.array = undefined;
188
- }
189
-
190
- override serialize(
191
- dataView: DataView,
192
- array: Uint8Array,
193
- offset: number,
194
- ): void {
195
- this.array ??= this.definition.converter.toBuffer(this.value);
196
- array.set(this.array, offset);
197
- }
198
- }
@@ -1,22 +0,0 @@
1
- import type { BufferFieldConverter } from "./base.js";
2
- import { BufferLikeFieldDefinition } from "./base.js";
3
-
4
- export interface FixedLengthBufferLikeFieldOptions {
5
- length: number;
6
- }
7
-
8
- export class FixedLengthBufferLikeFieldDefinition<
9
- TConverter extends BufferFieldConverter = BufferFieldConverter,
10
- TOptions extends
11
- FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
12
- TTypeScriptType = TConverter["TTypeScriptType"],
13
- > extends BufferLikeFieldDefinition<
14
- TConverter,
15
- TOptions,
16
- never,
17
- TTypeScriptType
18
- > {
19
- getSize(): number {
20
- return this.options.length;
21
- }
22
- }
@@ -1,3 +0,0 @@
1
- export * from "./base.js";
2
- export * from "./fixed-length.js";
3
- export * from "./variable-length.js";
@@ -1,198 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
2
- /* eslint-disable @typescript-eslint/no-explicit-any */
3
-
4
- import type {
5
- StructFieldDefinition,
6
- StructOptions,
7
- StructValue,
8
- } from "../../basic/index.js";
9
- import { StructFieldValue } from "../../basic/index.js";
10
- import type { KeysOfType } from "../../utils.js";
11
-
12
- import type { BufferFieldConverter } from "./base.js";
13
- import { BufferLikeFieldDefinition, BufferLikeFieldValue } from "./base.js";
14
-
15
- export type LengthField<TFields> = KeysOfType<TFields, number | string>;
16
-
17
- export interface VariableLengthBufferLikeFieldOptions<
18
- TFields extends object = object,
19
- TLengthField extends LengthField<TFields> = any,
20
- > {
21
- /**
22
- * The name of the field that contains the length of the buffer.
23
- *
24
- * This field must be a `number` or `string` (can't be `bigint`) field.
25
- */
26
- lengthField: TLengthField;
27
-
28
- /**
29
- * If the `lengthField` refers to a string field,
30
- * what radix to use when converting the string to a number.
31
- *
32
- * @default 10
33
- */
34
- lengthFieldRadix?: number;
35
- }
36
-
37
- export class VariableLengthBufferLikeFieldDefinition<
38
- TConverter extends BufferFieldConverter = BufferFieldConverter,
39
- TOptions extends
40
- VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions,
41
- TTypeScriptType = TConverter["TTypeScriptType"],
42
- > extends BufferLikeFieldDefinition<
43
- TConverter,
44
- TOptions,
45
- TOptions["lengthField"],
46
- TTypeScriptType
47
- > {
48
- getSize(): number {
49
- return 0;
50
- }
51
-
52
- protected override getDeserializeSize(struct: StructValue) {
53
- let value = struct.value[this.options.lengthField] as number | string;
54
- if (typeof value === "string") {
55
- value = Number.parseInt(value, this.options.lengthFieldRadix ?? 10);
56
- }
57
- return value;
58
- }
59
-
60
- override create(
61
- options: Readonly<StructOptions>,
62
- struct: StructValue,
63
- value: TTypeScriptType,
64
- array?: Uint8Array,
65
- ): VariableLengthBufferLikeStructFieldValue<this> {
66
- return new VariableLengthBufferLikeStructFieldValue(
67
- this,
68
- options,
69
- struct,
70
- value,
71
- array,
72
- );
73
- }
74
- }
75
-
76
- export class VariableLengthBufferLikeStructFieldValue<
77
- TDefinition extends
78
- VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition,
79
- > extends BufferLikeFieldValue<TDefinition> {
80
- protected length: number | undefined;
81
-
82
- protected lengthFieldValue: VariableLengthBufferLikeFieldLengthValue;
83
-
84
- constructor(
85
- definition: TDefinition,
86
- options: Readonly<StructOptions>,
87
- struct: StructValue,
88
- value: TDefinition["TValue"],
89
- array?: Uint8Array,
90
- ) {
91
- super(definition, options, struct, value, array);
92
-
93
- if (array) {
94
- this.length = array.length;
95
- }
96
-
97
- // Patch the associated length field.
98
- const lengthField = this.definition.options.lengthField as PropertyKey;
99
-
100
- const originalValue = struct.get(lengthField);
101
- this.lengthFieldValue = new VariableLengthBufferLikeFieldLengthValue(
102
- originalValue,
103
- this,
104
- );
105
- struct.set(lengthField, this.lengthFieldValue);
106
- }
107
-
108
- #tryGetSize() {
109
- const length = this.definition.converter.getSize(this.value);
110
- if (length !== undefined && length < 0) {
111
- throw new Error("Invalid length");
112
- }
113
- return length;
114
- }
115
-
116
- override getSize(): number {
117
- if (this.length === undefined) {
118
- // first try to get the size from the converter
119
- this.length = this.#tryGetSize();
120
- }
121
-
122
- if (this.length === undefined) {
123
- // The converter doesn't know the size, so convert the value to a buffer to get its size
124
- this.array = this.definition.converter.toBuffer(this.value);
125
- this.length = this.array.length;
126
- }
127
-
128
- return this.length;
129
- }
130
-
131
- override set(value: unknown) {
132
- super.set(value);
133
- this.array = undefined;
134
- this.length = undefined;
135
- }
136
- }
137
-
138
- // Not using `VariableLengthBufferLikeStructFieldValue` directly makes writing tests much easier...
139
- type VariableLengthBufferLikeFieldValueLike = StructFieldValue<
140
- StructFieldDefinition<
141
- VariableLengthBufferLikeFieldOptions<any, any>,
142
- any,
143
- any
144
- >
145
- >;
146
-
147
- export class VariableLengthBufferLikeFieldLengthValue extends StructFieldValue<
148
- StructFieldDefinition<unknown, unknown, PropertyKey>
149
- > {
150
- protected originalValue: StructFieldValue<
151
- StructFieldDefinition<unknown, unknown, PropertyKey>
152
- >;
153
-
154
- protected bufferValue: VariableLengthBufferLikeFieldValueLike;
155
-
156
- constructor(
157
- originalValue: StructFieldValue<
158
- StructFieldDefinition<unknown, unknown, PropertyKey>
159
- >,
160
- bufferValue: VariableLengthBufferLikeFieldValueLike,
161
- ) {
162
- super(
163
- originalValue.definition,
164
- originalValue.options,
165
- originalValue.struct,
166
- 0,
167
- );
168
- this.originalValue = originalValue;
169
- this.bufferValue = bufferValue;
170
- }
171
-
172
- override getSize() {
173
- return this.originalValue.getSize();
174
- }
175
-
176
- override get() {
177
- let value: string | number = this.bufferValue.getSize();
178
-
179
- const originalValue = this.originalValue.get();
180
- if (typeof originalValue === "string") {
181
- value = value.toString(
182
- this.bufferValue.definition.options.lengthFieldRadix ?? 10,
183
- );
184
- }
185
-
186
- return value;
187
- }
188
-
189
- override set() {
190
- // Ignore setting
191
- // It will always be in sync with the buffer size
192
- }
193
-
194
- serialize(dataView: DataView, array: Uint8Array, offset: number) {
195
- this.originalValue.set(this.get());
196
- this.originalValue.serialize(dataView, array, offset);
197
- }
198
- }
@@ -1,3 +0,0 @@
1
- export * from "./bigint.js";
2
- export * from "./buffer/index.js";
3
- export * from "./number.js";