compact-encoding 2.7.0 → 2.10.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.
Files changed (4) hide show
  1. package/README.md +11 -3
  2. package/index.js +44 -5
  3. package/package.json +1 -1
  4. package/test.js +142 -126
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 = { start: 0, end: 0, buffer: null }
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
 
@@ -107,14 +108,21 @@ to build others on top. Feel free to PR more that are missing.
107
108
  * `cenc.float64array` - Encodes a float64array with its element length uint prefixed.
108
109
  * `cenc.bool` - Encodes a boolean as 1 or 0.
109
110
  * `cenc.string`, `cenc.utf8` - Encodes a utf-8 string, similar to buffer.
111
+ * `cenc.string.fixed(n)`, `cenc.utf8.fixed(n)` - Encodes a fixed sized utf-8 string.
110
112
  * `cenc.ascii` - Encodes an ascii string.
113
+ * `cenc.ascii.fixed(n)` - Encodes a fixed size ascii string.
111
114
  * `cenc.hex` - Encodes a hex string.
115
+ * `cenc.hex.fixed(n)` - Encodes a fixed size hex string.
112
116
  * `cenc.base64` - Encodes a base64 string.
117
+ * `cenc.base64.fixed(n)` - Encodes a fixed size base64 string.
113
118
  * `cenc.utf16le`, `cenc.ucs2` - Encodes a utf16le string.
119
+ * `cenc.utf16le.fixed(n)`, `cenc.ucs2.fixed(n)` - Encodes a fixed size utf16le string.
114
120
  * `cenc.fixed32` - Encodes a fixed 32 byte buffer.
115
121
  * `cenc.fixed64` - Encodes a fixed 64 byte buffer.
116
122
  * `cenc.fixed(n)` - Makes a fixed sized encoder.
117
123
  * `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
124
+ * `cenc.json` - Encodes a JSON value as utf-8.
125
+ * `cenc.ndjson` - Encodes a JSON value as newline delimited utf-8.
118
126
  * `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding).
119
127
 
120
128
  ## License
package/index.js CHANGED
@@ -3,8 +3,8 @@ const b4a = require('b4a')
3
3
  const LE = (new Uint8Array(new Uint16Array([0xff]).buffer))[0] === 0xff
4
4
  const BE = !LE
5
5
 
6
- exports.state = function () {
7
- return { start: 0, end: 0, buffer: null }
6
+ exports.state = function (start = 0, end = 0, buffer = null) {
7
+ return { start, end, buffer, cache: null }
8
8
  }
9
9
 
10
10
  const uint = exports.uint = {
@@ -303,11 +303,26 @@ function string (encoding) {
303
303
  const len = uint.decode(state)
304
304
  if (state.end - state.start < len) throw new Error('Out of bounds')
305
305
  return b4a.toString(state.buffer, encoding, state.start, (state.start += len))
306
+ },
307
+ fixed (n) {
308
+ return {
309
+ preencode (state) {
310
+ state.end += n
311
+ },
312
+ encode (state, s) {
313
+ b4a.write(state.buffer, s, state.start, n, encoding)
314
+ state.start += n
315
+ },
316
+ decode (state) {
317
+ if (state.end - state.start < n) throw new Error('Out of bounds')
318
+ return b4a.toString(state.buffer, encoding, state.start, (state.start += n))
319
+ }
320
+ }
306
321
  }
307
322
  }
308
323
  }
309
324
 
310
- exports.string = exports.utf8 = string('utf-8')
325
+ const utf8 = exports.string = exports.utf8 = string('utf-8')
311
326
  exports.ascii = string('ascii')
312
327
  exports.hex = string('hex')
313
328
  exports.base64 = string('base64')
@@ -377,6 +392,30 @@ exports.array = function array (enc) {
377
392
  }
378
393
  }
379
394
 
395
+ exports.json = {
396
+ preencode (state, v) {
397
+ utf8.preencode(state, JSON.stringify(v))
398
+ },
399
+ encode (state, v) {
400
+ utf8.encode(state, JSON.stringify(v))
401
+ },
402
+ decode (state) {
403
+ return JSON.parse(utf8.decode(state))
404
+ }
405
+ }
406
+
407
+ exports.ndjson = {
408
+ preencode (state, v) {
409
+ utf8.preencode(state, JSON.stringify(v) + '\n')
410
+ },
411
+ encode (state, v) {
412
+ utf8.encode(state, JSON.stringify(v) + '\n')
413
+ },
414
+ decode (state) {
415
+ return JSON.parse(utf8.decode(state))
416
+ }
417
+ }
418
+
380
419
  exports.from = function from (enc) {
381
420
  if (enc.preencode) return enc
382
421
  if (enc.encodingLength) return fromAbstractEncoder(enc)
@@ -421,7 +460,7 @@ function fromAbstractEncoder (enc) {
421
460
  }
422
461
 
423
462
  exports.encode = function encode (enc, m) {
424
- const state = { start: 0, end: 0, buffer: null }
463
+ const state = exports.state()
425
464
  enc.preencode(state, m)
426
465
  state.buffer = b4a.allocUnsafe(state.end)
427
466
  enc.encode(state, m)
@@ -429,7 +468,7 @@ exports.encode = function encode (enc, m) {
429
468
  }
430
469
 
431
470
  exports.decode = function decode (enc, buffer) {
432
- return enc.decode({ start: 0, end: buffer.byteLength, buffer })
471
+ return enc.decode(exports.state(0, buffer.byteLength, buffer))
433
472
  }
434
473
 
435
474
  function zigZag (enc) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.7.0",
3
+ "version": "2.10.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": {
package/test.js CHANGED
@@ -1,23 +1,24 @@
1
1
  const enc = require('./')
2
2
  const tape = require('brittle')
3
+ const b4a = require('b4a')
3
4
 
4
5
  tape('uint', function (t) {
5
6
  const state = enc.state()
6
7
 
7
8
  enc.uint.preencode(state, 42)
8
- t.alike(state, { start: 0, end: 1, buffer: null })
9
+ t.alike(state, enc.state(0, 1))
9
10
  enc.uint.preencode(state, 4200)
10
- t.alike(state, { start: 0, end: 4, buffer: null })
11
+ t.alike(state, enc.state(0, 4))
11
12
  enc.uint.preencode(state, Number.MAX_SAFE_INTEGER)
12
- t.alike(state, { start: 0, end: 13, buffer: null })
13
+ t.alike(state, enc.state(0, 13))
13
14
 
14
- state.buffer = Buffer.alloc(state.end)
15
+ state.buffer = b4a.alloc(state.end)
15
16
  enc.uint.encode(state, 42)
16
- t.alike(state, { start: 1, end: 13, buffer: Buffer.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
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, { start: 4, end: 13, buffer: Buffer.from([42, 0xfd, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
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, { start: 13, end: 13, buffer: Buffer.from([42, 0xfd, 104, 16, 0xff, 255, 255, 255, 255, 255, 255, 31, 0]) })
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)
@@ -32,15 +33,15 @@ tape('int', function (t) {
32
33
  const state = enc.state()
33
34
 
34
35
  enc.int.preencode(state, 42)
35
- t.alike(state, { start: 0, end: 1, buffer: null })
36
+ t.alike(state, enc.state(0, 1))
36
37
  enc.int.preencode(state, -4200)
37
- t.alike(state, { start: 0, end: 4, buffer: null })
38
+ t.alike(state, enc.state(0, 4))
38
39
 
39
- state.buffer = Buffer.alloc(state.end)
40
+ state.buffer = b4a.alloc(state.end)
40
41
  enc.int.encode(state, 42)
41
- t.alike(state, { start: 1, end: 4, buffer: Buffer.from([84, 0, 0, 0]) })
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, { start: 4, end: 4, buffer: Buffer.from([84, 0xfd, 207, 32]) })
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)
@@ -54,12 +55,12 @@ tape('float64', function (t) {
54
55
  const state = enc.state()
55
56
 
56
57
  enc.float64.preencode(state, 162.2377294)
57
- t.alike(state, { start: 0, end: 8, buffer: null })
58
+ t.alike(state, enc.state(0, 8))
58
59
 
59
- state.buffer = Buffer.alloc(state.end)
60
- t.alike(state, { start: 0, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) })
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, { start: 8, end: 8, buffer: Buffer.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
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, { start: 0, end: 9, buffer: null })
78
+ t.alike(state, enc.state(0, 9))
78
79
 
79
- state.buffer = Buffer.alloc(state.end)
80
- t.alike(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) })
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, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
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 = Buffer.alloc(10)
92
+ const buf = b4a.alloc(10)
92
93
  state.start = 0
93
94
  state.buffer = buf.subarray(1)
94
- t.alike(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) })
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, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
98
- t.alike(buf, Buffer.from([0, 0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))
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 = Buffer.alloc(state.end)
112
+ state.buffer = b4a.alloc(state.end)
112
113
  enc.float64.encode(state, 0)
113
- t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) })
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 = Buffer.alloc(state.end)
126
+ state.buffer = b4a.alloc(state.end)
126
127
  enc.float64.encode(state, Infinity)
127
- t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f]) })
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,9 +137,9 @@ tape('float64', function (t) {
136
137
  state.buffer = null
137
138
 
138
139
  enc.float64.preencode(state, 0.1 + 0.2)
139
- state.buffer = Buffer.alloc(state.end)
140
+ state.buffer = b4a.alloc(state.end)
140
141
  enc.float64.encode(state, 0.1 + 0.2)
141
- t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f]) })
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)
@@ -148,24 +149,24 @@ tape('float64', function (t) {
148
149
  tape('buffer', function (t) {
149
150
  const state = enc.state()
150
151
 
151
- enc.buffer.preencode(state, Buffer.from('hi'))
152
- t.alike(state, { start: 0, end: 3, buffer: null })
153
- enc.buffer.preencode(state, Buffer.from('hello'))
154
- t.alike(state, { start: 0, end: 9, buffer: null })
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, { start: 0, end: 10, buffer: null })
157
+ t.alike(state, enc.state(0, 10))
157
158
 
158
- state.buffer = Buffer.alloc(state.end)
159
- enc.buffer.encode(state, Buffer.from('hi'))
160
- t.alike(state, { start: 3, end: 10, buffer: Buffer.from('\x02hi\x00\x00\x00\x00\x00\x00\x00') })
161
- enc.buffer.encode(state, Buffer.from('hello'))
162
- t.alike(state, { start: 9, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') })
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, { start: 10, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') })
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), Buffer.from('hi'))
168
- t.alike(enc.buffer.decode(state), Buffer.from('hello'))
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
 
@@ -175,15 +176,15 @@ tape('buffer', function (t) {
175
176
  tape('raw', function (t) {
176
177
  const state = enc.state()
177
178
 
178
- enc.raw.preencode(state, Buffer.from('hi'))
179
- t.alike(state, { start: 0, end: 2, buffer: null })
179
+ enc.raw.preencode(state, b4a.from('hi'))
180
+ t.alike(state, enc.state(0, 2))
180
181
 
181
- state.buffer = Buffer.alloc(state.end)
182
- enc.raw.encode(state, Buffer.from('hi'))
183
- t.alike(state, { start: 2, end: 2, buffer: Buffer.from('hi') })
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), Buffer.from('hi'))
187
+ t.alike(enc.raw.decode(state), b4a.from('hi'))
187
188
  t.is(state.start, state.end)
188
189
  })
189
190
 
@@ -191,11 +192,11 @@ tape('uint16array', function (t) {
191
192
  const state = enc.state()
192
193
 
193
194
  enc.uint16array.preencode(state, new Uint16Array([1, 2, 3]))
194
- t.alike(state, { start: 0, end: 7, buffer: null })
195
+ t.alike(state, enc.state(0, 7))
195
196
 
196
- state.buffer = Buffer.alloc(state.end)
197
+ state.buffer = b4a.alloc(state.end)
197
198
  enc.uint16array.encode(state, new Uint16Array([1, 2, 3]))
198
- t.alike(state, { start: 7, end: 7, buffer: Buffer.from([3, 1, 0, 2, 0, 3, 0]) })
199
+ t.alike(state, enc.state(7, 7, b4a.from([3, 1, 0, 2, 0, 3, 0])))
199
200
 
200
201
  state.start = 0
201
202
  t.alike(enc.uint16array.decode(state), new Uint16Array([1, 2, 3]))
@@ -208,15 +209,15 @@ tape('uint32array', function (t) {
208
209
  const state = enc.state()
209
210
 
210
211
  enc.uint32array.preencode(state, new Uint32Array([1]))
211
- t.alike(state, { start: 0, end: 5, buffer: null })
212
+ t.alike(state, enc.state(0, 5))
212
213
  enc.uint32array.preencode(state, new Uint32Array([42, 43]))
213
- t.alike(state, { start: 0, end: 14, buffer: null })
214
+ t.alike(state, enc.state(0, 14))
214
215
 
215
- state.buffer = Buffer.alloc(state.end)
216
+ state.buffer = b4a.alloc(state.end)
216
217
  enc.uint32array.encode(state, new Uint32Array([1]))
217
- t.alike(state, { start: 5, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
218
+ t.alike(state, enc.state(5, 14, b4a.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])))
218
219
  enc.uint32array.encode(state, new Uint32Array([42, 43]))
219
- t.alike(state, { start: 14, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0]) })
220
+ t.alike(state, enc.state(14, 14, b4a.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0])))
220
221
 
221
222
  state.start = 0
222
223
  t.alike(enc.uint32array.decode(state), new Uint32Array([1]))
@@ -230,11 +231,11 @@ tape('int16array', function (t) {
230
231
  const state = enc.state()
231
232
 
232
233
  enc.int16array.preencode(state, new Int16Array([1, -2, 3]))
233
- t.alike(state, { start: 0, end: 7, buffer: null })
234
+ t.alike(state, enc.state(0, 7))
234
235
 
235
- state.buffer = Buffer.alloc(state.end)
236
+ state.buffer = b4a.alloc(state.end)
236
237
  enc.int16array.encode(state, new Int16Array([1, -2, 3]))
237
- t.alike(state, { start: 7, end: 7, buffer: Buffer.from([3, 1, 0, 0xfe, 0xff, 3, 0]) })
238
+ t.alike(state, enc.state(7, 7, b4a.from([3, 1, 0, 0xfe, 0xff, 3, 0])))
238
239
 
239
240
  state.start = 0
240
241
  t.alike(enc.int16array.decode(state), new Int16Array([1, -2, 3]))
@@ -247,11 +248,11 @@ tape('int32array', function (t) {
247
248
  const state = enc.state()
248
249
 
249
250
  enc.int32array.preencode(state, new Int32Array([1, -2, 3]))
250
- t.alike(state, { start: 0, end: 13, buffer: null })
251
+ t.alike(state, enc.state(0, 13))
251
252
 
252
- state.buffer = Buffer.alloc(state.end)
253
+ state.buffer = b4a.alloc(state.end)
253
254
  enc.int32array.encode(state, new Int32Array([1, -2, 3]))
254
- t.alike(state, { start: 13, end: 13, buffer: Buffer.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0]) })
255
+ t.alike(state, enc.state(13, 13, b4a.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0])))
255
256
 
256
257
  state.start = 0
257
258
  t.alike(enc.int32array.decode(state), new Int32Array([1, -2, 3]))
@@ -264,11 +265,11 @@ tape('float32array', function (t) {
264
265
  const state = enc.state()
265
266
 
266
267
  enc.float32array.preencode(state, new Float32Array([1.1, -2.2, 3.3]))
267
- t.alike(state, { start: 0, end: 13, buffer: null })
268
+ t.alike(state, enc.state(0, 13))
268
269
 
269
- state.buffer = Buffer.alloc(state.end)
270
+ state.buffer = b4a.alloc(state.end)
270
271
  enc.float32array.encode(state, new Float32Array([1.1, -2.2, 3.3]))
271
- t.alike(state, { start: 13, end: 13, buffer: Buffer.from([3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53, 0x40]) })
272
+ t.alike(state, enc.state(13, 13, b4a.from([3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53, 0x40])))
272
273
 
273
274
  state.start = 0
274
275
  t.alike(enc.float32array.decode(state), new Float32Array([1.1, -2.2, 3.3]))
@@ -281,11 +282,11 @@ tape('float64array', function (t) {
281
282
  const state = enc.state()
282
283
 
283
284
  enc.float64array.preencode(state, new Float64Array([1.1, -2.2, 3.3]))
284
- t.alike(state, { start: 0, end: 25, buffer: null })
285
+ t.alike(state, enc.state(0, 25))
285
286
 
286
- state.buffer = Buffer.alloc(state.end)
287
+ state.buffer = b4a.alloc(state.end)
287
288
  enc.float64array.encode(state, new Float64Array([1.1, -2.2, 3.3]))
288
- t.alike(state, { start: 25, end: 25, buffer: Buffer.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
+ 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
290
 
290
291
  state.start = 0
291
292
  t.alike(enc.float64array.decode(state), new Float64Array([1.1, -2.2, 3.3]))
@@ -298,15 +299,15 @@ tape('string', function (t) {
298
299
  const state = enc.state()
299
300
 
300
301
  enc.string.preencode(state, '🌾')
301
- t.alike(state, { start: 0, end: 5, buffer: null })
302
+ t.alike(state, enc.state(0, 5))
302
303
  enc.string.preencode(state, 'høsten er fin')
303
- t.alike(state, { start: 0, end: 20, buffer: null })
304
+ t.alike(state, enc.state(0, 20))
304
305
 
305
- state.buffer = Buffer.alloc(state.end)
306
+ state.buffer = b4a.alloc(state.end)
306
307
  enc.string.encode(state, '🌾')
307
- t.alike(state, { start: 5, end: 20, buffer: Buffer.from('\x04🌾\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') })
308
+ 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
309
  enc.string.encode(state, 'høsten er fin')
309
- t.alike(state, { start: 20, end: 20, buffer: Buffer.from('\x04🌾\x0ehøsten er fin') })
310
+ t.alike(state, enc.state(20, 20, b4a.from('\x04🌾\x0ehøsten er fin')))
310
311
 
311
312
  state.start = 0
312
313
  t.is(enc.string.decode(state), '🌾')
@@ -319,20 +320,20 @@ tape('string', function (t) {
319
320
  tape('fixed32', function (t) {
320
321
  const state = enc.state()
321
322
 
322
- enc.fixed32.preencode(state, Buffer.alloc(32).fill('a'))
323
- t.alike(state, { start: 0, end: 32, buffer: null })
324
- enc.fixed32.preencode(state, Buffer.alloc(32).fill('b'))
325
- t.alike(state, { start: 0, end: 64, buffer: null })
323
+ enc.fixed32.preencode(state, b4a.alloc(32).fill('a'))
324
+ t.alike(state, enc.state(0, 32))
325
+ enc.fixed32.preencode(state, b4a.alloc(32).fill('b'))
326
+ t.alike(state, enc.state(0, 64))
326
327
 
327
- state.buffer = Buffer.alloc(state.end)
328
- enc.fixed32.encode(state, Buffer.alloc(32).fill('a'))
329
- t.alike(state, { start: 32, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32) })
330
- enc.fixed32.encode(state, Buffer.alloc(32).fill('b'))
331
- t.alike(state, { start: 64, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32).fill('b', 32, 64) })
328
+ state.buffer = b4a.alloc(state.end)
329
+ enc.fixed32.encode(state, b4a.alloc(32).fill('a'))
330
+ t.alike(state, enc.state(32, 64, b4a.alloc(64).fill('a', 0, 32)))
331
+ enc.fixed32.encode(state, b4a.alloc(32).fill('b'))
332
+ t.alike(state, enc.state(64, 64, b4a.alloc(64).fill('a', 0, 32).fill('b', 32, 64)))
332
333
 
333
334
  state.start = 0
334
- t.alike(enc.fixed32.decode(state), Buffer.alloc(32).fill('a'))
335
- t.alike(enc.fixed32.decode(state), Buffer.alloc(32).fill('b'))
335
+ t.alike(enc.fixed32.decode(state), b4a.alloc(32).fill('a'))
336
+ t.alike(enc.fixed32.decode(state), b4a.alloc(32).fill('b'))
336
337
  t.is(state.start, state.end)
337
338
 
338
339
  t.exception(() => enc.fixed32.decode(state))
@@ -341,20 +342,20 @@ tape('fixed32', function (t) {
341
342
  tape('fixed64', function (t) {
342
343
  const state = enc.state()
343
344
 
344
- enc.fixed64.preencode(state, Buffer.alloc(64).fill('a'))
345
- t.alike(state, { start: 0, end: 64, buffer: null })
346
- enc.fixed64.preencode(state, Buffer.alloc(64).fill('b'))
347
- t.alike(state, { start: 0, end: 128, buffer: null })
345
+ enc.fixed64.preencode(state, b4a.alloc(64).fill('a'))
346
+ t.alike(state, enc.state(0, 64))
347
+ enc.fixed64.preencode(state, b4a.alloc(64).fill('b'))
348
+ t.alike(state, enc.state(0, 128))
348
349
 
349
- state.buffer = Buffer.alloc(state.end)
350
- enc.fixed64.encode(state, Buffer.alloc(64).fill('a'))
351
- t.alike(state, { start: 64, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64) })
352
- enc.fixed64.encode(state, Buffer.alloc(64).fill('b'))
353
- t.alike(state, { start: 128, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64).fill('b', 64, 128) })
350
+ state.buffer = b4a.alloc(state.end)
351
+ enc.fixed64.encode(state, b4a.alloc(64).fill('a'))
352
+ t.alike(state, enc.state(64, 128, b4a.alloc(128).fill('a', 0, 64)))
353
+ enc.fixed64.encode(state, b4a.alloc(64).fill('b'))
354
+ t.alike(state, enc.state(128, 128, b4a.alloc(128).fill('a', 0, 64).fill('b', 64, 128)))
354
355
 
355
356
  state.start = 0
356
- t.alike(enc.fixed64.decode(state), Buffer.alloc(64).fill('a'))
357
- t.alike(enc.fixed64.decode(state), Buffer.alloc(64).fill('b'))
357
+ t.alike(enc.fixed64.decode(state), b4a.alloc(64).fill('a'))
358
+ t.alike(enc.fixed64.decode(state), b4a.alloc(64).fill('b'))
358
359
  t.is(state.start, state.end)
359
360
 
360
361
  t.exception(() => enc.fixed64.decode(state))
@@ -364,20 +365,20 @@ tape('fixed n', function (t) {
364
365
  const state = enc.state()
365
366
  const fixed = enc.fixed(3)
366
367
 
367
- fixed.preencode(state, Buffer.alloc(3).fill('a'))
368
- t.alike(state, { start: 0, end: 3, buffer: null })
369
- fixed.preencode(state, Buffer.alloc(3).fill('b'))
370
- t.alike(state, { start: 0, end: 6, buffer: null })
368
+ fixed.preencode(state, b4a.alloc(3).fill('a'))
369
+ t.alike(state, enc.state(0, 3))
370
+ fixed.preencode(state, b4a.alloc(3).fill('b'))
371
+ t.alike(state, enc.state(0, 6))
371
372
 
372
- state.buffer = Buffer.alloc(state.end)
373
- fixed.encode(state, Buffer.alloc(3).fill('a'))
374
- t.alike(state, { start: 3, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3) })
375
- fixed.encode(state, Buffer.alloc(3).fill('b'))
376
- t.alike(state, { start: 6, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3).fill('b', 3, 6) })
373
+ state.buffer = b4a.alloc(state.end)
374
+ fixed.encode(state, b4a.alloc(3).fill('a'))
375
+ t.alike(state, enc.state(3, 6, b4a.alloc(6).fill('a', 0, 3)))
376
+ fixed.encode(state, b4a.alloc(3).fill('b'))
377
+ t.alike(state, enc.state(6, 6, b4a.alloc(6).fill('a', 0, 3).fill('b', 3, 6)))
377
378
 
378
379
  state.start = 0
379
- t.alike(fixed.decode(state), Buffer.alloc(3).fill('a'))
380
- t.alike(fixed.decode(state), Buffer.alloc(3).fill('b'))
380
+ t.alike(fixed.decode(state), b4a.alloc(3).fill('a'))
381
+ t.alike(fixed.decode(state), b4a.alloc(3).fill('b'))
381
382
  t.is(state.start, state.end)
382
383
 
383
384
  t.exception(() => fixed.decode(state))
@@ -390,15 +391,15 @@ tape('array', function (t) {
390
391
  const arr = enc.array(enc.bool)
391
392
 
392
393
  arr.preencode(state, [true, false, true])
393
- t.alike(state, { start: 0, end: 4, buffer: null })
394
+ t.alike(state, enc.state(0, 4))
394
395
  arr.preencode(state, [false, false, true, true])
395
- t.alike(state, { start: 0, end: 9, buffer: null })
396
+ t.alike(state, enc.state(0, 9))
396
397
 
397
- state.buffer = Buffer.alloc(state.end)
398
+ state.buffer = b4a.alloc(state.end)
398
399
  arr.encode(state, [true, false, true])
399
- t.alike(state, { start: 4, end: 9, buffer: Buffer.from([3, 1, 0, 1, 0, 0, 0, 0, 0]) })
400
+ t.alike(state, enc.state(4, 9, b4a.from([3, 1, 0, 1, 0, 0, 0, 0, 0])))
400
401
  arr.encode(state, [false, false, true, true])
401
- t.alike(state, { start: 9, end: 9, buffer: Buffer.from([3, 1, 0, 1, 4, 0, 0, 1, 1]) })
402
+ t.alike(state, enc.state(9, 9, b4a.from([3, 1, 0, 1, 4, 0, 0, 1, 1])))
402
403
 
403
404
  state.start = 0
404
405
  t.alike(arr.decode(state), [true, false, true])
@@ -408,6 +409,25 @@ tape('array', function (t) {
408
409
  t.exception(() => arr.decode(state))
409
410
  })
410
411
 
412
+ tape('json', function (t) {
413
+ const state = enc.state()
414
+
415
+ enc.json.preencode(state, { a: 1, b: 2 })
416
+ t.alike(state, enc.state(0, 14))
417
+
418
+ state.buffer = b4a.alloc(state.end)
419
+ enc.json.encode(state, { a: 1, b: 2 })
420
+ t.alike(state, enc.state(14, 14, b4a.concat([
421
+ b4a.from([13]),
422
+ b4a.from('{"a":1,"b":2}')
423
+ ])))
424
+
425
+ state.start = 0
426
+ t.alike(enc.json.decode(state), { a: 1, b: 2 })
427
+
428
+ t.exception(() => enc.json.decode(state))
429
+ })
430
+
411
431
  tape('lexint: big numbers', function (t) {
412
432
  t.plan(1)
413
433
 
@@ -418,7 +438,7 @@ tape('lexint: big numbers', function (t) {
418
438
 
419
439
  for (n = 1; n < Number.MAX_VALUE; n += skip) {
420
440
  const cur = enc.encode(enc.lexint, n)
421
- if (Buffer.compare(cur, prev) < 1) break
441
+ if (b4a.compare(cur, prev) < 1) break
422
442
  prev = cur
423
443
  skip = 1 + Math.pow(245, Math.ceil(Math.log(n) / Math.log(256)))
424
444
  }
@@ -440,7 +460,7 @@ tape('lexint: range precision', function (t) {
440
460
  const skip = 0.000000001e55
441
461
  for (let i = 0, n = 1e55; i < 1000; n = 1e55 + skip * ++i) {
442
462
  const cur = enc.encode(enc.lexint, n)
443
- if (Buffer.compare(cur, prev) < 1) t.fail('cur <= prev')
463
+ if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev')
444
464
  prev = cur
445
465
  }
446
466
  t.ok(true)
@@ -451,7 +471,7 @@ tape('lexint: small numbers', function (t) {
451
471
  let prev = enc.encode(enc.lexint, 0)
452
472
  for (let n = 1; n < 256 * 256 * 16; n++) {
453
473
  const cur = enc.encode(enc.lexint, n)
454
- if (Buffer.compare(cur, prev) < 1) t.fail('cur <= prev')
474
+ if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev')
455
475
  prev = cur
456
476
  }
457
477
  t.ok(true)
@@ -460,19 +480,15 @@ tape('lexint: small numbers', function (t) {
460
480
 
461
481
  tape('lexint: throws', function (t) {
462
482
  t.exception(() => {
463
- enc.decode(enc.lexint, Buffer.alloc(1, 251))
483
+ enc.decode(enc.lexint, b4a.alloc(1, 251))
464
484
  })
465
485
 
466
486
  let num = 252
467
487
 
468
- const state = {
469
- start: 0,
470
- end: 0,
471
- buffer: null
472
- }
488
+ const state = enc.state()
473
489
 
474
490
  enc.lexint.preencode(state, num)
475
- state.buffer = Buffer.alloc(state.end - state.start)
491
+ state.buffer = b4a.alloc(state.end - state.start)
476
492
  enc.lexint.encode(state, num)
477
493
 
478
494
  t.exception(() => {
@@ -486,7 +502,7 @@ tape('lexint: throws', function (t) {
486
502
  state.buffer = null
487
503
 
488
504
  enc.lexint.preencode(state, num)
489
- state.buffer = Buffer.alloc(state.end - state.start)
505
+ state.buffer = b4a.alloc(state.end - state.start)
490
506
  enc.lexint.encode(state, num)
491
507
 
492
508
  t.exception(() => {
@@ -500,7 +516,7 @@ tape('lexint: throws', function (t) {
500
516
  state.buffer = null
501
517
 
502
518
  enc.lexint.preencode(state, num)
503
- state.buffer = Buffer.alloc(state.end - state.start)
519
+ state.buffer = b4a.alloc(state.end - state.start)
504
520
  enc.lexint.encode(state, num)
505
521
 
506
522
  t.exception(() => {
@@ -514,7 +530,7 @@ tape('lexint: throws', function (t) {
514
530
  state.buffer = null
515
531
 
516
532
  enc.lexint.preencode(state, num)
517
- state.buffer = Buffer.alloc(state.end - state.start)
533
+ state.buffer = b4a.alloc(state.end - state.start)
518
534
  enc.lexint.encode(state, num)
519
535
 
520
536
  t.exception(() => {
@@ -528,7 +544,7 @@ tape('lexint: throws', function (t) {
528
544
  state.buffer = null
529
545
 
530
546
  enc.lexint.preencode(state, num)
531
- state.buffer = Buffer.alloc(state.end - state.start)
547
+ state.buffer = b4a.alloc(state.end - state.start)
532
548
  enc.lexint.encode(state, num)
533
549
 
534
550
  t.exception(() => {