compact-encoding 2.10.0 → 2.12.0
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/.github/workflows/test-node.yml +2 -2
- package/LICENSE +201 -21
- package/NOTICE +13 -0
- package/README.md +20 -2
- package/endian.js +3 -0
- package/index.js +140 -24
- package/package.json +3 -3
- package/raw.js +148 -0
- package/test.js +114 -24
package/LICENSE
CHANGED
|
@@ -1,21 +1,201 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023 Holepunch Inc
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -75,6 +75,7 @@ const bool = cenc.decode(cenc.bool, buf)
|
|
|
75
75
|
The following encodings are bundled as they are primitives that can be used
|
|
76
76
|
to build others on top. Feel free to PR more that are missing.
|
|
77
77
|
|
|
78
|
+
* `cenc.raw` - Pass through encodes a buffer, i.e. a basic copy.
|
|
78
79
|
* `cenc.uint` - Encodes a uint using [compact-uint](https://github.com/mafintosh/compact-uint).
|
|
79
80
|
* `cenc.uint8` - Encodes a fixed size uint8.
|
|
80
81
|
* `cenc.uint16` - Encodes a fixed size uint16. Useful for things like ports.
|
|
@@ -97,34 +98,51 @@ to build others on top. Feel free to PR more that are missing.
|
|
|
97
98
|
* `cenc.float32` - Encodes a fixed size float32.
|
|
98
99
|
* `cenc.float64` - Encodes a fixed size float64.
|
|
99
100
|
* `cenc.buffer` - Encodes a buffer with its length uint prefixed. When decoding an empty buffer, `null` is returned.
|
|
100
|
-
* `cenc.raw` -
|
|
101
|
+
* `cenc.raw.buffer` - Encodes a buffer without a length prefixed.
|
|
101
102
|
* `cenc.uint8array` - Encodes a uint8array with its element length uint prefixed.
|
|
103
|
+
* `cenc.raw.uint8array` - Encodes a uint8array without a length prefixed.
|
|
102
104
|
* `cenc.uint16array` - Encodes a uint16array with its element length uint prefixed.
|
|
105
|
+
* `cenc.raw.uint16array` - Encodes a uint16array without a length prefixed.
|
|
103
106
|
* `cenc.uint32array` - Encodes a uint32array with its element length uint prefixed.
|
|
107
|
+
* `cenc.raw.uint32array` - Encodes a uint32array without a length prefixed.
|
|
104
108
|
* `cenc.int8array` - Encodes a int8array with its element length uint prefixed.
|
|
109
|
+
* `cenc.raw.int8array` - Encodes a int8array without a length prefixed.
|
|
105
110
|
* `cenc.int16array` - Encodes a int16array with its element length uint prefixed.
|
|
111
|
+
* `cenc.raw.int16array` - Encodes a int16array without a length prefixed.
|
|
106
112
|
* `cenc.int32array` - Encodes a int32array with its element length uint prefixed.
|
|
113
|
+
* `cenc.raw.int32array` - Encodes a int32array without a length prefixed.
|
|
107
114
|
* `cenc.float32array` - Encodes a float32array with its element length uint prefixed.
|
|
115
|
+
* `cenc.raw.float32array` - Encodes a float32array without a length prefixed.
|
|
108
116
|
* `cenc.float64array` - Encodes a float64array with its element length uint prefixed.
|
|
117
|
+
* `cenc.raw.float64array` - Encodes a float64array without a length prefixed.
|
|
109
118
|
* `cenc.bool` - Encodes a boolean as 1 or 0.
|
|
110
119
|
* `cenc.string`, `cenc.utf8` - Encodes a utf-8 string, similar to buffer.
|
|
120
|
+
* `cenc.raw.string`, `cenc.raw.utf8` - Encodes a utf-8 string without a length prefixed.
|
|
111
121
|
* `cenc.string.fixed(n)`, `cenc.utf8.fixed(n)` - Encodes a fixed sized utf-8 string.
|
|
112
122
|
* `cenc.ascii` - Encodes an ascii string.
|
|
123
|
+
* `cenc.raw.ascii` - Encodes an ascii string without a length prefixed.
|
|
113
124
|
* `cenc.ascii.fixed(n)` - Encodes a fixed size ascii string.
|
|
114
125
|
* `cenc.hex` - Encodes a hex string.
|
|
126
|
+
* `cenc.raw.hex` - Encodes a hex string without a length prefixed.
|
|
115
127
|
* `cenc.hex.fixed(n)` - Encodes a fixed size hex string.
|
|
116
128
|
* `cenc.base64` - Encodes a base64 string.
|
|
129
|
+
* `cenc.raw.base64` - Encodes a base64 string without a length prefixed.
|
|
117
130
|
* `cenc.base64.fixed(n)` - Encodes a fixed size base64 string.
|
|
118
131
|
* `cenc.utf16le`, `cenc.ucs2` - Encodes a utf16le string.
|
|
132
|
+
* `cenc.raw.utf16le`, `cenc.raw.ucs2` - Encodes a utf16le string without a length prefixed.
|
|
119
133
|
* `cenc.utf16le.fixed(n)`, `cenc.ucs2.fixed(n)` - Encodes a fixed size utf16le string.
|
|
120
134
|
* `cenc.fixed32` - Encodes a fixed 32 byte buffer.
|
|
121
135
|
* `cenc.fixed64` - Encodes a fixed 64 byte buffer.
|
|
122
136
|
* `cenc.fixed(n)` - Makes a fixed sized encoder.
|
|
123
137
|
* `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
|
|
138
|
+
* `cenc.raw.array(enc)` - Makes an array encoder from another encoder, without a length prefixed.
|
|
124
139
|
* `cenc.json` - Encodes a JSON value as utf-8.
|
|
140
|
+
* `cenc.raw.json` - Encodes a JSON value as utf-8 without a length prefixed.
|
|
125
141
|
* `cenc.ndjson` - Encodes a JSON value as newline delimited utf-8.
|
|
142
|
+
* `cenc.raw.ndjson` - Encodes a JSON value as newline delimited utf-8 without a length prefixed.
|
|
143
|
+
* `cenc.any` - Encodes any JSON representable value into a self described buffer. Like JSON + buffer, but using compact types. Useful for schemaless codecs.
|
|
126
144
|
* `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding).
|
|
127
145
|
|
|
128
146
|
## License
|
|
129
147
|
|
|
130
|
-
|
|
148
|
+
Apache 2.0
|
package/endian.js
ADDED
package/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
const b4a = require('b4a')
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const BE = !LE
|
|
3
|
+
const { BE } = require('./endian')
|
|
5
4
|
|
|
6
5
|
exports.state = function (start = 0, end = 0, buffer = null) {
|
|
7
6
|
return { start, end, buffer, cache: null }
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
const raw = exports.raw = require('./raw')
|
|
10
|
+
|
|
10
11
|
const uint = exports.uint = {
|
|
11
12
|
preencode (state, n) {
|
|
12
13
|
state.end += n <= 0xfc ? 1 : n <= 0xffff ? 3 : n <= 0xffffffff ? 5 : 9
|
|
@@ -211,7 +212,7 @@ exports.float64 = {
|
|
|
211
212
|
}
|
|
212
213
|
}
|
|
213
214
|
|
|
214
|
-
exports.buffer = {
|
|
215
|
+
const buffer = exports.buffer = {
|
|
215
216
|
preencode (state, b) {
|
|
216
217
|
if (b) uint8array.preencode(state, b)
|
|
217
218
|
else state.end++
|
|
@@ -228,18 +229,15 @@ exports.buffer = {
|
|
|
228
229
|
}
|
|
229
230
|
}
|
|
230
231
|
|
|
231
|
-
|
|
232
|
+
exports.binary = {
|
|
233
|
+
...buffer,
|
|
232
234
|
preencode (state, b) {
|
|
233
|
-
|
|
235
|
+
if (typeof b === 'string') utf8.preencode(state, b)
|
|
236
|
+
else buffer.preencode(state, b)
|
|
234
237
|
},
|
|
235
238
|
encode (state, b) {
|
|
236
|
-
|
|
237
|
-
state
|
|
238
|
-
},
|
|
239
|
-
decode (state) {
|
|
240
|
-
const b = state.buffer.subarray(state.start, state.end)
|
|
241
|
-
state.start = state.end
|
|
242
|
-
return b
|
|
239
|
+
if (typeof b === 'string') utf8.encode(state, b)
|
|
240
|
+
else buffer.encode(state, b)
|
|
243
241
|
}
|
|
244
242
|
}
|
|
245
243
|
|
|
@@ -360,18 +358,6 @@ const fixed = exports.fixed = function fixed (n) {
|
|
|
360
358
|
exports.fixed32 = fixed(32)
|
|
361
359
|
exports.fixed64 = fixed(64)
|
|
362
360
|
|
|
363
|
-
exports.none = {
|
|
364
|
-
preencode (state, m) {
|
|
365
|
-
// do nothing
|
|
366
|
-
},
|
|
367
|
-
encode (state, m) {
|
|
368
|
-
// do nothing
|
|
369
|
-
},
|
|
370
|
-
decode (state) {
|
|
371
|
-
return null
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
361
|
exports.array = function array (enc) {
|
|
376
362
|
return {
|
|
377
363
|
preencode (state, list) {
|
|
@@ -416,12 +402,142 @@ exports.ndjson = {
|
|
|
416
402
|
}
|
|
417
403
|
}
|
|
418
404
|
|
|
405
|
+
// simple helper for when you want to just express nothing
|
|
406
|
+
exports.none = {
|
|
407
|
+
preencode (state, n) {
|
|
408
|
+
// do nothing
|
|
409
|
+
},
|
|
410
|
+
encode (state, n) {
|
|
411
|
+
// do nothing
|
|
412
|
+
},
|
|
413
|
+
decode (state) {
|
|
414
|
+
return null
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// "any" encoders here for helping just structure any object without schematising it
|
|
419
|
+
|
|
420
|
+
const anyArray = {
|
|
421
|
+
preencode (state, arr) {
|
|
422
|
+
uint.preencode(state, arr.length)
|
|
423
|
+
for (let i = 0; i < arr.length; i++) {
|
|
424
|
+
any.preencode(state, arr[i])
|
|
425
|
+
}
|
|
426
|
+
},
|
|
427
|
+
encode (state, arr) {
|
|
428
|
+
uint.encode(state, arr.length)
|
|
429
|
+
for (let i = 0; i < arr.length; i++) {
|
|
430
|
+
any.encode(state, arr[i])
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
decode (state) {
|
|
434
|
+
const arr = []
|
|
435
|
+
let len = uint.decode(state)
|
|
436
|
+
while (len-- > 0) {
|
|
437
|
+
arr.push(any.decode(state))
|
|
438
|
+
}
|
|
439
|
+
return arr
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const anyObject = {
|
|
444
|
+
preencode (state, o) {
|
|
445
|
+
const keys = Object.keys(o)
|
|
446
|
+
uint.preencode(state, keys.length)
|
|
447
|
+
for (const key of keys) {
|
|
448
|
+
utf8.preencode(state, key)
|
|
449
|
+
any.preencode(state, o[key])
|
|
450
|
+
}
|
|
451
|
+
},
|
|
452
|
+
encode (state, o) {
|
|
453
|
+
const keys = Object.keys(o)
|
|
454
|
+
uint.encode(state, keys.length)
|
|
455
|
+
for (const key of keys) {
|
|
456
|
+
utf8.encode(state, key)
|
|
457
|
+
any.encode(state, o[key])
|
|
458
|
+
}
|
|
459
|
+
},
|
|
460
|
+
decode (state) {
|
|
461
|
+
let len = uint.decode(state)
|
|
462
|
+
const o = {}
|
|
463
|
+
while (len-- > 0) {
|
|
464
|
+
const key = utf8.decode(state)
|
|
465
|
+
o[key] = any.decode(state)
|
|
466
|
+
}
|
|
467
|
+
return o
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const anyTypes = [
|
|
472
|
+
exports.none,
|
|
473
|
+
exports.bool,
|
|
474
|
+
exports.string,
|
|
475
|
+
exports.buffer,
|
|
476
|
+
exports.uint,
|
|
477
|
+
exports.int,
|
|
478
|
+
exports.float64,
|
|
479
|
+
anyArray,
|
|
480
|
+
anyObject
|
|
481
|
+
]
|
|
482
|
+
|
|
483
|
+
const any = exports.any = {
|
|
484
|
+
preencode (state, o) {
|
|
485
|
+
const t = getType(o)
|
|
486
|
+
uint.preencode(state, t)
|
|
487
|
+
anyTypes[t].preencode(state, o)
|
|
488
|
+
},
|
|
489
|
+
encode (state, o) {
|
|
490
|
+
const t = getType(o)
|
|
491
|
+
uint.encode(state, t)
|
|
492
|
+
anyTypes[t].encode(state, o)
|
|
493
|
+
},
|
|
494
|
+
decode (state) {
|
|
495
|
+
const t = uint.decode(state)
|
|
496
|
+
if (t >= anyTypes.length) throw new Error('Unknown type: ' + t)
|
|
497
|
+
return anyTypes[t].decode(state)
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function getType (o) {
|
|
502
|
+
if (o === null || o === undefined) return 0
|
|
503
|
+
if (typeof o === 'boolean') return 1
|
|
504
|
+
if (typeof o === 'string') return 2
|
|
505
|
+
if (b4a.isBuffer(o)) return 3
|
|
506
|
+
if (typeof o === 'number') {
|
|
507
|
+
if (Number.isInteger(o)) return o >= 0 ? 4 : 5
|
|
508
|
+
return 6
|
|
509
|
+
}
|
|
510
|
+
if (Array.isArray(o)) return 7
|
|
511
|
+
if (typeof o === 'object') return 8
|
|
512
|
+
|
|
513
|
+
throw new Error('Unsupported type for ' + o)
|
|
514
|
+
}
|
|
515
|
+
|
|
419
516
|
exports.from = function from (enc) {
|
|
517
|
+
if (typeof enc === 'string') return fromNamed(enc)
|
|
420
518
|
if (enc.preencode) return enc
|
|
421
519
|
if (enc.encodingLength) return fromAbstractEncoder(enc)
|
|
422
520
|
return fromCodec(enc)
|
|
423
521
|
}
|
|
424
522
|
|
|
523
|
+
function fromNamed (enc) {
|
|
524
|
+
switch (enc) {
|
|
525
|
+
case 'ascii': return raw.ascii
|
|
526
|
+
case 'utf-8':
|
|
527
|
+
case 'utf8': return raw.utf8
|
|
528
|
+
case 'hex': return raw.hex
|
|
529
|
+
case 'base64': return raw.base64
|
|
530
|
+
case 'utf16-le':
|
|
531
|
+
case 'utf16le':
|
|
532
|
+
case 'ucs-2':
|
|
533
|
+
case 'ucs2': return raw.ucs2
|
|
534
|
+
case 'ndjson': return raw.ndjson
|
|
535
|
+
case 'json': return raw.json
|
|
536
|
+
case 'binary':
|
|
537
|
+
default: return raw.binary
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
425
541
|
function fromCodec (enc) {
|
|
426
542
|
let tmpM = null
|
|
427
543
|
let tmpBuf = null
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compact-encoding",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.12.0",
|
|
4
4
|
"description": "A series of compact encoding schemes for building small and fast parsers and serializers",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"b4a": "^1.3.0"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
|
-
"brittle": "^
|
|
10
|
+
"brittle": "^3.0.0",
|
|
11
11
|
"standard": "^16.0.3"
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"url": "https://github.com/compact-encoding/compact-encoding.git"
|
|
19
19
|
},
|
|
20
20
|
"author": "Mathias Buus (@mafintosh)",
|
|
21
|
-
"license": "
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
22
|
"bugs": {
|
|
23
23
|
"url": "https://github.com/compact-encoding/compact-encoding/issues"
|
|
24
24
|
},
|
package/raw.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
const b4a = require('b4a')
|
|
2
|
+
|
|
3
|
+
const { BE } = require('./endian')
|
|
4
|
+
|
|
5
|
+
exports = module.exports = {
|
|
6
|
+
preencode (state, b) {
|
|
7
|
+
state.end += b.byteLength
|
|
8
|
+
},
|
|
9
|
+
encode (state, b) {
|
|
10
|
+
state.buffer.set(b, state.start)
|
|
11
|
+
state.start += b.byteLength
|
|
12
|
+
},
|
|
13
|
+
decode (state) {
|
|
14
|
+
const b = state.buffer.subarray(state.start, state.end)
|
|
15
|
+
state.start = state.end
|
|
16
|
+
return b
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const buffer = exports.buffer = {
|
|
21
|
+
preencode (state, b) {
|
|
22
|
+
if (b) uint8array.preencode(state, b)
|
|
23
|
+
else state.end++
|
|
24
|
+
},
|
|
25
|
+
encode (state, b) {
|
|
26
|
+
if (b) uint8array.encode(state, b)
|
|
27
|
+
else state.buffer[state.start++] = 0
|
|
28
|
+
},
|
|
29
|
+
decode (state) {
|
|
30
|
+
const b = state.buffer.subarray(state.start)
|
|
31
|
+
if (b.byteLength === 0) return null
|
|
32
|
+
state.start = state.end
|
|
33
|
+
return b
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
exports.binary = {
|
|
38
|
+
...buffer,
|
|
39
|
+
preencode (state, b) {
|
|
40
|
+
if (typeof b === 'string') utf8.preencode(state, b)
|
|
41
|
+
else buffer.preencode(state, b)
|
|
42
|
+
},
|
|
43
|
+
encode (state, b) {
|
|
44
|
+
if (typeof b === 'string') utf8.encode(state, b)
|
|
45
|
+
else buffer.encode(state, b)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function typedarray (TypedArray, swap) {
|
|
50
|
+
const n = TypedArray.BYTES_PER_ELEMENT
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
preencode (state, b) {
|
|
54
|
+
state.end += b.byteLength
|
|
55
|
+
},
|
|
56
|
+
encode (state, b) {
|
|
57
|
+
const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
|
|
58
|
+
|
|
59
|
+
if (BE && swap) swap(view)
|
|
60
|
+
|
|
61
|
+
state.buffer.set(view, state.start)
|
|
62
|
+
state.start += b.byteLength
|
|
63
|
+
},
|
|
64
|
+
decode (state) {
|
|
65
|
+
let b = state.buffer.subarray(state.start)
|
|
66
|
+
if ((b.byteOffset % n) !== 0) b = new Uint8Array(b)
|
|
67
|
+
|
|
68
|
+
if (BE && swap) swap(b)
|
|
69
|
+
|
|
70
|
+
state.start = state.end
|
|
71
|
+
|
|
72
|
+
return new TypedArray(b.buffer, b.byteOffset, b.byteLength / n)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const uint8array = exports.uint8array = typedarray(Uint8Array)
|
|
78
|
+
exports.uint16array = typedarray(Uint16Array, b4a.swap16)
|
|
79
|
+
exports.uint32array = typedarray(Uint32Array, b4a.swap32)
|
|
80
|
+
|
|
81
|
+
exports.int8array = typedarray(Int8Array)
|
|
82
|
+
exports.int16array = typedarray(Int16Array, b4a.swap16)
|
|
83
|
+
exports.int32array = typedarray(Int32Array, b4a.swap32)
|
|
84
|
+
|
|
85
|
+
exports.float32array = typedarray(Float32Array, b4a.swap32)
|
|
86
|
+
exports.float64array = typedarray(Float64Array, b4a.swap64)
|
|
87
|
+
|
|
88
|
+
function string (encoding) {
|
|
89
|
+
return {
|
|
90
|
+
preencode (state, s) {
|
|
91
|
+
state.end += b4a.byteLength(s, encoding)
|
|
92
|
+
},
|
|
93
|
+
encode (state, s) {
|
|
94
|
+
state.start += b4a.write(state.buffer, s, state.start, encoding)
|
|
95
|
+
},
|
|
96
|
+
decode (state) {
|
|
97
|
+
const s = b4a.toString(state.buffer, encoding, state.start)
|
|
98
|
+
state.start = state.end
|
|
99
|
+
return s
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const utf8 = exports.string = exports.utf8 = string('utf-8')
|
|
105
|
+
exports.ascii = string('ascii')
|
|
106
|
+
exports.hex = string('hex')
|
|
107
|
+
exports.base64 = string('base64')
|
|
108
|
+
exports.ucs2 = exports.utf16le = string('utf16le')
|
|
109
|
+
|
|
110
|
+
exports.array = function array (enc) {
|
|
111
|
+
return {
|
|
112
|
+
preencode (state, list) {
|
|
113
|
+
for (const value of list) enc.preencode(state, value)
|
|
114
|
+
},
|
|
115
|
+
encode (state, list) {
|
|
116
|
+
for (const value of list) enc.encode(state, value)
|
|
117
|
+
},
|
|
118
|
+
decode (state) {
|
|
119
|
+
const arr = []
|
|
120
|
+
while (state.start < state.end) arr.push(enc.decode(state))
|
|
121
|
+
return arr
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
exports.json = {
|
|
127
|
+
preencode (state, v) {
|
|
128
|
+
utf8.preencode(state, JSON.stringify(v))
|
|
129
|
+
},
|
|
130
|
+
encode (state, v) {
|
|
131
|
+
utf8.encode(state, JSON.stringify(v))
|
|
132
|
+
},
|
|
133
|
+
decode (state) {
|
|
134
|
+
return JSON.parse(utf8.decode(state))
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
exports.ndjson = {
|
|
139
|
+
preencode (state, v) {
|
|
140
|
+
utf8.preencode(state, JSON.stringify(v) + '\n')
|
|
141
|
+
},
|
|
142
|
+
encode (state, v) {
|
|
143
|
+
utf8.encode(state, JSON.stringify(v) + '\n')
|
|
144
|
+
},
|
|
145
|
+
decode (state) {
|
|
146
|
+
return JSON.parse(utf8.decode(state))
|
|
147
|
+
}
|
|
148
|
+
}
|
package/test.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const enc = require('./')
|
|
2
|
-
const
|
|
2
|
+
const test = require('brittle')
|
|
3
3
|
const b4a = require('b4a')
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
test('uint', function (t) {
|
|
6
6
|
const state = enc.state()
|
|
7
7
|
|
|
8
8
|
enc.uint.preencode(state, 42)
|
|
@@ -29,7 +29,7 @@ tape('uint', function (t) {
|
|
|
29
29
|
t.exception(() => enc.uint.decode(state))
|
|
30
30
|
})
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
test('int', function (t) {
|
|
33
33
|
const state = enc.state()
|
|
34
34
|
|
|
35
35
|
enc.int.preencode(state, 42)
|
|
@@ -51,7 +51,7 @@ tape('int', function (t) {
|
|
|
51
51
|
t.exception(() => enc.int.decode(state))
|
|
52
52
|
})
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
test('float64', function (t) {
|
|
55
55
|
const state = enc.state()
|
|
56
56
|
|
|
57
57
|
enc.float64.preencode(state, 162.2377294)
|
|
@@ -146,7 +146,7 @@ tape('float64', function (t) {
|
|
|
146
146
|
t.is(state.start, state.end)
|
|
147
147
|
})
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
test('buffer', function (t) {
|
|
150
150
|
const state = enc.state()
|
|
151
151
|
|
|
152
152
|
enc.buffer.preencode(state, b4a.from('hi'))
|
|
@@ -173,7 +173,7 @@ tape('buffer', function (t) {
|
|
|
173
173
|
t.exception(() => enc.buffer.decode(state))
|
|
174
174
|
})
|
|
175
175
|
|
|
176
|
-
|
|
176
|
+
test('raw', function (t) {
|
|
177
177
|
const state = enc.state()
|
|
178
178
|
|
|
179
179
|
enc.raw.preencode(state, b4a.from('hi'))
|
|
@@ -188,7 +188,26 @@ tape('raw', function (t) {
|
|
|
188
188
|
t.is(state.start, state.end)
|
|
189
189
|
})
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
test('raw uint8array', function (t) {
|
|
192
|
+
const state = enc.state()
|
|
193
|
+
|
|
194
|
+
enc.raw.uint8array.preencode(state, Uint8Array.of(1, 2))
|
|
195
|
+
t.alike(state, enc.state(0, 2))
|
|
196
|
+
enc.raw.uint8array.preencode(state, Uint8Array.of(3, 4))
|
|
197
|
+
t.alike(state, enc.state(0, 4))
|
|
198
|
+
|
|
199
|
+
state.buffer = b4a.alloc(state.end)
|
|
200
|
+
enc.raw.uint8array.encode(state, Uint8Array.of(1, 2))
|
|
201
|
+
t.alike(state, enc.state(2, 4, b4a.from([1, 2, 0, 0])))
|
|
202
|
+
enc.raw.uint8array.encode(state, Uint8Array.of(3, 4))
|
|
203
|
+
t.alike(state, enc.state(4, 4, b4a.from([1, 2, 3, 4])))
|
|
204
|
+
|
|
205
|
+
state.start = 0
|
|
206
|
+
t.alike(enc.raw.uint8array.decode(state), Uint8Array.of(1, 2, 3, 4))
|
|
207
|
+
t.alike(enc.raw.uint8array.decode(state), Uint8Array.of())
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
test('uint16array', function (t) {
|
|
192
211
|
const state = enc.state()
|
|
193
212
|
|
|
194
213
|
enc.uint16array.preencode(state, new Uint16Array([1, 2, 3]))
|
|
@@ -205,7 +224,7 @@ tape('uint16array', function (t) {
|
|
|
205
224
|
t.exception(() => enc.uint16array.decode(state))
|
|
206
225
|
})
|
|
207
226
|
|
|
208
|
-
|
|
227
|
+
test('uint32array', function (t) {
|
|
209
228
|
const state = enc.state()
|
|
210
229
|
|
|
211
230
|
enc.uint32array.preencode(state, new Uint32Array([1]))
|
|
@@ -227,7 +246,7 @@ tape('uint32array', function (t) {
|
|
|
227
246
|
t.exception(() => enc.uint32array.decode(state))
|
|
228
247
|
})
|
|
229
248
|
|
|
230
|
-
|
|
249
|
+
test('int16array', function (t) {
|
|
231
250
|
const state = enc.state()
|
|
232
251
|
|
|
233
252
|
enc.int16array.preencode(state, new Int16Array([1, -2, 3]))
|
|
@@ -244,7 +263,7 @@ tape('int16array', function (t) {
|
|
|
244
263
|
t.exception(() => enc.int16array.decode(state))
|
|
245
264
|
})
|
|
246
265
|
|
|
247
|
-
|
|
266
|
+
test('int32array', function (t) {
|
|
248
267
|
const state = enc.state()
|
|
249
268
|
|
|
250
269
|
enc.int32array.preencode(state, new Int32Array([1, -2, 3]))
|
|
@@ -261,7 +280,7 @@ tape('int32array', function (t) {
|
|
|
261
280
|
t.exception(() => enc.int32array.decode(state))
|
|
262
281
|
})
|
|
263
282
|
|
|
264
|
-
|
|
283
|
+
test('float32array', function (t) {
|
|
265
284
|
const state = enc.state()
|
|
266
285
|
|
|
267
286
|
enc.float32array.preencode(state, new Float32Array([1.1, -2.2, 3.3]))
|
|
@@ -278,7 +297,7 @@ tape('float32array', function (t) {
|
|
|
278
297
|
t.exception(() => enc.float32array.decode(state))
|
|
279
298
|
})
|
|
280
299
|
|
|
281
|
-
|
|
300
|
+
test('float64array', function (t) {
|
|
282
301
|
const state = enc.state()
|
|
283
302
|
|
|
284
303
|
enc.float64array.preencode(state, new Float64Array([1.1, -2.2, 3.3]))
|
|
@@ -295,7 +314,7 @@ tape('float64array', function (t) {
|
|
|
295
314
|
t.exception(() => enc.float64array.decode(state))
|
|
296
315
|
})
|
|
297
316
|
|
|
298
|
-
|
|
317
|
+
test('string', function (t) {
|
|
299
318
|
const state = enc.state()
|
|
300
319
|
|
|
301
320
|
enc.string.preencode(state, '🌾')
|
|
@@ -317,7 +336,25 @@ tape('string', function (t) {
|
|
|
317
336
|
t.exception(() => enc.string.decode(state))
|
|
318
337
|
})
|
|
319
338
|
|
|
320
|
-
|
|
339
|
+
test('raw string', function (t) {
|
|
340
|
+
const state = enc.state()
|
|
341
|
+
|
|
342
|
+
enc.raw.string.preencode(state, 'hello')
|
|
343
|
+
t.alike(state, enc.state(0, 5))
|
|
344
|
+
enc.raw.string.preencode(state, ' world')
|
|
345
|
+
t.alike(state, enc.state(0, 11))
|
|
346
|
+
|
|
347
|
+
state.buffer = b4a.alloc(state.end)
|
|
348
|
+
enc.raw.string.encode(state, 'hello')
|
|
349
|
+
enc.raw.string.encode(state, ' world')
|
|
350
|
+
t.alike(state, enc.state(11, 11, b4a.from('hello world')))
|
|
351
|
+
|
|
352
|
+
state.start = 0
|
|
353
|
+
t.is(enc.raw.string.decode(state), 'hello world')
|
|
354
|
+
t.is(enc.raw.string.decode(state), '')
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
test('fixed32', function (t) {
|
|
321
358
|
const state = enc.state()
|
|
322
359
|
|
|
323
360
|
enc.fixed32.preencode(state, b4a.alloc(32).fill('a'))
|
|
@@ -339,7 +376,7 @@ tape('fixed32', function (t) {
|
|
|
339
376
|
t.exception(() => enc.fixed32.decode(state))
|
|
340
377
|
})
|
|
341
378
|
|
|
342
|
-
|
|
379
|
+
test('fixed64', function (t) {
|
|
343
380
|
const state = enc.state()
|
|
344
381
|
|
|
345
382
|
enc.fixed64.preencode(state, b4a.alloc(64).fill('a'))
|
|
@@ -361,7 +398,7 @@ tape('fixed64', function (t) {
|
|
|
361
398
|
t.exception(() => enc.fixed64.decode(state))
|
|
362
399
|
})
|
|
363
400
|
|
|
364
|
-
|
|
401
|
+
test('fixed n', function (t) {
|
|
365
402
|
const state = enc.state()
|
|
366
403
|
const fixed = enc.fixed(3)
|
|
367
404
|
|
|
@@ -386,7 +423,7 @@ tape('fixed n', function (t) {
|
|
|
386
423
|
t.exception(() => fixed.decode(state))
|
|
387
424
|
})
|
|
388
425
|
|
|
389
|
-
|
|
426
|
+
test('array', function (t) {
|
|
390
427
|
const state = enc.state()
|
|
391
428
|
const arr = enc.array(enc.bool)
|
|
392
429
|
|
|
@@ -409,7 +446,27 @@ tape('array', function (t) {
|
|
|
409
446
|
t.exception(() => arr.decode(state))
|
|
410
447
|
})
|
|
411
448
|
|
|
412
|
-
|
|
449
|
+
test('raw array', function (t) {
|
|
450
|
+
const state = enc.state()
|
|
451
|
+
const arr = enc.raw.array(enc.bool)
|
|
452
|
+
|
|
453
|
+
arr.preencode(state, [true])
|
|
454
|
+
t.alike(state, enc.state(0, 1))
|
|
455
|
+
arr.preencode(state, [true])
|
|
456
|
+
t.alike(state, enc.state(0, 2))
|
|
457
|
+
|
|
458
|
+
state.buffer = b4a.alloc(state.end)
|
|
459
|
+
arr.encode(state, [true])
|
|
460
|
+
t.alike(state, enc.state(1, 2, b4a.from([1, 0])))
|
|
461
|
+
arr.encode(state, [true])
|
|
462
|
+
t.alike(state, enc.state(2, 2, b4a.from([1, 1])))
|
|
463
|
+
|
|
464
|
+
state.start = 0
|
|
465
|
+
t.alike(arr.decode(state), [true, true])
|
|
466
|
+
t.alike(arr.decode(state), [])
|
|
467
|
+
})
|
|
468
|
+
|
|
469
|
+
test('json', function (t) {
|
|
413
470
|
const state = enc.state()
|
|
414
471
|
|
|
415
472
|
enc.json.preencode(state, { a: 1, b: 2 })
|
|
@@ -428,7 +485,23 @@ tape('json', function (t) {
|
|
|
428
485
|
t.exception(() => enc.json.decode(state))
|
|
429
486
|
})
|
|
430
487
|
|
|
431
|
-
|
|
488
|
+
test('raw json', function (t) {
|
|
489
|
+
const state = enc.state()
|
|
490
|
+
|
|
491
|
+
enc.raw.json.preencode(state, { a: 1, b: 2 })
|
|
492
|
+
t.alike(state, enc.state(0, 13))
|
|
493
|
+
|
|
494
|
+
state.buffer = b4a.alloc(state.end)
|
|
495
|
+
enc.raw.json.encode(state, { a: 1, b: 2 })
|
|
496
|
+
t.alike(state, enc.state(13, 13, b4a.from('{"a":1,"b":2}')))
|
|
497
|
+
|
|
498
|
+
state.start = 0
|
|
499
|
+
t.alike(enc.raw.json.decode(state), { a: 1, b: 2 })
|
|
500
|
+
|
|
501
|
+
t.exception(() => enc.json.decode(state))
|
|
502
|
+
})
|
|
503
|
+
|
|
504
|
+
test('lexint: big numbers', function (t) {
|
|
432
505
|
t.plan(1)
|
|
433
506
|
|
|
434
507
|
let prev = enc.encode(enc.lexint, 0)
|
|
@@ -445,7 +518,7 @@ tape('lexint: big numbers', function (t) {
|
|
|
445
518
|
t.is(n, Infinity)
|
|
446
519
|
})
|
|
447
520
|
|
|
448
|
-
|
|
521
|
+
test('lexint: range precision', function (t) {
|
|
449
522
|
t.plan(2)
|
|
450
523
|
const a = 1e55
|
|
451
524
|
const b = 1.0000000000001e55
|
|
@@ -455,7 +528,7 @@ tape('lexint: range precision', function (t) {
|
|
|
455
528
|
t.not(ha, hb)
|
|
456
529
|
})
|
|
457
530
|
|
|
458
|
-
|
|
531
|
+
test('lexint: range precision', function (t) {
|
|
459
532
|
let prev = enc.encode(enc.lexint, 0)
|
|
460
533
|
const skip = 0.000000001e55
|
|
461
534
|
for (let i = 0, n = 1e55; i < 1000; n = 1e55 + skip * ++i) {
|
|
@@ -467,7 +540,7 @@ tape('lexint: range precision', function (t) {
|
|
|
467
540
|
t.end()
|
|
468
541
|
})
|
|
469
542
|
|
|
470
|
-
|
|
543
|
+
test('lexint: small numbers', function (t) {
|
|
471
544
|
let prev = enc.encode(enc.lexint, 0)
|
|
472
545
|
for (let n = 1; n < 256 * 256 * 16; n++) {
|
|
473
546
|
const cur = enc.encode(enc.lexint, n)
|
|
@@ -478,7 +551,7 @@ tape('lexint: small numbers', function (t) {
|
|
|
478
551
|
t.end()
|
|
479
552
|
})
|
|
480
553
|
|
|
481
|
-
|
|
554
|
+
test('lexint: throws', function (t) {
|
|
482
555
|
t.exception(() => {
|
|
483
556
|
enc.decode(enc.lexint, b4a.alloc(1, 251))
|
|
484
557
|
})
|
|
@@ -554,7 +627,7 @@ tape('lexint: throws', function (t) {
|
|
|
554
627
|
t.end()
|
|
555
628
|
})
|
|
556
629
|
|
|
557
|
-
|
|
630
|
+
test('lexint: unpack', function (t) {
|
|
558
631
|
let n
|
|
559
632
|
let skip = 1
|
|
560
633
|
|
|
@@ -581,3 +654,20 @@ tape('lexint: unpack', function (t) {
|
|
|
581
654
|
}
|
|
582
655
|
}
|
|
583
656
|
})
|
|
657
|
+
|
|
658
|
+
test('any', function (t) {
|
|
659
|
+
const o = {
|
|
660
|
+
hello: 'world',
|
|
661
|
+
num: 42,
|
|
662
|
+
neg: -42,
|
|
663
|
+
arr: [{ yes: 1 }, { no: false }],
|
|
664
|
+
nest: {},
|
|
665
|
+
float: 0.54
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
t.alike(enc.decode(enc.any, enc.encode(enc.any, o)), o)
|
|
669
|
+
|
|
670
|
+
const arr = new Array(3)
|
|
671
|
+
|
|
672
|
+
t.alike(enc.decode(enc.any, enc.encode(enc.any, arr)), [null, null, null])
|
|
673
|
+
})
|