@yume-chan/struct 0.0.18 → 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 +6 -0
- package/CHANGELOG.md +6 -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/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/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.d.ts.map +1 -1
- 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 -56
- 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 -210
- package/esm/struct.js.map +1 -1
- package/esm/sync-promise.d.ts +12 -12
- package/esm/sync-promise.js +70 -70
- 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 -100
- 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 -6
- 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 -26
- package/esm/types/number.d.ts.map +1 -1
- package/esm/types/number.js +113 -113
- package/esm/types/number.js.map +1 -1
- package/esm/utils.d.ts +47 -47
- package/esm/utils.js +15 -15
- package/package.json +7 -6
- package/src/basic/definition.ts +6 -6
- package/src/basic/field-value.ts +3 -3
- package/src/basic/index.ts +5 -5
- package/src/basic/stream.ts +1 -1
- package/src/basic/struct-value.ts +1 -1
- package/src/index.ts +5 -5
- package/src/struct.ts +14 -15
- package/src/types/bigint.ts +7 -8
- package/src/types/buffer/base.ts +8 -8
- package/src/types/buffer/fixed-length.ts +2 -1
- package/src/types/buffer/variable-length.ts +8 -11
- package/src/types/number.ts +7 -8
- package/tsconfig.build.tsbuildinfo +1 -1
package/esm/struct.js
CHANGED
|
@@ -1,211 +1,211 @@
|
|
|
1
|
-
import { STRUCT_VALUE_SYMBOL, StructDefaultOptions, StructValue, } from "./basic/index.js";
|
|
2
|
-
import { SyncPromise } from "./sync-promise.js";
|
|
3
|
-
import { BigIntFieldDefinition, BigIntFieldType, FixedLengthBufferLikeFieldDefinition, NumberFieldDefinition, NumberFieldType, StringBufferFieldSubType, Uint8ArrayBufferFieldSubType, VariableLengthBufferLikeFieldDefinition, } from "./types/index.js";
|
|
4
|
-
export class Struct {
|
|
5
|
-
TFields;
|
|
6
|
-
TOmitInitKey;
|
|
7
|
-
TExtra;
|
|
8
|
-
TInit;
|
|
9
|
-
TDeserializeResult;
|
|
10
|
-
options;
|
|
11
|
-
_size = 0;
|
|
12
|
-
/**
|
|
13
|
-
* Gets the static size (exclude fields that can change size at runtime)
|
|
14
|
-
*/
|
|
15
|
-
get size() {
|
|
16
|
-
return this._size;
|
|
17
|
-
}
|
|
18
|
-
_fields = [];
|
|
19
|
-
_extra = {};
|
|
20
|
-
_postDeserialized;
|
|
21
|
-
constructor(options) {
|
|
22
|
-
this.options = { ...StructDefaultOptions, ...options };
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Appends a `StructFieldDefinition` to the `Struct
|
|
26
|
-
*/
|
|
27
|
-
field(name, definition) {
|
|
28
|
-
for (const field of this._fields) {
|
|
29
|
-
if (field[0] === name) {
|
|
30
|
-
throw new Error(`This struct already have a field with name '${String(name)}'`);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
this._fields.push([name, definition]);
|
|
34
|
-
const size = definition.getSize();
|
|
35
|
-
this._size += size;
|
|
36
|
-
// Force cast `this` to another type
|
|
37
|
-
return this;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Merges (flats) another `Struct`'s fields and extra fields into this one.
|
|
41
|
-
*/
|
|
42
|
-
fields(other) {
|
|
43
|
-
for (const field of other._fields) {
|
|
44
|
-
this._fields.push(field);
|
|
45
|
-
}
|
|
46
|
-
this._size += other._size;
|
|
47
|
-
Object.defineProperties(this._extra, Object.getOwnPropertyDescriptors(other._extra));
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
number(name, type, typeScriptType) {
|
|
51
|
-
return this.field(name, new NumberFieldDefinition(type, typeScriptType));
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Appends an `int8` field to the `Struct`
|
|
55
|
-
*/
|
|
56
|
-
int8(name, typeScriptType) {
|
|
57
|
-
return this.number(name, NumberFieldType.Int8, typeScriptType);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Appends an `uint8` field to the `Struct`
|
|
61
|
-
*/
|
|
62
|
-
uint8(name, typeScriptType) {
|
|
63
|
-
return this.number(name, NumberFieldType.Uint8, typeScriptType);
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Appends an `int16` field to the `Struct`
|
|
67
|
-
*/
|
|
68
|
-
int16(name, typeScriptType) {
|
|
69
|
-
return this.number(name, NumberFieldType.Int16, typeScriptType);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Appends an `uint16` field to the `Struct`
|
|
73
|
-
*/
|
|
74
|
-
uint16(name, typeScriptType) {
|
|
75
|
-
return this.number(name, NumberFieldType.Uint16, typeScriptType);
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Appends an `int32` field to the `Struct`
|
|
79
|
-
*/
|
|
80
|
-
int32(name, typeScriptType) {
|
|
81
|
-
return this.number(name, NumberFieldType.Int32, typeScriptType);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Appends an `uint32` field to the `Struct`
|
|
85
|
-
*/
|
|
86
|
-
uint32(name, typeScriptType) {
|
|
87
|
-
return this.number(name, NumberFieldType.Uint32, typeScriptType);
|
|
88
|
-
}
|
|
89
|
-
bigint(name, type, typeScriptType) {
|
|
90
|
-
return this.field(name, new BigIntFieldDefinition(type, typeScriptType));
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Appends an `int64` field to the `Struct`
|
|
94
|
-
*
|
|
95
|
-
* Requires native `BigInt` support
|
|
96
|
-
*/
|
|
97
|
-
int64(name, typeScriptType) {
|
|
98
|
-
return this.bigint(name, BigIntFieldType.Int64, typeScriptType);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Appends an `uint64` field to the `Struct`
|
|
102
|
-
*
|
|
103
|
-
* Requires native `BigInt` support
|
|
104
|
-
*/
|
|
105
|
-
uint64(name, typeScriptType) {
|
|
106
|
-
return this.bigint(name, BigIntFieldType.Uint64, typeScriptType);
|
|
107
|
-
}
|
|
108
|
-
arrayBufferLike = (name, type, options) => {
|
|
109
|
-
if ("length" in options) {
|
|
110
|
-
return this.field(name, new FixedLengthBufferLikeFieldDefinition(type, options));
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
return this.field(name, new VariableLengthBufferLikeFieldDefinition(type, options));
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
uint8Array = (name, options, typeScriptType) => {
|
|
117
|
-
return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options, typeScriptType);
|
|
118
|
-
};
|
|
119
|
-
string = (name, options, typeScriptType) => {
|
|
120
|
-
return this.arrayBufferLike(name, StringBufferFieldSubType.Instance, options, typeScriptType);
|
|
121
|
-
};
|
|
122
|
-
/**
|
|
123
|
-
* Adds some extra properties into every `Struct` value.
|
|
124
|
-
*
|
|
125
|
-
* Extra properties will not affect serialize or deserialize process.
|
|
126
|
-
*
|
|
127
|
-
* Multiple calls to `extra` will merge all properties together.
|
|
128
|
-
*
|
|
129
|
-
* @param value
|
|
130
|
-
* An object containing properties to be added to the result value. Accessors and methods are also allowed.
|
|
131
|
-
*/
|
|
132
|
-
extra(value) {
|
|
133
|
-
Object.defineProperties(this._extra, Object.getOwnPropertyDescriptors(value));
|
|
134
|
-
return this;
|
|
135
|
-
}
|
|
136
|
-
postDeserialize(callback) {
|
|
137
|
-
this._postDeserialized = callback;
|
|
138
|
-
return this;
|
|
139
|
-
}
|
|
140
|
-
deserialize(stream) {
|
|
141
|
-
const structValue = new StructValue(this._extra);
|
|
142
|
-
let promise = SyncPromise.resolve();
|
|
143
|
-
for (const [name, definition] of this._fields) {
|
|
144
|
-
promise = promise
|
|
145
|
-
.then(() => definition.deserialize(this.options, stream, structValue))
|
|
146
|
-
.then((fieldValue) => {
|
|
147
|
-
structValue.set(name, fieldValue);
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
return promise
|
|
151
|
-
.then(() => {
|
|
152
|
-
const object = structValue.value;
|
|
153
|
-
// Run `postDeserialized`
|
|
154
|
-
if (this._postDeserialized) {
|
|
155
|
-
const override = this._postDeserialized.call(object, object);
|
|
156
|
-
// If it returns a new value, use that as result
|
|
157
|
-
// Otherwise it only inspects/mutates the object in place.
|
|
158
|
-
if (override !== undefined) {
|
|
159
|
-
return override;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return object;
|
|
163
|
-
})
|
|
164
|
-
.valueOrPromise();
|
|
165
|
-
}
|
|
166
|
-
serialize(init, output) {
|
|
167
|
-
let structValue;
|
|
168
|
-
if (STRUCT_VALUE_SYMBOL in init) {
|
|
169
|
-
structValue = init[STRUCT_VALUE_SYMBOL];
|
|
170
|
-
for (const [key, value] of Object.entries(init)) {
|
|
171
|
-
const fieldValue = structValue.get(key);
|
|
172
|
-
if (fieldValue) {
|
|
173
|
-
fieldValue.set(value);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
structValue = new StructValue({});
|
|
179
|
-
for (const [name, definition] of this._fields) {
|
|
180
|
-
const fieldValue = definition.create(this.options, structValue, init[name]);
|
|
181
|
-
structValue.set(name, fieldValue);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
let structSize = 0;
|
|
185
|
-
const fieldsInfo = [];
|
|
186
|
-
for (const [name] of this._fields) {
|
|
187
|
-
const fieldValue = structValue.get(name);
|
|
188
|
-
const size = fieldValue.getSize();
|
|
189
|
-
fieldsInfo.push({ fieldValue, size });
|
|
190
|
-
structSize += size;
|
|
191
|
-
}
|
|
192
|
-
let outputType = "number";
|
|
193
|
-
if (!output) {
|
|
194
|
-
output = new Uint8Array(structSize);
|
|
195
|
-
outputType = "Uint8Array";
|
|
196
|
-
}
|
|
197
|
-
const dataView = new DataView(output.buffer, output.byteOffset, output.byteLength);
|
|
198
|
-
let offset = 0;
|
|
199
|
-
for (const { fieldValue, size } of fieldsInfo) {
|
|
200
|
-
fieldValue.serialize(dataView, offset);
|
|
201
|
-
offset += size;
|
|
202
|
-
}
|
|
203
|
-
if (outputType === "number") {
|
|
204
|
-
return structSize;
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
return output;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
1
|
+
import { STRUCT_VALUE_SYMBOL, StructDefaultOptions, StructValue, } from "./basic/index.js";
|
|
2
|
+
import { SyncPromise } from "./sync-promise.js";
|
|
3
|
+
import { BigIntFieldDefinition, BigIntFieldType, FixedLengthBufferLikeFieldDefinition, NumberFieldDefinition, NumberFieldType, StringBufferFieldSubType, Uint8ArrayBufferFieldSubType, VariableLengthBufferLikeFieldDefinition, } from "./types/index.js";
|
|
4
|
+
export class Struct {
|
|
5
|
+
TFields;
|
|
6
|
+
TOmitInitKey;
|
|
7
|
+
TExtra;
|
|
8
|
+
TInit;
|
|
9
|
+
TDeserializeResult;
|
|
10
|
+
options;
|
|
11
|
+
_size = 0;
|
|
12
|
+
/**
|
|
13
|
+
* Gets the static size (exclude fields that can change size at runtime)
|
|
14
|
+
*/
|
|
15
|
+
get size() {
|
|
16
|
+
return this._size;
|
|
17
|
+
}
|
|
18
|
+
_fields = [];
|
|
19
|
+
_extra = {};
|
|
20
|
+
_postDeserialized;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.options = { ...StructDefaultOptions, ...options };
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Appends a `StructFieldDefinition` to the `Struct
|
|
26
|
+
*/
|
|
27
|
+
field(name, definition) {
|
|
28
|
+
for (const field of this._fields) {
|
|
29
|
+
if (field[0] === name) {
|
|
30
|
+
throw new Error(`This struct already have a field with name '${String(name)}'`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
this._fields.push([name, definition]);
|
|
34
|
+
const size = definition.getSize();
|
|
35
|
+
this._size += size;
|
|
36
|
+
// Force cast `this` to another type
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Merges (flats) another `Struct`'s fields and extra fields into this one.
|
|
41
|
+
*/
|
|
42
|
+
fields(other) {
|
|
43
|
+
for (const field of other._fields) {
|
|
44
|
+
this._fields.push(field);
|
|
45
|
+
}
|
|
46
|
+
this._size += other._size;
|
|
47
|
+
Object.defineProperties(this._extra, Object.getOwnPropertyDescriptors(other._extra));
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
number(name, type, typeScriptType) {
|
|
51
|
+
return this.field(name, new NumberFieldDefinition(type, typeScriptType));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Appends an `int8` field to the `Struct`
|
|
55
|
+
*/
|
|
56
|
+
int8(name, typeScriptType) {
|
|
57
|
+
return this.number(name, NumberFieldType.Int8, typeScriptType);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Appends an `uint8` field to the `Struct`
|
|
61
|
+
*/
|
|
62
|
+
uint8(name, typeScriptType) {
|
|
63
|
+
return this.number(name, NumberFieldType.Uint8, typeScriptType);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Appends an `int16` field to the `Struct`
|
|
67
|
+
*/
|
|
68
|
+
int16(name, typeScriptType) {
|
|
69
|
+
return this.number(name, NumberFieldType.Int16, typeScriptType);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Appends an `uint16` field to the `Struct`
|
|
73
|
+
*/
|
|
74
|
+
uint16(name, typeScriptType) {
|
|
75
|
+
return this.number(name, NumberFieldType.Uint16, typeScriptType);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Appends an `int32` field to the `Struct`
|
|
79
|
+
*/
|
|
80
|
+
int32(name, typeScriptType) {
|
|
81
|
+
return this.number(name, NumberFieldType.Int32, typeScriptType);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Appends an `uint32` field to the `Struct`
|
|
85
|
+
*/
|
|
86
|
+
uint32(name, typeScriptType) {
|
|
87
|
+
return this.number(name, NumberFieldType.Uint32, typeScriptType);
|
|
88
|
+
}
|
|
89
|
+
bigint(name, type, typeScriptType) {
|
|
90
|
+
return this.field(name, new BigIntFieldDefinition(type, typeScriptType));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Appends an `int64` field to the `Struct`
|
|
94
|
+
*
|
|
95
|
+
* Requires native `BigInt` support
|
|
96
|
+
*/
|
|
97
|
+
int64(name, typeScriptType) {
|
|
98
|
+
return this.bigint(name, BigIntFieldType.Int64, typeScriptType);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Appends an `uint64` field to the `Struct`
|
|
102
|
+
*
|
|
103
|
+
* Requires native `BigInt` support
|
|
104
|
+
*/
|
|
105
|
+
uint64(name, typeScriptType) {
|
|
106
|
+
return this.bigint(name, BigIntFieldType.Uint64, typeScriptType);
|
|
107
|
+
}
|
|
108
|
+
arrayBufferLike = (name, type, options) => {
|
|
109
|
+
if ("length" in options) {
|
|
110
|
+
return this.field(name, new FixedLengthBufferLikeFieldDefinition(type, options));
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
return this.field(name, new VariableLengthBufferLikeFieldDefinition(type, options));
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
uint8Array = (name, options, typeScriptType) => {
|
|
117
|
+
return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options, typeScriptType);
|
|
118
|
+
};
|
|
119
|
+
string = (name, options, typeScriptType) => {
|
|
120
|
+
return this.arrayBufferLike(name, StringBufferFieldSubType.Instance, options, typeScriptType);
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Adds some extra properties into every `Struct` value.
|
|
124
|
+
*
|
|
125
|
+
* Extra properties will not affect serialize or deserialize process.
|
|
126
|
+
*
|
|
127
|
+
* Multiple calls to `extra` will merge all properties together.
|
|
128
|
+
*
|
|
129
|
+
* @param value
|
|
130
|
+
* An object containing properties to be added to the result value. Accessors and methods are also allowed.
|
|
131
|
+
*/
|
|
132
|
+
extra(value) {
|
|
133
|
+
Object.defineProperties(this._extra, Object.getOwnPropertyDescriptors(value));
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
postDeserialize(callback) {
|
|
137
|
+
this._postDeserialized = callback;
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
deserialize(stream) {
|
|
141
|
+
const structValue = new StructValue(this._extra);
|
|
142
|
+
let promise = SyncPromise.resolve();
|
|
143
|
+
for (const [name, definition] of this._fields) {
|
|
144
|
+
promise = promise
|
|
145
|
+
.then(() => definition.deserialize(this.options, stream, structValue))
|
|
146
|
+
.then((fieldValue) => {
|
|
147
|
+
structValue.set(name, fieldValue);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return promise
|
|
151
|
+
.then(() => {
|
|
152
|
+
const object = structValue.value;
|
|
153
|
+
// Run `postDeserialized`
|
|
154
|
+
if (this._postDeserialized) {
|
|
155
|
+
const override = this._postDeserialized.call(object, object);
|
|
156
|
+
// If it returns a new value, use that as result
|
|
157
|
+
// Otherwise it only inspects/mutates the object in place.
|
|
158
|
+
if (override !== undefined) {
|
|
159
|
+
return override;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return object;
|
|
163
|
+
})
|
|
164
|
+
.valueOrPromise();
|
|
165
|
+
}
|
|
166
|
+
serialize(init, output) {
|
|
167
|
+
let structValue;
|
|
168
|
+
if (STRUCT_VALUE_SYMBOL in init) {
|
|
169
|
+
structValue = init[STRUCT_VALUE_SYMBOL];
|
|
170
|
+
for (const [key, value] of Object.entries(init)) {
|
|
171
|
+
const fieldValue = structValue.get(key);
|
|
172
|
+
if (fieldValue) {
|
|
173
|
+
fieldValue.set(value);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
structValue = new StructValue({});
|
|
179
|
+
for (const [name, definition] of this._fields) {
|
|
180
|
+
const fieldValue = definition.create(this.options, structValue, init[name]);
|
|
181
|
+
structValue.set(name, fieldValue);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
let structSize = 0;
|
|
185
|
+
const fieldsInfo = [];
|
|
186
|
+
for (const [name] of this._fields) {
|
|
187
|
+
const fieldValue = structValue.get(name);
|
|
188
|
+
const size = fieldValue.getSize();
|
|
189
|
+
fieldsInfo.push({ fieldValue, size });
|
|
190
|
+
structSize += size;
|
|
191
|
+
}
|
|
192
|
+
let outputType = "number";
|
|
193
|
+
if (!output) {
|
|
194
|
+
output = new Uint8Array(structSize);
|
|
195
|
+
outputType = "Uint8Array";
|
|
196
|
+
}
|
|
197
|
+
const dataView = new DataView(output.buffer, output.byteOffset, output.byteLength);
|
|
198
|
+
let offset = 0;
|
|
199
|
+
for (const { fieldValue, size } of fieldsInfo) {
|
|
200
|
+
fieldValue.serialize(dataView, offset);
|
|
201
|
+
offset += size;
|
|
202
|
+
}
|
|
203
|
+
if (outputType === "number") {
|
|
204
|
+
return structSize;
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
return output;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
211
|
//# sourceMappingURL=struct.js.map
|
package/esm/struct.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"struct.js","sourceRoot":"","sources":["../src/struct.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"struct.js","sourceRoot":"","sources":["../src/struct.ts"],"names":[],"mappings":"AAOA,OAAO,EACH,mBAAmB,EACnB,oBAAoB,EACpB,WAAW,GACd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAOhD,OAAO,EACH,qBAAqB,EACrB,eAAe,EACf,oCAAoC,EACpC,qBAAqB,EACrB,eAAe,EACf,wBAAwB,EACxB,4BAA4B,EAC5B,uCAAuC,GAC1C,MAAM,kBAAkB,CAAC;AAsK1B,MAAM,OAAO,MAAM;IAUC,OAAO,CAAW;IAElB,YAAY,CAAgB;IAE5B,MAAM,CAAU;IAEhB,KAAK,CAAyC;IAE9C,kBAAkB,CAIhC;IAEc,OAAO,CAA0B;IAEzC,KAAK,GAAG,CAAC,CAAC;IAClB;;OAEG;IACH,IAAW,IAAI;QACX,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IAEO,OAAO,GAGT,EAAE,CAAC;IAED,MAAM,GAAiC,EAAE,CAAC;IAE1C,iBAAiB,CAAgD;IAEzE,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ise.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export interface SyncPromise<T> {
|
|
2
|
-
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): SyncPromise<TResult1 | TResult2>;
|
|
3
|
-
valueOrPromise(): T | PromiseLike<T>;
|
|
4
|
-
}
|
|
5
|
-
interface SyncPromiseStatic {
|
|
6
|
-
reject<T = never>(reason?: any): SyncPromise<T>;
|
|
7
|
-
resolve(): SyncPromise<void>;
|
|
8
|
-
resolve<T>(value: T | PromiseLike<T>): SyncPromise<T>;
|
|
9
|
-
try<T>(executor: () => T | PromiseLike<T>): SyncPromise<T>;
|
|
10
|
-
}
|
|
11
|
-
export declare const SyncPromise: SyncPromiseStatic;
|
|
12
|
-
export {};
|
|
1
|
+
export interface SyncPromise<T> {
|
|
2
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): SyncPromise<TResult1 | TResult2>;
|
|
3
|
+
valueOrPromise(): T | PromiseLike<T>;
|
|
4
|
+
}
|
|
5
|
+
interface SyncPromiseStatic {
|
|
6
|
+
reject<T = never>(reason?: any): SyncPromise<T>;
|
|
7
|
+
resolve(): SyncPromise<void>;
|
|
8
|
+
resolve<T>(value: T | PromiseLike<T>): SyncPromise<T>;
|
|
9
|
+
try<T>(executor: () => T | PromiseLike<T>): SyncPromise<T>;
|
|
10
|
+
}
|
|
11
|
+
export declare const SyncPromise: SyncPromiseStatic;
|
|
12
|
+
export {};
|
|
13
13
|
//# sourceMappingURL=sync-promise.d.ts.map
|
package/esm/sync-promise.js
CHANGED
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
export const SyncPromise = {
|
|
2
|
-
reject(reason) {
|
|
3
|
-
return new RejectedSyncPromise(reason);
|
|
4
|
-
},
|
|
5
|
-
resolve(value) {
|
|
6
|
-
if (typeof value === "object" &&
|
|
7
|
-
value !== null &&
|
|
8
|
-
typeof value.then === "function") {
|
|
9
|
-
if (value instanceof PendingSyncPromise ||
|
|
10
|
-
value instanceof ResolvedSyncPromise ||
|
|
11
|
-
value instanceof RejectedSyncPromise) {
|
|
12
|
-
return value;
|
|
13
|
-
}
|
|
14
|
-
return new PendingSyncPromise(value);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
return new ResolvedSyncPromise(value);
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
try(executor) {
|
|
21
|
-
try {
|
|
22
|
-
return SyncPromise.resolve(executor());
|
|
23
|
-
}
|
|
24
|
-
catch (e) {
|
|
25
|
-
return SyncPromise.reject(e);
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
class PendingSyncPromise {
|
|
30
|
-
promise;
|
|
31
|
-
constructor(promise) {
|
|
32
|
-
this.promise = promise;
|
|
33
|
-
}
|
|
34
|
-
then(onfulfilled, onrejected) {
|
|
35
|
-
return new PendingSyncPromise(this.promise.then(onfulfilled, onrejected));
|
|
36
|
-
}
|
|
37
|
-
valueOrPromise() {
|
|
38
|
-
return this.promise;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
class ResolvedSyncPromise {
|
|
42
|
-
value;
|
|
43
|
-
constructor(value) {
|
|
44
|
-
this.value = value;
|
|
45
|
-
}
|
|
46
|
-
then(onfulfilled) {
|
|
47
|
-
if (!onfulfilled) {
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
return SyncPromise.try(() => onfulfilled(this.value));
|
|
51
|
-
}
|
|
52
|
-
valueOrPromise() {
|
|
53
|
-
return this.value;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
class RejectedSyncPromise {
|
|
57
|
-
reason;
|
|
58
|
-
constructor(reason) {
|
|
59
|
-
this.reason = reason;
|
|
60
|
-
}
|
|
61
|
-
then(onfulfilled, onrejected) {
|
|
62
|
-
if (!onrejected) {
|
|
63
|
-
return this;
|
|
64
|
-
}
|
|
65
|
-
return SyncPromise.try(() => onrejected(this.reason));
|
|
66
|
-
}
|
|
67
|
-
valueOrPromise() {
|
|
68
|
-
throw this.reason;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
1
|
+
export const SyncPromise = {
|
|
2
|
+
reject(reason) {
|
|
3
|
+
return new RejectedSyncPromise(reason);
|
|
4
|
+
},
|
|
5
|
+
resolve(value) {
|
|
6
|
+
if (typeof value === "object" &&
|
|
7
|
+
value !== null &&
|
|
8
|
+
typeof value.then === "function") {
|
|
9
|
+
if (value instanceof PendingSyncPromise ||
|
|
10
|
+
value instanceof ResolvedSyncPromise ||
|
|
11
|
+
value instanceof RejectedSyncPromise) {
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
return new PendingSyncPromise(value);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return new ResolvedSyncPromise(value);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
try(executor) {
|
|
21
|
+
try {
|
|
22
|
+
return SyncPromise.resolve(executor());
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
return SyncPromise.reject(e);
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
class PendingSyncPromise {
|
|
30
|
+
promise;
|
|
31
|
+
constructor(promise) {
|
|
32
|
+
this.promise = promise;
|
|
33
|
+
}
|
|
34
|
+
then(onfulfilled, onrejected) {
|
|
35
|
+
return new PendingSyncPromise(this.promise.then(onfulfilled, onrejected));
|
|
36
|
+
}
|
|
37
|
+
valueOrPromise() {
|
|
38
|
+
return this.promise;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
class ResolvedSyncPromise {
|
|
42
|
+
value;
|
|
43
|
+
constructor(value) {
|
|
44
|
+
this.value = value;
|
|
45
|
+
}
|
|
46
|
+
then(onfulfilled) {
|
|
47
|
+
if (!onfulfilled) {
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
return SyncPromise.try(() => onfulfilled(this.value));
|
|
51
|
+
}
|
|
52
|
+
valueOrPromise() {
|
|
53
|
+
return this.value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
class RejectedSyncPromise {
|
|
57
|
+
reason;
|
|
58
|
+
constructor(reason) {
|
|
59
|
+
this.reason = reason;
|
|
60
|
+
}
|
|
61
|
+
then(onfulfilled, onrejected) {
|
|
62
|
+
if (!onrejected) {
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
return SyncPromise.try(() => onrejected(this.reason));
|
|
66
|
+
}
|
|
67
|
+
valueOrPromise() {
|
|
68
|
+
throw this.reason;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
71
|
//# sourceMappingURL=sync-promise.js.map
|