compact-encoding 2.9.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 +5 -0
  2. package/index.js +15 -0
  3. package/package.json +1 -1
  4. package/test.js +98 -97
package/README.md CHANGED
@@ -108,10 +108,15 @@ to build others on top. Feel free to PR more that are missing.
108
108
  * `cenc.float64array` - Encodes a float64array with its element length uint prefixed.
109
109
  * `cenc.bool` - Encodes a boolean as 1 or 0.
110
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.
111
112
  * `cenc.ascii` - Encodes an ascii string.
113
+ * `cenc.ascii.fixed(n)` - Encodes a fixed size ascii string.
112
114
  * `cenc.hex` - Encodes a hex string.
115
+ * `cenc.hex.fixed(n)` - Encodes a fixed size hex string.
113
116
  * `cenc.base64` - Encodes a base64 string.
117
+ * `cenc.base64.fixed(n)` - Encodes a fixed size base64 string.
114
118
  * `cenc.utf16le`, `cenc.ucs2` - Encodes a utf16le string.
119
+ * `cenc.utf16le.fixed(n)`, `cenc.ucs2.fixed(n)` - Encodes a fixed size utf16le string.
115
120
  * `cenc.fixed32` - Encodes a fixed 32 byte buffer.
116
121
  * `cenc.fixed64` - Encodes a fixed 64 byte buffer.
117
122
  * `cenc.fixed(n)` - Makes a fixed sized encoder.
package/index.js CHANGED
@@ -303,6 +303,21 @@ 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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.9.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,5 +1,6 @@
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()
@@ -11,13 +12,13 @@ tape('uint', function (t) {
11
12
  enc.uint.preencode(state, Number.MAX_SAFE_INTEGER)
12
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, enc.state(1, 13, 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, enc.state(4, 13, 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, enc.state(13, 13, 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)
@@ -36,11 +37,11 @@ tape('int', function (t) {
36
37
  enc.int.preencode(state, -4200)
37
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, enc.state(1, 4, 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, enc.state(4, 4, 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)
@@ -56,10 +57,10 @@ tape('float64', function (t) {
56
57
  enc.float64.preencode(state, 162.2377294)
57
58
  t.alike(state, enc.state(0, 8))
58
59
 
59
- state.buffer = Buffer.alloc(state.end)
60
- t.alike(state, enc.state(0, 8, 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, enc.state(8, 8, 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)
@@ -76,11 +77,11 @@ tape('float64', function (t) {
76
77
  enc.float64.preencode(state, 162.2377294)
77
78
  t.alike(state, enc.state(0, 9))
78
79
 
79
- state.buffer = Buffer.alloc(state.end)
80
- t.alike(state, enc.state(0, 9, 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, enc.state(9, 9, 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, enc.state(0, 9, 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, enc.state(9, 9, 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, enc.state(8, 8, 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, enc.state(8, 8, 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, enc.state(8, 8, 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
+ enc.buffer.preencode(state, b4a.from('hi'))
152
153
  t.alike(state, enc.state(0, 3))
153
- enc.buffer.preencode(state, Buffer.from('hello'))
154
+ enc.buffer.preencode(state, b4a.from('hello'))
154
155
  t.alike(state, enc.state(0, 9))
155
156
  enc.buffer.preencode(state, null)
156
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, enc.state(3, 10, Buffer.from('\x02hi\x00\x00\x00\x00\x00\x00\x00')))
161
- enc.buffer.encode(state, Buffer.from('hello'))
162
- t.alike(state, enc.state(9, 10, 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, enc.state(10, 10, 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
+ enc.raw.preencode(state, b4a.from('hi'))
179
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, enc.state(2, 2, 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
 
@@ -193,9 +194,9 @@ tape('uint16array', function (t) {
193
194
  enc.uint16array.preencode(state, new Uint16Array([1, 2, 3]))
194
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, enc.state(7, 7, 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]))
@@ -212,11 +213,11 @@ tape('uint32array', function (t) {
212
213
  enc.uint32array.preencode(state, new Uint32Array([42, 43]))
213
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, enc.state(5, 14, 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, enc.state(14, 14, 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]))
@@ -232,9 +233,9 @@ tape('int16array', function (t) {
232
233
  enc.int16array.preencode(state, new Int16Array([1, -2, 3]))
233
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, enc.state(7, 7, 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]))
@@ -249,9 +250,9 @@ tape('int32array', function (t) {
249
250
  enc.int32array.preencode(state, new Int32Array([1, -2, 3]))
250
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, enc.state(13, 13, 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]))
@@ -266,9 +267,9 @@ tape('float32array', function (t) {
266
267
  enc.float32array.preencode(state, new Float32Array([1.1, -2.2, 3.3]))
267
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, enc.state(13, 13, 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]))
@@ -283,9 +284,9 @@ tape('float64array', function (t) {
283
284
  enc.float64array.preencode(state, new Float64Array([1.1, -2.2, 3.3]))
284
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, enc.state(25, 25, 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]))
@@ -302,11 +303,11 @@ tape('string', function (t) {
302
303
  enc.string.preencode(state, 'høsten er fin')
303
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, enc.state(5, 20, 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, enc.state(20, 20, 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
+ enc.fixed32.preencode(state, b4a.alloc(32).fill('a'))
323
324
  t.alike(state, enc.state(0, 32))
324
- enc.fixed32.preencode(state, Buffer.alloc(32).fill('b'))
325
+ enc.fixed32.preencode(state, b4a.alloc(32).fill('b'))
325
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, enc.state(32, 64, Buffer.alloc(64).fill('a', 0, 32)))
330
- enc.fixed32.encode(state, Buffer.alloc(32).fill('b'))
331
- t.alike(state, enc.state(64, 64, 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
+ enc.fixed64.preencode(state, b4a.alloc(64).fill('a'))
345
346
  t.alike(state, enc.state(0, 64))
346
- enc.fixed64.preencode(state, Buffer.alloc(64).fill('b'))
347
+ enc.fixed64.preencode(state, b4a.alloc(64).fill('b'))
347
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, enc.state(64, 128, Buffer.alloc(128).fill('a', 0, 64)))
352
- enc.fixed64.encode(state, Buffer.alloc(64).fill('b'))
353
- t.alike(state, enc.state(128, 128, 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
+ fixed.preencode(state, b4a.alloc(3).fill('a'))
368
369
  t.alike(state, enc.state(0, 3))
369
- fixed.preencode(state, Buffer.alloc(3).fill('b'))
370
+ fixed.preencode(state, b4a.alloc(3).fill('b'))
370
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, enc.state(3, 6, Buffer.alloc(6).fill('a', 0, 3)))
375
- fixed.encode(state, Buffer.alloc(3).fill('b'))
376
- t.alike(state, enc.state(6, 6, 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))
@@ -394,11 +395,11 @@ tape('array', function (t) {
394
395
  arr.preencode(state, [false, false, true, true])
395
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, enc.state(4, 9, 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, enc.state(9, 9, 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])
@@ -414,11 +415,11 @@ tape('json', function (t) {
414
415
  enc.json.preencode(state, { a: 1, b: 2 })
415
416
  t.alike(state, enc.state(0, 14))
416
417
 
417
- state.buffer = Buffer.alloc(state.end)
418
+ state.buffer = b4a.alloc(state.end)
418
419
  enc.json.encode(state, { a: 1, b: 2 })
419
- t.alike(state, enc.state(14, 14, Buffer.concat([
420
- Buffer.from([13]),
421
- Buffer.from('{"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}')
422
423
  ])))
423
424
 
424
425
  state.start = 0
@@ -437,7 +438,7 @@ tape('lexint: big numbers', function (t) {
437
438
 
438
439
  for (n = 1; n < Number.MAX_VALUE; n += skip) {
439
440
  const cur = enc.encode(enc.lexint, n)
440
- if (Buffer.compare(cur, prev) < 1) break
441
+ if (b4a.compare(cur, prev) < 1) break
441
442
  prev = cur
442
443
  skip = 1 + Math.pow(245, Math.ceil(Math.log(n) / Math.log(256)))
443
444
  }
@@ -459,7 +460,7 @@ tape('lexint: range precision', function (t) {
459
460
  const skip = 0.000000001e55
460
461
  for (let i = 0, n = 1e55; i < 1000; n = 1e55 + skip * ++i) {
461
462
  const cur = enc.encode(enc.lexint, n)
462
- if (Buffer.compare(cur, prev) < 1) t.fail('cur <= prev')
463
+ if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev')
463
464
  prev = cur
464
465
  }
465
466
  t.ok(true)
@@ -470,7 +471,7 @@ tape('lexint: small numbers', function (t) {
470
471
  let prev = enc.encode(enc.lexint, 0)
471
472
  for (let n = 1; n < 256 * 256 * 16; n++) {
472
473
  const cur = enc.encode(enc.lexint, n)
473
- if (Buffer.compare(cur, prev) < 1) t.fail('cur <= prev')
474
+ if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev')
474
475
  prev = cur
475
476
  }
476
477
  t.ok(true)
@@ -479,7 +480,7 @@ tape('lexint: small numbers', function (t) {
479
480
 
480
481
  tape('lexint: throws', function (t) {
481
482
  t.exception(() => {
482
- enc.decode(enc.lexint, Buffer.alloc(1, 251))
483
+ enc.decode(enc.lexint, b4a.alloc(1, 251))
483
484
  })
484
485
 
485
486
  let num = 252
@@ -487,7 +488,7 @@ tape('lexint: throws', function (t) {
487
488
  const state = enc.state()
488
489
 
489
490
  enc.lexint.preencode(state, num)
490
- state.buffer = Buffer.alloc(state.end - state.start)
491
+ state.buffer = b4a.alloc(state.end - state.start)
491
492
  enc.lexint.encode(state, num)
492
493
 
493
494
  t.exception(() => {
@@ -501,7 +502,7 @@ tape('lexint: throws', function (t) {
501
502
  state.buffer = null
502
503
 
503
504
  enc.lexint.preencode(state, num)
504
- state.buffer = Buffer.alloc(state.end - state.start)
505
+ state.buffer = b4a.alloc(state.end - state.start)
505
506
  enc.lexint.encode(state, num)
506
507
 
507
508
  t.exception(() => {
@@ -515,7 +516,7 @@ tape('lexint: throws', function (t) {
515
516
  state.buffer = null
516
517
 
517
518
  enc.lexint.preencode(state, num)
518
- state.buffer = Buffer.alloc(state.end - state.start)
519
+ state.buffer = b4a.alloc(state.end - state.start)
519
520
  enc.lexint.encode(state, num)
520
521
 
521
522
  t.exception(() => {
@@ -529,7 +530,7 @@ tape('lexint: throws', function (t) {
529
530
  state.buffer = null
530
531
 
531
532
  enc.lexint.preencode(state, num)
532
- state.buffer = Buffer.alloc(state.end - state.start)
533
+ state.buffer = b4a.alloc(state.end - state.start)
533
534
  enc.lexint.encode(state, num)
534
535
 
535
536
  t.exception(() => {
@@ -543,7 +544,7 @@ tape('lexint: throws', function (t) {
543
544
  state.buffer = null
544
545
 
545
546
  enc.lexint.preencode(state, num)
546
- state.buffer = Buffer.alloc(state.end - state.start)
547
+ state.buffer = b4a.alloc(state.end - state.start)
547
548
  enc.lexint.encode(state, num)
548
549
 
549
550
  t.exception(() => {