compact-encoding 2.8.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 +27 -4
- package/endian.js +3 -0
- package/index.js +48 -16
- package/package.json +1 -1
- package/raw.js +148 -0
- package/test.js +226 -160
package/README.md
CHANGED
|
@@ -11,13 +11,13 @@ npm install compact-encoding
|
|
|
11
11
|
``` js
|
|
12
12
|
const cenc = require('compact-encoding')
|
|
13
13
|
|
|
14
|
-
const state =
|
|
14
|
+
const state = cenc.state()
|
|
15
15
|
|
|
16
16
|
// use preencode to figure out how big a buffer is needed
|
|
17
17
|
cenc.uint.preencode(state, 42)
|
|
18
18
|
cenc.string.preencode(state, 'hi')
|
|
19
19
|
|
|
20
|
-
console.log(state) // { start: 0, end: 4, buffer: null }
|
|
20
|
+
console.log(state) // { start: 0, end: 4, buffer: null, cache: null }
|
|
21
21
|
|
|
22
22
|
state.buffer = Buffer.allocUnsafe(state.end)
|
|
23
23
|
|
|
@@ -36,13 +36,14 @@ cenc.string.decode(state) // 'hi'
|
|
|
36
36
|
|
|
37
37
|
#### `state`
|
|
38
38
|
|
|
39
|
-
Should be an object that looks like this `{ start, end, buffer }`.
|
|
39
|
+
Should be an object that looks like this `{ start, end, buffer, cache }`.
|
|
40
40
|
|
|
41
41
|
You can also get a blank state object using `cenc.state()`.
|
|
42
42
|
|
|
43
43
|
* `start` is the byte offset to start encoding/decoding at.
|
|
44
44
|
* `end` is the byte offset indicating the end of the buffer.
|
|
45
45
|
* `buffer` should be either a Node.js Buffer or Uint8Array.
|
|
46
|
+
* `cache` is used internally be codecs, starts out as `null`.
|
|
46
47
|
|
|
47
48
|
#### `enc.preencode(state, val)`
|
|
48
49
|
|
|
@@ -74,6 +75,7 @@ const bool = cenc.decode(cenc.bool, buf)
|
|
|
74
75
|
The following encodings are bundled as they are primitives that can be used
|
|
75
76
|
to build others on top. Feel free to PR more that are missing.
|
|
76
77
|
|
|
78
|
+
* `cenc.raw` - Pass through encodes a buffer, i.e. a basic copy.
|
|
77
79
|
* `cenc.uint` - Encodes a uint using [compact-uint](https://github.com/mafintosh/compact-uint).
|
|
78
80
|
* `cenc.uint8` - Encodes a fixed size uint8.
|
|
79
81
|
* `cenc.uint16` - Encodes a fixed size uint16. Useful for things like ports.
|
|
@@ -96,27 +98,48 @@ to build others on top. Feel free to PR more that are missing.
|
|
|
96
98
|
* `cenc.float32` - Encodes a fixed size float32.
|
|
97
99
|
* `cenc.float64` - Encodes a fixed size float64.
|
|
98
100
|
* `cenc.buffer` - Encodes a buffer with its length uint prefixed. When decoding an empty buffer, `null` is returned.
|
|
99
|
-
* `cenc.raw` -
|
|
101
|
+
* `cenc.raw.buffer` - Encodes a buffer without a length prefixed.
|
|
100
102
|
* `cenc.uint8array` - Encodes a uint8array with its element length uint prefixed.
|
|
103
|
+
* `cenc.raw.uint8array` - Encodes a uint8array without a length prefixed.
|
|
101
104
|
* `cenc.uint16array` - Encodes a uint16array with its element length uint prefixed.
|
|
105
|
+
* `cenc.raw.uint16array` - Encodes a uint16array without a length prefixed.
|
|
102
106
|
* `cenc.uint32array` - Encodes a uint32array with its element length uint prefixed.
|
|
107
|
+
* `cenc.raw.uint32array` - Encodes a uint32array without a length prefixed.
|
|
103
108
|
* `cenc.int8array` - Encodes a int8array with its element length uint prefixed.
|
|
109
|
+
* `cenc.raw.int8array` - Encodes a int8array without a length prefixed.
|
|
104
110
|
* `cenc.int16array` - Encodes a int16array with its element length uint prefixed.
|
|
111
|
+
* `cenc.raw.int16array` - Encodes a int16array without a length prefixed.
|
|
105
112
|
* `cenc.int32array` - Encodes a int32array with its element length uint prefixed.
|
|
113
|
+
* `cenc.raw.int32array` - Encodes a int32array without a length prefixed.
|
|
106
114
|
* `cenc.float32array` - Encodes a float32array with its element length uint prefixed.
|
|
115
|
+
* `cenc.raw.float32array` - Encodes a float32array without a length prefixed.
|
|
107
116
|
* `cenc.float64array` - Encodes a float64array with its element length uint prefixed.
|
|
117
|
+
* `cenc.raw.float64array` - Encodes a float64array without a length prefixed.
|
|
108
118
|
* `cenc.bool` - Encodes a boolean as 1 or 0.
|
|
109
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.
|
|
121
|
+
* `cenc.string.fixed(n)`, `cenc.utf8.fixed(n)` - Encodes a fixed sized utf-8 string.
|
|
110
122
|
* `cenc.ascii` - Encodes an ascii string.
|
|
123
|
+
* `cenc.raw.ascii` - Encodes an ascii string without a length prefixed.
|
|
124
|
+
* `cenc.ascii.fixed(n)` - Encodes a fixed size ascii string.
|
|
111
125
|
* `cenc.hex` - Encodes a hex string.
|
|
126
|
+
* `cenc.raw.hex` - Encodes a hex string without a length prefixed.
|
|
127
|
+
* `cenc.hex.fixed(n)` - Encodes a fixed size hex string.
|
|
112
128
|
* `cenc.base64` - Encodes a base64 string.
|
|
129
|
+
* `cenc.raw.base64` - Encodes a base64 string without a length prefixed.
|
|
130
|
+
* `cenc.base64.fixed(n)` - Encodes a fixed size base64 string.
|
|
113
131
|
* `cenc.utf16le`, `cenc.ucs2` - Encodes a utf16le string.
|
|
132
|
+
* `cenc.raw.utf16le`, `cenc.raw.ucs2` - Encodes a utf16le string without a length prefixed.
|
|
133
|
+
* `cenc.utf16le.fixed(n)`, `cenc.ucs2.fixed(n)` - Encodes a fixed size utf16le string.
|
|
114
134
|
* `cenc.fixed32` - Encodes a fixed 32 byte buffer.
|
|
115
135
|
* `cenc.fixed64` - Encodes a fixed 64 byte buffer.
|
|
116
136
|
* `cenc.fixed(n)` - Makes a fixed sized encoder.
|
|
117
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.
|
|
118
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.
|
|
119
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.
|
|
120
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).
|
|
121
144
|
|
|
122
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
|
-
exports.state = function () {
|
|
7
|
-
return { start
|
|
5
|
+
exports.state = function (start = 0, end = 0, buffer = null) {
|
|
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
|
|
|
@@ -303,6 +301,21 @@ function string (encoding) {
|
|
|
303
301
|
const len = uint.decode(state)
|
|
304
302
|
if (state.end - state.start < len) throw new Error('Out of bounds')
|
|
305
303
|
return b4a.toString(state.buffer, encoding, state.start, (state.start += len))
|
|
304
|
+
},
|
|
305
|
+
fixed (n) {
|
|
306
|
+
return {
|
|
307
|
+
preencode (state) {
|
|
308
|
+
state.end += n
|
|
309
|
+
},
|
|
310
|
+
encode (state, s) {
|
|
311
|
+
b4a.write(state.buffer, s, state.start, n, encoding)
|
|
312
|
+
state.start += n
|
|
313
|
+
},
|
|
314
|
+
decode (state) {
|
|
315
|
+
if (state.end - state.start < n) throw new Error('Out of bounds')
|
|
316
|
+
return b4a.toString(state.buffer, encoding, state.start, (state.start += n))
|
|
317
|
+
}
|
|
318
|
+
}
|
|
306
319
|
}
|
|
307
320
|
}
|
|
308
321
|
}
|
|
@@ -402,11 +415,30 @@ exports.ndjson = {
|
|
|
402
415
|
}
|
|
403
416
|
|
|
404
417
|
exports.from = function from (enc) {
|
|
418
|
+
if (typeof enc === 'string') return fromNamed(enc)
|
|
405
419
|
if (enc.preencode) return enc
|
|
406
420
|
if (enc.encodingLength) return fromAbstractEncoder(enc)
|
|
407
421
|
return fromCodec(enc)
|
|
408
422
|
}
|
|
409
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
|
+
|
|
410
442
|
function fromCodec (enc) {
|
|
411
443
|
let tmpM = null
|
|
412
444
|
let tmpBuf = null
|
|
@@ -445,7 +477,7 @@ function fromAbstractEncoder (enc) {
|
|
|
445
477
|
}
|
|
446
478
|
|
|
447
479
|
exports.encode = function encode (enc, m) {
|
|
448
|
-
const state =
|
|
480
|
+
const state = exports.state()
|
|
449
481
|
enc.preencode(state, m)
|
|
450
482
|
state.buffer = b4a.allocUnsafe(state.end)
|
|
451
483
|
enc.encode(state, m)
|
|
@@ -453,7 +485,7 @@ exports.encode = function encode (enc, m) {
|
|
|
453
485
|
}
|
|
454
486
|
|
|
455
487
|
exports.decode = function decode (enc, buffer) {
|
|
456
|
-
return enc.decode(
|
|
488
|
+
return enc.decode(exports.state(0, buffer.byteLength, buffer))
|
|
457
489
|
}
|
|
458
490
|
|
|
459
491
|
function zigZag (enc) {
|
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,23 +1,24 @@
|
|
|
1
1
|
const enc = require('./')
|
|
2
|
-
const
|
|
2
|
+
const test = require('brittle')
|
|
3
|
+
const b4a = require('b4a')
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
test('uint', function (t) {
|
|
5
6
|
const state = enc.state()
|
|
6
7
|
|
|
7
8
|
enc.uint.preencode(state, 42)
|
|
8
|
-
t.alike(state,
|
|
9
|
+
t.alike(state, enc.state(0, 1))
|
|
9
10
|
enc.uint.preencode(state, 4200)
|
|
10
|
-
t.alike(state,
|
|
11
|
+
t.alike(state, enc.state(0, 4))
|
|
11
12
|
enc.uint.preencode(state, Number.MAX_SAFE_INTEGER)
|
|
12
|
-
t.alike(state,
|
|
13
|
+
t.alike(state, enc.state(0, 13))
|
|
13
14
|
|
|
14
|
-
state.buffer =
|
|
15
|
+
state.buffer = b4a.alloc(state.end)
|
|
15
16
|
enc.uint.encode(state, 42)
|
|
16
|
-
t.alike(state,
|
|
17
|
+
t.alike(state, enc.state(1, 13, b4a.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])))
|
|
17
18
|
enc.uint.encode(state, 4200)
|
|
18
|
-
t.alike(state,
|
|
19
|
+
t.alike(state, enc.state(4, 13, b4a.from([42, 0xfd, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0])))
|
|
19
20
|
enc.uint.encode(state, Number.MAX_SAFE_INTEGER)
|
|
20
|
-
t.alike(state,
|
|
21
|
+
t.alike(state, enc.state(13, 13, b4a.from([42, 0xfd, 104, 16, 0xff, 255, 255, 255, 255, 255, 255, 31, 0])))
|
|
21
22
|
|
|
22
23
|
state.start = 0
|
|
23
24
|
t.is(enc.uint.decode(state), 42)
|
|
@@ -28,19 +29,19 @@ tape('uint', function (t) {
|
|
|
28
29
|
t.exception(() => enc.uint.decode(state))
|
|
29
30
|
})
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
test('int', function (t) {
|
|
32
33
|
const state = enc.state()
|
|
33
34
|
|
|
34
35
|
enc.int.preencode(state, 42)
|
|
35
|
-
t.alike(state,
|
|
36
|
+
t.alike(state, enc.state(0, 1))
|
|
36
37
|
enc.int.preencode(state, -4200)
|
|
37
|
-
t.alike(state,
|
|
38
|
+
t.alike(state, enc.state(0, 4))
|
|
38
39
|
|
|
39
|
-
state.buffer =
|
|
40
|
+
state.buffer = b4a.alloc(state.end)
|
|
40
41
|
enc.int.encode(state, 42)
|
|
41
|
-
t.alike(state,
|
|
42
|
+
t.alike(state, enc.state(1, 4, b4a.from([84, 0, 0, 0])))
|
|
42
43
|
enc.int.encode(state, -4200)
|
|
43
|
-
t.alike(state,
|
|
44
|
+
t.alike(state, enc.state(4, 4, b4a.from([84, 0xfd, 207, 32])))
|
|
44
45
|
|
|
45
46
|
state.start = 0
|
|
46
47
|
t.is(enc.int.decode(state), 42)
|
|
@@ -50,16 +51,16 @@ tape('int', function (t) {
|
|
|
50
51
|
t.exception(() => enc.int.decode(state))
|
|
51
52
|
})
|
|
52
53
|
|
|
53
|
-
|
|
54
|
+
test('float64', function (t) {
|
|
54
55
|
const state = enc.state()
|
|
55
56
|
|
|
56
57
|
enc.float64.preencode(state, 162.2377294)
|
|
57
|
-
t.alike(state,
|
|
58
|
+
t.alike(state, enc.state(0, 8))
|
|
58
59
|
|
|
59
|
-
state.buffer =
|
|
60
|
-
t.alike(state,
|
|
60
|
+
state.buffer = b4a.alloc(state.end)
|
|
61
|
+
t.alike(state, enc.state(0, 8, b4a.from([0, 0, 0, 0, 0, 0, 0, 0])))
|
|
61
62
|
enc.float64.encode(state, 162.2377294)
|
|
62
|
-
t.alike(state,
|
|
63
|
+
t.alike(state, enc.state(8, 8, b4a.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])))
|
|
63
64
|
|
|
64
65
|
state.start = 0
|
|
65
66
|
t.is(enc.float64.decode(state), 162.2377294)
|
|
@@ -74,13 +75,13 @@ tape('float64', function (t) {
|
|
|
74
75
|
|
|
75
76
|
enc.int.preencode(state, 0)
|
|
76
77
|
enc.float64.preencode(state, 162.2377294)
|
|
77
|
-
t.alike(state,
|
|
78
|
+
t.alike(state, enc.state(0, 9))
|
|
78
79
|
|
|
79
|
-
state.buffer =
|
|
80
|
-
t.alike(state,
|
|
80
|
+
state.buffer = b4a.alloc(state.end)
|
|
81
|
+
t.alike(state, enc.state(0, 9, b4a.from([0, 0, 0, 0, 0, 0, 0, 0, 0])))
|
|
81
82
|
enc.int.encode(state, 0)
|
|
82
83
|
enc.float64.encode(state, 162.2377294)
|
|
83
|
-
t.alike(state,
|
|
84
|
+
t.alike(state, enc.state(9, 9, b4a.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])))
|
|
84
85
|
|
|
85
86
|
state.start = 0
|
|
86
87
|
t.is(enc.int.decode(state), 0)
|
|
@@ -88,14 +89,14 @@ tape('float64', function (t) {
|
|
|
88
89
|
t.is(state.start, state.end)
|
|
89
90
|
|
|
90
91
|
// subarray
|
|
91
|
-
const buf =
|
|
92
|
+
const buf = b4a.alloc(10)
|
|
92
93
|
state.start = 0
|
|
93
94
|
state.buffer = buf.subarray(1)
|
|
94
|
-
t.alike(state,
|
|
95
|
+
t.alike(state, enc.state(0, 9, b4a.from([0, 0, 0, 0, 0, 0, 0, 0, 0])))
|
|
95
96
|
enc.int.encode(state, 0)
|
|
96
97
|
enc.float64.encode(state, 162.2377294)
|
|
97
|
-
t.alike(state,
|
|
98
|
-
t.alike(buf,
|
|
98
|
+
t.alike(state, enc.state(9, 9, b4a.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])))
|
|
99
|
+
t.alike(buf, b4a.from([0, 0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))
|
|
99
100
|
|
|
100
101
|
state.start = 0
|
|
101
102
|
t.is(enc.int.decode(state), 0)
|
|
@@ -108,9 +109,9 @@ tape('float64', function (t) {
|
|
|
108
109
|
state.buffer = null
|
|
109
110
|
|
|
110
111
|
enc.float64.preencode(state, 162.2377294)
|
|
111
|
-
state.buffer =
|
|
112
|
+
state.buffer = b4a.alloc(state.end)
|
|
112
113
|
enc.float64.encode(state, 0)
|
|
113
|
-
t.alike(state,
|
|
114
|
+
t.alike(state, enc.state(8, 8, b4a.from([0, 0, 0, 0, 0, 0, 0, 0])))
|
|
114
115
|
|
|
115
116
|
state.start = 0
|
|
116
117
|
t.is(enc.float64.decode(state), 0)
|
|
@@ -122,9 +123,9 @@ tape('float64', function (t) {
|
|
|
122
123
|
state.buffer = null
|
|
123
124
|
|
|
124
125
|
enc.float64.preencode(state, Infinity)
|
|
125
|
-
state.buffer =
|
|
126
|
+
state.buffer = b4a.alloc(state.end)
|
|
126
127
|
enc.float64.encode(state, Infinity)
|
|
127
|
-
t.alike(state,
|
|
128
|
+
t.alike(state, enc.state(8, 8, b4a.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f])))
|
|
128
129
|
|
|
129
130
|
state.start = 0
|
|
130
131
|
t.is(enc.float64.decode(state), Infinity)
|
|
@@ -136,66 +137,85 @@ tape('float64', function (t) {
|
|
|
136
137
|
state.buffer = null
|
|
137
138
|
|
|
138
139
|
enc.float64.preencode(state, 0.1 + 0.2)
|
|
139
|
-
state.buffer =
|
|
140
|
+
state.buffer = b4a.alloc(state.end)
|
|
140
141
|
enc.float64.encode(state, 0.1 + 0.2)
|
|
141
|
-
t.alike(state,
|
|
142
|
+
t.alike(state, enc.state(8, 8, b4a.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f])))
|
|
142
143
|
|
|
143
144
|
state.start = 0
|
|
144
145
|
t.is(enc.float64.decode(state), 0.1 + 0.2)
|
|
145
146
|
t.is(state.start, state.end)
|
|
146
147
|
})
|
|
147
148
|
|
|
148
|
-
|
|
149
|
+
test('buffer', function (t) {
|
|
149
150
|
const state = enc.state()
|
|
150
151
|
|
|
151
|
-
enc.buffer.preencode(state,
|
|
152
|
-
t.alike(state,
|
|
153
|
-
enc.buffer.preencode(state,
|
|
154
|
-
t.alike(state,
|
|
152
|
+
enc.buffer.preencode(state, b4a.from('hi'))
|
|
153
|
+
t.alike(state, enc.state(0, 3))
|
|
154
|
+
enc.buffer.preencode(state, b4a.from('hello'))
|
|
155
|
+
t.alike(state, enc.state(0, 9))
|
|
155
156
|
enc.buffer.preencode(state, null)
|
|
156
|
-
t.alike(state,
|
|
157
|
+
t.alike(state, enc.state(0, 10))
|
|
157
158
|
|
|
158
|
-
state.buffer =
|
|
159
|
-
enc.buffer.encode(state,
|
|
160
|
-
t.alike(state,
|
|
161
|
-
enc.buffer.encode(state,
|
|
162
|
-
t.alike(state,
|
|
159
|
+
state.buffer = b4a.alloc(state.end)
|
|
160
|
+
enc.buffer.encode(state, b4a.from('hi'))
|
|
161
|
+
t.alike(state, enc.state(3, 10, b4a.from('\x02hi\x00\x00\x00\x00\x00\x00\x00')))
|
|
162
|
+
enc.buffer.encode(state, b4a.from('hello'))
|
|
163
|
+
t.alike(state, enc.state(9, 10, b4a.from('\x02hi\x05hello\x00')))
|
|
163
164
|
enc.buffer.encode(state, null)
|
|
164
|
-
t.alike(state,
|
|
165
|
+
t.alike(state, enc.state(10, 10, b4a.from('\x02hi\x05hello\x00')))
|
|
165
166
|
|
|
166
167
|
state.start = 0
|
|
167
|
-
t.alike(enc.buffer.decode(state),
|
|
168
|
-
t.alike(enc.buffer.decode(state),
|
|
168
|
+
t.alike(enc.buffer.decode(state), b4a.from('hi'))
|
|
169
|
+
t.alike(enc.buffer.decode(state), b4a.from('hello'))
|
|
169
170
|
t.is(enc.buffer.decode(state), null)
|
|
170
171
|
t.is(state.start, state.end)
|
|
171
172
|
|
|
172
173
|
t.exception(() => enc.buffer.decode(state))
|
|
173
174
|
})
|
|
174
175
|
|
|
175
|
-
|
|
176
|
+
test('raw', function (t) {
|
|
176
177
|
const state = enc.state()
|
|
177
178
|
|
|
178
|
-
enc.raw.preencode(state,
|
|
179
|
-
t.alike(state,
|
|
179
|
+
enc.raw.preencode(state, b4a.from('hi'))
|
|
180
|
+
t.alike(state, enc.state(0, 2))
|
|
180
181
|
|
|
181
|
-
state.buffer =
|
|
182
|
-
enc.raw.encode(state,
|
|
183
|
-
t.alike(state,
|
|
182
|
+
state.buffer = b4a.alloc(state.end)
|
|
183
|
+
enc.raw.encode(state, b4a.from('hi'))
|
|
184
|
+
t.alike(state, enc.state(2, 2, b4a.from('hi')))
|
|
184
185
|
|
|
185
186
|
state.start = 0
|
|
186
|
-
t.alike(enc.raw.decode(state),
|
|
187
|
+
t.alike(enc.raw.decode(state), b4a.from('hi'))
|
|
187
188
|
t.is(state.start, state.end)
|
|
188
189
|
})
|
|
189
190
|
|
|
190
|
-
|
|
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) {
|
|
191
211
|
const state = enc.state()
|
|
192
212
|
|
|
193
213
|
enc.uint16array.preencode(state, new Uint16Array([1, 2, 3]))
|
|
194
|
-
t.alike(state,
|
|
214
|
+
t.alike(state, enc.state(0, 7))
|
|
195
215
|
|
|
196
|
-
state.buffer =
|
|
216
|
+
state.buffer = b4a.alloc(state.end)
|
|
197
217
|
enc.uint16array.encode(state, new Uint16Array([1, 2, 3]))
|
|
198
|
-
t.alike(state,
|
|
218
|
+
t.alike(state, enc.state(7, 7, b4a.from([3, 1, 0, 2, 0, 3, 0])))
|
|
199
219
|
|
|
200
220
|
state.start = 0
|
|
201
221
|
t.alike(enc.uint16array.decode(state), new Uint16Array([1, 2, 3]))
|
|
@@ -204,19 +224,19 @@ tape('uint16array', function (t) {
|
|
|
204
224
|
t.exception(() => enc.uint16array.decode(state))
|
|
205
225
|
})
|
|
206
226
|
|
|
207
|
-
|
|
227
|
+
test('uint32array', function (t) {
|
|
208
228
|
const state = enc.state()
|
|
209
229
|
|
|
210
230
|
enc.uint32array.preencode(state, new Uint32Array([1]))
|
|
211
|
-
t.alike(state,
|
|
231
|
+
t.alike(state, enc.state(0, 5))
|
|
212
232
|
enc.uint32array.preencode(state, new Uint32Array([42, 43]))
|
|
213
|
-
t.alike(state,
|
|
233
|
+
t.alike(state, enc.state(0, 14))
|
|
214
234
|
|
|
215
|
-
state.buffer =
|
|
235
|
+
state.buffer = b4a.alloc(state.end)
|
|
216
236
|
enc.uint32array.encode(state, new Uint32Array([1]))
|
|
217
|
-
t.alike(state,
|
|
237
|
+
t.alike(state, enc.state(5, 14, b4a.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])))
|
|
218
238
|
enc.uint32array.encode(state, new Uint32Array([42, 43]))
|
|
219
|
-
t.alike(state,
|
|
239
|
+
t.alike(state, enc.state(14, 14, b4a.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0])))
|
|
220
240
|
|
|
221
241
|
state.start = 0
|
|
222
242
|
t.alike(enc.uint32array.decode(state), new Uint32Array([1]))
|
|
@@ -226,15 +246,15 @@ tape('uint32array', function (t) {
|
|
|
226
246
|
t.exception(() => enc.uint32array.decode(state))
|
|
227
247
|
})
|
|
228
248
|
|
|
229
|
-
|
|
249
|
+
test('int16array', function (t) {
|
|
230
250
|
const state = enc.state()
|
|
231
251
|
|
|
232
252
|
enc.int16array.preencode(state, new Int16Array([1, -2, 3]))
|
|
233
|
-
t.alike(state,
|
|
253
|
+
t.alike(state, enc.state(0, 7))
|
|
234
254
|
|
|
235
|
-
state.buffer =
|
|
255
|
+
state.buffer = b4a.alloc(state.end)
|
|
236
256
|
enc.int16array.encode(state, new Int16Array([1, -2, 3]))
|
|
237
|
-
t.alike(state,
|
|
257
|
+
t.alike(state, enc.state(7, 7, b4a.from([3, 1, 0, 0xfe, 0xff, 3, 0])))
|
|
238
258
|
|
|
239
259
|
state.start = 0
|
|
240
260
|
t.alike(enc.int16array.decode(state), new Int16Array([1, -2, 3]))
|
|
@@ -243,15 +263,15 @@ tape('int16array', function (t) {
|
|
|
243
263
|
t.exception(() => enc.int16array.decode(state))
|
|
244
264
|
})
|
|
245
265
|
|
|
246
|
-
|
|
266
|
+
test('int32array', function (t) {
|
|
247
267
|
const state = enc.state()
|
|
248
268
|
|
|
249
269
|
enc.int32array.preencode(state, new Int32Array([1, -2, 3]))
|
|
250
|
-
t.alike(state,
|
|
270
|
+
t.alike(state, enc.state(0, 13))
|
|
251
271
|
|
|
252
|
-
state.buffer =
|
|
272
|
+
state.buffer = b4a.alloc(state.end)
|
|
253
273
|
enc.int32array.encode(state, new Int32Array([1, -2, 3]))
|
|
254
|
-
t.alike(state,
|
|
274
|
+
t.alike(state, enc.state(13, 13, b4a.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0])))
|
|
255
275
|
|
|
256
276
|
state.start = 0
|
|
257
277
|
t.alike(enc.int32array.decode(state), new Int32Array([1, -2, 3]))
|
|
@@ -260,15 +280,15 @@ tape('int32array', function (t) {
|
|
|
260
280
|
t.exception(() => enc.int32array.decode(state))
|
|
261
281
|
})
|
|
262
282
|
|
|
263
|
-
|
|
283
|
+
test('float32array', function (t) {
|
|
264
284
|
const state = enc.state()
|
|
265
285
|
|
|
266
286
|
enc.float32array.preencode(state, new Float32Array([1.1, -2.2, 3.3]))
|
|
267
|
-
t.alike(state,
|
|
287
|
+
t.alike(state, enc.state(0, 13))
|
|
268
288
|
|
|
269
|
-
state.buffer =
|
|
289
|
+
state.buffer = b4a.alloc(state.end)
|
|
270
290
|
enc.float32array.encode(state, new Float32Array([1.1, -2.2, 3.3]))
|
|
271
|
-
t.alike(state,
|
|
291
|
+
t.alike(state, enc.state(13, 13, b4a.from([3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53, 0x40])))
|
|
272
292
|
|
|
273
293
|
state.start = 0
|
|
274
294
|
t.alike(enc.float32array.decode(state), new Float32Array([1.1, -2.2, 3.3]))
|
|
@@ -277,15 +297,15 @@ tape('float32array', function (t) {
|
|
|
277
297
|
t.exception(() => enc.float32array.decode(state))
|
|
278
298
|
})
|
|
279
299
|
|
|
280
|
-
|
|
300
|
+
test('float64array', function (t) {
|
|
281
301
|
const state = enc.state()
|
|
282
302
|
|
|
283
303
|
enc.float64array.preencode(state, new Float64Array([1.1, -2.2, 3.3]))
|
|
284
|
-
t.alike(state,
|
|
304
|
+
t.alike(state, enc.state(0, 25))
|
|
285
305
|
|
|
286
|
-
state.buffer =
|
|
306
|
+
state.buffer = b4a.alloc(state.end)
|
|
287
307
|
enc.float64array.encode(state, new Float64Array([1.1, -2.2, 3.3]))
|
|
288
|
-
t.alike(state,
|
|
308
|
+
t.alike(state, enc.state(25, 25, b4a.from([3, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x01, 0xc0, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40])))
|
|
289
309
|
|
|
290
310
|
state.start = 0
|
|
291
311
|
t.alike(enc.float64array.decode(state), new Float64Array([1.1, -2.2, 3.3]))
|
|
@@ -294,19 +314,19 @@ tape('float64array', function (t) {
|
|
|
294
314
|
t.exception(() => enc.float64array.decode(state))
|
|
295
315
|
})
|
|
296
316
|
|
|
297
|
-
|
|
317
|
+
test('string', function (t) {
|
|
298
318
|
const state = enc.state()
|
|
299
319
|
|
|
300
320
|
enc.string.preencode(state, '🌾')
|
|
301
|
-
t.alike(state,
|
|
321
|
+
t.alike(state, enc.state(0, 5))
|
|
302
322
|
enc.string.preencode(state, 'høsten er fin')
|
|
303
|
-
t.alike(state,
|
|
323
|
+
t.alike(state, enc.state(0, 20))
|
|
304
324
|
|
|
305
|
-
state.buffer =
|
|
325
|
+
state.buffer = b4a.alloc(state.end)
|
|
306
326
|
enc.string.encode(state, '🌾')
|
|
307
|
-
t.alike(state,
|
|
327
|
+
t.alike(state, enc.state(5, 20, b4a.from('\x04🌾\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')))
|
|
308
328
|
enc.string.encode(state, 'høsten er fin')
|
|
309
|
-
t.alike(state,
|
|
329
|
+
t.alike(state, enc.state(20, 20, b4a.from('\x04🌾\x0ehøsten er fin')))
|
|
310
330
|
|
|
311
331
|
state.start = 0
|
|
312
332
|
t.is(enc.string.decode(state), '🌾')
|
|
@@ -316,68 +336,86 @@ tape('string', function (t) {
|
|
|
316
336
|
t.exception(() => enc.string.decode(state))
|
|
317
337
|
})
|
|
318
338
|
|
|
319
|
-
|
|
339
|
+
test('raw string', function (t) {
|
|
320
340
|
const state = enc.state()
|
|
321
341
|
|
|
322
|
-
enc.
|
|
323
|
-
t.alike(state,
|
|
324
|
-
enc.
|
|
325
|
-
t.alike(state,
|
|
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))
|
|
326
346
|
|
|
327
|
-
state.buffer =
|
|
328
|
-
enc.
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
t.alike(state, { start: 64, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32).fill('b', 32, 64) })
|
|
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')))
|
|
332
351
|
|
|
333
352
|
state.start = 0
|
|
334
|
-
t.
|
|
335
|
-
t.
|
|
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) {
|
|
358
|
+
const state = enc.state()
|
|
359
|
+
|
|
360
|
+
enc.fixed32.preencode(state, b4a.alloc(32).fill('a'))
|
|
361
|
+
t.alike(state, enc.state(0, 32))
|
|
362
|
+
enc.fixed32.preencode(state, b4a.alloc(32).fill('b'))
|
|
363
|
+
t.alike(state, enc.state(0, 64))
|
|
364
|
+
|
|
365
|
+
state.buffer = b4a.alloc(state.end)
|
|
366
|
+
enc.fixed32.encode(state, b4a.alloc(32).fill('a'))
|
|
367
|
+
t.alike(state, enc.state(32, 64, b4a.alloc(64).fill('a', 0, 32)))
|
|
368
|
+
enc.fixed32.encode(state, b4a.alloc(32).fill('b'))
|
|
369
|
+
t.alike(state, enc.state(64, 64, b4a.alloc(64).fill('a', 0, 32).fill('b', 32, 64)))
|
|
370
|
+
|
|
371
|
+
state.start = 0
|
|
372
|
+
t.alike(enc.fixed32.decode(state), b4a.alloc(32).fill('a'))
|
|
373
|
+
t.alike(enc.fixed32.decode(state), b4a.alloc(32).fill('b'))
|
|
336
374
|
t.is(state.start, state.end)
|
|
337
375
|
|
|
338
376
|
t.exception(() => enc.fixed32.decode(state))
|
|
339
377
|
})
|
|
340
378
|
|
|
341
|
-
|
|
379
|
+
test('fixed64', function (t) {
|
|
342
380
|
const state = enc.state()
|
|
343
381
|
|
|
344
|
-
enc.fixed64.preencode(state,
|
|
345
|
-
t.alike(state,
|
|
346
|
-
enc.fixed64.preencode(state,
|
|
347
|
-
t.alike(state,
|
|
382
|
+
enc.fixed64.preencode(state, b4a.alloc(64).fill('a'))
|
|
383
|
+
t.alike(state, enc.state(0, 64))
|
|
384
|
+
enc.fixed64.preencode(state, b4a.alloc(64).fill('b'))
|
|
385
|
+
t.alike(state, enc.state(0, 128))
|
|
348
386
|
|
|
349
|
-
state.buffer =
|
|
350
|
-
enc.fixed64.encode(state,
|
|
351
|
-
t.alike(state,
|
|
352
|
-
enc.fixed64.encode(state,
|
|
353
|
-
t.alike(state,
|
|
387
|
+
state.buffer = b4a.alloc(state.end)
|
|
388
|
+
enc.fixed64.encode(state, b4a.alloc(64).fill('a'))
|
|
389
|
+
t.alike(state, enc.state(64, 128, b4a.alloc(128).fill('a', 0, 64)))
|
|
390
|
+
enc.fixed64.encode(state, b4a.alloc(64).fill('b'))
|
|
391
|
+
t.alike(state, enc.state(128, 128, b4a.alloc(128).fill('a', 0, 64).fill('b', 64, 128)))
|
|
354
392
|
|
|
355
393
|
state.start = 0
|
|
356
|
-
t.alike(enc.fixed64.decode(state),
|
|
357
|
-
t.alike(enc.fixed64.decode(state),
|
|
394
|
+
t.alike(enc.fixed64.decode(state), b4a.alloc(64).fill('a'))
|
|
395
|
+
t.alike(enc.fixed64.decode(state), b4a.alloc(64).fill('b'))
|
|
358
396
|
t.is(state.start, state.end)
|
|
359
397
|
|
|
360
398
|
t.exception(() => enc.fixed64.decode(state))
|
|
361
399
|
})
|
|
362
400
|
|
|
363
|
-
|
|
401
|
+
test('fixed n', function (t) {
|
|
364
402
|
const state = enc.state()
|
|
365
403
|
const fixed = enc.fixed(3)
|
|
366
404
|
|
|
367
|
-
fixed.preencode(state,
|
|
368
|
-
t.alike(state,
|
|
369
|
-
fixed.preencode(state,
|
|
370
|
-
t.alike(state,
|
|
405
|
+
fixed.preencode(state, b4a.alloc(3).fill('a'))
|
|
406
|
+
t.alike(state, enc.state(0, 3))
|
|
407
|
+
fixed.preencode(state, b4a.alloc(3).fill('b'))
|
|
408
|
+
t.alike(state, enc.state(0, 6))
|
|
371
409
|
|
|
372
|
-
state.buffer =
|
|
373
|
-
fixed.encode(state,
|
|
374
|
-
t.alike(state,
|
|
375
|
-
fixed.encode(state,
|
|
376
|
-
t.alike(state,
|
|
410
|
+
state.buffer = b4a.alloc(state.end)
|
|
411
|
+
fixed.encode(state, b4a.alloc(3).fill('a'))
|
|
412
|
+
t.alike(state, enc.state(3, 6, b4a.alloc(6).fill('a', 0, 3)))
|
|
413
|
+
fixed.encode(state, b4a.alloc(3).fill('b'))
|
|
414
|
+
t.alike(state, enc.state(6, 6, b4a.alloc(6).fill('a', 0, 3).fill('b', 3, 6)))
|
|
377
415
|
|
|
378
416
|
state.start = 0
|
|
379
|
-
t.alike(fixed.decode(state),
|
|
380
|
-
t.alike(fixed.decode(state),
|
|
417
|
+
t.alike(fixed.decode(state), b4a.alloc(3).fill('a'))
|
|
418
|
+
t.alike(fixed.decode(state), b4a.alloc(3).fill('b'))
|
|
381
419
|
t.is(state.start, state.end)
|
|
382
420
|
|
|
383
421
|
t.exception(() => fixed.decode(state))
|
|
@@ -385,20 +423,20 @@ tape('fixed n', function (t) {
|
|
|
385
423
|
t.exception(() => fixed.decode(state))
|
|
386
424
|
})
|
|
387
425
|
|
|
388
|
-
|
|
426
|
+
test('array', function (t) {
|
|
389
427
|
const state = enc.state()
|
|
390
428
|
const arr = enc.array(enc.bool)
|
|
391
429
|
|
|
392
430
|
arr.preencode(state, [true, false, true])
|
|
393
|
-
t.alike(state,
|
|
431
|
+
t.alike(state, enc.state(0, 4))
|
|
394
432
|
arr.preencode(state, [false, false, true, true])
|
|
395
|
-
t.alike(state,
|
|
433
|
+
t.alike(state, enc.state(0, 9))
|
|
396
434
|
|
|
397
|
-
state.buffer =
|
|
435
|
+
state.buffer = b4a.alloc(state.end)
|
|
398
436
|
arr.encode(state, [true, false, true])
|
|
399
|
-
t.alike(state,
|
|
437
|
+
t.alike(state, enc.state(4, 9, b4a.from([3, 1, 0, 1, 0, 0, 0, 0, 0])))
|
|
400
438
|
arr.encode(state, [false, false, true, true])
|
|
401
|
-
t.alike(state,
|
|
439
|
+
t.alike(state, enc.state(9, 9, b4a.from([3, 1, 0, 1, 4, 0, 0, 1, 1])))
|
|
402
440
|
|
|
403
441
|
state.start = 0
|
|
404
442
|
t.alike(arr.decode(state), [true, false, true])
|
|
@@ -408,22 +446,38 @@ tape('array', function (t) {
|
|
|
408
446
|
t.exception(() => arr.decode(state))
|
|
409
447
|
})
|
|
410
448
|
|
|
411
|
-
|
|
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) {
|
|
412
470
|
const state = enc.state()
|
|
413
471
|
|
|
414
472
|
enc.json.preencode(state, { a: 1, b: 2 })
|
|
415
|
-
t.alike(state,
|
|
473
|
+
t.alike(state, enc.state(0, 14))
|
|
416
474
|
|
|
417
|
-
state.buffer =
|
|
475
|
+
state.buffer = b4a.alloc(state.end)
|
|
418
476
|
enc.json.encode(state, { a: 1, b: 2 })
|
|
419
|
-
t.alike(state,
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
Buffer.from([13]),
|
|
424
|
-
Buffer.from('{"a":1,"b":2}')
|
|
425
|
-
])
|
|
426
|
-
})
|
|
477
|
+
t.alike(state, enc.state(14, 14, b4a.concat([
|
|
478
|
+
b4a.from([13]),
|
|
479
|
+
b4a.from('{"a":1,"b":2}')
|
|
480
|
+
])))
|
|
427
481
|
|
|
428
482
|
state.start = 0
|
|
429
483
|
t.alike(enc.json.decode(state), { a: 1, b: 2 })
|
|
@@ -431,7 +485,23 @@ tape('json', function (t) {
|
|
|
431
485
|
t.exception(() => enc.json.decode(state))
|
|
432
486
|
})
|
|
433
487
|
|
|
434
|
-
|
|
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) {
|
|
435
505
|
t.plan(1)
|
|
436
506
|
|
|
437
507
|
let prev = enc.encode(enc.lexint, 0)
|
|
@@ -441,14 +511,14 @@ tape('lexint: big numbers', function (t) {
|
|
|
441
511
|
|
|
442
512
|
for (n = 1; n < Number.MAX_VALUE; n += skip) {
|
|
443
513
|
const cur = enc.encode(enc.lexint, n)
|
|
444
|
-
if (
|
|
514
|
+
if (b4a.compare(cur, prev) < 1) break
|
|
445
515
|
prev = cur
|
|
446
516
|
skip = 1 + Math.pow(245, Math.ceil(Math.log(n) / Math.log(256)))
|
|
447
517
|
}
|
|
448
518
|
t.is(n, Infinity)
|
|
449
519
|
})
|
|
450
520
|
|
|
451
|
-
|
|
521
|
+
test('lexint: range precision', function (t) {
|
|
452
522
|
t.plan(2)
|
|
453
523
|
const a = 1e55
|
|
454
524
|
const b = 1.0000000000001e55
|
|
@@ -458,44 +528,40 @@ tape('lexint: range precision', function (t) {
|
|
|
458
528
|
t.not(ha, hb)
|
|
459
529
|
})
|
|
460
530
|
|
|
461
|
-
|
|
531
|
+
test('lexint: range precision', function (t) {
|
|
462
532
|
let prev = enc.encode(enc.lexint, 0)
|
|
463
533
|
const skip = 0.000000001e55
|
|
464
534
|
for (let i = 0, n = 1e55; i < 1000; n = 1e55 + skip * ++i) {
|
|
465
535
|
const cur = enc.encode(enc.lexint, n)
|
|
466
|
-
if (
|
|
536
|
+
if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev')
|
|
467
537
|
prev = cur
|
|
468
538
|
}
|
|
469
539
|
t.ok(true)
|
|
470
540
|
t.end()
|
|
471
541
|
})
|
|
472
542
|
|
|
473
|
-
|
|
543
|
+
test('lexint: small numbers', function (t) {
|
|
474
544
|
let prev = enc.encode(enc.lexint, 0)
|
|
475
545
|
for (let n = 1; n < 256 * 256 * 16; n++) {
|
|
476
546
|
const cur = enc.encode(enc.lexint, n)
|
|
477
|
-
if (
|
|
547
|
+
if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev')
|
|
478
548
|
prev = cur
|
|
479
549
|
}
|
|
480
550
|
t.ok(true)
|
|
481
551
|
t.end()
|
|
482
552
|
})
|
|
483
553
|
|
|
484
|
-
|
|
554
|
+
test('lexint: throws', function (t) {
|
|
485
555
|
t.exception(() => {
|
|
486
|
-
enc.decode(enc.lexint,
|
|
556
|
+
enc.decode(enc.lexint, b4a.alloc(1, 251))
|
|
487
557
|
})
|
|
488
558
|
|
|
489
559
|
let num = 252
|
|
490
560
|
|
|
491
|
-
const state =
|
|
492
|
-
start: 0,
|
|
493
|
-
end: 0,
|
|
494
|
-
buffer: null
|
|
495
|
-
}
|
|
561
|
+
const state = enc.state()
|
|
496
562
|
|
|
497
563
|
enc.lexint.preencode(state, num)
|
|
498
|
-
state.buffer =
|
|
564
|
+
state.buffer = b4a.alloc(state.end - state.start)
|
|
499
565
|
enc.lexint.encode(state, num)
|
|
500
566
|
|
|
501
567
|
t.exception(() => {
|
|
@@ -509,7 +575,7 @@ tape('lexint: throws', function (t) {
|
|
|
509
575
|
state.buffer = null
|
|
510
576
|
|
|
511
577
|
enc.lexint.preencode(state, num)
|
|
512
|
-
state.buffer =
|
|
578
|
+
state.buffer = b4a.alloc(state.end - state.start)
|
|
513
579
|
enc.lexint.encode(state, num)
|
|
514
580
|
|
|
515
581
|
t.exception(() => {
|
|
@@ -523,7 +589,7 @@ tape('lexint: throws', function (t) {
|
|
|
523
589
|
state.buffer = null
|
|
524
590
|
|
|
525
591
|
enc.lexint.preencode(state, num)
|
|
526
|
-
state.buffer =
|
|
592
|
+
state.buffer = b4a.alloc(state.end - state.start)
|
|
527
593
|
enc.lexint.encode(state, num)
|
|
528
594
|
|
|
529
595
|
t.exception(() => {
|
|
@@ -537,7 +603,7 @@ tape('lexint: throws', function (t) {
|
|
|
537
603
|
state.buffer = null
|
|
538
604
|
|
|
539
605
|
enc.lexint.preencode(state, num)
|
|
540
|
-
state.buffer =
|
|
606
|
+
state.buffer = b4a.alloc(state.end - state.start)
|
|
541
607
|
enc.lexint.encode(state, num)
|
|
542
608
|
|
|
543
609
|
t.exception(() => {
|
|
@@ -551,7 +617,7 @@ tape('lexint: throws', function (t) {
|
|
|
551
617
|
state.buffer = null
|
|
552
618
|
|
|
553
619
|
enc.lexint.preencode(state, num)
|
|
554
|
-
state.buffer =
|
|
620
|
+
state.buffer = b4a.alloc(state.end - state.start)
|
|
555
621
|
enc.lexint.encode(state, num)
|
|
556
622
|
|
|
557
623
|
t.exception(() => {
|
|
@@ -561,7 +627,7 @@ tape('lexint: throws', function (t) {
|
|
|
561
627
|
t.end()
|
|
562
628
|
})
|
|
563
629
|
|
|
564
|
-
|
|
630
|
+
test('lexint: unpack', function (t) {
|
|
565
631
|
let n
|
|
566
632
|
let skip = 1
|
|
567
633
|
|