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