protobufjs 8.6.6 → 8.7.1

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.
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * protobuf.js v8.6.6 (c) 2016, daniel wirtz
3
- * compiled sat, 04 jul 2026 01:09:57 utc
2
+ * protobuf.js v8.7.1 (c) 2016, daniel wirtz
3
+ * compiled mon, 13 jul 2026 11:04:12 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -66,7 +66,7 @@ exports.configure = configure;
66
66
  * @returns {undefined}
67
67
  */
68
68
  function configure() {
69
- exports.util._configure();
69
+ exports.util.LongBits._configure(exports.util.Long);
70
70
  exports.Writer._configure(exports.BufferWriter);
71
71
  exports.Reader._configure(exports.BufferReader);
72
72
  }
@@ -92,7 +92,7 @@ function indexOutOfRange(reader, writeLength) {
92
92
 
93
93
  /**
94
94
  * Constructs a new reader instance using the specified buffer.
95
- * @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
95
+ * @classdesc Wire format reader using `Uint8Array`.
96
96
  * @constructor
97
97
  * @param {Uint8Array} buffer Buffer to read from
98
98
  */
@@ -116,6 +116,12 @@ function Reader(buffer) {
116
116
  */
117
117
  this.len = buffer.length;
118
118
 
119
+ /**
120
+ * Cached DataView for packed reads.
121
+ * @type {DataView|null}
122
+ */
123
+ this.view = null;
124
+
119
125
  /**
120
126
  * Whether to discard unknown fields while decoding.
121
127
  * @type {boolean}
@@ -123,18 +129,14 @@ function Reader(buffer) {
123
129
  this.discardUnknown = Reader.discardUnknown;
124
130
  }
125
131
 
126
- var create_array = typeof Uint8Array !== "undefined"
127
- ? function create_typed_array(buffer) {
128
- if (buffer instanceof Uint8Array || Array.isArray(buffer))
129
- return new Reader(buffer);
130
- throw Error("illegal buffer");
131
- }
132
- /* istanbul ignore next */
133
- : function create_array(buffer) {
134
- if (Array.isArray(buffer))
135
- return new Reader(buffer);
136
- throw Error("illegal buffer");
137
- };
132
+ function create_array(buffer) {
133
+ // TODO: Remove plain array reader support in the next major release.
134
+ if (Array.isArray(buffer))
135
+ buffer = new Uint8Array(buffer);
136
+ if (buffer instanceof Uint8Array)
137
+ return new Reader(buffer);
138
+ throw Error("illegal buffer");
139
+ }
138
140
 
139
141
  var create = function create() {
140
142
  return util.Buffer
@@ -159,8 +161,6 @@ var create = function create() {
159
161
  */
160
162
  Reader.create = create();
161
163
 
162
- Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore next */ util.Array.prototype.slice;
163
-
164
164
  /**
165
165
  * Returns raw bytes from the backing buffer without advancing the reader.
166
166
  * @param {number} start Start offset
@@ -168,12 +168,7 @@ Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore ne
168
168
  * @returns {Uint8Array} Raw bytes
169
169
  */
170
170
  Reader.prototype.raw = function read_raw(start, end) {
171
- if (Array.isArray(this.buf)) // plain array
172
- return this.buf.slice(start, end);
173
-
174
- if (start === end) // fix for IE 10/Win8 and others' subarray returning array of size 1
175
- return new this.buf.constructor(0);
176
- return this._slice.call(this.buf, start, end);
171
+ return this.buf.subarray(start, end);
177
172
  };
178
173
 
179
174
  /**
@@ -470,6 +465,241 @@ Reader.prototype.double = function read_double() {
470
465
  return value;
471
466
  };
472
467
 
468
+ /**
469
+ * Reads a packed repeated field of unsigned 32 bit varints.
470
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
471
+ * @returns {number[]} Array read into
472
+ */
473
+ Reader.prototype.uint32s = function read_uint32s(array) {
474
+ if (array === undefined) array = [];
475
+ var end = this.uint32() + this.pos;
476
+ while (this.pos < end)
477
+ array.push(this.uint32());
478
+ return array;
479
+ };
480
+
481
+ /**
482
+ * Reads a packed repeated field of signed 32 bit varints.
483
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
484
+ * @returns {number[]} Array read into
485
+ */
486
+ Reader.prototype.int32s = function read_int32s(array) {
487
+ if (array === undefined) array = [];
488
+ var end = this.uint32() + this.pos;
489
+ while (this.pos < end)
490
+ array.push(this.int32());
491
+ return array;
492
+ };
493
+
494
+ /**
495
+ * Reads a packed repeated field of zig-zag encoded signed 32 bit varints.
496
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
497
+ * @returns {number[]} Array read into
498
+ */
499
+ Reader.prototype.sint32s = function read_sint32s(array) {
500
+ if (array === undefined) array = [];
501
+ var end = this.uint32() + this.pos;
502
+ while (this.pos < end)
503
+ array.push(this.sint32());
504
+ return array;
505
+ };
506
+
507
+ /**
508
+ * Reads a packed repeated field of booleans.
509
+ * @param {boolean[]} [array] Array to read into; a new one is created if omitted
510
+ * @returns {boolean[]} Array read into
511
+ */
512
+ Reader.prototype.bools = function read_bools(array) {
513
+ if (array === undefined) array = [];
514
+ var end = this.uint32() + this.pos;
515
+ while (this.pos < end)
516
+ array.push(this.bool());
517
+ return array;
518
+ };
519
+
520
+ // The view allocation only pays off when amortized over enough reads
521
+ var VIEW_THRESHOLD_FLOAT = 8,
522
+ VIEW_THRESHOLD_INT = 128;
523
+
524
+ function getLazyView(reader, count, threshold) {
525
+ var view = reader.view;
526
+ if (view || count < threshold)
527
+ return view;
528
+ var buf = reader.buf;
529
+ return reader.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
530
+ }
531
+
532
+ /**
533
+ * Reads a packed repeated field of unsigned 32 bit fixed values.
534
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
535
+ * @returns {number[]} Array read into
536
+ */
537
+ Reader.prototype.fixed32s = function read_fixed32s(array) {
538
+ if (array === undefined) array = [];
539
+ var len = this.uint32(), end = this.pos + len;
540
+ /* istanbul ignore if */
541
+ if (end > this.len) throw indexOutOfRange(this, len);
542
+ var count = len >>> 2, i = array.length, pos = this.pos;
543
+ array.length = i + count;
544
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
545
+ if (dv)
546
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getUint32(pos, true);
547
+ else {
548
+ var buf = this.buf;
549
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4);
550
+ }
551
+ this.pos = pos;
552
+ if (pos !== end) throw indexOutOfRange(this, 4);
553
+ return array;
554
+ };
555
+
556
+ /**
557
+ * Reads a packed repeated field of signed 32 bit fixed values.
558
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
559
+ * @returns {number[]} Array read into
560
+ */
561
+ Reader.prototype.sfixed32s = function read_sfixed32s(array) {
562
+ if (array === undefined) array = [];
563
+ var len = this.uint32(), end = this.pos + len;
564
+ /* istanbul ignore if */
565
+ if (end > this.len) throw indexOutOfRange(this, len);
566
+ var count = len >>> 2, i = array.length, pos = this.pos;
567
+ array.length = i + count;
568
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
569
+ if (dv)
570
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getInt32(pos, true);
571
+ else {
572
+ var buf = this.buf;
573
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4) | 0;
574
+ }
575
+ this.pos = pos;
576
+ if (pos !== end) throw indexOutOfRange(this, 4);
577
+ return array;
578
+ };
579
+
580
+ /**
581
+ * Reads a packed repeated field of floats (32 bit).
582
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
583
+ * @returns {number[]} Array read into
584
+ */
585
+ Reader.prototype.floats = function read_floats(array) {
586
+ if (array === undefined) array = [];
587
+ var len = this.uint32(), end = this.pos + len;
588
+ /* istanbul ignore if */
589
+ if (end > this.len) throw indexOutOfRange(this, len);
590
+ var count = len >>> 2, i = array.length, pos = this.pos;
591
+ array.length = i + count;
592
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
593
+ if (dv)
594
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getFloat32(pos, true);
595
+ else {
596
+ var buf = this.buf;
597
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = util.float.readFloatLE(buf, pos);
598
+ }
599
+ this.pos = pos;
600
+ if (pos !== end) throw indexOutOfRange(this, 4);
601
+ return array;
602
+ };
603
+
604
+ /**
605
+ * Reads a packed repeated field of doubles (64 bit float).
606
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
607
+ * @returns {number[]} Array read into
608
+ */
609
+ Reader.prototype.doubles = function read_doubles(array) {
610
+ if (array === undefined) array = [];
611
+ var len = this.uint32(), end = this.pos + len;
612
+ /* istanbul ignore if */
613
+ if (end > this.len) throw indexOutOfRange(this, len);
614
+ var count = len >>> 3, i = array.length, pos = this.pos;
615
+ array.length = i + count;
616
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
617
+ if (dv)
618
+ for (var k = 0; k < count; ++k, pos += 8) array[i++] = dv.getFloat64(pos, true);
619
+ else {
620
+ var buf = this.buf;
621
+ for (var j = 0; j < count; ++j, pos += 8) array[i++] = util.float.readDoubleLE(buf, pos);
622
+ }
623
+ this.pos = pos;
624
+ if (pos !== end) throw indexOutOfRange(this, 8);
625
+ return array;
626
+ };
627
+
628
+ /**
629
+ * Reads a packed repeated field of unsigned 64 bit varints.
630
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
631
+ * @returns {Array.<Long|number>} Array read into
632
+ */
633
+ Reader.prototype.uint64s = function read_uint64s(array) {
634
+ if (array === undefined) array = [];
635
+ var end = this.uint32() + this.pos;
636
+ while (this.pos < end)
637
+ array.push(this.uint64());
638
+ return array;
639
+ };
640
+
641
+ /**
642
+ * Reads a packed repeated field of signed 64 bit varints.
643
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
644
+ * @returns {Array.<Long|number>} Array read into
645
+ */
646
+ Reader.prototype.int64s = function read_int64s(array) {
647
+ if (array === undefined) array = [];
648
+ var end = this.uint32() + this.pos;
649
+ while (this.pos < end)
650
+ array.push(this.int64());
651
+ return array;
652
+ };
653
+
654
+ /**
655
+ * Reads a packed repeated field of zig-zag encoded signed 64 bit varints.
656
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
657
+ * @returns {Array.<Long|number>} Array read into
658
+ */
659
+ Reader.prototype.sint64s = function read_sint64s(array) {
660
+ if (array === undefined) array = [];
661
+ var end = this.uint32() + this.pos;
662
+ while (this.pos < end)
663
+ array.push(this.sint64());
664
+ return array;
665
+ };
666
+
667
+ /**
668
+ * Reads a packed repeated field of unsigned 64 bit fixed values.
669
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
670
+ * @returns {Array.<Long|number>} Array read into
671
+ */
672
+ Reader.prototype.fixed64s = function read_fixed64s(array) {
673
+ if (array === undefined) array = [];
674
+ var len = this.uint32(), end = this.pos + len, i = array.length;
675
+ /* istanbul ignore if */
676
+ if (end > this.len) throw indexOutOfRange(this, len);
677
+ var count = len >>> 3;
678
+ array.length = i + count; // 8 bytes per value, count is known
679
+ for (var j = 0; j < count; ++j)
680
+ array[i++] = this.fixed64();
681
+ if (this.pos !== end) throw indexOutOfRange(this, 8);
682
+ return array;
683
+ };
684
+
685
+ /**
686
+ * Reads a packed repeated field of signed 64 bit fixed values.
687
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
688
+ * @returns {Array.<Long|number>} Array read into
689
+ */
690
+ Reader.prototype.sfixed64s = function read_sfixed64s(array) {
691
+ if (array === undefined) array = [];
692
+ var len = this.uint32(), end = this.pos + len, i = array.length;
693
+ /* istanbul ignore if */
694
+ if (end > this.len) throw indexOutOfRange(this, len);
695
+ var count = len >>> 3;
696
+ array.length = i + count; // 8 bytes per value, count is known
697
+ for (var j = 0; j < count; ++j)
698
+ array[i++] = this.sfixed64();
699
+ if (this.pos !== end) throw indexOutOfRange(this, 8);
700
+ return array;
701
+ };
702
+
473
703
  /**
474
704
  * Reads a sequence of bytes preceeded by its length as a varint.
475
705
  * @returns {Uint8Array} Value read
@@ -665,7 +895,7 @@ function BufferReader(buffer) {
665
895
  * Read buffer.
666
896
  * @name BufferReader#buf
667
897
  * @type {Buffer}
668
- */
898
+ */
669
899
  }
670
900
 
671
901
  BufferReader._configure = function () {
@@ -683,8 +913,6 @@ BufferReader._configure = function () {
683
913
  * @returns {Buffer} Raw bytes
684
914
  */
685
915
  BufferReader.prototype.raw = function read_raw_buffer(start, end) {
686
- if (start === end)
687
- return util.Buffer.alloc(0);
688
916
  return this._slice.call(this.buf, start, end);
689
917
  };
690
918
 
@@ -1561,7 +1789,7 @@ function readUintBE(buf, pos) {
1561
1789
  "use strict";
1562
1790
  module.exports = LongBits;
1563
1791
 
1564
- var util = require(12);
1792
+ var Long;
1565
1793
 
1566
1794
  /**
1567
1795
  * Constructs new long bits.
@@ -1640,10 +1868,10 @@ LongBits.fromNumber = function fromNumber(value) {
1640
1868
  LongBits.from = function from(value) {
1641
1869
  if (typeof value === "number")
1642
1870
  return LongBits.fromNumber(value);
1643
- if (util.isString(value)) {
1871
+ if (typeof value === "string" || value instanceof String) {
1644
1872
  /* istanbul ignore else */
1645
- if (util.Long)
1646
- value = util.Long.fromString(value);
1873
+ if (Long)
1874
+ value = Long.fromString(value);
1647
1875
  else
1648
1876
  return LongBits.fromNumber(parseInt(value, 10));
1649
1877
  }
@@ -1672,8 +1900,8 @@ LongBits.prototype.toNumber = function toNumber(unsigned) {
1672
1900
  * @returns {Long} Long
1673
1901
  */
1674
1902
  LongBits.prototype.toLong = function toLong(unsigned) {
1675
- return util.Long
1676
- ? new util.Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
1903
+ return Long
1904
+ ? new Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
1677
1905
  /* istanbul ignore next */
1678
1906
  : { low: this.lo | 0, high: this.hi | 0, unsigned: Boolean(unsigned) };
1679
1907
  };
@@ -1759,7 +1987,11 @@ LongBits.prototype.length = function length() {
1759
1987
  : part2 < 128 ? 9 : 10;
1760
1988
  };
1761
1989
 
1762
- },{"12":12}],12:[function(require,module,exports){
1990
+ LongBits._configure = function(Long_) {
1991
+ Long = Long_;
1992
+ };
1993
+
1994
+ },{}],12:[function(require,module,exports){
1763
1995
  "use strict";
1764
1996
  /* global globalThis */
1765
1997
  var util = exports;
@@ -1899,36 +2131,29 @@ util.isSet = function isSet(obj, prop) {
1899
2131
  util.Buffer = (function() {
1900
2132
  try {
1901
2133
  var Buffer = util.global.Buffer;
1902
- // refuse to use non-node buffers if not explicitly assigned (perf reasons):
1903
- return Buffer.prototype.utf8Write ? Buffer : /* istanbul ignore next */ null;
2134
+ // refuse to use non-node buffers if not explicitly assigned (perf reasons)
2135
+ return Buffer.prototype.utf8Write || util.isNode ? Buffer : /* istanbul ignore next */ null;
1904
2136
  } catch (e) {
1905
2137
  /* istanbul ignore next */
1906
2138
  return null;
1907
2139
  }
1908
2140
  })();
1909
2141
 
1910
- // Internal alias of or polyfull for Buffer.from.
1911
- util._Buffer_from = null;
1912
-
1913
- // Internal alias of or polyfill for Buffer.allocUnsafe.
1914
- util._Buffer_allocUnsafe = null;
1915
-
1916
2142
  /**
1917
2143
  * Creates a new buffer of whatever type supported by the environment.
1918
2144
  * @param {number|number[]} [sizeOrArray=0] Buffer size or number array
1919
2145
  * @returns {Uint8Array|Buffer} Buffer
1920
2146
  */
1921
2147
  util.newBuffer = function newBuffer(sizeOrArray) {
2148
+ var Buffer = util.Buffer;
1922
2149
  /* istanbul ignore next */
1923
2150
  return typeof sizeOrArray === "number"
1924
- ? util.Buffer
1925
- ? util._Buffer_allocUnsafe(sizeOrArray)
1926
- : new util.Array(sizeOrArray)
1927
- : util.Buffer
1928
- ? util._Buffer_from(sizeOrArray)
1929
- : typeof Uint8Array === "undefined"
1930
- ? sizeOrArray
1931
- : new Uint8Array(sizeOrArray);
2151
+ ? Buffer
2152
+ ? Buffer.allocUnsafe(sizeOrArray)
2153
+ : new Uint8Array(sizeOrArray)
2154
+ : Buffer
2155
+ ? Buffer.from(sizeOrArray)
2156
+ : new Uint8Array(sizeOrArray);
1932
2157
  };
1933
2158
 
1934
2159
  /**
@@ -1954,10 +2179,11 @@ util.rawField = function rawField(id, wireType, data) {
1954
2179
  };
1955
2180
 
1956
2181
  /**
1957
- * Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
2182
+ * Array implementation used in the browser.
1958
2183
  * @type {Constructor<Uint8Array>}
2184
+ * @deprecated Use `Uint8Array` instead.
1959
2185
  */
1960
- util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : Array;
2186
+ util.Array = Uint8Array;
1961
2187
 
1962
2188
  /**
1963
2189
  * Any compatible Long instance.
@@ -2278,28 +2504,6 @@ util.toJSONOptions = {
2278
2504
  json: true
2279
2505
  };
2280
2506
 
2281
- // Sets up buffer utility according to the environment (called in index-minimal)
2282
- util._configure = function() {
2283
- var Buffer = util.Buffer;
2284
- /* istanbul ignore if */
2285
- if (!Buffer) {
2286
- util._Buffer_from = util._Buffer_allocUnsafe = null;
2287
- return;
2288
- }
2289
- // because node 4.x buffers are incompatible & immutable
2290
- // see: https://github.com/dcodeIO/protobuf.js/pull/665
2291
- util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
2292
- /* istanbul ignore next */
2293
- function Buffer_from(value, encoding) {
2294
- return new Buffer(value, encoding);
2295
- };
2296
- util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
2297
- /* istanbul ignore next */
2298
- function Buffer_allocUnsafe(size) {
2299
- return new Buffer(size);
2300
- };
2301
- };
2302
-
2303
2507
  },{"10":10,"11":11,"13":13,"14":14,"7":7,"8":8,"9":9,"long":"long"}],13:[function(require,module,exports){
2304
2508
  "use strict";
2305
2509
  module.exports = pool;
@@ -2354,19 +2558,21 @@ function pool(alloc, slice, size) {
2354
2558
  "use strict";
2355
2559
 
2356
2560
  /**
2357
- * A minimal UTF8 implementation for number arrays.
2561
+ * A minimal UTF8 implementation.
2358
2562
  * @memberof util
2359
2563
  * @namespace
2360
2564
  */
2361
2565
  var utf8 = exports,
2362
2566
  replacementChar = "\ufffd",
2567
+ looseDecoder = new TextDecoder("utf-8", { ignoreBOM: true }),
2363
2568
  strictDecoder;
2569
+ var TEXT_DECODER_MIN_LENGTH = 64;
2364
2570
 
2365
2571
  try {
2366
2572
  strictDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
2367
2573
  } catch (err) {
2368
2574
  // "fatal" option is not supported on Node.js compiled without ICU
2369
- strictDecoder = new TextDecoder("utf-8", { ignoreBOM: true });
2575
+ strictDecoder = looseDecoder;
2370
2576
  }
2371
2577
 
2372
2578
  /**
@@ -2417,6 +2623,13 @@ function utf8_read_js(buffer, start, end, str) {
2417
2623
  return str;
2418
2624
  }
2419
2625
 
2626
+ function utf8_read_decoder(decoder, buffer, start, end) {
2627
+ var source = start === 0 && end === buffer.length
2628
+ ? buffer
2629
+ : buffer.subarray(start, end);
2630
+ return decoder.decode(source);
2631
+ }
2632
+
2420
2633
  /**
2421
2634
  * Reads UTF8 bytes as a string.
2422
2635
  * @param {Uint8Array} buffer Source buffer
@@ -2424,9 +2637,11 @@ function utf8_read_js(buffer, start, end, str) {
2424
2637
  * @param {number} end Source end
2425
2638
  * @returns {string} String read
2426
2639
  */
2427
- utf8.read = function utf8_read_ascii(buffer, start, end) {
2640
+ utf8.read = function utf8_read_loose(buffer, start, end) {
2428
2641
  if (end - start < 1)
2429
2642
  return "";
2643
+ if (end - start >= TEXT_DECODER_MIN_LENGTH)
2644
+ return utf8_read_decoder(looseDecoder, buffer, start, end);
2430
2645
 
2431
2646
  var str = "",
2432
2647
  i = start,
@@ -2456,17 +2671,6 @@ utf8.read = function utf8_read_ascii(buffer, start, end) {
2456
2671
  return str;
2457
2672
  };
2458
2673
 
2459
- function utf8_read_strict(buffer, start, end) {
2460
- var source = start === 0 && end === buffer.length
2461
- ? buffer
2462
- : buffer.subarray
2463
- ? buffer.subarray(start, end)
2464
- : buffer.slice(start, end);
2465
- if (Array.isArray(source))
2466
- source = Uint8Array.from(source);
2467
- return strictDecoder.decode(source);
2468
- }
2469
-
2470
2674
  /**
2471
2675
  * Reads UTF8 bytes as a string, rejecting invalid UTF8.
2472
2676
  * @param {Uint8Array} buffer Source buffer
@@ -2474,9 +2678,11 @@ function utf8_read_strict(buffer, start, end) {
2474
2678
  * @param {number} end Source end
2475
2679
  * @returns {string} String read
2476
2680
  */
2477
- utf8.readStrict = function utf8_read_strict_ascii(buffer, start, end) {
2681
+ utf8.readStrict = function utf8_read_strict(buffer, start, end) {
2478
2682
  if (end - start < 1)
2479
2683
  return "";
2684
+ if (end - start >= TEXT_DECODER_MIN_LENGTH)
2685
+ return utf8_read_decoder(strictDecoder, buffer, start, end);
2480
2686
 
2481
2687
  var str = "",
2482
2688
  i = start,
@@ -2492,14 +2698,14 @@ utf8.readStrict = function utf8_read_strict_ascii(buffer, start, end) {
2492
2698
  c7 = buffer[i + 6];
2493
2699
  c8 = buffer[i + 7];
2494
2700
  if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80)
2495
- return str + utf8_read_strict(buffer, i, end);
2701
+ return str + utf8_read_decoder(strictDecoder, buffer, i, end);
2496
2702
  str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
2497
2703
  }
2498
2704
 
2499
2705
  for (; i < end; ++i) {
2500
2706
  c1 = buffer[i];
2501
2707
  if (c1 & 0x80)
2502
- return str + utf8_read_strict(buffer, i, end);
2708
+ return str + utf8_read_decoder(strictDecoder, buffer, i, end);
2503
2709
  str += String.fromCharCode(c1);
2504
2710
  }
2505
2711
 
@@ -2552,118 +2758,52 @@ var LongBits = util.LongBits,
2552
2758
  base64 = util.base64,
2553
2759
  utf8 = util.utf8;
2554
2760
 
2555
- /**
2556
- * Constructs a new writer operation instance.
2557
- * @classdesc Scheduled writer operation.
2558
- * @constructor
2559
- * @param {function(*, Uint8Array, number)} fn Function to call
2560
- * @param {number} len Value byte length
2561
- * @param {*} val Value to write
2562
- * @ignore
2563
- */
2564
- function Op(fn, len, val) {
2565
-
2566
- /**
2567
- * Function to call.
2568
- * @type {function(Uint8Array, number, *)}
2569
- */
2570
- this.fn = fn;
2571
-
2572
- /**
2573
- * Value byte length.
2574
- * @type {number}
2575
- */
2576
- this.len = len;
2577
-
2578
- /**
2579
- * Next operation.
2580
- * @type {Writer.Op|undefined}
2581
- */
2582
- this.next = undefined;
2583
-
2584
- /**
2585
- * Value to write.
2586
- * @type {*}
2587
- */
2588
- this.val = val; // type varies
2589
- }
2590
-
2591
- /* istanbul ignore next */
2592
- function noop() {} // eslint-disable-line no-empty-function
2593
-
2594
- /**
2595
- * Constructs a new writer state instance.
2596
- * @classdesc Copied writer state.
2597
- * @memberof Writer
2598
- * @constructor
2599
- * @param {Writer} writer Writer to copy state from
2600
- * @ignore
2601
- */
2602
- function State(writer) {
2603
-
2604
- /**
2605
- * Current head.
2606
- * @type {Writer.Op}
2607
- */
2608
- this.head = writer.head;
2609
-
2610
- /**
2611
- * Current tail.
2612
- * @type {Writer.Op}
2613
- */
2614
- this.tail = writer.tail;
2615
-
2616
- /**
2617
- * Current buffer length.
2618
- * @type {number}
2619
- */
2620
- this.len = writer.len;
2621
-
2622
- /**
2623
- * Next state.
2624
- * @type {State|null}
2625
- */
2626
- this.next = writer.states;
2627
- }
2628
-
2629
2761
  /**
2630
2762
  * Constructs a new writer instance.
2631
- * @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
2763
+ * @classdesc Wire format writer using `Uint8Array`.
2632
2764
  * @constructor
2633
2765
  */
2634
2766
  function Writer() {
2635
2767
 
2636
2768
  /**
2637
- * Current length.
2769
+ * Write cursor into {@link Writer#buf}.
2638
2770
  * @type {number}
2639
2771
  */
2640
- this.len = 0;
2772
+ this.pos = 0;
2641
2773
 
2642
2774
  /**
2643
- * Operations head.
2644
- * @type {Object}
2775
+ * Backing buffer.
2776
+ * @type {Uint8Array}
2645
2777
  */
2646
- this.head = new Op(noop, 0, 0);
2778
+ this.buf = this.constructor.alloc(64);
2647
2779
 
2648
2780
  /**
2649
- * Operations tail
2650
- * @type {Object}
2781
+ * Cached DataView over {@link Writer#buf}.
2782
+ * @type {DataView|null}
2651
2783
  */
2652
- this.tail = this.head;
2784
+ this.view = null;
2653
2785
 
2654
2786
  /**
2655
- * Linked forked states.
2656
- * @type {Object|null}
2787
+ * Stack of forked length-prefix positions.
2788
+ * @type {Array<number>|null}
2657
2789
  */
2658
2790
  this.states = null;
2659
-
2660
- // When a value is written, the writer calculates its byte length and puts it into a linked
2661
- // list of operations to perform when finish() is called. This both allows us to allocate
2662
- // buffers of the exact required size and reduces the amount of work we have to do compared
2663
- // to first calculating over objects and then encoding over objects. In our case, the encoding
2664
- // part is just a linked list walk calling operations with already prepared values.
2665
2791
  }
2666
2792
 
2793
+ /**
2794
+ * Current write position.
2795
+ * @name Writer#len
2796
+ * @type {number}
2797
+ * @deprecated Use {@link Writer#pos} instead.
2798
+ */
2799
+ Object.defineProperty(Writer.prototype, "len", {
2800
+ configurable: true,
2801
+ enumerable: true,
2802
+ get: function get_len() {
2803
+ return this.pos;
2804
+ }
2805
+ });
2806
+
2667
2807
  var create = function create() {
2668
2808
  return util.Buffer
2669
2809
  ? function create_buffer_setup() {
@@ -2690,32 +2830,45 @@ Writer.create = create();
2690
2830
  * @returns {Uint8Array} Buffer
2691
2831
  */
2692
2832
  Writer.alloc = function alloc(size) {
2693
- return new util.Array(size);
2833
+ return new Uint8Array(size);
2694
2834
  };
2695
2835
 
2696
2836
  // Use Uint8Array buffer pool in the browser, just like node does with buffers
2697
- /* istanbul ignore else */
2698
- if (util.Array !== Array)
2699
- Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
2837
+ Writer.alloc = util.pool(Writer.alloc, Uint8Array.prototype.subarray);
2700
2838
 
2701
2839
  /**
2702
- * Pushes a new operation to the queue.
2703
- * @param {function(Uint8Array, number, *)} fn Function to call
2704
- * @param {number} len Value byte length
2705
- * @param {number} val Value to write
2706
- * @returns {Writer} `this`
2840
+ * Calculates the number of bytes a value occupies as a varint.
2841
+ * @param {number} value Value to size (unsigned)
2842
+ * @returns {number} Byte length (1..5)
2843
+ * @ignore
2844
+ */
2845
+ function sizeVarint32(value) {
2846
+ return value < 128 ? 1
2847
+ : value < 16384 ? 2
2848
+ : value < 2097152 ? 3
2849
+ : value < 268435456 ? 4
2850
+ : 5;
2851
+ }
2852
+
2853
+ /**
2854
+ * Ensures that at least `n` more bytes fit into the backing buffer, doubling it if not.
2855
+ * @param {number} n Number of additional bytes required
2856
+ * @returns {undefined}
2707
2857
  * @private
2708
2858
  */
2709
- Writer.prototype._push = function push(fn, len, val) {
2710
- this.tail = this.tail.next = new Op(fn, len, val);
2711
- this.len += len;
2712
- return this;
2859
+ Writer.prototype._reserve = function _reserve(n) {
2860
+ var need = this.pos + n;
2861
+ if (need > this.buf.length) {
2862
+ var size = this.buf.length << 1;
2863
+ if (size < need)
2864
+ size = need;
2865
+ var buf = this.constructor.alloc(size);
2866
+ buf.set(this.buf.subarray(0, this.pos), 0);
2867
+ this.buf = buf;
2868
+ this.view = null; // invalidate
2869
+ }
2713
2870
  };
2714
2871
 
2715
- function writeByte(val, buf, pos) {
2716
- buf[pos] = val & 255;
2717
- }
2718
-
2719
2872
  function writeStringAscii(val, buf, pos) {
2720
2873
  for (var i = 0; i < val.length;)
2721
2874
  buf[pos++] = val.charCodeAt(i++);
@@ -2727,42 +2880,19 @@ function writeVarint32(val, buf, pos) {
2727
2880
  val >>>= 7;
2728
2881
  }
2729
2882
  buf[pos] = val;
2883
+ return pos + 1;
2730
2884
  }
2731
2885
 
2732
- /**
2733
- * Constructs a new varint writer operation instance.
2734
- * @classdesc Scheduled varint writer operation.
2735
- * @extends Op
2736
- * @constructor
2737
- * @param {number} len Value byte length
2738
- * @param {number} val Value to write
2739
- * @ignore
2740
- */
2741
- function VarintOp(len, val) {
2742
- this.len = len;
2743
- this.next = undefined;
2744
- this.val = val;
2745
- }
2746
-
2747
- VarintOp.prototype = Object.create(Op.prototype);
2748
- VarintOp.prototype.fn = writeVarint32;
2749
-
2750
2886
  /**
2751
2887
  * Writes an unsigned 32 bit value as a varint.
2752
2888
  * @param {number} value Value to write
2753
2889
  * @returns {Writer} `this`
2754
2890
  */
2755
2891
  Writer.prototype.uint32 = function write_uint32(value) {
2756
- // here, the call to this.push has been inlined and a varint specific Op subclass is used.
2757
- // uint32 is by far the most frequently used operation and benefits significantly from this.
2758
- this.len += (this.tail = this.tail.next = new VarintOp(
2759
- (value = value >>> 0)
2760
- < 128 ? 1
2761
- : value < 16384 ? 2
2762
- : value < 2097152 ? 3
2763
- : value < 268435456 ? 4
2764
- : 5,
2765
- value)).len;
2892
+ value = value >>> 0;
2893
+ this._reserve(5);
2894
+ var pos = this.pos;
2895
+ this.pos = writeVarint32(value, this.buf, pos);
2766
2896
  return this;
2767
2897
  };
2768
2898
 
@@ -2773,9 +2903,13 @@ Writer.prototype.uint32 = function write_uint32(value) {
2773
2903
  * @returns {Writer} `this`
2774
2904
  */
2775
2905
  Writer.prototype.int32 = function write_int32(value) {
2776
- return (value |= 0) < 0
2777
- ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
2778
- : this.uint32(value);
2906
+ if ((value |= 0) < 0) { // 10 bytes per spec
2907
+ this._reserve(10);
2908
+ writeVarint64(LongBits.fromNumber(value), this.buf, this.pos);
2909
+ this.pos += 10;
2910
+ return this;
2911
+ }
2912
+ return this.uint32(value);
2779
2913
  };
2780
2914
 
2781
2915
  /**
@@ -2799,7 +2933,8 @@ function writeVarint64(val, buf, pos) {
2799
2933
  buf[pos++] = lo & 127 | 128;
2800
2934
  lo = lo >>> 7;
2801
2935
  }
2802
- buf[pos++] = lo;
2936
+ buf[pos] = lo;
2937
+ return pos + 1;
2803
2938
  }
2804
2939
 
2805
2940
  /**
@@ -2810,7 +2945,10 @@ function writeVarint64(val, buf, pos) {
2810
2945
  */
2811
2946
  Writer.prototype.uint64 = function write_uint64(value) {
2812
2947
  var bits = LongBits.from(value);
2813
- return this._push(writeVarint64, bits.length(), bits);
2948
+ this._reserve(10);
2949
+ var pos = this.pos;
2950
+ this.pos = writeVarint64(bits, this.buf, pos);
2951
+ return this;
2814
2952
  };
2815
2953
 
2816
2954
  /**
@@ -2830,7 +2968,10 @@ Writer.prototype.int64 = Writer.prototype.uint64;
2830
2968
  */
2831
2969
  Writer.prototype.sint64 = function write_sint64(value) {
2832
2970
  var bits = LongBits.from(value).zzEncode();
2833
- return this._push(writeVarint64, bits.length(), bits);
2971
+ this._reserve(10);
2972
+ var pos = this.pos;
2973
+ this.pos = writeVarint64(bits, this.buf, pos);
2974
+ return this;
2834
2975
  };
2835
2976
 
2836
2977
  /**
@@ -2839,7 +2980,9 @@ Writer.prototype.sint64 = function write_sint64(value) {
2839
2980
  * @returns {Writer} `this`
2840
2981
  */
2841
2982
  Writer.prototype.bool = function write_bool(value) {
2842
- return this._push(writeByte, 1, value ? 1 : 0);
2983
+ this._reserve(1);
2984
+ this.buf[this.pos++] = value ? 1 : 0;
2985
+ return this;
2843
2986
  };
2844
2987
 
2845
2988
  function writeFixed32(val, buf, pos) {
@@ -2855,7 +2998,10 @@ function writeFixed32(val, buf, pos) {
2855
2998
  * @returns {Writer} `this`
2856
2999
  */
2857
3000
  Writer.prototype.fixed32 = function write_fixed32(value) {
2858
- return this._push(writeFixed32, 4, value >>> 0);
3001
+ this._reserve(4);
3002
+ writeFixed32(value >>> 0, this.buf, this.pos);
3003
+ this.pos += 4;
3004
+ return this;
2859
3005
  };
2860
3006
 
2861
3007
  /**
@@ -2874,7 +3020,11 @@ Writer.prototype.sfixed32 = Writer.prototype.fixed32;
2874
3020
  */
2875
3021
  Writer.prototype.fixed64 = function write_fixed64(value) {
2876
3022
  var bits = LongBits.from(value);
2877
- return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
3023
+ this._reserve(8);
3024
+ writeFixed32(bits.lo, this.buf, this.pos);
3025
+ writeFixed32(bits.hi, this.buf, this.pos + 4);
3026
+ this.pos += 8;
3027
+ return this;
2878
3028
  };
2879
3029
 
2880
3030
  /**
@@ -2893,7 +3043,10 @@ Writer.prototype.sfixed64 = Writer.prototype.fixed64;
2893
3043
  * @returns {Writer} `this`
2894
3044
  */
2895
3045
  Writer.prototype.float = function write_float(value) {
2896
- return this._push(util.float.writeFloatLE, 4, value);
3046
+ this._reserve(4);
3047
+ util.float.writeFloatLE(value, this.buf, this.pos);
3048
+ this.pos += 4;
3049
+ return this;
2897
3050
  };
2898
3051
 
2899
3052
  /**
@@ -2903,19 +3056,12 @@ Writer.prototype.float = function write_float(value) {
2903
3056
  * @returns {Writer} `this`
2904
3057
  */
2905
3058
  Writer.prototype.double = function write_double(value) {
2906
- return this._push(util.float.writeDoubleLE, 8, value);
3059
+ this._reserve(8);
3060
+ util.float.writeDoubleLE(value, this.buf, this.pos);
3061
+ this.pos += 8;
3062
+ return this;
2907
3063
  };
2908
3064
 
2909
- var writeBytes = util.Array.prototype.set
2910
- ? function writeBytes_set(val, buf, pos) {
2911
- buf.set(val, pos); // also works for plain array values
2912
- }
2913
- /* istanbul ignore next */
2914
- : function writeBytes_for(val, buf, pos) {
2915
- for (var i = 0; i < val.length; ++i)
2916
- buf[pos + i] = val[i];
2917
- };
2918
-
2919
3065
  /**
2920
3066
  * Writes a sequence of bytes.
2921
3067
  * @param {Uint8Array|string} value Buffer or base64 encoded string to write
@@ -2923,14 +3069,21 @@ var writeBytes = util.Array.prototype.set
2923
3069
  */
2924
3070
  Writer.prototype.bytes = function write_bytes(value) {
2925
3071
  var len = value.length >>> 0;
2926
- if (!len)
2927
- return this._push(writeByte, 1, 0);
3072
+ if (!len) {
3073
+ this._reserve(1);
3074
+ this.buf[this.pos++] = 0;
3075
+ return this;
3076
+ }
2928
3077
  if (util.isString(value)) {
2929
3078
  var buf = Writer.alloc(len = base64.length(value));
2930
3079
  base64.decode(value, buf, 0);
2931
3080
  value = buf;
2932
3081
  }
2933
- return this.uint32(len)._push(writeBytes, len, value);
3082
+ this.uint32(len);
3083
+ this._reserve(len);
3084
+ this.buf.set(value, this.pos);
3085
+ this.pos += len;
3086
+ return this;
2934
3087
  };
2935
3088
 
2936
3089
  /**
@@ -2940,7 +3093,28 @@ Writer.prototype.bytes = function write_bytes(value) {
2940
3093
  */
2941
3094
  Writer.prototype.raw = function write_raw(value) {
2942
3095
  var len = value.length >>> 0;
2943
- return len ? this._push(writeBytes, len, value) : this;
3096
+ if (!len)
3097
+ return this;
3098
+ this._reserve(len);
3099
+ this.buf.set(value, this.pos);
3100
+ this.pos += len;
3101
+ return this;
3102
+ };
3103
+
3104
+ /**
3105
+ * Backfills the length varint.
3106
+ * @param {number} pos Position of reserved length byte
3107
+ * @param {number} len Length of content after length varint
3108
+ * @returns {Writer} `this`
3109
+ * @private
3110
+ */
3111
+ Writer.prototype._delim = function _delim(pos, len) {
3112
+ var n = sizeVarint32(len);
3113
+ if (n > 1)
3114
+ this.buf.copyWithin(pos + n, pos + 1, pos + 1 + len);
3115
+ writeVarint32(len, this.buf, pos);
3116
+ this.pos = pos + n + len;
3117
+ return this;
2944
3118
  };
2945
3119
 
2946
3120
  /**
@@ -2949,21 +3123,245 @@ Writer.prototype.raw = function write_raw(value) {
2949
3123
  * @returns {Writer} `this`
2950
3124
  */
2951
3125
  Writer.prototype.string = function write_string(value) {
3126
+ var n = value.length;
3127
+ if (!n) {
3128
+ this._reserve(1);
3129
+ this.buf[this.pos++] = 0;
3130
+ return this;
3131
+ }
3132
+ if (n < 0x80) {
3133
+ this._reserve(n * 3 + 5); // worst case
3134
+ var lenPos = this.pos;
3135
+ return this._delim(lenPos, utf8.write(value, this.buf, lenPos + 1));
3136
+ }
2952
3137
  var len = utf8.length(value);
2953
- return len
2954
- ? this.uint32(len)._push(len === value.length ? writeStringAscii : utf8.write, len, value)
2955
- : this._push(writeByte, 1, 0);
3138
+ this.uint32(len);
3139
+ this._reserve(len);
3140
+ if (len === value.length)
3141
+ writeStringAscii(value, this.buf, this.pos);
3142
+ else
3143
+ utf8.write(value, this.buf, this.pos);
3144
+ this.pos += len;
3145
+ return this;
3146
+ };
3147
+
3148
+ /**
3149
+ * Writes an array of unsigned 32 bit values as a packed repeated field.
3150
+ * @param {number[]} value Values to write
3151
+ * @returns {Writer} `this`
3152
+ */
3153
+ Writer.prototype.uint32s = function write_uint32s(value) {
3154
+ var n = value.length;
3155
+ this._reserve(n * 5 + 5); // worst case: 5 bytes per value + length varint
3156
+ var buf = this.buf, lenPos = this.pos, p = lenPos + 1;
3157
+ for (var i = 0; i < n; ++i)
3158
+ p = writeVarint32(value[i] >>> 0, buf, p);
3159
+ return this._delim(lenPos, p - lenPos - 1);
3160
+ };
3161
+
3162
+ /**
3163
+ * Writes an array of signed 32 bit values as a packed repeated field.
3164
+ * @param {number[]} value Values to write
3165
+ * @returns {Writer} `this`
3166
+ */
3167
+ Writer.prototype.int32s = function write_int32s(value) {
3168
+ var n = value.length;
3169
+ this._reserve(n * 10 + 5); // worst case: 10 bytes per negative value + length varint
3170
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1, val;
3171
+ for (var i = 0; i < n; ++i) {
3172
+ if ((val = value[i] | 0) < 0) { // negatives are 10 bytes per spec
3173
+ pos = writeVarint64(LongBits.fromNumber(val), buf, pos);
3174
+ } else {
3175
+ pos = writeVarint32(val, buf, pos);
3176
+ }
3177
+ }
3178
+ return this._delim(lenPos, pos - lenPos - 1);
3179
+ };
3180
+
3181
+ /**
3182
+ * Writes an array of 32 bit values as packed, zig-zag encoded varints.
3183
+ * @param {number[]} value Values to write
3184
+ * @returns {Writer} `this`
3185
+ */
3186
+ Writer.prototype.sint32s = function write_sint32s(value) {
3187
+ var n = value.length;
3188
+ this._reserve(n * 5 + 5);
3189
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
3190
+ for (var i = 0; i < n; ++i)
3191
+ pos = writeVarint32((value[i] << 1 ^ value[i] >> 31) >>> 0, buf, pos);
3192
+ return this._delim(lenPos, pos - lenPos - 1);
3193
+ };
3194
+
3195
+ /**
3196
+ * Writes an array of unsigned 64 bit values as a packed repeated field.
3197
+ * @param {Array.<Long|number|string>} value Values to write
3198
+ * @returns {Writer} `this`
3199
+ */
3200
+ Writer.prototype.uint64s = function write_uint64s(value) {
3201
+ var n = value.length;
3202
+ this._reserve(n * 10 + 5);
3203
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
3204
+ for (var i = 0; i < n; ++i) {
3205
+ pos = writeVarint64(LongBits.from(value[i]), buf, pos);
3206
+ }
3207
+ return this._delim(lenPos, pos - lenPos - 1);
3208
+ };
3209
+
3210
+ /**
3211
+ * Writes an array of signed 64 bit values as a packed repeated field.
3212
+ * @function
3213
+ * @param {Array.<Long|number|string>} value Values to write
3214
+ * @returns {Writer} `this`
3215
+ */
3216
+ Writer.prototype.int64s = Writer.prototype.uint64s;
3217
+
3218
+ /**
3219
+ * Writes an array of 64 bit values as packed, zig-zag encoded varints.
3220
+ * @param {Array.<Long|number|string>} value Values to write
3221
+ * @returns {Writer} `this`
3222
+ */
3223
+ Writer.prototype.sint64s = function write_sint64s(value) {
3224
+ var n = value.length;
3225
+ this._reserve(n * 10 + 5);
3226
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
3227
+ for (var i = 0; i < n; ++i) {
3228
+ pos = writeVarint64(LongBits.from(value[i]).zzEncode(), buf, pos);
3229
+ }
3230
+ return this._delim(lenPos, pos - lenPos - 1);
3231
+ };
3232
+
3233
+ /**
3234
+ * Writes an array of boolish values as a packed repeated field.
3235
+ * @param {boolean[]} value Values to write
3236
+ * @returns {Writer} `this`
3237
+ */
3238
+ Writer.prototype.bools = function write_bools(value) {
3239
+ var n = value.length;
3240
+ this.uint32(n); // one byte per value
3241
+ this._reserve(n);
3242
+ var buf = this.buf, p = this.pos;
3243
+ for (var i = 0; i < n; ++i)
3244
+ buf[p++] = value[i] ? 1 : 0;
3245
+ this.pos += n;
3246
+ return this;
3247
+ };
3248
+
3249
+ // The view allocation only pays off when amortized over enough writes
3250
+ var VIEW_THRESHOLD_FLOAT = 16,
3251
+ VIEW_THRESHOLD_INT = 128;
3252
+
3253
+ function getLazyView(writer, count, threshold) {
3254
+ var view = writer.view;
3255
+ if (view || count < threshold)
3256
+ return view;
3257
+ var buf = writer.buf;
3258
+ return writer.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
3259
+ }
3260
+
3261
+ /**
3262
+ * Writes an array of unsigned 32 bit values as packed, fixed 32 bits.
3263
+ * @param {number[]} value Values to write
3264
+ * @returns {Writer} `this`
3265
+ */
3266
+ Writer.prototype.fixed32s = function write_fixed32s(value) {
3267
+ var n = value.length, bytes = n * 4;
3268
+ this.uint32(bytes); // length is known exactly
3269
+ this._reserve(bytes);
3270
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_INT);
3271
+ if (dv)
3272
+ for (i = 0; i < n; ++i) { dv.setUint32(p, value[i] >>> 0, true); p += 4; }
3273
+ else {
3274
+ var buf = this.buf;
3275
+ for (i = 0; i < n; ++i) { writeFixed32(value[i] >>> 0, buf, p); p += 4; }
3276
+ }
3277
+ this.pos += bytes;
3278
+ return this;
3279
+ };
3280
+
3281
+ /**
3282
+ * Writes an array of signed 32 bit values as packed, fixed 32 bits.
3283
+ * @function
3284
+ * @param {number[]} value Values to write
3285
+ * @returns {Writer} `this`
3286
+ */
3287
+ Writer.prototype.sfixed32s = Writer.prototype.fixed32s;
3288
+
3289
+ /**
3290
+ * Writes an array of unsigned 64 bit values as packed, fixed 64 bits.
3291
+ * @param {Array.<Long|number|string>} value Values to write
3292
+ * @returns {Writer} `this`
3293
+ */
3294
+ Writer.prototype.fixed64s = function write_fixed64s(value) {
3295
+ var n = value.length, bytes = n * 8;
3296
+ this.uint32(bytes);
3297
+ this._reserve(bytes);
3298
+ var p = this.pos, i, bits, dv = getLazyView(this, n, VIEW_THRESHOLD_INT);
3299
+ if (dv)
3300
+ for (i = 0; i < n; ++i) { bits = LongBits.from(value[i]); dv.setUint32(p, bits.lo, true); dv.setUint32(p + 4, bits.hi, true); p += 8; }
3301
+ else {
3302
+ var buf = this.buf;
3303
+ for (i = 0; i < n; ++i) { bits = LongBits.from(value[i]); writeFixed32(bits.lo, buf, p); writeFixed32(bits.hi, buf, p + 4); p += 8; }
3304
+ }
3305
+ this.pos += bytes;
3306
+ return this;
3307
+ };
3308
+
3309
+ /**
3310
+ * Writes an array of signed 64 bit values as packed, fixed 64 bits.
3311
+ * @function
3312
+ * @param {Array.<Long|number|string>} value Values to write
3313
+ * @returns {Writer} `this`
3314
+ */
3315
+ Writer.prototype.sfixed64s = Writer.prototype.fixed64s;
3316
+
3317
+ /**
3318
+ * Writes an array of floats (32 bit) as a packed repeated field.
3319
+ * @param {number[]} value Values to write
3320
+ * @returns {Writer} `this`
3321
+ */
3322
+ Writer.prototype.floats = function write_floats(value) {
3323
+ var n = value.length, bytes = n * 4;
3324
+ this.uint32(bytes);
3325
+ this._reserve(bytes);
3326
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_FLOAT);
3327
+ if (dv)
3328
+ for (i = 0; i < n; ++i) { dv.setFloat32(p, value[i], true); p += 4; }
3329
+ else {
3330
+ var buf = this.buf;
3331
+ for (i = 0; i < n; ++i) { util.float.writeFloatLE(value[i], buf, p); p += 4; }
3332
+ }
3333
+ this.pos += bytes;
3334
+ return this;
3335
+ };
3336
+
3337
+ /**
3338
+ * Writes an array of doubles (64 bit float) as a packed repeated field.
3339
+ * @param {number[]} value Values to write
3340
+ * @returns {Writer} `this`
3341
+ */
3342
+ Writer.prototype.doubles = function write_doubles(value) {
3343
+ var n = value.length, bytes = n * 8;
3344
+ this.uint32(bytes);
3345
+ this._reserve(bytes);
3346
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_FLOAT);
3347
+ if (dv)
3348
+ for (i = 0; i < n; ++i) { dv.setFloat64(p, value[i], true); p += 8; }
3349
+ else {
3350
+ var buf = this.buf;
3351
+ for (i = 0; i < n; ++i) { util.float.writeDoubleLE(value[i], buf, p); p += 8; }
3352
+ }
3353
+ this.pos += bytes;
3354
+ return this;
2956
3355
  };
2957
3356
 
2958
3357
  /**
2959
3358
  * Forks this writer's state by pushing it to a stack.
2960
- * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
2961
3359
  * @returns {Writer} `this`
2962
3360
  */
2963
3361
  Writer.prototype.fork = function fork() {
2964
- this.states = new State(this);
2965
- this.head = this.tail = new Op(noop, 0, 0);
2966
- this.len = 0;
3362
+ this._reserve(1);
3363
+ (this.states || (this.states = [])).push(this.pos);
3364
+ this.pos += 1;
2967
3365
  return this;
2968
3366
  };
2969
3367
 
@@ -2972,47 +3370,66 @@ Writer.prototype.fork = function fork() {
2972
3370
  * @returns {Writer} `this`
2973
3371
  */
2974
3372
  Writer.prototype.reset = function reset() {
2975
- if (this.states) {
2976
- this.head = this.states.head;
2977
- this.tail = this.states.tail;
2978
- this.len = this.states.len;
2979
- this.states = this.states.next;
3373
+ var states = this.states;
3374
+ if (states && states.length) {
3375
+ this.pos = states.pop();
2980
3376
  } else {
2981
- this.head = this.tail = new Op(noop, 0, 0);
2982
- this.len = 0;
3377
+ this.pos = 0;
2983
3378
  }
2984
3379
  return this;
2985
3380
  };
2986
3381
 
2987
3382
  /**
2988
- * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
3383
+ * Resets to the last state and prepends the fork state's current write length as a varint.
2989
3384
  * @returns {Writer} `this`
2990
3385
  */
2991
3386
  Writer.prototype.ldelim = function ldelim() {
2992
- var head = this.head,
2993
- tail = this.tail,
2994
- len = this.len;
2995
- this.reset().uint32(len);
2996
- if (len) {
2997
- this.tail.next = head.next; // skip noop
2998
- this.tail = tail;
2999
- this.len += len;
3387
+ var states = this.states,
3388
+ len,
3389
+ vlen;
3390
+ if (states && states.length) {
3391
+ var lenPos = states.pop();
3392
+ len = this.pos - lenPos - 1;
3393
+ vlen = sizeVarint32(len);
3394
+ if (vlen > 1) { // grow the reserved single byte and shift content forward
3395
+ this._reserve(vlen - 1);
3396
+ this.buf.copyWithin(lenPos + vlen, lenPos + 1, lenPos + 1 + len);
3397
+ this.pos += vlen - 1;
3398
+ writeVarint32(len, this.buf, lenPos);
3399
+ } else {
3400
+ this.buf[lenPos] = len;
3401
+ }
3402
+ } else { // not forked: prefix the entire buffer with its length
3403
+ // TODO: Compatibility with older generated code.
3404
+ // Remove this branch in the next major release.
3405
+ len = this.pos;
3406
+ vlen = sizeVarint32(len);
3407
+ this._reserve(vlen);
3408
+ this.buf.copyWithin(vlen, 0, len);
3409
+ writeVarint32(len, this.buf, 0);
3410
+ this.pos += vlen;
3000
3411
  }
3001
3412
  return this;
3002
3413
  };
3003
3414
 
3004
3415
  /**
3005
3416
  * Finishes the write operation.
3417
+ * Returns a buffer sized to the written data by default.
3418
+ * @param {boolean} [shared=false] Whether to return a shared view instead of a unique copy
3006
3419
  * @returns {Uint8Array} Finished buffer
3007
3420
  */
3008
- Writer.prototype.finish = function finish() {
3009
- return this.finishInto(this.constructor.alloc(this.len), 0);
3421
+ Writer.prototype.finish = function finish(shared) {
3422
+ if (shared)
3423
+ return this.buf.subarray(0, this.pos);
3424
+ var buf = this.constructor.alloc(this.pos);
3425
+ buf.set(this.buf.subarray(0, this.pos), 0);
3426
+ return buf;
3010
3427
  };
3011
3428
 
3012
3429
  /**
3013
3430
  * Finishes the write operation, writing into the provided buffer.
3014
3431
  * The caller must ensure that `buf` has enough space starting at `offset`
3015
- * to hold {@link Writer#len} bytes.
3432
+ * to hold {@link Writer#pos} bytes.
3016
3433
  * @param {T} buf Target buffer
3017
3434
  * @param {number} [offset=0] Offset to start writing at
3018
3435
  * @returns {T} The provided buffer
@@ -3021,13 +3438,7 @@ Writer.prototype.finish = function finish() {
3021
3438
  Writer.prototype.finishInto = function finishInto(buf, offset) {
3022
3439
  if (offset === undefined)
3023
3440
  offset = 0;
3024
- var head = this.head.next,
3025
- pos = offset;
3026
- while (head) {
3027
- head.fn(head.val, buf, pos);
3028
- pos += head.len;
3029
- head = head.next;
3030
- }
3441
+ buf.set(this.buf.subarray(0, this.pos), offset);
3031
3442
  return buf;
3032
3443
  };
3033
3444
 
@@ -3064,6 +3475,8 @@ function BufferWriter() {
3064
3475
  Writer.call(this);
3065
3476
  }
3066
3477
 
3478
+ var writeStringBuffer;
3479
+
3067
3480
  BufferWriter._configure = function () {
3068
3481
  /**
3069
3482
  * Allocates a buffer of the specified size.
@@ -3071,19 +3484,14 @@ BufferWriter._configure = function () {
3071
3484
  * @param {number} size Buffer size
3072
3485
  * @returns {Buffer} Buffer
3073
3486
  */
3074
- BufferWriter.alloc = util._Buffer_allocUnsafe;
3487
+ BufferWriter.alloc = util.Buffer && util.Buffer.allocUnsafe;
3075
3488
 
3076
- BufferWriter.writeBytesBuffer = util.Buffer && util.Buffer.prototype instanceof Uint8Array && util.Buffer.prototype.set.name === "set"
3077
- ? function writeBytesBuffer_set(val, buf, pos) {
3078
- buf.set(val, pos); // faster than copy (requires node >= 4 where Buffers extend Uint8Array and set is properly inherited)
3079
- // also works for plain array values
3489
+ writeStringBuffer = util.Buffer && util.Buffer.prototype.utf8Write
3490
+ ? function writeStringBuffer_utf8Write(val, buf, pos) {
3491
+ return buf.utf8Write(val, pos);
3080
3492
  }
3081
- /* istanbul ignore next */
3082
- : function writeBytesBuffer_copy(val, buf, pos) {
3083
- if (val.copy) // Buffer values
3084
- val.copy(buf, pos, 0, val.length);
3085
- else for (var i = 0; i < val.length;) // plain array values
3086
- buf[pos++] = val[i++];
3493
+ : function writeStringBuffer_write(val, buf, pos) {
3494
+ return buf.write(val, pos);
3087
3495
  };
3088
3496
  };
3089
3497
 
@@ -3093,48 +3501,42 @@ BufferWriter._configure = function () {
3093
3501
  */
3094
3502
  BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
3095
3503
  if (util.isString(value))
3096
- value = util._Buffer_from(value, "base64");
3504
+ value = util.Buffer.from(value, "base64");
3097
3505
  var len = value.length >>> 0;
3098
3506
  this.uint32(len);
3099
- if (len)
3100
- this._push(BufferWriter.writeBytesBuffer, len, value);
3507
+ if (len) {
3508
+ this._reserve(len);
3509
+ this.buf.set(value, this.pos);
3510
+ this.pos += len;
3511
+ }
3101
3512
  return this;
3102
3513
  };
3103
3514
 
3104
- /**
3105
- * Writes raw bytes without a tag or length prefix.
3106
- * @name BufferWriter#raw
3107
- * @function
3108
- * @param {Uint8Array} value Raw bytes
3109
- * @returns {BufferWriter} `this`
3110
- */
3111
- BufferWriter.prototype.raw = function write_raw_buffer(value) {
3112
- var len = value.length >>> 0;
3113
- return len ? this._push(BufferWriter.writeBytesBuffer, len, value) : this;
3114
- };
3115
-
3116
- function writeStringBufferAscii(val, buf, pos) {
3117
- for (var i = 0; i < val.length;)
3118
- buf[pos++] = val.charCodeAt(i++);
3119
- }
3120
-
3121
- function writeStringBuffer(val, buf, pos) {
3122
- if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
3123
- util.utf8.write(val, buf, pos);
3124
- else if (buf.utf8Write)
3125
- buf.utf8Write(val, pos);
3126
- else
3127
- buf.write(val, pos);
3128
- }
3129
-
3130
3515
  /**
3131
3516
  * @override
3132
3517
  */
3133
3518
  BufferWriter.prototype.string = function write_string_buffer(value) {
3519
+ var n = value.length;
3520
+ if (!n) {
3521
+ this._reserve(1);
3522
+ this.buf[this.pos++] = 0;
3523
+ return this;
3524
+ }
3525
+ if (n < 0x80) {
3526
+ this._reserve(n * 3 + 5); // worst case
3527
+ var pos = this.pos,
3528
+ buf = this.buf;
3529
+ return this._delim(pos,
3530
+ n < 40
3531
+ ? util.utf8.write(value, buf, pos + 1)
3532
+ : writeStringBuffer(value, buf, pos + 1)
3533
+ );
3534
+ }
3134
3535
  var len = util.Buffer.byteLength(value);
3135
3536
  this.uint32(len);
3136
- if (len)
3137
- this._push(len === value.length && len < 40 ? writeStringBufferAscii : writeStringBuffer, len, value);
3537
+ this._reserve(len);
3538
+ writeStringBuffer(value, this.buf, this.pos);
3539
+ this.pos += len;
3138
3540
  return this;
3139
3541
  };
3140
3542