@yume-chan/struct 0.0.9 → 0.0.13
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 +65 -0
- package/CHANGELOG.md +39 -0
- package/LICENSE +21 -21
- package/README.md +769 -709
- package/esm/basic/definition.d.ts +44 -0
- package/esm/basic/definition.d.ts.map +1 -0
- package/esm/basic/definition.js +13 -9
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.d.ts +38 -0
- package/esm/basic/field-value.d.ts.map +1 -0
- package/esm/basic/field-value.js +13 -7
- package/esm/basic/field-value.js.map +1 -1
- package/esm/basic/index.d.ts +6 -0
- package/esm/basic/index.d.ts.map +1 -0
- package/esm/basic/index.js +5 -4
- package/esm/basic/index.js.map +1 -1
- package/esm/basic/options.d.ts +10 -0
- package/esm/basic/options.d.ts.map +1 -0
- package/esm/basic/{context.js → options.js} +1 -1
- package/esm/basic/options.js.map +1 -0
- package/esm/basic/stream.d.ts +19 -0
- package/esm/basic/stream.d.ts.map +1 -0
- package/esm/basic/stream.js +2 -0
- package/esm/basic/stream.js.map +1 -0
- package/{dts → esm}/basic/struct-value.d.ts +3 -3
- package/esm/basic/struct-value.d.ts.map +1 -0
- package/esm/basic/struct-value.js +7 -15
- package/esm/basic/struct-value.js.map +1 -1
- package/esm/index.d.ts +14 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +5 -5
- package/esm/index.js.map +1 -1
- package/{dts → esm}/struct.d.ts +25 -22
- package/esm/struct.d.ts.map +1 -0
- package/esm/struct.js +75 -52
- package/esm/struct.js.map +1 -1
- package/esm/syncbird.d.ts +65 -0
- package/esm/syncbird.d.ts.map +1 -0
- package/esm/syncbird.js +38 -0
- package/esm/syncbird.js.map +1 -0
- package/esm/types/bigint.d.ts +25 -0
- package/esm/types/bigint.d.ts.map +1 -0
- package/esm/types/bigint.js +45 -0
- package/esm/types/bigint.js.map +1 -0
- package/esm/types/buffer/base.d.ts +63 -0
- package/esm/types/buffer/base.d.ts.map +1 -0
- package/esm/types/buffer/base.js +100 -0
- package/esm/types/buffer/base.js.map +1 -0
- package/esm/types/buffer/fixed-length.d.ts +8 -0
- package/esm/types/buffer/fixed-length.d.ts.map +1 -0
- package/esm/types/buffer/fixed-length.js +8 -0
- package/esm/types/buffer/fixed-length.js.map +1 -0
- package/esm/types/buffer/index.d.ts +4 -0
- package/esm/types/buffer/index.d.ts.map +1 -0
- package/esm/types/buffer/index.js +4 -0
- package/esm/types/buffer/index.js.map +1 -0
- package/esm/types/buffer/variable-length.d.ts +43 -0
- package/esm/types/buffer/variable-length.d.ts.map +1 -0
- package/esm/types/buffer/variable-length.js +76 -0
- package/esm/types/buffer/variable-length.js.map +1 -0
- package/esm/types/index.d.ts +4 -0
- package/esm/types/index.d.ts.map +1 -0
- package/esm/types/index.js +3 -4
- package/esm/types/index.js.map +1 -1
- package/esm/types/number.d.ts +32 -0
- package/esm/types/number.d.ts.map +1 -0
- package/esm/types/number.js +26 -18
- package/esm/types/number.js.map +1 -1
- package/{dts → esm}/utils.d.ts +3 -2
- package/{dts → esm}/utils.d.ts.map +1 -1
- package/esm/utils.js +16 -0
- package/esm/utils.js.map +1 -1
- package/package.json +46 -49
- package/src/basic/definition.ts +70 -69
- package/src/basic/field-value.ts +67 -73
- package/src/basic/index.ts +5 -4
- package/src/basic/options.ts +19 -0
- package/src/basic/stream.ts +19 -0
- package/src/basic/struct-value.ts +39 -45
- package/src/index.ts +17 -5
- package/src/struct.ts +609 -575
- package/src/syncbird.ts +131 -0
- package/src/types/bigint.ts +102 -0
- package/src/types/buffer/base.ts +176 -0
- package/src/types/buffer/fixed-length.ts +17 -0
- package/src/types/buffer/index.ts +3 -0
- package/src/types/buffer/variable-length.ts +156 -0
- package/src/types/index.ts +3 -4
- package/src/types/number.ts +115 -100
- package/src/utils.ts +70 -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/dts/basic/context.d.ts +0 -36
- package/dts/basic/context.d.ts.map +0 -1
- package/dts/basic/definition.d.ts +0 -48
- package/dts/basic/definition.d.ts.map +0 -1
- package/dts/basic/field-value.d.ts +0 -40
- package/dts/basic/field-value.d.ts.map +0 -1
- package/dts/basic/index.d.ts +0 -5
- package/dts/basic/index.d.ts.map +0 -1
- package/dts/basic/struct-value.d.ts.map +0 -1
- package/dts/index.d.ts +0 -6
- package/dts/index.d.ts.map +0 -1
- package/dts/struct.d.ts.map +0 -1
- package/dts/types/array-buffer.d.ts +0 -70
- package/dts/types/array-buffer.d.ts.map +0 -1
- package/dts/types/fixed-length-array-buffer.d.ts +0 -8
- package/dts/types/fixed-length-array-buffer.d.ts.map +0 -1
- package/dts/types/index.d.ts +0 -5
- package/dts/types/index.d.ts.map +0 -1
- package/dts/types/number.d.ts +0 -33
- package/dts/types/number.d.ts.map +0 -1
- package/dts/types/variable-length-array-buffer.d.ts +0 -32
- package/dts/types/variable-length-array-buffer.d.ts.map +0 -1
- package/esm/basic/context.js.map +0 -1
- package/esm/types/array-buffer.js +0 -109
- package/esm/types/array-buffer.js.map +0 -1
- package/esm/types/fixed-length-array-buffer.js +0 -8
- package/esm/types/fixed-length-array-buffer.js.map +0 -1
- package/esm/types/variable-length-array-buffer.js +0 -71
- package/esm/types/variable-length-array-buffer.js.map +0 -1
- package/src/basic/context.ts +0 -42
- package/src/types/array-buffer.ts +0 -184
- package/src/types/fixed-length-array-buffer.ts +0 -17
- package/src/types/variable-length-array-buffer.ts +0 -145
- package/tsconfig.cjs.json +0 -11
- package/tsconfig.esm.json +0 -15
package/src/types/number.ts
CHANGED
|
@@ -1,100 +1,115 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
public readonly
|
|
15
|
-
|
|
16
|
-
public readonly
|
|
17
|
-
|
|
18
|
-
public
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
export class NumberFieldDefinition<
|
|
46
|
-
TType extends NumberFieldType = NumberFieldType,
|
|
47
|
-
TTypeScriptType = TType[
|
|
48
|
-
> extends StructFieldDefinition<
|
|
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 create(
|
|
64
|
-
options: Readonly<StructOptions>,
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
}
|
|
1
|
+
// cspell: ignore syncbird
|
|
2
|
+
|
|
3
|
+
import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from '../basic/index.js';
|
|
4
|
+
import { Syncbird } from "../syncbird.js";
|
|
5
|
+
import type { ValueOrPromise } from "../utils.js";
|
|
6
|
+
|
|
7
|
+
export type DataViewGetters =
|
|
8
|
+
{ [TKey in keyof DataView]: TKey extends `get${string}` ? TKey : never }[keyof DataView];
|
|
9
|
+
|
|
10
|
+
export type DataViewSetters =
|
|
11
|
+
{ [TKey in keyof DataView]: TKey extends `set${string}` ? TKey : never }[keyof DataView];
|
|
12
|
+
|
|
13
|
+
export class NumberFieldType {
|
|
14
|
+
public readonly TTypeScriptType!: number;
|
|
15
|
+
|
|
16
|
+
public readonly size: number;
|
|
17
|
+
|
|
18
|
+
public readonly dataViewGetter: DataViewGetters;
|
|
19
|
+
|
|
20
|
+
public readonly dataViewSetter: DataViewSetters;
|
|
21
|
+
|
|
22
|
+
public constructor(
|
|
23
|
+
size: number,
|
|
24
|
+
dataViewGetter: DataViewGetters,
|
|
25
|
+
dataViewSetter: DataViewSetters
|
|
26
|
+
) {
|
|
27
|
+
this.size = size;
|
|
28
|
+
this.dataViewGetter = dataViewGetter;
|
|
29
|
+
this.dataViewSetter = dataViewSetter;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public static readonly Int8 = new NumberFieldType(1, 'getInt8', 'setInt8');
|
|
33
|
+
|
|
34
|
+
public static readonly Uint8 = new NumberFieldType(1, 'getUint8', 'setUint8');
|
|
35
|
+
|
|
36
|
+
public static readonly Int16 = new NumberFieldType(2, 'getInt16', 'setInt16');
|
|
37
|
+
|
|
38
|
+
public static readonly Uint16 = new NumberFieldType(2, 'getUint16', 'setUint16');
|
|
39
|
+
|
|
40
|
+
public static readonly Int32 = new NumberFieldType(4, 'getInt32', 'setInt32');
|
|
41
|
+
|
|
42
|
+
public static readonly Uint32 = new NumberFieldType(4, 'getUint32', 'setUint32');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class NumberFieldDefinition<
|
|
46
|
+
TType extends NumberFieldType = NumberFieldType,
|
|
47
|
+
TTypeScriptType = TType["TTypeScriptType"],
|
|
48
|
+
> extends StructFieldDefinition<
|
|
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 create(
|
|
64
|
+
options: Readonly<StructOptions>,
|
|
65
|
+
struct: StructValue,
|
|
66
|
+
value: TTypeScriptType,
|
|
67
|
+
): NumberFieldValue<this> {
|
|
68
|
+
return new NumberFieldValue(this, options, struct, value);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public override deserialize(
|
|
72
|
+
options: Readonly<StructOptions>,
|
|
73
|
+
stream: StructDeserializeStream,
|
|
74
|
+
struct: StructValue,
|
|
75
|
+
): NumberFieldValue<this>;
|
|
76
|
+
public override deserialize(
|
|
77
|
+
options: Readonly<StructOptions>,
|
|
78
|
+
stream: StructAsyncDeserializeStream,
|
|
79
|
+
struct: StructValue,
|
|
80
|
+
): Promise<NumberFieldValue<this>>;
|
|
81
|
+
public override deserialize(
|
|
82
|
+
options: Readonly<StructOptions>,
|
|
83
|
+
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
|
84
|
+
struct: StructValue,
|
|
85
|
+
): ValueOrPromise<NumberFieldValue<this>> {
|
|
86
|
+
return Syncbird
|
|
87
|
+
.try(() => {
|
|
88
|
+
return stream.read(this.getSize());
|
|
89
|
+
})
|
|
90
|
+
.then(array => {
|
|
91
|
+
const view = new DataView(array.buffer, array.byteOffset, array.byteLength);
|
|
92
|
+
const value = view[this.type.dataViewGetter](
|
|
93
|
+
0,
|
|
94
|
+
options.littleEndian
|
|
95
|
+
);
|
|
96
|
+
return this.create(options, struct, value as any);
|
|
97
|
+
})
|
|
98
|
+
.valueOrPromise();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export class NumberFieldValue<
|
|
103
|
+
TDefinition extends NumberFieldDefinition<NumberFieldType, any>,
|
|
104
|
+
> extends StructFieldValue<TDefinition> {
|
|
105
|
+
public serialize(dataView: DataView, offset: number): void {
|
|
106
|
+
// `setBigInt64` requires a `bigint` while others require `number`
|
|
107
|
+
// So `dataView[DataViewSetters]` requires `bigint & number`
|
|
108
|
+
// and that is, `never`
|
|
109
|
+
(dataView[this.definition.type.dataViewSetter] as any)(
|
|
110
|
+
offset,
|
|
111
|
+
this.value!,
|
|
112
|
+
this.options.littleEndian
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -1,51 +1,70 @@
|
|
|
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
|
+
// This library can't use `@types/node` or `lib: dom`
|
|
52
|
+
// because they will pollute the global scope
|
|
53
|
+
// So `TextEncoder` and `TextDecoder` are not available
|
|
54
|
+
|
|
55
|
+
// Node.js 8.3 ships `TextEncoder` and `TextDecoder` in `util` module.
|
|
56
|
+
// But using top level await to load them requires Node.js 14.1.
|
|
57
|
+
// So there is no point to do that. Let's just assume they exist in global.
|
|
58
|
+
|
|
59
|
+
// @ts-expect-error
|
|
60
|
+
const Utf8Encoder = new TextEncoder();
|
|
61
|
+
// @ts-expect-error
|
|
62
|
+
const Utf8Decoder = new TextDecoder();
|
|
63
|
+
|
|
64
|
+
export function encodeUtf8(input: string): Uint8Array {
|
|
65
|
+
return Utf8Encoder.encode(input);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function decodeUtf8(buffer: ArrayBufferView | ArrayBuffer): string {
|
|
69
|
+
return Utf8Decoder.decode(buffer);
|
|
70
|
+
}
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"definition.js","sourceRoot":"","sources":["../../src/basic/definition.ts"],"names":[],"mappings":";;;AAKA;;;;;;;;;;;;;;GAcG;AACH,MAAsB,qBAAqB;IAmBvC,YAAmB,OAAiB;QAChC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;CA2BJ;AAhDD,sDAgDC"}
|
package/cjs/basic/field-value.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StructFieldValue = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Field runtime value manages one field of one `Struct` instance.
|
|
6
|
-
*
|
|
7
|
-
* If one `StructFieldDefinition` needs to change other field's semantics
|
|
8
|
-
* It can override other fields' `StructFieldValue` in its own `StructFieldValue`'s constructor
|
|
9
|
-
*/
|
|
10
|
-
class StructFieldValue {
|
|
11
|
-
constructor(definition, options, context, struct, value) {
|
|
12
|
-
this.definition = definition;
|
|
13
|
-
this.options = options;
|
|
14
|
-
this.context = context;
|
|
15
|
-
this.struct = struct;
|
|
16
|
-
this.value = value;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Gets size of this field. By default, it returns its `definition`'s size.
|
|
20
|
-
*
|
|
21
|
-
* When overridden in derived classes, can have custom logic to calculate the actual size.
|
|
22
|
-
*/
|
|
23
|
-
getSize() {
|
|
24
|
-
return this.definition.getSize();
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* When implemented in derived classes, returns the current value of this field
|
|
28
|
-
*/
|
|
29
|
-
get() {
|
|
30
|
-
return this.value;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* When implemented in derived classes, update the current value of this field
|
|
34
|
-
*/
|
|
35
|
-
set(value) {
|
|
36
|
-
this.value = value;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
exports.StructFieldValue = StructFieldValue;
|
|
40
|
-
//# sourceMappingURL=field-value.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"field-value.js","sourceRoot":"","sources":["../../src/basic/field-value.ts"],"names":[],"mappings":";;;AAIA;;;;;GAKG;AACH,MAAsB,gBAAgB;IAiBlC,YACI,UAAuB,EACvB,OAAgC,EAChC,OAAmC,EACnC,MAAmB,EACnB,KAA4B;QAE5B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACI,OAAO;QACV,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;IACrC,CAAC;IAED;;OAEG;IACI,GAAG;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,KAA4B;QACnC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;CAUJ;AA9DD,4CA8DC"}
|
package/cjs/basic/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./context"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./definition"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./field-value"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./struct-value"), exports);
|
|
8
|
-
//# sourceMappingURL=index.js.map
|
package/cjs/basic/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/basic/index.ts"],"names":[],"mappings":";;;AAAA,oDAA0B;AAC1B,uDAA6B;AAC7B,wDAA8B;AAC9B,yDAA+B"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StructValue = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Manages the initialization process of a struct value
|
|
6
|
-
*/
|
|
7
|
-
class StructValue {
|
|
8
|
-
constructor() {
|
|
9
|
-
/** @internal */ this.fieldValues = {};
|
|
10
|
-
/**
|
|
11
|
-
* Gets the result struct value object
|
|
12
|
-
*/
|
|
13
|
-
this.value = {};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Sets a `StructFieldValue` for `key`
|
|
17
|
-
*
|
|
18
|
-
* @param key The field name
|
|
19
|
-
* @param value The associated `StructFieldValue`
|
|
20
|
-
*/
|
|
21
|
-
set(key, value) {
|
|
22
|
-
// TODO: TypeScript 4.2 will allow this behavior
|
|
23
|
-
// https://github.com/microsoft/TypeScript/pull/26797
|
|
24
|
-
// @ts-expect-error Type 'symbol' cannot be used as an index type. ts(2538)
|
|
25
|
-
this.fieldValues[key] = value;
|
|
26
|
-
Object.defineProperty(this.value, key, {
|
|
27
|
-
configurable: true,
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get() { return value.get(); },
|
|
30
|
-
set(v) { value.set(v); },
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Gets a previously `StructFieldValue` for `key`
|
|
35
|
-
*
|
|
36
|
-
* @param key The field name
|
|
37
|
-
*/
|
|
38
|
-
get(key) {
|
|
39
|
-
// TODO: TypeScript 4.2 will allow this behavior
|
|
40
|
-
// https://github.com/microsoft/TypeScript/pull/26797
|
|
41
|
-
// @ts-expect-error Type 'symbol' cannot be used as an index type. ts(2538)
|
|
42
|
-
return this.fieldValues[key];
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
exports.StructValue = StructValue;
|
|
46
|
-
//# sourceMappingURL=struct-value.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"struct-value.js","sourceRoot":"","sources":["../../src/basic/struct-value.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH,MAAa,WAAW;IAAxB;QACI,gBAAgB,CAAU,gBAAW,GAA0C,EAAE,CAAC;QAElF;;WAEG;QACa,UAAK,GAAiC,EAAE,CAAC;IAiC7D,CAAC;IA/BG;;;;;OAKG;IACI,GAAG,CAAC,GAAgB,EAAE,KAAuB;QAChD,gDAAgD;QAChD,qDAAqD;QACrD,2EAA2E;QAC3E,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QAE9B,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE;YACnC,YAAY,EAAE,IAAI;YAClB,UAAU,EAAE,IAAI;YAChB,GAAG,KAAK,OAAO,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YAC7B,GAAG,CAAC,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;SAC3B,CAAC,CAAC;IACP,CAAC;IAED;;;;OAIG;IACI,GAAG,CAAC,GAAgB;QACvB,gDAAgD;QAChD,qDAAqD;QACrD,2EAA2E;QAC3E,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC;CACJ;AAvCD,kCAuCC"}
|
package/cjs/index.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
tslib_1.__exportStar(require("./basic"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./struct"), exports);
|
|
7
|
-
var struct_1 = require("./struct");
|
|
8
|
-
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return struct_1.Struct; } });
|
|
9
|
-
tslib_1.__exportStar(require("./types"), exports);
|
|
10
|
-
tslib_1.__exportStar(require("./utils"), exports);
|
|
11
|
-
//# sourceMappingURL=index.js.map
|
package/cjs/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,kDAAwB;AACxB,mDAAyB;AACzB,mCAA6C;AAApC,iGAAA,MAAM,OAAW;AAC1B,kDAAwB;AACxB,kDAAwB"}
|