@yume-chan/struct 0.0.17 → 0.0.19
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 +43 -43
- package/esm/basic/definition.d.ts.map +1 -1
- package/esm/basic/definition.js +23 -23
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.d.ts +38 -38
- package/esm/basic/field-value.d.ts.map +1 -1
- package/esm/basic/field-value.js +45 -45
- package/esm/basic/field-value.js.map +1 -1
- package/esm/basic/index.d.ts +5 -5
- package/esm/basic/index.js +5 -5
- package/esm/basic/options.d.ts +9 -9
- package/esm/basic/options.js +3 -3
- package/esm/basic/stream.d.ts +19 -19
- package/esm/basic/stream.js +1 -1
- package/esm/basic/struct-value.d.ts +25 -25
- package/esm/basic/struct-value.d.ts.map +1 -1
- package/esm/basic/struct-value.js +56 -49
- package/esm/basic/struct-value.js.map +1 -1
- package/esm/index.d.ts +13 -13
- package/esm/index.js +5 -5
- package/esm/struct.d.ts +130 -129
- package/esm/struct.d.ts.map +1 -1
- package/esm/struct.js +210 -208
- package/esm/struct.js.map +1 -1
- package/esm/sync-promise.d.ts +12 -12
- package/esm/sync-promise.d.ts.map +1 -1
- package/esm/sync-promise.js +70 -70
- package/esm/sync-promise.js.map +1 -1
- package/esm/types/bigint.d.ts +25 -24
- package/esm/types/bigint.d.ts.map +1 -1
- package/esm/types/bigint.js +47 -46
- package/esm/types/bigint.js.map +1 -1
- package/esm/types/buffer/base.d.ts +64 -63
- package/esm/types/buffer/base.d.ts.map +1 -1
- package/esm/types/buffer/base.js +102 -101
- package/esm/types/buffer/base.js.map +1 -1
- package/esm/types/buffer/fixed-length.d.ts +8 -7
- package/esm/types/buffer/fixed-length.d.ts.map +1 -1
- package/esm/types/buffer/fixed-length.js +6 -7
- package/esm/types/buffer/fixed-length.js.map +1 -1
- package/esm/types/buffer/index.d.ts +3 -3
- package/esm/types/buffer/index.js +3 -3
- package/esm/types/buffer/variable-length.d.ts +44 -42
- package/esm/types/buffer/variable-length.d.ts.map +1 -1
- package/esm/types/buffer/variable-length.js +75 -75
- package/esm/types/buffer/variable-length.js.map +1 -1
- package/esm/types/index.d.ts +3 -3
- package/esm/types/index.js +3 -3
- package/esm/types/number.d.ts +27 -32
- package/esm/types/number.d.ts.map +1 -1
- package/esm/types/number.js +113 -64
- package/esm/types/number.js.map +1 -1
- package/esm/utils.d.ts +47 -47
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +15 -22
- package/esm/utils.js.map +1 -1
- package/package.json +13 -9
- package/src/basic/definition.ts +10 -7
- package/src/basic/field-value.ts +15 -12
- package/src/basic/index.ts +5 -5
- package/src/basic/struct-value.ts +16 -10
- package/src/index.ts +5 -5
- package/src/struct.ts +205 -200
- package/src/sync-promise.ts +32 -12
- package/src/types/bigint.ts +56 -30
- package/src/types/buffer/base.ts +54 -36
- package/src/types/buffer/fixed-length.ts +5 -9
- package/src/types/buffer/variable-length.ts +38 -24
- package/src/types/number.ts +128 -77
- package/src/utils.ts +30 -8
- package/tsconfig.build.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
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,32 @@
|
|
|
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 type {
|
|
9
|
+
StructAsyncDeserializeStream,
|
|
10
|
+
StructDeserializeStream,
|
|
11
|
+
StructOptions,
|
|
12
|
+
StructValue,
|
|
13
|
+
} from "../basic/index.js";
|
|
14
|
+
import { StructFieldDefinition, StructFieldValue } from "../basic/index.js";
|
|
3
15
|
import { SyncPromise } from "../sync-promise.js";
|
|
4
16
|
import type { ValueOrPromise } from "../utils.js";
|
|
5
17
|
|
|
6
|
-
type DataViewBigInt64Getter = (
|
|
18
|
+
type DataViewBigInt64Getter = (
|
|
19
|
+
dataView: DataView,
|
|
20
|
+
byteOffset: number,
|
|
21
|
+
littleEndian: boolean | undefined
|
|
22
|
+
) => bigint;
|
|
7
23
|
|
|
8
|
-
type DataViewBigInt64Setter = (
|
|
24
|
+
type DataViewBigInt64Setter = (
|
|
25
|
+
dataView: DataView,
|
|
26
|
+
byteOffset: number,
|
|
27
|
+
value: bigint,
|
|
28
|
+
littleEndian: boolean | undefined
|
|
29
|
+
) => void;
|
|
9
30
|
|
|
10
31
|
export class BigIntFieldType {
|
|
11
32
|
public readonly TTypeScriptType!: bigint;
|
|
@@ -19,28 +40,34 @@ export class BigIntFieldType {
|
|
|
19
40
|
public constructor(
|
|
20
41
|
size: number,
|
|
21
42
|
getter: DataViewBigInt64Getter,
|
|
22
|
-
setter: DataViewBigInt64Setter
|
|
43
|
+
setter: DataViewBigInt64Setter
|
|
23
44
|
) {
|
|
24
45
|
this.size = size;
|
|
25
46
|
this.getter = getter;
|
|
26
47
|
this.setter = setter;
|
|
27
48
|
}
|
|
28
49
|
|
|
29
|
-
public static readonly Int64 = new BigIntFieldType(
|
|
50
|
+
public static readonly Int64 = new BigIntFieldType(
|
|
51
|
+
8,
|
|
52
|
+
getBigInt64,
|
|
53
|
+
setBigInt64
|
|
54
|
+
);
|
|
30
55
|
|
|
31
|
-
public static readonly Uint64 = new BigIntFieldType(
|
|
56
|
+
public static readonly Uint64 = new BigIntFieldType(
|
|
57
|
+
8,
|
|
58
|
+
getBigUint64,
|
|
59
|
+
setBigUint64
|
|
60
|
+
);
|
|
32
61
|
}
|
|
33
62
|
|
|
34
63
|
export class BigIntFieldDefinition<
|
|
35
64
|
TType extends BigIntFieldType = BigIntFieldType,
|
|
36
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
37
|
-
|
|
38
|
-
void,
|
|
39
|
-
TTypeScriptType
|
|
40
|
-
> {
|
|
65
|
+
TTypeScriptType = TType["TTypeScriptType"]
|
|
66
|
+
> extends StructFieldDefinition<void, TTypeScriptType> {
|
|
41
67
|
public readonly type: TType;
|
|
42
68
|
|
|
43
|
-
public constructor(type: TType,
|
|
69
|
+
public constructor(type: TType, typescriptType?: TTypeScriptType) {
|
|
70
|
+
void typescriptType;
|
|
44
71
|
super();
|
|
45
72
|
this.type = type;
|
|
46
73
|
}
|
|
@@ -52,7 +79,7 @@ export class BigIntFieldDefinition<
|
|
|
52
79
|
public create(
|
|
53
80
|
options: Readonly<StructOptions>,
|
|
54
81
|
struct: StructValue,
|
|
55
|
-
value: TTypeScriptType
|
|
82
|
+
value: TTypeScriptType
|
|
56
83
|
): BigIntFieldValue<this> {
|
|
57
84
|
return new BigIntFieldValue(this, options, struct, value);
|
|
58
85
|
}
|
|
@@ -60,29 +87,28 @@ export class BigIntFieldDefinition<
|
|
|
60
87
|
public override deserialize(
|
|
61
88
|
options: Readonly<StructOptions>,
|
|
62
89
|
stream: StructDeserializeStream,
|
|
63
|
-
struct: StructValue
|
|
90
|
+
struct: StructValue
|
|
64
91
|
): BigIntFieldValue<this>;
|
|
65
92
|
public override deserialize(
|
|
66
93
|
options: Readonly<StructOptions>,
|
|
67
94
|
stream: StructAsyncDeserializeStream,
|
|
68
|
-
struct: StructValue
|
|
95
|
+
struct: StructValue
|
|
69
96
|
): Promise<BigIntFieldValue<this>>;
|
|
70
97
|
public override deserialize(
|
|
71
98
|
options: Readonly<StructOptions>,
|
|
72
99
|
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
|
73
|
-
struct: StructValue
|
|
100
|
+
struct: StructValue
|
|
74
101
|
): ValueOrPromise<BigIntFieldValue<this>> {
|
|
75
|
-
return SyncPromise
|
|
76
|
-
.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
0,
|
|
84
|
-
options.littleEndian
|
|
102
|
+
return SyncPromise.try(() => {
|
|
103
|
+
return stream.read(this.getSize());
|
|
104
|
+
})
|
|
105
|
+
.then((array) => {
|
|
106
|
+
const view = new DataView(
|
|
107
|
+
array.buffer,
|
|
108
|
+
array.byteOffset,
|
|
109
|
+
array.byteLength
|
|
85
110
|
);
|
|
111
|
+
const value = this.type.getter(view, 0, options.littleEndian);
|
|
86
112
|
return this.create(options, struct, value as any);
|
|
87
113
|
})
|
|
88
114
|
.valueOrPromise();
|
|
@@ -90,13 +116,13 @@ export class BigIntFieldDefinition<
|
|
|
90
116
|
}
|
|
91
117
|
|
|
92
118
|
export class BigIntFieldValue<
|
|
93
|
-
TDefinition extends BigIntFieldDefinition<BigIntFieldType, any
|
|
94
|
-
|
|
119
|
+
TDefinition extends BigIntFieldDefinition<BigIntFieldType, any>
|
|
120
|
+
> extends StructFieldValue<TDefinition> {
|
|
95
121
|
public serialize(dataView: DataView, offset: number): void {
|
|
96
122
|
this.definition.type.setter(
|
|
97
123
|
dataView,
|
|
98
124
|
offset,
|
|
99
|
-
this.value
|
|
125
|
+
this.value,
|
|
100
126
|
this.options.littleEndian
|
|
101
127
|
);
|
|
102
128
|
}
|
package/src/types/buffer/base.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type {
|
|
2
|
+
StructAsyncDeserializeStream,
|
|
3
|
+
StructDeserializeStream,
|
|
4
|
+
StructOptions,
|
|
5
|
+
StructValue,
|
|
6
|
+
} from "../../basic/index.js";
|
|
7
|
+
import { StructFieldDefinition, StructFieldValue } from "../../basic/index.js";
|
|
2
8
|
import { SyncPromise } from "../../sync-promise.js";
|
|
3
|
-
import {
|
|
9
|
+
import type { ValueOrPromise } from "../../utils.js";
|
|
10
|
+
import { decodeUtf8, encodeUtf8 } from "../../utils.js";
|
|
4
11
|
|
|
5
12
|
/**
|
|
6
13
|
* Base class for all types that
|
|
@@ -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
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { BufferFieldSubType } from "./base.js";
|
|
2
|
+
import { BufferLikeFieldDefinition } from "./base.js";
|
|
2
3
|
|
|
3
4
|
export interface FixedLengthBufferLikeFieldOptions {
|
|
4
5
|
length: number;
|
|
@@ -7,14 +8,9 @@ export interface FixedLengthBufferLikeFieldOptions {
|
|
|
7
8
|
export class FixedLengthBufferLikeFieldDefinition<
|
|
8
9
|
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
9
10
|
TOptions extends FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
|
|
10
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
11
|
-
|
|
12
|
-
TType,
|
|
13
|
-
TOptions,
|
|
14
|
-
never,
|
|
15
|
-
TTypeScriptType
|
|
16
|
-
> {
|
|
11
|
+
TTypeScriptType = TType["TTypeScriptType"]
|
|
12
|
+
> extends BufferLikeFieldDefinition<TType, TOptions, never, TTypeScriptType> {
|
|
17
13
|
public getSize(): number {
|
|
18
14
|
return this.options.length;
|
|
19
15
|
}
|
|
20
|
-
}
|
|
16
|
+
}
|
|
@@ -1,13 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type {
|
|
2
|
+
StructFieldDefinition,
|
|
3
|
+
StructOptions,
|
|
4
|
+
StructValue,
|
|
5
|
+
} from "../../basic/index.js";
|
|
6
|
+
import { StructFieldValue } from "../../basic/index.js";
|
|
7
|
+
import type { KeysOfType } from "../../utils.js";
|
|
8
|
+
|
|
9
|
+
import type { BufferFieldSubType } from "./base.js";
|
|
10
|
+
import { BufferLikeFieldDefinition, BufferLikeFieldValue } from "./base.js";
|
|
4
11
|
|
|
5
12
|
export type LengthField<TFields> = KeysOfType<TFields, number | string>;
|
|
6
13
|
|
|
7
14
|
export interface VariableLengthBufferLikeFieldOptions<
|
|
8
15
|
TFields = object,
|
|
9
|
-
TLengthField extends LengthField<TFields> = any
|
|
10
|
-
|
|
16
|
+
TLengthField extends LengthField<TFields> = any
|
|
17
|
+
> {
|
|
11
18
|
/**
|
|
12
19
|
* The name of the field that contains the length of the buffer.
|
|
13
20
|
*
|
|
@@ -27,20 +34,20 @@ export interface VariableLengthBufferLikeFieldOptions<
|
|
|
27
34
|
export class VariableLengthBufferLikeFieldDefinition<
|
|
28
35
|
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
29
36
|
TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions,
|
|
30
|
-
TTypeScriptType = TType["TTypeScriptType"]
|
|
31
|
-
|
|
37
|
+
TTypeScriptType = TType["TTypeScriptType"]
|
|
38
|
+
> extends BufferLikeFieldDefinition<
|
|
32
39
|
TType,
|
|
33
40
|
TOptions,
|
|
34
|
-
TOptions[
|
|
41
|
+
TOptions["lengthField"],
|
|
35
42
|
TTypeScriptType
|
|
36
|
-
|
|
43
|
+
> {
|
|
37
44
|
public getSize(): number {
|
|
38
45
|
return 0;
|
|
39
46
|
}
|
|
40
47
|
|
|
41
48
|
protected override getDeserializeSize(struct: StructValue) {
|
|
42
49
|
let value = struct.value[this.options.lengthField] as number | string;
|
|
43
|
-
if (typeof value ===
|
|
50
|
+
if (typeof value === "string") {
|
|
44
51
|
value = Number.parseInt(value, this.options.lengthFieldRadix ?? 10);
|
|
45
52
|
}
|
|
46
53
|
return value;
|
|
@@ -57,14 +64,14 @@ export class VariableLengthBufferLikeFieldDefinition<
|
|
|
57
64
|
options,
|
|
58
65
|
struct,
|
|
59
66
|
value,
|
|
60
|
-
array
|
|
67
|
+
array
|
|
61
68
|
);
|
|
62
69
|
}
|
|
63
70
|
}
|
|
64
71
|
|
|
65
72
|
export class VariableLengthBufferLikeStructFieldValue<
|
|
66
|
-
TDefinition extends VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition
|
|
67
|
-
|
|
73
|
+
TDefinition extends VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition
|
|
74
|
+
> extends BufferLikeFieldValue<TDefinition> {
|
|
68
75
|
protected length: number | undefined;
|
|
69
76
|
|
|
70
77
|
protected lengthFieldValue: VariableLengthBufferLikeFieldLengthValue;
|
|
@@ -73,8 +80,8 @@ export class VariableLengthBufferLikeStructFieldValue<
|
|
|
73
80
|
definition: TDefinition,
|
|
74
81
|
options: Readonly<StructOptions>,
|
|
75
82
|
struct: StructValue,
|
|
76
|
-
value: TDefinition[
|
|
77
|
-
array?: Uint8Array
|
|
83
|
+
value: TDefinition["TValue"],
|
|
84
|
+
array?: Uint8Array
|
|
78
85
|
) {
|
|
79
86
|
super(definition, options, struct, value, array);
|
|
80
87
|
|
|
@@ -88,7 +95,7 @@ export class VariableLengthBufferLikeStructFieldValue<
|
|
|
88
95
|
const originalValue = struct.get(lengthField);
|
|
89
96
|
this.lengthFieldValue = new VariableLengthBufferLikeFieldLengthValue(
|
|
90
97
|
originalValue,
|
|
91
|
-
this
|
|
98
|
+
this
|
|
92
99
|
);
|
|
93
100
|
struct.set(lengthField, this.lengthFieldValue);
|
|
94
101
|
}
|
|
@@ -113,20 +120,25 @@ export class VariableLengthBufferLikeStructFieldValue<
|
|
|
113
120
|
}
|
|
114
121
|
|
|
115
122
|
// Not using `VariableLengthBufferLikeStructFieldValue` directly makes writing tests much easier...
|
|
116
|
-
type VariableLengthBufferLikeFieldValueLike =
|
|
117
|
-
|
|
123
|
+
type VariableLengthBufferLikeFieldValueLike = StructFieldValue<
|
|
124
|
+
StructFieldDefinition<VariableLengthBufferLikeFieldOptions, any, any>
|
|
125
|
+
>;
|
|
118
126
|
|
|
119
|
-
export class VariableLengthBufferLikeFieldLengthValue
|
|
120
|
-
extends StructFieldValue {
|
|
127
|
+
export class VariableLengthBufferLikeFieldLengthValue extends StructFieldValue {
|
|
121
128
|
protected originalField: StructFieldValue;
|
|
122
129
|
|
|
123
130
|
protected bufferField: VariableLengthBufferLikeFieldValueLike;
|
|
124
131
|
|
|
125
132
|
public constructor(
|
|
126
133
|
originalField: StructFieldValue,
|
|
127
|
-
arrayBufferField: VariableLengthBufferLikeFieldValueLike
|
|
134
|
+
arrayBufferField: VariableLengthBufferLikeFieldValueLike
|
|
128
135
|
) {
|
|
129
|
-
super(
|
|
136
|
+
super(
|
|
137
|
+
originalField.definition,
|
|
138
|
+
originalField.options,
|
|
139
|
+
originalField.struct,
|
|
140
|
+
0
|
|
141
|
+
);
|
|
130
142
|
this.originalField = originalField;
|
|
131
143
|
this.bufferField = arrayBufferField;
|
|
132
144
|
}
|
|
@@ -139,8 +151,10 @@ export class VariableLengthBufferLikeFieldLengthValue
|
|
|
139
151
|
let value: string | number = this.bufferField.getSize();
|
|
140
152
|
|
|
141
153
|
const originalValue = this.originalField.get();
|
|
142
|
-
if (typeof originalValue ===
|
|
143
|
-
value = value.toString(
|
|
154
|
+
if (typeof originalValue === "string") {
|
|
155
|
+
value = value.toString(
|
|
156
|
+
this.bufferField.definition.options.lengthFieldRadix ?? 10
|
|
157
|
+
);
|
|
144
158
|
}
|
|
145
159
|
|
|
146
160
|
return value;
|