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