@yume-chan/struct 0.0.13 → 0.0.16
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 +33 -0
- package/CHANGELOG.md +21 -1
- package/LICENSE +21 -21
- package/README.md +767 -769
- package/esm/basic/definition.d.ts +3 -3
- package/esm/basic/definition.d.ts.map +1 -1
- package/esm/basic/definition.js +0 -1
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.d.ts +1 -0
- package/esm/basic/field-value.d.ts.map +1 -1
- package/esm/basic/field-value.js +4 -0
- package/esm/basic/field-value.js.map +1 -1
- package/esm/basic/stream.d.ts +2 -1
- package/esm/basic/stream.d.ts.map +1 -1
- package/esm/basic/struct-value.d.ts +7 -5
- package/esm/basic/struct-value.d.ts.map +1 -1
- package/esm/basic/struct-value.js +30 -14
- package/esm/basic/struct-value.js.map +1 -1
- package/esm/struct.d.ts +11 -11
- package/esm/struct.d.ts.map +1 -1
- package/esm/struct.js +55 -41
- package/esm/struct.js.map +1 -1
- package/esm/sync-promise.d.ts +13 -0
- package/esm/sync-promise.d.ts.map +1 -0
- package/esm/sync-promise.js +71 -0
- package/esm/sync-promise.js.map +1 -0
- package/esm/types/bigint.d.ts.map +1 -1
- package/esm/types/bigint.js +7 -5
- package/esm/types/bigint.js.map +1 -1
- package/esm/types/buffer/base.d.ts +4 -3
- package/esm/types/buffer/base.d.ts.map +1 -1
- package/esm/types/buffer/base.js +9 -7
- package/esm/types/buffer/base.js.map +1 -1
- package/esm/types/buffer/fixed-length.d.ts +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 +2 -2
- 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 +6 -5
- package/esm/types/number.d.ts.map +1 -1
- package/esm/types/number.js +25 -14
- package/esm/types/number.js.map +1 -1
- package/package.json +11 -11
- package/src/basic/definition.ts +68 -70
- package/src/basic/field-value.ts +72 -67
- package/src/basic/index.ts +5 -5
- package/src/basic/options.ts +19 -19
- package/src/basic/stream.ts +21 -19
- package/src/basic/struct-value.ts +61 -39
- package/src/index.ts +17 -17
- package/src/struct.ts +632 -609
- package/src/sync-promise.ts +114 -0
- package/src/types/bigint.ts +103 -102
- package/src/types/buffer/base.ts +179 -176
- package/src/types/buffer/fixed-length.ts +20 -17
- package/src/types/buffer/index.ts +3 -3
- package/src/types/buffer/variable-length.ts +158 -156
- package/src/types/index.ts +3 -3
- package/src/types/number.ts +127 -115
- package/src/utils.ts +70 -70
- package/tsconfig.build.json +3 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.test.json +8 -0
- package/esm/syncbird.d.ts +0 -65
- package/esm/syncbird.d.ts.map +0 -1
- package/esm/syncbird.js +0 -38
- package/esm/syncbird.js.map +0 -1
- package/src/syncbird.ts +0 -131
package/src/types/buffer/base.ts
CHANGED
|
@@ -1,176 +1,179 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
> extends StructFieldDefinition<
|
|
89
|
-
TOptions,
|
|
90
|
-
|
|
91
|
-
TOmitInitKey
|
|
92
|
-
>{
|
|
93
|
-
public readonly type: TType;
|
|
94
|
-
|
|
95
|
-
public constructor(type: TType, options: TOptions) {
|
|
96
|
-
super(options);
|
|
97
|
-
this.type = type;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
protected getDeserializeSize(struct: StructValue): number {
|
|
101
|
-
return this.getSize();
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* When implemented in derived classes, creates a `StructFieldValue` for the current field definition.
|
|
106
|
-
*/
|
|
107
|
-
public create(
|
|
108
|
-
options: Readonly<StructOptions>,
|
|
109
|
-
struct: StructValue,
|
|
110
|
-
value: TType['TTypeScriptType'],
|
|
111
|
-
array?: Uint8Array,
|
|
112
|
-
): BufferLikeFieldValue<this> {
|
|
113
|
-
return new BufferLikeFieldValue(this, options, struct, value, array);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public override deserialize(
|
|
117
|
-
options: Readonly<StructOptions>,
|
|
118
|
-
stream: StructDeserializeStream,
|
|
119
|
-
struct: StructValue,
|
|
120
|
-
): BufferLikeFieldValue<this>;
|
|
121
|
-
public override deserialize(
|
|
122
|
-
options: Readonly<StructOptions>,
|
|
123
|
-
stream: StructAsyncDeserializeStream,
|
|
124
|
-
struct: StructValue,
|
|
125
|
-
): Promise<BufferLikeFieldValue<this>>;
|
|
126
|
-
public override deserialize(
|
|
127
|
-
options: Readonly<StructOptions>,
|
|
128
|
-
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
|
129
|
-
struct: StructValue,
|
|
130
|
-
): ValueOrPromise<BufferLikeFieldValue<this>> {
|
|
131
|
-
return
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
1
|
+
import { StructFieldDefinition, StructFieldValue, StructValue, type StructAsyncDeserializeStream, type StructDeserializeStream, type StructOptions } from '../../basic/index.js';
|
|
2
|
+
import { SyncPromise } from "../../sync-promise.js";
|
|
3
|
+
import { decodeUtf8, encodeUtf8, type ValueOrPromise } from "../../utils.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base class for all types that
|
|
7
|
+
* can be converted from an `Uint8Array` when deserialized,
|
|
8
|
+
* and need to be converted to an `Uint8Array` when serializing
|
|
9
|
+
*
|
|
10
|
+
* @template TValue The actual TypeScript type of this type
|
|
11
|
+
* @template TTypeScriptType Optional another type (should be compatible with `TType`)
|
|
12
|
+
* specified by user when creating field definitions.
|
|
13
|
+
*/
|
|
14
|
+
export abstract class BufferFieldSubType<TValue = unknown, TTypeScriptType = TValue> {
|
|
15
|
+
public readonly TTypeScriptType!: TTypeScriptType;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* When implemented in derived classes, converts the type-specific `value` to an `Uint8Array`
|
|
19
|
+
*
|
|
20
|
+
* This function should be "pure", i.e.,
|
|
21
|
+
* same `value` should always be converted to `Uint8Array`s that have same content.
|
|
22
|
+
*/
|
|
23
|
+
public abstract toBuffer(value: TValue): Uint8Array;
|
|
24
|
+
|
|
25
|
+
/** When implemented in derived classes, converts the `Uint8Array` to a type-specific value */
|
|
26
|
+
public abstract toValue(array: Uint8Array): TValue;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* When implemented in derived classes, gets the size in byte of the type-specific `value`.
|
|
30
|
+
*
|
|
31
|
+
* If the size can't be calculated without first converting the `value` back to an `Uint8Array`,
|
|
32
|
+
* implementer can returns `-1`, so the caller will get its size by first converting it to
|
|
33
|
+
* an `Uint8Array` (and cache the result).
|
|
34
|
+
*/
|
|
35
|
+
public abstract getSize(value: TValue): number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** An `BufferFieldSubType` that's actually an `Uint8Array` */
|
|
39
|
+
export class Uint8ArrayBufferFieldSubType<TTypeScriptType = Uint8Array>
|
|
40
|
+
extends BufferFieldSubType<Uint8Array, TTypeScriptType> {
|
|
41
|
+
public static readonly Instance = new Uint8ArrayBufferFieldSubType();
|
|
42
|
+
|
|
43
|
+
protected constructor() {
|
|
44
|
+
super();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public toBuffer(value: Uint8Array): Uint8Array {
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public toValue(buffer: Uint8Array): Uint8Array {
|
|
52
|
+
return buffer;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public getSize(value: Uint8Array): number {
|
|
56
|
+
return value.byteLength;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** An `BufferFieldSubType` that converts between `Uint8Array` and `string` */
|
|
61
|
+
export class StringBufferFieldSubType<TTypeScriptType = string>
|
|
62
|
+
extends BufferFieldSubType<string, TTypeScriptType> {
|
|
63
|
+
public static readonly Instance = new StringBufferFieldSubType();
|
|
64
|
+
|
|
65
|
+
public toBuffer(value: string): Uint8Array {
|
|
66
|
+
return encodeUtf8(value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public toValue(array: Uint8Array): string {
|
|
70
|
+
return decodeUtf8(array);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public getSize(): number {
|
|
74
|
+
// Return `-1`, so `BufferLikeFieldDefinition` will
|
|
75
|
+
// convert this `value` into an `Uint8Array` (and cache the result),
|
|
76
|
+
// Then get the size from that `Uint8Array`
|
|
77
|
+
return -1;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const EMPTY_UINT8_ARRAY = new Uint8Array(0);
|
|
82
|
+
|
|
83
|
+
export abstract class BufferLikeFieldDefinition<
|
|
84
|
+
TType extends BufferFieldSubType<any, any> = BufferFieldSubType<unknown, unknown>,
|
|
85
|
+
TOptions = void,
|
|
86
|
+
TOmitInitKey extends PropertyKey = never,
|
|
87
|
+
TTypeScriptType = TType["TTypeScriptType"],
|
|
88
|
+
> extends StructFieldDefinition<
|
|
89
|
+
TOptions,
|
|
90
|
+
TTypeScriptType,
|
|
91
|
+
TOmitInitKey
|
|
92
|
+
>{
|
|
93
|
+
public readonly type: TType;
|
|
94
|
+
|
|
95
|
+
public constructor(type: TType, options: TOptions) {
|
|
96
|
+
super(options);
|
|
97
|
+
this.type = type;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
protected getDeserializeSize(struct: StructValue): number {
|
|
101
|
+
return this.getSize();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* When implemented in derived classes, creates a `StructFieldValue` for the current field definition.
|
|
106
|
+
*/
|
|
107
|
+
public create(
|
|
108
|
+
options: Readonly<StructOptions>,
|
|
109
|
+
struct: StructValue,
|
|
110
|
+
value: TType['TTypeScriptType'],
|
|
111
|
+
array?: Uint8Array,
|
|
112
|
+
): BufferLikeFieldValue<this> {
|
|
113
|
+
return new BufferLikeFieldValue(this, options, struct, value, array);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public override deserialize(
|
|
117
|
+
options: Readonly<StructOptions>,
|
|
118
|
+
stream: StructDeserializeStream,
|
|
119
|
+
struct: StructValue,
|
|
120
|
+
): BufferLikeFieldValue<this>;
|
|
121
|
+
public override deserialize(
|
|
122
|
+
options: Readonly<StructOptions>,
|
|
123
|
+
stream: StructAsyncDeserializeStream,
|
|
124
|
+
struct: StructValue,
|
|
125
|
+
): Promise<BufferLikeFieldValue<this>>;
|
|
126
|
+
public override deserialize(
|
|
127
|
+
options: Readonly<StructOptions>,
|
|
128
|
+
stream: StructDeserializeStream | StructAsyncDeserializeStream,
|
|
129
|
+
struct: StructValue,
|
|
130
|
+
): ValueOrPromise<BufferLikeFieldValue<this>> {
|
|
131
|
+
return SyncPromise
|
|
132
|
+
.try(() => {
|
|
133
|
+
const size = this.getDeserializeSize(struct);
|
|
134
|
+
if (size === 0) {
|
|
135
|
+
return EMPTY_UINT8_ARRAY;
|
|
136
|
+
} else {
|
|
137
|
+
return stream.read(size);
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
.then(array => {
|
|
141
|
+
const value = this.type.toValue(array);
|
|
142
|
+
return this.create(options, struct, value, array);
|
|
143
|
+
})
|
|
144
|
+
.valueOrPromise();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export class BufferLikeFieldValue<
|
|
149
|
+
TDefinition extends BufferLikeFieldDefinition<BufferFieldSubType<unknown, unknown>, any, any>,
|
|
150
|
+
> extends StructFieldValue<TDefinition> {
|
|
151
|
+
protected array: Uint8Array | undefined;
|
|
152
|
+
|
|
153
|
+
public constructor(
|
|
154
|
+
definition: TDefinition,
|
|
155
|
+
options: Readonly<StructOptions>,
|
|
156
|
+
struct: StructValue,
|
|
157
|
+
value: TDefinition['TValue'],
|
|
158
|
+
array?: Uint8Array,
|
|
159
|
+
) {
|
|
160
|
+
super(definition, options, struct, value);
|
|
161
|
+
this.array = array;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
public override set(value: TDefinition['TValue']): void {
|
|
165
|
+
super.set(value);
|
|
166
|
+
// When value changes, clear the cached `array`
|
|
167
|
+
// It will be lazily calculated in `serialize()`
|
|
168
|
+
this.array = undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
public serialize(dataView: DataView, offset: number): void {
|
|
172
|
+
if (!this.array) {
|
|
173
|
+
this.array = this.definition.type.toBuffer(this.value);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
new Uint8Array(dataView.buffer, dataView.byteOffset, dataView.byteLength)
|
|
177
|
+
.set(this.array, offset);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
import { BufferLikeFieldDefinition, type BufferFieldSubType } from "./base.js";
|
|
2
|
-
|
|
3
|
-
export interface FixedLengthBufferLikeFieldOptions {
|
|
4
|
-
length: number;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export class FixedLengthBufferLikeFieldDefinition<
|
|
8
|
-
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
9
|
-
TOptions extends FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { BufferLikeFieldDefinition, type BufferFieldSubType } from "./base.js";
|
|
2
|
+
|
|
3
|
+
export interface FixedLengthBufferLikeFieldOptions {
|
|
4
|
+
length: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class FixedLengthBufferLikeFieldDefinition<
|
|
8
|
+
TType extends BufferFieldSubType = BufferFieldSubType,
|
|
9
|
+
TOptions extends FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
|
|
10
|
+
TTypeScriptType = TType["TTypeScriptType"],
|
|
11
|
+
> extends BufferLikeFieldDefinition<
|
|
12
|
+
TType,
|
|
13
|
+
TOptions,
|
|
14
|
+
never,
|
|
15
|
+
TTypeScriptType
|
|
16
|
+
> {
|
|
17
|
+
public getSize(): number {
|
|
18
|
+
return this.options.length;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./base.js";
|
|
2
|
-
export * from "./fixed-length.js";
|
|
3
|
-
export * from "./variable-length.js";
|
|
1
|
+
export * from "./base.js";
|
|
2
|
+
export * from "./fixed-length.js";
|
|
3
|
+
export * from "./variable-length.js";
|