compact-encoding 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -105,6 +105,28 @@ const uint32 = (exports.uint32 = {
105
105
  }
106
106
  })
107
107
 
108
+ const uint32be = (exports.uint32be = {
109
+ preencode(state, n) {
110
+ state.end += 4
111
+ },
112
+ encode(state, n) {
113
+ validateUint(n)
114
+ state.buffer[state.start++] = n >>> 24
115
+ state.buffer[state.start++] = n >>> 16
116
+ state.buffer[state.start++] = n >>> 8
117
+ state.buffer[state.start++] = n
118
+ },
119
+ decode(state) {
120
+ if (state.end - state.start < 4) throw new Error('Out of bounds')
121
+ return (
122
+ state.buffer[state.start++] * 0x1000000 +
123
+ state.buffer[state.start++] * 0x10000 +
124
+ state.buffer[state.start++] * 0x100 +
125
+ state.buffer[state.start++]
126
+ )
127
+ }
128
+ })
129
+
108
130
  const uint40 = (exports.uint40 = {
109
131
  preencode(state, n) {
110
132
  state.end += 5
@@ -169,6 +191,22 @@ const uint64 = (exports.uint64 = {
169
191
  }
170
192
  })
171
193
 
194
+ exports.uint64be = {
195
+ preencode(state, n) {
196
+ state.end += 8
197
+ },
198
+ encode(state, n) {
199
+ validateUint(n)
200
+ const r = Math.floor(n / 0x100000000)
201
+ uint32be.encode(state, r)
202
+ uint32be.encode(state, n)
203
+ },
204
+ decode(state) {
205
+ if (state.end - state.start < 8) throw new Error('Out of bounds')
206
+ return 0x100000000 * uint32be.decode(state) + uint32be.decode(state)
207
+ }
208
+ }
209
+
172
210
  const int = (exports.int = zigZagInt(uint))
173
211
  exports.int8 = zigZagInt(uint8)
174
212
  exports.int16 = zigZagInt(uint16)
package/package.json CHANGED
@@ -1,8 +1,14 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "A series of compact encoding schemes for building small and fast parsers and serializers",
5
5
  "main": "index.js",
6
+ "files": [
7
+ "endian.js",
8
+ "index.js",
9
+ "lexint.js",
10
+ "raw.js"
11
+ ],
6
12
  "dependencies": {
7
13
  "b4a": "^1.3.0"
8
14
  },
package/.gitattributes DELETED
@@ -1 +0,0 @@
1
- * text=auto eol=lf
@@ -1,17 +0,0 @@
1
- name: Publish
2
- on:
3
- push:
4
- tags:
5
- - v*
6
- permissions:
7
- id-token: write
8
- contents: read
9
- jobs:
10
- publish:
11
- runs-on: ubuntu-latest
12
- environment:
13
- name: npm
14
- name: Publish
15
- steps:
16
- - uses: holepunchto/actions/node-base@v1
17
- - uses: holepunchto/actions/publish@v1
@@ -1,23 +0,0 @@
1
- name: Build Status
2
- on:
3
- push:
4
- branches:
5
- - main
6
- pull_request:
7
- branches:
8
- - main
9
- jobs:
10
- build:
11
- strategy:
12
- matrix:
13
- node-version: [lts/*]
14
- os: [ubuntu-latest, macos-latest, windows-latest]
15
- runs-on: ${{ matrix.os }}
16
- steps:
17
- - uses: actions/checkout@v2
18
- - name: Use Node.js ${{ matrix.node-version }}
19
- uses: actions/setup-node@v2
20
- with:
21
- node-version: ${{ matrix.node-version }}
22
- - run: npm install
23
- - run: npm test
package/.prettierrc DELETED
@@ -1 +0,0 @@
1
- prettier-config-holepunch
package/NOTICE DELETED
@@ -1,13 +0,0 @@
1
- Copyright 2023 Holepunch Inc
2
-
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
-
7
- http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
package/test.js DELETED
@@ -1,1236 +0,0 @@
1
- const enc = require('./')
2
- const test = require('brittle')
3
- const b4a = require('b4a')
4
-
5
- test('uint', function (t) {
6
- const state = enc.state()
7
-
8
- enc.uint.preencode(state, 42)
9
- t.alike(state, enc.state(0, 1))
10
- enc.uint.preencode(state, 4200)
11
- t.alike(state, enc.state(0, 4))
12
- enc.uint.preencode(state, Number.MAX_SAFE_INTEGER)
13
- t.alike(state, enc.state(0, 13))
14
-
15
- state.buffer = b4a.alloc(state.end)
16
- enc.uint.encode(state, 42)
17
- t.alike(
18
- state,
19
- enc.state(1, 13, b4a.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
20
- )
21
- enc.uint.encode(state, 4200)
22
- t.alike(
23
- state,
24
- enc.state(4, 13, b4a.from([42, 0xfd, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
25
- )
26
- enc.uint.encode(state, Number.MAX_SAFE_INTEGER)
27
- t.alike(
28
- state,
29
- enc.state(
30
- 13,
31
- 13,
32
- b4a.from([42, 0xfd, 104, 16, 0xff, 255, 255, 255, 255, 255, 255, 31, 0])
33
- )
34
- )
35
-
36
- state.start = 0
37
- t.is(enc.uint.decode(state), 42)
38
- t.is(enc.uint.decode(state), 4200)
39
- t.is(enc.uint.decode(state), Number.MAX_SAFE_INTEGER)
40
- t.is(state.start, state.end)
41
-
42
- t.exception(() => enc.uint.decode(state))
43
- })
44
-
45
- test('int', function (t) {
46
- const state = enc.state()
47
-
48
- enc.int.preencode(state, 42)
49
- t.alike(state, enc.state(0, 1))
50
- enc.int.preencode(state, -4200)
51
- t.alike(state, enc.state(0, 4))
52
-
53
- state.buffer = b4a.alloc(state.end)
54
- enc.int.encode(state, 42)
55
- t.alike(state, enc.state(1, 4, b4a.from([84, 0, 0, 0])))
56
- enc.int.encode(state, -4200)
57
- t.alike(state, enc.state(4, 4, b4a.from([84, 0xfd, 207, 32])))
58
-
59
- state.start = 0
60
- t.is(enc.int.decode(state), 42)
61
- t.is(enc.int.decode(state), -4200)
62
- t.is(state.start, state.end)
63
-
64
- t.exception(() => enc.int.decode(state))
65
- })
66
-
67
- test('float64', function (t) {
68
- const state = enc.state()
69
-
70
- enc.float64.preencode(state, 162.2377294)
71
- t.alike(state, enc.state(0, 8))
72
-
73
- state.buffer = b4a.alloc(state.end)
74
- t.alike(state, enc.state(0, 8, b4a.from([0, 0, 0, 0, 0, 0, 0, 0])))
75
- enc.float64.encode(state, 162.2377294)
76
- t.alike(
77
- state,
78
- enc.state(8, 8, b4a.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))
79
- )
80
-
81
- state.start = 0
82
- t.is(enc.float64.decode(state), 162.2377294)
83
- t.is(state.start, state.end)
84
-
85
- t.exception(() => enc.float64.decode(state))
86
-
87
- // alignement
88
- state.start = 0
89
- state.end = 0
90
- state.buffer = null
91
-
92
- enc.int.preencode(state, 0)
93
- enc.float64.preencode(state, 162.2377294)
94
- t.alike(state, enc.state(0, 9))
95
-
96
- state.buffer = b4a.alloc(state.end)
97
- t.alike(state, enc.state(0, 9, b4a.from([0, 0, 0, 0, 0, 0, 0, 0, 0])))
98
- enc.int.encode(state, 0)
99
- enc.float64.encode(state, 162.2377294)
100
- t.alike(
101
- state,
102
- enc.state(
103
- 9,
104
- 9,
105
- b4a.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])
106
- )
107
- )
108
-
109
- state.start = 0
110
- t.is(enc.int.decode(state), 0)
111
- t.is(enc.float64.decode(state), 162.2377294)
112
- t.is(state.start, state.end)
113
-
114
- // subarray
115
- const buf = b4a.alloc(10)
116
- state.start = 0
117
- state.buffer = buf.subarray(1)
118
- t.alike(state, enc.state(0, 9, b4a.from([0, 0, 0, 0, 0, 0, 0, 0, 0])))
119
- enc.int.encode(state, 0)
120
- enc.float64.encode(state, 162.2377294)
121
- t.alike(
122
- state,
123
- enc.state(
124
- 9,
125
- 9,
126
- b4a.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])
127
- )
128
- )
129
- t.alike(buf, b4a.from([0, 0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))
130
-
131
- state.start = 0
132
- t.is(enc.int.decode(state), 0)
133
- t.is(enc.float64.decode(state), 162.2377294)
134
- t.is(state.start, state.end)
135
-
136
- // 0
137
- state.start = 0
138
- state.end = 0
139
- state.buffer = null
140
-
141
- enc.float64.preencode(state, 162.2377294)
142
- state.buffer = b4a.alloc(state.end)
143
- enc.float64.encode(state, 0)
144
- t.alike(state, enc.state(8, 8, b4a.from([0, 0, 0, 0, 0, 0, 0, 0])))
145
-
146
- state.start = 0
147
- t.is(enc.float64.decode(state), 0)
148
- t.is(state.start, state.end)
149
-
150
- // Infinity
151
- state.start = 0
152
- state.end = 0
153
- state.buffer = null
154
-
155
- enc.float64.preencode(state, Infinity)
156
- state.buffer = b4a.alloc(state.end)
157
- enc.float64.encode(state, Infinity)
158
- t.alike(state, enc.state(8, 8, b4a.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f])))
159
-
160
- state.start = 0
161
- t.is(enc.float64.decode(state), Infinity)
162
- t.is(state.start, state.end)
163
-
164
- // Edge cases
165
- state.start = 0
166
- state.end = 0
167
- state.buffer = null
168
-
169
- enc.float64.preencode(state, 0.1 + 0.2)
170
- state.buffer = b4a.alloc(state.end)
171
- enc.float64.encode(state, 0.1 + 0.2)
172
- t.alike(
173
- state,
174
- enc.state(8, 8, b4a.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f]))
175
- )
176
-
177
- state.start = 0
178
- t.is(enc.float64.decode(state), 0.1 + 0.2)
179
- t.is(state.start, state.end)
180
- })
181
-
182
- test('biguint64', function (t) {
183
- const state = enc.state()
184
-
185
- const n = 0x0102030405060708n
186
-
187
- enc.biguint64.preencode(state, n)
188
- t.alike(state, enc.state(0, 8))
189
-
190
- state.buffer = b4a.alloc(state.end)
191
- enc.biguint64.encode(state, n)
192
- t.alike(
193
- state,
194
- enc.state(8, 8, b4a.from([0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1]))
195
- )
196
-
197
- state.start = 0
198
- t.is(enc.biguint64.decode(state), n)
199
- t.is(state.start, state.end)
200
-
201
- t.exception(() => enc.biguint64.decode(state))
202
- })
203
-
204
- test('bigint64', function (t) {
205
- const state = enc.state()
206
-
207
- const n = -0x0102030405060708n
208
-
209
- enc.bigint64.preencode(state, n)
210
- t.alike(state, enc.state(0, 8))
211
-
212
- state.buffer = b4a.alloc(state.end)
213
- enc.bigint64.encode(state, n)
214
- t.alike(
215
- state,
216
- enc.state(8, 8, b4a.from([0xf, 0xe, 0xc, 0xa, 0x8, 0x6, 0x4, 0x2]))
217
- )
218
-
219
- state.start = 0
220
- t.is(enc.bigint64.decode(state), n)
221
- t.is(state.start, state.end)
222
-
223
- t.exception(() => enc.bigint64.decode(state))
224
- })
225
-
226
- test('biguint', function (t) {
227
- const state = enc.state()
228
-
229
- const n = 0x0102030405060708090a0b0cn
230
-
231
- enc.biguint.preencode(state, n)
232
- t.alike(state, enc.state(0, 17))
233
-
234
- state.buffer = b4a.alloc(state.end)
235
- enc.biguint.encode(state, n)
236
- t.alike(
237
- state,
238
- enc.state(
239
- 17,
240
- 17,
241
- b4a.from([
242
- 2, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0, 0x0,
243
- 0x0, 0x0
244
- ])
245
- )
246
- )
247
-
248
- state.start = 0
249
- t.is(enc.biguint.decode(state), n)
250
- t.is(state.start, state.end)
251
-
252
- t.exception(() => enc.biguint.decode(state))
253
- })
254
-
255
- test('bigint', function (t) {
256
- const state = enc.state()
257
-
258
- const n = -0x0102030405060708090a0b0cn
259
-
260
- enc.bigint.preencode(state, n)
261
- t.alike(state, enc.state(0, 17))
262
-
263
- state.buffer = b4a.alloc(state.end)
264
- enc.bigint.encode(state, n)
265
- t.alike(
266
- state,
267
- enc.state(
268
- 17,
269
- 17,
270
- b4a.from([
271
- 2, 0x17, 0x16, 0x14, 0x12, 0x10, 0xe, 0xc, 0xa, 0x8, 0x6, 0x4, 0x2, 0x0,
272
- 0x0, 0x0, 0x0
273
- ])
274
- )
275
- )
276
-
277
- state.start = 0
278
- t.is(enc.bigint.decode(state), n)
279
- t.is(state.start, state.end)
280
-
281
- t.exception(() => enc.bigint.decode(state))
282
- })
283
-
284
- test('buffer', function (t) {
285
- const state = enc.state()
286
-
287
- enc.buffer.preencode(state, b4a.from('hi'))
288
- t.alike(state, enc.state(0, 3))
289
- enc.buffer.preencode(state, b4a.from('hello'))
290
- t.alike(state, enc.state(0, 9))
291
- enc.buffer.preencode(state, b4a.alloc(0))
292
- t.alike(state, enc.state(0, 10))
293
-
294
- state.buffer = b4a.alloc(state.end)
295
- enc.buffer.encode(state, b4a.from('hi'))
296
- t.alike(
297
- state,
298
- enc.state(3, 10, b4a.from('\x02hi\x00\x00\x00\x00\x00\x00\x00'))
299
- )
300
- enc.buffer.encode(state, b4a.from('hello'))
301
- t.alike(state, enc.state(9, 10, b4a.from('\x02hi\x05hello\x00')))
302
- enc.buffer.encode(state, b4a.alloc(0))
303
- t.alike(state, enc.state(10, 10, b4a.from('\x02hi\x05hello\x00')))
304
-
305
- state.start = 0
306
- t.alike(enc.buffer.decode(state), b4a.from('hi'))
307
- t.alike(enc.buffer.decode(state), b4a.from('hello'))
308
- t.alike(enc.buffer.decode(state), b4a.alloc(0))
309
- t.is(state.start, state.end)
310
-
311
- t.exception(() => enc.buffer.decode(state))
312
- })
313
-
314
- test('optionalBuffer', function (t) {
315
- const state = enc.state()
316
-
317
- enc.optionalBuffer.preencode(state, b4a.from('hi'))
318
- t.alike(state, enc.state(0, 3))
319
- enc.optionalBuffer.preencode(state, b4a.from('hello'))
320
- t.alike(state, enc.state(0, 9))
321
- enc.optionalBuffer.preencode(state, null)
322
- enc.optionalBuffer.preencode(state, b4a.alloc(0))
323
- t.alike(state, enc.state(0, 11))
324
-
325
- state.buffer = b4a.alloc(state.end)
326
- enc.optionalBuffer.encode(state, b4a.from('hi'))
327
- t.alike(
328
- state,
329
- enc.state(3, 11, b4a.from('\x02hi\x00\x00\x00\x00\x00\x00\x00\x00'))
330
- )
331
- enc.optionalBuffer.encode(state, b4a.from('hello'))
332
- t.alike(state, enc.state(9, 11, b4a.from('\x02hi\x05hello\x00\x00')))
333
- enc.optionalBuffer.encode(state, null)
334
- t.alike(state, enc.state(10, 11, b4a.from('\x02hi\x05hello\x00\x00')))
335
- enc.optionalBuffer.encode(state, b4a.alloc(0))
336
- t.alike(state, enc.state(11, 11, b4a.from('\x02hi\x05hello\x00\x00')))
337
-
338
- state.start = 0
339
- t.alike(enc.optionalBuffer.decode(state), b4a.from('hi'))
340
- t.alike(enc.optionalBuffer.decode(state), b4a.from('hello'))
341
- t.is(enc.optionalBuffer.decode(state), null)
342
- t.is(enc.optionalBuffer.decode(state), null, 'empty buffer decodes as null')
343
- t.is(state.start, state.end)
344
-
345
- t.exception(() => enc.optionalBuffer.decode(state))
346
- })
347
-
348
- test('arraybuffer', function (t) {
349
- const state = enc.state()
350
-
351
- const b1 = new ArrayBuffer(4)
352
- b4a.from(b1).fill('a')
353
-
354
- const b2 = new ArrayBuffer(8)
355
- b4a.from(b2).fill('b')
356
-
357
- enc.arraybuffer.preencode(state, b1)
358
- t.alike(state, enc.state(0, 5))
359
- enc.arraybuffer.preencode(state, b2)
360
- t.alike(state, enc.state(0, 14))
361
-
362
- state.buffer = b4a.alloc(state.end)
363
- enc.arraybuffer.encode(state, b1)
364
- t.alike(
365
- state,
366
- enc.state(5, 14, b4a.from('\x04aaaa\x00\x00\x00\x00\x00\x00\x00\x00\x00'))
367
- )
368
- enc.arraybuffer.encode(state, b2)
369
- t.alike(state, enc.state(14, 14, b4a.from('\x04aaaa\x08bbbbbbbb')))
370
-
371
- state.start = 0
372
- t.alike(enc.arraybuffer.decode(state), b1)
373
- t.alike(enc.arraybuffer.decode(state), b2)
374
- t.is(state.start, state.end)
375
-
376
- t.exception(() => enc.arraybuffer.decode(state))
377
- })
378
-
379
- test('raw', function (t) {
380
- const state = enc.state()
381
-
382
- enc.raw.preencode(state, b4a.from('hi'))
383
- t.alike(state, enc.state(0, 2))
384
-
385
- state.buffer = b4a.alloc(state.end)
386
- enc.raw.encode(state, b4a.from('hi'))
387
- t.alike(state, enc.state(2, 2, b4a.from('hi')))
388
-
389
- state.start = 0
390
- t.alike(enc.raw.decode(state), b4a.from('hi'))
391
- t.is(state.start, state.end)
392
- })
393
-
394
- test('raw uint8array', function (t) {
395
- const state = enc.state()
396
-
397
- enc.raw.uint8array.preencode(state, Uint8Array.of(1, 2))
398
- t.alike(state, enc.state(0, 2))
399
- enc.raw.uint8array.preencode(state, Uint8Array.of(3, 4))
400
- t.alike(state, enc.state(0, 4))
401
-
402
- state.buffer = b4a.alloc(state.end)
403
- enc.raw.uint8array.encode(state, Uint8Array.of(1, 2))
404
- t.alike(state, enc.state(2, 4, b4a.from([1, 2, 0, 0])))
405
- enc.raw.uint8array.encode(state, Uint8Array.of(3, 4))
406
- t.alike(state, enc.state(4, 4, b4a.from([1, 2, 3, 4])))
407
-
408
- state.start = 0
409
- t.alike(enc.raw.uint8array.decode(state), Uint8Array.of(1, 2, 3, 4))
410
- t.alike(enc.raw.uint8array.decode(state), Uint8Array.of())
411
- })
412
-
413
- test('uint16array', function (t) {
414
- const state = enc.state()
415
-
416
- enc.uint16array.preencode(state, new Uint16Array([1, 2, 3]))
417
- t.alike(state, enc.state(0, 7))
418
-
419
- state.buffer = b4a.alloc(state.end)
420
- enc.uint16array.encode(state, new Uint16Array([1, 2, 3]))
421
- t.alike(state, enc.state(7, 7, b4a.from([3, 1, 0, 2, 0, 3, 0])))
422
-
423
- state.start = 0
424
- t.alike(enc.uint16array.decode(state), new Uint16Array([1, 2, 3]))
425
- t.is(state.start, state.end)
426
-
427
- t.exception(() => enc.uint16array.decode(state))
428
- })
429
-
430
- test('uint32array', function (t) {
431
- const state = enc.state()
432
-
433
- enc.uint32array.preencode(state, new Uint32Array([1]))
434
- t.alike(state, enc.state(0, 5))
435
- enc.uint32array.preencode(state, new Uint32Array([42, 43]))
436
- t.alike(state, enc.state(0, 14))
437
-
438
- state.buffer = b4a.alloc(state.end)
439
- enc.uint32array.encode(state, new Uint32Array([1]))
440
- t.alike(
441
- state,
442
- enc.state(5, 14, b4a.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
443
- )
444
- enc.uint32array.encode(state, new Uint32Array([42, 43]))
445
- t.alike(
446
- state,
447
- enc.state(14, 14, b4a.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0]))
448
- )
449
-
450
- state.start = 0
451
- t.alike(enc.uint32array.decode(state), new Uint32Array([1]))
452
- t.alike(enc.uint32array.decode(state), new Uint32Array([42, 43]))
453
- t.is(state.start, state.end)
454
-
455
- t.exception(() => enc.uint32array.decode(state))
456
- })
457
-
458
- test('int16array', function (t) {
459
- const state = enc.state()
460
-
461
- enc.int16array.preencode(state, new Int16Array([1, -2, 3]))
462
- t.alike(state, enc.state(0, 7))
463
-
464
- state.buffer = b4a.alloc(state.end)
465
- enc.int16array.encode(state, new Int16Array([1, -2, 3]))
466
- t.alike(state, enc.state(7, 7, b4a.from([3, 1, 0, 0xfe, 0xff, 3, 0])))
467
-
468
- state.start = 0
469
- t.alike(enc.int16array.decode(state), new Int16Array([1, -2, 3]))
470
- t.is(state.start, state.end)
471
-
472
- t.exception(() => enc.int16array.decode(state))
473
- })
474
-
475
- test('int32array', function (t) {
476
- const state = enc.state()
477
-
478
- enc.int32array.preencode(state, new Int32Array([1, -2, 3]))
479
- t.alike(state, enc.state(0, 13))
480
-
481
- state.buffer = b4a.alloc(state.end)
482
- enc.int32array.encode(state, new Int32Array([1, -2, 3]))
483
- t.alike(
484
- state,
485
- enc.state(
486
- 13,
487
- 13,
488
- b4a.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0])
489
- )
490
- )
491
-
492
- state.start = 0
493
- t.alike(enc.int32array.decode(state), new Int32Array([1, -2, 3]))
494
- t.is(state.start, state.end)
495
-
496
- t.exception(() => enc.int32array.decode(state))
497
- })
498
-
499
- test('biguint64array', function (t) {
500
- const state = enc.state()
501
-
502
- const arr = new BigUint64Array([0x01020304n, 0x05060708n, 0x090a0b0cn])
503
-
504
- enc.biguint64array.preencode(state, arr)
505
- t.alike(state, enc.state(0, 25))
506
-
507
- state.buffer = b4a.alloc(state.end)
508
- enc.biguint64array.encode(state, arr)
509
- t.alike(
510
- state,
511
- enc.state(
512
- 25,
513
- 25,
514
- b4a.from([
515
- 3, 0x4, 0x3, 0x2, 0x1, 0x0, 0x0, 0x0, 0x0, 0x8, 0x7, 0x6, 0x5, 0x0, 0x0,
516
- 0x0, 0x0, 0xc, 0xb, 0xa, 0x9, 0x0, 0x0, 0x0, 0x0
517
- ])
518
- )
519
- )
520
-
521
- state.start = 0
522
- t.alike(enc.biguint64array.decode(state), arr)
523
- t.is(state.start, state.end)
524
-
525
- t.exception(() => enc.biguint64array.decode(state))
526
- })
527
-
528
- test('bigint64array', function (t) {
529
- const state = enc.state()
530
-
531
- const arr = new BigInt64Array([-0x01020304n, 0x05060708n, -0x090a0b0cn])
532
-
533
- enc.bigint64array.preencode(state, arr)
534
- t.alike(state, enc.state(0, 25))
535
-
536
- state.buffer = b4a.alloc(state.end)
537
- enc.bigint64array.encode(state, arr)
538
- t.alike(
539
- state,
540
- enc.state(
541
- 25,
542
- 25,
543
- b4a.from([
544
- 3, 0xfc, 0xfc, 0xfd, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x8, 0x7, 0x6, 0x5,
545
- 0x0, 0x0, 0x0, 0x0, 0xf4, 0xf4, 0xf5, 0xf6, 0xff, 0xff, 0xff, 0xff
546
- ])
547
- )
548
- )
549
-
550
- state.start = 0
551
- t.alike(enc.bigint64array.decode(state), arr)
552
- t.is(state.start, state.end)
553
-
554
- t.exception(() => enc.bigint64array.decode(state))
555
- })
556
-
557
- test('float32array', function (t) {
558
- const state = enc.state()
559
-
560
- enc.float32array.preencode(state, new Float32Array([1.1, -2.2, 3.3]))
561
- t.alike(state, enc.state(0, 13))
562
-
563
- state.buffer = b4a.alloc(state.end)
564
- enc.float32array.encode(state, new Float32Array([1.1, -2.2, 3.3]))
565
- t.alike(
566
- state,
567
- enc.state(
568
- 13,
569
- 13,
570
- b4a.from([
571
- 3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53,
572
- 0x40
573
- ])
574
- )
575
- )
576
-
577
- state.start = 0
578
- t.alike(enc.float32array.decode(state), new Float32Array([1.1, -2.2, 3.3]))
579
- t.is(state.start, state.end)
580
-
581
- t.exception(() => enc.float32array.decode(state))
582
- })
583
-
584
- test('float64array', function (t) {
585
- const state = enc.state()
586
-
587
- enc.float64array.preencode(state, new Float64Array([1.1, -2.2, 3.3]))
588
- t.alike(state, enc.state(0, 25))
589
-
590
- state.buffer = b4a.alloc(state.end)
591
- enc.float64array.encode(state, new Float64Array([1.1, -2.2, 3.3]))
592
- t.alike(
593
- state,
594
- enc.state(
595
- 25,
596
- 25,
597
- b4a.from([
598
- 3, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f, 0x9a, 0x99, 0x99,
599
- 0x99, 0x99, 0x99, 0x01, 0xc0, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a,
600
- 0x40
601
- ])
602
- )
603
- )
604
-
605
- state.start = 0
606
- t.alike(enc.float64array.decode(state), new Float64Array([1.1, -2.2, 3.3]))
607
- t.is(state.start, state.end)
608
-
609
- t.exception(() => enc.float64array.decode(state))
610
- })
611
-
612
- test('string', function (t) {
613
- const state = enc.state()
614
-
615
- enc.string.preencode(state, '🌾')
616
- t.alike(state, enc.state(0, 5))
617
- enc.string.preencode(state, 'høsten er fin')
618
- t.alike(state, enc.state(0, 20))
619
-
620
- state.buffer = b4a.alloc(state.end)
621
- enc.string.encode(state, '🌾')
622
- t.alike(
623
- state,
624
- enc.state(
625
- 5,
626
- 20,
627
- b4a.from(
628
- '\x04🌾\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
629
- )
630
- )
631
- )
632
- enc.string.encode(state, 'høsten er fin')
633
- t.alike(state, enc.state(20, 20, b4a.from('\x04🌾\x0ehøsten er fin')))
634
-
635
- state.start = 0
636
- t.is(enc.string.decode(state), '🌾')
637
- t.is(enc.string.decode(state), 'høsten er fin')
638
- t.is(state.start, state.end)
639
-
640
- t.exception(() => enc.string.decode(state))
641
- })
642
-
643
- test('raw string', function (t) {
644
- const state = enc.state()
645
-
646
- enc.raw.string.preencode(state, 'hello')
647
- t.alike(state, enc.state(0, 5))
648
- enc.raw.string.preencode(state, ' world')
649
- t.alike(state, enc.state(0, 11))
650
-
651
- state.buffer = b4a.alloc(state.end)
652
- enc.raw.string.encode(state, 'hello')
653
- enc.raw.string.encode(state, ' world')
654
- t.alike(state, enc.state(11, 11, b4a.from('hello world')))
655
-
656
- state.start = 0
657
- t.is(enc.raw.string.decode(state), 'hello world')
658
- t.is(enc.raw.string.decode(state), '')
659
- })
660
-
661
- test('raw buffer', function (t) {
662
- const state = enc.state()
663
-
664
- enc.raw.string.preencode(state, b4a.from('hello'))
665
- t.alike(state, enc.state(0, 5))
666
- enc.raw.string.preencode(state, b4a.from(' world'))
667
- t.alike(state, enc.state(0, 11))
668
-
669
- state.buffer = b4a.alloc(state.end)
670
- enc.raw.buffer.encode(state, b4a.from('hello'))
671
- enc.raw.buffer.encode(state, b4a.from(' world'))
672
- t.alike(state, enc.state(11, 11, b4a.from('hello world')))
673
-
674
- state.start = 0
675
- t.alike(enc.raw.buffer.decode(state), b4a.from('hello world'))
676
- t.alike(enc.raw.buffer.decode(state), b4a.alloc(0))
677
- })
678
-
679
- test('fixed32', function (t) {
680
- const state = enc.state()
681
-
682
- enc.fixed32.preencode(state, b4a.alloc(32).fill('a'))
683
- t.alike(state, enc.state(0, 32))
684
- enc.fixed32.preencode(state, b4a.alloc(32).fill('b'))
685
- t.alike(state, enc.state(0, 64))
686
-
687
- state.buffer = b4a.alloc(state.end)
688
- enc.fixed32.encode(state, b4a.alloc(32).fill('a'))
689
- t.alike(state, enc.state(32, 64, b4a.alloc(64).fill('a', 0, 32)))
690
- enc.fixed32.encode(state, b4a.alloc(32).fill('b'))
691
- t.alike(
692
- state,
693
- enc.state(64, 64, b4a.alloc(64).fill('a', 0, 32).fill('b', 32, 64))
694
- )
695
-
696
- state.start = 0
697
- t.alike(enc.fixed32.decode(state), b4a.alloc(32).fill('a'))
698
- t.alike(enc.fixed32.decode(state), b4a.alloc(32).fill('b'))
699
- t.is(state.start, state.end)
700
-
701
- t.exception(() => enc.fixed32.decode(state))
702
- })
703
-
704
- test('fixed64', function (t) {
705
- const state = enc.state()
706
-
707
- enc.fixed64.preencode(state, b4a.alloc(64).fill('a'))
708
- t.alike(state, enc.state(0, 64))
709
- enc.fixed64.preencode(state, b4a.alloc(64).fill('b'))
710
- t.alike(state, enc.state(0, 128))
711
-
712
- state.buffer = b4a.alloc(state.end)
713
- enc.fixed64.encode(state, b4a.alloc(64).fill('a'))
714
- t.alike(state, enc.state(64, 128, b4a.alloc(128).fill('a', 0, 64)))
715
- enc.fixed64.encode(state, b4a.alloc(64).fill('b'))
716
- t.alike(
717
- state,
718
- enc.state(128, 128, b4a.alloc(128).fill('a', 0, 64).fill('b', 64, 128))
719
- )
720
-
721
- state.start = 0
722
- t.alike(enc.fixed64.decode(state), b4a.alloc(64).fill('a'))
723
- t.alike(enc.fixed64.decode(state), b4a.alloc(64).fill('b'))
724
- t.is(state.start, state.end)
725
-
726
- t.exception(() => enc.fixed64.decode(state))
727
- })
728
-
729
- test('fixed n', function (t) {
730
- const state = enc.state()
731
- const fixed = enc.fixed(3)
732
-
733
- fixed.preencode(state, b4a.alloc(3).fill('a'))
734
- t.alike(state, enc.state(0, 3))
735
- fixed.preencode(state, b4a.alloc(3).fill('b'))
736
- t.alike(state, enc.state(0, 6))
737
-
738
- state.buffer = b4a.alloc(state.end)
739
- fixed.encode(state, b4a.alloc(3).fill('a'))
740
- t.alike(state, enc.state(3, 6, b4a.alloc(6).fill('a', 0, 3)))
741
- fixed.encode(state, b4a.alloc(3).fill('b'))
742
- t.alike(state, enc.state(6, 6, b4a.alloc(6).fill('a', 0, 3).fill('b', 3, 6)))
743
-
744
- state.start = 0
745
- t.alike(fixed.decode(state), b4a.alloc(3).fill('a'))
746
- t.alike(fixed.decode(state), b4a.alloc(3).fill('b'))
747
- t.is(state.start, state.end)
748
-
749
- t.exception(() => fixed.decode(state))
750
- state.start = 4
751
- t.exception(() => fixed.decode(state))
752
- })
753
-
754
- test('error for incorrect buffer sizes when encoding fixed-length buffers', function (t) {
755
- const smallbuf = b4a.from('aa', 'hex')
756
- const bigBuf = b4a.from('aa'.repeat(500), 'hex')
757
-
758
- t.exception(() => enc.encode(enc.fixed32, smallbuf), /Incorrect buffer size/)
759
- t.exception(() => enc.encode(enc.fixed64, smallbuf), /Incorrect buffer size/)
760
- t.exception(
761
- () => enc.encode(enc.fixed(100), smallbuf),
762
- /Incorrect buffer size/
763
- )
764
-
765
- t.exception(() => enc.encode(enc.fixed32, bigBuf), /Incorrect buffer size/)
766
- t.exception(() => enc.encode(enc.fixed64, bigBuf), /Incorrect buffer size/)
767
- t.exception(() => enc.encode(enc.fixed(100), bigBuf), /Incorrect buffer size/)
768
- })
769
-
770
- test('array', function (t) {
771
- const state = enc.state()
772
- const arr = enc.array(enc.bool)
773
-
774
- arr.preencode(state, [true, false, true])
775
- t.alike(state, enc.state(0, 4))
776
- arr.preencode(state, [false, false, true, true])
777
- t.alike(state, enc.state(0, 9))
778
-
779
- state.buffer = b4a.alloc(state.end)
780
- arr.encode(state, [true, false, true])
781
- t.alike(state, enc.state(4, 9, b4a.from([3, 1, 0, 1, 0, 0, 0, 0, 0])))
782
- arr.encode(state, [false, false, true, true])
783
- t.alike(state, enc.state(9, 9, b4a.from([3, 1, 0, 1, 4, 0, 0, 1, 1])))
784
-
785
- state.start = 0
786
- t.alike(arr.decode(state), [true, false, true])
787
- t.alike(arr.decode(state), [false, false, true, true])
788
- t.is(state.start, state.end)
789
-
790
- t.exception(() => arr.decode(state))
791
- })
792
-
793
- test('raw array', function (t) {
794
- const state = enc.state()
795
- const arr = enc.raw.array(enc.bool)
796
-
797
- arr.preencode(state, [true])
798
- t.alike(state, enc.state(0, 1))
799
- arr.preencode(state, [true])
800
- t.alike(state, enc.state(0, 2))
801
-
802
- state.buffer = b4a.alloc(state.end)
803
- arr.encode(state, [true])
804
- t.alike(state, enc.state(1, 2, b4a.from([1, 0])))
805
- arr.encode(state, [true])
806
- t.alike(state, enc.state(2, 2, b4a.from([1, 1])))
807
-
808
- state.start = 0
809
- t.alike(arr.decode(state), [true, true])
810
- t.alike(arr.decode(state), [])
811
- })
812
-
813
- test('json', function (t) {
814
- const state = enc.state()
815
-
816
- enc.json.preencode(state, { a: 1, b: 2 })
817
- t.alike(state, enc.state(0, 14))
818
-
819
- state.buffer = b4a.alloc(state.end)
820
- enc.json.encode(state, { a: 1, b: 2 })
821
- t.alike(
822
- state,
823
- enc.state(14, 14, b4a.concat([b4a.from([13]), b4a.from('{"a":1,"b":2}')]))
824
- )
825
-
826
- state.start = 0
827
- t.alike(enc.json.decode(state), { a: 1, b: 2 })
828
-
829
- t.exception(() => enc.json.decode(state))
830
- })
831
-
832
- test('raw json', function (t) {
833
- const state = enc.state()
834
-
835
- enc.raw.json.preencode(state, { a: 1, b: 2 })
836
- t.alike(state, enc.state(0, 13))
837
-
838
- state.buffer = b4a.alloc(state.end)
839
- enc.raw.json.encode(state, { a: 1, b: 2 })
840
- t.alike(state, enc.state(13, 13, b4a.from('{"a":1,"b":2}')))
841
-
842
- state.start = 0
843
- t.alike(enc.raw.json.decode(state), { a: 1, b: 2 })
844
-
845
- t.exception(() => enc.json.decode(state))
846
- })
847
-
848
- test('lexint: big numbers', function (t) {
849
- t.plan(1)
850
-
851
- let prev = enc.encode(enc.lexint, 0)
852
-
853
- let n
854
- let skip = 1
855
-
856
- for (n = 1; n < Number.MAX_VALUE; n += skip) {
857
- const cur = enc.encode(enc.lexint, n)
858
- if (b4a.compare(cur, prev) < 1) break
859
- prev = cur
860
- skip = 1 + Math.pow(245, Math.ceil(Math.log(n) / Math.log(256)))
861
- }
862
- t.is(n, Infinity)
863
- })
864
-
865
- test('lexint: range precision', function (t) {
866
- t.plan(2)
867
- const a = 1e55
868
- const b = 1.0000000000001e55
869
- const ha = enc.encode(enc.lexint, a).toString('hex')
870
- const hb = enc.encode(enc.lexint, b).toString('hex')
871
- t.not(a, b)
872
- t.not(ha, hb)
873
- })
874
-
875
- test('lexint: range precision', function (t) {
876
- let prev = enc.encode(enc.lexint, 0)
877
- const skip = 0.000000001e55
878
- for (let i = 0, n = 1e55; i < 1000; n = 1e55 + skip * ++i) {
879
- const cur = enc.encode(enc.lexint, n)
880
- if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev')
881
- prev = cur
882
- }
883
- t.ok(true)
884
- t.end()
885
- })
886
-
887
- test('lexint: small numbers', function (t) {
888
- let prev = enc.encode(enc.lexint, 0)
889
- for (let n = 1; n < 256 * 256 * 16; n++) {
890
- const cur = enc.encode(enc.lexint, n)
891
- if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev')
892
- prev = cur
893
- }
894
- t.ok(true)
895
- t.end()
896
- })
897
-
898
- test('lexint: throws', function (t) {
899
- t.exception(() => {
900
- enc.decode(enc.lexint, b4a.alloc(1, 251))
901
- })
902
-
903
- let num = 252
904
-
905
- const state = enc.state()
906
-
907
- enc.lexint.preencode(state, num)
908
- state.buffer = b4a.alloc(state.end - state.start)
909
- enc.lexint.encode(state, num)
910
-
911
- t.exception(() => {
912
- enc.decode(
913
- enc.lexint,
914
- state.buffer.subarray(0, state.buffer.byteLength - 2)
915
- )
916
- })
917
-
918
- num <<= 8
919
-
920
- state.start = 0
921
- state.end = 0
922
- state.buffer = null
923
-
924
- enc.lexint.preencode(state, num)
925
- state.buffer = b4a.alloc(state.end - state.start)
926
- enc.lexint.encode(state, num)
927
-
928
- t.exception(() => {
929
- enc.decode(
930
- enc.lexint,
931
- state.buffer.subarray(0, state.buffer.byteLength - 2)
932
- )
933
- })
934
-
935
- num <<= 8
936
-
937
- state.start = 0
938
- state.end = 0
939
- state.buffer = null
940
-
941
- enc.lexint.preencode(state, num)
942
- state.buffer = b4a.alloc(state.end - state.start)
943
- enc.lexint.encode(state, num)
944
-
945
- t.exception(() => {
946
- enc.decode(
947
- enc.lexint,
948
- state.buffer.subarray(0, state.buffer.byteLength - 2)
949
- )
950
- })
951
-
952
- num *= 256
953
-
954
- state.start = 0
955
- state.end = 0
956
- state.buffer = null
957
-
958
- enc.lexint.preencode(state, num)
959
- state.buffer = b4a.alloc(state.end - state.start)
960
- enc.lexint.encode(state, num)
961
-
962
- t.exception(() => {
963
- enc.decode(
964
- enc.lexint,
965
- state.buffer.subarray(0, state.buffer.byteLength - 2)
966
- )
967
- })
968
-
969
- num *= 256 * 256
970
-
971
- state.start = 0
972
- state.end = 0
973
- state.buffer = null
974
-
975
- enc.lexint.preencode(state, num)
976
- state.buffer = b4a.alloc(state.end - state.start)
977
- enc.lexint.encode(state, num)
978
-
979
- t.exception(() => {
980
- enc.decode(
981
- enc.lexint,
982
- state.buffer.subarray(0, state.buffer.byteLength - 2)
983
- )
984
- })
985
-
986
- t.end()
987
- })
988
-
989
- test('lexint: unpack', function (t) {
990
- let n
991
- let skip = 1
992
-
993
- for (n = 1; n < Number.MAX_VALUE; n += skip) {
994
- const cur = enc.encode(enc.lexint, n)
995
- compare(n, enc.decode(enc.lexint, cur))
996
- skip = 1 + Math.pow(245, Math.ceil(Math.log(n) / Math.log(256)))
997
- }
998
- t.is(n, Infinity)
999
- t.end()
1000
-
1001
- function compare(a, b) {
1002
- const desc = a + ' !=~ ' + b
1003
- if (/e\+\d+$/.test(a) || /e\+\d+$/.test(b)) {
1004
- if (
1005
- String(a).slice(0, 8) !== String(b).slice(0, 8) ||
1006
- /e\+(\d+)$/.exec(a)[1] !== /e\+(\d+)$/.exec(b)[1]
1007
- ) {
1008
- t.fail(desc)
1009
- }
1010
- } else {
1011
- if (
1012
- String(a).slice(0, 8) !== String(b).slice(0, 8) ||
1013
- String(a).length !== String(b).length
1014
- ) {
1015
- t.fail(desc)
1016
- }
1017
- }
1018
- }
1019
- })
1020
-
1021
- test('date', function (t) {
1022
- const d = new Date()
1023
-
1024
- t.alike(enc.decode(enc.date, enc.encode(enc.date, d)), d)
1025
- })
1026
-
1027
- test('any', function (t) {
1028
- const o = {
1029
- hello: 'world',
1030
- num: 42,
1031
- neg: -42,
1032
- arr: [{ yes: 1 }, { no: false }],
1033
- nest: {},
1034
- today: new Date(),
1035
- float: 0.54
1036
- }
1037
-
1038
- t.alike(enc.decode(enc.any, enc.encode(enc.any, o)), o)
1039
-
1040
- const arr = new Array(3)
1041
-
1042
- t.alike(enc.decode(enc.any, enc.encode(enc.any, arr)), [null, null, null])
1043
- })
1044
-
1045
- test('framed', function (t) {
1046
- const e = enc.frame(enc.uint)
1047
- t.alike(enc.encode(e, 42), b4a.from([0x01, 0x2a]))
1048
- t.alike(enc.decode(e, b4a.from([0x01, 0x2a])), 42)
1049
- t.alike(enc.encode(e, 4200), b4a.from([0x03, 0xfd, 0x68, 0x10]))
1050
- t.alike(enc.decode(e, b4a.from([0x03, 0xfd, 0x68, 0x10])), 4200)
1051
- })
1052
-
1053
- test('port', function (t) {
1054
- const p = 0x1234
1055
- const buf = Buffer.from([0x34, 0x12])
1056
-
1057
- t.alike(enc.encode(enc.port, p), buf)
1058
- t.alike(enc.decode(enc.port, buf), p)
1059
- })
1060
-
1061
- test('ipv4', function (t) {
1062
- const ip = '1.2.3.4'
1063
- const buf = Buffer.from([1, 2, 3, 4])
1064
-
1065
- t.alike(enc.encode(enc.ipv4, ip), buf)
1066
- t.alike(enc.decode(enc.ipv4, buf), ip)
1067
- })
1068
-
1069
- test('ipv4 + port', function (t) {
1070
- const host = '1.2.3.4'
1071
- const port = 1234
1072
-
1073
- t.alike(
1074
- enc.decode(enc.ipv4Address, enc.encode(enc.ipv4Address, { host, port })),
1075
- {
1076
- host,
1077
- family: 4,
1078
- port
1079
- }
1080
- )
1081
- })
1082
-
1083
- test('ipv6', function (t) {
1084
- const ip = '1:2:3:4:5:6:7:8'
1085
- const buf = Buffer.from([0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8])
1086
-
1087
- t.alike(enc.encode(enc.ipv6, ip), buf)
1088
- t.alike(enc.decode(enc.ipv6, buf), ip)
1089
-
1090
- t.test('abbreviated', function (t) {
1091
- const buf = Buffer.from([0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 8])
1092
-
1093
- t.alike(enc.encode(enc.ipv6, '1:2::7:8'), buf)
1094
- t.alike(enc.decode(enc.ipv6, buf), '1:2:0:0:0:0:7:8')
1095
- })
1096
-
1097
- t.test('prefix abbreviated', function (t) {
1098
- const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0, 7, 0, 8])
1099
-
1100
- t.alike(enc.encode(enc.ipv6, '::5:6:7:8'), buf)
1101
- t.alike(enc.decode(enc.ipv6, buf), '0:0:0:0:5:6:7:8')
1102
- })
1103
-
1104
- t.test('suffix abbreviated', function (t) {
1105
- const buf = Buffer.from([0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0])
1106
-
1107
- t.alike(enc.encode(enc.ipv6, '1:2:3:4::'), buf)
1108
- t.alike(enc.decode(enc.ipv6, buf), '1:2:3:4:0:0:0:0')
1109
- })
1110
-
1111
- t.test('any', function (t) {
1112
- const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
1113
-
1114
- t.alike(enc.encode(enc.ipv6, '::'), buf)
1115
- t.alike(enc.decode(enc.ipv6, buf), '0:0:0:0:0:0:0:0')
1116
- })
1117
-
1118
- t.test('lowercase hex', function (t) {
1119
- const buf = Buffer.from([
1120
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd
1121
- ])
1122
-
1123
- t.alike(enc.encode(enc.ipv6, '::abcd'), buf)
1124
- t.alike(enc.decode(enc.ipv6, buf), '0:0:0:0:0:0:0:abcd')
1125
- })
1126
-
1127
- t.test('uppercase hex', function (t) {
1128
- const buf = Buffer.from([
1129
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd
1130
- ])
1131
-
1132
- t.alike(enc.encode(enc.ipv6, '::ABCD'), buf)
1133
- t.alike(enc.decode(enc.ipv6, buf), '0:0:0:0:0:0:0:abcd')
1134
- })
1135
- })
1136
-
1137
- test('ipv6 + port', function (t) {
1138
- const host = '1:2:3:4:5:6:7:8'
1139
- const port = 1234
1140
-
1141
- t.alike(
1142
- enc.decode(enc.ipv6Address, enc.encode(enc.ipv6Address, { host, port })),
1143
- {
1144
- host,
1145
- family: 6,
1146
- port
1147
- }
1148
- )
1149
- })
1150
-
1151
- test('dual ip', function (t) {
1152
- {
1153
- const host = '1.2.3.4'
1154
-
1155
- t.alike(enc.decode(enc.ip, enc.encode(enc.ip, host)), host, 'ipv4')
1156
- }
1157
- {
1158
- const host = '1:2:3:4:5:6:7:8'
1159
-
1160
- t.alike(enc.decode(enc.ip, enc.encode(enc.ip, host)), host, 'ipv6')
1161
- }
1162
- })
1163
-
1164
- test('dual ip + port', function (t) {
1165
- const port = 1234
1166
-
1167
- {
1168
- const host = '1.2.3.4'
1169
-
1170
- t.alike(
1171
- enc.decode(enc.ipAddress, enc.encode(enc.ipAddress, { host, port })),
1172
- {
1173
- host,
1174
- family: 4,
1175
- port
1176
- },
1177
- 'ipv4'
1178
- )
1179
- }
1180
- {
1181
- const host = '1:2:3:4:5:6:7:8'
1182
-
1183
- t.alike(
1184
- enc.decode(enc.ipAddress, enc.encode(enc.ipAddress, { host, port })),
1185
- {
1186
- host,
1187
- family: 6,
1188
- port
1189
- },
1190
- 'ipv6'
1191
- )
1192
- }
1193
- })
1194
-
1195
- test('record', function (t) {
1196
- const encoding = enc.record(enc.string, enc.string)
1197
-
1198
- t.alike(
1199
- enc.decode(encoding, enc.encode(encoding, { a: 'hello', b: 'world' })),
1200
- Object.assign(Object.create(null), {
1201
- a: 'hello',
1202
- b: 'world'
1203
- })
1204
- )
1205
- })
1206
-
1207
- test('record - nested', function (t) {
1208
- const encoding = enc.record(enc.string, enc.record(enc.string, enc.string))
1209
-
1210
- t.alike(
1211
- enc.decode(
1212
- encoding,
1213
- enc.encode(encoding, {
1214
- a: { b: 'record' },
1215
- c: { d: 'nested', e: 'test' }
1216
- })
1217
- ),
1218
- Object.assign(Object.create(null), {
1219
- a: Object.assign(Object.create(null), { b: 'record' }),
1220
- c: Object.assign(Object.create(null), { d: 'nested', e: 'test' })
1221
- })
1222
- )
1223
- })
1224
-
1225
- test('stringRecord', function (t) {
1226
- t.alike(
1227
- enc.decode(
1228
- enc.stringRecord,
1229
- enc.encode(enc.stringRecord, { a: 'hello', b: 'world' })
1230
- ),
1231
- Object.assign(Object.create(null), {
1232
- a: 'hello',
1233
- b: 'world'
1234
- })
1235
- )
1236
- })