@yume-chan/struct 0.0.10 → 0.0.12
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/LICENSE +1 -1
- package/README.md +102 -53
- package/{dts → esm}/basic/definition.d.ts +9 -13
- package/esm/basic/definition.d.ts.map +1 -0
- package/esm/basic/definition.js +13 -9
- package/esm/basic/definition.js.map +1 -1
- package/{dts → esm}/basic/field-value.d.ts +8 -8
- package/esm/basic/field-value.d.ts.map +1 -0
- package/esm/basic/field-value.js +12 -5
- package/esm/basic/field-value.js.map +1 -1
- package/esm/basic/index.d.ts +6 -0
- package/esm/basic/index.d.ts.map +1 -0
- package/esm/basic/index.js +5 -4
- package/esm/basic/index.js.map +1 -1
- package/esm/basic/options.d.ts +10 -0
- package/esm/basic/options.d.ts.map +1 -0
- package/esm/basic/{context.js → options.js} +1 -1
- package/esm/basic/options.js.map +1 -0
- package/esm/basic/stream.d.ts +19 -0
- package/esm/basic/stream.d.ts.map +1 -0
- package/esm/basic/stream.js +2 -0
- package/esm/basic/stream.js.map +1 -0
- package/{dts → esm}/basic/struct-value.d.ts +3 -3
- package/esm/basic/struct-value.d.ts.map +1 -0
- package/esm/basic/struct-value.js +7 -9
- package/esm/basic/struct-value.js.map +1 -1
- package/esm/index.d.ts +14 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +5 -5
- package/esm/index.js.map +1 -1
- package/{dts → esm}/struct.d.ts +16 -12
- package/esm/struct.d.ts.map +1 -0
- package/esm/struct.js +67 -54
- package/esm/struct.js.map +1 -1
- package/esm/syncbird.d.ts +65 -0
- package/esm/syncbird.d.ts.map +1 -0
- package/esm/syncbird.js +15 -4
- package/esm/syncbird.js.map +1 -1
- package/esm/types/bigint.d.ts +25 -0
- package/esm/types/bigint.d.ts.map +1 -0
- package/esm/types/bigint.js +45 -0
- package/esm/types/bigint.js.map +1 -0
- package/esm/types/buffer/base.d.ts +63 -0
- package/esm/types/buffer/base.d.ts.map +1 -0
- package/esm/types/buffer/base.js +100 -0
- package/esm/types/buffer/base.js.map +1 -0
- package/esm/types/buffer/fixed-length.d.ts +8 -0
- package/esm/types/buffer/fixed-length.d.ts.map +1 -0
- package/esm/types/buffer/fixed-length.js +8 -0
- package/esm/types/buffer/fixed-length.js.map +1 -0
- package/esm/types/buffer/index.d.ts +4 -0
- package/esm/types/buffer/index.d.ts.map +1 -0
- package/esm/types/buffer/index.js +4 -0
- package/esm/types/buffer/index.js.map +1 -0
- package/esm/types/buffer/variable-length.d.ts +43 -0
- package/esm/types/buffer/variable-length.d.ts.map +1 -0
- package/esm/types/buffer/variable-length.js +76 -0
- package/esm/types/buffer/variable-length.js.map +1 -0
- package/esm/types/index.d.ts +4 -0
- package/esm/types/index.d.ts.map +1 -0
- package/esm/types/index.js +3 -4
- package/esm/types/index.js.map +1 -1
- package/{dts → esm}/types/number.d.ts +10 -12
- package/esm/types/number.d.ts.map +1 -0
- package/esm/types/number.js +21 -14
- package/esm/types/number.js.map +1 -1
- package/{dts → esm}/utils.d.ts +2 -2
- package/{dts → esm}/utils.d.ts.map +1 -1
- package/esm/utils.js +9 -3
- package/esm/utils.js.map +1 -1
- package/package.json +47 -47
- package/src/basic/definition.ts +11 -13
- package/src/basic/field-value.ts +8 -8
- package/src/basic/index.ts +5 -4
- package/src/basic/options.ts +19 -0
- package/src/basic/stream.ts +19 -0
- package/src/basic/struct-value.ts +4 -4
- package/src/index.ts +17 -5
- package/src/struct.ts +89 -75
- package/src/syncbird.ts +86 -10
- package/src/types/bigint.ts +102 -0
- package/src/types/buffer/base.ts +176 -0
- package/src/types/buffer/fixed-length.ts +17 -0
- package/src/types/buffer/index.ts +3 -0
- package/src/types/{variable-length-array-buffer.ts → buffer/variable-length.ts} +48 -34
- package/src/types/index.ts +3 -4
- package/src/types/number.ts +27 -26
- package/src/utils.ts +13 -5
- package/dts/basic/context.d.ts +0 -28
- package/dts/basic/context.d.ts.map +0 -1
- package/dts/basic/definition.d.ts.map +0 -1
- package/dts/basic/field-value.d.ts.map +0 -1
- package/dts/basic/index.d.ts +0 -5
- package/dts/basic/index.d.ts.map +0 -1
- package/dts/basic/struct-value.d.ts.map +0 -1
- package/dts/index.d.ts +0 -6
- package/dts/index.d.ts.map +0 -1
- package/dts/struct.d.ts.map +0 -1
- package/dts/syncbird.d.ts +0 -20
- package/dts/syncbird.d.ts.map +0 -1
- package/dts/types/array-buffer.d.ts +0 -71
- package/dts/types/array-buffer.d.ts.map +0 -1
- package/dts/types/fixed-length-array-buffer.d.ts +0 -8
- package/dts/types/fixed-length-array-buffer.d.ts.map +0 -1
- package/dts/types/index.d.ts +0 -5
- package/dts/types/index.d.ts.map +0 -1
- package/dts/types/number.d.ts.map +0 -1
- package/dts/types/variable-length-array-buffer.d.ts +0 -32
- package/dts/types/variable-length-array-buffer.d.ts.map +0 -1
- package/esm/basic/context.js.map +0 -1
- package/esm/types/array-buffer.js +0 -110
- package/esm/types/array-buffer.js.map +0 -1
- package/esm/types/fixed-length-array-buffer.js +0 -8
- package/esm/types/fixed-length-array-buffer.js.map +0 -1
- package/esm/types/variable-length-array-buffer.js +0 -71
- package/esm/types/variable-length-array-buffer.js.map +0 -1
- package/src/basic/context.ts +0 -32
- package/src/types/array-buffer.ts +0 -194
- package/src/types/fixed-length-array-buffer.ts +0 -17
package/CHANGELOG.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yume-chan/struct",
|
|
3
3
|
"entries": [
|
|
4
|
+
{
|
|
5
|
+
"version": "0.0.12",
|
|
6
|
+
"tag": "@yume-chan/struct_v0.0.12",
|
|
7
|
+
"date": "Sun, 03 Apr 2022 11:18:47 GMT",
|
|
8
|
+
"comments": {}
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"version": "0.0.11",
|
|
12
|
+
"tag": "@yume-chan/struct_v0.0.11",
|
|
13
|
+
"date": "Sun, 03 Apr 2022 11:18:11 GMT",
|
|
14
|
+
"comments": {
|
|
15
|
+
"none": [
|
|
16
|
+
{
|
|
17
|
+
"comment": "Update to use Web Streams API"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"comment": "Improve compatibility with Node.js 12 ESM format"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"comment": "Update compatibility matrix"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"comment": "Update license year"
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
},
|
|
4
31
|
{
|
|
5
32
|
"version": "0.0.10",
|
|
6
33
|
"tag": "@yume-chan/struct_v0.0.10",
|
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
# Change Log - @yume-chan/struct
|
|
2
2
|
|
|
3
|
-
This log was last generated on Sun,
|
|
3
|
+
This log was last generated on Sun, 03 Apr 2022 11:18:47 GMT and should not be manually modified.
|
|
4
|
+
|
|
5
|
+
## 0.0.12
|
|
6
|
+
Sun, 03 Apr 2022 11:18:47 GMT
|
|
7
|
+
|
|
8
|
+
_Version update only_
|
|
9
|
+
|
|
10
|
+
## 0.0.11
|
|
11
|
+
Sun, 03 Apr 2022 11:18:11 GMT
|
|
12
|
+
|
|
13
|
+
### Updates
|
|
14
|
+
|
|
15
|
+
- Update to use Web Streams API
|
|
16
|
+
- Improve compatibility with Node.js 12 ESM format
|
|
17
|
+
- Update compatibility matrix
|
|
18
|
+
- Update license year
|
|
4
19
|
|
|
5
20
|
## 0.0.10
|
|
6
21
|
Sun, 09 Jan 2022 15:52:20 GMT
|
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# @yume-chan/struct
|
|
2
2
|
|
|
3
|
+
<!--
|
|
4
|
+
cspell: ignore Codecov
|
|
5
|
+
cspell: ignore uint8arraystring
|
|
6
|
+
-->
|
|
7
|
+
|
|
3
8
|

|
|
4
9
|

|
|
5
10
|
[](https://www.npmjs.com/package/@yume-chan/struct)
|
|
@@ -37,20 +42,25 @@ value.baz // string
|
|
|
37
42
|
const buffer = MyStruct.serialize({
|
|
38
43
|
foo: 42,
|
|
39
44
|
bar: 42n,
|
|
40
|
-
// `bazLength` automatically set to `baz
|
|
45
|
+
// `bazLength` automatically set to `baz`'s byte length
|
|
41
46
|
baz: 'Hello, World!',
|
|
42
47
|
});
|
|
43
48
|
```
|
|
44
49
|
|
|
50
|
+
<!-- cspell: disable -->
|
|
51
|
+
|
|
45
52
|
- [Installation](#installation)
|
|
46
53
|
- [Quick Start](#quick-start)
|
|
47
54
|
- [Compatibility](#compatibility)
|
|
55
|
+
- [Basic usage](#basic-usage)
|
|
56
|
+
- [`int64`/`uint64`](#int64uint64)
|
|
57
|
+
- [`string`](#string)
|
|
48
58
|
- [API](#api)
|
|
49
59
|
- [`placeholder`](#placeholder)
|
|
50
60
|
- [`Struct`](#struct)
|
|
51
61
|
- [`int8`/`uint8`/`int16`/`uint16`/`int32`/`uint32`](#int8uint8int16uint16int32uint32)
|
|
52
|
-
- [`int64`/`uint64`](#int64uint64)
|
|
53
|
-
- [`
|
|
62
|
+
- [`int64`/`uint64`](#int64uint64-1)
|
|
63
|
+
- [`uint8Array`/`string`](#uint8arraystring)
|
|
54
64
|
- [`fields`](#fields)
|
|
55
65
|
- [`extra`](#extra)
|
|
56
66
|
- [`postDeserialize`](#postdeserialize)
|
|
@@ -58,6 +68,7 @@ const buffer = MyStruct.serialize({
|
|
|
58
68
|
- [`serialize`](#serialize)
|
|
59
69
|
- [Custom field type](#custom-field-type)
|
|
60
70
|
- [`Struct#field`](#structfield)
|
|
71
|
+
- [Relationship between types](#relationship-between-types)
|
|
61
72
|
- [`StructFieldDefinition`](#structfielddefinition)
|
|
62
73
|
- [`TValue`/`TOmitInitKey`](#tvaluetomitinitkey)
|
|
63
74
|
- [`getSize`](#getsize)
|
|
@@ -68,31 +79,47 @@ const buffer = MyStruct.serialize({
|
|
|
68
79
|
- [`get`/`set`](#getset)
|
|
69
80
|
- [`serialize`](#serialize-1)
|
|
70
81
|
|
|
82
|
+
<!-- cspell: enable -->
|
|
83
|
+
|
|
71
84
|
## Compatibility
|
|
72
85
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
|
80
|
-
|
|
|
81
|
-
| [`
|
|
82
|
-
| [`
|
|
83
|
-
|
|
|
86
|
+
Here is a list of features, their used APIs, and their compatibilities. If an optional feature is not actually used, its requirements can be ignored.
|
|
87
|
+
|
|
88
|
+
Some features can be polyfilled to support older runtimes, but this library doesn't ship with any polyfills.
|
|
89
|
+
|
|
90
|
+
### Basic usage
|
|
91
|
+
|
|
92
|
+
| API | Chrome | Edge | Firefox | Internet Explorer | Safari | Node.js |
|
|
93
|
+
| -------------------------------- | ------ | ---- | ------- | ----------------- | ------ | ------- |
|
|
94
|
+
| [`Promise`][MDN_Promise] | 32 | 12 | 29 | No | 8 | 0.12 |
|
|
95
|
+
| [`ArrayBuffer`][MDN_ArrayBuffer] | 7 | 12 | 4 | 10 | 5.1 | 0.10 |
|
|
96
|
+
| [`Uint8Array`][MDN_Uint8Array] | 7 | 12 | 4 | 10 | 5.1 | 0.10 |
|
|
97
|
+
| [`DataView`][MDN_DataView] | 9 | 12 | 15 | 10 | 5.1 | 0.10 |
|
|
98
|
+
| *Overall* | 32 | 12 | 29 | No | 8 | 0.12 |
|
|
99
|
+
|
|
100
|
+
### [`int64`/`uint64`](#int64uint64-1)
|
|
84
101
|
|
|
85
|
-
|
|
102
|
+
| API | Chrome | Edge | Firefox | Internet Explorer | Safari | Node.js |
|
|
103
|
+
| ---------------------------------- | ------ | ---- | ------- | ----------------- | ------ | ------- |
|
|
104
|
+
| [`BigInt`][MDN_BigInt]<sup>1</sup> | 67 | 79 | 68 | No | 14 | 10.4 |
|
|
86
105
|
|
|
87
|
-
<sup>
|
|
106
|
+
<sup>1</sup> Can't be polyfilled
|
|
107
|
+
|
|
108
|
+
### [`string`](#uint8arraystring)
|
|
109
|
+
|
|
110
|
+
| API | Chrome | Edge | Firefox | Internet Explorer | Safari | Node.js |
|
|
111
|
+
| -------------------------------- | ------ | ---- | ------- | ----------------- | ------ | ------------------- |
|
|
112
|
+
| [`TextEncoder`][MDN_TextEncoder] | 38 | 79 | 19 | No | 10.1 | 8.3<sup>1</sup>, 11 |
|
|
113
|
+
|
|
114
|
+
<sup>1</sup> `TextEncoder` and `TextDecoder` are only available in `util` module. Need to be assigned to `globalThis`.
|
|
88
115
|
|
|
89
116
|
[MDN_Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
|
|
90
117
|
[MDN_ArrayBuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
|
|
91
118
|
[MDN_Uint8Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
|
|
92
119
|
[MDN_DataView]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
|
|
93
120
|
[MDN_BigInt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
|
|
94
|
-
[
|
|
95
|
-
[
|
|
121
|
+
[MDN_DataView]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
|
|
122
|
+
[MDN_TextEncoder]: https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
|
|
96
123
|
|
|
97
124
|
## API
|
|
98
125
|
|
|
@@ -148,7 +175,7 @@ class Struct<
|
|
|
148
175
|
}
|
|
149
176
|
```
|
|
150
177
|
|
|
151
|
-
Creates a new structure
|
|
178
|
+
Creates a new structure definition.
|
|
152
179
|
|
|
153
180
|
<details>
|
|
154
181
|
<summary>Generic parameters (click to expand)</summary>
|
|
@@ -156,7 +183,7 @@ Creates a new structure declaration.
|
|
|
156
183
|
This information was added to help you understand how does it work. These are considered as "internal state" so don't specify them manually.
|
|
157
184
|
|
|
158
185
|
1. `TFields`: Type of the Struct value. Modified when new fields are added.
|
|
159
|
-
2. `TOmitInitKey`: When serializing a structure containing variable length
|
|
186
|
+
2. `TOmitInitKey`: When serializing a structure containing variable length buffers, the length field can be calculate from the buffer field, so they doesn't need to be provided explicitly.
|
|
160
187
|
3. `TExtra`: Type of extra fields. Modified when `extra` is called.
|
|
161
188
|
4. `TPostDeserialized`: State of the `postDeserialize` function. Modified when `postDeserialize` is called. Affects return type of `deserialize`
|
|
162
189
|
</details>
|
|
@@ -196,7 +223,7 @@ Appends an `int8`/`uint8`/`int16`/`uint16`/`int32`/`uint32` field to the `Struct
|
|
|
196
223
|
|
|
197
224
|
**Parameters**
|
|
198
225
|
|
|
199
|
-
1. `name`: (Required) Field name. Must
|
|
226
|
+
1. `name`: (Required) Field name. Must be a string literal.
|
|
200
227
|
2. `_typescriptType`: Set field's type. See examples below.
|
|
201
228
|
|
|
202
229
|
**Note**
|
|
@@ -259,19 +286,19 @@ int64<
|
|
|
259
286
|
>;
|
|
260
287
|
```
|
|
261
288
|
|
|
262
|
-
Appends an `int64`/`uint64` field to the `Struct`.
|
|
289
|
+
Appends an `int64`/`uint64` field to the `Struct`. The usage is same as `uint32`/`uint32`.
|
|
263
290
|
|
|
264
291
|
Requires native support for `BigInt`. Check [compatibility table](#compatibility) for more information.
|
|
265
292
|
|
|
266
|
-
#### `
|
|
293
|
+
#### `uint8Array`/`string`
|
|
267
294
|
|
|
268
295
|
```ts
|
|
269
|
-
|
|
296
|
+
uint8Array<
|
|
270
297
|
TName extends string | number | symbol,
|
|
271
298
|
TTypeScriptType = ArrayBuffer
|
|
272
299
|
>(
|
|
273
300
|
name: TName,
|
|
274
|
-
options:
|
|
301
|
+
options: FixedLengthBufferLikeFieldOptions,
|
|
275
302
|
_typescriptType?: TTypeScriptType,
|
|
276
303
|
): Struct<
|
|
277
304
|
TFields & Record<TName, TTypeScriptType>,
|
|
@@ -280,9 +307,10 @@ arraybuffer<
|
|
|
280
307
|
TPostDeserialized
|
|
281
308
|
>;
|
|
282
309
|
|
|
283
|
-
|
|
310
|
+
uint8Array<
|
|
284
311
|
TName extends string | number | symbol,
|
|
285
|
-
|
|
312
|
+
TLengthField extends LengthField<TFields>,
|
|
313
|
+
TOptions extends VariableLengthBufferLikeFieldOptions<TFields, TLengthField>,
|
|
286
314
|
TTypeScriptType = ArrayBuffer,
|
|
287
315
|
>(
|
|
288
316
|
name: TName,
|
|
@@ -290,20 +318,18 @@ arraybuffer<
|
|
|
290
318
|
_typescriptType?: TTypeScriptType,
|
|
291
319
|
): Struct<
|
|
292
320
|
TFields & Record<TName, TTypeScriptType>,
|
|
293
|
-
TOmitInitKey |
|
|
321
|
+
TOmitInitKey | TLengthField,
|
|
294
322
|
TExtra,
|
|
295
323
|
TPostDeserialized
|
|
296
324
|
>;
|
|
297
325
|
```
|
|
298
326
|
|
|
299
|
-
Appends an `
|
|
327
|
+
Appends an `uint8Array`/`string` field to the `Struct`.
|
|
300
328
|
|
|
301
329
|
The `options` parameter defines its length, it can be in two formats:
|
|
302
330
|
|
|
303
331
|
* `{ length: number }`: Presence of the `length` option indicates that it's a fixed length array.
|
|
304
|
-
* `{ lengthField: string }`: Presence of the `lengthField` option indicates it's a variable length array. The `lengthField` options must refers to a `number` or `string` typed field that's already defined in this `Struct`. When deserializing, it will use that field's value as its length.
|
|
305
|
-
|
|
306
|
-
All these three are actually deserialized to `ArrayBuffer`, then converted to `Uint8ClampedArray` or `string` for ease of use.
|
|
332
|
+
* `{ lengthField: string; lengthFieldRadix?: number }`: Presence of the `lengthField` option indicates it's a variable length array. The `lengthField` options must refers to a `number` or `string` (can't be `bigint`) typed field that's already defined in this `Struct`. If the length field is a `string`, the optional `lengthFieldRadix` option (defaults to `10`) defines the radix when converting the string to a number. When deserializing, it will use that field's value as its length. When serializing, it will write its length to that field.
|
|
307
333
|
|
|
308
334
|
#### `fields`
|
|
309
335
|
|
|
@@ -443,7 +469,7 @@ Multiple calls merge all extra fields together.
|
|
|
443
469
|
postDeserialize(): Struct<TFields, TOmitInitKey, TExtra, undefined>;
|
|
444
470
|
```
|
|
445
471
|
|
|
446
|
-
Remove any registered post
|
|
472
|
+
Remove any registered post-deserialization callback.
|
|
447
473
|
|
|
448
474
|
```ts
|
|
449
475
|
postDeserialize(
|
|
@@ -526,10 +552,20 @@ interface StructDeserializeStream {
|
|
|
526
552
|
/**
|
|
527
553
|
* Read data from the underlying data source.
|
|
528
554
|
*
|
|
529
|
-
*
|
|
555
|
+
* The stream must return exactly `length` bytes or data. If that's not possible
|
|
530
556
|
* (due to end of file or other error condition), it must throw an error.
|
|
531
557
|
*/
|
|
532
|
-
read(length: number):
|
|
558
|
+
read(length: number): Uint8Array;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
interface StructAsyncDeserializeStream {
|
|
562
|
+
/**
|
|
563
|
+
* Read data from the underlying data source.
|
|
564
|
+
*
|
|
565
|
+
* The stream must return exactly `length` bytes or data. If that's not possible
|
|
566
|
+
* (due to end of file or other error condition), it must throw an error.
|
|
567
|
+
*/
|
|
568
|
+
read(length: number): Promise<Uint8Array>;
|
|
533
569
|
}
|
|
534
570
|
|
|
535
571
|
deserialize(
|
|
@@ -548,7 +584,9 @@ deserialize(
|
|
|
548
584
|
>;
|
|
549
585
|
```
|
|
550
586
|
|
|
551
|
-
Deserialize a
|
|
587
|
+
Deserialize a struct value from `stream`.
|
|
588
|
+
|
|
589
|
+
It will be synchronous (returns a value) or asynchronous (returns a `Promise`) depending on the type of `stream`.
|
|
552
590
|
|
|
553
591
|
As the signature shows, if the `postDeserialize` callback returns any value, `deserialize` will return that value instead.
|
|
554
592
|
|
|
@@ -557,16 +595,17 @@ The `read` method of `stream`, when being called, should returns exactly `length
|
|
|
557
595
|
#### `serialize`
|
|
558
596
|
|
|
559
597
|
```ts
|
|
560
|
-
serialize(
|
|
561
|
-
|
|
562
|
-
): ArrayBuffer;
|
|
598
|
+
serialize(init: Evaluate<Omit<TFields, TOmitInitKey>>): Uint8Array;
|
|
599
|
+
serialize(init: Evaluate<Omit<TFields, TOmitInitKey>>, output: Uint8Array): number;
|
|
563
600
|
```
|
|
564
601
|
|
|
565
|
-
Serialize a
|
|
602
|
+
Serialize a struct value into an `Uint8Array`.
|
|
603
|
+
|
|
604
|
+
If an `output` is given, it will serialize the struct into it, and returns the number of bytes written.
|
|
566
605
|
|
|
567
606
|
## Custom field type
|
|
568
607
|
|
|
569
|
-
|
|
608
|
+
It's also possible to create your own field types.
|
|
570
609
|
|
|
571
610
|
### `Struct#field`
|
|
572
611
|
|
|
@@ -587,7 +626,7 @@ field<
|
|
|
587
626
|
|
|
588
627
|
Appends a `StructFieldDefinition` to the `Struct`.
|
|
589
628
|
|
|
590
|
-
|
|
629
|
+
All built-in field type methods are actually aliases to it. For example, calling
|
|
591
630
|
|
|
592
631
|
```ts
|
|
593
632
|
struct.int8('foo')
|
|
@@ -604,6 +643,15 @@ struct.field(
|
|
|
604
643
|
)
|
|
605
644
|
```
|
|
606
645
|
|
|
646
|
+
### Relationship between types
|
|
647
|
+
|
|
648
|
+
A `Struct` is a map between keys and `StructFieldDefinition`s.
|
|
649
|
+
|
|
650
|
+
A `StructValue` is a map between keys and `StructFieldValue`s.
|
|
651
|
+
|
|
652
|
+
A `Struct` can create (deserialize) multiple `StructValue`s with same field definitions.
|
|
653
|
+
|
|
654
|
+
Each time a `Struct` deserialize, each `StructFieldDefinition` in it creates exactly one `StructFieldValue` to be put into the `StructValue`.
|
|
607
655
|
|
|
608
656
|
### `StructFieldDefinition`
|
|
609
657
|
|
|
@@ -619,13 +667,13 @@ abstract class StructFieldDefinition<
|
|
|
619
667
|
}
|
|
620
668
|
```
|
|
621
669
|
|
|
622
|
-
A
|
|
670
|
+
A field definition defines how to deserialize a field.
|
|
623
671
|
|
|
624
|
-
It's an `abstract` class, means it
|
|
672
|
+
It's an `abstract` class, means it can't be constructed (`new`ed) directly. It's only used as a base class for other field types.
|
|
625
673
|
|
|
626
674
|
#### `TValue`/`TOmitInitKey`
|
|
627
675
|
|
|
628
|
-
These two fields
|
|
676
|
+
These two fields provide type information to TypeScript compiler. Their values will always be `undefined`, but having correct types is enough. You don't need to touch them.
|
|
629
677
|
|
|
630
678
|
#### `getSize`
|
|
631
679
|
|
|
@@ -666,14 +714,17 @@ abstract deserialize(
|
|
|
666
714
|
): Promise<StructFieldValue<this>>;
|
|
667
715
|
```
|
|
668
716
|
|
|
669
|
-
Derived classes must implement this method to define how to deserialize a value from `stream`.
|
|
717
|
+
Derived classes must implement this method to define how to deserialize a value from `stream`.
|
|
718
|
+
|
|
719
|
+
It must be synchronous (returns a value) or asynchronous (returns a `Promise`) depending on the type of `stream`.
|
|
670
720
|
|
|
671
721
|
Usually implementations should be:
|
|
672
722
|
|
|
673
|
-
1.
|
|
674
|
-
2.
|
|
723
|
+
1. Read required bytes from `stream`
|
|
724
|
+
2. Parse it to your type
|
|
725
|
+
3. Pass the value into your own `create` method
|
|
675
726
|
|
|
676
|
-
Sometimes,
|
|
727
|
+
Sometimes, extra metadata is present when deserializing, but need to be calculated when serializing, for example a UTF-8 encoded string may have different length between itself (character count) and serialized form (byte length). So `deserialize` can save those metadata on the `StructFieldValue` instance for later use.
|
|
677
728
|
|
|
678
729
|
### `StructFieldValue`
|
|
679
730
|
|
|
@@ -683,9 +734,7 @@ abstract class StructFieldValue<
|
|
|
683
734
|
>
|
|
684
735
|
```
|
|
685
736
|
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
Each `StructFieldValue` is linked to a `StructFieldDefinition`.
|
|
737
|
+
A field value defines how to serialize a field.
|
|
689
738
|
|
|
690
739
|
#### `getSize`
|
|
691
740
|
|
|
@@ -704,7 +753,7 @@ get(): TDefinition['TValue'];
|
|
|
704
753
|
set(value: TDefinition['TValue']): void;
|
|
705
754
|
```
|
|
706
755
|
|
|
707
|
-
Defines how to get or set this field's value. By default, it
|
|
756
|
+
Defines how to get or set this field's value. By default, it reads/writes its `value` field.
|
|
708
757
|
|
|
709
758
|
If one needs to manipulate other states when getting/setting values, they can override these methods.
|
|
710
759
|
|
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
import type { StructAsyncDeserializeStream, StructDeserializeStream
|
|
2
|
-
import type { StructFieldValue } from
|
|
3
|
-
import type { StructValue } from
|
|
1
|
+
import type { StructAsyncDeserializeStream, StructDeserializeStream } from "./stream.js";
|
|
2
|
+
import type { StructFieldValue } from "./field-value.js";
|
|
3
|
+
import type { StructValue } from "./struct-value.js";
|
|
4
|
+
import type { StructOptions } from "./options.js";
|
|
4
5
|
/**
|
|
5
|
-
* A field definition
|
|
6
|
-
*
|
|
7
|
-
* `Struct` record fields in a list of `StructFieldDefinition`s.
|
|
8
|
-
*
|
|
9
|
-
* When `Struct#create` or `Struct#deserialize` are called, each field's definition
|
|
10
|
-
* crates its own type of `StructFieldValue` to manage the field value in that `Struct` instance.
|
|
11
|
-
*
|
|
12
|
-
* One `StructFieldDefinition` can represents multiple similar types, just returns the corresponding
|
|
13
|
-
* `StructFieldValue` when `createValue` was called.
|
|
6
|
+
* A field definition defines how to deserialize a field.
|
|
14
7
|
*
|
|
15
8
|
* @template TOptions TypeScript type of this definition's `options`.
|
|
16
9
|
* @template TValue TypeScript type of this field.
|
|
@@ -40,7 +33,10 @@ export declare abstract class StructFieldDefinition<TOptions = void, TValue = un
|
|
|
40
33
|
*/
|
|
41
34
|
abstract create(options: Readonly<StructOptions>, struct: StructValue, value: TValue): StructFieldValue<this>;
|
|
42
35
|
/**
|
|
43
|
-
* When implemented in derived classes,
|
|
36
|
+
* When implemented in derived classes,It must be synchronous (returns a value) or asynchronous (returns a `Promise`) depending
|
|
37
|
+
* on the type of `stream`. reads and creates a `StructFieldValue` from `stream`.
|
|
38
|
+
*
|
|
39
|
+
* `Syncbird` can be used to make the implementation easier.
|
|
44
40
|
*/
|
|
45
41
|
abstract deserialize(options: Readonly<StructOptions>, stream: StructDeserializeStream, struct: StructValue): StructFieldValue<this>;
|
|
46
42
|
abstract deserialize(options: Readonly<StructOptions>, stream: StructAsyncDeserializeStream, struct: StructValue): Promise<StructFieldValue<this>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definition.d.ts","sourceRoot":"","sources":["../../src/basic/definition.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,4BAA4B,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACzF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD;;;;;;GAMG;AACH,8BAAsB,qBAAqB,CACvC,QAAQ,GAAG,IAAI,EACf,MAAM,GAAG,OAAO,EAChB,YAAY,SAAS,WAAW,GAAG,KAAK;IAExC;;;OAGG;IACH,SAAgB,MAAM,EAAG,MAAM,CAAC;IAEhC;;;OAGG;IACH,SAAgB,YAAY,EAAG,YAAY,CAAC;IAE5C,SAAgB,OAAO,EAAE,QAAQ,CAAC;gBAEf,OAAO,EAAE,QAAQ;IAIpC;;;;OAIG;aACa,OAAO,IAAI,MAAM;IAEjC;;OAEG;aACa,MAAM,CAClB,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,EAChC,MAAM,EAAE,WAAW,EACnB,KAAK,EAAE,MAAM,GACd,gBAAgB,CAAC,IAAI,CAAC;IAEzB;;;;;OAKG;aACa,WAAW,CACvB,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,EAChC,MAAM,EAAE,uBAAuB,EAC/B,MAAM,EAAE,WAAW,GACpB,gBAAgB,CAAC,IAAI,CAAC;aACT,WAAW,CACvB,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,EAChC,MAAM,EAAE,4BAA4B,EACpC,MAAM,EAAE,WAAW,GACpB,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;CACrC"}
|
package/esm/basic/definition.js
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
|
+
// cspell: ignore Syncbird
|
|
1
2
|
/**
|
|
2
|
-
* A field definition
|
|
3
|
-
*
|
|
4
|
-
* `Struct` record fields in a list of `StructFieldDefinition`s.
|
|
5
|
-
*
|
|
6
|
-
* When `Struct#create` or `Struct#deserialize` are called, each field's definition
|
|
7
|
-
* crates its own type of `StructFieldValue` to manage the field value in that `Struct` instance.
|
|
8
|
-
*
|
|
9
|
-
* One `StructFieldDefinition` can represents multiple similar types, just returns the corresponding
|
|
10
|
-
* `StructFieldValue` when `createValue` was called.
|
|
3
|
+
* A field definition defines how to deserialize a field.
|
|
11
4
|
*
|
|
12
5
|
* @template TOptions TypeScript type of this definition's `options`.
|
|
13
6
|
* @template TValue TypeScript type of this field.
|
|
14
7
|
* @template TOmitInitKey Optionally remove some fields from the init type. Should be a union of string literal types.
|
|
15
8
|
*/
|
|
16
9
|
export class StructFieldDefinition {
|
|
10
|
+
/**
|
|
11
|
+
* When `T` is a type initiated `StructFieldDefinition`,
|
|
12
|
+
* use `T['TValue']` to retrieve its `TValue` type parameter.
|
|
13
|
+
*/
|
|
14
|
+
TValue;
|
|
15
|
+
/**
|
|
16
|
+
* When `T` is a type initiated `StructFieldDefinition`,
|
|
17
|
+
* use `T['TOmitInitKey']` to retrieve its `TOmitInitKey` type parameter.
|
|
18
|
+
*/
|
|
19
|
+
TOmitInitKey;
|
|
20
|
+
options;
|
|
17
21
|
constructor(options) {
|
|
18
22
|
this.options = options;
|
|
19
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definition.js","sourceRoot":"","sources":["../../src/basic/definition.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"definition.js","sourceRoot":"","sources":["../../src/basic/definition.ts"],"names":[],"mappings":"AAAA,0BAA0B;AAO1B;;;;;;GAMG;AACH,MAAM,OAAgB,qBAAqB;IAKvC;;;OAGG;IACa,MAAM,CAAU;IAEhC;;;OAGG;IACa,YAAY,CAAgB;IAE5B,OAAO,CAAW;IAElC,YAAmB,OAAiB;QAChC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;CAkCJ"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type { StructValue } from
|
|
1
|
+
import type { StructFieldDefinition } from "./definition.js";
|
|
2
|
+
import type { StructOptions } from "./options.js";
|
|
3
|
+
import type { StructValue } from "./struct-value.js";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* A field value defines how to serialize a field.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* It may contains extra metadata about the value which are essential or
|
|
8
|
+
* helpful for the serialization process.
|
|
9
9
|
*/
|
|
10
10
|
export declare abstract class StructFieldValue<TDefinition extends StructFieldDefinition<any, any, any> = StructFieldDefinition<any, any, any>> {
|
|
11
11
|
/** Gets the definition associated with this runtime value */
|
|
@@ -23,11 +23,11 @@ export declare abstract class StructFieldValue<TDefinition extends StructFieldDe
|
|
|
23
23
|
*/
|
|
24
24
|
getSize(): number;
|
|
25
25
|
/**
|
|
26
|
-
* When implemented in derived classes,
|
|
26
|
+
* When implemented in derived classes, reads current field's value.
|
|
27
27
|
*/
|
|
28
28
|
get(): TDefinition['TValue'];
|
|
29
29
|
/**
|
|
30
|
-
* When implemented in derived classes,
|
|
30
|
+
* When implemented in derived classes, updates current field's value.
|
|
31
31
|
*/
|
|
32
32
|
set(value: TDefinition['TValue']): void;
|
|
33
33
|
/**
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"field-value.d.ts","sourceRoot":"","sources":["../../src/basic/field-value.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD;;;;;GAKG;AACH,8BAAsB,gBAAgB,CAClC,WAAW,SAAS,qBAAqB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,qBAAqB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;IAE/F,6DAA6D;IAC7D,SAAgB,UAAU,EAAE,WAAW,CAAC;IAExC,kDAAkD;IAClD,SAAgB,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,CAAC;IAEjD,4CAA4C;IAC5C,SAAgB,MAAM,EAAE,WAAW,CAAC;IAEpC,SAAS,CAAC,KAAK,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;gBAGnC,UAAU,EAAE,WAAW,EACvB,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,EAChC,MAAM,EAAE,WAAW,EACnB,KAAK,EAAE,WAAW,CAAC,QAAQ,CAAC;IAQhC;;;;OAIG;IACI,OAAO,IAAI,MAAM;IAIxB;;OAEG;IACI,GAAG,IAAI,WAAW,CAAC,QAAQ,CAAC;IAInC;;OAEG;IACI,GAAG,CAAC,KAAK,EAAE,WAAW,CAAC,QAAQ,CAAC,GAAG,IAAI;IAI9C;;OAEG;aACa,SAAS,CACrB,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,MAAM,GACf,IAAI;CACV"}
|
package/esm/basic/field-value.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* A field value defines how to serialize a field.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* It may contains extra metadata about the value which are essential or
|
|
5
|
+
* helpful for the serialization process.
|
|
6
6
|
*/
|
|
7
7
|
export class StructFieldValue {
|
|
8
|
+
/** Gets the definition associated with this runtime value */
|
|
9
|
+
definition;
|
|
10
|
+
/** Gets the options of the associated `Struct` */
|
|
11
|
+
options;
|
|
12
|
+
/** Gets the associated `Struct` instance */
|
|
13
|
+
struct;
|
|
14
|
+
value;
|
|
8
15
|
constructor(definition, options, struct, value) {
|
|
9
16
|
this.definition = definition;
|
|
10
17
|
this.options = options;
|
|
@@ -20,13 +27,13 @@ export class StructFieldValue {
|
|
|
20
27
|
return this.definition.getSize();
|
|
21
28
|
}
|
|
22
29
|
/**
|
|
23
|
-
* When implemented in derived classes,
|
|
30
|
+
* When implemented in derived classes, reads current field's value.
|
|
24
31
|
*/
|
|
25
32
|
get() {
|
|
26
33
|
return this.value;
|
|
27
34
|
}
|
|
28
35
|
/**
|
|
29
|
-
* When implemented in derived classes,
|
|
36
|
+
* When implemented in derived classes, updates current field's value.
|
|
30
37
|
*/
|
|
31
38
|
set(value) {
|
|
32
39
|
this.value = value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field-value.js","sourceRoot":"","sources":["../../src/basic/field-value.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,MAAM,OAAgB,gBAAgB;
|
|
1
|
+
{"version":3,"file":"field-value.js","sourceRoot":"","sources":["../../src/basic/field-value.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,MAAM,OAAgB,gBAAgB;IAGlC,6DAA6D;IAC7C,UAAU,CAAc;IAExC,kDAAkD;IAClC,OAAO,CAA0B;IAEjD,4CAA4C;IAC5B,MAAM,CAAc;IAE1B,KAAK,CAAwB;IAEvC,YACI,UAAuB,EACvB,OAAgC,EAChC,MAAmB,EACnB,KAA4B;QAE5B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACI,OAAO;QACV,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;IACrC,CAAC;IAED;;OAEG;IACI,GAAG;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,KAA4B;QACnC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;CASJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/basic/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC"}
|
package/esm/basic/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
1
|
+
export * from './definition.js';
|
|
2
|
+
export * from './field-value.js';
|
|
3
|
+
export * from './options.js';
|
|
4
|
+
export * from './stream.js';
|
|
5
|
+
export * from './struct-value.js';
|
|
5
6
|
//# sourceMappingURL=index.js.map
|
package/esm/basic/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/basic/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/basic/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface StructOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Whether all multi-byte fields in this struct are little-endian encoded.
|
|
4
|
+
*
|
|
5
|
+
* @default false
|
|
6
|
+
*/
|
|
7
|
+
littleEndian: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const StructDefaultOptions: Readonly<StructOptions>;
|
|
10
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../src/basic/options.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC1B;;;;OAIG;IACH,YAAY,EAAE,OAAO,CAAC;CAQzB;AAED,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,aAAa,CAExD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","sourceRoot":"","sources":["../../src/basic/options.ts"],"names":[],"mappings":"AAgBA,MAAM,CAAC,MAAM,oBAAoB,GAA4B;IACzD,YAAY,EAAE,KAAK;CACtB,CAAC"}
|