compact-encoding 2.4.0 → 2.6.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.
@@ -1,5 +1,4 @@
1
1
  name: Build Status
2
-
3
2
  on:
4
3
  push:
5
4
  branches:
@@ -11,13 +10,13 @@ jobs:
11
10
  build:
12
11
  strategy:
13
12
  matrix:
14
- node-version: [14.x]
15
- os: [ubuntu-16.04, macos-latest, windows-latest]
13
+ node-version: [lts/*]
14
+ os: [ubuntu-latest, macos-latest, windows-latest]
16
15
  runs-on: ${{ matrix.os }}
17
16
  steps:
18
17
  - uses: actions/checkout@v2
19
18
  - name: Use Node.js ${{ matrix.node-version }}
20
- uses: actions/setup-node@v1
19
+ uses: actions/setup-node@v2
21
20
  with:
22
21
  node-version: ${{ matrix.node-version }}
23
22
  - run: npm install
package/README.md CHANGED
@@ -40,6 +40,10 @@ Should be an object that looks like this `{ start, end, buffer }`.
40
40
 
41
41
  You can also get a blank state object using `cenc.state()`.
42
42
 
43
+ * `start` is the byte offset to start encoding/decoding at.
44
+ * `end` is the byte offset indicating the end of the buffer.
45
+ * `buffer` should be either a Node.js Buffer or Uint8Array.
46
+
43
47
  #### `enc.preencode(state, val)`
44
48
 
45
49
  Does a fast preencode dry-run that only sets state.end.
@@ -70,21 +74,37 @@ const bool = cenc.decode(cenc.bool, buf)
70
74
  The following encodings are bundled as they are primitives that can be used
71
75
  to build others on top. Feel free to PR more that are missing.
72
76
 
73
- * `cenc.uint` - Encodes a uint using [compact-uint](https://github.com/mafintosh/compact-uint)
74
- * `cenc.int` - Encodes an int using [compact-uint](https://github.com/mafintosh/compact-uint) as a signed int using ZigZag encoding.
75
- * `cenc.uint16` - Encodes a fixed size uint16 (useful for things like ports)
76
- * `cenc.uint24` - Encodes a fixed size uint24 (useful for message framing)
77
- * `cenc.uint32` - Encodes a fixes size uint32 (useful for very large message framing)
78
- * `cenc.buffer` - Encodes a buffer with it's length uint prefixed. When decoding an empty buf, null is returned.
79
- * `cenc.raw` - Pass through encodes a buffer - ie a basic copy.
80
- * `cenc.uint32array` - Encodes a uint32array with it's element length uint32 prefixed.
77
+ * `cenc.uint` - Encodes a uint using [compact-uint](https://github.com/mafintosh/compact-uint).
78
+ * `cenc.uint8` - Encodes a fixed size uint8.
79
+ * `cenc.uint16` - Encodes a fixed size uint16. Useful for things like ports.
80
+ * `cenc.uint24` - Encodes a fixed size uint24. Useful for message framing.
81
+ * `cenc.uint32` - Encodes a fixed size uint32. Useful for very large message framing.
82
+ * `cenc.uint64` - Encodes a fixed size uint64.
83
+ * `cenc.int` - Encodes an int using `cenc.uint` with ZigZag encoding.
84
+ * `cenc.int8` - Encodes a fixed size int8 using `cenc.uint8` with ZigZag encoding.
85
+ * `cenc.int16` - Encodes a fixed size int16 using `cenc.uint16` with ZigZag encoding.
86
+ * `cenc.int24` - Encodes a fixed size int24 using `cenc.uint24` with ZigZag encoding.
87
+ * `cenc.int32` - Encodes a fixed size int32 using `cenc.uint32` with ZigZag encoding.
88
+ * `cenc.int64` - Encodes a fixed size int64 using `cenc.uint64` with ZigZag encoding.
89
+ * `cenc.float32` - Encodes a fixed size float32.
90
+ * `cenc.float64` - Encodes a fixed size float64.
91
+ * `cenc.buffer` - Encodes a buffer with its length uint prefixed. When decoding an empty buffer, `null` is returned.
92
+ * `cenc.raw` - Pass through encodes a buffer, i.e. a basic copy.
93
+ * `cenc.uint8array` - Encodes a uint8array with its element length uint prefixed.
94
+ * `cenc.uint16array` - Encodes a uint16array with its element length uint prefixed.
95
+ * `cenc.uint32array` - Encodes a uint32array with its element length uint prefixed.
96
+ * `cenc.int8array` - Encodes a int8array with its element length uint prefixed.
97
+ * `cenc.int16array` - Encodes a int16array with its element length uint prefixed.
98
+ * `cenc.int32array` - Encodes a int32array with its element length uint prefixed.
99
+ * `cenc.float32array` - Encodes a float32array with its element length uint prefixed.
100
+ * `cenc.float64array` - Encodes a float64array with its element length uint prefixed.
81
101
  * `cenc.bool` - Encodes a boolean as 1 or 0.
82
102
  * `cenc.string` - Encodes a utf-8 string, similar to buffer.
83
103
  * `cenc.fixed32` - Encodes a fixed 32 byte buffer.
84
104
  * `cenc.fixed64` - Encodes a fixed 64 byte buffer.
85
105
  * `cenc.fixed(n)` - Makes a fixed sized encoder.
86
106
  * `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
87
- * `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding)
107
+ * `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding).
88
108
 
89
109
  ## License
90
110
 
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ const b4a = require('b4a')
2
+
1
3
  const LE = (new Uint8Array(new Uint16Array([255]).buffer))[0] === 0xff
2
4
  const BE = !LE
3
5
 
@@ -10,34 +12,41 @@ const uint = exports.uint = {
10
12
  state.end += n <= 0xfc ? 1 : n <= 0xffff ? 3 : n <= 0xffffffff ? 5 : 9
11
13
  },
12
14
  encode (state, n) {
13
- if (n <= 0xfc) state.buffer[state.start++] = n
14
- else if (n <= 0xffff) encode16(state, n)
15
- else if (n <= 0xffffffff) encode32(state, n)
16
- else encode64(state, n)
15
+ if (n <= 0xfc) uint8.encode(state, n)
16
+ else if (n <= 0xffff) {
17
+ state.buffer[state.start++] = 0xfd
18
+ uint16.encode(state, n)
19
+ } else if (n <= 0xffffffff) {
20
+ state.buffer[state.start++] = 0xfe
21
+ uint32.encode(state, n)
22
+ } else {
23
+ state.buffer[state.start++] = 0xff
24
+ uint64.encode(state, n)
25
+ }
17
26
  },
18
27
  decode (state) {
19
- if (state.start >= state.buffer.length) throw new Error('Out of bounds')
20
- const a = state.buffer[state.start++]
28
+ const a = uint8.decode(state)
21
29
  if (a <= 0xfc) return a
22
- if (a === 0xfd) return decode16(state)
23
- if (a === 0xfe) return decode32(state)
24
- return decode64(state)
30
+ if (a === 0xfd) return uint16.decode(state)
31
+ if (a === 0xfe) return uint32.decode(state)
32
+ return uint64.decode(state)
25
33
  }
26
34
  }
27
35
 
28
- exports.int = {
36
+ const uint8 = exports.uint8 = {
29
37
  preencode (state, n) {
30
- uint.preencode(state, zigzagEncode(n))
38
+ state.end += 1
31
39
  },
32
40
  encode (state, n) {
33
- uint.encode(state, zigzagEncode(n))
41
+ state.buffer[state.start++] = n
34
42
  },
35
43
  decode (state) {
36
- return zigzagDecode(uint.decode(state))
44
+ if (state.start >= state.end) throw new Error('Out of bounds')
45
+ return state.buffer[state.start++]
37
46
  }
38
47
  }
39
48
 
40
- exports.uint16 = {
49
+ const uint16 = exports.uint16 = {
41
50
  preencode (state, n) {
42
51
  state.end += 2
43
52
  },
@@ -47,11 +56,14 @@ exports.uint16 = {
47
56
  },
48
57
  decode (state) {
49
58
  if (state.end - state.start < 2) throw new Error('Out of bounds')
50
- return state.buffer[state.start++] + state.buffer[state.start++] * 256
59
+ return (
60
+ state.buffer[state.start++] +
61
+ state.buffer[state.start++] * 256
62
+ )
51
63
  }
52
64
  }
53
65
 
54
- exports.uint24 = {
66
+ const uint24 = exports.uint24 = {
55
67
  preencode (state, n) {
56
68
  state.end += 3
57
69
  },
@@ -62,11 +74,15 @@ exports.uint24 = {
62
74
  },
63
75
  decode (state) {
64
76
  if (state.end - state.start < 3) throw new Error('Out of bounds')
65
- return state.buffer[state.start++] + state.buffer[state.start++] * 256 + state.buffer[state.start++] * 65536
77
+ return (
78
+ state.buffer[state.start++] +
79
+ state.buffer[state.start++] * 256 +
80
+ state.buffer[state.start++] * 65536
81
+ )
66
82
  }
67
83
  }
68
84
 
69
- exports.uint32 = {
85
+ const uint32 = exports.uint32 = {
70
86
  preencode (state, n) {
71
87
  state.end += 4
72
88
  },
@@ -78,7 +94,52 @@ exports.uint32 = {
78
94
  },
79
95
  decode (state) {
80
96
  if (state.end - state.start < 4) throw new Error('Out of bounds')
81
- return state.buffer[state.start++] + state.buffer[state.start++] * 256 + state.buffer[state.start++] * 65536 + state.buffer[state.start++] * 16777216
97
+ return (
98
+ state.buffer[state.start++] +
99
+ state.buffer[state.start++] * 256 +
100
+ state.buffer[state.start++] * 65536 +
101
+ state.buffer[state.start++] * 16777216
102
+ )
103
+ }
104
+ }
105
+
106
+ const uint64 = exports.uint64 = {
107
+ preencode (state, n) {
108
+ state.end += 8
109
+ },
110
+ encode (state, n) {
111
+ const r = Math.floor(n / 4294967296)
112
+ uint32.encode(state, n)
113
+ uint32.encode(state, r)
114
+ },
115
+ decode (state) {
116
+ if (state.end - state.start < 8) throw new Error('Out of bounds')
117
+ return uint32.decode(state) + 4294967296 * uint32.decode(state)
118
+ }
119
+ }
120
+
121
+ exports.int = zigZag(uint)
122
+ exports.int8 = zigZag(uint8)
123
+ exports.int16 = zigZag(uint16)
124
+ exports.int24 = zigZag(uint24)
125
+ exports.int32 = zigZag(uint32)
126
+ exports.int64 = zigZag(uint64)
127
+
128
+ exports.float32 = {
129
+ preencode (state, n) {
130
+ state.end += 4
131
+ },
132
+ encode (state, n) {
133
+ const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 4)
134
+ view.setFloat32(0, n, true) // little endian
135
+ state.start += 4
136
+ },
137
+ decode (state) {
138
+ if (state.end - state.start < 4) throw new Error('Out of bounds')
139
+ const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 4)
140
+ const float = view.getFloat32(0, true) // little endian
141
+ state.start += 4
142
+ return float
82
143
  }
83
144
  }
84
145
 
@@ -92,6 +153,7 @@ exports.float64 = {
92
153
  state.start += 8
93
154
  },
94
155
  decode (state) {
156
+ if (state.end - state.start < 8) throw new Error('Out of bounds')
95
157
  const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
96
158
  const float = view.getFloat64(0, true) // little endian
97
159
  state.start += 8
@@ -101,21 +163,12 @@ exports.float64 = {
101
163
 
102
164
  exports.buffer = {
103
165
  preencode (state, b) {
104
- if (b) {
105
- uint.preencode(state, b.length)
106
- state.end += b.length
107
- } else {
108
- state.end++
109
- }
166
+ if (b) uint8array.preencode(state, b)
167
+ else state.end++
110
168
  },
111
169
  encode (state, b) {
112
- if (b) {
113
- uint.encode(state, b.length)
114
- state.buffer.set(b, state.start)
115
- state.start += b.length
116
- } else {
117
- state.buffer[state.start++] = 0
118
- }
170
+ if (b) uint8array.encode(state, b)
171
+ else state.buffer[state.start++] = 0
119
172
  },
120
173
  decode (state) {
121
174
  const len = uint.decode(state)
@@ -141,57 +194,65 @@ const raw = exports.raw = {
141
194
  }
142
195
  }
143
196
 
144
- exports.uint32array = {
145
- preencode (state, b) {
146
- uint.preencode(state, b.length)
147
- state.end += b.byteLength
148
- },
149
- encode (state, b) {
150
- uint.encode(state, b.length)
151
- const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
152
- if (BE) hostToLE32(view, b.length)
153
- state.buffer.set(view, state.start)
154
- state.start += b.byteLength
155
- },
156
- decode (state) {
157
- const len = uint.decode(state)
197
+ function typedarray (TypedArray, swap) {
198
+ const n = TypedArray.BYTES_PER_ELEMENT
199
+
200
+ return {
201
+ preencode (state, b) {
202
+ uint.preencode(state, b.length)
203
+ state.end += b.byteLength
204
+ },
205
+ encode (state, b) {
206
+ uint.encode(state, b.length)
158
207
 
159
- const byteOffset = state.buffer.byteOffset + state.start
160
- const s = state.start
208
+ const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
161
209
 
162
- state.start += len * 4
210
+ if (BE && swap) swap(view)
163
211
 
164
- if ((byteOffset & 3) === 0) {
165
- const arr = new Uint32Array(state.buffer.buffer, byteOffset, len)
166
- if (BE) LEToHost32(arr, len)
167
- return arr
168
- }
212
+ state.buffer.set(view, state.start)
213
+ state.start += b.byteLength
214
+ },
215
+ decode (state) {
216
+ const len = uint.decode(state)
217
+
218
+ let b = state.buffer.subarray(state.start, state.start += len * n)
219
+ if (b.byteLength !== len * n) throw new Error('Out of bounds')
220
+ if ((b.byteOffset % n) !== 0) b = new Uint8Array(b)
221
+
222
+ if (BE && swap) swap(b)
169
223
 
170
- // align mismatch
171
- const copy = new Uint8Array(len * 4)
172
- const arr = new Uint32Array(copy.buffer, copy.byteOffset, len)
173
- copy.set(state.buffer.subarray(s, state.start), 0)
174
- if (BE) LEToHost32(arr, len)
175
- return arr
224
+ return new TypedArray(b.buffer, b.byteOffset, b.byteLength / n)
225
+ }
176
226
  }
177
227
  }
178
228
 
179
- exports.string = { // TODO: un"Buffer" this one
229
+ const uint8array = exports.uint8array = typedarray(Uint8Array)
230
+ exports.uint16array = typedarray(Uint16Array, b4a.swap16)
231
+ exports.uint32array = typedarray(Uint32Array, b4a.swap32)
232
+
233
+ exports.int8array = typedarray(Int8Array)
234
+ exports.int16array = typedarray(Int16Array, b4a.swap16)
235
+ exports.int32array = typedarray(Int32Array, b4a.swap32)
236
+
237
+ exports.float32array = typedarray(Float32Array, b4a.swap32)
238
+ exports.float64array = typedarray(Float64Array, b4a.swap64)
239
+
240
+ exports.string = {
180
241
  preencode (state, s) {
181
- const len = Buffer.byteLength(s)
242
+ const len = b4a.byteLength(s)
182
243
  uint.preencode(state, len)
183
244
  state.end += len
184
245
  },
185
246
  encode (state, s) {
186
- const len = Buffer.byteLength(s)
247
+ const len = b4a.byteLength(s)
187
248
  uint.encode(state, len)
188
- state.buffer.write(s, state.start)
249
+ b4a.write(state.buffer, s, state.start)
189
250
  state.start += len
190
251
  },
191
252
  decode (state) {
192
253
  const len = uint.decode(state)
193
- const s = state.buffer.toString('utf-8', state.start, state.start += len)
194
- if (Buffer.byteLength(s) !== len) throw new Error('Out of bounds')
254
+ const s = b4a.toString(state.buffer, 'utf-8', state.start, state.start += len)
255
+ if (b4a.byteLength(s) !== len || state.start > state.end) throw new Error('Out of bounds')
195
256
  return s
196
257
  }
197
258
  }
@@ -204,40 +265,30 @@ exports.bool = {
204
265
  state.buffer[state.start++] = b ? 1 : 0
205
266
  },
206
267
  decode (state) {
207
- if (state.start >= state.buffer.byteLength) throw Error('Out of bounds')
268
+ if (state.start >= state.end) throw Error('Out of bounds')
208
269
  return state.buffer[state.start++] === 1
209
270
  }
210
271
  }
211
272
 
212
- exports.fixed32 = {
213
- preencode (state, h) {
214
- state.end += 32
215
- },
216
- encode (state, h) {
217
- state.buffer.set(h, state.start)
218
- state.start += 32
219
- },
220
- decode (state) {
221
- const b = state.buffer.subarray(state.start, state.start += 32)
222
- if (b.length !== 32) throw new Error('Out of bounds')
223
- return b
273
+ const fixed = exports.fixed = function fixed (n) {
274
+ return {
275
+ preencode (state, s) {
276
+ state.end += n
277
+ },
278
+ encode (state, s) {
279
+ state.buffer.set(s, state.start)
280
+ state.start += n
281
+ },
282
+ decode (state) {
283
+ const b = state.buffer.subarray(state.start, state.start += n)
284
+ if (b.length !== n) throw new Error('Out of bounds')
285
+ return b
286
+ }
224
287
  }
225
288
  }
226
289
 
227
- exports.fixed64 = {
228
- preencode (state, s) {
229
- state.end += 64
230
- },
231
- encode (state, s) {
232
- state.buffer.set(s, state.start)
233
- state.start += 64
234
- },
235
- decode (state) {
236
- const b = state.buffer.subarray(state.start, state.start += 64)
237
- if (b.length !== 64) throw new Error('Out of bounds')
238
- return b
239
- }
240
- }
290
+ exports.fixed32 = fixed(32)
291
+ exports.fixed64 = fixed(64)
241
292
 
242
293
  exports.none = {
243
294
  preencode (state, m) {
@@ -251,23 +302,27 @@ exports.none = {
251
302
  }
252
303
  }
253
304
 
254
- exports.fixed = fixed
255
- exports.array = array
256
- exports.from = from
257
-
258
- exports.encode = function (enc, m) {
259
- const state = { start: 0, end: 0, buffer: null }
260
- enc.preencode(state, m)
261
- state.buffer = Buffer.allocUnsafe(state.end)
262
- enc.encode(state, m)
263
- return state.buffer
264
- }
265
-
266
- exports.decode = function (enc, buffer) {
267
- return enc.decode({ start: 0, end: buffer.byteLength, buffer })
305
+ exports.array = function array (enc) {
306
+ return {
307
+ preencode (state, list) {
308
+ uint.preencode(state, list.length)
309
+ for (let i = 0; i < list.length; i++) enc.preencode(state, list[i])
310
+ },
311
+ encode (state, list) {
312
+ uint.encode(state, list.length)
313
+ for (let i = 0; i < list.length; i++) enc.encode(state, list[i])
314
+ },
315
+ decode (state) {
316
+ const len = uint.decode(state)
317
+ if (len > 1048576) throw new Error('Array is too big')
318
+ const arr = new Array(len)
319
+ for (let i = 0; i < len; i++) arr[i] = enc.decode(state)
320
+ return arr
321
+ }
322
+ }
268
323
  }
269
324
 
270
- function from (enc) {
325
+ exports.from = function from (enc) {
271
326
  if (enc.preencode) return enc
272
327
  if (enc.encodingLength) return fromAbstractEncoder(enc)
273
328
  return fromCodec(enc)
@@ -310,109 +365,37 @@ function fromAbstractEncoder (enc) {
310
365
  }
311
366
  }
312
367
 
313
- function array (enc) {
314
- return {
315
- preencode (state, list) {
316
- uint.preencode(state, list.length)
317
- for (let i = 0; i < list.length; i++) enc.preencode(state, list[i])
318
- },
319
- encode (state, list) {
320
- uint.encode(state, list.length)
321
- for (let i = 0; i < list.length; i++) enc.encode(state, list[i])
322
- },
323
- decode (state) {
324
- const len = uint.decode(state)
325
- if (len > 1048576) throw new Error('Array is too big')
326
- const arr = new Array(len)
327
- for (let i = 0; i < len; i++) arr[i] = enc.decode(state)
328
- return arr
329
- }
330
- }
368
+ exports.encode = function encode (enc, m) {
369
+ const state = { start: 0, end: 0, buffer: null }
370
+ enc.preencode(state, m)
371
+ state.buffer = b4a.allocUnsafe(state.end)
372
+ enc.encode(state, m)
373
+ return state.buffer
374
+ }
375
+
376
+ exports.decode = function decode (enc, buffer) {
377
+ return enc.decode({ start: 0, end: buffer.byteLength, buffer })
331
378
  }
332
379
 
333
- function fixed (n) {
380
+ function zigZag (enc) {
334
381
  return {
335
- preencode (state, s) {
336
- state.end += n
382
+ preencode (state, n) {
383
+ enc.preencode(state, zigZagEncode(n))
337
384
  },
338
- encode (state, s) {
339
- state.buffer.set(s, state.start)
340
- state.start += n
385
+ encode (state, n) {
386
+ enc.encode(state, zigZagEncode(n))
341
387
  },
342
388
  decode (state) {
343
- const b = state.buffer.subarray(state.start, state.start += n)
344
- if (b.length !== n) throw new Error('Out of bounds')
345
- return b
389
+ return zigZagDecode(enc.decode(state))
346
390
  }
347
391
  }
348
392
  }
349
393
 
350
- function LEToHost32 (arr, len) {
351
- const view = new DataView(arr.buffer, arr.byteOffset)
352
- const host = new Uint32Array(arr.buffer, arr.byteOffset, len)
353
-
354
- for (let i = 0; i < host.length; i++) {
355
- host[i] = view.getUint32(4 * i, BE)
356
- }
357
- }
358
-
359
- function hostToLE32 (arr, len) {
360
- const view = new DataView(arr.buffer, arr.byteOffset)
361
- const host = new Uint32Array(arr.buffer, arr.byteOffset, len)
362
-
363
- for (let i = 0; i < host.length; i++) {
364
- view.setUint32(4 * i, host[i], BE)
365
- }
366
- }
367
-
368
- function decode16 (state) {
369
- if (state.start + 2 > state.buffer.length) throw new Error('Out of bounds')
370
- return state.buffer[state.start++] + 256 * state.buffer[state.start++]
371
- }
372
-
373
- function encode16 (state, n) {
374
- state.buffer[state.start++] = 0xfd
375
- state.buffer[state.start++] = n
376
- state.buffer[state.start++] = (n >>> 8)
377
- }
378
-
379
- function decode32 (state) {
380
- if (state.start + 4 > state.buffer.length) throw new Error('Out of bounds')
381
- return state.buffer[state.start++] + 256 * state.buffer[state.start++] +
382
- 65536 * state.buffer[state.start++] + 16777216 * state.buffer[state.start++]
383
- }
384
-
385
- function encode32 (state, n) {
386
- state.buffer[state.start++] = 0xfe
387
- state.buffer[state.start++] = n
388
- state.buffer[state.start++] = (n = (n >>> 8))
389
- state.buffer[state.start++] = (n = (n >>> 8))
390
- state.buffer[state.start++] = (n >>> 8)
391
- }
392
-
393
- function decode64 (state) {
394
- if (state.start + 8 > state.buffer.length) throw new Error('Out of bounds')
395
- return decode32(state) + 4294967296 * decode32(state)
396
- }
397
-
398
- function encode64 (state, n) {
399
- state.buffer[state.start++] = 0xff
400
- let r = Math.floor(n / 4294967296)
401
- state.buffer[state.start++] = n
402
- state.buffer[state.start++] = (n = (n >>> 8))
403
- state.buffer[state.start++] = (n = (n >>> 8))
404
- state.buffer[state.start++] = (n >>> 8)
405
- state.buffer[state.start++] = r
406
- state.buffer[state.start++] = (r = (r >>> 8))
407
- state.buffer[state.start++] = (r = (r >>> 8))
408
- state.buffer[state.start++] = (r >>> 8)
409
- }
410
-
411
- function zigzagDecode (n) {
394
+ function zigZagDecode (n) {
412
395
  return n === 0 ? n : (n & 1) === 0 ? n / 2 : -(n + 1) / 2
413
396
  }
414
397
 
415
- function zigzagEncode (n) {
398
+ function zigZagEncode (n) {
416
399
  // 0, -1, 1, -2, 2, ...
417
400
  return n < 0 ? (2 * -n) - 1 : n === 0 ? 0 : 2 * n
418
401
  }
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "description": "A series of compact encoding schemes for building small and fast parsers and serializers",
5
5
  "main": "index.js",
6
- "dependencies": {},
6
+ "dependencies": {
7
+ "b4a": "^1.3.0"
8
+ },
7
9
  "devDependencies": {
8
- "standard": "^16.0.3",
9
- "tape": "^5.1.1"
10
+ "brittle": "^1.5.1",
11
+ "standard": "^16.0.3"
10
12
  },
11
13
  "scripts": {
12
- "test": "standard && tape test.js"
14
+ "test": "standard && brittle test.js"
13
15
  },
14
16
  "repository": {
15
17
  "type": "git",
package/test.js CHANGED
@@ -1,75 +1,71 @@
1
1
  const enc = require('./')
2
- const tape = require('tape')
2
+ const tape = require('brittle')
3
3
 
4
4
  tape('uint', function (t) {
5
5
  const state = enc.state()
6
6
 
7
7
  enc.uint.preencode(state, 42)
8
- t.same(state, { start: 0, end: 1, buffer: null })
8
+ t.alike(state, { start: 0, end: 1, buffer: null })
9
9
  enc.uint.preencode(state, 4200)
10
- t.same(state, { start: 0, end: 4, buffer: null })
10
+ t.alike(state, { start: 0, end: 4, buffer: null })
11
11
  enc.uint.preencode(state, Number.MAX_SAFE_INTEGER)
12
- t.same(state, { start: 0, end: 13, buffer: null })
12
+ t.alike(state, { start: 0, end: 13, buffer: null })
13
13
 
14
14
  state.buffer = Buffer.alloc(state.end)
15
15
  enc.uint.encode(state, 42)
16
- t.same(state, { start: 1, end: 13, buffer: Buffer.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
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
17
  enc.uint.encode(state, 4200)
18
- t.same(state, { start: 4, end: 13, buffer: Buffer.from([42, 0xfd, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
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
19
  enc.uint.encode(state, Number.MAX_SAFE_INTEGER)
20
- t.same(state, { start: 13, end: 13, buffer: Buffer.from([42, 0xfd, 104, 16, 0xff, 255, 255, 255, 255, 255, 255, 31, 0]) })
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
21
 
22
22
  state.start = 0
23
- t.same(enc.uint.decode(state), 42)
24
- t.same(enc.uint.decode(state), 4200)
25
- t.same(enc.uint.decode(state), Number.MAX_SAFE_INTEGER)
26
- t.same(state.start, state.end)
23
+ t.is(enc.uint.decode(state), 42)
24
+ t.is(enc.uint.decode(state), 4200)
25
+ t.is(enc.uint.decode(state), Number.MAX_SAFE_INTEGER)
26
+ t.is(state.start, state.end)
27
27
 
28
- t.throws(() => enc.uint.decode(state))
29
-
30
- t.end()
28
+ t.exception(() => enc.uint.decode(state))
31
29
  })
32
30
 
33
31
  tape('int', function (t) {
34
32
  const state = enc.state()
35
33
 
36
34
  enc.int.preencode(state, 42)
37
- t.same(state, { start: 0, end: 1, buffer: null })
35
+ t.alike(state, { start: 0, end: 1, buffer: null })
38
36
  enc.int.preencode(state, -4200)
39
- t.same(state, { start: 0, end: 4, buffer: null })
37
+ t.alike(state, { start: 0, end: 4, buffer: null })
40
38
 
41
39
  state.buffer = Buffer.alloc(state.end)
42
40
  enc.int.encode(state, 42)
43
- t.same(state, { start: 1, end: 4, buffer: Buffer.from([84, 0, 0, 0]) })
41
+ t.alike(state, { start: 1, end: 4, buffer: Buffer.from([84, 0, 0, 0]) })
44
42
  enc.int.encode(state, -4200)
45
- t.same(state, { start: 4, end: 4, buffer: Buffer.from([84, 0xfd, 207, 32]) })
43
+ t.alike(state, { start: 4, end: 4, buffer: Buffer.from([84, 0xfd, 207, 32]) })
46
44
 
47
45
  state.start = 0
48
- t.same(enc.int.decode(state), 42)
49
- t.same(enc.int.decode(state), -4200)
50
- t.same(state.start, state.end)
51
-
52
- t.throws(() => enc.int.decode(state))
46
+ t.is(enc.int.decode(state), 42)
47
+ t.is(enc.int.decode(state), -4200)
48
+ t.is(state.start, state.end)
53
49
 
54
- t.end()
50
+ t.exception(() => enc.int.decode(state))
55
51
  })
56
52
 
57
53
  tape('float64', function (t) {
58
54
  const state = enc.state()
59
55
 
60
56
  enc.float64.preencode(state, 162.2377294)
61
- t.same(state, { start: 0, end: 8, buffer: null })
57
+ t.alike(state, { start: 0, end: 8, buffer: null })
62
58
 
63
59
  state.buffer = Buffer.alloc(state.end)
64
- t.same(state, { start: 0, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) })
60
+ t.alike(state, { start: 0, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) })
65
61
  enc.float64.encode(state, 162.2377294)
66
- t.same(state, { start: 8, end: 8, buffer: Buffer.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
62
+ t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
67
63
 
68
64
  state.start = 0
69
- t.same(enc.float64.decode(state), 162.2377294)
70
- t.same(state.start, state.end)
65
+ t.is(enc.float64.decode(state), 162.2377294)
66
+ t.is(state.start, state.end)
71
67
 
72
- t.throws(() => enc.float64.decode(state))
68
+ t.exception(() => enc.float64.decode(state))
73
69
 
74
70
  // alignement
75
71
  state.start = 0
@@ -78,33 +74,33 @@ tape('float64', function (t) {
78
74
 
79
75
  enc.int.preencode(state, 0)
80
76
  enc.float64.preencode(state, 162.2377294)
81
- t.same(state, { start: 0, end: 9, buffer: null })
77
+ t.alike(state, { start: 0, end: 9, buffer: null })
82
78
 
83
79
  state.buffer = Buffer.alloc(state.end)
84
- t.same(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) })
80
+ t.alike(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) })
85
81
  enc.int.encode(state, 0)
86
82
  enc.float64.encode(state, 162.2377294)
87
- t.same(state, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
83
+ t.alike(state, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
88
84
 
89
85
  state.start = 0
90
- t.same(enc.int.decode(state), 0)
91
- t.same(enc.float64.decode(state), 162.2377294)
92
- t.same(state.start, state.end)
86
+ t.is(enc.int.decode(state), 0)
87
+ t.is(enc.float64.decode(state), 162.2377294)
88
+ t.is(state.start, state.end)
93
89
 
94
90
  // subarray
95
91
  const buf = Buffer.alloc(10)
96
92
  state.start = 0
97
93
  state.buffer = buf.subarray(1)
98
- t.same(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) })
94
+ t.alike(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) })
99
95
  enc.int.encode(state, 0)
100
96
  enc.float64.encode(state, 162.2377294)
101
- t.same(state, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
102
- t.same(buf, Buffer.from([0, 0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))
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]))
103
99
 
104
100
  state.start = 0
105
- t.same(enc.int.decode(state), 0)
106
- t.same(enc.float64.decode(state), 162.2377294)
107
- t.same(state.start, state.end)
101
+ t.is(enc.int.decode(state), 0)
102
+ t.is(enc.float64.decode(state), 162.2377294)
103
+ t.is(state.start, state.end)
108
104
 
109
105
  // 0
110
106
  state.start = 0
@@ -114,11 +110,11 @@ tape('float64', function (t) {
114
110
  enc.float64.preencode(state, 162.2377294)
115
111
  state.buffer = Buffer.alloc(state.end)
116
112
  enc.float64.encode(state, 0)
117
- t.same(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) })
113
+ t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) })
118
114
 
119
115
  state.start = 0
120
- t.same(enc.float64.decode(state), 0)
121
- t.same(state.start, state.end)
116
+ t.is(enc.float64.decode(state), 0)
117
+ t.is(state.start, state.end)
122
118
 
123
119
  // Infinity
124
120
  state.start = 0
@@ -128,11 +124,11 @@ tape('float64', function (t) {
128
124
  enc.float64.preencode(state, Infinity)
129
125
  state.buffer = Buffer.alloc(state.end)
130
126
  enc.float64.encode(state, Infinity)
131
- t.same(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f]) })
127
+ t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f]) })
132
128
 
133
129
  state.start = 0
134
- t.same(enc.float64.decode(state), Infinity)
135
- t.same(state.start, state.end)
130
+ t.is(enc.float64.decode(state), Infinity)
131
+ t.is(state.start, state.end)
136
132
 
137
133
  // Edge cases
138
134
  state.start = 0
@@ -142,158 +138,229 @@ tape('float64', function (t) {
142
138
  enc.float64.preencode(state, 0.1 + 0.2)
143
139
  state.buffer = Buffer.alloc(state.end)
144
140
  enc.float64.encode(state, 0.1 + 0.2)
145
- t.same(state, { start: 8, end: 8, buffer: Buffer.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f]) })
141
+ t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f]) })
146
142
 
147
143
  state.start = 0
148
- t.same(enc.float64.decode(state), 0.1 + 0.2)
149
- t.same(state.start, state.end)
150
-
151
- t.end()
144
+ t.is(enc.float64.decode(state), 0.1 + 0.2)
145
+ t.is(state.start, state.end)
152
146
  })
153
147
 
154
148
  tape('buffer', function (t) {
155
149
  const state = enc.state()
156
150
 
157
151
  enc.buffer.preencode(state, Buffer.from('hi'))
158
- t.same(state, { start: 0, end: 3, buffer: null })
152
+ t.alike(state, { start: 0, end: 3, buffer: null })
159
153
  enc.buffer.preencode(state, Buffer.from('hello'))
160
- t.same(state, { start: 0, end: 9, buffer: null })
154
+ t.alike(state, { start: 0, end: 9, buffer: null })
161
155
  enc.buffer.preencode(state, null)
162
- t.same(state, { start: 0, end: 10, buffer: null })
156
+ t.alike(state, { start: 0, end: 10, buffer: null })
163
157
 
164
158
  state.buffer = Buffer.alloc(state.end)
165
159
  enc.buffer.encode(state, Buffer.from('hi'))
166
- t.same(state, { start: 3, end: 10, buffer: Buffer.from('\x02hi\x00\x00\x00\x00\x00\x00\x00') })
160
+ t.alike(state, { start: 3, end: 10, buffer: Buffer.from('\x02hi\x00\x00\x00\x00\x00\x00\x00') })
167
161
  enc.buffer.encode(state, Buffer.from('hello'))
168
- t.same(state, { start: 9, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') })
162
+ t.alike(state, { start: 9, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') })
169
163
  enc.buffer.encode(state, null)
170
- t.same(state, { start: 10, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') })
164
+ t.alike(state, { start: 10, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') })
171
165
 
172
166
  state.start = 0
173
- t.same(enc.buffer.decode(state), Buffer.from('hi'))
174
- t.same(enc.buffer.decode(state), Buffer.from('hello'))
175
- t.same(enc.buffer.decode(state), null)
176
- t.same(state.start, state.end)
167
+ t.alike(enc.buffer.decode(state), Buffer.from('hi'))
168
+ t.alike(enc.buffer.decode(state), Buffer.from('hello'))
169
+ t.is(enc.buffer.decode(state), null)
170
+ t.is(state.start, state.end)
177
171
 
178
- t.throws(() => enc.buffer.decode(state))
172
+ t.exception(() => enc.buffer.decode(state))
179
173
  state.buffer = state.buffer.subarray(0, 8)
180
174
  state.start = 3
181
- t.throws(() => enc.buffer.decode(state), 'partial throws')
182
-
183
- t.end()
175
+ t.exception(() => enc.buffer.decode(state), 'partial throws')
184
176
  })
185
177
 
186
178
  tape('raw', function (t) {
187
179
  const state = enc.state()
188
180
 
189
181
  enc.raw.preencode(state, Buffer.from('hi'))
190
- t.same(state, { start: 0, end: 2, buffer: null })
182
+ t.alike(state, { start: 0, end: 2, buffer: null })
191
183
 
192
184
  state.buffer = Buffer.alloc(state.end)
193
185
  enc.raw.encode(state, Buffer.from('hi'))
194
- t.same(state, { start: 2, end: 2, buffer: Buffer.from('hi') })
186
+ t.alike(state, { start: 2, end: 2, buffer: Buffer.from('hi') })
195
187
 
196
188
  state.start = 0
197
- t.same(enc.raw.decode(state), Buffer.from('hi'))
198
- t.same(state.start, state.end)
189
+ t.alike(enc.raw.decode(state), Buffer.from('hi'))
190
+ t.is(state.start, state.end)
191
+ })
192
+
193
+ tape('uint16array', function (t) {
194
+ const state = enc.state()
199
195
 
200
- t.end()
196
+ enc.uint16array.preencode(state, new Uint16Array([1, 2, 3]))
197
+ t.alike(state, { start: 0, end: 7, buffer: null })
198
+
199
+ state.buffer = Buffer.alloc(state.end)
200
+ enc.uint16array.encode(state, new Uint16Array([1, 2, 3]))
201
+ t.alike(state, { start: 7, end: 7, buffer: Buffer.from([3, 1, 0, 2, 0, 3, 0]) })
202
+
203
+ state.start = 0
204
+ t.alike(enc.uint16array.decode(state), new Uint16Array([1, 2, 3]))
205
+ t.is(state.start, state.end)
206
+
207
+ t.exception(() => enc.uint16array.decode(state))
201
208
  })
202
209
 
203
210
  tape('uint32array', function (t) {
204
211
  const state = enc.state()
205
212
 
206
213
  enc.uint32array.preencode(state, new Uint32Array([1]))
207
- t.same(state, { start: 0, end: 5, buffer: null })
214
+ t.alike(state, { start: 0, end: 5, buffer: null })
208
215
  enc.uint32array.preencode(state, new Uint32Array([42, 43]))
209
- t.same(state, { start: 0, end: 14, buffer: null })
216
+ t.alike(state, { start: 0, end: 14, buffer: null })
210
217
 
211
218
  state.buffer = Buffer.alloc(state.end)
212
219
  enc.uint32array.encode(state, new Uint32Array([1]))
213
- t.same(state, { start: 5, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
220
+ t.alike(state, { start: 5, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
214
221
  enc.uint32array.encode(state, new Uint32Array([42, 43]))
215
- t.same(state, { start: 14, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0]) })
222
+ t.alike(state, { start: 14, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0]) })
223
+
224
+ state.start = 0
225
+ t.alike(enc.uint32array.decode(state), new Uint32Array([1]))
226
+ t.alike(enc.uint32array.decode(state), new Uint32Array([42, 43]))
227
+ t.is(state.start, state.end)
228
+
229
+ t.exception(() => enc.uint32array.decode(state))
230
+ })
231
+
232
+ tape('int16array', function (t) {
233
+ const state = enc.state()
234
+
235
+ enc.int16array.preencode(state, new Int16Array([1, -2, 3]))
236
+ t.alike(state, { start: 0, end: 7, buffer: null })
237
+
238
+ state.buffer = Buffer.alloc(state.end)
239
+ enc.int16array.encode(state, new Int16Array([1, -2, 3]))
240
+ t.alike(state, { start: 7, end: 7, buffer: Buffer.from([3, 1, 0, 0xfe, 0xff, 3, 0]) })
241
+
242
+ state.start = 0
243
+ t.alike(enc.int16array.decode(state), new Int16Array([1, -2, 3]))
244
+ t.is(state.start, state.end)
245
+
246
+ t.exception(() => enc.int16array.decode(state))
247
+ })
248
+
249
+ tape('int32array', function (t) {
250
+ const state = enc.state()
251
+
252
+ enc.int32array.preencode(state, new Int32Array([1, -2, 3]))
253
+ t.alike(state, { start: 0, end: 13, buffer: null })
254
+
255
+ state.buffer = Buffer.alloc(state.end)
256
+ enc.int32array.encode(state, new Int32Array([1, -2, 3]))
257
+ t.alike(state, { start: 13, end: 13, buffer: Buffer.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0]) })
258
+
259
+ state.start = 0
260
+ t.alike(enc.int32array.decode(state), new Int32Array([1, -2, 3]))
261
+ t.is(state.start, state.end)
262
+
263
+ t.exception(() => enc.int32array.decode(state))
264
+ })
265
+
266
+ tape('float32array', function (t) {
267
+ const state = enc.state()
268
+
269
+ enc.float32array.preencode(state, new Float32Array([1.1, -2.2, 3.3]))
270
+ t.alike(state, { start: 0, end: 13, buffer: null })
271
+
272
+ state.buffer = Buffer.alloc(state.end)
273
+ enc.float32array.encode(state, new Float32Array([1.1, -2.2, 3.3]))
274
+ t.alike(state, { start: 13, end: 13, buffer: Buffer.from([3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53, 0x40]) })
216
275
 
217
276
  state.start = 0
218
- t.same(enc.uint32array.decode(state), new Uint32Array([1]))
219
- t.same(enc.uint32array.decode(state), new Uint32Array([42, 43]))
220
- t.same(state.start, state.end)
277
+ t.alike(enc.float32array.decode(state), new Float32Array([1.1, -2.2, 3.3]))
278
+ t.is(state.start, state.end)
221
279
 
222
- t.throws(() => enc.uint32Array.decode(state))
280
+ t.exception(() => enc.float32array.decode(state))
281
+ })
282
+
283
+ tape('float64array', function (t) {
284
+ const state = enc.state()
285
+
286
+ enc.float64array.preencode(state, new Float64Array([1.1, -2.2, 3.3]))
287
+ t.alike(state, { start: 0, end: 25, buffer: null })
223
288
 
224
- t.end()
289
+ state.buffer = Buffer.alloc(state.end)
290
+ enc.float64array.encode(state, new Float64Array([1.1, -2.2, 3.3]))
291
+ 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]) })
292
+
293
+ state.start = 0
294
+ t.alike(enc.float64array.decode(state), new Float64Array([1.1, -2.2, 3.3]))
295
+ t.is(state.start, state.end)
296
+
297
+ t.exception(() => enc.float64array.decode(state))
225
298
  })
226
299
 
227
300
  tape('string', function (t) {
228
301
  const state = enc.state()
229
302
 
230
- enc.string.preencode(state, 'hi')
231
- t.same(state, { start: 0, end: 3, buffer: null })
303
+ enc.string.preencode(state, '🌾')
304
+ t.alike(state, { start: 0, end: 5, buffer: null })
232
305
  enc.string.preencode(state, 'høsten er fin')
233
- t.same(state, { start: 0, end: 18, buffer: null })
306
+ t.alike(state, { start: 0, end: 20, buffer: null })
234
307
 
235
308
  state.buffer = Buffer.alloc(state.end)
236
- enc.string.encode(state, 'hi')
237
- t.same(state, { start: 3, end: 18, buffer: Buffer.from('\x02hi\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') })
309
+ enc.string.encode(state, '🌾')
310
+ 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') })
238
311
  enc.string.encode(state, 'høsten er fin')
239
- t.same(state, { start: 18, end: 18, buffer: Buffer.from('\x02hi\x0ehøsten er fin') })
312
+ t.alike(state, { start: 20, end: 20, buffer: Buffer.from('\x04🌾\x0ehøsten er fin') })
240
313
 
241
314
  state.start = 0
242
- t.same(enc.string.decode(state), 'hi')
243
- t.same(enc.string.decode(state), 'høsten er fin')
244
- t.same(state.start, state.end)
245
-
246
- t.throws(() => enc.string.decode(state))
315
+ t.is(enc.string.decode(state), '🌾')
316
+ t.is(enc.string.decode(state), 'høsten er fin')
317
+ t.is(state.start, state.end)
247
318
 
248
- t.end()
319
+ t.exception(() => enc.string.decode(state))
249
320
  })
250
321
 
251
322
  tape('fixed32', function (t) {
252
323
  const state = enc.state()
253
324
 
254
325
  enc.fixed32.preencode(state, Buffer.alloc(32).fill('a'))
255
- t.same(state, { start: 0, end: 32, buffer: null })
326
+ t.alike(state, { start: 0, end: 32, buffer: null })
256
327
  enc.fixed32.preencode(state, Buffer.alloc(32).fill('b'))
257
- t.same(state, { start: 0, end: 64, buffer: null })
328
+ t.alike(state, { start: 0, end: 64, buffer: null })
258
329
 
259
330
  state.buffer = Buffer.alloc(state.end)
260
331
  enc.fixed32.encode(state, Buffer.alloc(32).fill('a'))
261
- t.same(state, { start: 32, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32) })
332
+ t.alike(state, { start: 32, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32) })
262
333
  enc.fixed32.encode(state, Buffer.alloc(32).fill('b'))
263
- t.same(state, { start: 64, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32).fill('b', 32, 64) })
334
+ t.alike(state, { start: 64, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32).fill('b', 32, 64) })
264
335
 
265
336
  state.start = 0
266
- t.same(enc.fixed32.decode(state), Buffer.alloc(32).fill('a'))
267
- t.same(enc.fixed32.decode(state), Buffer.alloc(32).fill('b'))
268
- t.same(state.start, state.end)
269
-
270
- t.throws(() => enc.fixed32.decode(state))
337
+ t.alike(enc.fixed32.decode(state), Buffer.alloc(32).fill('a'))
338
+ t.alike(enc.fixed32.decode(state), Buffer.alloc(32).fill('b'))
339
+ t.is(state.start, state.end)
271
340
 
272
- t.end()
341
+ t.exception(() => enc.fixed32.decode(state))
273
342
  })
274
343
 
275
344
  tape('fixed64', function (t) {
276
345
  const state = enc.state()
277
346
 
278
347
  enc.fixed64.preencode(state, Buffer.alloc(64).fill('a'))
279
- t.same(state, { start: 0, end: 64, buffer: null })
348
+ t.alike(state, { start: 0, end: 64, buffer: null })
280
349
  enc.fixed64.preencode(state, Buffer.alloc(64).fill('b'))
281
- t.same(state, { start: 0, end: 128, buffer: null })
350
+ t.alike(state, { start: 0, end: 128, buffer: null })
282
351
 
283
352
  state.buffer = Buffer.alloc(state.end)
284
353
  enc.fixed64.encode(state, Buffer.alloc(64).fill('a'))
285
- t.same(state, { start: 64, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64) })
354
+ t.alike(state, { start: 64, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64) })
286
355
  enc.fixed64.encode(state, Buffer.alloc(64).fill('b'))
287
- t.same(state, { start: 128, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64).fill('b', 64, 128) })
356
+ t.alike(state, { start: 128, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64).fill('b', 64, 128) })
288
357
 
289
358
  state.start = 0
290
- t.same(enc.fixed64.decode(state), Buffer.alloc(64).fill('a'))
291
- t.same(enc.fixed64.decode(state), Buffer.alloc(64).fill('b'))
292
- t.same(state.start, state.end)
359
+ t.alike(enc.fixed64.decode(state), Buffer.alloc(64).fill('a'))
360
+ t.alike(enc.fixed64.decode(state), Buffer.alloc(64).fill('b'))
361
+ t.is(state.start, state.end)
293
362
 
294
- t.throws(() => enc.fixed64.decode(state))
295
-
296
- t.end()
363
+ t.exception(() => enc.fixed64.decode(state))
297
364
  })
298
365
 
299
366
  tape('fixed n', function (t) {
@@ -301,26 +368,24 @@ tape('fixed n', function (t) {
301
368
  const fixed = enc.fixed(3)
302
369
 
303
370
  fixed.preencode(state, Buffer.alloc(3).fill('a'))
304
- t.same(state, { start: 0, end: 3, buffer: null })
371
+ t.alike(state, { start: 0, end: 3, buffer: null })
305
372
  fixed.preencode(state, Buffer.alloc(3).fill('b'))
306
- t.same(state, { start: 0, end: 6, buffer: null })
373
+ t.alike(state, { start: 0, end: 6, buffer: null })
307
374
 
308
375
  state.buffer = Buffer.alloc(state.end)
309
376
  fixed.encode(state, Buffer.alloc(3).fill('a'))
310
- t.same(state, { start: 3, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3) })
377
+ t.alike(state, { start: 3, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3) })
311
378
  fixed.encode(state, Buffer.alloc(3).fill('b'))
312
- t.same(state, { start: 6, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3).fill('b', 3, 6) })
379
+ t.alike(state, { start: 6, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3).fill('b', 3, 6) })
313
380
 
314
381
  state.start = 0
315
- t.same(fixed.decode(state), Buffer.alloc(3).fill('a'))
316
- t.same(fixed.decode(state), Buffer.alloc(3).fill('b'))
317
- t.same(state.start, state.end)
382
+ t.alike(fixed.decode(state), Buffer.alloc(3).fill('a'))
383
+ t.alike(fixed.decode(state), Buffer.alloc(3).fill('b'))
384
+ t.is(state.start, state.end)
318
385
 
319
- t.throws(() => fixed.decode(state))
386
+ t.exception(() => fixed.decode(state))
320
387
  state.start = 4
321
- t.throws(() => fixed.decode(state))
322
-
323
- t.end()
388
+ t.exception(() => fixed.decode(state))
324
389
  })
325
390
 
326
391
  tape('array', function (t) {
@@ -328,22 +393,20 @@ tape('array', function (t) {
328
393
  const arr = enc.array(enc.bool)
329
394
 
330
395
  arr.preencode(state, [true, false, true])
331
- t.same(state, { start: 0, end: 4, buffer: null })
396
+ t.alike(state, { start: 0, end: 4, buffer: null })
332
397
  arr.preencode(state, [false, false, true, true])
333
- t.same(state, { start: 0, end: 9, buffer: null })
398
+ t.alike(state, { start: 0, end: 9, buffer: null })
334
399
 
335
400
  state.buffer = Buffer.alloc(state.end)
336
401
  arr.encode(state, [true, false, true])
337
- t.same(state, { start: 4, end: 9, buffer: Buffer.from([3, 1, 0, 1, 0, 0, 0, 0, 0]) })
402
+ t.alike(state, { start: 4, end: 9, buffer: Buffer.from([3, 1, 0, 1, 0, 0, 0, 0, 0]) })
338
403
  arr.encode(state, [false, false, true, true])
339
- t.same(state, { start: 9, end: 9, buffer: Buffer.from([3, 1, 0, 1, 4, 0, 0, 1, 1]) })
404
+ t.alike(state, { start: 9, end: 9, buffer: Buffer.from([3, 1, 0, 1, 4, 0, 0, 1, 1]) })
340
405
 
341
406
  state.start = 0
342
- t.same(arr.decode(state), [true, false, true])
343
- t.same(arr.decode(state), [false, false, true, true])
344
- t.same(state.start, state.end)
345
-
346
- t.throws(() => arr.decode(state))
407
+ t.alike(arr.decode(state), [true, false, true])
408
+ t.alike(arr.decode(state), [false, false, true, true])
409
+ t.is(state.start, state.end)
347
410
 
348
- t.end()
411
+ t.exception(() => arr.decode(state))
349
412
  })