compact-encoding 2.17.0 → 2.19.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,37 +647,37 @@ 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
  }
626
- }
666
+ })
627
667
 
628
- const port = exports.port = uint16
668
+ const port = (exports.port = uint16)
629
669
 
630
670
  const address = (host, family) => {
631
671
  return {
632
- preencode (state, m) {
672
+ preencode(state, m) {
633
673
  host.preencode(state, m.host)
634
674
  port.preencode(state, m.port)
635
675
  },
636
- encode (state, m) {
676
+ encode(state, m) {
637
677
  host.encode(state, m.host)
638
678
  port.encode(state, m.port)
639
679
  },
640
- decode (state) {
680
+ decode(state) {
641
681
  return {
642
682
  host: host.decode(state),
643
683
  family,
@@ -647,11 +687,11 @@ const address = (host, family) => {
647
687
  }
648
688
  }
649
689
 
650
- const ipv4 = exports.ipv4 = {
651
- preencode (state) {
690
+ const ipv4 = (exports.ipv4 = {
691
+ preencode(state) {
652
692
  state.end += 4
653
693
  },
654
- encode (state, string) {
694
+ encode(state, string) {
655
695
  const start = state.start
656
696
  const end = start + 4
657
697
 
@@ -661,7 +701,10 @@ const ipv4 = exports.ipv4 = {
661
701
  let n = 0
662
702
  let c
663
703
 
664
- while (i < string.length && (c = string.charCodeAt(i++)) !== /* . */ 0x2e) {
704
+ while (
705
+ i < string.length &&
706
+ (c = string.charCodeAt(i++)) !== /* . */ 0x2e
707
+ ) {
665
708
  n = n * 10 + (c - /* 0 */ 0x30)
666
709
  }
667
710
 
@@ -670,24 +713,27 @@ const ipv4 = exports.ipv4 = {
670
713
 
671
714
  state.start = end
672
715
  },
673
- decode (state) {
716
+ decode(state) {
674
717
  if (state.end - state.start < 4) throw new Error('Out of bounds')
675
718
  return (
676
- state.buffer[state.start++] + '.' +
677
- state.buffer[state.start++] + '.' +
678
- state.buffer[state.start++] + '.' +
719
+ state.buffer[state.start++] +
720
+ '.' +
721
+ state.buffer[state.start++] +
722
+ '.' +
723
+ state.buffer[state.start++] +
724
+ '.' +
679
725
  state.buffer[state.start++]
680
726
  )
681
727
  }
682
- }
728
+ })
683
729
 
684
730
  exports.ipv4Address = address(ipv4, 4)
685
731
 
686
- const ipv6 = exports.ipv6 = {
687
- preencode (state) {
732
+ const ipv6 = (exports.ipv6 = {
733
+ preencode(state) {
688
734
  state.end += 16
689
735
  },
690
- encode (state, string) {
736
+ encode(state, string) {
691
737
  const start = state.start
692
738
  const end = start + 16
693
739
 
@@ -698,7 +744,10 @@ const ipv6 = exports.ipv6 = {
698
744
  let n = 0
699
745
  let c
700
746
 
701
- while (i < string.length && (c = string.charCodeAt(i++)) !== /* : */ 0x3a) {
747
+ while (
748
+ i < string.length &&
749
+ (c = string.charCodeAt(i++)) !== /* : */ 0x3a
750
+ ) {
702
751
  if (c >= 0x30 && c <= 0x39) n = n * 0x10 + (c - /* 0 */ 0x30)
703
752
  else if (c >= 0x41 && c <= 0x46) n = n * 0x10 + (c - /* A */ 0x41 + 10)
704
753
  else if (c >= 0x61 && c <= 0x66) n = n * 0x10 + (c - /* a */ 0x61 + 10)
@@ -722,53 +771,84 @@ const ipv6 = exports.ipv6 = {
722
771
 
723
772
  state.start = end
724
773
  },
725
- decode (state) {
774
+ decode(state) {
726
775
  if (state.end - state.start < 16) throw new Error('Out of bounds')
727
776
  return (
728
- (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
729
- (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
730
- (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
731
- (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
732
- (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
733
- (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
734
- (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
735
- (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16)
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)
736
816
  )
737
817
  }
738
- }
818
+ })
739
819
 
740
820
  exports.ipv6Address = address(ipv6, 6)
741
821
 
742
- const ip = exports.ip = {
743
- preencode (state, string) {
822
+ const ip = (exports.ip = {
823
+ preencode(state, string) {
744
824
  const family = string.includes(':') ? 6 : 4
745
825
  uint8.preencode(state, family)
746
826
  if (family === 4) ipv4.preencode(state)
747
827
  else ipv6.preencode(state)
748
828
  },
749
- encode (state, string) {
829
+ encode(state, string) {
750
830
  const family = string.includes(':') ? 6 : 4
751
831
  uint8.encode(state, family)
752
832
  if (family === 4) ipv4.encode(state, string)
753
833
  else ipv6.encode(state, string)
754
834
  },
755
- decode (state) {
835
+ decode(state) {
756
836
  const family = uint8.decode(state)
757
837
  if (family === 4) return ipv4.decode(state)
758
838
  else return ipv6.decode(state)
759
839
  }
760
- }
840
+ })
761
841
 
762
842
  exports.ipAddress = {
763
- preencode (state, m) {
843
+ preencode(state, m) {
764
844
  ip.preencode(state, m.host)
765
845
  port.preencode(state, m.port)
766
846
  },
767
- encode (state, m) {
847
+ encode(state, m) {
768
848
  ip.encode(state, m.host)
769
849
  port.encode(state, m.port)
770
850
  },
771
- decode (state) {
851
+ decode(state) {
772
852
  const family = uint8.decode(state)
773
853
  return {
774
854
  host: family === 4 ? ipv4.decode(state) : ipv6.decode(state),
@@ -778,7 +858,38 @@ exports.ipAddress = {
778
858
  }
779
859
  }
780
860
 
781
- function getType (o) {
861
+ const record = (exports.record = function (keyEncoding, valueEncoding) {
862
+ return {
863
+ preencode(state, v) {
864
+ const keys = Object.keys(v)
865
+ uint.preencode(state, keys.length)
866
+ for (const k of keys) {
867
+ keyEncoding.preencode(state, k)
868
+ valueEncoding.preencode(state, v[k])
869
+ }
870
+ },
871
+ encode(state, v) {
872
+ const keys = Object.keys(v)
873
+ uint.encode(state, keys.length)
874
+ for (const k of keys) {
875
+ keyEncoding.encode(state, k)
876
+ valueEncoding.encode(state, v[k])
877
+ }
878
+ },
879
+ decode(state) {
880
+ const out = Object.create(null)
881
+ const keys = uint.decode(state)
882
+ for (let i = 0; i < keys; i++) {
883
+ out[keyEncoding.decode(state)] = valueEncoding.decode(state)
884
+ }
885
+ return out
886
+ }
887
+ }
888
+ })
889
+
890
+ exports.stringRecord = record(utf8, utf8)
891
+
892
+ function getType(o) {
782
893
  if (o === null || o === undefined) return 0
783
894
  if (typeof o === 'boolean') return 1
784
895
  if (typeof o === 'string') return 2
@@ -794,61 +905,69 @@ function getType (o) {
794
905
  throw new Error('Unsupported type for ' + o)
795
906
  }
796
907
 
797
- exports.from = function from (enc) {
908
+ exports.from = function from(enc) {
798
909
  if (typeof enc === 'string') return fromNamed(enc)
799
910
  if (enc.preencode) return enc
800
911
  if (enc.encodingLength) return fromAbstractEncoder(enc)
801
912
  return fromCodec(enc)
802
913
  }
803
914
 
804
- function fromNamed (enc) {
915
+ function fromNamed(enc) {
805
916
  switch (enc) {
806
- case 'ascii': return raw.ascii
917
+ case 'ascii':
918
+ return raw.ascii
807
919
  case 'utf-8':
808
- case 'utf8': return raw.utf8
809
- case 'hex': return raw.hex
810
- case 'base64': return raw.base64
920
+ case 'utf8':
921
+ return raw.utf8
922
+ case 'hex':
923
+ return raw.hex
924
+ case 'base64':
925
+ return raw.base64
811
926
  case 'utf16-le':
812
927
  case 'utf16le':
813
928
  case 'ucs-2':
814
- case 'ucs2': return raw.ucs2
815
- case 'ndjson': return raw.ndjson
816
- case 'json': return raw.json
929
+ case 'ucs2':
930
+ return raw.ucs2
931
+ case 'ndjson':
932
+ return raw.ndjson
933
+ case 'json':
934
+ return raw.json
817
935
  case 'binary':
818
- default: return raw.binary
936
+ default:
937
+ return raw.binary
819
938
  }
820
939
  }
821
940
 
822
- function fromCodec (enc) {
941
+ function fromCodec(enc) {
823
942
  let tmpM = null
824
943
  let tmpBuf = null
825
944
 
826
945
  return {
827
- preencode (state, m) {
946
+ preencode(state, m) {
828
947
  tmpM = m
829
948
  tmpBuf = enc.encode(m)
830
949
  state.end += tmpBuf.byteLength
831
950
  },
832
- encode (state, m) {
951
+ encode(state, m) {
833
952
  raw.encode(state, m === tmpM ? tmpBuf : enc.encode(m))
834
953
  tmpM = tmpBuf = null
835
954
  },
836
- decode (state) {
955
+ decode(state) {
837
956
  return enc.decode(raw.decode(state))
838
957
  }
839
958
  }
840
959
  }
841
960
 
842
- function fromAbstractEncoder (enc) {
961
+ function fromAbstractEncoder(enc) {
843
962
  return {
844
- preencode (state, m) {
963
+ preencode(state, m) {
845
964
  state.end += enc.encodingLength(m)
846
965
  },
847
- encode (state, m) {
966
+ encode(state, m) {
848
967
  enc.encode(m, state.buffer, state.start)
849
968
  state.start += enc.encode.bytes
850
969
  },
851
- decode (state) {
970
+ decode(state) {
852
971
  const m = enc.decode(state.buffer, state.start, state.end)
853
972
  state.start += enc.decode.bytes
854
973
  return m
@@ -856,7 +975,7 @@ function fromAbstractEncoder (enc) {
856
975
  }
857
976
  }
858
977
 
859
- exports.encode = function encode (enc, m) {
978
+ exports.encode = function encode(enc, m) {
860
979
  const state = exports.state()
861
980
  enc.preencode(state, m)
862
981
  state.buffer = b4a.allocUnsafe(state.end)
@@ -864,56 +983,57 @@ exports.encode = function encode (enc, m) {
864
983
  return state.buffer
865
984
  }
866
985
 
867
- exports.decode = function decode (enc, buffer) {
986
+ exports.decode = function decode(enc, buffer) {
868
987
  return enc.decode(exports.state(0, buffer.byteLength, buffer))
869
988
  }
870
989
 
871
- function zigZagInt (enc) {
990
+ function zigZagInt(enc) {
872
991
  return {
873
- preencode (state, n) {
992
+ preencode(state, n) {
874
993
  enc.preencode(state, zigZagEncodeInt(n))
875
994
  },
876
- encode (state, n) {
995
+ encode(state, n) {
877
996
  enc.encode(state, zigZagEncodeInt(n))
878
997
  },
879
- decode (state) {
998
+ decode(state) {
880
999
  return zigZagDecodeInt(enc.decode(state))
881
1000
  }
882
1001
  }
883
1002
  }
884
1003
 
885
- function zigZagDecodeInt (n) {
1004
+ function zigZagDecodeInt(n) {
886
1005
  return n === 0 ? n : (n & 1) === 0 ? n / 2 : -(n + 1) / 2
887
1006
  }
888
1007
 
889
- function zigZagEncodeInt (n) {
1008
+ function zigZagEncodeInt(n) {
890
1009
  // 0, -1, 1, -2, 2, ...
891
- return n < 0 ? (2 * -n) - 1 : n === 0 ? 0 : 2 * n
1010
+ return n < 0 ? 2 * -n - 1 : n === 0 ? 0 : 2 * n
892
1011
  }
893
1012
 
894
- function zigZagBigInt (enc) {
1013
+ function zigZagBigInt(enc) {
895
1014
  return {
896
- preencode (state, n) {
1015
+ preencode(state, n) {
897
1016
  enc.preencode(state, zigZagEncodeBigInt(n))
898
1017
  },
899
- encode (state, n) {
1018
+ encode(state, n) {
900
1019
  enc.encode(state, zigZagEncodeBigInt(n))
901
1020
  },
902
- decode (state) {
1021
+ decode(state) {
903
1022
  return zigZagDecodeBigInt(enc.decode(state))
904
1023
  }
905
1024
  }
906
1025
  }
907
1026
 
908
- function zigZagDecodeBigInt (n) {
1027
+ function zigZagDecodeBigInt(n) {
909
1028
  return n === 0n ? n : (n & 1n) === 0n ? n / 2n : -(n + 1n) / 2n
910
1029
  }
911
1030
 
912
- function zigZagEncodeBigInt (n) {
1031
+ function zigZagEncodeBigInt(n) {
913
1032
  // 0, -1, 1, -2, 2, ...
914
- return n < 0n ? (2n * -n) - 1n : n === 0n ? 0n : 2n * n
1033
+ return n < 0n ? 2n * -n - 1n : n === 0n ? 0n : 2n * n
915
1034
  }
916
1035
 
917
- function validateUint (n) {
918
- if ((n >= 0) === false /* Handles NaN as well */) throw new Error('uint must be positive')
1036
+ function validateUint(n) {
1037
+ if (n >= 0 === false /* Handles NaN as well */)
1038
+ throw new Error('uint must be positive')
919
1039
  }