@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.
Files changed (84) hide show
  1. package/CHANGELOG.json +23 -0
  2. package/CHANGELOG.md +16 -0
  3. package/LICENSE +21 -21
  4. package/README.md +720 -709
  5. package/dts/basic/context.d.ts +10 -18
  6. package/dts/basic/context.d.ts.map +1 -1
  7. package/dts/basic/definition.d.ts +4 -4
  8. package/dts/basic/definition.d.ts.map +1 -1
  9. package/dts/basic/field-value.d.ts +3 -5
  10. package/dts/basic/field-value.d.ts.map +1 -1
  11. package/dts/basic/struct-value.d.ts.map +1 -1
  12. package/dts/struct.d.ts +16 -17
  13. package/dts/struct.d.ts.map +1 -1
  14. package/dts/syncbird.d.ts +20 -0
  15. package/dts/syncbird.d.ts.map +1 -0
  16. package/dts/types/array-buffer.d.ts +13 -12
  17. package/dts/types/array-buffer.d.ts.map +1 -1
  18. package/dts/types/number.d.ts +4 -3
  19. package/dts/types/number.d.ts.map +1 -1
  20. package/dts/types/variable-length-array-buffer.d.ts +4 -4
  21. package/dts/types/variable-length-array-buffer.d.ts.map +1 -1
  22. package/dts/utils.d.ts +3 -2
  23. package/dts/utils.d.ts.map +1 -1
  24. package/esm/basic/context.js.map +1 -1
  25. package/esm/basic/definition.js.map +1 -1
  26. package/esm/basic/field-value.js +1 -2
  27. package/esm/basic/field-value.js.map +1 -1
  28. package/esm/basic/struct-value.js +0 -6
  29. package/esm/basic/struct-value.js.map +1 -1
  30. package/esm/struct.js +27 -17
  31. package/esm/struct.js.map +1 -1
  32. package/esm/syncbird.js +25 -0
  33. package/esm/syncbird.js.map +1 -0
  34. package/esm/types/array-buffer.js +21 -20
  35. package/esm/types/array-buffer.js.map +1 -1
  36. package/esm/types/number.js +9 -8
  37. package/esm/types/number.js.map +1 -1
  38. package/esm/types/variable-length-array-buffer.js +8 -8
  39. package/esm/types/variable-length-array-buffer.js.map +1 -1
  40. package/esm/utils.js +10 -0
  41. package/esm/utils.js.map +1 -1
  42. package/package.json +47 -50
  43. package/src/basic/context.ts +32 -42
  44. package/src/basic/definition.ts +72 -69
  45. package/src/basic/field-value.ts +67 -73
  46. package/src/basic/index.ts +4 -4
  47. package/src/basic/struct-value.ts +39 -45
  48. package/src/index.ts +5 -5
  49. package/src/struct.ts +595 -575
  50. package/src/syncbird.ts +53 -0
  51. package/src/types/array-buffer.ts +194 -184
  52. package/src/types/fixed-length-array-buffer.ts +17 -17
  53. package/src/types/index.ts +4 -4
  54. package/src/types/number.ts +114 -100
  55. package/src/types/variable-length-array-buffer.ts +142 -145
  56. package/src/utils.ts +62 -51
  57. package/cjs/basic/context.js +0 -7
  58. package/cjs/basic/context.js.map +0 -1
  59. package/cjs/basic/definition.js +0 -25
  60. package/cjs/basic/definition.js.map +0 -1
  61. package/cjs/basic/field-value.js +0 -40
  62. package/cjs/basic/field-value.js.map +0 -1
  63. package/cjs/basic/index.js +0 -8
  64. package/cjs/basic/index.js.map +0 -1
  65. package/cjs/basic/struct-value.js +0 -46
  66. package/cjs/basic/struct-value.js.map +0 -1
  67. package/cjs/index.js +0 -11
  68. package/cjs/index.js.map +0 -1
  69. package/cjs/struct.js +0 -177
  70. package/cjs/struct.js.map +0 -1
  71. package/cjs/types/array-buffer.js +0 -118
  72. package/cjs/types/array-buffer.js.map +0 -1
  73. package/cjs/types/fixed-length-array-buffer.js +0 -12
  74. package/cjs/types/fixed-length-array-buffer.js.map +0 -1
  75. package/cjs/types/index.js +0 -8
  76. package/cjs/types/index.js.map +0 -1
  77. package/cjs/types/number.js +0 -52
  78. package/cjs/types/number.js.map +0 -1
  79. package/cjs/types/variable-length-array-buffer.js +0 -77
  80. package/cjs/types/variable-length-array-buffer.js.map +0 -1
  81. package/cjs/utils.js +0 -11
  82. package/cjs/utils.js.map +0 -1
  83. package/tsconfig.cjs.json +0 -11
  84. package/tsconfig.esm.json +0 -15
@@ -1,100 +1,114 @@
1
- import { StructDeserializationContext, StructFieldDefinition, StructFieldValue, StructOptions, StructSerializationContext, StructValue } 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 readonly TTypeScriptType!: TTypeScriptType;
11
-
12
- public readonly size: number;
13
-
14
- public readonly dataViewGetter: DataViewGetters;
15
-
16
- public readonly dataViewSetter: DataViewSetters;
17
-
18
- public constructor(
19
- size: number,
20
- dataViewGetter: DataViewGetters,
21
- dataViewSetter: DataViewSetters
22
- ) {
23
- this.size = size;
24
- this.dataViewGetter = dataViewGetter;
25
- this.dataViewSetter = dataViewSetter;
26
- }
27
-
28
- public static readonly Int8 = new NumberFieldType<number>(1, 'getInt8', 'setInt8');
29
-
30
- public static readonly Uint8 = new NumberFieldType<number>(1, 'getUint8', 'setUint8');
31
-
32
- public static readonly Int16 = new NumberFieldType<number>(2, 'getInt16', 'setInt16');
33
-
34
- public static readonly Uint16 = new NumberFieldType<number>(2, 'getUint16', 'setUint16');
35
-
36
- public static readonly Int32 = new NumberFieldType<number>(4, 'getInt32', 'setInt32');
37
-
38
- public static readonly Uint32 = new NumberFieldType<number>(4, 'getUint32', 'setUint32');
39
-
40
- public static readonly Int64 = new NumberFieldType<bigint>(8, 'getBigInt64', 'setBigInt64');
41
-
42
- public static readonly Uint64 = new NumberFieldType<bigint>(8, 'getBigUint64', 'setBigUint64');
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
- context: StructSerializationContext,
66
- struct: StructValue,
67
- value: TTypeScriptType,
68
- ): NumberFieldValue<this> {
69
- return new NumberFieldValue(this, options, context, struct, value);
70
- }
71
-
72
- public async deserialize(
73
- options: Readonly<StructOptions>,
74
- context: StructDeserializationContext,
75
- struct: StructValue,
76
- ): Promise<NumberFieldValue<this>> {
77
- const buffer = await context.read(this.getSize());
78
- const view = new DataView(buffer);
79
- const value = view[this.type.dataViewGetter](
80
- 0,
81
- options.littleEndian
82
- ) as any;
83
- return this.create(options, context, struct, value);
84
- }
85
- }
86
-
87
- export class NumberFieldValue<
88
- TDefinition extends NumberFieldDefinition<NumberFieldType, any>,
89
- > extends StructFieldValue<TDefinition> {
90
- public serialize(dataView: DataView, offset: number): void {
91
- // `setBigInt64` requires a `bigint` while others require `number`
92
- // So `dataView[DataViewSetters]` requires `bigint & number`
93
- // and that is, `never`
94
- (dataView[this.definition.type.dataViewSetter] as any)(
95
- offset,
96
- this.value!,
97
- this.options.littleEndian
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, StructSerializationContext, 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 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
- context: StructSerializationContext,
39
- struct: StructValue,
40
- value: TType['TTypeScriptType'],
41
- arrayBuffer?: ArrayBuffer
42
- ): VariableLengthArrayBufferLikeStructFieldValue<this> {
43
- return new VariableLengthArrayBufferLikeStructFieldValue(
44
- this,
45
- options,
46
- context,
47
- struct,
48
- value,
49
- arrayBuffer,
50
- );
51
- }
52
- }
53
-
54
- export class VariableLengthArrayBufferLikeStructFieldValue<
55
- TDefinition extends VariableLengthArrayBufferLikeFieldDefinition = VariableLengthArrayBufferLikeFieldDefinition,
56
- > extends ArrayBufferLikeFieldValue<TDefinition> {
57
- protected length: number | undefined;
58
-
59
- protected lengthFieldValue: VariableLengthArrayBufferLikeFieldLengthValue;
60
-
61
- public constructor(
62
- definition: TDefinition,
63
- options: Readonly<StructOptions>,
64
- context: StructSerializationContext,
65
- struct: StructValue,
66
- value: TDefinition['TValue'],
67
- arrayBuffer?: ArrayBuffer,
68
- ) {
69
- super(definition, options, context, struct, value, arrayBuffer);
70
-
71
- if (arrayBuffer) {
72
- this.length = arrayBuffer.byteLength;
73
- }
74
-
75
- // Patch the associated length field.
76
- const lengthField = this.definition.options.lengthField;
77
-
78
- const originalValue = struct.get(lengthField);
79
- this.lengthFieldValue = new VariableLengthArrayBufferLikeFieldLengthValue(
80
- originalValue,
81
- this,
82
- );
83
- struct.set(lengthField, this.lengthFieldValue);
84
- }
85
-
86
- public getSize() {
87
- if (this.length === undefined) {
88
- this.length = this.definition.type.getSize(this.value);
89
- if (this.length === -1) {
90
- this.arrayBuffer = this.definition.type.toArrayBuffer(this.value, this.context);
91
- this.length = this.arrayBuffer.byteLength;
92
- }
93
- }
94
-
95
- return this.length;
96
- }
97
-
98
- public set(value: unknown) {
99
- super.set(value);
100
- this.arrayBuffer = undefined;
101
- this.length = undefined;
102
- }
103
- }
104
-
105
- // Not using `VariableLengthArrayBufferLikeStructFieldValue` directly makes writing tests much easier...
106
- type VariableLengthArrayBufferLikeFieldValueLike =
107
- StructFieldValue<StructFieldDefinition<VariableLengthArrayBufferLikeFieldOptions, any, any>>;
108
-
109
- export class VariableLengthArrayBufferLikeFieldLengthValue
110
- extends StructFieldValue {
111
- protected originalField: StructFieldValue;
112
-
113
- protected arrayBufferField: VariableLengthArrayBufferLikeFieldValueLike;
114
-
115
- public constructor(
116
- originalField: StructFieldValue,
117
- arrayBufferField: VariableLengthArrayBufferLikeFieldValueLike,
118
- ) {
119
- super(originalField.definition, originalField.options, originalField.context, originalField.struct, 0);
120
- this.originalField = originalField;
121
- this.arrayBufferField = arrayBufferField;
122
- }
123
-
124
- public getSize() {
125
- return this.originalField.getSize();
126
- }
127
-
128
- get() {
129
- let value: string | number = this.arrayBufferField.getSize();
130
-
131
- const originalValue = this.originalField.get();
132
- if (typeof originalValue === 'string') {
133
- value = value.toString(this.arrayBufferField.definition.options.lengthFieldBase ?? 10);
134
- }
135
-
136
- return value;
137
- }
138
-
139
- set() { }
140
-
141
- serialize(dataView: DataView, offset: number, context: StructSerializationContext) {
142
- this.originalField.set(this.get());
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 | Promise<T>;
43
-
44
- export type Awaited<T> = T extends Promise<infer R> ? Awaited<R> : T;
45
-
46
- /**
47
- * Returns a (fake) value of the given type.
48
- */
49
- export function placeholder<T>(): T {
50
- return undefined as unknown as T;
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
+ }
@@ -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":";;;AAuCa,QAAA,oBAAoB,GAA4B;IACzD,YAAY,EAAE,KAAK;CACtB,CAAC"}
@@ -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