compact-encoding 2.10.0 → 2.11.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/README.md +18 -1
- package/endian.js +3 -0
- package/index.js +29 -12
- package/package.json +1 -1
- package/raw.js +148 -0
- package/test.js +97 -24
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,32 +98,48 @@ 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.
|
|
126
143
|
* `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
144
|
|
|
128
145
|
## License
|
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
|
|
|
@@ -417,11 +415,30 @@ exports.ndjson = {
|
|
|
417
415
|
}
|
|
418
416
|
|
|
419
417
|
exports.from = function from (enc) {
|
|
418
|
+
if (typeof enc === 'string') return fromNamed(enc)
|
|
420
419
|
if (enc.preencode) return enc
|
|
421
420
|
if (enc.encodingLength) return fromAbstractEncoder(enc)
|
|
422
421
|
return fromCodec(enc)
|
|
423
422
|
}
|
|
424
423
|
|
|
424
|
+
function fromNamed (enc) {
|
|
425
|
+
switch (enc) {
|
|
426
|
+
case 'ascii': return raw.ascii
|
|
427
|
+
case 'utf-8':
|
|
428
|
+
case 'utf8': return raw.utf8
|
|
429
|
+
case 'hex': return raw.hex
|
|
430
|
+
case 'base64': return raw.base64
|
|
431
|
+
case 'utf16-le':
|
|
432
|
+
case 'utf16le':
|
|
433
|
+
case 'ucs-2':
|
|
434
|
+
case 'ucs2': return raw.ucs2
|
|
435
|
+
case 'ndjson': return raw.ndjson
|
|
436
|
+
case 'json': return raw.json
|
|
437
|
+
case 'binary':
|
|
438
|
+
default: return raw.binary
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
425
442
|
function fromCodec (enc) {
|
|
426
443
|
let tmpM = null
|
|
427
444
|
let tmpBuf = null
|
package/package.json
CHANGED
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
|
|