@yume-chan/struct 0.0.10 → 0.0.14

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.
Files changed (122) hide show
  1. package/CHANGELOG.json +48 -0
  2. package/CHANGELOG.md +29 -1
  3. package/LICENSE +1 -1
  4. package/README.md +102 -53
  5. package/{dts → esm}/basic/definition.d.ts +9 -13
  6. package/esm/basic/definition.d.ts.map +1 -0
  7. package/esm/basic/definition.js +13 -9
  8. package/esm/basic/definition.js.map +1 -1
  9. package/{dts → esm}/basic/field-value.d.ts +8 -8
  10. package/esm/basic/field-value.d.ts.map +1 -0
  11. package/esm/basic/field-value.js +12 -5
  12. package/esm/basic/field-value.js.map +1 -1
  13. package/esm/basic/index.d.ts +6 -0
  14. package/esm/basic/index.d.ts.map +1 -0
  15. package/esm/basic/index.js +5 -4
  16. package/esm/basic/index.js.map +1 -1
  17. package/esm/basic/options.d.ts +10 -0
  18. package/esm/basic/options.d.ts.map +1 -0
  19. package/esm/basic/{context.js → options.js} +1 -1
  20. package/esm/basic/options.js.map +1 -0
  21. package/esm/basic/stream.d.ts +19 -0
  22. package/esm/basic/stream.d.ts.map +1 -0
  23. package/esm/basic/stream.js +2 -0
  24. package/esm/basic/stream.js.map +1 -0
  25. package/{dts → esm}/basic/struct-value.d.ts +3 -3
  26. package/esm/basic/struct-value.d.ts.map +1 -0
  27. package/esm/basic/struct-value.js +7 -9
  28. package/esm/basic/struct-value.js.map +1 -1
  29. package/esm/index.d.ts +14 -0
  30. package/esm/index.d.ts.map +1 -0
  31. package/esm/index.js +5 -5
  32. package/esm/index.js.map +1 -1
  33. package/{dts → esm}/struct.d.ts +16 -12
  34. package/esm/struct.d.ts.map +1 -0
  35. package/esm/struct.js +67 -54
  36. package/esm/struct.js.map +1 -1
  37. package/esm/syncbird.d.ts +65 -0
  38. package/esm/syncbird.d.ts.map +1 -0
  39. package/esm/syncbird.js +17 -4
  40. package/esm/syncbird.js.map +1 -1
  41. package/esm/types/bigint.d.ts +25 -0
  42. package/esm/types/bigint.d.ts.map +1 -0
  43. package/esm/types/bigint.js +45 -0
  44. package/esm/types/bigint.js.map +1 -0
  45. package/esm/types/buffer/base.d.ts +63 -0
  46. package/esm/types/buffer/base.d.ts.map +1 -0
  47. package/esm/types/buffer/base.js +100 -0
  48. package/esm/types/buffer/base.js.map +1 -0
  49. package/esm/types/buffer/fixed-length.d.ts +8 -0
  50. package/esm/types/buffer/fixed-length.d.ts.map +1 -0
  51. package/esm/types/buffer/fixed-length.js +8 -0
  52. package/esm/types/buffer/fixed-length.js.map +1 -0
  53. package/esm/types/buffer/index.d.ts +4 -0
  54. package/esm/types/buffer/index.d.ts.map +1 -0
  55. package/esm/types/buffer/index.js +4 -0
  56. package/esm/types/buffer/index.js.map +1 -0
  57. package/esm/types/buffer/variable-length.d.ts +43 -0
  58. package/esm/types/buffer/variable-length.d.ts.map +1 -0
  59. package/esm/types/buffer/variable-length.js +76 -0
  60. package/esm/types/buffer/variable-length.js.map +1 -0
  61. package/esm/types/index.d.ts +4 -0
  62. package/esm/types/index.d.ts.map +1 -0
  63. package/esm/types/index.js +3 -4
  64. package/esm/types/index.js.map +1 -1
  65. package/{dts → esm}/types/number.d.ts +10 -12
  66. package/esm/types/number.d.ts.map +1 -0
  67. package/esm/types/number.js +21 -14
  68. package/esm/types/number.js.map +1 -1
  69. package/{dts → esm}/utils.d.ts +2 -2
  70. package/{dts → esm}/utils.d.ts.map +1 -1
  71. package/esm/utils.js +9 -3
  72. package/esm/utils.js.map +1 -1
  73. package/package.json +47 -47
  74. package/src/basic/definition.ts +11 -13
  75. package/src/basic/field-value.ts +8 -8
  76. package/src/basic/index.ts +5 -4
  77. package/src/basic/options.ts +19 -0
  78. package/src/basic/stream.ts +19 -0
  79. package/src/basic/struct-value.ts +4 -4
  80. package/src/index.ts +17 -5
  81. package/src/struct.ts +89 -75
  82. package/src/syncbird.ts +88 -10
  83. package/src/types/bigint.ts +102 -0
  84. package/src/types/buffer/base.ts +176 -0
  85. package/src/types/buffer/fixed-length.ts +17 -0
  86. package/src/types/buffer/index.ts +3 -0
  87. package/src/types/buffer/variable-length.ts +156 -0
  88. package/src/types/index.ts +3 -4
  89. package/src/types/number.ts +27 -26
  90. package/src/utils.ts +13 -5
  91. package/dts/basic/context.d.ts +0 -28
  92. package/dts/basic/context.d.ts.map +0 -1
  93. package/dts/basic/definition.d.ts.map +0 -1
  94. package/dts/basic/field-value.d.ts.map +0 -1
  95. package/dts/basic/index.d.ts +0 -5
  96. package/dts/basic/index.d.ts.map +0 -1
  97. package/dts/basic/struct-value.d.ts.map +0 -1
  98. package/dts/index.d.ts +0 -6
  99. package/dts/index.d.ts.map +0 -1
  100. package/dts/struct.d.ts.map +0 -1
  101. package/dts/syncbird.d.ts +0 -20
  102. package/dts/syncbird.d.ts.map +0 -1
  103. package/dts/types/array-buffer.d.ts +0 -71
  104. package/dts/types/array-buffer.d.ts.map +0 -1
  105. package/dts/types/fixed-length-array-buffer.d.ts +0 -8
  106. package/dts/types/fixed-length-array-buffer.d.ts.map +0 -1
  107. package/dts/types/index.d.ts +0 -5
  108. package/dts/types/index.d.ts.map +0 -1
  109. package/dts/types/number.d.ts.map +0 -1
  110. package/dts/types/variable-length-array-buffer.d.ts +0 -32
  111. package/dts/types/variable-length-array-buffer.d.ts.map +0 -1
  112. package/esm/basic/context.js.map +0 -1
  113. package/esm/types/array-buffer.js +0 -110
  114. package/esm/types/array-buffer.js.map +0 -1
  115. package/esm/types/fixed-length-array-buffer.js +0 -8
  116. package/esm/types/fixed-length-array-buffer.js.map +0 -1
  117. package/esm/types/variable-length-array-buffer.js +0 -71
  118. package/esm/types/variable-length-array-buffer.js.map +0 -1
  119. package/src/basic/context.ts +0 -32
  120. package/src/types/array-buffer.ts +0 -194
  121. package/src/types/fixed-length-array-buffer.ts +0 -17
  122. package/src/types/variable-length-array-buffer.ts +0 -142
package/CHANGELOG.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@yume-chan/struct",
3
3
  "entries": [
4
+ {
5
+ "version": "0.0.14",
6
+ "tag": "@yume-chan/struct_v0.0.14",
7
+ "date": "Sat, 30 Apr 2022 14:05:48 GMT",
8
+ "comments": {}
9
+ },
10
+ {
11
+ "version": "0.0.13",
12
+ "tag": "@yume-chan/struct_v0.0.13",
13
+ "date": "Thu, 28 Apr 2022 01:23:53 GMT",
14
+ "comments": {
15
+ "none": [
16
+ {
17
+ "comment": "Fix an issue that `uint64` still deserialize to negative numbers"
18
+ },
19
+ {
20
+ "comment": "Fix an issue where `Syncbird` can't synchronously invoke `then` on some Bluebird internal methods (for example `reduce`)"
21
+ }
22
+ ]
23
+ }
24
+ },
25
+ {
26
+ "version": "0.0.12",
27
+ "tag": "@yume-chan/struct_v0.0.12",
28
+ "date": "Sun, 03 Apr 2022 11:18:47 GMT",
29
+ "comments": {}
30
+ },
31
+ {
32
+ "version": "0.0.11",
33
+ "tag": "@yume-chan/struct_v0.0.11",
34
+ "date": "Sun, 03 Apr 2022 11:18:11 GMT",
35
+ "comments": {
36
+ "none": [
37
+ {
38
+ "comment": "Update to use Web Streams API"
39
+ },
40
+ {
41
+ "comment": "Improve compatibility with Node.js 12 ESM format"
42
+ },
43
+ {
44
+ "comment": "Update compatibility matrix"
45
+ },
46
+ {
47
+ "comment": "Update license year"
48
+ }
49
+ ]
50
+ }
51
+ },
4
52
  {
5
53
  "version": "0.0.10",
6
54
  "tag": "@yume-chan/struct_v0.0.10",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,34 @@
1
1
  # Change Log - @yume-chan/struct
2
2
 
3
- This log was last generated on Sun, 09 Jan 2022 15:52:20 GMT and should not be manually modified.
3
+ This log was last generated on Sat, 30 Apr 2022 14:05:48 GMT and should not be manually modified.
4
+
5
+ ## 0.0.14
6
+ Sat, 30 Apr 2022 14:05:48 GMT
7
+
8
+ _Version update only_
9
+
10
+ ## 0.0.13
11
+ Thu, 28 Apr 2022 01:23:53 GMT
12
+
13
+ ### Updates
14
+
15
+ - Fix an issue that `uint64` still deserialize to negative numbers
16
+ - Fix an issue where `Syncbird` can't synchronously invoke `then` on some Bluebird internal methods (for example `reduce`)
17
+
18
+ ## 0.0.12
19
+ Sun, 03 Apr 2022 11:18:47 GMT
20
+
21
+ _Version update only_
22
+
23
+ ## 0.0.11
24
+ Sun, 03 Apr 2022 11:18:11 GMT
25
+
26
+ ### Updates
27
+
28
+ - Update to use Web Streams API
29
+ - Improve compatibility with Node.js 12 ESM format
30
+ - Update compatibility matrix
31
+ - Update license year
4
32
 
5
33
  ## 0.0.10
6
34
  Sun, 09 Jan 2022 15:52:20 GMT
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 Simon Chan
3
+ Copyright (c) 2020-2022 Simon Chan
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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
  ![license](https://img.shields.io/npm/l/@yume-chan/struct)
4
9
  ![npm type definitions](https://img.shields.io/npm/types/@yume-chan/struct)
5
10
  [![npm version](https://img.shields.io/npm/v/@yume-chan/struct)](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.length`
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
- - [`arraybuffer`/`uint8ClampedArray`/`string`](#arraybufferuint8clampedarraystring)
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
- | | Chrome | Edge | Firefox | Internet Explorer | Safari | Node.js |
74
- | ---------------------------------------------------- | ------ | ---- | ------- | ----------------- | -------------- | ------- |
75
- | [`Promise`][MDN_Promise] | 32 | 12 | 29 | No<sup>1</sup> | 8 | 0.12 |
76
- | [`ArrayBuffer`][MDN_ArrayBuffer] | 7 | 12 | 4 | 10 | 5.1 | 0.10 |
77
- | [`Uint8Array`][MDN_Uint8Array] | 7 | 12 | 4 | 10 | 5.1 | 0.10 |
78
- | [`DataView`][MDN_DataView] | 9 | 12 | 15 | 10 | 5.1 | 0.10 |
79
- | **Basic usage** | 32 | 12 | 29 | 10<sup>1</sup> | 8 | 0.12 |
80
- | [`BigInt`][MDN_BigInt] | 67 | 79 | 68 | No | 14 | 10.4 |
81
- | [`DataView#getBigUint64`][MDN_DataView_getBigUint64] | 67 | 79 | 68 | No | No<sup>2</sup> | 10.4 |
82
- | [`DataView#setBigUint64`][MDN_DataView_setBigUint64] | 67 | 79 | 68 | No | No<sup>2</sup> | 10.4 |
83
- | **Use [`int64/uint64`](#int64uint64) API** | 67 | 79 | 68 | No | 14<sup>2</sup> | 10.4 |
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
- <sup>1</sup> Requires a polyfill for Promise (e.g. [promise-polyfill](https://www.npmjs.com/package/promise-polyfill))
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>2</sup> Requires a polyfill for `DataView#getBigUint64` and `DataView#setBigUint64`
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
- [MDN_DataView_getBigUint64]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getBigUint64
95
- [MDN_DataView_setBigUint64]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setBigUint64
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 declaration.
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 arrays, the length field can be calculate from the array field, so they doesn't need to be provided explicitly.
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 have a [literal type](https://www.typescriptlang.org/docs/handbook/literal-types.html).
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
- #### `arraybuffer`/`uint8ClampedArray`/`string`
293
+ #### `uint8Array`/`string`
267
294
 
268
295
  ```ts
269
- arraybuffer<
296
+ uint8Array<
270
297
  TName extends string | number | symbol,
271
298
  TTypeScriptType = ArrayBuffer
272
299
  >(
273
300
  name: TName,
274
- options: FixedLengthArrayBufferLikeFieldOptions,
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
- arraybuffer<
310
+ uint8Array<
284
311
  TName extends string | number | symbol,
285
- TOptions extends VariableLengthArrayBufferLikeFieldOptions<TFields>,
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 | TOptions['lengthField'],
321
+ TOmitInitKey | TLengthField,
294
322
  TExtra,
295
323
  TPostDeserialized
296
324
  >;
297
325
  ```
298
326
 
299
- Appends an `ArrayBuffer`/`Uint8ClampedArray`/`string` field to the `Struct`.
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. And when serializing, it will write its length to that field.
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 deserialization callback.
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
- * Stream must return exactly `length` bytes or data. If that's not possible
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): ArrayBuffer;
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 Struct value from `stream`.
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
- init: Omit<TFields, TOmitInitKey>
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 Struct value into an `ArrayBuffer`.
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
- This library supports adding fields of user defined types.
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
- Actually, all built-in field type methods are aliases of `field`. For example, calling
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 `StructFieldDefinition` describes type, size and runtime semantics of a field.
670
+ A field definition defines how to deserialize a field.
623
671
 
624
- It's an `abstract` class, means it lacks some method implementations, so it shouldn't be constructed.
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 are used to provide type information to TypeScript. Their values will always be `undefined`, but having correct types is enough. You don't need to care about them.
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`. Can also return a `Promise`.
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. Somehow parse the value from `stream`
674
- 2. Pass the value into its `create` method
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, some 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.
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
- To define a custom type, one must create their own `StructFieldValue` type to define the runtime semantics.
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 store its value in `value` field.
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, StructOptions } from './context';
2
- import type { StructFieldValue } from './field-value';
3
- import type { StructValue } from './struct-value';
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 is a bridge between its type and its runtime value.
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, creates a `StructFieldValue` by parsing `context`.
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"}
@@ -1,19 +1,23 @@
1
+ // cspell: ignore Syncbird
1
2
  /**
2
- * A field definition is a bridge between its type and its runtime value.
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":"AAIA;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAgB,qBAAqB;IAmBvC,YAAmB,OAAiB;QAChC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;CA+BJ"}
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 { StructOptions } from './context';
2
- import type { StructFieldDefinition } from './definition';
3
- import type { StructValue } from './struct-value';
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
- * Field runtime value manages one field of one `Struct` instance.
5
+ * A field value defines how to serialize a field.
6
6
  *
7
- * If one `StructFieldDefinition` needs to change other field's semantics
8
- * It can override other fields' `StructFieldValue` in its own `StructFieldValue`'s constructor
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, returns the current value of this field
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, update the current value of this field
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"}
@@ -1,10 +1,17 @@
1
1
  /**
2
- * Field runtime value manages one field of one `Struct` instance.
2
+ * A field value defines how to serialize a field.
3
3
  *
4
- * If one `StructFieldDefinition` needs to change other field's semantics
5
- * It can override other fields' `StructFieldValue` in its own `StructFieldValue`'s constructor
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, returns the current value of this field
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, update the current value of this field
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;IAclC,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"}
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,6 @@
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';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -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"}
@@ -1,5 +1,6 @@
1
- export * from './context';
2
- export * from './definition';
3
- export * from './field-value';
4
- export * from './struct-value';
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
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/basic/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,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