zkjson 0.5.1 → 0.5.3

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/encoder-v2.js DELETED
@@ -1,892 +0,0 @@
1
- const { bits, tobits, strmap, base64 } = require("./utils.js")
2
-
3
- class u8 {
4
- constructor(size = 100, log = false) {
5
- this.log = log
6
-
7
- this.kc_counts = new Uint32Array(32)
8
- this.vc_counts = new Uint32Array(32)
9
- this.kc_diffs = new Uint32Array(4)
10
- this.vc_diffs = new Uint32Array(4)
11
- this.vlinks = new Uint32Array(32)
12
- this.klinks = new Uint32Array(32)
13
- this.vflags = new Uint32Array(16)
14
- this.kflags = new Uint32Array(16)
15
- this.bools = new Uint32Array(16)
16
- this.keys = new Uint32Array(32)
17
- this.types = new Uint32Array(32)
18
- this.nums = new Uint32Array(32)
19
- this.dc = new Uint32Array(32)
20
- this.kvals = new Uint32Array(64)
21
- this.vals = new Uint32Array(64)
22
-
23
- this.strMap = new Map()
24
-
25
- this.bitsLookup = new Uint8Array(17)
26
- for (let i = 0; i < 17; i++) {
27
- this.bitsLookup[i] = i === 0 ? 1 : 32 - Math.clz32(i)
28
- }
29
- }
30
- fastBits(n) {
31
- return n < 17 ? this.bitsLookup[n] : bits(n)
32
- }
33
-
34
- vc_diffs_set(index, value) {
35
- const wordIndex = index >>> 5
36
- const bitOffset = index & 31
37
- if (value) {
38
- this.vc_diffs[wordIndex] |= 1 << bitOffset
39
- } else {
40
- this.vc_diffs[wordIndex] &= ~(1 << bitOffset)
41
- }
42
- }
43
- vc_diffs_get(index) {
44
- const wordIndex = index >>> 5
45
- const bitOffset = index & 31
46
- return (this.vc_diffs[wordIndex] >>> bitOffset) & 1
47
- }
48
- kc_diffs_set(index, value) {
49
- const wordIndex = index >>> 5
50
- const bitOffset = index & 31
51
- if (value) {
52
- this.kc_diffs[wordIndex] |= 1 << bitOffset
53
- } else {
54
- this.kc_diffs[wordIndex] &= ~(1 << bitOffset)
55
- }
56
- }
57
- kc_diffs_get(index) {
58
- const wordIndex = index >>> 5
59
- const bitOffset = index & 31
60
- return (this.kc_diffs[wordIndex] >>> bitOffset) & 1
61
- }
62
-
63
- add_vlinks(val, vlen) {
64
- this.vlinks_len = this._add(this.vlinks, this.vlinks_len, val, vlen)
65
- }
66
- add_klinks(val, vlen) {
67
- this.klinks_len = this._add(this.klinks, this.klinks_len, val, vlen)
68
- }
69
- add_vflags(val, vlen) {
70
- this.vflags_len = this._add(this.vflags, this.vflags_len, val, vlen)
71
- }
72
- add_kflags(val, vlen) {
73
- this.kflags_len = this._add(this.kflags, this.kflags_len, val, vlen)
74
- }
75
- add_bools(val, vlen) {
76
- this.bools_len = this._add(this.bools, this.bools_len, val, vlen)
77
- }
78
- add_keys(val, vlen) {
79
- this.keys_len = this._add(this.keys, this.keys_len, val, vlen)
80
- }
81
- add_types(val, vlen) {
82
- this.types_len = this._add(this.types, this.types_len, val, vlen)
83
- }
84
- add_nums(val, vlen) {
85
- this.nums_len = this._add(this.nums, this.nums_len, val, vlen)
86
- }
87
- add_dc(val, vlen) {
88
- this.dc_len = this._add(this.dc, this.dc_len, val, vlen)
89
- }
90
- add_kvals(val, vlen) {
91
- this.kvals_len = this._add(this.kvals, this.kvals_len, val, vlen)
92
- }
93
- add_vals(val, vlen) {
94
- this.vals_len = this._add(this.vals, this.vals_len, val, vlen)
95
- }
96
-
97
- _add(tar, len, val, vlen) {
98
- val &= vlen >= 32 ? 0xffffffff : (1 << vlen) - 1
99
- const used = len & 31
100
- const free = used === 0 ? 32 : 32 - used
101
- const idx = len >> 5
102
-
103
- if (vlen <= free) {
104
- if (used === 0) tar[idx] = val
105
- else tar[idx] = (tar[idx] << vlen) | val
106
- len += vlen
107
- return len
108
- }
109
-
110
- const high = val >>> (vlen - free)
111
- if (used === 0) tar[idx] = high
112
- else tar[idx] = (tar[idx] << free) | high
113
- len += free
114
-
115
- let rest = vlen - free
116
- if (rest <= 32) {
117
- tar[idx + 1] = val & ((1 << rest) - 1)
118
- len += rest
119
- return len
120
- }
121
-
122
- let writeIdx = idx + 1
123
- while (rest > 32) {
124
- tar[writeIdx++] = (val >>> (rest - 32)) & 0xffffffff
125
- len += 32
126
- rest -= 32
127
- }
128
-
129
- if (rest > 0) {
130
- tar[writeIdx] = val & ((1 << rest) - 1)
131
- len += rest
132
- }
133
- return len
134
- }
135
-
136
- push_vflag(flag) {
137
- this.add_vflags(flag, 1)
138
- }
139
-
140
- push_bool(bool) {
141
- this.add_bools(bool ? 1 : 0, 1)
142
- }
143
-
144
- push_kflag(flag) {
145
- this.add_kflags(flag, 1)
146
- }
147
-
148
- get_diff(v, prev) {
149
- let diff = prev === null ? v : v - prev
150
- let isDiff = false
151
- if (diff < 0) {
152
- diff = Math.abs(diff) + 3
153
- isDiff = diff < 7
154
- } else isDiff = diff < 4
155
- const v2 = isDiff ? diff : v
156
- return (v2 << 1) | (isDiff ? 1 : 0)
157
- }
158
-
159
- push_vlink(v) {
160
- let result = this.get_diff(v, this.prev_link)
161
- const isDiff = (result & 1) === 1
162
- const v2 = result >>> 1
163
- this.prev_link = v
164
- this.push_vflag(isDiff ? 1 : 0)
165
- this._push_vlink(v2, isDiff, this.dcount)
166
- this.rcount++
167
- }
168
-
169
- push_klink(v) {
170
- let result = this.get_diff(v, this.prev_klink)
171
- const isDiff = (result & 1) === 1
172
- const v2 = result >>> 1
173
- this.prev_klink = v
174
- this.push_kflag(isDiff ? 1 : 0)
175
- this._push_klink(v2, isDiff, this.dcount)
176
- }
177
-
178
- set_newbits(count) {
179
- const new_bits = this.fastBits(count + 1)
180
- if (new_bits > this.prev_bits) {
181
- const diff = new_bits - this.prev_bits
182
- for (let i = 0; i < diff; i++) {
183
- this.add_vlinks(0, this.prev_bits + i)
184
- }
185
- this.prev_bits = new_bits
186
- }
187
- return new_bits
188
- }
189
-
190
- set_newbits_k(count) {
191
- const new_bits = this.fastBits(count + 1)
192
- if (new_bits > this.prev_kbits) {
193
- const diff = new_bits - this.prev_kbits
194
- for (let i = 0; i < diff; i++) {
195
- this.add_klinks(0, this.prev_kbits + i)
196
- }
197
- this.prev_kbits = new_bits
198
- }
199
- return new_bits
200
- }
201
-
202
- _flush_vlink(v, diff, count) {
203
- if (diff) {
204
- this.add_vlinks(v + 1, 3)
205
- } else {
206
- const nb = this.set_newbits(count)
207
- this.add_vlinks(v + 1, nb)
208
- }
209
- }
210
-
211
- flush_vlink() {
212
- if (this.vc_v === null) return
213
- if (this.vc_count < 4) {
214
- for (let i = 0; i < this.vc_count; i++)
215
- this._flush_vlink(
216
- this.vc_v,
217
- this.vc_diffs_get(i) === 1,
218
- this.vc_counts[i],
219
- )
220
- } else {
221
- if (this.vc_diffs_get(0) === 1) {
222
- this.add_vlinks(0, 3)
223
- this.short_vlinks(this.vc_count)
224
- this.add_vlinks(this.vc_v + 1, 3)
225
- } else {
226
- const nb = this.set_newbits(this.vc_counts[0])
227
- this.add_vlinks(0, nb)
228
- this.short_vlinks(this.vc_count)
229
- this.add_vlinks(this.vc_v + 1, nb)
230
- }
231
- }
232
- }
233
-
234
- _push_vlink(v, diff, count) {
235
- if (this.vc_v === null) {
236
- this.vc_v = v
237
- this.vc_diffs_set(0, diff ? 1 : 0)
238
- this.vc_counts[0] = count
239
- this.vc_count = 1
240
- } else if (v === this.vc_v) {
241
- this.vc_diffs_set(this.vc_count, diff ? 1 : 0)
242
- this.vc_counts[this.vc_count] = count
243
- this.vc_count++
244
- } else {
245
- this.flush_vlink()
246
- this.vc_v = v
247
- this.vc_diffs_set(0, diff ? 1 : 0)
248
- this.vc_counts[0] = count
249
- this.vc_count = 1
250
- }
251
- }
252
-
253
- flush_klink() {
254
- if (this.kc_v === null) return
255
- if (this.kc_count < 4) {
256
- for (let i = 0; i < this.kc_count; i++)
257
- this._flush_klink(
258
- this.kc_v,
259
- this.kc_diffs_get(i) === 1,
260
- this.kc_counts[i],
261
- )
262
- } else {
263
- if (this.kc_diffs_get(0) === 1) {
264
- this.add_klinks(0, 3)
265
- this.short_klinks(this.kc_count)
266
- this.add_klinks(this.kc_v + 1, 3)
267
- } else {
268
- const nb = this.set_newbits_k(this.kc_counts[0])
269
- this.add_klinks(0, nb)
270
- this.short_klinks(this.kc_count)
271
- this.add_klinks(this.kc_v + 1, nb)
272
- }
273
- }
274
- }
275
-
276
- _flush_klink(v, diff, count) {
277
- if (diff) {
278
- this.add_klinks(v + 1, 3)
279
- } else {
280
- const nb = this.set_newbits_k(count)
281
- this.add_klinks(v + 1, nb)
282
- }
283
- }
284
-
285
- _push_klink(v, diff, count) {
286
- if (this.kc_v === null) {
287
- this.kc_v = v
288
- this.kc_diffs_set(0, diff ? 1 : 0)
289
- this.kc_counts[0] = count
290
- this.kc_count = 1
291
- } else if (v === this.kc_v) {
292
- this.kc_diffs_set(this.kc_count, diff ? 1 : 0)
293
- this.kc_counts[this.kc_count] = count
294
- this.kc_count++
295
- } else {
296
- this.flush_klink()
297
- this.kc_v = v
298
- this.kc_diffs_set(0, diff ? 1 : 0)
299
- this.kc_counts[0] = count
300
- this.kc_count = 1
301
- }
302
- }
303
-
304
- push_type(v) {
305
- let count = this.tcount
306
- if (count > 3) {
307
- this.add_types(0, 3)
308
- this.short_types(count)
309
- this.add_types(v, 3)
310
- } else for (let i = 0; i < count; i++) this.add_types(v, 3)
311
- this.tcount = 1
312
- }
313
-
314
- push_keylen(v) {
315
- this.short_keys(v)
316
- }
317
-
318
- push_int(v) {
319
- let result = this.get_diff(v, this.prev_num)
320
- const isDiff = (result & 1) === 1
321
- const v2 = result >>> 1
322
-
323
- this.prev_num = v
324
- this.dint(v2, isDiff)
325
- }
326
-
327
- push_float(neg, v) {
328
- if (v < 4) this.push_int(neg ? 4 + v : v)
329
- else this.push_int(neg ? 4 : 0)
330
- }
331
-
332
- flush_nums() {
333
- if (this.nc_diff !== null) {
334
- if (this.nc_count < 3) {
335
- for (let i = 0; i < this.nc_count; i++)
336
- this._dint(this.nc_v, this.nc_diff)
337
- } else {
338
- this.add_nums(0, 2)
339
- this.add_nums(7, 3)
340
- this.short_nums(this.nc_count)
341
- if (this.nc_diff) {
342
- this.add_nums(0, 2)
343
- this.add_nums(this.nc_v, 3)
344
- } else if (this.nc_v < 64) {
345
- const d = this.nc_v < 16 ? 4 : 6
346
- const flag = this.nc_v < 16 ? 1 : 2
347
- this.add_nums(flag, 2)
348
- this.add_nums(this.nc_v, d)
349
- } else this.leb128_nums(this.nc_v)
350
- }
351
- }
352
- }
353
-
354
- dint(v, diff = false) {
355
- if (this.nc_diff === null) {
356
- this.nc_diff = diff
357
- this.nc_v = v
358
- this.nc_count = 1
359
- } else if (this.nc_diff === diff && this.nc_v === v) {
360
- this.nc_count += 1
361
- } else {
362
- if (this.nc_count === 1) this._dint(this.nc_v, this.nc_diff)
363
- else this.flush_nums()
364
- this.nc_diff = diff
365
- this.nc_v = v
366
- this.nc_count = 1
367
- }
368
- }
369
-
370
- _dint(v, diff) {
371
- if (diff) {
372
- this.add_nums(0, 2)
373
- this.add_nums(v, 3)
374
- } else if (v < 64) {
375
- const d = v < 16 ? 4 : 6
376
- const flag = v < 16 ? 1 : 2
377
- this.add_nums(flag, 2)
378
- this.add_nums(v, d)
379
- } else this.leb128_nums(v)
380
- }
381
-
382
- leb128_2_kvals(v) {
383
- while (v >= 128) {
384
- this.add_kvals((v & 0x7f) | 0x80, 8)
385
- v >>>= 7
386
- }
387
- this.add_kvals(v, 8)
388
- }
389
-
390
- leb128_dc(v) {
391
- this.add_dc(3, 2)
392
- while (v >= 128) {
393
- this.add_dc((v & 0x7f) | 0x80, 8)
394
- v >>>= 7
395
- }
396
- this.add_dc(v, 8)
397
- }
398
-
399
- leb128_keys(v) {
400
- this.add_keys(3, 2)
401
- while (v >= 128) {
402
- this.add_keys((v & 0x7f) | 0x80, 8)
403
- v >>>= 7
404
- }
405
- this.add_keys(v, 8)
406
- }
407
-
408
- leb128_klinks(v) {
409
- this.add_klinks(3, 2)
410
- while (v >= 128) {
411
- this.add_klinks((v & 0x7f) | 0x80, 8)
412
- v >>>= 7
413
- }
414
- this.add_klinks(v, 8)
415
- }
416
-
417
- leb128_vals(v) {
418
- this.add_vals(3, 2)
419
- while (v >= 128) {
420
- this.add_vals((v & 0x7f) | 0x80, 8)
421
- v >>>= 7
422
- }
423
- this.add_vals(v, 8)
424
- }
425
-
426
- leb128_kvals(v) {
427
- this.add_kvals(3, 2)
428
- while (v >= 128) {
429
- this.add_kvals((v & 0x7f) | 0x80, 8)
430
- v >>>= 7
431
- }
432
- this.add_kvals(v, 8)
433
- }
434
-
435
- leb128_nums(v) {
436
- this.add_nums(3, 2)
437
- while (v >= 128) {
438
- this.add_nums((v & 0x7f) | 0x80, 8)
439
- v >>>= 7
440
- }
441
- this.add_nums(v, 8)
442
- }
443
-
444
- leb128_types(v) {
445
- this.add_types(3, 2)
446
- while (v >= 128) {
447
- this.add_types((v & 0x7f) | 0x80, 8)
448
- v >>>= 7
449
- }
450
- this.add_types(v, 8)
451
- }
452
-
453
- leb128_vlinks(v) {
454
- this.add_vlinks(3, 2)
455
- while (v >= 128) {
456
- this.add_vlinks((v & 0x7f) | 0x80, 8)
457
- v >>>= 7
458
- }
459
- this.add_vlinks(v, 8)
460
- }
461
-
462
- uint_dc(v) {
463
- if (v < 64) {
464
- const d = v < 8 ? 3 : v < 16 ? 4 : 6
465
- const flag = v < 8 ? 0 : v < 16 ? 1 : 2
466
- this.add_dc(flag, 2)
467
- this.add_dc(v, d)
468
- } else this.leb128_dc(v)
469
- }
470
-
471
- short_types(v) {
472
- if (v < 16) {
473
- const d = v < 4 ? 2 : this.fastBits(v)
474
- this.add_types(d - 2, 2)
475
- this.add_types(v, d)
476
- } else this.leb128_types(v)
477
- }
478
-
479
- short_dc(v) {
480
- if (v < 16) {
481
- const d = v < 4 ? 2 : this.fastBits(v)
482
- this.add_dc(d - 2, 2)
483
- this.add_dc(v, d)
484
- } else this.leb128_dc(v)
485
- }
486
-
487
- short_vals(v) {
488
- if (v < 16) {
489
- const d = v < 4 ? 2 : this.fastBits(v)
490
- this.add_vals(d - 2, 2)
491
- this.add_vals(v, d)
492
- } else this.leb128_vals(v)
493
- }
494
-
495
- short_nums(v) {
496
- if (v < 16) {
497
- const d = v < 4 ? 2 : this.fastBits(v)
498
- this.add_nums(d - 2, 2)
499
- this.add_nums(v, d)
500
- } else this.leb128_nums(v)
501
- }
502
-
503
- short_kvals(v) {
504
- if (v < 16) {
505
- const d = v < 4 ? 2 : this.fastBits(v)
506
- this.add_kvals(d - 2, 2)
507
- this.add_kvals(v, d)
508
- } else this.leb128_kvals(v)
509
- }
510
-
511
- short_keys(v) {
512
- if (v < 16) {
513
- const d = v < 4 ? 2 : this.fastBits(v)
514
- this.add_keys(d - 2, 2)
515
- this.add_keys(v, d)
516
- } else this.leb128_keys(v)
517
- }
518
-
519
- short_klinks(v) {
520
- if (v < 16) {
521
- const d = v < 4 ? 2 : this.fastBits(v)
522
- this.add_klinks(d - 2, 2)
523
- this.add_klinks(v, d)
524
- } else this.leb128_klinks(v)
525
- }
526
-
527
- short_vlinks(v) {
528
- if (v < 16) {
529
- const d = v < 4 ? 2 : this.fastBits(v)
530
- this.add_vlinks(d - 2, 2)
531
- this.add_vlinks(v, d)
532
- } else this.leb128_vlinks(v)
533
- }
534
-
535
- reset() {
536
- this.strMap.clear()
537
- this.str_len = 0
538
- this.prev_bits = 1
539
- this.prev_kbits = 1
540
- this.prev_num = 0
541
- this.nums_count = 0
542
- this.prev_link = null
543
- this.prev_klink = null
544
- this.single = true
545
- this.len = 0
546
- this.dlen = 0
547
- this.jlen = 0
548
- this.dcount = 0
549
- this.rcount = 0
550
- this.tcount = 0
551
- this.oid = 0
552
- this.iid = 0
553
-
554
- this.vc_v = null
555
-
556
- this.vc_count = null
557
-
558
- this.kc_v = null
559
-
560
- this.kc_count = null
561
-
562
- this.nc_diff = null
563
- this.nc_v = null
564
- this.nc_count = null
565
-
566
- this.vlinks_len = 0
567
- this.klinks_len = 0
568
- this.vflags_len = 0
569
- this.kflags_len = 0
570
- this.bools_len = 0
571
- this.keys_len = 0
572
- this.types_len = 0
573
- this.nums_len = 0
574
- this.dc_len = 0
575
- this.kvals_len = 0
576
- this.vals_len = 0
577
- }
578
-
579
- dump() {
580
- if (!this.single) {
581
- this.flush_vlink()
582
- this.flush_klink()
583
- this.flush_nums()
584
- this.add_dc(this.single ? 1 : 0, 1)
585
- this.short_dc(this.rcount)
586
- }
587
-
588
- const totalBits =
589
- this.dc_len +
590
- this.vflags_len +
591
- this.vlinks_len +
592
- this.kflags_len +
593
- this.klinks_len +
594
- this.keys_len +
595
- this.types_len +
596
- this.nums_len +
597
- this.bools_len +
598
- this.kvals_len +
599
- this.vals_len
600
-
601
- const padBits = (8 - (totalBits % 8)) % 8
602
- const finalBits = totalBits + padBits
603
- const outLength = finalBits / 8
604
- const out = new Uint8Array(outLength)
605
-
606
- let outIndex = 0
607
- let accumulator = 0
608
- let accBits = 0
609
-
610
- const writeBits = (num, numBits) => {
611
- while (numBits > 0) {
612
- const free = 8 - accBits
613
- if (numBits <= free) {
614
- accumulator = (accumulator << numBits) | (num & ((1 << numBits) - 1))
615
- accBits += numBits
616
- numBits = 0
617
- if (accBits === 8) {
618
- out[outIndex++] = accumulator
619
- accumulator = 0
620
- accBits = 0
621
- }
622
- } else {
623
- const shift = numBits - free
624
- const part = num >>> shift
625
- accumulator = (accumulator << free) | (part & ((1 << free) - 1))
626
- out[outIndex++] = accumulator
627
- num = num & ((1 << shift) - 1)
628
- numBits -= free
629
- accumulator = 0
630
- accBits = 0
631
- }
632
- }
633
- }
634
-
635
- const writeBuffer = (buffer, bitLen) => {
636
- let remaining = bitLen
637
- let i = 0
638
- while (remaining > 0 && i < buffer.length) {
639
- const bitsThis = Math.min(32, remaining)
640
- writeBits(buffer[i] >>> 0, bitsThis)
641
- remaining -= bitsThis
642
- i++
643
- }
644
- }
645
-
646
- writeBuffer(this.dc, this.dc_len)
647
- writeBuffer(this.vflags, this.vflags_len)
648
- writeBuffer(this.vlinks, this.vlinks_len)
649
- writeBuffer(this.kflags, this.kflags_len)
650
- writeBuffer(this.klinks, this.klinks_len)
651
- writeBuffer(this.keys, this.keys_len)
652
- writeBuffer(this.types, this.types_len)
653
- writeBuffer(this.bools, this.bools_len)
654
- writeBuffer(this.nums, this.nums_len)
655
- writeBuffer(this.kvals, this.kvals_len)
656
- writeBuffer(this.vals, this.vals_len)
657
-
658
- if (padBits > 0) writeBits(0, padBits)
659
-
660
- return out
661
- }
662
- }
663
-
664
- function pushPathStr(u, v2, prev = null) {
665
- if (u.dcount > 0) u.push_klink(prev === null ? 0 : prev + 1)
666
- if (u.strMap.has(v2)) {
667
- u.add_keys(2, 2)
668
- u.push_keylen(0)
669
- u.short_kvals(u.strMap.get(v2))
670
- } else {
671
- u.strMap.set(v2, u.str_len++)
672
- const len = v2.length
673
- let ktype = 3
674
- let codes = []
675
- let codes2 = []
676
- if (len !== 0) {
677
- let is64 = true
678
- for (let i = 0; i < len; i++) {
679
- codes2.push(v2.charCodeAt(i))
680
- const c = base64[v2[i]]
681
- if (typeof c === "undefined") is64 = false
682
- else codes.push(c)
683
- }
684
- if (is64) ktype = 2
685
- }
686
- u.add_keys(ktype, 2)
687
- u.push_keylen(len + 2)
688
- if (ktype === 3) for (let v of codes2) u.leb128_2_kvals(v)
689
- else for (let v of codes) u.add_kvals(v, 6)
690
- }
691
- u.dcount++
692
- }
693
-
694
- function pushPathNum(u, prev = null, keylen) {
695
- if (u.dcount > 0) u.push_klink(prev === null ? 0 : prev + 1)
696
- u.add_keys(keylen, 2)
697
- const id = keylen === 0 ? u.iid++ : u.oid++
698
- u.dcount++
699
- }
700
-
701
- function encode_x(v, u) {
702
- u.reset()
703
- if (v === null) {
704
- u.add_dc(1, 1)
705
- u.add_dc(0, 7)
706
- } else if (typeof v !== "object") {
707
- u.add_dc(1, 1)
708
- if (v === true) u.add_dc(1, 7)
709
- else if (v === false) u.add_dc(2, 7)
710
- else if (v === "") u.add_dc(3, 7)
711
- else if (typeof v === "number") {
712
- const moved = v % 1 === v ? 0 : getPrecision(v)
713
- const type = moved === 0 ? (v < 0 ? 5 : 4) : v < 0 ? 7 : 6
714
- if (type === 4) {
715
- u.add_dc(1, 1)
716
- if (v < 63) u.add_dc(v, 6)
717
- else {
718
- u.add_dc(63, 6)
719
- u.leb128_2_dc(v - 63)
720
- }
721
- } else {
722
- u.add_dc(0, 1)
723
- u.add_dc(type + 1, 6)
724
- if (moved > 0) u.uint_dc(moved)
725
- u.uint_dc((v < 0 ? -1 : 1) * v * Math.pow(10, moved))
726
- }
727
- } else if (typeof v === "string") {
728
- u.add_dc(0, 1)
729
-
730
- if (v.length === 1) {
731
- const charCode = v.charCodeAt(0)
732
- const mapValue = strmap[v]
733
-
734
- if (typeof mapValue !== "undefined") {
735
- u.add_dc(mapValue + 9, 6)
736
- } else {
737
- u.add_dc(61, 6)
738
- u.leb128_2_dc(charCode)
739
- }
740
- } else {
741
- let is64 = true
742
- for (let i = 0; i < v.length; i++) {
743
- if (typeof base64[v[i]] === "undefined") {
744
- is64 = false
745
- break
746
- }
747
- }
748
-
749
- if (is64) {
750
- u.add_dc(62, 6)
751
- u.short_dc(v.length)
752
- for (let i = 0; i < v.length; i++) {
753
- u.add_dc(base64[v[i]], 6)
754
- }
755
- } else {
756
- u.add_dc(63, 6)
757
- u.short_dc(v.length)
758
- for (let i = 0; i < v.length; i++) {
759
- u.leb128_2_dc(v.charCodeAt(i))
760
- }
761
- }
762
- }
763
- }
764
- } else if (Array.isArray(v) && v.length === 0) {
765
- u.add_dc(1, 1)
766
- u.add_dc(4, 7)
767
- } else if (Object.keys(v).length === 0) {
768
- u.add_dc(1, 1)
769
- u.add_dc(5, 7)
770
- } else {
771
- u.single = false
772
- u.push_type(_encode_x(v, u))
773
- }
774
- return u.dump()
775
- }
776
-
777
- function getPrecision(v) {
778
- const s = v.toString()
779
- const dot = s.indexOf(".")
780
- if (dot === -1) return 0
781
- const frac = s.slice(dot + 1).replace(/0+$/, "")
782
- return frac.length
783
- }
784
-
785
- function _encode_x(
786
- v,
787
- u,
788
- plen = 0,
789
- prev = null,
790
- prev_type = null,
791
- index = null,
792
- ) {
793
- if (typeof v === "number") {
794
- if (prev !== null) u.push_vlink(prev + 1)
795
-
796
- const moved = v % 1 === v ? 0 : getPrecision(v)
797
- const type = moved === 0 ? (v < 0 ? 5 : 4) : 6
798
-
799
- if (prev_type !== null && prev_type !== 4) u.push_type(prev_type)
800
- else u.tcount++
801
- if (moved > 0) {
802
- u.push_float(v < 0, moved + 1)
803
- if (moved > 2) u.push_int(moved + 1)
804
- }
805
- u.push_int((v < 0 ? -1 : 1) * v * Math.pow(10, moved))
806
- return type
807
- } else if (typeof v === "boolean") {
808
- if (prev !== null) u.push_vlink(prev + 1)
809
- const type = 3
810
- if (prev_type !== null && prev_type !== type) u.push_type(prev_type)
811
- else u.tcount++
812
- u.push_bool(v)
813
- return type
814
- } else if (v === null) {
815
- if (prev !== null) u.push_vlink(prev + 1)
816
- if (prev_type !== null && prev_type !== 1) u.push_type(prev_type)
817
- else u.tcount++
818
- return 1
819
- } else if (typeof v === "string") {
820
- let ktype = 7
821
- if (prev !== null) u.push_vlink(prev + 1)
822
- if (u.strMap.has(v)) {
823
- ktype = 2
824
- u.push_type(prev_type)
825
- u.short_vals(0)
826
- u.short_vals(u.strMap.get(v))
827
- } else {
828
- u.strMap.set(v, u.str_len++)
829
- const len = v.length
830
- u.short_vals(len)
831
- let codes = []
832
- let codes2 = []
833
- let is64 = true
834
- if (len === 0) {
835
- is64 = false
836
- } else {
837
- for (let i = 0; i < len; i++) {
838
- codes2.push(v.charCodeAt(i))
839
- const c = base64[v[i]]
840
- if (typeof c === "undefined") is64 = false
841
- else codes.push(c)
842
- }
843
- if (is64) ktype = 2
844
- }
845
- if (prev_type !== null && prev_type !== ktype) u.push_type(prev_type)
846
- else u.tcount++
847
-
848
- if (is64) for (let v of codes) u.add_vals(v, 6)
849
- else for (let v of codes2) u.leb128_2_vals(v)
850
- }
851
- return ktype
852
- } else if (Array.isArray(v)) {
853
- if (v.length === 0) {
854
- if (prev !== null) u.push_vlink(prev + 1)
855
- u.push_type(prev_type)
856
- u.push_float(false, 1)
857
- return 6
858
- } else {
859
- const _prev = u.dcount
860
- pushPathNum(u, prev, 0)
861
- let i = 0
862
- for (const v2 of v) {
863
- prev_type = _encode_x(v2, u, plen + 1, _prev, prev_type, i)
864
- i++
865
- }
866
- }
867
- return prev_type
868
- } else if (typeof v === "object") {
869
- if (Object.keys(v).length === 0) {
870
- if (prev !== null) u.push_vlink(prev + 1)
871
- u.push_type(prev_type)
872
- u.push_float(true, 1)
873
- return 6
874
- } else {
875
- pushPathNum(u, prev, 1)
876
- const __prev = u.dcount
877
- for (const k in v) {
878
- const _prev = u.dcount
879
- pushPathStr(u, k, __prev - 1)
880
- prev_type = _encode_x(v[k], u, plen + 1, _prev, prev_type)
881
- }
882
- return prev_type
883
- }
884
- }
885
- }
886
-
887
- function decode_x(v, d) {
888
- d.decode(v)
889
- return d.json
890
- }
891
-
892
- module.exports = { encode_x, decode_x, u8 }