@yume-chan/struct 0.0.16 → 0.0.18
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 +18 -0
- package/CHANGELOG.md +13 -1
- package/LICENSE +1 -1
- package/esm/basic/definition.d.ts +4 -4
- package/esm/basic/definition.d.ts.map +1 -1
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.d.ts +7 -7
- package/esm/basic/field-value.d.ts.map +1 -1
- package/esm/basic/field-value.js +2 -2
- package/esm/basic/field-value.js.map +1 -1
- package/esm/basic/stream.d.ts +1 -1
- package/esm/basic/stream.d.ts.map +1 -1
- package/esm/basic/struct-value.d.ts +2 -2
- package/esm/basic/struct-value.d.ts.map +1 -1
- package/esm/basic/struct-value.js +10 -3
- package/esm/basic/struct-value.js.map +1 -1
- package/esm/struct.d.ts +19 -19
- package/esm/struct.d.ts.map +1 -1
- package/esm/struct.js +11 -10
- package/esm/struct.js.map +1 -1
- package/esm/sync-promise.d.ts.map +1 -1
- package/esm/sync-promise.js +4 -4
- package/esm/sync-promise.js.map +1 -1
- package/esm/types/bigint.d.ts +4 -4
- package/esm/types/bigint.d.ts.map +1 -1
- package/esm/types/bigint.js +6 -6
- package/esm/types/bigint.js.map +1 -1
- package/esm/types/buffer/base.d.ts +4 -4
- package/esm/types/buffer/base.d.ts.map +1 -1
- package/esm/types/buffer/base.js +5 -6
- package/esm/types/buffer/base.js.map +1 -1
- package/esm/types/buffer/fixed-length.d.ts.map +1 -1
- package/esm/types/buffer/fixed-length.js +0 -1
- package/esm/types/buffer/fixed-length.js.map +1 -1
- package/esm/types/buffer/variable-length.d.ts +7 -7
- package/esm/types/buffer/variable-length.d.ts.map +1 -1
- package/esm/types/buffer/variable-length.js +4 -4
- package/esm/types/buffer/variable-length.js.map +1 -1
- package/esm/types/number.d.ts +16 -22
- package/esm/types/number.d.ts.map +1 -1
- package/esm/types/number.js +89 -40
- package/esm/types/number.js.map +1 -1
- package/esm/utils.d.ts +6 -6
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +1 -8
- package/esm/utils.js.map +1 -1
- package/package.json +14 -12
- package/src/basic/definition.ts +12 -9
- package/src/basic/field-value.ts +18 -15
- package/src/basic/stream.ts +3 -1
- package/src/basic/struct-value.ts +17 -11
- package/src/struct.ts +208 -203
- package/src/sync-promise.ts +32 -12
- package/src/types/bigint.ts +58 -31
- package/src/types/buffer/base.ts +53 -35
- package/src/types/buffer/fixed-length.ts +3 -8
- package/src/types/buffer/variable-length.ts +41 -24
- package/src/types/number.ts +131 -79
- package/src/utils.ts +30 -8
- package/tsconfig.build.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.test.json +0 -8
package/src/sync-promise.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
export interface SyncPromise<T> {
|
|
2
2
|
then<TResult1 = T, TResult2 = never>(
|
|
3
|
-
onfulfilled?:
|
|
4
|
-
|
|
3
|
+
onfulfilled?:
|
|
4
|
+
| ((value: T) => TResult1 | PromiseLike<TResult1>)
|
|
5
|
+
| null
|
|
6
|
+
| undefined,
|
|
7
|
+
onrejected?:
|
|
8
|
+
| ((reason: any) => TResult2 | PromiseLike<TResult2>)
|
|
9
|
+
| null
|
|
10
|
+
| undefined
|
|
5
11
|
): SyncPromise<TResult1 | TResult2>;
|
|
6
12
|
|
|
7
13
|
valueOrPromise(): T | PromiseLike<T>;
|
|
@@ -22,9 +28,9 @@ export const SyncPromise: SyncPromiseStatic = {
|
|
|
22
28
|
},
|
|
23
29
|
resolve<T>(value?: T | PromiseLike<T>): SyncPromise<T> {
|
|
24
30
|
if (
|
|
25
|
-
typeof value ===
|
|
31
|
+
typeof value === "object" &&
|
|
26
32
|
value !== null &&
|
|
27
|
-
typeof (value as PromiseLike<T>).then ===
|
|
33
|
+
typeof (value as PromiseLike<T>).then === "function"
|
|
28
34
|
) {
|
|
29
35
|
if (
|
|
30
36
|
value instanceof PendingSyncPromise ||
|
|
@@ -45,7 +51,7 @@ export const SyncPromise: SyncPromiseStatic = {
|
|
|
45
51
|
} catch (e) {
|
|
46
52
|
return SyncPromise.reject(e);
|
|
47
53
|
}
|
|
48
|
-
}
|
|
54
|
+
},
|
|
49
55
|
};
|
|
50
56
|
|
|
51
57
|
class PendingSyncPromise<T> implements SyncPromise<T> {
|
|
@@ -56,8 +62,14 @@ class PendingSyncPromise<T> implements SyncPromise<T> {
|
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
public then<TResult1 = T, TResult2 = never>(
|
|
59
|
-
onfulfilled?:
|
|
60
|
-
|
|
65
|
+
onfulfilled?:
|
|
66
|
+
| ((value: T) => TResult1 | PromiseLike<TResult1>)
|
|
67
|
+
| null
|
|
68
|
+
| undefined,
|
|
69
|
+
onrejected?:
|
|
70
|
+
| ((reason: any) => TResult2 | PromiseLike<TResult2>)
|
|
71
|
+
| null
|
|
72
|
+
| undefined
|
|
61
73
|
) {
|
|
62
74
|
return new PendingSyncPromise<TResult1 | TResult2>(
|
|
63
75
|
this.promise.then(onfulfilled, onrejected)
|
|
@@ -76,9 +88,11 @@ class ResolvedSyncPromise<T> implements SyncPromise<T> {
|
|
|
76
88
|
this.value = value;
|
|
77
89
|
}
|
|
78
90
|
|
|
79
|
-
public then<TResult1 = T
|
|
80
|
-
onfulfilled?:
|
|
81
|
-
|
|
91
|
+
public then<TResult1 = T>(
|
|
92
|
+
onfulfilled?:
|
|
93
|
+
| ((value: T) => TResult1 | PromiseLike<TResult1>)
|
|
94
|
+
| null
|
|
95
|
+
| undefined
|
|
82
96
|
) {
|
|
83
97
|
if (!onfulfilled) {
|
|
84
98
|
return this as any;
|
|
@@ -99,8 +113,14 @@ class RejectedSyncPromise<T> implements SyncPromise<T> {
|
|
|
99
113
|
}
|
|
100
114
|
|
|
101
115
|
public then<TResult1 = T, TResult2 = never>(
|
|
102
|
-
onfulfilled?:
|
|
103
|
-
|
|
116
|
+
onfulfilled?:
|
|
117
|
+
| ((value: T) => TResult1 | PromiseLike<TResult1>)
|
|
118
|
+
| null
|
|
119
|
+
| undefined,
|
|
120
|
+
onrejected?:
|
|
121
|
+
| ((reason: any) => TResult2 | PromiseLike<TResult2>)
|
|
122
|
+
| null
|
|
123
|
+
| undefined
|
|
104
124
|
) {
|
|
105
125
|
if (!onrejected) {
|
|
106
126
|
return this as any;
|
package/src/types/bigint.ts
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
getBigInt64,
|
|
3
|
+
getBigUint64,
|
|
4
|
+
setBigInt64,
|
|
5
|
+
setBigUint64,
|
|
6
|
+
} from "@yume-chan/dataview-bigint-polyfill/esm/fallback.js";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
StructFieldDefinition,
|
|
10
|
+
StructFieldValue,
|
|
11
|
+
type StructAsyncDeserializeStream,
|
|
12
|
+
type StructDeserializeStream,
|
|
13
|
+
type StructOptions,
|
|
14
|
+
type StructValue,
|
|
15
|
+
} from "../basic/index.js";
|
|
3
16
|
import { SyncPromise } from "../sync-promise.js";
|
|
4
|
-
import type
|
|
17
|
+
import { type ValueOrPromise } from "../utils.js";
|
|
5
18
|
|
|
6
|
-
type DataViewBigInt64Getter = (
|
|
19
|
+
type DataViewBigInt64Getter = (
|
|
20
|
+
dataView: DataView,
|
|
21
|
+
byteOffset: number,
|
|
22
|
+
littleEndian: boolean | undefined
|
|
23
|
+
) => bigint;
|
|
7
24
|
|
|
8
|
-
type DataViewBigInt64Setter = (
|
|
25
|
+
type DataViewBigInt64Setter = (
|
|
26
|
+
dataView: DataView,
|
|
27
|
+
byteOffset: number,
|
|
28
|
+
value: bigint,
|
|
29
|
+
littleEndian: boolean | undefined
|
|
30
|
+
) => void;
|
|
9
31
|
|
|
10
32
|
export class BigIntFieldType {
|
|
11
33
|
public readonly TTypeScriptType!: bigint;
|
|
@@ -19,28 +41,34 @@ export class BigIntFieldType {
|
|
|
19
41
|
public constructor(
|
|
20
42
|
size: number,
|
|
21
43
|
getter: DataViewBigInt64Getter,
|
|
22
|
-
setter: DataViewBigInt64Setter
|
|
44
|
+
setter: DataViewBigInt64Setter
|
|
23
45
|
) {
|
|
24
46
|
this.size = size;
|
|
25
47
|
this.getter = getter;
|
|
26
48
|
this.setter = setter;
|
|
27
49
|
}
|
|
28
50
|
|
|
29
|
-
public static readonly Int64 = new BigIntFieldType(
|
|
51
|
+
public static readonly Int64 = new BigIntFieldType(
|
|
52
|
+
8,
|
|
53
|
+
getBigInt64,
|
|
54
|
+
setBigInt64
|
|
55
|
+
);
|
|
30
56
|
|
|
31
|
-
public static readonly Uint64 = new BigIntFieldType(
|
|
57
|
+
public static readonly Uint64 = new BigIntFieldType(
|
|
58
|
+
8,
|
|
59
|
+
getBigUint64,
|
|
60
|
+
setBigUint64
|
|
61
|
+
);
|
|
32
62
|
}
|
|
33
63
|
|
|
34
64
|
export class BigIntFieldDefinition<
|
|
35
65
|
TType extends BigIntFieldType = BigIntFieldType,
|
|
36
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
37
|
-
|
|
38
|
-
void,
|
|
39
|
-
TTypeScriptType
|
|
40
|
-
> {
|
|
66
|
+
TTypeScriptType = TType["TTypeScriptType"]
|
|
67
|
+
> extends StructFieldDefinition<void, TTypeScriptType> {
|
|
41
68
|
public readonly type: TType;
|
|
42
69
|
|
|
43
|
-
public constructor(type: TType,
|
|
70
|
+
public constructor(type: TType, typescriptType?: TTypeScriptType) {
|
|
71
|
+
void typescriptType;
|
|
44
72
|
super();
|
|
45
73
|
this.type = type;
|
|
46
74
|
}
|
|
@@ -52,7 +80,7 @@ export class BigIntFieldDefinition<
|
|
|
52
80
|
public create(
|
|
53
81
|
options: Readonly<StructOptions>,
|
|
54
82
|
struct: StructValue,
|
|
55
|
-
value: TTypeScriptType
|
|
83
|
+
value: TTypeScriptType
|
|
56
84
|
): BigIntFieldValue<this> {
|
|
57
85
|
return new BigIntFieldValue(this, options, struct, value);
|
|
58
86
|
}
|
|
@@ -60,29 +88,28 @@ export class BigIntFieldDefinition<
|
|
|
60
88
|
public override deserialize(
|
|
61
89
|
options: Readonly<StructOptions>,
|
|
62
90
|
stream: StructDeserializeStream,
|
|
63
|
-
struct: StructValue
|
|
91
|
+
struct: StructValue
|
|
64
92
|
): BigIntFieldValue<this>;
|
|
65
93
|
public override deserialize(
|
|
66
94
|
options: Readonly<StructOptions>,
|
|
67
95
|
stream: StructAsyncDeserializeStream,
|
|
68
|
-
struct: StructValue
|
|
96
|
+
struct: StructValue
|
|
69
97
|
): Promise<BigIntFieldValue<this>>;
|
|
70
98
|
public override deserialize(
|
|
71
99
|
options: Readonly<StructOptions>,
|
|
72
100
|
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
|
73
|
-
struct: StructValue
|
|
101
|
+
struct: StructValue
|
|
74
102
|
): ValueOrPromise<BigIntFieldValue<this>> {
|
|
75
|
-
return SyncPromise
|
|
76
|
-
.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
0,
|
|
84
|
-
options.littleEndian
|
|
103
|
+
return SyncPromise.try(() => {
|
|
104
|
+
return stream.read(this.getSize());
|
|
105
|
+
})
|
|
106
|
+
.then((array) => {
|
|
107
|
+
const view = new DataView(
|
|
108
|
+
array.buffer,
|
|
109
|
+
array.byteOffset,
|
|
110
|
+
array.byteLength
|
|
85
111
|
);
|
|
112
|
+
const value = this.type.getter(view, 0, options.littleEndian);
|
|
86
113
|
return this.create(options, struct, value as any);
|
|
87
114
|
})
|
|
88
115
|
.valueOrPromise();
|
|
@@ -90,13 +117,13 @@ export class BigIntFieldDefinition<
|
|
|
90
117
|
}
|
|
91
118
|
|
|
92
119
|
export class BigIntFieldValue<
|
|
93
|
-
TDefinition extends BigIntFieldDefinition<BigIntFieldType, any
|
|
94
|
-
|
|
120
|
+
TDefinition extends BigIntFieldDefinition<BigIntFieldType, any>
|
|
121
|
+
> extends StructFieldValue<TDefinition> {
|
|
95
122
|
public serialize(dataView: DataView, offset: number): void {
|
|
96
123
|
this.definition.type.setter(
|
|
97
124
|
dataView,
|
|
98
125
|
offset,
|
|
99
|
-
this.value
|
|
126
|
+
this.value,
|
|
100
127
|
this.options.littleEndian
|
|
101
128
|
);
|
|
102
129
|
}
|
package/src/types/buffer/base.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
StructFieldDefinition,
|
|
3
|
+
StructFieldValue,
|
|
4
|
+
type StructAsyncDeserializeStream,
|
|
5
|
+
type StructDeserializeStream,
|
|
6
|
+
type StructOptions,
|
|
7
|
+
type StructValue,
|
|
8
|
+
} from "../../basic/index.js";
|
|
2
9
|
import { SyncPromise } from "../../sync-promise.js";
|
|
3
10
|
import { decodeUtf8, encodeUtf8, type ValueOrPromise } from "../../utils.js";
|
|
4
11
|
|
|
@@ -11,7 +18,10 @@ import { decodeUtf8, encodeUtf8, type ValueOrPromise } from "../../utils.js";
|
|
|
11
18
|
* @template TTypeScriptType Optional another type (should be compatible with `TType`)
|
|
12
19
|
* specified by user when creating field definitions.
|
|
13
20
|
*/
|
|
14
|
-
export abstract class BufferFieldSubType<
|
|
21
|
+
export abstract class BufferFieldSubType<
|
|
22
|
+
TValue = unknown,
|
|
23
|
+
TTypeScriptType = TValue
|
|
24
|
+
> {
|
|
15
25
|
public readonly TTypeScriptType!: TTypeScriptType;
|
|
16
26
|
|
|
17
27
|
/**
|
|
@@ -36,8 +46,9 @@ export abstract class BufferFieldSubType<TValue = unknown, TTypeScriptType = TVa
|
|
|
36
46
|
}
|
|
37
47
|
|
|
38
48
|
/** An `BufferFieldSubType` that's actually an `Uint8Array` */
|
|
39
|
-
export class Uint8ArrayBufferFieldSubType<
|
|
40
|
-
|
|
49
|
+
export class Uint8ArrayBufferFieldSubType<
|
|
50
|
+
TTypeScriptType = Uint8Array
|
|
51
|
+
> extends BufferFieldSubType<Uint8Array, TTypeScriptType> {
|
|
41
52
|
public static readonly Instance = new Uint8ArrayBufferFieldSubType();
|
|
42
53
|
|
|
43
54
|
protected constructor() {
|
|
@@ -58,8 +69,9 @@ export class Uint8ArrayBufferFieldSubType<TTypeScriptType = Uint8Array>
|
|
|
58
69
|
}
|
|
59
70
|
|
|
60
71
|
/** An `BufferFieldSubType` that converts between `Uint8Array` and `string` */
|
|
61
|
-
export class StringBufferFieldSubType<
|
|
62
|
-
|
|
72
|
+
export class StringBufferFieldSubType<
|
|
73
|
+
TTypeScriptType = string
|
|
74
|
+
> extends BufferFieldSubType<string, TTypeScriptType> {
|
|
63
75
|
public static readonly Instance = new StringBufferFieldSubType();
|
|
64
76
|
|
|
65
77
|
public toBuffer(value: string): Uint8Array {
|
|
@@ -81,15 +93,14 @@ export class StringBufferFieldSubType<TTypeScriptType = string>
|
|
|
81
93
|
export const EMPTY_UINT8_ARRAY = new Uint8Array(0);
|
|
82
94
|
|
|
83
95
|
export abstract class BufferLikeFieldDefinition<
|
|
84
|
-
TType extends BufferFieldSubType<any, any> = BufferFieldSubType<
|
|
96
|
+
TType extends BufferFieldSubType<any, any> = BufferFieldSubType<
|
|
97
|
+
unknown,
|
|
98
|
+
unknown
|
|
99
|
+
>,
|
|
85
100
|
TOptions = void,
|
|
86
101
|
TOmitInitKey extends PropertyKey = never,
|
|
87
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
88
|
-
|
|
89
|
-
TOptions,
|
|
90
|
-
TTypeScriptType,
|
|
91
|
-
TOmitInitKey
|
|
92
|
-
>{
|
|
102
|
+
TTypeScriptType = TType["TTypeScriptType"]
|
|
103
|
+
> extends StructFieldDefinition<TOptions, TTypeScriptType, TOmitInitKey> {
|
|
93
104
|
public readonly type: TType;
|
|
94
105
|
|
|
95
106
|
public constructor(type: TType, options: TOptions) {
|
|
@@ -98,6 +109,7 @@ export abstract class BufferLikeFieldDefinition<
|
|
|
98
109
|
}
|
|
99
110
|
|
|
100
111
|
protected getDeserializeSize(struct: StructValue): number {
|
|
112
|
+
void struct;
|
|
101
113
|
return this.getSize();
|
|
102
114
|
}
|
|
103
115
|
|
|
@@ -107,8 +119,8 @@ export abstract class BufferLikeFieldDefinition<
|
|
|
107
119
|
public create(
|
|
108
120
|
options: Readonly<StructOptions>,
|
|
109
121
|
struct: StructValue,
|
|
110
|
-
value: TType[
|
|
111
|
-
array?: Uint8Array
|
|
122
|
+
value: TType["TTypeScriptType"],
|
|
123
|
+
array?: Uint8Array
|
|
112
124
|
): BufferLikeFieldValue<this> {
|
|
113
125
|
return new BufferLikeFieldValue(this, options, struct, value, array);
|
|
114
126
|
}
|
|
@@ -116,28 +128,27 @@ export abstract class BufferLikeFieldDefinition<
|
|
|
116
128
|
public override deserialize(
|
|
117
129
|
options: Readonly<StructOptions>,
|
|
118
130
|
stream: StructDeserializeStream,
|
|
119
|
-
struct: StructValue
|
|
131
|
+
struct: StructValue
|
|
120
132
|
): BufferLikeFieldValue<this>;
|
|
121
133
|
public override deserialize(
|
|
122
134
|
options: Readonly<StructOptions>,
|
|
123
135
|
stream: StructAsyncDeserializeStream,
|
|
124
|
-
struct: StructValue
|
|
136
|
+
struct: StructValue
|
|
125
137
|
): Promise<BufferLikeFieldValue<this>>;
|
|
126
138
|
public override deserialize(
|
|
127
139
|
options: Readonly<StructOptions>,
|
|
128
140
|
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
|
129
|
-
struct: StructValue
|
|
141
|
+
struct: StructValue
|
|
130
142
|
): ValueOrPromise<BufferLikeFieldValue<this>> {
|
|
131
|
-
return SyncPromise
|
|
132
|
-
.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
.then(array => {
|
|
143
|
+
return SyncPromise.try(() => {
|
|
144
|
+
const size = this.getDeserializeSize(struct);
|
|
145
|
+
if (size === 0) {
|
|
146
|
+
return EMPTY_UINT8_ARRAY;
|
|
147
|
+
} else {
|
|
148
|
+
return stream.read(size);
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
.then((array) => {
|
|
141
152
|
const value = this.type.toValue(array);
|
|
142
153
|
return this.create(options, struct, value, array);
|
|
143
154
|
})
|
|
@@ -146,22 +157,26 @@ export abstract class BufferLikeFieldDefinition<
|
|
|
146
157
|
}
|
|
147
158
|
|
|
148
159
|
export class BufferLikeFieldValue<
|
|
149
|
-
TDefinition extends BufferLikeFieldDefinition<
|
|
150
|
-
|
|
160
|
+
TDefinition extends BufferLikeFieldDefinition<
|
|
161
|
+
BufferFieldSubType<unknown, unknown>,
|
|
162
|
+
any,
|
|
163
|
+
any
|
|
164
|
+
>
|
|
165
|
+
> extends StructFieldValue<TDefinition> {
|
|
151
166
|
protected array: Uint8Array | undefined;
|
|
152
167
|
|
|
153
168
|
public constructor(
|
|
154
169
|
definition: TDefinition,
|
|
155
170
|
options: Readonly<StructOptions>,
|
|
156
171
|
struct: StructValue,
|
|
157
|
-
value: TDefinition[
|
|
158
|
-
array?: Uint8Array
|
|
172
|
+
value: TDefinition["TValue"],
|
|
173
|
+
array?: Uint8Array
|
|
159
174
|
) {
|
|
160
175
|
super(definition, options, struct, value);
|
|
161
176
|
this.array = array;
|
|
162
177
|
}
|
|
163
178
|
|
|
164
|
-
public override set(value: TDefinition[
|
|
179
|
+
public override set(value: TDefinition["TValue"]): void {
|
|
165
180
|
super.set(value);
|
|
166
181
|
// When value changes, clear the cached `array`
|
|
167
182
|
// It will be lazily calculated in `serialize()`
|
|
@@ -173,7 +188,10 @@ export class BufferLikeFieldValue<
|
|
|
173
188
|
this.array = this.definition.type.toBuffer(this.value);
|
|
174
189
|
}
|
|
175
190
|
|
|
176
|
-
new Uint8Array(
|
|
177
|
-
.
|
|
191
|
+
new Uint8Array(
|
|
192
|
+
dataView.buffer,
|
|
193
|
+
dataView.byteOffset,
|
|
194
|
+
dataView.byteLength
|
|
195
|
+
).set(this.array, offset);
|
|
178
196
|
}
|
|
179
197
|
}
|
|
@@ -7,14 +7,9 @@ export interface FixedLengthBufferLikeFieldOptions {
|
|
|
7
7
|
export class FixedLengthBufferLikeFieldDefinition<
|
|
8
8
|
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
9
9
|
TOptions extends FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
|
|
10
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
11
|
-
|
|
12
|
-
TType,
|
|
13
|
-
TOptions,
|
|
14
|
-
never,
|
|
15
|
-
TTypeScriptType
|
|
16
|
-
> {
|
|
10
|
+
TTypeScriptType = TType["TTypeScriptType"]
|
|
11
|
+
> extends BufferLikeFieldDefinition<TType, TOptions, never, TTypeScriptType> {
|
|
17
12
|
public getSize(): number {
|
|
18
13
|
return this.options.length;
|
|
19
14
|
}
|
|
20
|
-
}
|
|
15
|
+
}
|
|
@@ -1,13 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
StructFieldValue,
|
|
3
|
+
type StructFieldDefinition,
|
|
4
|
+
type StructOptions,
|
|
5
|
+
type StructValue,
|
|
6
|
+
} from "../../basic/index.js";
|
|
7
|
+
import { type KeysOfType } from "../../utils.js";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
BufferLikeFieldDefinition,
|
|
11
|
+
BufferLikeFieldValue,
|
|
12
|
+
type BufferFieldSubType,
|
|
13
|
+
} from "./base.js";
|
|
4
14
|
|
|
5
15
|
export type LengthField<TFields> = KeysOfType<TFields, number | string>;
|
|
6
16
|
|
|
7
17
|
export interface VariableLengthBufferLikeFieldOptions<
|
|
8
18
|
TFields = object,
|
|
9
|
-
TLengthField extends LengthField<TFields> = any
|
|
10
|
-
|
|
19
|
+
TLengthField extends LengthField<TFields> = any
|
|
20
|
+
> {
|
|
11
21
|
/**
|
|
12
22
|
* The name of the field that contains the length of the buffer.
|
|
13
23
|
*
|
|
@@ -27,20 +37,20 @@ export interface VariableLengthBufferLikeFieldOptions<
|
|
|
27
37
|
export class VariableLengthBufferLikeFieldDefinition<
|
|
28
38
|
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
29
39
|
TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions,
|
|
30
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
31
|
-
|
|
40
|
+
TTypeScriptType = TType["TTypeScriptType"]
|
|
41
|
+
> extends BufferLikeFieldDefinition<
|
|
32
42
|
TType,
|
|
33
43
|
TOptions,
|
|
34
|
-
TOptions[
|
|
44
|
+
TOptions["lengthField"],
|
|
35
45
|
TTypeScriptType
|
|
36
|
-
|
|
46
|
+
> {
|
|
37
47
|
public getSize(): number {
|
|
38
48
|
return 0;
|
|
39
49
|
}
|
|
40
50
|
|
|
41
51
|
protected override getDeserializeSize(struct: StructValue) {
|
|
42
52
|
let value = struct.value[this.options.lengthField] as number | string;
|
|
43
|
-
if (typeof value ===
|
|
53
|
+
if (typeof value === "string") {
|
|
44
54
|
value = Number.parseInt(value, this.options.lengthFieldRadix ?? 10);
|
|
45
55
|
}
|
|
46
56
|
return value;
|
|
@@ -57,14 +67,14 @@ export class VariableLengthBufferLikeFieldDefinition<
|
|
|
57
67
|
options,
|
|
58
68
|
struct,
|
|
59
69
|
value,
|
|
60
|
-
array
|
|
70
|
+
array
|
|
61
71
|
);
|
|
62
72
|
}
|
|
63
73
|
}
|
|
64
74
|
|
|
65
75
|
export class VariableLengthBufferLikeStructFieldValue<
|
|
66
|
-
TDefinition extends VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition
|
|
67
|
-
|
|
76
|
+
TDefinition extends VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition
|
|
77
|
+
> extends BufferLikeFieldValue<TDefinition> {
|
|
68
78
|
protected length: number | undefined;
|
|
69
79
|
|
|
70
80
|
protected lengthFieldValue: VariableLengthBufferLikeFieldLengthValue;
|
|
@@ -73,8 +83,8 @@ export class VariableLengthBufferLikeStructFieldValue<
|
|
|
73
83
|
definition: TDefinition,
|
|
74
84
|
options: Readonly<StructOptions>,
|
|
75
85
|
struct: StructValue,
|
|
76
|
-
value: TDefinition[
|
|
77
|
-
array?: Uint8Array
|
|
86
|
+
value: TDefinition["TValue"],
|
|
87
|
+
array?: Uint8Array
|
|
78
88
|
) {
|
|
79
89
|
super(definition, options, struct, value, array);
|
|
80
90
|
|
|
@@ -88,7 +98,7 @@ export class VariableLengthBufferLikeStructFieldValue<
|
|
|
88
98
|
const originalValue = struct.get(lengthField);
|
|
89
99
|
this.lengthFieldValue = new VariableLengthBufferLikeFieldLengthValue(
|
|
90
100
|
originalValue,
|
|
91
|
-
this
|
|
101
|
+
this
|
|
92
102
|
);
|
|
93
103
|
struct.set(lengthField, this.lengthFieldValue);
|
|
94
104
|
}
|
|
@@ -113,20 +123,25 @@ export class VariableLengthBufferLikeStructFieldValue<
|
|
|
113
123
|
}
|
|
114
124
|
|
|
115
125
|
// Not using `VariableLengthBufferLikeStructFieldValue` directly makes writing tests much easier...
|
|
116
|
-
type VariableLengthBufferLikeFieldValueLike =
|
|
117
|
-
|
|
126
|
+
type VariableLengthBufferLikeFieldValueLike = StructFieldValue<
|
|
127
|
+
StructFieldDefinition<VariableLengthBufferLikeFieldOptions, any, any>
|
|
128
|
+
>;
|
|
118
129
|
|
|
119
|
-
export class VariableLengthBufferLikeFieldLengthValue
|
|
120
|
-
extends StructFieldValue {
|
|
130
|
+
export class VariableLengthBufferLikeFieldLengthValue extends StructFieldValue {
|
|
121
131
|
protected originalField: StructFieldValue;
|
|
122
132
|
|
|
123
133
|
protected bufferField: VariableLengthBufferLikeFieldValueLike;
|
|
124
134
|
|
|
125
135
|
public constructor(
|
|
126
136
|
originalField: StructFieldValue,
|
|
127
|
-
arrayBufferField: VariableLengthBufferLikeFieldValueLike
|
|
137
|
+
arrayBufferField: VariableLengthBufferLikeFieldValueLike
|
|
128
138
|
) {
|
|
129
|
-
super(
|
|
139
|
+
super(
|
|
140
|
+
originalField.definition,
|
|
141
|
+
originalField.options,
|
|
142
|
+
originalField.struct,
|
|
143
|
+
0
|
|
144
|
+
);
|
|
130
145
|
this.originalField = originalField;
|
|
131
146
|
this.bufferField = arrayBufferField;
|
|
132
147
|
}
|
|
@@ -139,8 +154,10 @@ export class VariableLengthBufferLikeFieldLengthValue
|
|
|
139
154
|
let value: string | number = this.bufferField.getSize();
|
|
140
155
|
|
|
141
156
|
const originalValue = this.originalField.get();
|
|
142
|
-
if (typeof originalValue ===
|
|
143
|
-
value = value.toString(
|
|
157
|
+
if (typeof originalValue === "string") {
|
|
158
|
+
value = value.toString(
|
|
159
|
+
this.bufferField.definition.options.lengthFieldRadix ?? 10
|
|
160
|
+
);
|
|
144
161
|
}
|
|
145
162
|
|
|
146
163
|
return value;
|