@yume-chan/struct 0.0.5 → 0.0.10

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 (121) hide show
  1. package/CHANGELOG.json +23 -0
  2. package/CHANGELOG.md +16 -0
  3. package/LICENSE +21 -21
  4. package/README.md +720 -643
  5. package/dts/basic/context.d.ts +10 -17
  6. package/dts/basic/context.d.ts.map +1 -1
  7. package/dts/basic/definition.d.ts +23 -23
  8. package/dts/basic/definition.d.ts.map +1 -1
  9. package/dts/basic/field-value.d.ts +38 -0
  10. package/dts/basic/field-value.d.ts.map +1 -0
  11. package/dts/basic/index.d.ts +2 -2
  12. package/dts/basic/index.d.ts.map +1 -1
  13. package/dts/basic/struct-value.d.ts +24 -0
  14. package/dts/basic/struct-value.d.ts.map +1 -0
  15. package/dts/index.d.ts +1 -0
  16. package/dts/index.d.ts.map +1 -1
  17. package/dts/struct.d.ts +44 -47
  18. package/dts/struct.d.ts.map +1 -1
  19. package/dts/syncbird.d.ts +20 -0
  20. package/dts/syncbird.d.ts.map +1 -0
  21. package/dts/types/array-buffer.d.ts +22 -20
  22. package/dts/types/array-buffer.d.ts.map +1 -1
  23. package/dts/types/fixed-length-array-buffer.d.ts +1 -3
  24. package/dts/types/fixed-length-array-buffer.d.ts.map +1 -1
  25. package/dts/types/index.d.ts +0 -1
  26. package/dts/types/index.d.ts.map +1 -1
  27. package/dts/types/number.d.ts +11 -10
  28. package/dts/types/number.d.ts.map +1 -1
  29. package/dts/types/variable-length-array-buffer.d.ts +21 -19
  30. package/dts/types/variable-length-array-buffer.d.ts.map +1 -1
  31. package/dts/{types/utils.d.ts → utils.d.ts} +3 -0
  32. package/dts/utils.d.ts.map +1 -0
  33. package/esm/basic/context.js +1 -1
  34. package/esm/basic/context.js.map +1 -1
  35. package/esm/basic/definition.js +9 -11
  36. package/esm/basic/definition.js.map +1 -1
  37. package/esm/basic/field-value.js +35 -0
  38. package/esm/basic/field-value.js.map +1 -0
  39. package/esm/basic/index.js +2 -2
  40. package/esm/basic/index.js.map +1 -1
  41. package/esm/basic/struct-value.js +36 -0
  42. package/esm/basic/struct-value.js.map +1 -0
  43. package/esm/index.js +1 -0
  44. package/esm/index.js.map +1 -1
  45. package/esm/struct.js +103 -176
  46. package/esm/struct.js.map +1 -1
  47. package/esm/syncbird.js +25 -0
  48. package/esm/syncbird.js.map +1 -0
  49. package/esm/types/array-buffer.js +83 -107
  50. package/esm/types/array-buffer.js.map +1 -1
  51. package/esm/types/fixed-length-array-buffer.js +5 -15
  52. package/esm/types/fixed-length-array-buffer.js.map +1 -1
  53. package/esm/types/index.js +0 -1
  54. package/esm/types/index.js.map +1 -1
  55. package/esm/types/number.js +35 -53
  56. package/esm/types/number.js.map +1 -1
  57. package/esm/types/variable-length-array-buffer.js +57 -68
  58. package/esm/types/variable-length-array-buffer.js.map +1 -1
  59. package/esm/utils.js +17 -0
  60. package/esm/utils.js.map +1 -0
  61. package/package.json +47 -50
  62. package/src/basic/context.ts +32 -40
  63. package/src/basic/definition.ts +72 -69
  64. package/src/basic/{runtime-value.ts → field-value.ts} +67 -72
  65. package/src/basic/index.ts +4 -4
  66. package/src/basic/struct-value.ts +39 -0
  67. package/src/index.ts +5 -4
  68. package/src/struct.ts +595 -579
  69. package/src/syncbird.ts +53 -0
  70. package/src/types/array-buffer.ts +194 -171
  71. package/src/types/fixed-length-array-buffer.ts +17 -27
  72. package/src/types/index.ts +4 -5
  73. package/src/types/number.ts +114 -101
  74. package/src/types/variable-length-array-buffer.ts +142 -121
  75. package/src/{types/utils.ts → utils.ts} +62 -47
  76. package/cjs/basic/context.js +0 -7
  77. package/cjs/basic/context.js.map +0 -1
  78. package/cjs/basic/definition.js +0 -26
  79. package/cjs/basic/definition.js.map +0 -1
  80. package/cjs/basic/index.js +0 -8
  81. package/cjs/basic/index.js.map +0 -1
  82. package/cjs/basic/runtime-object.js +0 -34
  83. package/cjs/basic/runtime-object.js.map +0 -1
  84. package/cjs/basic/runtime-value.js +0 -41
  85. package/cjs/basic/runtime-value.js.map +0 -1
  86. package/cjs/index.js +0 -10
  87. package/cjs/index.js.map +0 -1
  88. package/cjs/struct.js +0 -259
  89. package/cjs/struct.js.map +0 -1
  90. package/cjs/types/array-buffer.js +0 -137
  91. package/cjs/types/array-buffer.js.map +0 -1
  92. package/cjs/types/fixed-length-array-buffer.js +0 -21
  93. package/cjs/types/fixed-length-array-buffer.js.map +0 -1
  94. package/cjs/types/index.js +0 -9
  95. package/cjs/types/index.js.map +0 -1
  96. package/cjs/types/number.js +0 -68
  97. package/cjs/types/number.js.map +0 -1
  98. package/cjs/types/utils.js +0 -11
  99. package/cjs/types/utils.js.map +0 -1
  100. package/cjs/types/variable-length-array-buffer.js +0 -85
  101. package/cjs/types/variable-length-array-buffer.js.map +0 -1
  102. package/dts/basic/runtime-object.d.ts +0 -17
  103. package/dts/basic/runtime-object.d.ts.map +0 -1
  104. package/dts/basic/runtime-value.d.ts +0 -39
  105. package/dts/basic/runtime-value.d.ts.map +0 -1
  106. package/dts/types/utils.d.ts.map +0 -1
  107. package/esm/basic/runtime-object.js +0 -28
  108. package/esm/basic/runtime-object.js.map +0 -1
  109. package/esm/basic/runtime-value.js +0 -38
  110. package/esm/basic/runtime-value.js.map +0 -1
  111. package/esm/types/utils.js +0 -7
  112. package/esm/types/utils.js.map +0 -1
  113. package/src/basic/context.spec.ts +0 -9
  114. package/src/basic/definition.spec.ts +0 -26
  115. package/src/basic/runtime-object.spec.ts +0 -38
  116. package/src/basic/runtime-object.ts +0 -36
  117. package/src/basic/runtime-value.spec.ts +0 -71
  118. package/src/types/array-buffer.spec.ts +0 -88
  119. package/src/types/number.spec.ts +0 -262
  120. package/tsconfig.cjs.json +0 -11
  121. package/tsconfig.esm.json +0 -15
@@ -1,101 +1,114 @@
1
- import { FieldDefinition, FieldRuntimeValue, StructDeserializationContext, StructOptions, StructSerializationContext } from '../basic';
2
-
3
- export type DataViewGetters =
4
- { [TKey in keyof DataView]: TKey extends `get${string}` ? TKey : never }[keyof DataView];
5
-
6
- export type DataViewSetters =
7
- { [TKey in keyof DataView]: TKey extends `set${string}` ? TKey : never }[keyof DataView];
8
-
9
- export class NumberFieldType<TTypeScriptType extends number | bigint = number | bigint> {
10
- public static readonly Int8 = new NumberFieldType<number>(1, 'getInt8', 'setInt8');
11
-
12
- public static readonly Uint8 = new NumberFieldType<number>(1, 'getUint8', 'setUint8');
13
-
14
- public static readonly Int16 = new NumberFieldType<number>(2, 'getInt16', 'setInt16');
15
-
16
- public static readonly Uint16 = new NumberFieldType<number>(2, 'getUint16', 'setUint16');
17
-
18
- public static readonly Int32 = new NumberFieldType<number>(4, 'getInt32', 'setInt32');
19
-
20
- public static readonly Uint32 = new NumberFieldType<number>(4, 'getUint32', 'setUint32');
21
-
22
- public static readonly Int64 = new NumberFieldType<bigint>(8, 'getBigInt64', 'setBigInt64');
23
-
24
- public static readonly Uint64 = new NumberFieldType<bigint>(8, 'getBigUint64', 'setBigUint64');
25
-
26
- public readonly valueType!: TTypeScriptType;
27
-
28
- public readonly size: number;
29
-
30
- public readonly dataViewGetter: DataViewGetters;
31
-
32
- public readonly dataViewSetter: DataViewSetters;
33
-
34
- public constructor(
35
- size: number,
36
- dataViewGetter: DataViewGetters,
37
- dataViewSetter: DataViewSetters
38
- ) {
39
- this.size = size;
40
- this.dataViewGetter = dataViewGetter;
41
- this.dataViewSetter = dataViewSetter;
42
- }
43
- }
44
-
45
- export class NumberFieldDefinition<
46
- TType extends NumberFieldType = NumberFieldType,
47
- TTypeScriptType = TType['valueType'],
48
- > extends FieldDefinition<
49
- void,
50
- TTypeScriptType
51
- > {
52
- public readonly type: TType;
53
-
54
- public constructor(type: TType, _typescriptType?: TTypeScriptType) {
55
- super();
56
- this.type = type;
57
- }
58
-
59
- public getSize(): number {
60
- return this.type.size;
61
- }
62
-
63
- public async deserialize(
64
- options: Readonly<StructOptions>,
65
- context: StructDeserializationContext,
66
- object: any,
67
- ): Promise<NumberFieldRuntimeValue<TType, TTypeScriptType>> {
68
- const buffer = await context.read(this.getSize());
69
- const view = new DataView(buffer);
70
- const value = view[this.type.dataViewGetter](
71
- 0,
72
- options.littleEndian
73
- ) as any;
74
- return this.createValue(options, context, object, value);
75
- }
76
-
77
- public createValue(
78
- options: Readonly<StructOptions>,
79
- context: StructSerializationContext,
80
- object: any,
81
- value: TTypeScriptType,
82
- ): NumberFieldRuntimeValue<TType, TTypeScriptType> {
83
- return new NumberFieldRuntimeValue(this, options, context, object, value);
84
- }
85
- }
86
-
87
- export class NumberFieldRuntimeValue<
88
- TType extends NumberFieldType = NumberFieldType,
89
- TTypeScriptType = TType['valueType'],
90
- > extends FieldRuntimeValue<NumberFieldDefinition<TType, TTypeScriptType>> {
91
- public serialize(dataView: DataView, offset: number): void {
92
- // `setBigInt64` requires a `bigint` while others require `number`
93
- // So `dataView[DataViewSetters]` requires `bigint & number`
94
- // and that is, `never`
95
- (dataView[this.definition.type.dataViewSetter] as any)(
96
- offset,
97
- this.value!,
98
- this.options.littleEndian
99
- );
100
- }
101
- }
1
+ import { StructAsyncDeserializeStream, StructDeserializeStream, StructFieldDefinition, StructFieldValue, StructOptions, StructValue } from '../basic';
2
+ import { Syncbird } from "../syncbird";
3
+ import { ValueOrPromise } from "../utils";
4
+
5
+ export type DataViewGetters =
6
+ { [TKey in keyof DataView]: TKey extends `get${string}` ? TKey : never }[keyof DataView];
7
+
8
+ export type DataViewSetters =
9
+ { [TKey in keyof DataView]: TKey extends `set${string}` ? TKey : never }[keyof DataView];
10
+
11
+ export class NumberFieldType<TTypeScriptType extends number | bigint = number | bigint> {
12
+ public readonly TTypeScriptType!: TTypeScriptType;
13
+
14
+ public readonly size: number;
15
+
16
+ public readonly dataViewGetter: DataViewGetters;
17
+
18
+ public readonly dataViewSetter: DataViewSetters;
19
+
20
+ public constructor(
21
+ size: number,
22
+ dataViewGetter: DataViewGetters,
23
+ dataViewSetter: DataViewSetters
24
+ ) {
25
+ this.size = size;
26
+ this.dataViewGetter = dataViewGetter;
27
+ this.dataViewSetter = dataViewSetter;
28
+ }
29
+
30
+ public static readonly Int8 = new NumberFieldType<number>(1, 'getInt8', 'setInt8');
31
+
32
+ public static readonly Uint8 = new NumberFieldType<number>(1, 'getUint8', 'setUint8');
33
+
34
+ public static readonly Int16 = new NumberFieldType<number>(2, 'getInt16', 'setInt16');
35
+
36
+ public static readonly Uint16 = new NumberFieldType<number>(2, 'getUint16', 'setUint16');
37
+
38
+ public static readonly Int32 = new NumberFieldType<number>(4, 'getInt32', 'setInt32');
39
+
40
+ public static readonly Uint32 = new NumberFieldType<number>(4, 'getUint32', 'setUint32');
41
+
42
+ public static readonly Int64 = new NumberFieldType<bigint>(8, 'getBigInt64', 'setBigInt64');
43
+
44
+ public static readonly Uint64 = new NumberFieldType<bigint>(8, 'getBigUint64', 'setBigUint64');
45
+ }
46
+
47
+ export class NumberFieldDefinition<
48
+ TType extends NumberFieldType = NumberFieldType,
49
+ TTypeScriptType = TType['TTypeScriptType'],
50
+ > extends StructFieldDefinition<
51
+ void,
52
+ TTypeScriptType
53
+ > {
54
+ public readonly type: TType;
55
+
56
+ public constructor(type: TType, _typescriptType?: TTypeScriptType) {
57
+ super();
58
+ this.type = type;
59
+ }
60
+
61
+ public getSize(): number {
62
+ return this.type.size;
63
+ }
64
+
65
+ public create(
66
+ options: Readonly<StructOptions>,
67
+ struct: StructValue,
68
+ value: TTypeScriptType,
69
+ ): NumberFieldValue<this> {
70
+ return new NumberFieldValue(this, options, struct, value);
71
+ }
72
+
73
+ public override deserialize(
74
+ options: Readonly<StructOptions>,
75
+ stream: StructDeserializeStream,
76
+ struct: StructValue,
77
+ ): NumberFieldValue<this>;
78
+ public override deserialize(
79
+ options: Readonly<StructOptions>,
80
+ stream: StructAsyncDeserializeStream,
81
+ struct: StructValue,
82
+ ): Promise<NumberFieldValue<this>>;
83
+ public override deserialize(
84
+ options: Readonly<StructOptions>,
85
+ stream: StructDeserializeStream | StructAsyncDeserializeStream,
86
+ struct: StructValue,
87
+ ): ValueOrPromise<NumberFieldValue<this>> {
88
+ return Syncbird.try(() => {
89
+ return stream.read(this.getSize());
90
+ }).then(buffer => {
91
+ const view = new DataView(buffer);
92
+ const value = view[this.type.dataViewGetter](
93
+ 0,
94
+ options.littleEndian
95
+ ) as any;
96
+ return this.create(options, struct, value);
97
+ }).valueOrPromise();
98
+ }
99
+ }
100
+
101
+ export class NumberFieldValue<
102
+ TDefinition extends NumberFieldDefinition<NumberFieldType, any>,
103
+ > extends StructFieldValue<TDefinition> {
104
+ public serialize(dataView: DataView, offset: number): void {
105
+ // `setBigInt64` requires a `bigint` while others require `number`
106
+ // So `dataView[DataViewSetters]` requires `bigint & number`
107
+ // and that is, `never`
108
+ (dataView[this.definition.type.dataViewSetter] as any)(
109
+ offset,
110
+ this.value!,
111
+ this.options.littleEndian
112
+ );
113
+ }
114
+ }
@@ -1,121 +1,142 @@
1
- import { FieldRuntimeValue, getRuntimeValue, setRuntimeValue, StructOptions, StructSerializationContext } from '../basic';
2
- import { ArrayBufferLikeFieldDefinition, ArrayBufferLikeFieldRuntimeValue, ArrayBufferLikeFieldType } from './array-buffer';
3
- import { KeysOfType } from './utils';
4
-
5
- export interface VariableLengthArrayBufferLikeFieldOptions<
6
- TFields = object,
7
- TLengthField extends KeysOfType<TFields, number | string> = any,
8
- > {
9
- lengthField: TLengthField;
10
- }
11
-
12
- export class VariableLengthArrayBufferLikeFieldDefinition<
13
- TType extends ArrayBufferLikeFieldType = ArrayBufferLikeFieldType,
14
- TOptions extends VariableLengthArrayBufferLikeFieldOptions = VariableLengthArrayBufferLikeFieldOptions
15
- > extends ArrayBufferLikeFieldDefinition<
16
- TType,
17
- TOptions,
18
- TOptions['lengthField']
19
- > {
20
- public getSize(): number {
21
- return 0;
22
- }
23
-
24
- protected getDeserializeSize(object: any) {
25
- let value = object[this.options.lengthField] as number | string;
26
- if (typeof value === 'string') {
27
- value = Number.parseInt(value, 10);
28
- }
29
- return value;
30
- }
31
-
32
- public createValue(
33
- options: Readonly<StructOptions>,
34
- context: StructSerializationContext,
35
- object: any,
36
- value: TType['valueType'],
37
- ): VariableLengthArrayBufferLikeFieldRuntimeValue<TType, TOptions> {
38
- return new VariableLengthArrayBufferLikeFieldRuntimeValue(this, options, context, object, value);
39
- }
40
- }
41
-
42
- class VariableLengthArrayBufferLikeLengthFieldRuntimeValue extends FieldRuntimeValue {
43
- protected originalValue: FieldRuntimeValue;
44
-
45
- protected arrayBufferValue: VariableLengthArrayBufferLikeFieldRuntimeValue;
46
-
47
- public constructor(
48
- originalValue: FieldRuntimeValue,
49
- arrayBufferValue: VariableLengthArrayBufferLikeFieldRuntimeValue,
50
- ) {
51
- super(originalValue.definition, originalValue.options, originalValue.context, originalValue.object, 0);
52
- this.originalValue = originalValue;
53
- this.arrayBufferValue = arrayBufferValue;
54
- }
55
-
56
- public getSize() {
57
- return this.originalValue.getSize();
58
- }
59
-
60
- get() {
61
- // TODO: originalValue might be a `string` type, now it always returns `number`.
62
- return this.arrayBufferValue.getSize();
63
- }
64
-
65
- set() { }
66
-
67
- serialize(dataView: DataView, offset: number, context: StructSerializationContext) {
68
- this.originalValue.set(this.get());
69
- this.originalValue.serialize(dataView, offset, context);
70
- }
71
- }
72
-
73
- class VariableLengthArrayBufferLikeFieldRuntimeValue<
74
- TType extends ArrayBufferLikeFieldType = ArrayBufferLikeFieldType,
75
- TOptions extends VariableLengthArrayBufferLikeFieldOptions = VariableLengthArrayBufferLikeFieldOptions
76
- > extends ArrayBufferLikeFieldRuntimeValue<VariableLengthArrayBufferLikeFieldDefinition<TType, TOptions>> {
77
- public static getSize() {
78
- return 0;
79
- }
80
-
81
- protected length: number | undefined;
82
-
83
- protected lengthFieldValue: VariableLengthArrayBufferLikeLengthFieldRuntimeValue;
84
-
85
- public constructor(
86
- definition: VariableLengthArrayBufferLikeFieldDefinition<TType, TOptions>,
87
- options: Readonly<StructOptions>,
88
- context: StructSerializationContext,
89
- object: any,
90
- value: TType['valueType'],
91
- ) {
92
- super(definition, options, context, object, value);
93
-
94
- // Patch the associated length field.
95
- const lengthField = this.definition.options.lengthField;
96
- const originalValue = getRuntimeValue(object, lengthField);
97
- this.lengthFieldValue = new VariableLengthArrayBufferLikeLengthFieldRuntimeValue(originalValue, this);
98
- setRuntimeValue(object, lengthField, this.lengthFieldValue);
99
- }
100
-
101
- public getSize() {
102
- if (this.length === undefined) {
103
- if (this.arrayBuffer !== undefined) {
104
- this.length = this.arrayBuffer.byteLength;
105
- } else {
106
- this.length = this.definition.type.getSize(this.value);
107
- if (this.length === -1) {
108
- this.arrayBuffer = this.definition.type.toArrayBuffer(this.value, this.context);
109
- this.length = this.arrayBuffer.byteLength;
110
- }
111
- }
112
- }
113
-
114
- return this.length;
115
- }
116
-
117
- public set(value: unknown) {
118
- super.set(value);
119
- this.length = undefined;
120
- }
121
- }
1
+ import { StructFieldDefinition, StructFieldValue, StructOptions, StructValue } from '../basic';
2
+ import type { KeysOfType } from '../utils';
3
+ import { ArrayBufferLikeFieldDefinition, ArrayBufferLikeFieldType, ArrayBufferLikeFieldValue } from './array-buffer';
4
+
5
+ export type LengthField<TFields> = KeysOfType<TFields, number | string>;
6
+
7
+ export interface VariableLengthArrayBufferLikeFieldOptions<
8
+ TFields = object,
9
+ TLengthField extends LengthField<TFields> = any,
10
+ > {
11
+ lengthField: TLengthField;
12
+
13
+ lengthFieldBase?: number;
14
+ }
15
+
16
+ export class VariableLengthArrayBufferLikeFieldDefinition<
17
+ TType extends ArrayBufferLikeFieldType = ArrayBufferLikeFieldType,
18
+ TOptions extends VariableLengthArrayBufferLikeFieldOptions = VariableLengthArrayBufferLikeFieldOptions
19
+ > extends ArrayBufferLikeFieldDefinition<
20
+ TType,
21
+ TOptions,
22
+ TOptions['lengthField']
23
+ > {
24
+ public getSize(): number {
25
+ return 0;
26
+ }
27
+
28
+ protected override getDeserializeSize(struct: StructValue) {
29
+ let value = struct.value[this.options.lengthField] as number | string;
30
+ if (typeof value === 'string') {
31
+ value = Number.parseInt(value, this.options.lengthFieldBase ?? 10);
32
+ }
33
+ return value;
34
+ }
35
+
36
+ public override create(
37
+ options: Readonly<StructOptions>,
38
+ struct: StructValue,
39
+ value: TType['TTypeScriptType'],
40
+ arrayBuffer?: ArrayBuffer
41
+ ): VariableLengthArrayBufferLikeStructFieldValue<this> {
42
+ return new VariableLengthArrayBufferLikeStructFieldValue(
43
+ this,
44
+ options,
45
+ struct,
46
+ value,
47
+ arrayBuffer,
48
+ );
49
+ }
50
+ }
51
+
52
+ export class VariableLengthArrayBufferLikeStructFieldValue<
53
+ TDefinition extends VariableLengthArrayBufferLikeFieldDefinition = VariableLengthArrayBufferLikeFieldDefinition,
54
+ > extends ArrayBufferLikeFieldValue<TDefinition> {
55
+ protected length: number | undefined;
56
+
57
+ protected lengthFieldValue: VariableLengthArrayBufferLikeFieldLengthValue;
58
+
59
+ public constructor(
60
+ definition: TDefinition,
61
+ options: Readonly<StructOptions>,
62
+ struct: StructValue,
63
+ value: TDefinition['TValue'],
64
+ arrayBuffer?: ArrayBuffer,
65
+ ) {
66
+ super(definition, options, struct, value, arrayBuffer);
67
+
68
+ if (arrayBuffer) {
69
+ this.length = arrayBuffer.byteLength;
70
+ }
71
+
72
+ // Patch the associated length field.
73
+ const lengthField = this.definition.options.lengthField;
74
+
75
+ const originalValue = struct.get(lengthField);
76
+ this.lengthFieldValue = new VariableLengthArrayBufferLikeFieldLengthValue(
77
+ originalValue,
78
+ this,
79
+ );
80
+ struct.set(lengthField, this.lengthFieldValue);
81
+ }
82
+
83
+ public override getSize() {
84
+ if (this.length === undefined) {
85
+ this.length = this.definition.type.getSize(this.value);
86
+ if (this.length === -1) {
87
+ this.arrayBuffer = this.definition.type.toArrayBuffer(this.value);
88
+ this.length = this.arrayBuffer.byteLength;
89
+ }
90
+ }
91
+
92
+ return this.length;
93
+ }
94
+
95
+ public override set(value: unknown) {
96
+ super.set(value);
97
+ this.arrayBuffer = undefined;
98
+ this.length = undefined;
99
+ }
100
+ }
101
+
102
+ // Not using `VariableLengthArrayBufferLikeStructFieldValue` directly makes writing tests much easier...
103
+ type VariableLengthArrayBufferLikeFieldValueLike =
104
+ StructFieldValue<StructFieldDefinition<VariableLengthArrayBufferLikeFieldOptions, any, any>>;
105
+
106
+ export class VariableLengthArrayBufferLikeFieldLengthValue
107
+ extends StructFieldValue {
108
+ protected originalField: StructFieldValue;
109
+
110
+ protected arrayBufferField: VariableLengthArrayBufferLikeFieldValueLike;
111
+
112
+ public constructor(
113
+ originalField: StructFieldValue,
114
+ arrayBufferField: VariableLengthArrayBufferLikeFieldValueLike,
115
+ ) {
116
+ super(originalField.definition, originalField.options, originalField.struct, 0);
117
+ this.originalField = originalField;
118
+ this.arrayBufferField = arrayBufferField;
119
+ }
120
+
121
+ public override getSize() {
122
+ return this.originalField.getSize();
123
+ }
124
+
125
+ public override get() {
126
+ let value: string | number = this.arrayBufferField.getSize();
127
+
128
+ const originalValue = this.originalField.get();
129
+ if (typeof originalValue === 'string') {
130
+ value = value.toString(this.arrayBufferField.definition.options.lengthFieldBase ?? 10);
131
+ }
132
+
133
+ return value;
134
+ }
135
+
136
+ public override set() { }
137
+
138
+ serialize(dataView: DataView, offset: number) {
139
+ this.originalField.set(this.get());
140
+ this.originalField.serialize(dataView, offset);
141
+ }
142
+ }
@@ -1,47 +1,62 @@
1
- /**
2
- * When evaluating a very complex generic type alias,
3
- * tell TypeScript to use `T`, instead of current type alias' name, as the result type name
4
- *
5
- * Example:
6
- *
7
- * ```ts
8
- * type WithIdentity<T> = Identity<SomeType<T>>;
9
- * type WithoutIdentity<T> = SomeType<T>;
10
- *
11
- * type WithIdentityResult = WithIdentity<number>;
12
- * // Hover on this one shows `SomeType<number>`
13
- *
14
- * type WithoutIdentityResult = WithoutIdentity<number>;
15
- * // Hover on this one shows `WithoutIdentity<number>`
16
- * ```
17
- */
18
- export type Identity<T> = T;
19
-
20
- /**
21
- * Collapse an intersection type (`{ foo: string } & { bar: number }`) to a simple type (`{ foo: string, bar: number }`)
22
- */
23
- export type Evaluate<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
24
-
25
- /**
26
- * Overwrite fields in `TBase` with fields in `TNew`
27
- */
28
- export type Overwrite<TBase extends object, TNew extends object> =
29
- Evaluate<Omit<TBase, keyof TNew> & TNew>;
30
-
31
- /**
32
- * Remove fields with `never` type
33
- */
34
- export type OmitNever<T> = Pick<T, { [K in keyof T]: [T[K]] extends [never] ? never : K }[keyof T]>;
35
-
36
- /**
37
- * Extract keys of fields in `T` that has type `TValue`
38
- */
39
- export type KeysOfType<T, TValue> =
40
- { [TKey in keyof T]: T[TKey] extends TValue ? TKey : never }[keyof T];
41
-
42
- /**
43
- * Returns a (fake) value of the given type.
44
- */
45
- export function placeholder<T>(): T {
46
- return undefined as unknown as T;
47
- }
1
+ /**
2
+ * When evaluating a very complex generic type alias,
3
+ * tell TypeScript to use `T`, instead of current type alias' name, as the result type name
4
+ *
5
+ * Example:
6
+ *
7
+ * ```ts
8
+ * type WithIdentity<T> = Identity<SomeType<T>>;
9
+ * type WithoutIdentity<T> = SomeType<T>;
10
+ *
11
+ * type WithIdentityResult = WithIdentity<number>;
12
+ * // Hover on this one shows `SomeType<number>`
13
+ *
14
+ * type WithoutIdentityResult = WithoutIdentity<number>;
15
+ * // Hover on this one shows `WithoutIdentity<number>`
16
+ * ```
17
+ */
18
+ export type Identity<T> = T;
19
+
20
+ /**
21
+ * Collapse an intersection type (`{ foo: string } & { bar: number }`) to a simple type (`{ foo: string, bar: number }`)
22
+ */
23
+ export type Evaluate<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
24
+
25
+ /**
26
+ * Overwrite fields in `TBase` with fields in `TNew`
27
+ */
28
+ export type Overwrite<TBase extends object, TNew extends object> =
29
+ Evaluate<Omit<TBase, keyof TNew> & TNew>;
30
+
31
+ /**
32
+ * Remove fields with `never` type
33
+ */
34
+ export type OmitNever<T> = Pick<T, { [K in keyof T]: [T[K]] extends [never] ? never : K }[keyof T]>;
35
+
36
+ /**
37
+ * Extract keys of fields in `T` that has type `TValue`
38
+ */
39
+ export type KeysOfType<T, TValue> =
40
+ { [TKey in keyof T]: T[TKey] extends TValue ? TKey : never }[keyof T];
41
+
42
+ export type ValueOrPromise<T> = T | PromiseLike<T>;
43
+
44
+ /**
45
+ * Returns a (fake) value of the given type.
46
+ */
47
+ export function placeholder<T>(): T {
48
+ return undefined as unknown as T;
49
+ }
50
+
51
+ // @ts-expect-error @types/node missing `TextEncoder`
52
+ const Utf8Encoder = new TextEncoder();
53
+ // @ts-expect-error @types/node missing `TextDecoder`
54
+ const Utf8Decoder = new TextDecoder();
55
+
56
+ export function encodeUtf8(input: string): ArrayBuffer {
57
+ return Utf8Encoder.encode(input).buffer;
58
+ }
59
+
60
+ export function decodeUtf8(buffer: ArrayBuffer): string {
61
+ return Utf8Decoder.decode(buffer);
62
+ }
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StructDefaultOptions = void 0;
4
- exports.StructDefaultOptions = {
5
- littleEndian: false,
6
- };
7
- //# sourceMappingURL=context.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/basic/context.ts"],"names":[],"mappings":";;;AAqCa,QAAA,oBAAoB,GAA4B;IACzD,YAAY,EAAE,KAAK;CACtB,CAAC"}
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FieldDefinition = void 0;
4
- /**
5
- * A field definition is a bridge between its type and its runtime value.
6
- *
7
- * `Struct` record fields in a list of `FieldDefinition`s.
8
- *
9
- * When `Struct#create` or `Struct#deserialize` are called, each field's definition
10
- * crates its own type of `FieldRuntimeValue` to manage the field value in that `Struct` instance.
11
- *
12
- * One `FieldDefinition` can represents multiple similar types, just returns the corresponding
13
- * `FieldRuntimeValue` when `createValue` was called.
14
- *
15
- * @template TOptions TypeScript type of this definition's `options`.
16
- * @template TValueType TypeScript type of this field.
17
- * @template TOmitInit Optionally remove some fields from the init type. Should be a union of string literal types.
18
- */
19
- var FieldDefinition = /** @class */ (function () {
20
- function FieldDefinition(options) {
21
- this.options = options;
22
- }
23
- return FieldDefinition;
24
- }());
25
- exports.FieldDefinition = FieldDefinition;
26
- //# sourceMappingURL=definition.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"definition.js","sourceRoot":"","sources":["../../src/basic/definition.ts"],"names":[],"mappings":";;;AAKA;;;;;;;;;;;;;;GAcG;AACH;IAmBI,yBAAmB,OAAiB;QAChC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;IA2BL,sBAAC;AAAD,CAAC,AAhDD,IAgDC;AAhDqB,0CAAe"}
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./context"), exports);
5
- tslib_1.__exportStar(require("./definition"), exports);
6
- tslib_1.__exportStar(require("./runtime-value"), exports);
7
- tslib_1.__exportStar(require("./runtime-object"), exports);
8
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/basic/index.ts"],"names":[],"mappings":";;;AAAA,oDAA0B;AAC1B,uDAA6B;AAC7B,0DAAgC;AAChC,2DAAiC"}