@mapwhit/pbf 1.0.2 → 1.0.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.
Files changed (2) hide show
  1. package/index.js +123 -97
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -9,10 +9,9 @@ const SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
9
9
  const TEXT_DECODER_MIN_LENGTH = 12;
10
10
  const utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8');
11
11
 
12
-
13
12
  class Pbf {
14
13
  constructor(buf) {
15
- this.buf = ArrayBuffer.isView && ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf || 0);
14
+ this.buf = ArrayBuffer.isView?.(buf) ? buf : new Uint8Array(buf || 0);
16
15
  this.pos = 0;
17
16
  this.type = 0;
18
17
  this.length = this.buf.length;
@@ -101,7 +100,8 @@ class Pbf {
101
100
  return readVarintRemainder(val, isSigned, this);
102
101
  }
103
102
 
104
- readVarint64() { // for compatibility with v2.0.1
103
+ readVarint64() {
104
+ // for compatibility with v2.0.1
105
105
  return this.readVarint(true);
106
106
  }
107
107
 
@@ -202,15 +202,22 @@ class Pbf {
202
202
  skip(val) {
203
203
  const type = val & 0x7;
204
204
  switch (type) {
205
- case Pbf.Varint: while (this.buf[this.pos++] > 0x7f) { /* skip */ }
205
+ case Pbf.Varint:
206
+ while (this.buf[this.pos++] > 0x7f) {
207
+ /* skip */
208
+ }
206
209
  break;
207
- case Pbf.Bytes: this.pos = this.readVarint() + this.pos;
210
+ case Pbf.Bytes:
211
+ this.pos = this.readVarint() + this.pos;
208
212
  break;
209
- case Pbf.Fixed32: this.pos += 4;
213
+ case Pbf.Fixed32:
214
+ this.pos += 4;
210
215
  break;
211
- case Pbf.Fixed64: this.pos += 8;
216
+ case Pbf.Fixed64:
217
+ this.pos += 8;
212
218
  break;
213
- default: assert(false, `Unimplemented type: ${type}`);
219
+ default:
220
+ assert(false, `Unimplemented type: ${type}`);
214
221
  }
215
222
  }
216
223
 
@@ -275,7 +282,7 @@ class Pbf {
275
282
 
276
283
  this.realloc(4);
277
284
 
278
- this.buf[this.pos++] = val & 0x7f | (val > 0x7f ? 0x80 : 0);
285
+ this.buf[this.pos++] = (val & 0x7f) | (val > 0x7f ? 0x80 : 0);
279
286
  if (val <= 0x7f) return;
280
287
  this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0);
281
288
  if (val <= 0x7f) return;
@@ -351,15 +358,33 @@ class Pbf {
351
358
  this.writeRawMessage(fn, obj);
352
359
  }
353
360
 
354
- writePackedVarint(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedVarint, arr); }
355
- writePackedSVarint(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedSVarint, arr); }
356
- writePackedBoolean(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedBoolean, arr); }
357
- writePackedFloat(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedFloat, arr); }
358
- writePackedDouble(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedDouble, arr); }
359
- writePackedFixed32(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedFixed32, arr); }
360
- writePackedSFixed32(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr); }
361
- writePackedFixed64(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedFixed64, arr); }
362
- writePackedSFixed64(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr); }
361
+ writePackedVarint(tag, arr) {
362
+ if (arr.length) this.writeMessage(tag, writePackedVarint, arr);
363
+ }
364
+ writePackedSVarint(tag, arr) {
365
+ if (arr.length) this.writeMessage(tag, writePackedSVarint, arr);
366
+ }
367
+ writePackedBoolean(tag, arr) {
368
+ if (arr.length) this.writeMessage(tag, writePackedBoolean, arr);
369
+ }
370
+ writePackedFloat(tag, arr) {
371
+ if (arr.length) this.writeMessage(tag, writePackedFloat, arr);
372
+ }
373
+ writePackedDouble(tag, arr) {
374
+ if (arr.length) this.writeMessage(tag, writePackedDouble, arr);
375
+ }
376
+ writePackedFixed32(tag, arr) {
377
+ if (arr.length) this.writeMessage(tag, writePackedFixed32, arr);
378
+ }
379
+ writePackedSFixed32(tag, arr) {
380
+ if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr);
381
+ }
382
+ writePackedFixed64(tag, arr) {
383
+ if (arr.length) this.writeMessage(tag, writePackedFixed64, arr);
384
+ }
385
+ writePackedSFixed64(tag, arr) {
386
+ if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr);
387
+ }
363
388
 
364
389
  writeBytesField(tag, buffer) {
365
390
  this.writeTag(tag, Pbf.Bytes);
@@ -449,8 +474,7 @@ function readVarintRemainder(l, s, p) {
449
474
  }
450
475
 
451
476
  function readPackedEnd(pbf) {
452
- return pbf.type === Pbf.Bytes ?
453
- pbf.readVarint() + pbf.pos : pbf.pos + 1;
477
+ return pbf.type === Pbf.Bytes ? pbf.readVarint() + pbf.pos : pbf.pos + 1;
454
478
  }
455
479
 
456
480
  function toNum(low, high, isSigned) {
@@ -458,16 +482,13 @@ function toNum(low, high, isSigned) {
458
482
  return high * 0x100000000 + (low >>> 0);
459
483
  }
460
484
 
461
- return ((high >>> 0) * 0x100000000) + (low >>> 0);
485
+ return (high >>> 0) * 0x100000000 + (low >>> 0);
462
486
  }
463
487
 
464
488
  function writeBigVarint(val, pbf) {
465
489
  /* global DEBUG */
466
490
  if (DEBUG) {
467
- assert(
468
- val < 0x10000000000000000 && val >= -0x10000000000000000,
469
- 'Given varint doesn\'t fit into 10 bytes'
470
- );
491
+ assert(val < 0x10000000000000000 && val >= -0x10000000000000000, "Given varint doesn't fit into 10 bytes");
471
492
  }
472
493
 
473
494
  let low;
@@ -494,14 +515,14 @@ function writeBigVarint(val, pbf) {
494
515
  writeBigVarintHigh(high, pbf);
495
516
  }
496
517
 
497
- function writeBigVarintLow(low, high, pbf) {
498
- pbf.buf[pbf.pos++] = low & 0x7f | 0x80;
518
+ function writeBigVarintLow(low, _high, pbf) {
519
+ pbf.buf[pbf.pos++] = (low & 0x7f) | 0x80;
499
520
  low >>>= 7;
500
- pbf.buf[pbf.pos++] = low & 0x7f | 0x80;
521
+ pbf.buf[pbf.pos++] = (low & 0x7f) | 0x80;
501
522
  low >>>= 7;
502
- pbf.buf[pbf.pos++] = low & 0x7f | 0x80;
523
+ pbf.buf[pbf.pos++] = (low & 0x7f) | 0x80;
503
524
  low >>>= 7;
504
- pbf.buf[pbf.pos++] = low & 0x7f | 0x80;
525
+ pbf.buf[pbf.pos++] = (low & 0x7f) | 0x80;
505
526
  low >>>= 7;
506
527
  pbf.buf[pbf.pos] = low & 0x7f;
507
528
  }
@@ -509,69 +530,79 @@ function writeBigVarintLow(low, high, pbf) {
509
530
  function writeBigVarintHigh(high, pbf) {
510
531
  const lsb = (high & 0x07) << 4;
511
532
 
512
- pbf.buf[pbf.pos++] |= lsb | (((high >>>= 3)) ? 0x80 : 0);
533
+ pbf.buf[pbf.pos++] |= lsb | ((high >>>= 3) ? 0x80 : 0);
513
534
  if (!high) return;
514
- pbf.buf[pbf.pos++] = high & 0x7f | (((high >>>= 7)) ? 0x80 : 0);
535
+ pbf.buf[pbf.pos++] = (high & 0x7f) | ((high >>>= 7) ? 0x80 : 0);
515
536
  if (!high) return;
516
- pbf.buf[pbf.pos++] = high & 0x7f | (((high >>>= 7)) ? 0x80 : 0);
537
+ pbf.buf[pbf.pos++] = (high & 0x7f) | ((high >>>= 7) ? 0x80 : 0);
517
538
  if (!high) return;
518
- pbf.buf[pbf.pos++] = high & 0x7f | (((high >>>= 7)) ? 0x80 : 0);
539
+ pbf.buf[pbf.pos++] = (high & 0x7f) | ((high >>>= 7) ? 0x80 : 0);
519
540
  if (!high) return;
520
- pbf.buf[pbf.pos++] = high & 0x7f | (((high >>>= 7)) ? 0x80 : 0);
541
+ pbf.buf[pbf.pos++] = (high & 0x7f) | ((high >>>= 7) ? 0x80 : 0);
521
542
  if (!high) return;
522
543
  pbf.buf[pbf.pos++] = high & 0x7f;
523
544
  }
524
545
 
525
546
  function makeRoomForExtraLength(startPos, len, pbf) {
526
547
  const extraLen =
527
- len <= 0x3fff ? 1 :
528
- len <= 0x1fffff ? 2 :
529
- len <= 0xfffffff ? 3 : Math.floor(Math.log(len) / (Math.LN2 * 7));
548
+ len <= 0x3fff ? 1 : len <= 0x1fffff ? 2 : len <= 0xfffffff ? 3 : Math.floor(Math.log(len) / (Math.LN2 * 7));
530
549
 
531
550
  // if 1 byte isn't enough for encoding message length, shift the data to the right
532
551
  pbf.realloc(extraLen);
533
552
  for (let i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i];
534
553
  }
535
554
 
536
- function writePackedVarint(arr, pbf) { for (let i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]); }
555
+ function writePackedVarint(arr, pbf) {
556
+ for (let i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]);
557
+ }
537
558
 
538
- function writePackedSVarint(arr, pbf) { for (let i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]); }
559
+ function writePackedSVarint(arr, pbf) {
560
+ for (let i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]);
561
+ }
539
562
 
540
- function writePackedFloat(arr, pbf) { for (let i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]); }
563
+ function writePackedFloat(arr, pbf) {
564
+ for (let i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]);
565
+ }
541
566
 
542
- function writePackedDouble(arr, pbf) { for (let i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]); }
567
+ function writePackedDouble(arr, pbf) {
568
+ for (let i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]);
569
+ }
543
570
 
544
- function writePackedBoolean(arr, pbf) { for (let i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]); }
571
+ function writePackedBoolean(arr, pbf) {
572
+ for (let i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]);
573
+ }
545
574
 
546
- function writePackedFixed32(arr, pbf) { for (let i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]); }
575
+ function writePackedFixed32(arr, pbf) {
576
+ for (let i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]);
577
+ }
547
578
 
548
- function writePackedSFixed32(arr, pbf) { for (let i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]); }
579
+ function writePackedSFixed32(arr, pbf) {
580
+ for (let i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]);
581
+ }
549
582
 
550
- function writePackedFixed64(arr, pbf) { for (let i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]); }
583
+ function writePackedFixed64(arr, pbf) {
584
+ for (let i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]);
585
+ }
551
586
 
552
- function writePackedSFixed64(arr, pbf) { for (let i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]); }
587
+ function writePackedSFixed64(arr, pbf) {
588
+ for (let i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]);
589
+ }
553
590
 
554
591
  // Buffer code below from https://github.com/feross/buffer, MIT-licensed
555
592
 
556
593
  function readUInt32(buf, pos) {
557
- return ((buf[pos]) |
558
- (buf[pos + 1] << 8) |
559
- (buf[pos + 2] << 16)) +
560
- (buf[pos + 3] * 0x1000000);
594
+ return (buf[pos] | (buf[pos + 1] << 8) | (buf[pos + 2] << 16)) + buf[pos + 3] * 0x1000000;
561
595
  }
562
596
 
563
597
  function writeInt32(buf, val, pos) {
564
598
  buf[pos] = val;
565
- buf[pos + 1] = (val >>> 8);
566
- buf[pos + 2] = (val >>> 16);
567
- buf[pos + 3] = (val >>> 24);
599
+ buf[pos + 1] = val >>> 8;
600
+ buf[pos + 2] = val >>> 16;
601
+ buf[pos + 3] = val >>> 24;
568
602
  }
569
603
 
570
604
  function readInt32(buf, pos) {
571
- return ((buf[pos]) |
572
- (buf[pos + 1] << 8) |
573
- (buf[pos + 2] << 16)) +
574
- (buf[pos + 3] << 24);
605
+ return (buf[pos] | (buf[pos + 1] << 8) | (buf[pos + 2] << 16)) + (buf[pos + 3] << 24);
575
606
  }
576
607
 
577
608
  function readUtf8(buf, pos, end) {
@@ -581,10 +612,7 @@ function readUtf8(buf, pos, end) {
581
612
  while (i < end) {
582
613
  const b0 = buf[i];
583
614
  let c = null; // codepoint
584
- let bytesPerSequence =
585
- b0 > 0xEF ? 4 :
586
- b0 > 0xDF ? 3 :
587
- b0 > 0xBF ? 2 : 1;
615
+ let bytesPerSequence = b0 > 0xef ? 4 : b0 > 0xdf ? 3 : b0 > 0xbf ? 2 : 1;
588
616
 
589
617
  if (i + bytesPerSequence > end) break;
590
618
 
@@ -598,18 +626,18 @@ function readUtf8(buf, pos, end) {
598
626
  }
599
627
  } else if (bytesPerSequence === 2) {
600
628
  b1 = buf[i + 1];
601
- if ((b1 & 0xC0) === 0x80) {
602
- c = (b0 & 0x1F) << 0x6 | (b1 & 0x3F);
603
- if (c <= 0x7F) {
629
+ if ((b1 & 0xc0) === 0x80) {
630
+ c = ((b0 & 0x1f) << 0x6) | (b1 & 0x3f);
631
+ if (c <= 0x7f) {
604
632
  c = null;
605
633
  }
606
634
  }
607
635
  } else if (bytesPerSequence === 3) {
608
636
  b1 = buf[i + 1];
609
637
  b2 = buf[i + 2];
610
- if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80) {
611
- c = (b0 & 0xF) << 0xC | (b1 & 0x3F) << 0x6 | (b2 & 0x3F);
612
- if (c <= 0x7FF || (c >= 0xD800 && c <= 0xDFFF)) {
638
+ if ((b1 & 0xc0) === 0x80 && (b2 & 0xc0) === 0x80) {
639
+ c = ((b0 & 0xf) << 0xc) | ((b1 & 0x3f) << 0x6) | (b2 & 0x3f);
640
+ if (c <= 0x7ff || (c >= 0xd800 && c <= 0xdfff)) {
613
641
  c = null;
614
642
  }
615
643
  }
@@ -617,22 +645,21 @@ function readUtf8(buf, pos, end) {
617
645
  b1 = buf[i + 1];
618
646
  b2 = buf[i + 2];
619
647
  b3 = buf[i + 3];
620
- if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80) {
621
- c = (b0 & 0xF) << 0x12 | (b1 & 0x3F) << 0xC | (b2 & 0x3F) << 0x6 | (b3 & 0x3F);
622
- if (c <= 0xFFFF || c >= 0x110000) {
648
+ if ((b1 & 0xc0) === 0x80 && (b2 & 0xc0) === 0x80 && (b3 & 0xc0) === 0x80) {
649
+ c = ((b0 & 0xf) << 0x12) | ((b1 & 0x3f) << 0xc) | ((b2 & 0x3f) << 0x6) | (b3 & 0x3f);
650
+ if (c <= 0xffff || c >= 0x110000) {
623
651
  c = null;
624
652
  }
625
653
  }
626
654
  }
627
655
 
628
656
  if (c === null) {
629
- c = 0xFFFD;
657
+ c = 0xfffd;
630
658
  bytesPerSequence = 1;
631
-
632
- } else if (c > 0xFFFF) {
659
+ } else if (c > 0xffff) {
633
660
  c -= 0x10000;
634
- str += String.fromCharCode(c >>> 10 & 0x3FF | 0xD800);
635
- c = 0xDC00 | c & 0x3FF;
661
+ str += String.fromCharCode(((c >>> 10) & 0x3ff) | 0xd800);
662
+ c = 0xdc00 | (c & 0x3ff);
636
663
  }
637
664
 
638
665
  str += String.fromCharCode(c);
@@ -650,32 +677,31 @@ function writeUtf8(buf, str, pos) {
650
677
  for (let i = 0, c, lead; i < str.length; i++) {
651
678
  c = str.charCodeAt(i); // code point
652
679
 
653
- if (c > 0xD7FF && c < 0xE000) {
680
+ if (c > 0xd7ff && c < 0xe000) {
654
681
  if (lead) {
655
- if (c < 0xDC00) {
656
- buf[pos++] = 0xEF;
657
- buf[pos++] = 0xBF;
658
- buf[pos++] = 0xBD;
682
+ if (c < 0xdc00) {
683
+ buf[pos++] = 0xef;
684
+ buf[pos++] = 0xbf;
685
+ buf[pos++] = 0xbd;
659
686
  lead = c;
660
687
  continue;
661
- } else {
662
- c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
663
- lead = null;
664
688
  }
689
+ c = ((lead - 0xd800) << 10) | (c - 0xdc00) | 0x10000;
690
+ lead = null;
665
691
  } else {
666
- if (c > 0xDBFF || (i + 1 === str.length)) {
667
- buf[pos++] = 0xEF;
668
- buf[pos++] = 0xBF;
669
- buf[pos++] = 0xBD;
692
+ if (c > 0xdbff || i + 1 === str.length) {
693
+ buf[pos++] = 0xef;
694
+ buf[pos++] = 0xbf;
695
+ buf[pos++] = 0xbd;
670
696
  } else {
671
697
  lead = c;
672
698
  }
673
699
  continue;
674
700
  }
675
701
  } else if (lead) {
676
- buf[pos++] = 0xEF;
677
- buf[pos++] = 0xBF;
678
- buf[pos++] = 0xBD;
702
+ buf[pos++] = 0xef;
703
+ buf[pos++] = 0xbf;
704
+ buf[pos++] = 0xbd;
679
705
  lead = null;
680
706
  }
681
707
 
@@ -683,17 +709,17 @@ function writeUtf8(buf, str, pos) {
683
709
  buf[pos++] = c;
684
710
  } else {
685
711
  if (c < 0x800) {
686
- buf[pos++] = c >> 0x6 | 0xC0;
712
+ buf[pos++] = (c >> 0x6) | 0xc0;
687
713
  } else {
688
714
  if (c < 0x10000) {
689
- buf[pos++] = c >> 0xC | 0xE0;
715
+ buf[pos++] = (c >> 0xc) | 0xe0;
690
716
  } else {
691
- buf[pos++] = c >> 0x12 | 0xF0;
692
- buf[pos++] = c >> 0xC & 0x3F | 0x80;
717
+ buf[pos++] = (c >> 0x12) | 0xf0;
718
+ buf[pos++] = ((c >> 0xc) & 0x3f) | 0x80;
693
719
  }
694
- buf[pos++] = c >> 0x6 & 0x3F | 0x80;
720
+ buf[pos++] = ((c >> 0x6) & 0x3f) | 0x80;
695
721
  }
696
- buf[pos++] = c & 0x3F | 0x80;
722
+ buf[pos++] = (c & 0x3f) | 0x80;
697
723
  }
698
724
  }
699
725
  return pos;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapwhit/pbf",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "a low-level, lightweight protocol buffers implementation in JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -35,8 +35,8 @@
35
35
  "ieee754": "^1.1.12"
36
36
  },
37
37
  "devDependencies": {
38
+ "@biomejs/biome": "2.0.5",
38
39
  "benchmark": "^2.1.4",
39
- "eslint": "^8",
40
40
  "protobufjs": "~7",
41
41
  "protocol-buffers": "~5",
42
42
  "tile-stats-runner": "^1.0.0"
@@ -44,4 +44,4 @@
44
44
  "browser": {
45
45
  "assert": "@pirxpilot/nanoassert"
46
46
  }
47
- }
47
+ }