protobufjs 8.6.6 → 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.
package/dist/protobuf.js CHANGED
@@ -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.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
  */
@@ -938,13 +938,9 @@ function decoder(mtype) {
938
938
  if (types.packed[type] !== undefined) {
939
939
  gen
940
940
  ("if(u===2){");
941
- if (!closed) gen
942
- ("if(!(%s&&%s.length))", ref, ref)
943
- ("%s=[]", ref);
944
- gen
945
- ("var c2=r.uint32()+r.pos");
946
941
  if (closed) {
947
942
  gen
943
+ ("var c2=r.uint32()+r.pos")
948
944
  ("while(r.pos<c2){")
949
945
  ("s=r.pos")
950
946
  ("v=r.%s()", type)
@@ -956,8 +952,9 @@ function decoder(mtype) {
956
952
  genPreserveUnknown(gen, "util.rawField(" + field.id + ",0,r.raw(s,r.pos))")
957
953
  ("}");
958
954
  } else gen
959
- ("while(r.pos<c2)")
960
- ("%s.push(r.%s())", ref, type);
955
+ ("if(!(%s&&%s.length))", ref, ref)
956
+ ("%s=[]", ref)
957
+ ("r.%ss(%s)", type, ref);
961
958
  gen
962
959
  ("continue")
963
960
  ("}");
@@ -1153,10 +1150,7 @@ function encoder(mtype) {
1153
1150
  // Packed repeated
1154
1151
  if (field.packed && types.packed[type] !== undefined) { gen
1155
1152
 
1156
- ("w.uint32(%i).fork()", (field.id << 3 | 2) >>> 0)
1157
- ("for(var i=0;i<%s.length;++i)", ref)
1158
- ("w.%s(%s[i])", type, ref)
1159
- ("w.ldelim()");
1153
+ ("w.uint32(%i).%ss(%s)", (field.id << 3 | 2) >>> 0, type, ref);
1160
1154
 
1161
1155
  // Non-packed
1162
1156
  } else { gen
@@ -2089,7 +2083,6 @@ exports.configure = configure;
2089
2083
  * @returns {undefined}
2090
2084
  */
2091
2085
  function configure() {
2092
- exports.util._configure();
2093
2086
  exports.Writer._configure(exports.BufferWriter);
2094
2087
  exports.Reader._configure(exports.BufferReader);
2095
2088
  }
@@ -4009,6 +4002,9 @@ function parse(source, root, options) {
4009
4002
  }
4010
4003
 
4011
4004
  function parseId(token, acceptNegative, max) {
4005
+ if (token === null) {
4006
+ throw illegal(token, "end of input");
4007
+ }
4012
4008
  switch (token) {
4013
4009
  case "max": case "MAX": case "Max":
4014
4010
  return max || maxFieldId;
@@ -4041,7 +4037,7 @@ function parse(source, root, options) {
4041
4037
  pkg = next();
4042
4038
 
4043
4039
  /* istanbul ignore if */
4044
- if (!typeRefRe.test(pkg))
4040
+ if (pkg === null || !typeRefRe.test(pkg))
4045
4041
  throw illegal(pkg, "name");
4046
4042
 
4047
4043
  ptr = ptr.define(pkg);
@@ -4178,7 +4174,7 @@ function parse(source, root, options) {
4178
4174
  throw Error("max depth exceeded");
4179
4175
 
4180
4176
  /* istanbul ignore if */
4181
- if (!nameRe.test(token = next()))
4177
+ if ((token = next()) === null || !nameRe.test(token))
4182
4178
  throw illegal(token, "type name");
4183
4179
 
4184
4180
  var type = new Type(token);
@@ -4245,6 +4241,9 @@ function parse(source, root, options) {
4245
4241
 
4246
4242
  function parseField(parent, rule, extend, depth) {
4247
4243
  var type = next();
4244
+ if (type === null) {
4245
+ throw illegal(type, "end of input");
4246
+ }
4248
4247
  if (type === "group") {
4249
4248
  parseGroup(parent, rule, extend, depth);
4250
4249
  return;
@@ -4256,8 +4255,12 @@ function parse(source, root, options) {
4256
4255
  // package .subpackage field tokens: "package" ".subpackage" [TYPE NAME ENDS HERE] "field"
4257
4256
  // Keep reading tokens until we get a type name with no period at the end,
4258
4257
  // and the next token does not start with a period.
4259
- while (type.endsWith(".") || peek().startsWith(".")) {
4260
- type += next();
4258
+ while (type.endsWith(".") || (peek() || "").startsWith(".")) {
4259
+ var part = next();
4260
+ if (part === null) {
4261
+ throw illegal(part, "end of input");
4262
+ }
4263
+ type += part;
4261
4264
  }
4262
4265
 
4263
4266
  /* istanbul ignore if */
@@ -4265,6 +4268,9 @@ function parse(source, root, options) {
4265
4268
  throw illegal(type, "type");
4266
4269
 
4267
4270
  var name = next();
4271
+ if (name === null) {
4272
+ throw illegal(name, "end of input");
4273
+ }
4268
4274
 
4269
4275
  /* istanbul ignore if */
4270
4276
 
@@ -4317,7 +4323,7 @@ function parse(source, root, options) {
4317
4323
  var name = next();
4318
4324
 
4319
4325
  /* istanbul ignore if */
4320
- if (!nameRe.test(name))
4326
+ if (name === null || !nameRe.test(name))
4321
4327
  throw illegal(name, "name");
4322
4328
 
4323
4329
  var fieldName = util.lcFirst(name);
@@ -4415,7 +4421,7 @@ function parse(source, root, options) {
4415
4421
  var name = next();
4416
4422
 
4417
4423
  /* istanbul ignore if */
4418
- if (!nameRe.test(name))
4424
+ if (name === null || !nameRe.test(name))
4419
4425
  throw illegal(name, "name");
4420
4426
 
4421
4427
  skip("=");
@@ -4442,7 +4448,7 @@ function parse(source, root, options) {
4442
4448
  function parseOneOf(parent, token, depth) {
4443
4449
 
4444
4450
  /* istanbul ignore if */
4445
- if (!nameRe.test(token = next()))
4451
+ if ((token = next()) === null || !nameRe.test(token))
4446
4452
  throw illegal(token, "name");
4447
4453
 
4448
4454
  var oneof = new OneOf(applyCase(token));
@@ -4461,7 +4467,7 @@ function parse(source, root, options) {
4461
4467
  function parseEnum(parent, token) {
4462
4468
 
4463
4469
  /* istanbul ignore if */
4464
- if (!nameRe.test(token = next()))
4470
+ if ((token = next()) === null || !nameRe.test(token))
4465
4471
  throw illegal(token, "name");
4466
4472
 
4467
4473
  var enm = new Enum(token);
@@ -4643,15 +4649,12 @@ function parse(source, root, options) {
4643
4649
  // lift json_name onto Field
4644
4650
  if (name === "json_name" && parent instanceof Field) {
4645
4651
  parent.jsonName = value;
4646
- return;
4647
4652
  }
4648
4653
  if (parent.setOption)
4649
4654
  parent.setOption(name, value);
4650
4655
  }
4651
4656
 
4652
4657
  function setParsedOption(parent, name, value, propName) {
4653
- if (name === "json_name" && parent instanceof Field)
4654
- return; // lifted onto Field#jsonName above
4655
4658
  if (parent.setParsedOption)
4656
4659
  parent.setParsedOption(name, value, propName);
4657
4660
  }
@@ -4673,7 +4676,7 @@ function parse(source, root, options) {
4673
4676
  throw Error("max depth exceeded");
4674
4677
 
4675
4678
  /* istanbul ignore if */
4676
- if (!nameRe.test(token = next()))
4679
+ if ((token = next()) === null || !nameRe.test(token))
4677
4680
  throw illegal(token, "service name");
4678
4681
 
4679
4682
  var service = new Service(token);
@@ -4751,7 +4754,7 @@ function parse(source, root, options) {
4751
4754
  function parseExtension(parent, token, depth) {
4752
4755
 
4753
4756
  /* istanbul ignore if */
4754
- if (!typeRefRe.test(token = next()))
4757
+ if ((token = next()) === null || !typeRefRe.test(token))
4755
4758
  throw illegal(token, "reference");
4756
4759
 
4757
4760
  var reference = token;
@@ -4879,7 +4882,7 @@ function indexOutOfRange(reader, writeLength) {
4879
4882
 
4880
4883
  /**
4881
4884
  * Constructs a new reader instance using the specified buffer.
4882
- * @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
4885
+ * @classdesc Wire format reader using `Uint8Array`.
4883
4886
  * @constructor
4884
4887
  * @param {Uint8Array} buffer Buffer to read from
4885
4888
  */
@@ -4903,6 +4906,12 @@ function Reader(buffer) {
4903
4906
  */
4904
4907
  this.len = buffer.length;
4905
4908
 
4909
+ /**
4910
+ * Cached DataView for packed reads.
4911
+ * @type {DataView|null}
4912
+ */
4913
+ this.view = null;
4914
+
4906
4915
  /**
4907
4916
  * Whether to discard unknown fields while decoding.
4908
4917
  * @type {boolean}
@@ -4910,18 +4919,14 @@ function Reader(buffer) {
4910
4919
  this.discardUnknown = Reader.discardUnknown;
4911
4920
  }
4912
4921
 
4913
- var create_array = typeof Uint8Array !== "undefined"
4914
- ? function create_typed_array(buffer) {
4915
- if (buffer instanceof Uint8Array || Array.isArray(buffer))
4916
- return new Reader(buffer);
4917
- throw Error("illegal buffer");
4918
- }
4919
- /* istanbul ignore next */
4920
- : function create_array(buffer) {
4921
- if (Array.isArray(buffer))
4922
- return new Reader(buffer);
4923
- throw Error("illegal buffer");
4924
- };
4922
+ function create_array(buffer) {
4923
+ // TODO: Remove plain array reader support in the next major release.
4924
+ if (Array.isArray(buffer))
4925
+ buffer = new Uint8Array(buffer);
4926
+ if (buffer instanceof Uint8Array)
4927
+ return new Reader(buffer);
4928
+ throw Error("illegal buffer");
4929
+ }
4925
4930
 
4926
4931
  var create = function create() {
4927
4932
  return util.Buffer
@@ -4946,8 +4951,6 @@ var create = function create() {
4946
4951
  */
4947
4952
  Reader.create = create();
4948
4953
 
4949
- Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore next */ util.Array.prototype.slice;
4950
-
4951
4954
  /**
4952
4955
  * Returns raw bytes from the backing buffer without advancing the reader.
4953
4956
  * @param {number} start Start offset
@@ -4955,12 +4958,7 @@ Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore ne
4955
4958
  * @returns {Uint8Array} Raw bytes
4956
4959
  */
4957
4960
  Reader.prototype.raw = function read_raw(start, end) {
4958
- if (Array.isArray(this.buf)) // plain array
4959
- return this.buf.slice(start, end);
4960
-
4961
- if (start === end) // fix for IE 10/Win8 and others' subarray returning array of size 1
4962
- return new this.buf.constructor(0);
4963
- return this._slice.call(this.buf, start, end);
4961
+ return this.buf.subarray(start, end);
4964
4962
  };
4965
4963
 
4966
4964
  /**
@@ -5257,6 +5255,241 @@ Reader.prototype.double = function read_double() {
5257
5255
  return value;
5258
5256
  };
5259
5257
 
5258
+ /**
5259
+ * Reads a packed repeated field of unsigned 32 bit varints.
5260
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5261
+ * @returns {number[]} Array read into
5262
+ */
5263
+ Reader.prototype.uint32s = function read_uint32s(array) {
5264
+ if (array === undefined) array = [];
5265
+ var end = this.uint32() + this.pos;
5266
+ while (this.pos < end)
5267
+ array.push(this.uint32());
5268
+ return array;
5269
+ };
5270
+
5271
+ /**
5272
+ * Reads a packed repeated field of signed 32 bit varints.
5273
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5274
+ * @returns {number[]} Array read into
5275
+ */
5276
+ Reader.prototype.int32s = function read_int32s(array) {
5277
+ if (array === undefined) array = [];
5278
+ var end = this.uint32() + this.pos;
5279
+ while (this.pos < end)
5280
+ array.push(this.int32());
5281
+ return array;
5282
+ };
5283
+
5284
+ /**
5285
+ * Reads a packed repeated field of zig-zag encoded signed 32 bit varints.
5286
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5287
+ * @returns {number[]} Array read into
5288
+ */
5289
+ Reader.prototype.sint32s = function read_sint32s(array) {
5290
+ if (array === undefined) array = [];
5291
+ var end = this.uint32() + this.pos;
5292
+ while (this.pos < end)
5293
+ array.push(this.sint32());
5294
+ return array;
5295
+ };
5296
+
5297
+ /**
5298
+ * Reads a packed repeated field of booleans.
5299
+ * @param {boolean[]} [array] Array to read into; a new one is created if omitted
5300
+ * @returns {boolean[]} Array read into
5301
+ */
5302
+ Reader.prototype.bools = function read_bools(array) {
5303
+ if (array === undefined) array = [];
5304
+ var end = this.uint32() + this.pos;
5305
+ while (this.pos < end)
5306
+ array.push(this.bool());
5307
+ return array;
5308
+ };
5309
+
5310
+ // The view allocation only pays off when amortized over enough reads
5311
+ var VIEW_THRESHOLD_FLOAT = 8,
5312
+ VIEW_THRESHOLD_INT = 128;
5313
+
5314
+ function getLazyView(reader, count, threshold) {
5315
+ var view = reader.view;
5316
+ if (view || count < threshold)
5317
+ return view;
5318
+ var buf = reader.buf;
5319
+ return reader.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
5320
+ }
5321
+
5322
+ /**
5323
+ * Reads a packed repeated field of unsigned 32 bit fixed values.
5324
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5325
+ * @returns {number[]} Array read into
5326
+ */
5327
+ Reader.prototype.fixed32s = function read_fixed32s(array) {
5328
+ if (array === undefined) array = [];
5329
+ var len = this.uint32(), end = this.pos + len;
5330
+ /* istanbul ignore if */
5331
+ if (end > this.len) throw indexOutOfRange(this, len);
5332
+ var count = len >>> 2, i = array.length, pos = this.pos;
5333
+ array.length = i + count;
5334
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
5335
+ if (dv)
5336
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getUint32(pos, true);
5337
+ else {
5338
+ var buf = this.buf;
5339
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4);
5340
+ }
5341
+ this.pos = pos;
5342
+ if (pos !== end) throw indexOutOfRange(this, 4);
5343
+ return array;
5344
+ };
5345
+
5346
+ /**
5347
+ * Reads a packed repeated field of signed 32 bit fixed values.
5348
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5349
+ * @returns {number[]} Array read into
5350
+ */
5351
+ Reader.prototype.sfixed32s = function read_sfixed32s(array) {
5352
+ if (array === undefined) array = [];
5353
+ var len = this.uint32(), end = this.pos + len;
5354
+ /* istanbul ignore if */
5355
+ if (end > this.len) throw indexOutOfRange(this, len);
5356
+ var count = len >>> 2, i = array.length, pos = this.pos;
5357
+ array.length = i + count;
5358
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
5359
+ if (dv)
5360
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getInt32(pos, true);
5361
+ else {
5362
+ var buf = this.buf;
5363
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4) | 0;
5364
+ }
5365
+ this.pos = pos;
5366
+ if (pos !== end) throw indexOutOfRange(this, 4);
5367
+ return array;
5368
+ };
5369
+
5370
+ /**
5371
+ * Reads a packed repeated field of floats (32 bit).
5372
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5373
+ * @returns {number[]} Array read into
5374
+ */
5375
+ Reader.prototype.floats = function read_floats(array) {
5376
+ if (array === undefined) array = [];
5377
+ var len = this.uint32(), end = this.pos + len;
5378
+ /* istanbul ignore if */
5379
+ if (end > this.len) throw indexOutOfRange(this, len);
5380
+ var count = len >>> 2, i = array.length, pos = this.pos;
5381
+ array.length = i + count;
5382
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
5383
+ if (dv)
5384
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getFloat32(pos, true);
5385
+ else {
5386
+ var buf = this.buf;
5387
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = util.float.readFloatLE(buf, pos);
5388
+ }
5389
+ this.pos = pos;
5390
+ if (pos !== end) throw indexOutOfRange(this, 4);
5391
+ return array;
5392
+ };
5393
+
5394
+ /**
5395
+ * Reads a packed repeated field of doubles (64 bit float).
5396
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5397
+ * @returns {number[]} Array read into
5398
+ */
5399
+ Reader.prototype.doubles = function read_doubles(array) {
5400
+ if (array === undefined) array = [];
5401
+ var len = this.uint32(), end = this.pos + len;
5402
+ /* istanbul ignore if */
5403
+ if (end > this.len) throw indexOutOfRange(this, len);
5404
+ var count = len >>> 3, i = array.length, pos = this.pos;
5405
+ array.length = i + count;
5406
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
5407
+ if (dv)
5408
+ for (var k = 0; k < count; ++k, pos += 8) array[i++] = dv.getFloat64(pos, true);
5409
+ else {
5410
+ var buf = this.buf;
5411
+ for (var j = 0; j < count; ++j, pos += 8) array[i++] = util.float.readDoubleLE(buf, pos);
5412
+ }
5413
+ this.pos = pos;
5414
+ if (pos !== end) throw indexOutOfRange(this, 8);
5415
+ return array;
5416
+ };
5417
+
5418
+ /**
5419
+ * Reads a packed repeated field of unsigned 64 bit varints.
5420
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5421
+ * @returns {Array.<Long|number>} Array read into
5422
+ */
5423
+ Reader.prototype.uint64s = function read_uint64s(array) {
5424
+ if (array === undefined) array = [];
5425
+ var end = this.uint32() + this.pos;
5426
+ while (this.pos < end)
5427
+ array.push(this.uint64());
5428
+ return array;
5429
+ };
5430
+
5431
+ /**
5432
+ * Reads a packed repeated field of signed 64 bit varints.
5433
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5434
+ * @returns {Array.<Long|number>} Array read into
5435
+ */
5436
+ Reader.prototype.int64s = function read_int64s(array) {
5437
+ if (array === undefined) array = [];
5438
+ var end = this.uint32() + this.pos;
5439
+ while (this.pos < end)
5440
+ array.push(this.int64());
5441
+ return array;
5442
+ };
5443
+
5444
+ /**
5445
+ * Reads a packed repeated field of zig-zag encoded signed 64 bit varints.
5446
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5447
+ * @returns {Array.<Long|number>} Array read into
5448
+ */
5449
+ Reader.prototype.sint64s = function read_sint64s(array) {
5450
+ if (array === undefined) array = [];
5451
+ var end = this.uint32() + this.pos;
5452
+ while (this.pos < end)
5453
+ array.push(this.sint64());
5454
+ return array;
5455
+ };
5456
+
5457
+ /**
5458
+ * Reads a packed repeated field of unsigned 64 bit fixed values.
5459
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5460
+ * @returns {Array.<Long|number>} Array read into
5461
+ */
5462
+ Reader.prototype.fixed64s = function read_fixed64s(array) {
5463
+ if (array === undefined) array = [];
5464
+ var len = this.uint32(), end = this.pos + len, i = array.length;
5465
+ /* istanbul ignore if */
5466
+ if (end > this.len) throw indexOutOfRange(this, len);
5467
+ var count = len >>> 3;
5468
+ array.length = i + count; // 8 bytes per value, count is known
5469
+ for (var j = 0; j < count; ++j)
5470
+ array[i++] = this.fixed64();
5471
+ if (this.pos !== end) throw indexOutOfRange(this, 8);
5472
+ return array;
5473
+ };
5474
+
5475
+ /**
5476
+ * Reads a packed repeated field of signed 64 bit fixed values.
5477
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5478
+ * @returns {Array.<Long|number>} Array read into
5479
+ */
5480
+ Reader.prototype.sfixed64s = function read_sfixed64s(array) {
5481
+ if (array === undefined) array = [];
5482
+ var len = this.uint32(), end = this.pos + len, i = array.length;
5483
+ /* istanbul ignore if */
5484
+ if (end > this.len) throw indexOutOfRange(this, len);
5485
+ var count = len >>> 3;
5486
+ array.length = i + count; // 8 bytes per value, count is known
5487
+ for (var j = 0; j < count; ++j)
5488
+ array[i++] = this.sfixed64();
5489
+ if (this.pos !== end) throw indexOutOfRange(this, 8);
5490
+ return array;
5491
+ };
5492
+
5260
5493
  /**
5261
5494
  * Reads a sequence of bytes preceeded by its length as a varint.
5262
5495
  * @returns {Uint8Array} Value read
@@ -5452,7 +5685,7 @@ function BufferReader(buffer) {
5452
5685
  * Read buffer.
5453
5686
  * @name BufferReader#buf
5454
5687
  * @type {Buffer}
5455
- */
5688
+ */
5456
5689
  }
5457
5690
 
5458
5691
  BufferReader._configure = function () {
@@ -5470,8 +5703,6 @@ BufferReader._configure = function () {
5470
5703
  * @returns {Buffer} Raw bytes
5471
5704
  */
5472
5705
  BufferReader.prototype.raw = function read_raw_buffer(start, end) {
5473
- if (start === end)
5474
- return util.Buffer.alloc(0);
5475
5706
  return this._slice.call(this.buf, start, end);
5476
5707
  };
5477
5708
 
@@ -7271,6 +7502,14 @@ Type.prototype.setup = function setup() {
7271
7502
  // Sets up everything at once so that the prototype chain does not have to be re-evaluated
7272
7503
  // multiple times (V8, soft-deopt prototype-check).
7273
7504
 
7505
+ // Resolve feature defaults incl. field presence before generating codecs
7506
+ var root = this.root;
7507
+ if (root && root._needsRecursiveFeatureResolution) {
7508
+ var edition = root._edition || this._edition;
7509
+ if (edition)
7510
+ root._resolveFeaturesRecursive(edition);
7511
+ }
7512
+
7274
7513
  var fullName = this.fullName,
7275
7514
  types = [];
7276
7515
  for (var i = 0; i < /* initializes */ this.fieldsArray.length; ++i)
@@ -7334,7 +7573,7 @@ Type.prototype.encode = function encode_setup(message, writer) { // eslint-disab
7334
7573
  * @returns {Writer} writer
7335
7574
  */
7336
7575
  Type.prototype.encodeDelimited = function encodeDelimited(message, writer) {
7337
- return this.encode(message, writer && writer.len ? writer.fork() : writer).ldelim();
7576
+ return this.encode(message, (writer || Writer.create()).fork()).ldelim();
7338
7577
  };
7339
7578
 
7340
7579
  /**
@@ -9124,36 +9363,29 @@ util.isSet = function isSet(obj, prop) {
9124
9363
  util.Buffer = (function() {
9125
9364
  try {
9126
9365
  var Buffer = util.global.Buffer;
9127
- // refuse to use non-node buffers if not explicitly assigned (perf reasons):
9128
- return Buffer.prototype.utf8Write ? Buffer : /* istanbul ignore next */ null;
9366
+ // refuse to use non-node buffers if not explicitly assigned (perf reasons)
9367
+ return Buffer.prototype.utf8Write || util.isNode ? Buffer : /* istanbul ignore next */ null;
9129
9368
  } catch (e) {
9130
9369
  /* istanbul ignore next */
9131
9370
  return null;
9132
9371
  }
9133
9372
  })();
9134
9373
 
9135
- // Internal alias of or polyfull for Buffer.from.
9136
- util._Buffer_from = null;
9137
-
9138
- // Internal alias of or polyfill for Buffer.allocUnsafe.
9139
- util._Buffer_allocUnsafe = null;
9140
-
9141
9374
  /**
9142
9375
  * Creates a new buffer of whatever type supported by the environment.
9143
9376
  * @param {number|number[]} [sizeOrArray=0] Buffer size or number array
9144
9377
  * @returns {Uint8Array|Buffer} Buffer
9145
9378
  */
9146
9379
  util.newBuffer = function newBuffer(sizeOrArray) {
9380
+ var Buffer = util.Buffer;
9147
9381
  /* istanbul ignore next */
9148
9382
  return typeof sizeOrArray === "number"
9149
- ? util.Buffer
9150
- ? util._Buffer_allocUnsafe(sizeOrArray)
9151
- : new util.Array(sizeOrArray)
9152
- : util.Buffer
9153
- ? util._Buffer_from(sizeOrArray)
9154
- : typeof Uint8Array === "undefined"
9155
- ? sizeOrArray
9156
- : new Uint8Array(sizeOrArray);
9383
+ ? Buffer
9384
+ ? Buffer.allocUnsafe(sizeOrArray)
9385
+ : new Uint8Array(sizeOrArray)
9386
+ : Buffer
9387
+ ? Buffer.from(sizeOrArray)
9388
+ : new Uint8Array(sizeOrArray);
9157
9389
  };
9158
9390
 
9159
9391
  /**
@@ -9179,10 +9411,11 @@ util.rawField = function rawField(id, wireType, data) {
9179
9411
  };
9180
9412
 
9181
9413
  /**
9182
- * Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
9414
+ * Array implementation used in the browser.
9183
9415
  * @type {Constructor<Uint8Array>}
9416
+ * @deprecated Use `Uint8Array` instead.
9184
9417
  */
9185
- util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : Array;
9418
+ util.Array = Uint8Array;
9186
9419
 
9187
9420
  /**
9188
9421
  * Any compatible Long instance.
@@ -9503,28 +9736,6 @@ util.toJSONOptions = {
9503
9736
  json: true
9504
9737
  };
9505
9738
 
9506
- // Sets up buffer utility according to the environment (called in index-minimal)
9507
- util._configure = function() {
9508
- var Buffer = util.Buffer;
9509
- /* istanbul ignore if */
9510
- if (!Buffer) {
9511
- util._Buffer_from = util._Buffer_allocUnsafe = null;
9512
- return;
9513
- }
9514
- // because node 4.x buffers are incompatible & immutable
9515
- // see: https://github.com/dcodeIO/protobuf.js/pull/665
9516
- util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
9517
- /* istanbul ignore next */
9518
- function Buffer_from(value, encoding) {
9519
- return new Buffer(value, encoding);
9520
- };
9521
- util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
9522
- /* istanbul ignore next */
9523
- function Buffer_allocUnsafe(size) {
9524
- return new Buffer(size);
9525
- };
9526
- };
9527
-
9528
9739
  },{"29":29,"30":30,"32":32,"34":34,"36":36,"40":40,"41":41,"long":"long"}],38:[function(require,module,exports){
9529
9740
  "use strict";
9530
9741
 
@@ -9690,19 +9901,21 @@ function pool(alloc, slice, size) {
9690
9901
  "use strict";
9691
9902
 
9692
9903
  /**
9693
- * A minimal UTF8 implementation for number arrays.
9904
+ * A minimal UTF8 implementation.
9694
9905
  * @memberof util
9695
9906
  * @namespace
9696
9907
  */
9697
9908
  var utf8 = exports,
9698
9909
  replacementChar = "\ufffd",
9910
+ looseDecoder = new TextDecoder("utf-8", { ignoreBOM: true }),
9699
9911
  strictDecoder;
9912
+ var TEXT_DECODER_MIN_LENGTH = 64;
9700
9913
 
9701
9914
  try {
9702
9915
  strictDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
9703
9916
  } catch (err) {
9704
9917
  // "fatal" option is not supported on Node.js compiled without ICU
9705
- strictDecoder = new TextDecoder("utf-8", { ignoreBOM: true });
9918
+ strictDecoder = looseDecoder;
9706
9919
  }
9707
9920
 
9708
9921
  /**
@@ -9753,6 +9966,13 @@ function utf8_read_js(buffer, start, end, str) {
9753
9966
  return str;
9754
9967
  }
9755
9968
 
9969
+ function utf8_read_decoder(decoder, buffer, start, end) {
9970
+ var source = start === 0 && end === buffer.length
9971
+ ? buffer
9972
+ : buffer.subarray(start, end);
9973
+ return decoder.decode(source);
9974
+ }
9975
+
9756
9976
  /**
9757
9977
  * Reads UTF8 bytes as a string.
9758
9978
  * @param {Uint8Array} buffer Source buffer
@@ -9760,9 +9980,11 @@ function utf8_read_js(buffer, start, end, str) {
9760
9980
  * @param {number} end Source end
9761
9981
  * @returns {string} String read
9762
9982
  */
9763
- utf8.read = function utf8_read_ascii(buffer, start, end) {
9983
+ utf8.read = function utf8_read_loose(buffer, start, end) {
9764
9984
  if (end - start < 1)
9765
9985
  return "";
9986
+ if (end - start >= TEXT_DECODER_MIN_LENGTH)
9987
+ return utf8_read_decoder(looseDecoder, buffer, start, end);
9766
9988
 
9767
9989
  var str = "",
9768
9990
  i = start,
@@ -9792,17 +10014,6 @@ utf8.read = function utf8_read_ascii(buffer, start, end) {
9792
10014
  return str;
9793
10015
  };
9794
10016
 
9795
- function utf8_read_strict(buffer, start, end) {
9796
- var source = start === 0 && end === buffer.length
9797
- ? buffer
9798
- : buffer.subarray
9799
- ? buffer.subarray(start, end)
9800
- : buffer.slice(start, end);
9801
- if (Array.isArray(source))
9802
- source = Uint8Array.from(source);
9803
- return strictDecoder.decode(source);
9804
- }
9805
-
9806
10017
  /**
9807
10018
  * Reads UTF8 bytes as a string, rejecting invalid UTF8.
9808
10019
  * @param {Uint8Array} buffer Source buffer
@@ -9810,9 +10021,11 @@ function utf8_read_strict(buffer, start, end) {
9810
10021
  * @param {number} end Source end
9811
10022
  * @returns {string} String read
9812
10023
  */
9813
- utf8.readStrict = function utf8_read_strict_ascii(buffer, start, end) {
10024
+ utf8.readStrict = function utf8_read_strict(buffer, start, end) {
9814
10025
  if (end - start < 1)
9815
10026
  return "";
10027
+ if (end - start >= TEXT_DECODER_MIN_LENGTH)
10028
+ return utf8_read_decoder(strictDecoder, buffer, start, end);
9816
10029
 
9817
10030
  var str = "",
9818
10031
  i = start,
@@ -9828,14 +10041,14 @@ utf8.readStrict = function utf8_read_strict_ascii(buffer, start, end) {
9828
10041
  c7 = buffer[i + 6];
9829
10042
  c8 = buffer[i + 7];
9830
10043
  if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80)
9831
- return str + utf8_read_strict(buffer, i, end);
10044
+ return str + utf8_read_decoder(strictDecoder, buffer, i, end);
9832
10045
  str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
9833
10046
  }
9834
10047
 
9835
10048
  for (; i < end; ++i) {
9836
10049
  c1 = buffer[i];
9837
10050
  if (c1 & 0x80)
9838
- return str + utf8_read_strict(buffer, i, end);
10051
+ return str + utf8_read_decoder(strictDecoder, buffer, i, end);
9839
10052
  str += String.fromCharCode(c1);
9840
10053
  }
9841
10054
 
@@ -10183,118 +10396,52 @@ var LongBits = util.LongBits,
10183
10396
  base64 = util.base64,
10184
10397
  utf8 = util.utf8;
10185
10398
 
10186
- /**
10187
- * Constructs a new writer operation instance.
10188
- * @classdesc Scheduled writer operation.
10189
- * @constructor
10190
- * @param {function(*, Uint8Array, number)} fn Function to call
10191
- * @param {number} len Value byte length
10192
- * @param {*} val Value to write
10193
- * @ignore
10194
- */
10195
- function Op(fn, len, val) {
10196
-
10197
- /**
10198
- * Function to call.
10199
- * @type {function(Uint8Array, number, *)}
10200
- */
10201
- this.fn = fn;
10202
-
10203
- /**
10204
- * Value byte length.
10205
- * @type {number}
10206
- */
10207
- this.len = len;
10208
-
10209
- /**
10210
- * Next operation.
10211
- * @type {Writer.Op|undefined}
10212
- */
10213
- this.next = undefined;
10214
-
10215
- /**
10216
- * Value to write.
10217
- * @type {*}
10218
- */
10219
- this.val = val; // type varies
10220
- }
10221
-
10222
- /* istanbul ignore next */
10223
- function noop() {} // eslint-disable-line no-empty-function
10224
-
10225
- /**
10226
- * Constructs a new writer state instance.
10227
- * @classdesc Copied writer state.
10228
- * @memberof Writer
10229
- * @constructor
10230
- * @param {Writer} writer Writer to copy state from
10231
- * @ignore
10232
- */
10233
- function State(writer) {
10234
-
10235
- /**
10236
- * Current head.
10237
- * @type {Writer.Op}
10238
- */
10239
- this.head = writer.head;
10240
-
10241
- /**
10242
- * Current tail.
10243
- * @type {Writer.Op}
10244
- */
10245
- this.tail = writer.tail;
10246
-
10247
- /**
10248
- * Current buffer length.
10249
- * @type {number}
10250
- */
10251
- this.len = writer.len;
10252
-
10253
- /**
10254
- * Next state.
10255
- * @type {State|null}
10256
- */
10257
- this.next = writer.states;
10258
- }
10259
-
10260
10399
  /**
10261
10400
  * Constructs a new writer instance.
10262
- * @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
10401
+ * @classdesc Wire format writer using `Uint8Array`.
10263
10402
  * @constructor
10264
10403
  */
10265
10404
  function Writer() {
10266
10405
 
10267
10406
  /**
10268
- * Current length.
10407
+ * Write cursor into {@link Writer#buf}.
10269
10408
  * @type {number}
10270
10409
  */
10271
- this.len = 0;
10410
+ this.pos = 0;
10272
10411
 
10273
10412
  /**
10274
- * Operations head.
10275
- * @type {Object}
10413
+ * Backing buffer.
10414
+ * @type {Uint8Array}
10276
10415
  */
10277
- this.head = new Op(noop, 0, 0);
10416
+ this.buf = this.constructor.alloc(64);
10278
10417
 
10279
10418
  /**
10280
- * Operations tail
10281
- * @type {Object}
10419
+ * Cached DataView over {@link Writer#buf}.
10420
+ * @type {DataView|null}
10282
10421
  */
10283
- this.tail = this.head;
10422
+ this.view = null;
10284
10423
 
10285
10424
  /**
10286
- * Linked forked states.
10287
- * @type {Object|null}
10425
+ * Stack of forked length-prefix positions.
10426
+ * @type {Array<number>|null}
10288
10427
  */
10289
10428
  this.states = null;
10290
-
10291
- // When a value is written, the writer calculates its byte length and puts it into a linked
10292
- // list of operations to perform when finish() is called. This both allows us to allocate
10293
- // buffers of the exact required size and reduces the amount of work we have to do compared
10294
- // to first calculating over objects and then encoding over objects. In our case, the encoding
10295
- // part is just a linked list walk calling operations with already prepared values.
10296
10429
  }
10297
10430
 
10431
+ /**
10432
+ * Current write position.
10433
+ * @name Writer#len
10434
+ * @type {number}
10435
+ * @deprecated Use {@link Writer#pos} instead.
10436
+ */
10437
+ Object.defineProperty(Writer.prototype, "len", {
10438
+ configurable: true,
10439
+ enumerable: true,
10440
+ get: function get_len() {
10441
+ return this.pos;
10442
+ }
10443
+ });
10444
+
10298
10445
  var create = function create() {
10299
10446
  return util.Buffer
10300
10447
  ? function create_buffer_setup() {
@@ -10321,32 +10468,45 @@ Writer.create = create();
10321
10468
  * @returns {Uint8Array} Buffer
10322
10469
  */
10323
10470
  Writer.alloc = function alloc(size) {
10324
- return new util.Array(size);
10471
+ return new Uint8Array(size);
10325
10472
  };
10326
10473
 
10327
10474
  // Use Uint8Array buffer pool in the browser, just like node does with buffers
10328
- /* istanbul ignore else */
10329
- if (util.Array !== Array)
10330
- Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
10475
+ Writer.alloc = util.pool(Writer.alloc, Uint8Array.prototype.subarray);
10331
10476
 
10332
10477
  /**
10333
- * Pushes a new operation to the queue.
10334
- * @param {function(Uint8Array, number, *)} fn Function to call
10335
- * @param {number} len Value byte length
10336
- * @param {number} val Value to write
10337
- * @returns {Writer} `this`
10478
+ * Calculates the number of bytes a value occupies as a varint.
10479
+ * @param {number} value Value to size (unsigned)
10480
+ * @returns {number} Byte length (1..5)
10481
+ * @ignore
10482
+ */
10483
+ function sizeVarint32(value) {
10484
+ return value < 128 ? 1
10485
+ : value < 16384 ? 2
10486
+ : value < 2097152 ? 3
10487
+ : value < 268435456 ? 4
10488
+ : 5;
10489
+ }
10490
+
10491
+ /**
10492
+ * Ensures that at least `n` more bytes fit into the backing buffer, doubling it if not.
10493
+ * @param {number} n Number of additional bytes required
10494
+ * @returns {undefined}
10338
10495
  * @private
10339
10496
  */
10340
- Writer.prototype._push = function push(fn, len, val) {
10341
- this.tail = this.tail.next = new Op(fn, len, val);
10342
- this.len += len;
10343
- return this;
10497
+ Writer.prototype._reserve = function _reserve(n) {
10498
+ var need = this.pos + n;
10499
+ if (need > this.buf.length) {
10500
+ var size = this.buf.length << 1;
10501
+ if (size < need)
10502
+ size = need;
10503
+ var buf = this.constructor.alloc(size);
10504
+ buf.set(this.buf.subarray(0, this.pos), 0);
10505
+ this.buf = buf;
10506
+ this.view = null; // invalidate
10507
+ }
10344
10508
  };
10345
10509
 
10346
- function writeByte(val, buf, pos) {
10347
- buf[pos] = val & 255;
10348
- }
10349
-
10350
10510
  function writeStringAscii(val, buf, pos) {
10351
10511
  for (var i = 0; i < val.length;)
10352
10512
  buf[pos++] = val.charCodeAt(i++);
@@ -10358,42 +10518,19 @@ function writeVarint32(val, buf, pos) {
10358
10518
  val >>>= 7;
10359
10519
  }
10360
10520
  buf[pos] = val;
10521
+ return pos + 1;
10361
10522
  }
10362
10523
 
10363
- /**
10364
- * Constructs a new varint writer operation instance.
10365
- * @classdesc Scheduled varint writer operation.
10366
- * @extends Op
10367
- * @constructor
10368
- * @param {number} len Value byte length
10369
- * @param {number} val Value to write
10370
- * @ignore
10371
- */
10372
- function VarintOp(len, val) {
10373
- this.len = len;
10374
- this.next = undefined;
10375
- this.val = val;
10376
- }
10377
-
10378
- VarintOp.prototype = Object.create(Op.prototype);
10379
- VarintOp.prototype.fn = writeVarint32;
10380
-
10381
10524
  /**
10382
10525
  * Writes an unsigned 32 bit value as a varint.
10383
10526
  * @param {number} value Value to write
10384
10527
  * @returns {Writer} `this`
10385
10528
  */
10386
10529
  Writer.prototype.uint32 = function write_uint32(value) {
10387
- // here, the call to this.push has been inlined and a varint specific Op subclass is used.
10388
- // uint32 is by far the most frequently used operation and benefits significantly from this.
10389
- this.len += (this.tail = this.tail.next = new VarintOp(
10390
- (value = value >>> 0)
10391
- < 128 ? 1
10392
- : value < 16384 ? 2
10393
- : value < 2097152 ? 3
10394
- : value < 268435456 ? 4
10395
- : 5,
10396
- value)).len;
10530
+ value = value >>> 0;
10531
+ this._reserve(5);
10532
+ var pos = this.pos;
10533
+ this.pos = writeVarint32(value, this.buf, pos);
10397
10534
  return this;
10398
10535
  };
10399
10536
 
@@ -10404,9 +10541,13 @@ Writer.prototype.uint32 = function write_uint32(value) {
10404
10541
  * @returns {Writer} `this`
10405
10542
  */
10406
10543
  Writer.prototype.int32 = function write_int32(value) {
10407
- return (value |= 0) < 0
10408
- ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
10409
- : this.uint32(value);
10544
+ if ((value |= 0) < 0) { // 10 bytes per spec
10545
+ this._reserve(10);
10546
+ writeVarint64(LongBits.fromNumber(value), this.buf, this.pos);
10547
+ this.pos += 10;
10548
+ return this;
10549
+ }
10550
+ return this.uint32(value);
10410
10551
  };
10411
10552
 
10412
10553
  /**
@@ -10430,7 +10571,8 @@ function writeVarint64(val, buf, pos) {
10430
10571
  buf[pos++] = lo & 127 | 128;
10431
10572
  lo = lo >>> 7;
10432
10573
  }
10433
- buf[pos++] = lo;
10574
+ buf[pos] = lo;
10575
+ return pos + 1;
10434
10576
  }
10435
10577
 
10436
10578
  /**
@@ -10441,7 +10583,10 @@ function writeVarint64(val, buf, pos) {
10441
10583
  */
10442
10584
  Writer.prototype.uint64 = function write_uint64(value) {
10443
10585
  var bits = LongBits.from(value);
10444
- return this._push(writeVarint64, bits.length(), bits);
10586
+ this._reserve(10);
10587
+ var pos = this.pos;
10588
+ this.pos = writeVarint64(bits, this.buf, pos);
10589
+ return this;
10445
10590
  };
10446
10591
 
10447
10592
  /**
@@ -10461,7 +10606,10 @@ Writer.prototype.int64 = Writer.prototype.uint64;
10461
10606
  */
10462
10607
  Writer.prototype.sint64 = function write_sint64(value) {
10463
10608
  var bits = LongBits.from(value).zzEncode();
10464
- return this._push(writeVarint64, bits.length(), bits);
10609
+ this._reserve(10);
10610
+ var pos = this.pos;
10611
+ this.pos = writeVarint64(bits, this.buf, pos);
10612
+ return this;
10465
10613
  };
10466
10614
 
10467
10615
  /**
@@ -10470,7 +10618,9 @@ Writer.prototype.sint64 = function write_sint64(value) {
10470
10618
  * @returns {Writer} `this`
10471
10619
  */
10472
10620
  Writer.prototype.bool = function write_bool(value) {
10473
- return this._push(writeByte, 1, value ? 1 : 0);
10621
+ this._reserve(1);
10622
+ this.buf[this.pos++] = value ? 1 : 0;
10623
+ return this;
10474
10624
  };
10475
10625
 
10476
10626
  function writeFixed32(val, buf, pos) {
@@ -10486,7 +10636,10 @@ function writeFixed32(val, buf, pos) {
10486
10636
  * @returns {Writer} `this`
10487
10637
  */
10488
10638
  Writer.prototype.fixed32 = function write_fixed32(value) {
10489
- return this._push(writeFixed32, 4, value >>> 0);
10639
+ this._reserve(4);
10640
+ writeFixed32(value >>> 0, this.buf, this.pos);
10641
+ this.pos += 4;
10642
+ return this;
10490
10643
  };
10491
10644
 
10492
10645
  /**
@@ -10505,7 +10658,11 @@ Writer.prototype.sfixed32 = Writer.prototype.fixed32;
10505
10658
  */
10506
10659
  Writer.prototype.fixed64 = function write_fixed64(value) {
10507
10660
  var bits = LongBits.from(value);
10508
- return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
10661
+ this._reserve(8);
10662
+ writeFixed32(bits.lo, this.buf, this.pos);
10663
+ writeFixed32(bits.hi, this.buf, this.pos + 4);
10664
+ this.pos += 8;
10665
+ return this;
10509
10666
  };
10510
10667
 
10511
10668
  /**
@@ -10524,7 +10681,10 @@ Writer.prototype.sfixed64 = Writer.prototype.fixed64;
10524
10681
  * @returns {Writer} `this`
10525
10682
  */
10526
10683
  Writer.prototype.float = function write_float(value) {
10527
- return this._push(util.float.writeFloatLE, 4, value);
10684
+ this._reserve(4);
10685
+ util.float.writeFloatLE(value, this.buf, this.pos);
10686
+ this.pos += 4;
10687
+ return this;
10528
10688
  };
10529
10689
 
10530
10690
  /**
@@ -10534,19 +10694,12 @@ Writer.prototype.float = function write_float(value) {
10534
10694
  * @returns {Writer} `this`
10535
10695
  */
10536
10696
  Writer.prototype.double = function write_double(value) {
10537
- return this._push(util.float.writeDoubleLE, 8, value);
10697
+ this._reserve(8);
10698
+ util.float.writeDoubleLE(value, this.buf, this.pos);
10699
+ this.pos += 8;
10700
+ return this;
10538
10701
  };
10539
10702
 
10540
- var writeBytes = util.Array.prototype.set
10541
- ? function writeBytes_set(val, buf, pos) {
10542
- buf.set(val, pos); // also works for plain array values
10543
- }
10544
- /* istanbul ignore next */
10545
- : function writeBytes_for(val, buf, pos) {
10546
- for (var i = 0; i < val.length; ++i)
10547
- buf[pos + i] = val[i];
10548
- };
10549
-
10550
10703
  /**
10551
10704
  * Writes a sequence of bytes.
10552
10705
  * @param {Uint8Array|string} value Buffer or base64 encoded string to write
@@ -10554,14 +10707,21 @@ var writeBytes = util.Array.prototype.set
10554
10707
  */
10555
10708
  Writer.prototype.bytes = function write_bytes(value) {
10556
10709
  var len = value.length >>> 0;
10557
- if (!len)
10558
- return this._push(writeByte, 1, 0);
10710
+ if (!len) {
10711
+ this._reserve(1);
10712
+ this.buf[this.pos++] = 0;
10713
+ return this;
10714
+ }
10559
10715
  if (util.isString(value)) {
10560
10716
  var buf = Writer.alloc(len = base64.length(value));
10561
10717
  base64.decode(value, buf, 0);
10562
10718
  value = buf;
10563
10719
  }
10564
- return this.uint32(len)._push(writeBytes, len, value);
10720
+ this.uint32(len);
10721
+ this._reserve(len);
10722
+ this.buf.set(value, this.pos);
10723
+ this.pos += len;
10724
+ return this;
10565
10725
  };
10566
10726
 
10567
10727
  /**
@@ -10571,7 +10731,28 @@ Writer.prototype.bytes = function write_bytes(value) {
10571
10731
  */
10572
10732
  Writer.prototype.raw = function write_raw(value) {
10573
10733
  var len = value.length >>> 0;
10574
- return len ? this._push(writeBytes, len, value) : this;
10734
+ if (!len)
10735
+ return this;
10736
+ this._reserve(len);
10737
+ this.buf.set(value, this.pos);
10738
+ this.pos += len;
10739
+ return this;
10740
+ };
10741
+
10742
+ /**
10743
+ * Backfills the length varint.
10744
+ * @param {number} pos Position of reserved length byte
10745
+ * @param {number} len Length of content after length varint
10746
+ * @returns {Writer} `this`
10747
+ * @private
10748
+ */
10749
+ Writer.prototype._delim = function _delim(pos, len) {
10750
+ var n = sizeVarint32(len);
10751
+ if (n > 1)
10752
+ this.buf.copyWithin(pos + n, pos + 1, pos + 1 + len);
10753
+ writeVarint32(len, this.buf, pos);
10754
+ this.pos = pos + n + len;
10755
+ return this;
10575
10756
  };
10576
10757
 
10577
10758
  /**
@@ -10580,21 +10761,245 @@ Writer.prototype.raw = function write_raw(value) {
10580
10761
  * @returns {Writer} `this`
10581
10762
  */
10582
10763
  Writer.prototype.string = function write_string(value) {
10764
+ var n = value.length;
10765
+ if (!n) {
10766
+ this._reserve(1);
10767
+ this.buf[this.pos++] = 0;
10768
+ return this;
10769
+ }
10770
+ if (n < 0x80) {
10771
+ this._reserve(n * 3 + 5); // worst case
10772
+ var lenPos = this.pos;
10773
+ return this._delim(lenPos, utf8.write(value, this.buf, lenPos + 1));
10774
+ }
10583
10775
  var len = utf8.length(value);
10584
- return len
10585
- ? this.uint32(len)._push(len === value.length ? writeStringAscii : utf8.write, len, value)
10586
- : this._push(writeByte, 1, 0);
10776
+ this.uint32(len);
10777
+ this._reserve(len);
10778
+ if (len === value.length)
10779
+ writeStringAscii(value, this.buf, this.pos);
10780
+ else
10781
+ utf8.write(value, this.buf, this.pos);
10782
+ this.pos += len;
10783
+ return this;
10784
+ };
10785
+
10786
+ /**
10787
+ * Writes an array of unsigned 32 bit values as a packed repeated field.
10788
+ * @param {number[]} value Values to write
10789
+ * @returns {Writer} `this`
10790
+ */
10791
+ Writer.prototype.uint32s = function write_uint32s(value) {
10792
+ var n = value.length;
10793
+ this._reserve(n * 5 + 5); // worst case: 5 bytes per value + length varint
10794
+ var buf = this.buf, lenPos = this.pos, p = lenPos + 1;
10795
+ for (var i = 0; i < n; ++i)
10796
+ p = writeVarint32(value[i] >>> 0, buf, p);
10797
+ return this._delim(lenPos, p - lenPos - 1);
10798
+ };
10799
+
10800
+ /**
10801
+ * Writes an array of signed 32 bit values as a packed repeated field.
10802
+ * @param {number[]} value Values to write
10803
+ * @returns {Writer} `this`
10804
+ */
10805
+ Writer.prototype.int32s = function write_int32s(value) {
10806
+ var n = value.length;
10807
+ this._reserve(n * 10 + 5); // worst case: 10 bytes per negative value + length varint
10808
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1, val;
10809
+ for (var i = 0; i < n; ++i) {
10810
+ if ((val = value[i] | 0) < 0) { // negatives are 10 bytes per spec
10811
+ pos = writeVarint64(LongBits.fromNumber(val), buf, pos);
10812
+ } else {
10813
+ pos = writeVarint32(val, buf, pos);
10814
+ }
10815
+ }
10816
+ return this._delim(lenPos, pos - lenPos - 1);
10817
+ };
10818
+
10819
+ /**
10820
+ * Writes an array of 32 bit values as packed, zig-zag encoded varints.
10821
+ * @param {number[]} value Values to write
10822
+ * @returns {Writer} `this`
10823
+ */
10824
+ Writer.prototype.sint32s = function write_sint32s(value) {
10825
+ var n = value.length;
10826
+ this._reserve(n * 5 + 5);
10827
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
10828
+ for (var i = 0; i < n; ++i)
10829
+ pos = writeVarint32((value[i] << 1 ^ value[i] >> 31) >>> 0, buf, pos);
10830
+ return this._delim(lenPos, pos - lenPos - 1);
10831
+ };
10832
+
10833
+ /**
10834
+ * Writes an array of unsigned 64 bit values as a packed repeated field.
10835
+ * @param {Array.<Long|number|string>} value Values to write
10836
+ * @returns {Writer} `this`
10837
+ */
10838
+ Writer.prototype.uint64s = function write_uint64s(value) {
10839
+ var n = value.length;
10840
+ this._reserve(n * 10 + 5);
10841
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
10842
+ for (var i = 0; i < n; ++i) {
10843
+ pos = writeVarint64(LongBits.from(value[i]), buf, pos);
10844
+ }
10845
+ return this._delim(lenPos, pos - lenPos - 1);
10846
+ };
10847
+
10848
+ /**
10849
+ * Writes an array of signed 64 bit values as a packed repeated field.
10850
+ * @function
10851
+ * @param {Array.<Long|number|string>} value Values to write
10852
+ * @returns {Writer} `this`
10853
+ */
10854
+ Writer.prototype.int64s = Writer.prototype.uint64s;
10855
+
10856
+ /**
10857
+ * Writes an array of 64 bit values as packed, zig-zag encoded varints.
10858
+ * @param {Array.<Long|number|string>} value Values to write
10859
+ * @returns {Writer} `this`
10860
+ */
10861
+ Writer.prototype.sint64s = function write_sint64s(value) {
10862
+ var n = value.length;
10863
+ this._reserve(n * 10 + 5);
10864
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
10865
+ for (var i = 0; i < n; ++i) {
10866
+ pos = writeVarint64(LongBits.from(value[i]).zzEncode(), buf, pos);
10867
+ }
10868
+ return this._delim(lenPos, pos - lenPos - 1);
10869
+ };
10870
+
10871
+ /**
10872
+ * Writes an array of boolish values as a packed repeated field.
10873
+ * @param {boolean[]} value Values to write
10874
+ * @returns {Writer} `this`
10875
+ */
10876
+ Writer.prototype.bools = function write_bools(value) {
10877
+ var n = value.length;
10878
+ this.uint32(n); // one byte per value
10879
+ this._reserve(n);
10880
+ var buf = this.buf, p = this.pos;
10881
+ for (var i = 0; i < n; ++i)
10882
+ buf[p++] = value[i] ? 1 : 0;
10883
+ this.pos += n;
10884
+ return this;
10885
+ };
10886
+
10887
+ // The view allocation only pays off when amortized over enough writes
10888
+ var VIEW_THRESHOLD_FLOAT = 16,
10889
+ VIEW_THRESHOLD_INT = 128;
10890
+
10891
+ function getLazyView(writer, count, threshold) {
10892
+ var view = writer.view;
10893
+ if (view || count < threshold)
10894
+ return view;
10895
+ var buf = writer.buf;
10896
+ return writer.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
10897
+ }
10898
+
10899
+ /**
10900
+ * Writes an array of unsigned 32 bit values as packed, fixed 32 bits.
10901
+ * @param {number[]} value Values to write
10902
+ * @returns {Writer} `this`
10903
+ */
10904
+ Writer.prototype.fixed32s = function write_fixed32s(value) {
10905
+ var n = value.length, bytes = n * 4;
10906
+ this.uint32(bytes); // length is known exactly
10907
+ this._reserve(bytes);
10908
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_INT);
10909
+ if (dv)
10910
+ for (i = 0; i < n; ++i) { dv.setUint32(p, value[i] >>> 0, true); p += 4; }
10911
+ else {
10912
+ var buf = this.buf;
10913
+ for (i = 0; i < n; ++i) { writeFixed32(value[i] >>> 0, buf, p); p += 4; }
10914
+ }
10915
+ this.pos += bytes;
10916
+ return this;
10917
+ };
10918
+
10919
+ /**
10920
+ * Writes an array of signed 32 bit values as packed, fixed 32 bits.
10921
+ * @function
10922
+ * @param {number[]} value Values to write
10923
+ * @returns {Writer} `this`
10924
+ */
10925
+ Writer.prototype.sfixed32s = Writer.prototype.fixed32s;
10926
+
10927
+ /**
10928
+ * Writes an array of unsigned 64 bit values as packed, fixed 64 bits.
10929
+ * @param {Array.<Long|number|string>} value Values to write
10930
+ * @returns {Writer} `this`
10931
+ */
10932
+ Writer.prototype.fixed64s = function write_fixed64s(value) {
10933
+ var n = value.length, bytes = n * 8;
10934
+ this.uint32(bytes);
10935
+ this._reserve(bytes);
10936
+ var p = this.pos, i, bits, dv = getLazyView(this, n, VIEW_THRESHOLD_INT);
10937
+ if (dv)
10938
+ 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; }
10939
+ else {
10940
+ var buf = this.buf;
10941
+ for (i = 0; i < n; ++i) { bits = LongBits.from(value[i]); writeFixed32(bits.lo, buf, p); writeFixed32(bits.hi, buf, p + 4); p += 8; }
10942
+ }
10943
+ this.pos += bytes;
10944
+ return this;
10945
+ };
10946
+
10947
+ /**
10948
+ * Writes an array of signed 64 bit values as packed, fixed 64 bits.
10949
+ * @function
10950
+ * @param {Array.<Long|number|string>} value Values to write
10951
+ * @returns {Writer} `this`
10952
+ */
10953
+ Writer.prototype.sfixed64s = Writer.prototype.fixed64s;
10954
+
10955
+ /**
10956
+ * Writes an array of floats (32 bit) as a packed repeated field.
10957
+ * @param {number[]} value Values to write
10958
+ * @returns {Writer} `this`
10959
+ */
10960
+ Writer.prototype.floats = function write_floats(value) {
10961
+ var n = value.length, bytes = n * 4;
10962
+ this.uint32(bytes);
10963
+ this._reserve(bytes);
10964
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_FLOAT);
10965
+ if (dv)
10966
+ for (i = 0; i < n; ++i) { dv.setFloat32(p, value[i], true); p += 4; }
10967
+ else {
10968
+ var buf = this.buf;
10969
+ for (i = 0; i < n; ++i) { util.float.writeFloatLE(value[i], buf, p); p += 4; }
10970
+ }
10971
+ this.pos += bytes;
10972
+ return this;
10973
+ };
10974
+
10975
+ /**
10976
+ * Writes an array of doubles (64 bit float) as a packed repeated field.
10977
+ * @param {number[]} value Values to write
10978
+ * @returns {Writer} `this`
10979
+ */
10980
+ Writer.prototype.doubles = function write_doubles(value) {
10981
+ var n = value.length, bytes = n * 8;
10982
+ this.uint32(bytes);
10983
+ this._reserve(bytes);
10984
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_FLOAT);
10985
+ if (dv)
10986
+ for (i = 0; i < n; ++i) { dv.setFloat64(p, value[i], true); p += 8; }
10987
+ else {
10988
+ var buf = this.buf;
10989
+ for (i = 0; i < n; ++i) { util.float.writeDoubleLE(value[i], buf, p); p += 8; }
10990
+ }
10991
+ this.pos += bytes;
10992
+ return this;
10587
10993
  };
10588
10994
 
10589
10995
  /**
10590
10996
  * Forks this writer's state by pushing it to a stack.
10591
- * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
10592
10997
  * @returns {Writer} `this`
10593
10998
  */
10594
10999
  Writer.prototype.fork = function fork() {
10595
- this.states = new State(this);
10596
- this.head = this.tail = new Op(noop, 0, 0);
10597
- this.len = 0;
11000
+ this._reserve(1);
11001
+ (this.states || (this.states = [])).push(this.pos);
11002
+ this.pos += 1;
10598
11003
  return this;
10599
11004
  };
10600
11005
 
@@ -10603,47 +11008,66 @@ Writer.prototype.fork = function fork() {
10603
11008
  * @returns {Writer} `this`
10604
11009
  */
10605
11010
  Writer.prototype.reset = function reset() {
10606
- if (this.states) {
10607
- this.head = this.states.head;
10608
- this.tail = this.states.tail;
10609
- this.len = this.states.len;
10610
- this.states = this.states.next;
11011
+ var states = this.states;
11012
+ if (states && states.length) {
11013
+ this.pos = states.pop();
10611
11014
  } else {
10612
- this.head = this.tail = new Op(noop, 0, 0);
10613
- this.len = 0;
11015
+ this.pos = 0;
10614
11016
  }
10615
11017
  return this;
10616
11018
  };
10617
11019
 
10618
11020
  /**
10619
- * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
11021
+ * Resets to the last state and prepends the fork state's current write length as a varint.
10620
11022
  * @returns {Writer} `this`
10621
11023
  */
10622
11024
  Writer.prototype.ldelim = function ldelim() {
10623
- var head = this.head,
10624
- tail = this.tail,
10625
- len = this.len;
10626
- this.reset().uint32(len);
10627
- if (len) {
10628
- this.tail.next = head.next; // skip noop
10629
- this.tail = tail;
10630
- this.len += len;
11025
+ var states = this.states,
11026
+ len,
11027
+ vlen;
11028
+ if (states && states.length) {
11029
+ var lenPos = states.pop();
11030
+ len = this.pos - lenPos - 1;
11031
+ vlen = sizeVarint32(len);
11032
+ if (vlen > 1) { // grow the reserved single byte and shift content forward
11033
+ this._reserve(vlen - 1);
11034
+ this.buf.copyWithin(lenPos + vlen, lenPos + 1, lenPos + 1 + len);
11035
+ this.pos += vlen - 1;
11036
+ writeVarint32(len, this.buf, lenPos);
11037
+ } else {
11038
+ this.buf[lenPos] = len;
11039
+ }
11040
+ } else { // not forked: prefix the entire buffer with its length
11041
+ // TODO: Compatibility with older generated code.
11042
+ // Remove this branch in the next major release.
11043
+ len = this.pos;
11044
+ vlen = sizeVarint32(len);
11045
+ this._reserve(vlen);
11046
+ this.buf.copyWithin(vlen, 0, len);
11047
+ writeVarint32(len, this.buf, 0);
11048
+ this.pos += vlen;
10631
11049
  }
10632
11050
  return this;
10633
11051
  };
10634
11052
 
10635
11053
  /**
10636
11054
  * Finishes the write operation.
11055
+ * Returns a buffer sized to the written data by default.
11056
+ * @param {boolean} [shared=false] Whether to return a shared view instead of a unique copy
10637
11057
  * @returns {Uint8Array} Finished buffer
10638
11058
  */
10639
- Writer.prototype.finish = function finish() {
10640
- return this.finishInto(this.constructor.alloc(this.len), 0);
11059
+ Writer.prototype.finish = function finish(shared) {
11060
+ if (shared)
11061
+ return this.buf.subarray(0, this.pos);
11062
+ var buf = this.constructor.alloc(this.pos);
11063
+ buf.set(this.buf.subarray(0, this.pos), 0);
11064
+ return buf;
10641
11065
  };
10642
11066
 
10643
11067
  /**
10644
11068
  * Finishes the write operation, writing into the provided buffer.
10645
11069
  * The caller must ensure that `buf` has enough space starting at `offset`
10646
- * to hold {@link Writer#len} bytes.
11070
+ * to hold {@link Writer#pos} bytes.
10647
11071
  * @param {T} buf Target buffer
10648
11072
  * @param {number} [offset=0] Offset to start writing at
10649
11073
  * @returns {T} The provided buffer
@@ -10652,13 +11076,7 @@ Writer.prototype.finish = function finish() {
10652
11076
  Writer.prototype.finishInto = function finishInto(buf, offset) {
10653
11077
  if (offset === undefined)
10654
11078
  offset = 0;
10655
- var head = this.head.next,
10656
- pos = offset;
10657
- while (head) {
10658
- head.fn(head.val, buf, pos);
10659
- pos += head.len;
10660
- head = head.next;
10661
- }
11079
+ buf.set(this.buf.subarray(0, this.pos), offset);
10662
11080
  return buf;
10663
11081
  };
10664
11082
 
@@ -10695,6 +11113,8 @@ function BufferWriter() {
10695
11113
  Writer.call(this);
10696
11114
  }
10697
11115
 
11116
+ var writeStringBuffer;
11117
+
10698
11118
  BufferWriter._configure = function () {
10699
11119
  /**
10700
11120
  * Allocates a buffer of the specified size.
@@ -10702,19 +11122,14 @@ BufferWriter._configure = function () {
10702
11122
  * @param {number} size Buffer size
10703
11123
  * @returns {Buffer} Buffer
10704
11124
  */
10705
- BufferWriter.alloc = util._Buffer_allocUnsafe;
11125
+ BufferWriter.alloc = util.Buffer && util.Buffer.allocUnsafe;
10706
11126
 
10707
- BufferWriter.writeBytesBuffer = util.Buffer && util.Buffer.prototype instanceof Uint8Array && util.Buffer.prototype.set.name === "set"
10708
- ? function writeBytesBuffer_set(val, buf, pos) {
10709
- buf.set(val, pos); // faster than copy (requires node >= 4 where Buffers extend Uint8Array and set is properly inherited)
10710
- // also works for plain array values
11127
+ writeStringBuffer = util.Buffer && util.Buffer.prototype.utf8Write
11128
+ ? function writeStringBuffer_utf8Write(val, buf, pos) {
11129
+ return buf.utf8Write(val, pos);
10711
11130
  }
10712
- /* istanbul ignore next */
10713
- : function writeBytesBuffer_copy(val, buf, pos) {
10714
- if (val.copy) // Buffer values
10715
- val.copy(buf, pos, 0, val.length);
10716
- else for (var i = 0; i < val.length;) // plain array values
10717
- buf[pos++] = val[i++];
11131
+ : function writeStringBuffer_write(val, buf, pos) {
11132
+ return buf.write(val, pos);
10718
11133
  };
10719
11134
  };
10720
11135
 
@@ -10724,48 +11139,42 @@ BufferWriter._configure = function () {
10724
11139
  */
10725
11140
  BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
10726
11141
  if (util.isString(value))
10727
- value = util._Buffer_from(value, "base64");
11142
+ value = util.Buffer.from(value, "base64");
10728
11143
  var len = value.length >>> 0;
10729
11144
  this.uint32(len);
10730
- if (len)
10731
- this._push(BufferWriter.writeBytesBuffer, len, value);
11145
+ if (len) {
11146
+ this._reserve(len);
11147
+ this.buf.set(value, this.pos);
11148
+ this.pos += len;
11149
+ }
10732
11150
  return this;
10733
11151
  };
10734
11152
 
10735
- /**
10736
- * Writes raw bytes without a tag or length prefix.
10737
- * @name BufferWriter#raw
10738
- * @function
10739
- * @param {Uint8Array} value Raw bytes
10740
- * @returns {BufferWriter} `this`
10741
- */
10742
- BufferWriter.prototype.raw = function write_raw_buffer(value) {
10743
- var len = value.length >>> 0;
10744
- return len ? this._push(BufferWriter.writeBytesBuffer, len, value) : this;
10745
- };
10746
-
10747
- function writeStringBufferAscii(val, buf, pos) {
10748
- for (var i = 0; i < val.length;)
10749
- buf[pos++] = val.charCodeAt(i++);
10750
- }
10751
-
10752
- function writeStringBuffer(val, buf, pos) {
10753
- if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
10754
- util.utf8.write(val, buf, pos);
10755
- else if (buf.utf8Write)
10756
- buf.utf8Write(val, pos);
10757
- else
10758
- buf.write(val, pos);
10759
- }
10760
-
10761
11153
  /**
10762
11154
  * @override
10763
11155
  */
10764
11156
  BufferWriter.prototype.string = function write_string_buffer(value) {
11157
+ var n = value.length;
11158
+ if (!n) {
11159
+ this._reserve(1);
11160
+ this.buf[this.pos++] = 0;
11161
+ return this;
11162
+ }
11163
+ if (n < 0x80) {
11164
+ this._reserve(n * 3 + 5); // worst case
11165
+ var pos = this.pos,
11166
+ buf = this.buf;
11167
+ return this._delim(pos,
11168
+ n < 40
11169
+ ? util.utf8.write(value, buf, pos + 1)
11170
+ : writeStringBuffer(value, buf, pos + 1)
11171
+ );
11172
+ }
10765
11173
  var len = util.Buffer.byteLength(value);
10766
11174
  this.uint32(len);
10767
- if (len)
10768
- this._push(len === value.length && len < 40 ? writeStringBufferAscii : writeStringBuffer, len, value);
11175
+ this._reserve(len);
11176
+ writeStringBuffer(value, this.buf, this.pos);
11177
+ this.pos += len;
10769
11178
  return this;
10770
11179
  };
10771
11180