@yume-chan/struct 0.0.9 → 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.
- package/CHANGELOG.json +23 -0
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -21
- package/README.md +720 -709
- package/dts/basic/context.d.ts +10 -18
- package/dts/basic/context.d.ts.map +1 -1
- package/dts/basic/definition.d.ts +4 -4
- package/dts/basic/definition.d.ts.map +1 -1
- package/dts/basic/field-value.d.ts +3 -5
- package/dts/basic/field-value.d.ts.map +1 -1
- package/dts/basic/struct-value.d.ts.map +1 -1
- package/dts/struct.d.ts +16 -17
- package/dts/struct.d.ts.map +1 -1
- package/dts/syncbird.d.ts +20 -0
- package/dts/syncbird.d.ts.map +1 -0
- package/dts/types/array-buffer.d.ts +13 -12
- package/dts/types/array-buffer.d.ts.map +1 -1
- package/dts/types/number.d.ts +4 -3
- package/dts/types/number.d.ts.map +1 -1
- package/dts/types/variable-length-array-buffer.d.ts +4 -4
- package/dts/types/variable-length-array-buffer.d.ts.map +1 -1
- package/dts/utils.d.ts +3 -2
- package/dts/utils.d.ts.map +1 -1
- package/esm/basic/context.js.map +1 -1
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.js +1 -2
- package/esm/basic/field-value.js.map +1 -1
- package/esm/basic/struct-value.js +0 -6
- package/esm/basic/struct-value.js.map +1 -1
- package/esm/struct.js +27 -17
- package/esm/struct.js.map +1 -1
- package/esm/syncbird.js +25 -0
- package/esm/syncbird.js.map +1 -0
- package/esm/types/array-buffer.js +21 -20
- package/esm/types/array-buffer.js.map +1 -1
- package/esm/types/number.js +9 -8
- package/esm/types/number.js.map +1 -1
- package/esm/types/variable-length-array-buffer.js +8 -8
- package/esm/types/variable-length-array-buffer.js.map +1 -1
- package/esm/utils.js +10 -0
- package/esm/utils.js.map +1 -1
- package/package.json +47 -50
- package/src/basic/context.ts +32 -42
- package/src/basic/definition.ts +72 -69
- package/src/basic/field-value.ts +67 -73
- package/src/basic/index.ts +4 -4
- package/src/basic/struct-value.ts +39 -45
- package/src/index.ts +5 -5
- package/src/struct.ts +595 -575
- package/src/syncbird.ts +53 -0
- package/src/types/array-buffer.ts +194 -184
- package/src/types/fixed-length-array-buffer.ts +17 -17
- package/src/types/index.ts +4 -4
- package/src/types/number.ts +114 -100
- package/src/types/variable-length-array-buffer.ts +142 -145
- package/src/utils.ts +62 -51
- package/cjs/basic/context.js +0 -7
- package/cjs/basic/context.js.map +0 -1
- package/cjs/basic/definition.js +0 -25
- package/cjs/basic/definition.js.map +0 -1
- package/cjs/basic/field-value.js +0 -40
- package/cjs/basic/field-value.js.map +0 -1
- package/cjs/basic/index.js +0 -8
- package/cjs/basic/index.js.map +0 -1
- package/cjs/basic/struct-value.js +0 -46
- package/cjs/basic/struct-value.js.map +0 -1
- package/cjs/index.js +0 -11
- package/cjs/index.js.map +0 -1
- package/cjs/struct.js +0 -177
- package/cjs/struct.js.map +0 -1
- package/cjs/types/array-buffer.js +0 -118
- package/cjs/types/array-buffer.js.map +0 -1
- package/cjs/types/fixed-length-array-buffer.js +0 -12
- package/cjs/types/fixed-length-array-buffer.js.map +0 -1
- package/cjs/types/index.js +0 -8
- package/cjs/types/index.js.map +0 -1
- package/cjs/types/number.js +0 -52
- package/cjs/types/number.js.map +0 -1
- package/cjs/types/variable-length-array-buffer.js +0 -77
- package/cjs/types/variable-length-array-buffer.js.map +0 -1
- package/cjs/utils.js +0 -11
- package/cjs/utils.js.map +0 -1
- package/tsconfig.cjs.json +0 -11
- package/tsconfig.esm.json +0 -15
package/src/types/number.ts
CHANGED
|
@@ -1,100 +1,114 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
public readonly
|
|
13
|
-
|
|
14
|
-
public readonly
|
|
15
|
-
|
|
16
|
-
public readonly
|
|
17
|
-
|
|
18
|
-
public
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
public static readonly
|
|
31
|
-
|
|
32
|
-
public static readonly
|
|
33
|
-
|
|
34
|
-
public static readonly
|
|
35
|
-
|
|
36
|
-
public static readonly
|
|
37
|
-
|
|
38
|
-
public static readonly
|
|
39
|
-
|
|
40
|
-
public static readonly
|
|
41
|
-
|
|
42
|
-
public static readonly
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
this.value
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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,145 +1,142 @@
|
|
|
1
|
-
import { StructFieldDefinition, StructFieldValue, StructOptions,
|
|
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 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 create(
|
|
37
|
-
options: Readonly<StructOptions>,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
protected
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
value
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
StructFieldValue
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
originalField
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
this.originalField.serialize(dataView, offset, context);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
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
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -1,51 +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
|
-
export type ValueOrPromise<T> = T |
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
+
}
|
package/cjs/basic/context.js
DELETED
package/cjs/basic/context.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/basic/context.ts"],"names":[],"mappings":";;;AAuCa,QAAA,oBAAoB,GAA4B;IACzD,YAAY,EAAE,KAAK;CACtB,CAAC"}
|
package/cjs/basic/definition.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StructFieldDefinition = 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 `StructFieldDefinition`s.
|
|
8
|
-
*
|
|
9
|
-
* When `Struct#create` or `Struct#deserialize` are called, each field's definition
|
|
10
|
-
* crates its own type of `StructFieldValue` to manage the field value in that `Struct` instance.
|
|
11
|
-
*
|
|
12
|
-
* One `StructFieldDefinition` can represents multiple similar types, just returns the corresponding
|
|
13
|
-
* `StructFieldValue` when `createValue` was called.
|
|
14
|
-
*
|
|
15
|
-
* @template TOptions TypeScript type of this definition's `options`.
|
|
16
|
-
* @template TValue TypeScript type of this field.
|
|
17
|
-
* @template TOmitInitKey Optionally remove some fields from the init type. Should be a union of string literal types.
|
|
18
|
-
*/
|
|
19
|
-
class StructFieldDefinition {
|
|
20
|
-
constructor(options) {
|
|
21
|
-
this.options = options;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
exports.StructFieldDefinition = StructFieldDefinition;
|
|
25
|
-
//# sourceMappingURL=definition.js.map
|