protobufjs 8.6.5 → 8.7.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.
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * protobuf.js v8.6.5 (c) 2016, daniel wirtz
3
- * compiled tue, 23 jun 2026 14:36:35 utc
2
+ * protobuf.js v8.7.0 (c) 2016, daniel wirtz
3
+ * compiled mon, 06 jul 2026 13:50:00 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -537,13 +537,9 @@ function decoder(mtype) {
537
537
  if (types.packed[type] !== undefined) {
538
538
  gen
539
539
  ("if(u===2){");
540
- if (!closed) gen
541
- ("if(!(%s&&%s.length))", ref, ref)
542
- ("%s=[]", ref);
543
- gen
544
- ("var c2=r.uint32()+r.pos");
545
540
  if (closed) {
546
541
  gen
542
+ ("var c2=r.uint32()+r.pos")
547
543
  ("while(r.pos<c2){")
548
544
  ("s=r.pos")
549
545
  ("v=r.%s()", type)
@@ -555,8 +551,9 @@ function decoder(mtype) {
555
551
  genPreserveUnknown(gen, "util.rawField(" + field.id + ",0,r.raw(s,r.pos))")
556
552
  ("}");
557
553
  } else gen
558
- ("while(r.pos<c2)")
559
- ("%s.push(r.%s())", ref, type);
554
+ ("if(!(%s&&%s.length))", ref, ref)
555
+ ("%s=[]", ref)
556
+ ("r.%ss(%s)", type, ref);
560
557
  gen
561
558
  ("continue")
562
559
  ("}");
@@ -752,10 +749,7 @@ function encoder(mtype) {
752
749
  // Packed repeated
753
750
  if (field.packed && types.packed[type] !== undefined) { gen
754
751
 
755
- ("w.uint32(%i).fork()", (field.id << 3 | 2) >>> 0)
756
- ("for(var i=0;i<%s.length;++i)", ref)
757
- ("w.%s(%s[i])", type, ref)
758
- ("w.ldelim()");
752
+ ("w.uint32(%i).%ss(%s)", (field.id << 3 | 2) >>> 0, type, ref);
759
753
 
760
754
  // Non-packed
761
755
  } else { gen
@@ -1688,7 +1682,6 @@ exports.configure = configure;
1688
1682
  * @returns {undefined}
1689
1683
  */
1690
1684
  function configure() {
1691
- exports.util._configure();
1692
1685
  exports.Writer._configure(exports.BufferWriter);
1693
1686
  exports.Reader._configure(exports.BufferReader);
1694
1687
  }
@@ -3389,7 +3382,7 @@ function indexOutOfRange(reader, writeLength) {
3389
3382
 
3390
3383
  /**
3391
3384
  * Constructs a new reader instance using the specified buffer.
3392
- * @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
3385
+ * @classdesc Wire format reader using `Uint8Array`.
3393
3386
  * @constructor
3394
3387
  * @param {Uint8Array} buffer Buffer to read from
3395
3388
  */
@@ -3413,6 +3406,12 @@ function Reader(buffer) {
3413
3406
  */
3414
3407
  this.len = buffer.length;
3415
3408
 
3409
+ /**
3410
+ * Cached DataView for packed reads.
3411
+ * @type {DataView|null}
3412
+ */
3413
+ this.view = null;
3414
+
3416
3415
  /**
3417
3416
  * Whether to discard unknown fields while decoding.
3418
3417
  * @type {boolean}
@@ -3420,18 +3419,14 @@ function Reader(buffer) {
3420
3419
  this.discardUnknown = Reader.discardUnknown;
3421
3420
  }
3422
3421
 
3423
- var create_array = typeof Uint8Array !== "undefined"
3424
- ? function create_typed_array(buffer) {
3425
- if (buffer instanceof Uint8Array || Array.isArray(buffer))
3426
- return new Reader(buffer);
3427
- throw Error("illegal buffer");
3428
- }
3429
- /* istanbul ignore next */
3430
- : function create_array(buffer) {
3431
- if (Array.isArray(buffer))
3432
- return new Reader(buffer);
3433
- throw Error("illegal buffer");
3434
- };
3422
+ function create_array(buffer) {
3423
+ // TODO: Remove plain array reader support in the next major release.
3424
+ if (Array.isArray(buffer))
3425
+ buffer = new Uint8Array(buffer);
3426
+ if (buffer instanceof Uint8Array)
3427
+ return new Reader(buffer);
3428
+ throw Error("illegal buffer");
3429
+ }
3435
3430
 
3436
3431
  var create = function create() {
3437
3432
  return util.Buffer
@@ -3456,8 +3451,6 @@ var create = function create() {
3456
3451
  */
3457
3452
  Reader.create = create();
3458
3453
 
3459
- Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore next */ util.Array.prototype.slice;
3460
-
3461
3454
  /**
3462
3455
  * Returns raw bytes from the backing buffer without advancing the reader.
3463
3456
  * @param {number} start Start offset
@@ -3465,12 +3458,7 @@ Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore ne
3465
3458
  * @returns {Uint8Array} Raw bytes
3466
3459
  */
3467
3460
  Reader.prototype.raw = function read_raw(start, end) {
3468
- if (Array.isArray(this.buf)) // plain array
3469
- return this.buf.slice(start, end);
3470
-
3471
- if (start === end) // fix for IE 10/Win8 and others' subarray returning array of size 1
3472
- return new this.buf.constructor(0);
3473
- return this._slice.call(this.buf, start, end);
3461
+ return this.buf.subarray(start, end);
3474
3462
  };
3475
3463
 
3476
3464
  /**
@@ -3767,6 +3755,241 @@ Reader.prototype.double = function read_double() {
3767
3755
  return value;
3768
3756
  };
3769
3757
 
3758
+ /**
3759
+ * Reads a packed repeated field of unsigned 32 bit varints.
3760
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
3761
+ * @returns {number[]} Array read into
3762
+ */
3763
+ Reader.prototype.uint32s = function read_uint32s(array) {
3764
+ if (array === undefined) array = [];
3765
+ var end = this.uint32() + this.pos;
3766
+ while (this.pos < end)
3767
+ array.push(this.uint32());
3768
+ return array;
3769
+ };
3770
+
3771
+ /**
3772
+ * Reads a packed repeated field of signed 32 bit varints.
3773
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
3774
+ * @returns {number[]} Array read into
3775
+ */
3776
+ Reader.prototype.int32s = function read_int32s(array) {
3777
+ if (array === undefined) array = [];
3778
+ var end = this.uint32() + this.pos;
3779
+ while (this.pos < end)
3780
+ array.push(this.int32());
3781
+ return array;
3782
+ };
3783
+
3784
+ /**
3785
+ * Reads a packed repeated field of zig-zag encoded signed 32 bit varints.
3786
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
3787
+ * @returns {number[]} Array read into
3788
+ */
3789
+ Reader.prototype.sint32s = function read_sint32s(array) {
3790
+ if (array === undefined) array = [];
3791
+ var end = this.uint32() + this.pos;
3792
+ while (this.pos < end)
3793
+ array.push(this.sint32());
3794
+ return array;
3795
+ };
3796
+
3797
+ /**
3798
+ * Reads a packed repeated field of booleans.
3799
+ * @param {boolean[]} [array] Array to read into; a new one is created if omitted
3800
+ * @returns {boolean[]} Array read into
3801
+ */
3802
+ Reader.prototype.bools = function read_bools(array) {
3803
+ if (array === undefined) array = [];
3804
+ var end = this.uint32() + this.pos;
3805
+ while (this.pos < end)
3806
+ array.push(this.bool());
3807
+ return array;
3808
+ };
3809
+
3810
+ // The view allocation only pays off when amortized over enough reads
3811
+ var VIEW_THRESHOLD_FLOAT = 8,
3812
+ VIEW_THRESHOLD_INT = 128;
3813
+
3814
+ function getLazyView(reader, count, threshold) {
3815
+ var view = reader.view;
3816
+ if (view || count < threshold)
3817
+ return view;
3818
+ var buf = reader.buf;
3819
+ return reader.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
3820
+ }
3821
+
3822
+ /**
3823
+ * Reads a packed repeated field of unsigned 32 bit fixed values.
3824
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
3825
+ * @returns {number[]} Array read into
3826
+ */
3827
+ Reader.prototype.fixed32s = function read_fixed32s(array) {
3828
+ if (array === undefined) array = [];
3829
+ var len = this.uint32(), end = this.pos + len;
3830
+ /* istanbul ignore if */
3831
+ if (end > this.len) throw indexOutOfRange(this, len);
3832
+ var count = len >>> 2, i = array.length, pos = this.pos;
3833
+ array.length = i + count;
3834
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
3835
+ if (dv)
3836
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getUint32(pos, true);
3837
+ else {
3838
+ var buf = this.buf;
3839
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4);
3840
+ }
3841
+ this.pos = pos;
3842
+ if (pos !== end) throw indexOutOfRange(this, 4);
3843
+ return array;
3844
+ };
3845
+
3846
+ /**
3847
+ * Reads a packed repeated field of signed 32 bit fixed values.
3848
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
3849
+ * @returns {number[]} Array read into
3850
+ */
3851
+ Reader.prototype.sfixed32s = function read_sfixed32s(array) {
3852
+ if (array === undefined) array = [];
3853
+ var len = this.uint32(), end = this.pos + len;
3854
+ /* istanbul ignore if */
3855
+ if (end > this.len) throw indexOutOfRange(this, len);
3856
+ var count = len >>> 2, i = array.length, pos = this.pos;
3857
+ array.length = i + count;
3858
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
3859
+ if (dv)
3860
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getInt32(pos, true);
3861
+ else {
3862
+ var buf = this.buf;
3863
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4) | 0;
3864
+ }
3865
+ this.pos = pos;
3866
+ if (pos !== end) throw indexOutOfRange(this, 4);
3867
+ return array;
3868
+ };
3869
+
3870
+ /**
3871
+ * Reads a packed repeated field of floats (32 bit).
3872
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
3873
+ * @returns {number[]} Array read into
3874
+ */
3875
+ Reader.prototype.floats = function read_floats(array) {
3876
+ if (array === undefined) array = [];
3877
+ var len = this.uint32(), end = this.pos + len;
3878
+ /* istanbul ignore if */
3879
+ if (end > this.len) throw indexOutOfRange(this, len);
3880
+ var count = len >>> 2, i = array.length, pos = this.pos;
3881
+ array.length = i + count;
3882
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
3883
+ if (dv)
3884
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getFloat32(pos, true);
3885
+ else {
3886
+ var buf = this.buf;
3887
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = util.float.readFloatLE(buf, pos);
3888
+ }
3889
+ this.pos = pos;
3890
+ if (pos !== end) throw indexOutOfRange(this, 4);
3891
+ return array;
3892
+ };
3893
+
3894
+ /**
3895
+ * Reads a packed repeated field of doubles (64 bit float).
3896
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
3897
+ * @returns {number[]} Array read into
3898
+ */
3899
+ Reader.prototype.doubles = function read_doubles(array) {
3900
+ if (array === undefined) array = [];
3901
+ var len = this.uint32(), end = this.pos + len;
3902
+ /* istanbul ignore if */
3903
+ if (end > this.len) throw indexOutOfRange(this, len);
3904
+ var count = len >>> 3, i = array.length, pos = this.pos;
3905
+ array.length = i + count;
3906
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
3907
+ if (dv)
3908
+ for (var k = 0; k < count; ++k, pos += 8) array[i++] = dv.getFloat64(pos, true);
3909
+ else {
3910
+ var buf = this.buf;
3911
+ for (var j = 0; j < count; ++j, pos += 8) array[i++] = util.float.readDoubleLE(buf, pos);
3912
+ }
3913
+ this.pos = pos;
3914
+ if (pos !== end) throw indexOutOfRange(this, 8);
3915
+ return array;
3916
+ };
3917
+
3918
+ /**
3919
+ * Reads a packed repeated field of unsigned 64 bit varints.
3920
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
3921
+ * @returns {Array.<Long|number>} Array read into
3922
+ */
3923
+ Reader.prototype.uint64s = function read_uint64s(array) {
3924
+ if (array === undefined) array = [];
3925
+ var end = this.uint32() + this.pos;
3926
+ while (this.pos < end)
3927
+ array.push(this.uint64());
3928
+ return array;
3929
+ };
3930
+
3931
+ /**
3932
+ * Reads a packed repeated field of signed 64 bit varints.
3933
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
3934
+ * @returns {Array.<Long|number>} Array read into
3935
+ */
3936
+ Reader.prototype.int64s = function read_int64s(array) {
3937
+ if (array === undefined) array = [];
3938
+ var end = this.uint32() + this.pos;
3939
+ while (this.pos < end)
3940
+ array.push(this.int64());
3941
+ return array;
3942
+ };
3943
+
3944
+ /**
3945
+ * Reads a packed repeated field of zig-zag encoded signed 64 bit varints.
3946
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
3947
+ * @returns {Array.<Long|number>} Array read into
3948
+ */
3949
+ Reader.prototype.sint64s = function read_sint64s(array) {
3950
+ if (array === undefined) array = [];
3951
+ var end = this.uint32() + this.pos;
3952
+ while (this.pos < end)
3953
+ array.push(this.sint64());
3954
+ return array;
3955
+ };
3956
+
3957
+ /**
3958
+ * Reads a packed repeated field of unsigned 64 bit fixed values.
3959
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
3960
+ * @returns {Array.<Long|number>} Array read into
3961
+ */
3962
+ Reader.prototype.fixed64s = function read_fixed64s(array) {
3963
+ if (array === undefined) array = [];
3964
+ var len = this.uint32(), end = this.pos + len, i = array.length;
3965
+ /* istanbul ignore if */
3966
+ if (end > this.len) throw indexOutOfRange(this, len);
3967
+ var count = len >>> 3;
3968
+ array.length = i + count; // 8 bytes per value, count is known
3969
+ for (var j = 0; j < count; ++j)
3970
+ array[i++] = this.fixed64();
3971
+ if (this.pos !== end) throw indexOutOfRange(this, 8);
3972
+ return array;
3973
+ };
3974
+
3975
+ /**
3976
+ * Reads a packed repeated field of signed 64 bit fixed values.
3977
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
3978
+ * @returns {Array.<Long|number>} Array read into
3979
+ */
3980
+ Reader.prototype.sfixed64s = function read_sfixed64s(array) {
3981
+ if (array === undefined) array = [];
3982
+ var len = this.uint32(), end = this.pos + len, i = array.length;
3983
+ /* istanbul ignore if */
3984
+ if (end > this.len) throw indexOutOfRange(this, len);
3985
+ var count = len >>> 3;
3986
+ array.length = i + count; // 8 bytes per value, count is known
3987
+ for (var j = 0; j < count; ++j)
3988
+ array[i++] = this.sfixed64();
3989
+ if (this.pos !== end) throw indexOutOfRange(this, 8);
3990
+ return array;
3991
+ };
3992
+
3770
3993
  /**
3771
3994
  * Reads a sequence of bytes preceeded by its length as a varint.
3772
3995
  * @returns {Uint8Array} Value read
@@ -3962,7 +4185,7 @@ function BufferReader(buffer) {
3962
4185
  * Read buffer.
3963
4186
  * @name BufferReader#buf
3964
4187
  * @type {Buffer}
3965
- */
4188
+ */
3966
4189
  }
3967
4190
 
3968
4191
  BufferReader._configure = function () {
@@ -3980,8 +4203,6 @@ BufferReader._configure = function () {
3980
4203
  * @returns {Buffer} Raw bytes
3981
4204
  */
3982
4205
  BufferReader.prototype.raw = function read_raw_buffer(start, end) {
3983
- if (start === end)
3984
- return util.Buffer.alloc(0);
3985
4206
  return this._slice.call(this.buf, start, end);
3986
4207
  };
3987
4208
 
@@ -5358,6 +5579,14 @@ Type.prototype.setup = function setup() {
5358
5579
  // Sets up everything at once so that the prototype chain does not have to be re-evaluated
5359
5580
  // multiple times (V8, soft-deopt prototype-check).
5360
5581
 
5582
+ // Resolve feature defaults incl. field presence before generating codecs
5583
+ var root = this.root;
5584
+ if (root && root._needsRecursiveFeatureResolution) {
5585
+ var edition = root._edition || this._edition;
5586
+ if (edition)
5587
+ root._resolveFeaturesRecursive(edition);
5588
+ }
5589
+
5361
5590
  var fullName = this.fullName,
5362
5591
  types = [];
5363
5592
  for (var i = 0; i < /* initializes */ this.fieldsArray.length; ++i)
@@ -5421,7 +5650,7 @@ Type.prototype.encode = function encode_setup(message, writer) { // eslint-disab
5421
5650
  * @returns {Writer} writer
5422
5651
  */
5423
5652
  Type.prototype.encodeDelimited = function encodeDelimited(message, writer) {
5424
- return this.encode(message, writer && writer.len ? writer.fork() : writer).ldelim();
5653
+ return this.encode(message, (writer || Writer.create()).fork()).ldelim();
5425
5654
  };
5426
5655
 
5427
5656
  /**
@@ -7211,36 +7440,29 @@ util.isSet = function isSet(obj, prop) {
7211
7440
  util.Buffer = (function() {
7212
7441
  try {
7213
7442
  var Buffer = util.global.Buffer;
7214
- // refuse to use non-node buffers if not explicitly assigned (perf reasons):
7215
- return Buffer.prototype.utf8Write ? Buffer : /* istanbul ignore next */ null;
7443
+ // refuse to use non-node buffers if not explicitly assigned (perf reasons)
7444
+ return Buffer.prototype.utf8Write || util.isNode ? Buffer : /* istanbul ignore next */ null;
7216
7445
  } catch (e) {
7217
7446
  /* istanbul ignore next */
7218
7447
  return null;
7219
7448
  }
7220
7449
  })();
7221
7450
 
7222
- // Internal alias of or polyfull for Buffer.from.
7223
- util._Buffer_from = null;
7224
-
7225
- // Internal alias of or polyfill for Buffer.allocUnsafe.
7226
- util._Buffer_allocUnsafe = null;
7227
-
7228
7451
  /**
7229
7452
  * Creates a new buffer of whatever type supported by the environment.
7230
7453
  * @param {number|number[]} [sizeOrArray=0] Buffer size or number array
7231
7454
  * @returns {Uint8Array|Buffer} Buffer
7232
7455
  */
7233
7456
  util.newBuffer = function newBuffer(sizeOrArray) {
7457
+ var Buffer = util.Buffer;
7234
7458
  /* istanbul ignore next */
7235
7459
  return typeof sizeOrArray === "number"
7236
- ? util.Buffer
7237
- ? util._Buffer_allocUnsafe(sizeOrArray)
7238
- : new util.Array(sizeOrArray)
7239
- : util.Buffer
7240
- ? util._Buffer_from(sizeOrArray)
7241
- : typeof Uint8Array === "undefined"
7242
- ? sizeOrArray
7243
- : new Uint8Array(sizeOrArray);
7460
+ ? Buffer
7461
+ ? Buffer.allocUnsafe(sizeOrArray)
7462
+ : new Uint8Array(sizeOrArray)
7463
+ : Buffer
7464
+ ? Buffer.from(sizeOrArray)
7465
+ : new Uint8Array(sizeOrArray);
7244
7466
  };
7245
7467
 
7246
7468
  /**
@@ -7266,10 +7488,11 @@ util.rawField = function rawField(id, wireType, data) {
7266
7488
  };
7267
7489
 
7268
7490
  /**
7269
- * Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
7491
+ * Array implementation used in the browser.
7270
7492
  * @type {Constructor<Uint8Array>}
7493
+ * @deprecated Use `Uint8Array` instead.
7271
7494
  */
7272
- util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : Array;
7495
+ util.Array = Uint8Array;
7273
7496
 
7274
7497
  /**
7275
7498
  * Any compatible Long instance.
@@ -7590,28 +7813,6 @@ util.toJSONOptions = {
7590
7813
  json: true
7591
7814
  };
7592
7815
 
7593
- // Sets up buffer utility according to the environment (called in index-minimal)
7594
- util._configure = function() {
7595
- var Buffer = util.Buffer;
7596
- /* istanbul ignore if */
7597
- if (!Buffer) {
7598
- util._Buffer_from = util._Buffer_allocUnsafe = null;
7599
- return;
7600
- }
7601
- // because node 4.x buffers are incompatible & immutable
7602
- // see: https://github.com/dcodeIO/protobuf.js/pull/665
7603
- util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
7604
- /* istanbul ignore next */
7605
- function Buffer_from(value, encoding) {
7606
- return new Buffer(value, encoding);
7607
- };
7608
- util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
7609
- /* istanbul ignore next */
7610
- function Buffer_allocUnsafe(size) {
7611
- return new Buffer(size);
7612
- };
7613
- };
7614
-
7615
7816
  },{"25":25,"26":26,"28":28,"30":30,"32":32,"36":36,"37":37,"long":"long"}],34:[function(require,module,exports){
7616
7817
  "use strict";
7617
7818
 
@@ -7777,19 +7978,21 @@ function pool(alloc, slice, size) {
7777
7978
  "use strict";
7778
7979
 
7779
7980
  /**
7780
- * A minimal UTF8 implementation for number arrays.
7981
+ * A minimal UTF8 implementation.
7781
7982
  * @memberof util
7782
7983
  * @namespace
7783
7984
  */
7784
7985
  var utf8 = exports,
7785
7986
  replacementChar = "\ufffd",
7987
+ looseDecoder = new TextDecoder("utf-8", { ignoreBOM: true }),
7786
7988
  strictDecoder;
7989
+ var TEXT_DECODER_MIN_LENGTH = 64;
7787
7990
 
7788
7991
  try {
7789
7992
  strictDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
7790
7993
  } catch (err) {
7791
7994
  // "fatal" option is not supported on Node.js compiled without ICU
7792
- strictDecoder = new TextDecoder("utf-8", { ignoreBOM: true });
7995
+ strictDecoder = looseDecoder;
7793
7996
  }
7794
7997
 
7795
7998
  /**
@@ -7840,6 +8043,13 @@ function utf8_read_js(buffer, start, end, str) {
7840
8043
  return str;
7841
8044
  }
7842
8045
 
8046
+ function utf8_read_decoder(decoder, buffer, start, end) {
8047
+ var source = start === 0 && end === buffer.length
8048
+ ? buffer
8049
+ : buffer.subarray(start, end);
8050
+ return decoder.decode(source);
8051
+ }
8052
+
7843
8053
  /**
7844
8054
  * Reads UTF8 bytes as a string.
7845
8055
  * @param {Uint8Array} buffer Source buffer
@@ -7847,9 +8057,11 @@ function utf8_read_js(buffer, start, end, str) {
7847
8057
  * @param {number} end Source end
7848
8058
  * @returns {string} String read
7849
8059
  */
7850
- utf8.read = function utf8_read_ascii(buffer, start, end) {
8060
+ utf8.read = function utf8_read_loose(buffer, start, end) {
7851
8061
  if (end - start < 1)
7852
8062
  return "";
8063
+ if (end - start >= TEXT_DECODER_MIN_LENGTH)
8064
+ return utf8_read_decoder(looseDecoder, buffer, start, end);
7853
8065
 
7854
8066
  var str = "",
7855
8067
  i = start,
@@ -7879,17 +8091,6 @@ utf8.read = function utf8_read_ascii(buffer, start, end) {
7879
8091
  return str;
7880
8092
  };
7881
8093
 
7882
- function utf8_read_strict(buffer, start, end) {
7883
- var source = start === 0 && end === buffer.length
7884
- ? buffer
7885
- : buffer.subarray
7886
- ? buffer.subarray(start, end)
7887
- : buffer.slice(start, end);
7888
- if (Array.isArray(source))
7889
- source = Uint8Array.from(source);
7890
- return strictDecoder.decode(source);
7891
- }
7892
-
7893
8094
  /**
7894
8095
  * Reads UTF8 bytes as a string, rejecting invalid UTF8.
7895
8096
  * @param {Uint8Array} buffer Source buffer
@@ -7897,9 +8098,11 @@ function utf8_read_strict(buffer, start, end) {
7897
8098
  * @param {number} end Source end
7898
8099
  * @returns {string} String read
7899
8100
  */
7900
- utf8.readStrict = function utf8_read_strict_ascii(buffer, start, end) {
8101
+ utf8.readStrict = function utf8_read_strict(buffer, start, end) {
7901
8102
  if (end - start < 1)
7902
8103
  return "";
8104
+ if (end - start >= TEXT_DECODER_MIN_LENGTH)
8105
+ return utf8_read_decoder(strictDecoder, buffer, start, end);
7903
8106
 
7904
8107
  var str = "",
7905
8108
  i = start,
@@ -7915,14 +8118,14 @@ utf8.readStrict = function utf8_read_strict_ascii(buffer, start, end) {
7915
8118
  c7 = buffer[i + 6];
7916
8119
  c8 = buffer[i + 7];
7917
8120
  if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80)
7918
- return str + utf8_read_strict(buffer, i, end);
8121
+ return str + utf8_read_decoder(strictDecoder, buffer, i, end);
7919
8122
  str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
7920
8123
  }
7921
8124
 
7922
8125
  for (; i < end; ++i) {
7923
8126
  c1 = buffer[i];
7924
8127
  if (c1 & 0x80)
7925
- return str + utf8_read_strict(buffer, i, end);
8128
+ return str + utf8_read_decoder(strictDecoder, buffer, i, end);
7926
8129
  str += String.fromCharCode(c1);
7927
8130
  }
7928
8131
 
@@ -8198,7 +8401,7 @@ wrappers[".google.protobuf.Any"] = {
8198
8401
  if (object && object["@type"]) {
8199
8402
  // Only use fully qualified type name after the last '/'
8200
8403
  var name = object["@type"].substring(object["@type"].lastIndexOf("/") + 1);
8201
- var type = this.lookup(name);
8404
+ var type = this.lookup(name, [ this.constructor ]);
8202
8405
  /* istanbul ignore else */
8203
8406
  if (type) {
8204
8407
  // type_url does not accept leading "."
@@ -8234,7 +8437,7 @@ wrappers[".google.protobuf.Any"] = {
8234
8437
  name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
8235
8438
  // Separate the prefix used
8236
8439
  prefix = message.type_url.substring(0, message.type_url.lastIndexOf("/") + 1);
8237
- var type = this.lookup(name);
8440
+ var type = this.lookup(name, [ this.constructor ]);
8238
8441
  /* istanbul ignore else */
8239
8442
  if (type)
8240
8443
  message = type.decode(message.value, undefined, undefined, depth + 1);
@@ -8270,118 +8473,52 @@ var LongBits = util.LongBits,
8270
8473
  base64 = util.base64,
8271
8474
  utf8 = util.utf8;
8272
8475
 
8273
- /**
8274
- * Constructs a new writer operation instance.
8275
- * @classdesc Scheduled writer operation.
8276
- * @constructor
8277
- * @param {function(*, Uint8Array, number)} fn Function to call
8278
- * @param {number} len Value byte length
8279
- * @param {*} val Value to write
8280
- * @ignore
8281
- */
8282
- function Op(fn, len, val) {
8283
-
8284
- /**
8285
- * Function to call.
8286
- * @type {function(Uint8Array, number, *)}
8287
- */
8288
- this.fn = fn;
8289
-
8290
- /**
8291
- * Value byte length.
8292
- * @type {number}
8293
- */
8294
- this.len = len;
8295
-
8296
- /**
8297
- * Next operation.
8298
- * @type {Writer.Op|undefined}
8299
- */
8300
- this.next = undefined;
8301
-
8302
- /**
8303
- * Value to write.
8304
- * @type {*}
8305
- */
8306
- this.val = val; // type varies
8307
- }
8308
-
8309
- /* istanbul ignore next */
8310
- function noop() {} // eslint-disable-line no-empty-function
8311
-
8312
- /**
8313
- * Constructs a new writer state instance.
8314
- * @classdesc Copied writer state.
8315
- * @memberof Writer
8316
- * @constructor
8317
- * @param {Writer} writer Writer to copy state from
8318
- * @ignore
8319
- */
8320
- function State(writer) {
8321
-
8322
- /**
8323
- * Current head.
8324
- * @type {Writer.Op}
8325
- */
8326
- this.head = writer.head;
8327
-
8328
- /**
8329
- * Current tail.
8330
- * @type {Writer.Op}
8331
- */
8332
- this.tail = writer.tail;
8333
-
8334
- /**
8335
- * Current buffer length.
8336
- * @type {number}
8337
- */
8338
- this.len = writer.len;
8339
-
8340
- /**
8341
- * Next state.
8342
- * @type {State|null}
8343
- */
8344
- this.next = writer.states;
8345
- }
8346
-
8347
8476
  /**
8348
8477
  * Constructs a new writer instance.
8349
- * @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
8478
+ * @classdesc Wire format writer using `Uint8Array`.
8350
8479
  * @constructor
8351
8480
  */
8352
8481
  function Writer() {
8353
8482
 
8354
8483
  /**
8355
- * Current length.
8484
+ * Write cursor into {@link Writer#buf}.
8356
8485
  * @type {number}
8357
8486
  */
8358
- this.len = 0;
8487
+ this.pos = 0;
8359
8488
 
8360
8489
  /**
8361
- * Operations head.
8362
- * @type {Object}
8490
+ * Backing buffer.
8491
+ * @type {Uint8Array}
8363
8492
  */
8364
- this.head = new Op(noop, 0, 0);
8493
+ this.buf = this.constructor.alloc(64);
8365
8494
 
8366
8495
  /**
8367
- * Operations tail
8368
- * @type {Object}
8496
+ * Cached DataView over {@link Writer#buf}.
8497
+ * @type {DataView|null}
8369
8498
  */
8370
- this.tail = this.head;
8499
+ this.view = null;
8371
8500
 
8372
8501
  /**
8373
- * Linked forked states.
8374
- * @type {Object|null}
8502
+ * Stack of forked length-prefix positions.
8503
+ * @type {Array<number>|null}
8375
8504
  */
8376
8505
  this.states = null;
8377
-
8378
- // When a value is written, the writer calculates its byte length and puts it into a linked
8379
- // list of operations to perform when finish() is called. This both allows us to allocate
8380
- // buffers of the exact required size and reduces the amount of work we have to do compared
8381
- // to first calculating over objects and then encoding over objects. In our case, the encoding
8382
- // part is just a linked list walk calling operations with already prepared values.
8383
8506
  }
8384
8507
 
8508
+ /**
8509
+ * Current write position.
8510
+ * @name Writer#len
8511
+ * @type {number}
8512
+ * @deprecated Use {@link Writer#pos} instead.
8513
+ */
8514
+ Object.defineProperty(Writer.prototype, "len", {
8515
+ configurable: true,
8516
+ enumerable: true,
8517
+ get: function get_len() {
8518
+ return this.pos;
8519
+ }
8520
+ });
8521
+
8385
8522
  var create = function create() {
8386
8523
  return util.Buffer
8387
8524
  ? function create_buffer_setup() {
@@ -8408,32 +8545,45 @@ Writer.create = create();
8408
8545
  * @returns {Uint8Array} Buffer
8409
8546
  */
8410
8547
  Writer.alloc = function alloc(size) {
8411
- return new util.Array(size);
8548
+ return new Uint8Array(size);
8412
8549
  };
8413
8550
 
8414
8551
  // Use Uint8Array buffer pool in the browser, just like node does with buffers
8415
- /* istanbul ignore else */
8416
- if (util.Array !== Array)
8417
- Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
8552
+ Writer.alloc = util.pool(Writer.alloc, Uint8Array.prototype.subarray);
8418
8553
 
8419
8554
  /**
8420
- * Pushes a new operation to the queue.
8421
- * @param {function(Uint8Array, number, *)} fn Function to call
8422
- * @param {number} len Value byte length
8423
- * @param {number} val Value to write
8424
- * @returns {Writer} `this`
8555
+ * Calculates the number of bytes a value occupies as a varint.
8556
+ * @param {number} value Value to size (unsigned)
8557
+ * @returns {number} Byte length (1..5)
8558
+ * @ignore
8559
+ */
8560
+ function sizeVarint32(value) {
8561
+ return value < 128 ? 1
8562
+ : value < 16384 ? 2
8563
+ : value < 2097152 ? 3
8564
+ : value < 268435456 ? 4
8565
+ : 5;
8566
+ }
8567
+
8568
+ /**
8569
+ * Ensures that at least `n` more bytes fit into the backing buffer, doubling it if not.
8570
+ * @param {number} n Number of additional bytes required
8571
+ * @returns {undefined}
8425
8572
  * @private
8426
8573
  */
8427
- Writer.prototype._push = function push(fn, len, val) {
8428
- this.tail = this.tail.next = new Op(fn, len, val);
8429
- this.len += len;
8430
- return this;
8574
+ Writer.prototype._reserve = function _reserve(n) {
8575
+ var need = this.pos + n;
8576
+ if (need > this.buf.length) {
8577
+ var size = this.buf.length << 1;
8578
+ if (size < need)
8579
+ size = need;
8580
+ var buf = this.constructor.alloc(size);
8581
+ buf.set(this.buf.subarray(0, this.pos), 0);
8582
+ this.buf = buf;
8583
+ this.view = null; // invalidate
8584
+ }
8431
8585
  };
8432
8586
 
8433
- function writeByte(val, buf, pos) {
8434
- buf[pos] = val & 255;
8435
- }
8436
-
8437
8587
  function writeStringAscii(val, buf, pos) {
8438
8588
  for (var i = 0; i < val.length;)
8439
8589
  buf[pos++] = val.charCodeAt(i++);
@@ -8445,42 +8595,19 @@ function writeVarint32(val, buf, pos) {
8445
8595
  val >>>= 7;
8446
8596
  }
8447
8597
  buf[pos] = val;
8598
+ return pos + 1;
8448
8599
  }
8449
8600
 
8450
- /**
8451
- * Constructs a new varint writer operation instance.
8452
- * @classdesc Scheduled varint writer operation.
8453
- * @extends Op
8454
- * @constructor
8455
- * @param {number} len Value byte length
8456
- * @param {number} val Value to write
8457
- * @ignore
8458
- */
8459
- function VarintOp(len, val) {
8460
- this.len = len;
8461
- this.next = undefined;
8462
- this.val = val;
8463
- }
8464
-
8465
- VarintOp.prototype = Object.create(Op.prototype);
8466
- VarintOp.prototype.fn = writeVarint32;
8467
-
8468
8601
  /**
8469
8602
  * Writes an unsigned 32 bit value as a varint.
8470
8603
  * @param {number} value Value to write
8471
8604
  * @returns {Writer} `this`
8472
8605
  */
8473
8606
  Writer.prototype.uint32 = function write_uint32(value) {
8474
- // here, the call to this.push has been inlined and a varint specific Op subclass is used.
8475
- // uint32 is by far the most frequently used operation and benefits significantly from this.
8476
- this.len += (this.tail = this.tail.next = new VarintOp(
8477
- (value = value >>> 0)
8478
- < 128 ? 1
8479
- : value < 16384 ? 2
8480
- : value < 2097152 ? 3
8481
- : value < 268435456 ? 4
8482
- : 5,
8483
- value)).len;
8607
+ value = value >>> 0;
8608
+ this._reserve(5);
8609
+ var pos = this.pos;
8610
+ this.pos = writeVarint32(value, this.buf, pos);
8484
8611
  return this;
8485
8612
  };
8486
8613
 
@@ -8491,9 +8618,13 @@ Writer.prototype.uint32 = function write_uint32(value) {
8491
8618
  * @returns {Writer} `this`
8492
8619
  */
8493
8620
  Writer.prototype.int32 = function write_int32(value) {
8494
- return (value |= 0) < 0
8495
- ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
8496
- : this.uint32(value);
8621
+ if ((value |= 0) < 0) { // 10 bytes per spec
8622
+ this._reserve(10);
8623
+ writeVarint64(LongBits.fromNumber(value), this.buf, this.pos);
8624
+ this.pos += 10;
8625
+ return this;
8626
+ }
8627
+ return this.uint32(value);
8497
8628
  };
8498
8629
 
8499
8630
  /**
@@ -8517,7 +8648,8 @@ function writeVarint64(val, buf, pos) {
8517
8648
  buf[pos++] = lo & 127 | 128;
8518
8649
  lo = lo >>> 7;
8519
8650
  }
8520
- buf[pos++] = lo;
8651
+ buf[pos] = lo;
8652
+ return pos + 1;
8521
8653
  }
8522
8654
 
8523
8655
  /**
@@ -8528,7 +8660,10 @@ function writeVarint64(val, buf, pos) {
8528
8660
  */
8529
8661
  Writer.prototype.uint64 = function write_uint64(value) {
8530
8662
  var bits = LongBits.from(value);
8531
- return this._push(writeVarint64, bits.length(), bits);
8663
+ this._reserve(10);
8664
+ var pos = this.pos;
8665
+ this.pos = writeVarint64(bits, this.buf, pos);
8666
+ return this;
8532
8667
  };
8533
8668
 
8534
8669
  /**
@@ -8548,7 +8683,10 @@ Writer.prototype.int64 = Writer.prototype.uint64;
8548
8683
  */
8549
8684
  Writer.prototype.sint64 = function write_sint64(value) {
8550
8685
  var bits = LongBits.from(value).zzEncode();
8551
- return this._push(writeVarint64, bits.length(), bits);
8686
+ this._reserve(10);
8687
+ var pos = this.pos;
8688
+ this.pos = writeVarint64(bits, this.buf, pos);
8689
+ return this;
8552
8690
  };
8553
8691
 
8554
8692
  /**
@@ -8557,7 +8695,9 @@ Writer.prototype.sint64 = function write_sint64(value) {
8557
8695
  * @returns {Writer} `this`
8558
8696
  */
8559
8697
  Writer.prototype.bool = function write_bool(value) {
8560
- return this._push(writeByte, 1, value ? 1 : 0);
8698
+ this._reserve(1);
8699
+ this.buf[this.pos++] = value ? 1 : 0;
8700
+ return this;
8561
8701
  };
8562
8702
 
8563
8703
  function writeFixed32(val, buf, pos) {
@@ -8573,7 +8713,10 @@ function writeFixed32(val, buf, pos) {
8573
8713
  * @returns {Writer} `this`
8574
8714
  */
8575
8715
  Writer.prototype.fixed32 = function write_fixed32(value) {
8576
- return this._push(writeFixed32, 4, value >>> 0);
8716
+ this._reserve(4);
8717
+ writeFixed32(value >>> 0, this.buf, this.pos);
8718
+ this.pos += 4;
8719
+ return this;
8577
8720
  };
8578
8721
 
8579
8722
  /**
@@ -8592,7 +8735,11 @@ Writer.prototype.sfixed32 = Writer.prototype.fixed32;
8592
8735
  */
8593
8736
  Writer.prototype.fixed64 = function write_fixed64(value) {
8594
8737
  var bits = LongBits.from(value);
8595
- return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
8738
+ this._reserve(8);
8739
+ writeFixed32(bits.lo, this.buf, this.pos);
8740
+ writeFixed32(bits.hi, this.buf, this.pos + 4);
8741
+ this.pos += 8;
8742
+ return this;
8596
8743
  };
8597
8744
 
8598
8745
  /**
@@ -8611,7 +8758,10 @@ Writer.prototype.sfixed64 = Writer.prototype.fixed64;
8611
8758
  * @returns {Writer} `this`
8612
8759
  */
8613
8760
  Writer.prototype.float = function write_float(value) {
8614
- return this._push(util.float.writeFloatLE, 4, value);
8761
+ this._reserve(4);
8762
+ util.float.writeFloatLE(value, this.buf, this.pos);
8763
+ this.pos += 4;
8764
+ return this;
8615
8765
  };
8616
8766
 
8617
8767
  /**
@@ -8621,19 +8771,12 @@ Writer.prototype.float = function write_float(value) {
8621
8771
  * @returns {Writer} `this`
8622
8772
  */
8623
8773
  Writer.prototype.double = function write_double(value) {
8624
- return this._push(util.float.writeDoubleLE, 8, value);
8774
+ this._reserve(8);
8775
+ util.float.writeDoubleLE(value, this.buf, this.pos);
8776
+ this.pos += 8;
8777
+ return this;
8625
8778
  };
8626
8779
 
8627
- var writeBytes = util.Array.prototype.set
8628
- ? function writeBytes_set(val, buf, pos) {
8629
- buf.set(val, pos); // also works for plain array values
8630
- }
8631
- /* istanbul ignore next */
8632
- : function writeBytes_for(val, buf, pos) {
8633
- for (var i = 0; i < val.length; ++i)
8634
- buf[pos + i] = val[i];
8635
- };
8636
-
8637
8780
  /**
8638
8781
  * Writes a sequence of bytes.
8639
8782
  * @param {Uint8Array|string} value Buffer or base64 encoded string to write
@@ -8641,14 +8784,21 @@ var writeBytes = util.Array.prototype.set
8641
8784
  */
8642
8785
  Writer.prototype.bytes = function write_bytes(value) {
8643
8786
  var len = value.length >>> 0;
8644
- if (!len)
8645
- return this._push(writeByte, 1, 0);
8787
+ if (!len) {
8788
+ this._reserve(1);
8789
+ this.buf[this.pos++] = 0;
8790
+ return this;
8791
+ }
8646
8792
  if (util.isString(value)) {
8647
8793
  var buf = Writer.alloc(len = base64.length(value));
8648
8794
  base64.decode(value, buf, 0);
8649
8795
  value = buf;
8650
8796
  }
8651
- return this.uint32(len)._push(writeBytes, len, value);
8797
+ this.uint32(len);
8798
+ this._reserve(len);
8799
+ this.buf.set(value, this.pos);
8800
+ this.pos += len;
8801
+ return this;
8652
8802
  };
8653
8803
 
8654
8804
  /**
@@ -8658,7 +8808,28 @@ Writer.prototype.bytes = function write_bytes(value) {
8658
8808
  */
8659
8809
  Writer.prototype.raw = function write_raw(value) {
8660
8810
  var len = value.length >>> 0;
8661
- return len ? this._push(writeBytes, len, value) : this;
8811
+ if (!len)
8812
+ return this;
8813
+ this._reserve(len);
8814
+ this.buf.set(value, this.pos);
8815
+ this.pos += len;
8816
+ return this;
8817
+ };
8818
+
8819
+ /**
8820
+ * Backfills the length varint.
8821
+ * @param {number} pos Position of reserved length byte
8822
+ * @param {number} len Length of content after length varint
8823
+ * @returns {Writer} `this`
8824
+ * @private
8825
+ */
8826
+ Writer.prototype._delim = function _delim(pos, len) {
8827
+ var n = sizeVarint32(len);
8828
+ if (n > 1)
8829
+ this.buf.copyWithin(pos + n, pos + 1, pos + 1 + len);
8830
+ writeVarint32(len, this.buf, pos);
8831
+ this.pos = pos + n + len;
8832
+ return this;
8662
8833
  };
8663
8834
 
8664
8835
  /**
@@ -8667,21 +8838,245 @@ Writer.prototype.raw = function write_raw(value) {
8667
8838
  * @returns {Writer} `this`
8668
8839
  */
8669
8840
  Writer.prototype.string = function write_string(value) {
8841
+ var n = value.length;
8842
+ if (!n) {
8843
+ this._reserve(1);
8844
+ this.buf[this.pos++] = 0;
8845
+ return this;
8846
+ }
8847
+ if (n < 0x80) {
8848
+ this._reserve(n * 3 + 5); // worst case
8849
+ var lenPos = this.pos;
8850
+ return this._delim(lenPos, utf8.write(value, this.buf, lenPos + 1));
8851
+ }
8670
8852
  var len = utf8.length(value);
8671
- return len
8672
- ? this.uint32(len)._push(len === value.length ? writeStringAscii : utf8.write, len, value)
8673
- : this._push(writeByte, 1, 0);
8853
+ this.uint32(len);
8854
+ this._reserve(len);
8855
+ if (len === value.length)
8856
+ writeStringAscii(value, this.buf, this.pos);
8857
+ else
8858
+ utf8.write(value, this.buf, this.pos);
8859
+ this.pos += len;
8860
+ return this;
8861
+ };
8862
+
8863
+ /**
8864
+ * Writes an array of unsigned 32 bit values as a packed repeated field.
8865
+ * @param {number[]} value Values to write
8866
+ * @returns {Writer} `this`
8867
+ */
8868
+ Writer.prototype.uint32s = function write_uint32s(value) {
8869
+ var n = value.length;
8870
+ this._reserve(n * 5 + 5); // worst case: 5 bytes per value + length varint
8871
+ var buf = this.buf, lenPos = this.pos, p = lenPos + 1;
8872
+ for (var i = 0; i < n; ++i)
8873
+ p = writeVarint32(value[i] >>> 0, buf, p);
8874
+ return this._delim(lenPos, p - lenPos - 1);
8875
+ };
8876
+
8877
+ /**
8878
+ * Writes an array of signed 32 bit values as a packed repeated field.
8879
+ * @param {number[]} value Values to write
8880
+ * @returns {Writer} `this`
8881
+ */
8882
+ Writer.prototype.int32s = function write_int32s(value) {
8883
+ var n = value.length;
8884
+ this._reserve(n * 10 + 5); // worst case: 10 bytes per negative value + length varint
8885
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1, val;
8886
+ for (var i = 0; i < n; ++i) {
8887
+ if ((val = value[i] | 0) < 0) { // negatives are 10 bytes per spec
8888
+ pos = writeVarint64(LongBits.fromNumber(val), buf, pos);
8889
+ } else {
8890
+ pos = writeVarint32(val, buf, pos);
8891
+ }
8892
+ }
8893
+ return this._delim(lenPos, pos - lenPos - 1);
8894
+ };
8895
+
8896
+ /**
8897
+ * Writes an array of 32 bit values as packed, zig-zag encoded varints.
8898
+ * @param {number[]} value Values to write
8899
+ * @returns {Writer} `this`
8900
+ */
8901
+ Writer.prototype.sint32s = function write_sint32s(value) {
8902
+ var n = value.length;
8903
+ this._reserve(n * 5 + 5);
8904
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
8905
+ for (var i = 0; i < n; ++i)
8906
+ pos = writeVarint32((value[i] << 1 ^ value[i] >> 31) >>> 0, buf, pos);
8907
+ return this._delim(lenPos, pos - lenPos - 1);
8908
+ };
8909
+
8910
+ /**
8911
+ * Writes an array of unsigned 64 bit values as a packed repeated field.
8912
+ * @param {Array.<Long|number|string>} value Values to write
8913
+ * @returns {Writer} `this`
8914
+ */
8915
+ Writer.prototype.uint64s = function write_uint64s(value) {
8916
+ var n = value.length;
8917
+ this._reserve(n * 10 + 5);
8918
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
8919
+ for (var i = 0; i < n; ++i) {
8920
+ pos = writeVarint64(LongBits.from(value[i]), buf, pos);
8921
+ }
8922
+ return this._delim(lenPos, pos - lenPos - 1);
8923
+ };
8924
+
8925
+ /**
8926
+ * Writes an array of signed 64 bit values as a packed repeated field.
8927
+ * @function
8928
+ * @param {Array.<Long|number|string>} value Values to write
8929
+ * @returns {Writer} `this`
8930
+ */
8931
+ Writer.prototype.int64s = Writer.prototype.uint64s;
8932
+
8933
+ /**
8934
+ * Writes an array of 64 bit values as packed, zig-zag encoded varints.
8935
+ * @param {Array.<Long|number|string>} value Values to write
8936
+ * @returns {Writer} `this`
8937
+ */
8938
+ Writer.prototype.sint64s = function write_sint64s(value) {
8939
+ var n = value.length;
8940
+ this._reserve(n * 10 + 5);
8941
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
8942
+ for (var i = 0; i < n; ++i) {
8943
+ pos = writeVarint64(LongBits.from(value[i]).zzEncode(), buf, pos);
8944
+ }
8945
+ return this._delim(lenPos, pos - lenPos - 1);
8946
+ };
8947
+
8948
+ /**
8949
+ * Writes an array of boolish values as a packed repeated field.
8950
+ * @param {boolean[]} value Values to write
8951
+ * @returns {Writer} `this`
8952
+ */
8953
+ Writer.prototype.bools = function write_bools(value) {
8954
+ var n = value.length;
8955
+ this.uint32(n); // one byte per value
8956
+ this._reserve(n);
8957
+ var buf = this.buf, p = this.pos;
8958
+ for (var i = 0; i < n; ++i)
8959
+ buf[p++] = value[i] ? 1 : 0;
8960
+ this.pos += n;
8961
+ return this;
8962
+ };
8963
+
8964
+ // The view allocation only pays off when amortized over enough writes
8965
+ var VIEW_THRESHOLD_FLOAT = 16,
8966
+ VIEW_THRESHOLD_INT = 128;
8967
+
8968
+ function getLazyView(writer, count, threshold) {
8969
+ var view = writer.view;
8970
+ if (view || count < threshold)
8971
+ return view;
8972
+ var buf = writer.buf;
8973
+ return writer.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
8974
+ }
8975
+
8976
+ /**
8977
+ * Writes an array of unsigned 32 bit values as packed, fixed 32 bits.
8978
+ * @param {number[]} value Values to write
8979
+ * @returns {Writer} `this`
8980
+ */
8981
+ Writer.prototype.fixed32s = function write_fixed32s(value) {
8982
+ var n = value.length, bytes = n * 4;
8983
+ this.uint32(bytes); // length is known exactly
8984
+ this._reserve(bytes);
8985
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_INT);
8986
+ if (dv)
8987
+ for (i = 0; i < n; ++i) { dv.setUint32(p, value[i] >>> 0, true); p += 4; }
8988
+ else {
8989
+ var buf = this.buf;
8990
+ for (i = 0; i < n; ++i) { writeFixed32(value[i] >>> 0, buf, p); p += 4; }
8991
+ }
8992
+ this.pos += bytes;
8993
+ return this;
8994
+ };
8995
+
8996
+ /**
8997
+ * Writes an array of signed 32 bit values as packed, fixed 32 bits.
8998
+ * @function
8999
+ * @param {number[]} value Values to write
9000
+ * @returns {Writer} `this`
9001
+ */
9002
+ Writer.prototype.sfixed32s = Writer.prototype.fixed32s;
9003
+
9004
+ /**
9005
+ * Writes an array of unsigned 64 bit values as packed, fixed 64 bits.
9006
+ * @param {Array.<Long|number|string>} value Values to write
9007
+ * @returns {Writer} `this`
9008
+ */
9009
+ Writer.prototype.fixed64s = function write_fixed64s(value) {
9010
+ var n = value.length, bytes = n * 8;
9011
+ this.uint32(bytes);
9012
+ this._reserve(bytes);
9013
+ var p = this.pos, i, bits, dv = getLazyView(this, n, VIEW_THRESHOLD_INT);
9014
+ if (dv)
9015
+ 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; }
9016
+ else {
9017
+ var buf = this.buf;
9018
+ for (i = 0; i < n; ++i) { bits = LongBits.from(value[i]); writeFixed32(bits.lo, buf, p); writeFixed32(bits.hi, buf, p + 4); p += 8; }
9019
+ }
9020
+ this.pos += bytes;
9021
+ return this;
9022
+ };
9023
+
9024
+ /**
9025
+ * Writes an array of signed 64 bit values as packed, fixed 64 bits.
9026
+ * @function
9027
+ * @param {Array.<Long|number|string>} value Values to write
9028
+ * @returns {Writer} `this`
9029
+ */
9030
+ Writer.prototype.sfixed64s = Writer.prototype.fixed64s;
9031
+
9032
+ /**
9033
+ * Writes an array of floats (32 bit) as a packed repeated field.
9034
+ * @param {number[]} value Values to write
9035
+ * @returns {Writer} `this`
9036
+ */
9037
+ Writer.prototype.floats = function write_floats(value) {
9038
+ var n = value.length, bytes = n * 4;
9039
+ this.uint32(bytes);
9040
+ this._reserve(bytes);
9041
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_FLOAT);
9042
+ if (dv)
9043
+ for (i = 0; i < n; ++i) { dv.setFloat32(p, value[i], true); p += 4; }
9044
+ else {
9045
+ var buf = this.buf;
9046
+ for (i = 0; i < n; ++i) { util.float.writeFloatLE(value[i], buf, p); p += 4; }
9047
+ }
9048
+ this.pos += bytes;
9049
+ return this;
9050
+ };
9051
+
9052
+ /**
9053
+ * Writes an array of doubles (64 bit float) as a packed repeated field.
9054
+ * @param {number[]} value Values to write
9055
+ * @returns {Writer} `this`
9056
+ */
9057
+ Writer.prototype.doubles = function write_doubles(value) {
9058
+ var n = value.length, bytes = n * 8;
9059
+ this.uint32(bytes);
9060
+ this._reserve(bytes);
9061
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_FLOAT);
9062
+ if (dv)
9063
+ for (i = 0; i < n; ++i) { dv.setFloat64(p, value[i], true); p += 8; }
9064
+ else {
9065
+ var buf = this.buf;
9066
+ for (i = 0; i < n; ++i) { util.float.writeDoubleLE(value[i], buf, p); p += 8; }
9067
+ }
9068
+ this.pos += bytes;
9069
+ return this;
8674
9070
  };
8675
9071
 
8676
9072
  /**
8677
9073
  * Forks this writer's state by pushing it to a stack.
8678
- * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
8679
9074
  * @returns {Writer} `this`
8680
9075
  */
8681
9076
  Writer.prototype.fork = function fork() {
8682
- this.states = new State(this);
8683
- this.head = this.tail = new Op(noop, 0, 0);
8684
- this.len = 0;
9077
+ this._reserve(1);
9078
+ (this.states || (this.states = [])).push(this.pos);
9079
+ this.pos += 1;
8685
9080
  return this;
8686
9081
  };
8687
9082
 
@@ -8690,47 +9085,66 @@ Writer.prototype.fork = function fork() {
8690
9085
  * @returns {Writer} `this`
8691
9086
  */
8692
9087
  Writer.prototype.reset = function reset() {
8693
- if (this.states) {
8694
- this.head = this.states.head;
8695
- this.tail = this.states.tail;
8696
- this.len = this.states.len;
8697
- this.states = this.states.next;
9088
+ var states = this.states;
9089
+ if (states && states.length) {
9090
+ this.pos = states.pop();
8698
9091
  } else {
8699
- this.head = this.tail = new Op(noop, 0, 0);
8700
- this.len = 0;
9092
+ this.pos = 0;
8701
9093
  }
8702
9094
  return this;
8703
9095
  };
8704
9096
 
8705
9097
  /**
8706
- * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
9098
+ * Resets to the last state and prepends the fork state's current write length as a varint.
8707
9099
  * @returns {Writer} `this`
8708
9100
  */
8709
9101
  Writer.prototype.ldelim = function ldelim() {
8710
- var head = this.head,
8711
- tail = this.tail,
8712
- len = this.len;
8713
- this.reset().uint32(len);
8714
- if (len) {
8715
- this.tail.next = head.next; // skip noop
8716
- this.tail = tail;
8717
- this.len += len;
9102
+ var states = this.states,
9103
+ len,
9104
+ vlen;
9105
+ if (states && states.length) {
9106
+ var lenPos = states.pop();
9107
+ len = this.pos - lenPos - 1;
9108
+ vlen = sizeVarint32(len);
9109
+ if (vlen > 1) { // grow the reserved single byte and shift content forward
9110
+ this._reserve(vlen - 1);
9111
+ this.buf.copyWithin(lenPos + vlen, lenPos + 1, lenPos + 1 + len);
9112
+ this.pos += vlen - 1;
9113
+ writeVarint32(len, this.buf, lenPos);
9114
+ } else {
9115
+ this.buf[lenPos] = len;
9116
+ }
9117
+ } else { // not forked: prefix the entire buffer with its length
9118
+ // TODO: Compatibility with older generated code.
9119
+ // Remove this branch in the next major release.
9120
+ len = this.pos;
9121
+ vlen = sizeVarint32(len);
9122
+ this._reserve(vlen);
9123
+ this.buf.copyWithin(vlen, 0, len);
9124
+ writeVarint32(len, this.buf, 0);
9125
+ this.pos += vlen;
8718
9126
  }
8719
9127
  return this;
8720
9128
  };
8721
9129
 
8722
9130
  /**
8723
9131
  * Finishes the write operation.
9132
+ * Returns a buffer sized to the written data by default.
9133
+ * @param {boolean} [shared=false] Whether to return a shared view instead of a unique copy
8724
9134
  * @returns {Uint8Array} Finished buffer
8725
9135
  */
8726
- Writer.prototype.finish = function finish() {
8727
- return this.finishInto(this.constructor.alloc(this.len), 0);
9136
+ Writer.prototype.finish = function finish(shared) {
9137
+ if (shared)
9138
+ return this.buf.subarray(0, this.pos);
9139
+ var buf = this.constructor.alloc(this.pos);
9140
+ buf.set(this.buf.subarray(0, this.pos), 0);
9141
+ return buf;
8728
9142
  };
8729
9143
 
8730
9144
  /**
8731
9145
  * Finishes the write operation, writing into the provided buffer.
8732
9146
  * The caller must ensure that `buf` has enough space starting at `offset`
8733
- * to hold {@link Writer#len} bytes.
9147
+ * to hold {@link Writer#pos} bytes.
8734
9148
  * @param {T} buf Target buffer
8735
9149
  * @param {number} [offset=0] Offset to start writing at
8736
9150
  * @returns {T} The provided buffer
@@ -8739,13 +9153,7 @@ Writer.prototype.finish = function finish() {
8739
9153
  Writer.prototype.finishInto = function finishInto(buf, offset) {
8740
9154
  if (offset === undefined)
8741
9155
  offset = 0;
8742
- var head = this.head.next,
8743
- pos = offset;
8744
- while (head) {
8745
- head.fn(head.val, buf, pos);
8746
- pos += head.len;
8747
- head = head.next;
8748
- }
9156
+ buf.set(this.buf.subarray(0, this.pos), offset);
8749
9157
  return buf;
8750
9158
  };
8751
9159
 
@@ -8782,6 +9190,8 @@ function BufferWriter() {
8782
9190
  Writer.call(this);
8783
9191
  }
8784
9192
 
9193
+ var writeStringBuffer;
9194
+
8785
9195
  BufferWriter._configure = function () {
8786
9196
  /**
8787
9197
  * Allocates a buffer of the specified size.
@@ -8789,19 +9199,14 @@ BufferWriter._configure = function () {
8789
9199
  * @param {number} size Buffer size
8790
9200
  * @returns {Buffer} Buffer
8791
9201
  */
8792
- BufferWriter.alloc = util._Buffer_allocUnsafe;
9202
+ BufferWriter.alloc = util.Buffer && util.Buffer.allocUnsafe;
8793
9203
 
8794
- BufferWriter.writeBytesBuffer = util.Buffer && util.Buffer.prototype instanceof Uint8Array && util.Buffer.prototype.set.name === "set"
8795
- ? function writeBytesBuffer_set(val, buf, pos) {
8796
- buf.set(val, pos); // faster than copy (requires node >= 4 where Buffers extend Uint8Array and set is properly inherited)
8797
- // also works for plain array values
9204
+ writeStringBuffer = util.Buffer && util.Buffer.prototype.utf8Write
9205
+ ? function writeStringBuffer_utf8Write(val, buf, pos) {
9206
+ return buf.utf8Write(val, pos);
8798
9207
  }
8799
- /* istanbul ignore next */
8800
- : function writeBytesBuffer_copy(val, buf, pos) {
8801
- if (val.copy) // Buffer values
8802
- val.copy(buf, pos, 0, val.length);
8803
- else for (var i = 0; i < val.length;) // plain array values
8804
- buf[pos++] = val[i++];
9208
+ : function writeStringBuffer_write(val, buf, pos) {
9209
+ return buf.write(val, pos);
8805
9210
  };
8806
9211
  };
8807
9212
 
@@ -8811,48 +9216,42 @@ BufferWriter._configure = function () {
8811
9216
  */
8812
9217
  BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
8813
9218
  if (util.isString(value))
8814
- value = util._Buffer_from(value, "base64");
9219
+ value = util.Buffer.from(value, "base64");
8815
9220
  var len = value.length >>> 0;
8816
9221
  this.uint32(len);
8817
- if (len)
8818
- this._push(BufferWriter.writeBytesBuffer, len, value);
9222
+ if (len) {
9223
+ this._reserve(len);
9224
+ this.buf.set(value, this.pos);
9225
+ this.pos += len;
9226
+ }
8819
9227
  return this;
8820
9228
  };
8821
9229
 
8822
- /**
8823
- * Writes raw bytes without a tag or length prefix.
8824
- * @name BufferWriter#raw
8825
- * @function
8826
- * @param {Uint8Array} value Raw bytes
8827
- * @returns {BufferWriter} `this`
8828
- */
8829
- BufferWriter.prototype.raw = function write_raw_buffer(value) {
8830
- var len = value.length >>> 0;
8831
- return len ? this._push(BufferWriter.writeBytesBuffer, len, value) : this;
8832
- };
8833
-
8834
- function writeStringBufferAscii(val, buf, pos) {
8835
- for (var i = 0; i < val.length;)
8836
- buf[pos++] = val.charCodeAt(i++);
8837
- }
8838
-
8839
- function writeStringBuffer(val, buf, pos) {
8840
- if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
8841
- util.utf8.write(val, buf, pos);
8842
- else if (buf.utf8Write)
8843
- buf.utf8Write(val, pos);
8844
- else
8845
- buf.write(val, pos);
8846
- }
8847
-
8848
9230
  /**
8849
9231
  * @override
8850
9232
  */
8851
9233
  BufferWriter.prototype.string = function write_string_buffer(value) {
9234
+ var n = value.length;
9235
+ if (!n) {
9236
+ this._reserve(1);
9237
+ this.buf[this.pos++] = 0;
9238
+ return this;
9239
+ }
9240
+ if (n < 0x80) {
9241
+ this._reserve(n * 3 + 5); // worst case
9242
+ var pos = this.pos,
9243
+ buf = this.buf;
9244
+ return this._delim(pos,
9245
+ n < 40
9246
+ ? util.utf8.write(value, buf, pos + 1)
9247
+ : writeStringBuffer(value, buf, pos + 1)
9248
+ );
9249
+ }
8852
9250
  var len = util.Buffer.byteLength(value);
8853
9251
  this.uint32(len);
8854
- if (len)
8855
- this._push(len === value.length && len < 40 ? writeStringBufferAscii : writeStringBuffer, len, value);
9252
+ this._reserve(len);
9253
+ writeStringBuffer(value, this.buf, this.pos);
9254
+ this.pos += len;
8856
9255
  return this;
8857
9256
  };
8858
9257