compact-encoding 2.16.1 → 2.18.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
@@ -3,16 +3,16 @@ const b4a = require('b4a')
3
3
  const { BE } = require('./endian')
4
4
 
5
5
  exports.state = function (start = 0, end = 0, buffer = null) {
6
- return { start, end, buffer, cache: null }
6
+ return { start, end, buffer }
7
7
  }
8
8
 
9
- const raw = exports.raw = require('./raw')
9
+ const raw = (exports.raw = require('./raw'))
10
10
 
11
- const uint = exports.uint = {
12
- preencode (state, n) {
11
+ const uint = (exports.uint = {
12
+ preencode(state, n) {
13
13
  state.end += n <= 0xfc ? 1 : n <= 0xffff ? 3 : n <= 0xffffffff ? 5 : 9
14
14
  },
15
- encode (state, n) {
15
+ encode(state, n) {
16
16
  if (n <= 0xfc) uint8.encode(state, n)
17
17
  else if (n <= 0xffff) {
18
18
  state.buffer[state.start++] = 0xfd
@@ -25,58 +25,55 @@ const uint = exports.uint = {
25
25
  uint64.encode(state, n)
26
26
  }
27
27
  },
28
- decode (state) {
28
+ decode(state) {
29
29
  const a = uint8.decode(state)
30
30
  if (a <= 0xfc) return a
31
31
  if (a === 0xfd) return uint16.decode(state)
32
32
  if (a === 0xfe) return uint32.decode(state)
33
33
  return uint64.decode(state)
34
34
  }
35
- }
35
+ })
36
36
 
37
- const uint8 = exports.uint8 = {
38
- preencode (state, n) {
37
+ const uint8 = (exports.uint8 = {
38
+ preencode(state, n) {
39
39
  state.end += 1
40
40
  },
41
- encode (state, n) {
41
+ encode(state, n) {
42
42
  validateUint(n)
43
43
  state.buffer[state.start++] = n
44
44
  },
45
- decode (state) {
45
+ decode(state) {
46
46
  if (state.start >= state.end) throw new Error('Out of bounds')
47
47
  return state.buffer[state.start++]
48
48
  }
49
- }
49
+ })
50
50
 
51
- const uint16 = exports.uint16 = {
52
- preencode (state, n) {
51
+ const uint16 = (exports.uint16 = {
52
+ preencode(state, n) {
53
53
  state.end += 2
54
54
  },
55
- encode (state, n) {
55
+ encode(state, n) {
56
56
  validateUint(n)
57
57
  state.buffer[state.start++] = n
58
58
  state.buffer[state.start++] = n >>> 8
59
59
  },
60
- decode (state) {
60
+ decode(state) {
61
61
  if (state.end - state.start < 2) throw new Error('Out of bounds')
62
- return (
63
- state.buffer[state.start++] +
64
- state.buffer[state.start++] * 0x100
65
- )
62
+ return state.buffer[state.start++] + state.buffer[state.start++] * 0x100
66
63
  }
67
- }
64
+ })
68
65
 
69
- const uint24 = exports.uint24 = {
70
- preencode (state, n) {
66
+ const uint24 = (exports.uint24 = {
67
+ preencode(state, n) {
71
68
  state.end += 3
72
69
  },
73
- encode (state, n) {
70
+ encode(state, n) {
74
71
  validateUint(n)
75
72
  state.buffer[state.start++] = n
76
73
  state.buffer[state.start++] = n >>> 8
77
74
  state.buffer[state.start++] = n >>> 16
78
75
  },
79
- decode (state) {
76
+ decode(state) {
80
77
  if (state.end - state.start < 3) throw new Error('Out of bounds')
81
78
  return (
82
79
  state.buffer[state.start++] +
@@ -84,20 +81,20 @@ const uint24 = exports.uint24 = {
84
81
  state.buffer[state.start++] * 0x10000
85
82
  )
86
83
  }
87
- }
84
+ })
88
85
 
89
- const uint32 = exports.uint32 = {
90
- preencode (state, n) {
86
+ const uint32 = (exports.uint32 = {
87
+ preencode(state, n) {
91
88
  state.end += 4
92
89
  },
93
- encode (state, n) {
90
+ encode(state, n) {
94
91
  validateUint(n)
95
92
  state.buffer[state.start++] = n
96
93
  state.buffer[state.start++] = n >>> 8
97
94
  state.buffer[state.start++] = n >>> 16
98
95
  state.buffer[state.start++] = n >>> 24
99
96
  },
100
- decode (state) {
97
+ decode(state) {
101
98
  if (state.end - state.start < 4) throw new Error('Out of bounds')
102
99
  return (
103
100
  state.buffer[state.start++] +
@@ -106,73 +103,73 @@ const uint32 = exports.uint32 = {
106
103
  state.buffer[state.start++] * 0x1000000
107
104
  )
108
105
  }
109
- }
106
+ })
110
107
 
111
- const uint40 = exports.uint40 = {
112
- preencode (state, n) {
108
+ const uint40 = (exports.uint40 = {
109
+ preencode(state, n) {
113
110
  state.end += 5
114
111
  },
115
- encode (state, n) {
112
+ encode(state, n) {
116
113
  validateUint(n)
117
114
  const r = Math.floor(n / 0x100)
118
115
  uint8.encode(state, n)
119
116
  uint32.encode(state, r)
120
117
  },
121
- decode (state) {
118
+ decode(state) {
122
119
  if (state.end - state.start < 5) throw new Error('Out of bounds')
123
120
  return uint8.decode(state) + 0x100 * uint32.decode(state)
124
121
  }
125
- }
122
+ })
126
123
 
127
- const uint48 = exports.uint48 = {
128
- preencode (state, n) {
124
+ const uint48 = (exports.uint48 = {
125
+ preencode(state, n) {
129
126
  state.end += 6
130
127
  },
131
- encode (state, n) {
128
+ encode(state, n) {
132
129
  validateUint(n)
133
130
  const r = Math.floor(n / 0x10000)
134
131
  uint16.encode(state, n)
135
132
  uint32.encode(state, r)
136
133
  },
137
- decode (state) {
134
+ decode(state) {
138
135
  if (state.end - state.start < 6) throw new Error('Out of bounds')
139
136
  return uint16.decode(state) + 0x10000 * uint32.decode(state)
140
137
  }
141
- }
138
+ })
142
139
 
143
- const uint56 = exports.uint56 = {
144
- preencode (state, n) {
140
+ const uint56 = (exports.uint56 = {
141
+ preencode(state, n) {
145
142
  state.end += 7
146
143
  },
147
- encode (state, n) {
144
+ encode(state, n) {
148
145
  validateUint(n)
149
146
  const r = Math.floor(n / 0x1000000)
150
147
  uint24.encode(state, n)
151
148
  uint32.encode(state, r)
152
149
  },
153
- decode (state) {
150
+ decode(state) {
154
151
  if (state.end - state.start < 7) throw new Error('Out of bounds')
155
152
  return uint24.decode(state) + 0x1000000 * uint32.decode(state)
156
153
  }
157
- }
154
+ })
158
155
 
159
- const uint64 = exports.uint64 = {
160
- preencode (state, n) {
156
+ const uint64 = (exports.uint64 = {
157
+ preencode(state, n) {
161
158
  state.end += 8
162
159
  },
163
- encode (state, n) {
160
+ encode(state, n) {
164
161
  validateUint(n)
165
162
  const r = Math.floor(n / 0x100000000)
166
163
  uint32.encode(state, n)
167
164
  uint32.encode(state, r)
168
165
  },
169
- decode (state) {
166
+ decode(state) {
170
167
  if (state.end - state.start < 8) throw new Error('Out of bounds')
171
168
  return uint32.decode(state) + 0x100000000 * uint32.decode(state)
172
169
  }
173
- }
170
+ })
174
171
 
175
- const int = exports.int = zigZagInt(uint)
172
+ const int = (exports.int = zigZagInt(uint))
176
173
  exports.int8 = zigZagInt(uint8)
177
174
  exports.int16 = zigZagInt(uint16)
178
175
  exports.int24 = zigZagInt(uint24)
@@ -182,70 +179,95 @@ exports.int48 = zigZagInt(uint48)
182
179
  exports.int56 = zigZagInt(uint56)
183
180
  exports.int64 = zigZagInt(uint64)
184
181
 
185
- const biguint64 = exports.biguint64 = {
186
- preencode (state, n) {
182
+ const biguint64 = (exports.biguint64 = {
183
+ preencode(state, n) {
187
184
  state.end += 8
188
185
  },
189
- encode (state, n) {
190
- const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
186
+ encode(state, n) {
187
+ const view = new DataView(
188
+ state.buffer.buffer,
189
+ state.start + state.buffer.byteOffset,
190
+ 8
191
+ )
191
192
  view.setBigUint64(0, n, true) // little endian
192
193
  state.start += 8
193
194
  },
194
- decode (state) {
195
+ decode(state) {
195
196
  if (state.end - state.start < 8) throw new Error('Out of bounds')
196
- const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
197
+ const view = new DataView(
198
+ state.buffer.buffer,
199
+ state.start + state.buffer.byteOffset,
200
+ 8
201
+ )
197
202
  const n = view.getBigUint64(0, true) // little endian
198
203
  state.start += 8
199
204
  return n
200
205
  }
201
- }
206
+ })
202
207
 
203
208
  exports.bigint64 = zigZagBigInt(biguint64)
204
209
 
205
- const biguint = exports.biguint = {
206
- preencode (state, n) {
210
+ const biguint = (exports.biguint = {
211
+ preencode(state, n) {
207
212
  let len = 0
208
213
  for (let m = n; m; m = m >> 64n) len++
209
214
  uint.preencode(state, len)
210
215
  state.end += 8 * len
211
216
  },
212
- encode (state, n) {
217
+ encode(state, n) {
213
218
  let len = 0
214
219
  for (let m = n; m; m = m >> 64n) len++
215
220
  uint.encode(state, len)
216
- const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8 * len)
221
+ const view = new DataView(
222
+ state.buffer.buffer,
223
+ state.start + state.buffer.byteOffset,
224
+ 8 * len
225
+ )
217
226
  for (let m = n, i = 0; m; m = m >> 64n, i += 8) {
218
227
  view.setBigUint64(i, BigInt.asUintN(64, m), true) // little endian
219
228
  }
220
229
  state.start += 8 * len
221
230
  },
222
- decode (state) {
231
+ decode(state) {
223
232
  const len = uint.decode(state)
224
233
  if (state.end - state.start < 8 * len) throw new Error('Out of bounds')
225
- const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8 * len)
234
+ const view = new DataView(
235
+ state.buffer.buffer,
236
+ state.start + state.buffer.byteOffset,
237
+ 8 * len
238
+ )
226
239
  let n = 0n
227
- for (let i = len - 1; i >= 0; i--) n = (n << 64n) + view.getBigUint64(i * 8, true) // little endian
240
+ for (let i = len - 1; i >= 0; i--)
241
+ n = (n << 64n) + view.getBigUint64(i * 8, true) // little endian
228
242
  state.start += 8 * len
229
243
  return n
230
244
  }
231
- }
245
+ })
232
246
 
233
247
  exports.bigint = zigZagBigInt(biguint)
234
248
 
235
249
  exports.lexint = require('./lexint')
236
250
 
237
251
  exports.float32 = {
238
- preencode (state, n) {
252
+ preencode(state, n) {
239
253
  state.end += 4
240
254
  },
241
- encode (state, n) {
242
- const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 4)
255
+ encode(state, n) {
256
+ const view = new DataView(
257
+ state.buffer.buffer,
258
+ state.start + state.buffer.byteOffset,
259
+ 4
260
+ )
243
261
  view.setFloat32(0, n, true) // little endian
244
262
  state.start += 4
245
263
  },
246
- decode (state) {
264
+ decode(state) {
247
265
  if (state.end - state.start < 4) throw new Error('Out of bounds')
248
- const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 4)
266
+ const view = new DataView(
267
+ state.buffer.buffer,
268
+ state.start + state.buffer.byteOffset,
269
+ 4
270
+ )
249
271
  const float = view.getFloat32(0, true) // little endian
250
272
  state.start += 4
251
273
  return float
@@ -253,58 +275,66 @@ exports.float32 = {
253
275
  }
254
276
 
255
277
  exports.float64 = {
256
- preencode (state, n) {
278
+ preencode(state, n) {
257
279
  state.end += 8
258
280
  },
259
- encode (state, n) {
260
- const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
281
+ encode(state, n) {
282
+ const view = new DataView(
283
+ state.buffer.buffer,
284
+ state.start + state.buffer.byteOffset,
285
+ 8
286
+ )
261
287
  view.setFloat64(0, n, true) // little endian
262
288
  state.start += 8
263
289
  },
264
- decode (state) {
290
+ decode(state) {
265
291
  if (state.end - state.start < 8) throw new Error('Out of bounds')
266
- const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8)
292
+ const view = new DataView(
293
+ state.buffer.buffer,
294
+ state.start + state.buffer.byteOffset,
295
+ 8
296
+ )
267
297
  const float = view.getFloat64(0, true) // little endian
268
298
  state.start += 8
269
299
  return float
270
300
  }
271
301
  }
272
302
 
273
- const buffer = exports.buffer = {
274
- preencode (state, b) {
303
+ const buffer = (exports.buffer = {
304
+ preencode(state, b) {
275
305
  if (b) uint8array.preencode(state, b)
276
306
  else state.end++
277
307
  },
278
- encode (state, b) {
308
+ encode(state, b) {
279
309
  if (b) uint8array.encode(state, b)
280
310
  else state.buffer[state.start++] = 0
281
311
  },
282
- decode (state) {
312
+ decode(state) {
283
313
  const len = uint.decode(state)
284
314
  if (len === 0) return null
285
315
  if (state.end - state.start < len) throw new Error('Out of bounds')
286
316
  return state.buffer.subarray(state.start, (state.start += len))
287
317
  }
288
- }
318
+ })
289
319
 
290
320
  exports.binary = {
291
321
  ...buffer,
292
- preencode (state, b) {
322
+ preencode(state, b) {
293
323
  if (typeof b === 'string') utf8.preencode(state, b)
294
324
  else buffer.preencode(state, b)
295
325
  },
296
- encode (state, b) {
326
+ encode(state, b) {
297
327
  if (typeof b === 'string') utf8.encode(state, b)
298
328
  else buffer.encode(state, b)
299
329
  }
300
330
  }
301
331
 
302
332
  exports.arraybuffer = {
303
- preencode (state, b) {
333
+ preencode(state, b) {
304
334
  uint.preencode(state, b.byteLength)
305
335
  state.end += b.byteLength
306
336
  },
307
- encode (state, b) {
337
+ encode(state, b) {
308
338
  uint.encode(state, b.byteLength)
309
339
 
310
340
  const view = new Uint8Array(b)
@@ -312,27 +342,27 @@ exports.arraybuffer = {
312
342
  state.buffer.set(view, state.start)
313
343
  state.start += b.byteLength
314
344
  },
315
- decode (state) {
345
+ decode(state) {
316
346
  const len = uint.decode(state)
317
347
 
318
348
  const b = new ArrayBuffer(len)
319
349
  const view = new Uint8Array(b)
320
350
 
321
- view.set(state.buffer.subarray(state.start, state.start += len))
351
+ view.set(state.buffer.subarray(state.start, (state.start += len)))
322
352
 
323
353
  return b
324
354
  }
325
355
  }
326
356
 
327
- function typedarray (TypedArray, swap) {
357
+ function typedarray(TypedArray, swap) {
328
358
  const n = TypedArray.BYTES_PER_ELEMENT
329
359
 
330
360
  return {
331
- preencode (state, b) {
361
+ preencode(state, b) {
332
362
  uint.preencode(state, b.length)
333
363
  state.end += b.byteLength
334
364
  },
335
- encode (state, b) {
365
+ encode(state, b) {
336
366
  uint.encode(state, b.length)
337
367
 
338
368
  const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
@@ -342,12 +372,12 @@ function typedarray (TypedArray, swap) {
342
372
  state.buffer.set(view, state.start)
343
373
  state.start += b.byteLength
344
374
  },
345
- decode (state) {
375
+ decode(state) {
346
376
  const len = uint.decode(state)
347
377
 
348
- let b = state.buffer.subarray(state.start, state.start += len * n)
378
+ let b = state.buffer.subarray(state.start, (state.start += len * n))
349
379
  if (b.byteLength !== len * n) throw new Error('Out of bounds')
350
- if ((b.byteOffset % n) !== 0) b = new Uint8Array(b)
380
+ if (b.byteOffset % n !== 0) b = new Uint8Array(b)
351
381
 
352
382
  if (BE && swap) swap(b)
353
383
 
@@ -356,7 +386,7 @@ function typedarray (TypedArray, swap) {
356
386
  }
357
387
  }
358
388
 
359
- const uint8array = exports.uint8array = typedarray(Uint8Array)
389
+ const uint8array = (exports.uint8array = typedarray(Uint8Array))
360
390
  exports.uint16array = typedarray(Uint16Array, b4a.swap16)
361
391
  exports.uint32array = typedarray(Uint32Array, b4a.swap32)
362
392
 
@@ -370,92 +400,102 @@ exports.bigint64array = typedarray(BigInt64Array, b4a.swap64)
370
400
  exports.float32array = typedarray(Float32Array, b4a.swap32)
371
401
  exports.float64array = typedarray(Float64Array, b4a.swap64)
372
402
 
373
- function string (encoding) {
403
+ function string(encoding) {
374
404
  return {
375
- preencode (state, s) {
405
+ preencode(state, s) {
376
406
  const len = b4a.byteLength(s, encoding)
377
407
  uint.preencode(state, len)
378
408
  state.end += len
379
409
  },
380
- encode (state, s) {
410
+ encode(state, s) {
381
411
  const len = b4a.byteLength(s, encoding)
382
412
  uint.encode(state, len)
383
413
  b4a.write(state.buffer, s, state.start, encoding)
384
414
  state.start += len
385
415
  },
386
- decode (state) {
416
+ decode(state) {
387
417
  const len = uint.decode(state)
388
418
  if (state.end - state.start < len) throw new Error('Out of bounds')
389
- return b4a.toString(state.buffer, encoding, state.start, (state.start += len))
419
+ return b4a.toString(
420
+ state.buffer,
421
+ encoding,
422
+ state.start,
423
+ (state.start += len)
424
+ )
390
425
  },
391
- fixed (n) {
426
+ fixed(n) {
392
427
  return {
393
- preencode (state) {
428
+ preencode(state) {
394
429
  state.end += n
395
430
  },
396
- encode (state, s) {
431
+ encode(state, s) {
397
432
  b4a.write(state.buffer, s, state.start, n, encoding)
398
433
  state.start += n
399
434
  },
400
- decode (state) {
435
+ decode(state) {
401
436
  if (state.end - state.start < n) throw new Error('Out of bounds')
402
- return b4a.toString(state.buffer, encoding, state.start, (state.start += n))
437
+ return b4a.toString(
438
+ state.buffer,
439
+ encoding,
440
+ state.start,
441
+ (state.start += n)
442
+ )
403
443
  }
404
444
  }
405
445
  }
406
446
  }
407
447
  }
408
448
 
409
- const utf8 = exports.string = exports.utf8 = string('utf-8')
449
+ const utf8 = (exports.string = exports.utf8 = string('utf-8'))
410
450
  exports.ascii = string('ascii')
411
451
  exports.hex = string('hex')
412
452
  exports.base64 = string('base64')
413
453
  exports.ucs2 = exports.utf16le = string('utf16le')
414
454
 
415
455
  exports.bool = {
416
- preencode (state, b) {
456
+ preencode(state, b) {
417
457
  state.end++
418
458
  },
419
- encode (state, b) {
459
+ encode(state, b) {
420
460
  state.buffer[state.start++] = b ? 1 : 0
421
461
  },
422
- decode (state) {
462
+ decode(state) {
423
463
  if (state.start >= state.end) throw Error('Out of bounds')
424
464
  return state.buffer[state.start++] === 1
425
465
  }
426
466
  }
427
467
 
428
- const fixed = exports.fixed = function fixed (n) {
468
+ const fixed = (exports.fixed = function fixed(n) {
429
469
  return {
430
- preencode (state, s) {
470
+ preencode(state, s) {
431
471
  if (s.byteLength !== n) throw new Error('Incorrect buffer size')
432
472
  state.end += n
433
473
  },
434
- encode (state, s) {
474
+ encode(state, s) {
435
475
  state.buffer.set(s, state.start)
436
476
  state.start += n
437
477
  },
438
- decode (state) {
478
+ decode(state) {
439
479
  if (state.end - state.start < n) throw new Error('Out of bounds')
440
480
  return state.buffer.subarray(state.start, (state.start += n))
441
481
  }
442
482
  }
443
- }
483
+ })
444
484
 
445
485
  exports.fixed32 = fixed(32)
446
486
  exports.fixed64 = fixed(64)
447
487
 
448
- exports.array = function array (enc) {
488
+ exports.array = function array(enc) {
449
489
  return {
450
- preencode (state, list) {
490
+ preencode(state, list) {
451
491
  uint.preencode(state, list.length)
452
492
  for (let i = 0; i < list.length; i++) enc.preencode(state, list[i])
453
493
  },
454
- encode (state, list) {
494
+ encode(state, list) {
455
495
  uint.encode(state, list.length)
456
496
  for (let i = 0; i < list.length; i++) enc.encode(state, list[i])
457
497
  },
458
- decode (state) {
498
+ decode(state) {
459
499
  const len = uint.decode(state)
460
500
  if (len > 0x100000) throw new Error('Array is too big')
461
501
  const arr = new Array(len)
@@ -465,22 +505,22 @@ exports.array = function array (enc) {
465
505
  }
466
506
  }
467
507
 
468
- exports.frame = function frame (enc) {
508
+ exports.frame = function frame(enc) {
469
509
  const dummy = exports.state()
470
510
 
471
511
  return {
472
- preencode (state, m) {
512
+ preencode(state, m) {
473
513
  const end = state.end
474
514
  enc.preencode(state, m)
475
515
  uint.preencode(state, state.end - end)
476
516
  },
477
- encode (state, m) {
517
+ encode(state, m) {
478
518
  dummy.end = 0
479
519
  enc.preencode(dummy, m)
480
520
  uint.encode(state, dummy.end)
481
521
  enc.encode(state, m)
482
522
  },
483
- decode (state) {
523
+ decode(state) {
484
524
  const end = state.end
485
525
  const len = uint.decode(state)
486
526
  state.end = state.start + len
@@ -493,50 +533,50 @@ exports.frame = function frame (enc) {
493
533
  }
494
534
 
495
535
  exports.date = {
496
- preencode (state, d) {
536
+ preencode(state, d) {
497
537
  int.preencode(state, d.getTime())
498
538
  },
499
- encode (state, d) {
539
+ encode(state, d) {
500
540
  int.encode(state, d.getTime())
501
541
  },
502
- decode (state, d) {
542
+ decode(state, d) {
503
543
  return new Date(int.decode(state))
504
544
  }
505
545
  }
506
546
 
507
547
  exports.json = {
508
- preencode (state, v) {
548
+ preencode(state, v) {
509
549
  utf8.preencode(state, JSON.stringify(v))
510
550
  },
511
- encode (state, v) {
551
+ encode(state, v) {
512
552
  utf8.encode(state, JSON.stringify(v))
513
553
  },
514
- decode (state) {
554
+ decode(state) {
515
555
  return JSON.parse(utf8.decode(state))
516
556
  }
517
557
  }
518
558
 
519
559
  exports.ndjson = {
520
- preencode (state, v) {
560
+ preencode(state, v) {
521
561
  utf8.preencode(state, JSON.stringify(v) + '\n')
522
562
  },
523
- encode (state, v) {
563
+ encode(state, v) {
524
564
  utf8.encode(state, JSON.stringify(v) + '\n')
525
565
  },
526
- decode (state) {
566
+ decode(state) {
527
567
  return JSON.parse(utf8.decode(state))
528
568
  }
529
569
  }
530
570
 
531
571
  // simple helper for when you want to just express nothing
532
572
  exports.none = {
533
- preencode (state, n) {
573
+ preencode(state, n) {
534
574
  // do nothing
535
575
  },
536
- encode (state, n) {
576
+ encode(state, n) {
537
577
  // do nothing
538
578
  },
539
- decode (state) {
579
+ decode(state) {
540
580
  return null
541
581
  }
542
582
  }
@@ -544,19 +584,19 @@ exports.none = {
544
584
  // "any" encoders here for helping just structure any object without schematising it
545
585
 
546
586
  const anyArray = {
547
- preencode (state, arr) {
587
+ preencode(state, arr) {
548
588
  uint.preencode(state, arr.length)
549
589
  for (let i = 0; i < arr.length; i++) {
550
590
  any.preencode(state, arr[i])
551
591
  }
552
592
  },
553
- encode (state, arr) {
593
+ encode(state, arr) {
554
594
  uint.encode(state, arr.length)
555
595
  for (let i = 0; i < arr.length; i++) {
556
596
  any.encode(state, arr[i])
557
597
  }
558
598
  },
559
- decode (state) {
599
+ decode(state) {
560
600
  const arr = []
561
601
  let len = uint.decode(state)
562
602
  while (len-- > 0) {
@@ -567,7 +607,7 @@ const anyArray = {
567
607
  }
568
608
 
569
609
  const anyObject = {
570
- preencode (state, o) {
610
+ preencode(state, o) {
571
611
  const keys = Object.keys(o)
572
612
  uint.preencode(state, keys.length)
573
613
  for (const key of keys) {
@@ -575,7 +615,7 @@ const anyObject = {
575
615
  any.preencode(state, o[key])
576
616
  }
577
617
  },
578
- encode (state, o) {
618
+ encode(state, o) {
579
619
  const keys = Object.keys(o)
580
620
  uint.encode(state, keys.length)
581
621
  for (const key of keys) {
@@ -583,7 +623,7 @@ const anyObject = {
583
623
  any.encode(state, o[key])
584
624
  }
585
625
  },
586
- decode (state) {
626
+ decode(state) {
587
627
  let len = uint.decode(state)
588
628
  const o = {}
589
629
  while (len-- > 0) {
@@ -607,25 +647,218 @@ const anyTypes = [
607
647
  exports.date
608
648
  ]
609
649
 
610
- const any = exports.any = {
611
- preencode (state, o) {
650
+ const any = (exports.any = {
651
+ preencode(state, o) {
612
652
  const t = getType(o)
613
653
  uint.preencode(state, t)
614
654
  anyTypes[t].preencode(state, o)
615
655
  },
616
- encode (state, o) {
656
+ encode(state, o) {
617
657
  const t = getType(o)
618
658
  uint.encode(state, t)
619
659
  anyTypes[t].encode(state, o)
620
660
  },
621
- decode (state) {
661
+ decode(state) {
622
662
  const t = uint.decode(state)
623
663
  if (t >= anyTypes.length) throw new Error('Unknown type: ' + t)
624
664
  return anyTypes[t].decode(state)
625
665
  }
666
+ })
667
+
668
+ const port = (exports.port = uint16)
669
+
670
+ const address = (host, family) => {
671
+ return {
672
+ preencode(state, m) {
673
+ host.preencode(state, m.host)
674
+ port.preencode(state, m.port)
675
+ },
676
+ encode(state, m) {
677
+ host.encode(state, m.host)
678
+ port.encode(state, m.port)
679
+ },
680
+ decode(state) {
681
+ return {
682
+ host: host.decode(state),
683
+ family,
684
+ port: port.decode(state)
685
+ }
686
+ }
687
+ }
688
+ }
689
+
690
+ const ipv4 = (exports.ipv4 = {
691
+ preencode(state) {
692
+ state.end += 4
693
+ },
694
+ encode(state, string) {
695
+ const start = state.start
696
+ const end = start + 4
697
+
698
+ let i = 0
699
+
700
+ while (i < string.length) {
701
+ let n = 0
702
+ let c
703
+
704
+ while (
705
+ i < string.length &&
706
+ (c = string.charCodeAt(i++)) !== /* . */ 0x2e
707
+ ) {
708
+ n = n * 10 + (c - /* 0 */ 0x30)
709
+ }
710
+
711
+ state.buffer[state.start++] = n
712
+ }
713
+
714
+ state.start = end
715
+ },
716
+ decode(state) {
717
+ if (state.end - state.start < 4) throw new Error('Out of bounds')
718
+ return (
719
+ state.buffer[state.start++] +
720
+ '.' +
721
+ state.buffer[state.start++] +
722
+ '.' +
723
+ state.buffer[state.start++] +
724
+ '.' +
725
+ state.buffer[state.start++]
726
+ )
727
+ }
728
+ })
729
+
730
+ exports.ipv4Address = address(ipv4, 4)
731
+
732
+ const ipv6 = (exports.ipv6 = {
733
+ preencode(state) {
734
+ state.end += 16
735
+ },
736
+ encode(state, string) {
737
+ const start = state.start
738
+ const end = start + 16
739
+
740
+ let i = 0
741
+ let split = null
742
+
743
+ while (i < string.length) {
744
+ let n = 0
745
+ let c
746
+
747
+ while (
748
+ i < string.length &&
749
+ (c = string.charCodeAt(i++)) !== /* : */ 0x3a
750
+ ) {
751
+ if (c >= 0x30 && c <= 0x39) n = n * 0x10 + (c - /* 0 */ 0x30)
752
+ else if (c >= 0x41 && c <= 0x46) n = n * 0x10 + (c - /* A */ 0x41 + 10)
753
+ else if (c >= 0x61 && c <= 0x66) n = n * 0x10 + (c - /* a */ 0x61 + 10)
754
+ }
755
+
756
+ state.buffer[state.start++] = n >>> 8
757
+ state.buffer[state.start++] = n
758
+
759
+ if (i < string.length && string.charCodeAt(i) === /* : */ 0x3a) {
760
+ i++
761
+ split = state.start
762
+ }
763
+ }
764
+
765
+ if (split !== null) {
766
+ const offset = end - state.start
767
+ state.buffer
768
+ .copyWithin(split + offset, split)
769
+ .fill(0, split, split + offset)
770
+ }
771
+
772
+ state.start = end
773
+ },
774
+ decode(state) {
775
+ if (state.end - state.start < 16) throw new Error('Out of bounds')
776
+ return (
777
+ (
778
+ state.buffer[state.start++] * 256 +
779
+ state.buffer[state.start++]
780
+ ).toString(16) +
781
+ ':' +
782
+ (
783
+ state.buffer[state.start++] * 256 +
784
+ state.buffer[state.start++]
785
+ ).toString(16) +
786
+ ':' +
787
+ (
788
+ state.buffer[state.start++] * 256 +
789
+ state.buffer[state.start++]
790
+ ).toString(16) +
791
+ ':' +
792
+ (
793
+ state.buffer[state.start++] * 256 +
794
+ state.buffer[state.start++]
795
+ ).toString(16) +
796
+ ':' +
797
+ (
798
+ state.buffer[state.start++] * 256 +
799
+ state.buffer[state.start++]
800
+ ).toString(16) +
801
+ ':' +
802
+ (
803
+ state.buffer[state.start++] * 256 +
804
+ state.buffer[state.start++]
805
+ ).toString(16) +
806
+ ':' +
807
+ (
808
+ state.buffer[state.start++] * 256 +
809
+ state.buffer[state.start++]
810
+ ).toString(16) +
811
+ ':' +
812
+ (
813
+ state.buffer[state.start++] * 256 +
814
+ state.buffer[state.start++]
815
+ ).toString(16)
816
+ )
817
+ }
818
+ })
819
+
820
+ exports.ipv6Address = address(ipv6, 6)
821
+
822
+ const ip = (exports.ip = {
823
+ preencode(state, string) {
824
+ const family = string.includes(':') ? 6 : 4
825
+ uint8.preencode(state, family)
826
+ if (family === 4) ipv4.preencode(state)
827
+ else ipv6.preencode(state)
828
+ },
829
+ encode(state, string) {
830
+ const family = string.includes(':') ? 6 : 4
831
+ uint8.encode(state, family)
832
+ if (family === 4) ipv4.encode(state, string)
833
+ else ipv6.encode(state, string)
834
+ },
835
+ decode(state) {
836
+ const family = uint8.decode(state)
837
+ if (family === 4) return ipv4.decode(state)
838
+ else return ipv6.decode(state)
839
+ }
840
+ })
841
+
842
+ exports.ipAddress = {
843
+ preencode(state, m) {
844
+ ip.preencode(state, m.host)
845
+ port.preencode(state, m.port)
846
+ },
847
+ encode(state, m) {
848
+ ip.encode(state, m.host)
849
+ port.encode(state, m.port)
850
+ },
851
+ decode(state) {
852
+ const family = uint8.decode(state)
853
+ return {
854
+ host: family === 4 ? ipv4.decode(state) : ipv6.decode(state),
855
+ family,
856
+ port: port.decode(state)
857
+ }
858
+ }
626
859
  }
627
860
 
628
- function getType (o) {
861
+ function getType(o) {
629
862
  if (o === null || o === undefined) return 0
630
863
  if (typeof o === 'boolean') return 1
631
864
  if (typeof o === 'string') return 2
@@ -641,61 +874,69 @@ function getType (o) {
641
874
  throw new Error('Unsupported type for ' + o)
642
875
  }
643
876
 
644
- exports.from = function from (enc) {
877
+ exports.from = function from(enc) {
645
878
  if (typeof enc === 'string') return fromNamed(enc)
646
879
  if (enc.preencode) return enc
647
880
  if (enc.encodingLength) return fromAbstractEncoder(enc)
648
881
  return fromCodec(enc)
649
882
  }
650
883
 
651
- function fromNamed (enc) {
884
+ function fromNamed(enc) {
652
885
  switch (enc) {
653
- case 'ascii': return raw.ascii
886
+ case 'ascii':
887
+ return raw.ascii
654
888
  case 'utf-8':
655
- case 'utf8': return raw.utf8
656
- case 'hex': return raw.hex
657
- case 'base64': return raw.base64
889
+ case 'utf8':
890
+ return raw.utf8
891
+ case 'hex':
892
+ return raw.hex
893
+ case 'base64':
894
+ return raw.base64
658
895
  case 'utf16-le':
659
896
  case 'utf16le':
660
897
  case 'ucs-2':
661
- case 'ucs2': return raw.ucs2
662
- case 'ndjson': return raw.ndjson
663
- case 'json': return raw.json
898
+ case 'ucs2':
899
+ return raw.ucs2
900
+ case 'ndjson':
901
+ return raw.ndjson
902
+ case 'json':
903
+ return raw.json
664
904
  case 'binary':
665
- default: return raw.binary
905
+ default:
906
+ return raw.binary
666
907
  }
667
908
  }
668
909
 
669
- function fromCodec (enc) {
910
+ function fromCodec(enc) {
670
911
  let tmpM = null
671
912
  let tmpBuf = null
672
913
 
673
914
  return {
674
- preencode (state, m) {
915
+ preencode(state, m) {
675
916
  tmpM = m
676
917
  tmpBuf = enc.encode(m)
677
918
  state.end += tmpBuf.byteLength
678
919
  },
679
- encode (state, m) {
920
+ encode(state, m) {
680
921
  raw.encode(state, m === tmpM ? tmpBuf : enc.encode(m))
681
922
  tmpM = tmpBuf = null
682
923
  },
683
- decode (state) {
924
+ decode(state) {
684
925
  return enc.decode(raw.decode(state))
685
926
  }
686
927
  }
687
928
  }
688
929
 
689
- function fromAbstractEncoder (enc) {
930
+ function fromAbstractEncoder(enc) {
690
931
  return {
691
- preencode (state, m) {
932
+ preencode(state, m) {
692
933
  state.end += enc.encodingLength(m)
693
934
  },
694
- encode (state, m) {
935
+ encode(state, m) {
695
936
  enc.encode(m, state.buffer, state.start)
696
937
  state.start += enc.encode.bytes
697
938
  },
698
- decode (state) {
939
+ decode(state) {
699
940
  const m = enc.decode(state.buffer, state.start, state.end)
700
941
  state.start += enc.decode.bytes
701
942
  return m
@@ -703,7 +944,7 @@ function fromAbstractEncoder (enc) {
703
944
  }
704
945
  }
705
946
 
706
- exports.encode = function encode (enc, m) {
947
+ exports.encode = function encode(enc, m) {
707
948
  const state = exports.state()
708
949
  enc.preencode(state, m)
709
950
  state.buffer = b4a.allocUnsafe(state.end)
@@ -711,56 +952,57 @@ exports.encode = function encode (enc, m) {
711
952
  return state.buffer
712
953
  }
713
954
 
714
- exports.decode = function decode (enc, buffer) {
955
+ exports.decode = function decode(enc, buffer) {
715
956
  return enc.decode(exports.state(0, buffer.byteLength, buffer))
716
957
  }
717
958
 
718
- function zigZagInt (enc) {
959
+ function zigZagInt(enc) {
719
960
  return {
720
- preencode (state, n) {
961
+ preencode(state, n) {
721
962
  enc.preencode(state, zigZagEncodeInt(n))
722
963
  },
723
- encode (state, n) {
964
+ encode(state, n) {
724
965
  enc.encode(state, zigZagEncodeInt(n))
725
966
  },
726
- decode (state) {
967
+ decode(state) {
727
968
  return zigZagDecodeInt(enc.decode(state))
728
969
  }
729
970
  }
730
971
  }
731
972
 
732
- function zigZagDecodeInt (n) {
973
+ function zigZagDecodeInt(n) {
733
974
  return n === 0 ? n : (n & 1) === 0 ? n / 2 : -(n + 1) / 2
734
975
  }
735
976
 
736
- function zigZagEncodeInt (n) {
977
+ function zigZagEncodeInt(n) {
737
978
  // 0, -1, 1, -2, 2, ...
738
- return n < 0 ? (2 * -n) - 1 : n === 0 ? 0 : 2 * n
979
+ return n < 0 ? 2 * -n - 1 : n === 0 ? 0 : 2 * n
739
980
  }
740
981
 
741
- function zigZagBigInt (enc) {
982
+ function zigZagBigInt(enc) {
742
983
  return {
743
- preencode (state, n) {
984
+ preencode(state, n) {
744
985
  enc.preencode(state, zigZagEncodeBigInt(n))
745
986
  },
746
- encode (state, n) {
987
+ encode(state, n) {
747
988
  enc.encode(state, zigZagEncodeBigInt(n))
748
989
  },
749
- decode (state) {
990
+ decode(state) {
750
991
  return zigZagDecodeBigInt(enc.decode(state))
751
992
  }
752
993
  }
753
994
  }
754
995
 
755
- function zigZagDecodeBigInt (n) {
996
+ function zigZagDecodeBigInt(n) {
756
997
  return n === 0n ? n : (n & 1n) === 0n ? n / 2n : -(n + 1n) / 2n
757
998
  }
758
999
 
759
- function zigZagEncodeBigInt (n) {
1000
+ function zigZagEncodeBigInt(n) {
760
1001
  // 0, -1, 1, -2, 2, ...
761
- return n < 0n ? (2n * -n) - 1n : n === 0n ? 0n : 2n * n
1002
+ return n < 0n ? 2n * -n - 1n : n === 0n ? 0n : 2n * n
762
1003
  }
763
1004
 
764
- function validateUint (n) {
765
- if ((n >= 0) === false /* Handles NaN as well */) throw new Error('uint must be positive')
1005
+ function validateUint(n) {
1006
+ if (n >= 0 === false /* Handles NaN as well */)
1007
+ throw new Error('uint must be positive')
766
1008
  }