protobufjs 8.6.6 → 8.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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.1 (c) 2016, daniel wirtz
3
+ * compiled mon, 13 jul 2026 11:04:12 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -205,27 +205,33 @@ common("struct", {
205
205
  fields: {
206
206
  nullValue: {
207
207
  type: "NullValue",
208
- id: 1
208
+ id: 1,
209
+ protoName: "null_value"
209
210
  },
210
211
  numberValue: {
211
212
  type: "double",
212
- id: 2
213
+ id: 2,
214
+ protoName: "number_value"
213
215
  },
214
216
  stringValue: {
215
217
  type: "string",
216
- id: 3
218
+ id: 3,
219
+ protoName: "string_value"
217
220
  },
218
221
  boolValue: {
219
222
  type: "bool",
220
- id: 4
223
+ id: 4,
224
+ protoName: "bool_value"
221
225
  },
222
226
  structValue: {
223
227
  type: "Struct",
224
- id: 5
228
+ id: 5,
229
+ protoName: "struct_value"
225
230
  },
226
231
  listValue: {
227
232
  type: "ListValue",
228
- id: 6
233
+ id: 6,
234
+ protoName: "list_value"
229
235
  }
230
236
  }
231
237
  },
@@ -741,7 +747,7 @@ converter.toObject = function toObject(mtype) {
741
747
  else if (field.bytes) {
742
748
  var arrayDefault = Array.prototype.slice.call(field.typeDefault);
743
749
  gen
744
- ("if(o.bytes===String)d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault))
750
+ ("if(o.bytes===String)d%s=%j", prop, util.base64.encode(field.typeDefault, 0, field.typeDefault.length))
745
751
  ("else{")
746
752
  ("d%s=%j", prop, arrayDefault)
747
753
  ("if(o.bytes!==Array)d%s=util.newBuffer(d%s)", prop, prop)
@@ -899,7 +905,7 @@ function decoder(mtype) {
899
905
  ("break");
900
906
 
901
907
  if (types.basic[type] === undefined) gen
902
- ("v=types[%i].decode(r,r.uint32(),undefined,q+1)", i); // can't be groups
908
+ ("v=types[%i].decode(r,r.uint32(),undefined,q+1,v)", i); // can't be groups
903
909
  else gen
904
910
  ("v=r.%s()", type === "string" ? stringMethod(field) : type);
905
911
 
@@ -938,13 +944,9 @@ function decoder(mtype) {
938
944
  if (types.packed[type] !== undefined) {
939
945
  gen
940
946
  ("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
947
  if (closed) {
947
948
  gen
949
+ ("var c2=r.uint32()+r.pos")
948
950
  ("while(r.pos<c2){")
949
951
  ("s=r.pos")
950
952
  ("v=r.%s()", type)
@@ -956,8 +958,9 @@ function decoder(mtype) {
956
958
  genPreserveUnknown(gen, "util.rawField(" + field.id + ",0,r.raw(s,r.pos))")
957
959
  ("}");
958
960
  } else gen
959
- ("while(r.pos<c2)")
960
- ("%s.push(r.%s())", ref, type);
961
+ ("if(!(%s&&%s.length))", ref, ref)
962
+ ("%s=[]", ref)
963
+ ("r.%ss(%s)", type, ref);
961
964
  gen
962
965
  ("continue")
963
966
  ("}");
@@ -1153,10 +1156,7 @@ function encoder(mtype) {
1153
1156
  // Packed repeated
1154
1157
  if (field.packed && types.packed[type] !== undefined) { gen
1155
1158
 
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()");
1159
+ ("w.uint32(%i).%ss(%s)", (field.id << 3 | 2) >>> 0, type, ref);
1160
1160
 
1161
1161
  // Non-packed
1162
1162
  } else { gen
@@ -2089,7 +2089,7 @@ exports.configure = configure;
2089
2089
  * @returns {undefined}
2090
2090
  */
2091
2091
  function configure() {
2092
- exports.util._configure();
2092
+ exports.util.LongBits._configure(exports.util.Long);
2093
2093
  exports.Writer._configure(exports.BufferWriter);
2094
2094
  exports.Reader._configure(exports.BufferReader);
2095
2095
  }
@@ -4009,6 +4009,9 @@ function parse(source, root, options) {
4009
4009
  }
4010
4010
 
4011
4011
  function parseId(token, acceptNegative, max) {
4012
+ if (token === null) {
4013
+ throw illegal(token, "end of input");
4014
+ }
4012
4015
  switch (token) {
4013
4016
  case "max": case "MAX": case "Max":
4014
4017
  return max || maxFieldId;
@@ -4041,7 +4044,7 @@ function parse(source, root, options) {
4041
4044
  pkg = next();
4042
4045
 
4043
4046
  /* istanbul ignore if */
4044
- if (!typeRefRe.test(pkg))
4047
+ if (pkg === null || !typeRefRe.test(pkg))
4045
4048
  throw illegal(pkg, "name");
4046
4049
 
4047
4050
  ptr = ptr.define(pkg);
@@ -4178,7 +4181,7 @@ function parse(source, root, options) {
4178
4181
  throw Error("max depth exceeded");
4179
4182
 
4180
4183
  /* istanbul ignore if */
4181
- if (!nameRe.test(token = next()))
4184
+ if ((token = next()) === null || !nameRe.test(token))
4182
4185
  throw illegal(token, "type name");
4183
4186
 
4184
4187
  var type = new Type(token);
@@ -4245,6 +4248,9 @@ function parse(source, root, options) {
4245
4248
 
4246
4249
  function parseField(parent, rule, extend, depth) {
4247
4250
  var type = next();
4251
+ if (type === null) {
4252
+ throw illegal(type, "end of input");
4253
+ }
4248
4254
  if (type === "group") {
4249
4255
  parseGroup(parent, rule, extend, depth);
4250
4256
  return;
@@ -4256,8 +4262,12 @@ function parse(source, root, options) {
4256
4262
  // package .subpackage field tokens: "package" ".subpackage" [TYPE NAME ENDS HERE] "field"
4257
4263
  // Keep reading tokens until we get a type name with no period at the end,
4258
4264
  // and the next token does not start with a period.
4259
- while (type.endsWith(".") || peek().startsWith(".")) {
4260
- type += next();
4265
+ while (type.endsWith(".") || (peek() || "").startsWith(".")) {
4266
+ var part = next();
4267
+ if (part === null) {
4268
+ throw illegal(part, "end of input");
4269
+ }
4270
+ type += part;
4261
4271
  }
4262
4272
 
4263
4273
  /* istanbul ignore if */
@@ -4265,6 +4275,9 @@ function parse(source, root, options) {
4265
4275
  throw illegal(type, "type");
4266
4276
 
4267
4277
  var name = next();
4278
+ if (name === null) {
4279
+ throw illegal(name, "end of input");
4280
+ }
4268
4281
 
4269
4282
  /* istanbul ignore if */
4270
4283
 
@@ -4317,7 +4330,7 @@ function parse(source, root, options) {
4317
4330
  var name = next();
4318
4331
 
4319
4332
  /* istanbul ignore if */
4320
- if (!nameRe.test(name))
4333
+ if (name === null || !nameRe.test(name))
4321
4334
  throw illegal(name, "name");
4322
4335
 
4323
4336
  var fieldName = util.lcFirst(name);
@@ -4335,6 +4348,10 @@ function parse(source, root, options) {
4335
4348
  case ";":
4336
4349
  break;
4337
4350
 
4351
+ case "map":
4352
+ parseMapField(type);
4353
+ break;
4354
+
4338
4355
  case "option":
4339
4356
  parseOption(type, token);
4340
4357
  skip(";");
@@ -4415,7 +4432,7 @@ function parse(source, root, options) {
4415
4432
  var name = next();
4416
4433
 
4417
4434
  /* istanbul ignore if */
4418
- if (!nameRe.test(name))
4435
+ if (name === null || !nameRe.test(name))
4419
4436
  throw illegal(name, "name");
4420
4437
 
4421
4438
  skip("=");
@@ -4442,7 +4459,7 @@ function parse(source, root, options) {
4442
4459
  function parseOneOf(parent, token, depth) {
4443
4460
 
4444
4461
  /* istanbul ignore if */
4445
- if (!nameRe.test(token = next()))
4462
+ if ((token = next()) === null || !nameRe.test(token))
4446
4463
  throw illegal(token, "name");
4447
4464
 
4448
4465
  var oneof = new OneOf(applyCase(token));
@@ -4461,10 +4478,11 @@ function parse(source, root, options) {
4461
4478
  function parseEnum(parent, token) {
4462
4479
 
4463
4480
  /* istanbul ignore if */
4464
- if (!nameRe.test(token = next()))
4481
+ if ((token = next()) === null || !nameRe.test(token))
4465
4482
  throw illegal(token, "name");
4466
4483
 
4467
- var enm = new Enum(token);
4484
+ var enm = new Enum(token),
4485
+ values = [];
4468
4486
  ifBlock(enm, function parseEnum_block(token) {
4469
4487
  switch(token) {
4470
4488
  case ";":
@@ -4481,16 +4499,18 @@ function parse(source, root, options) {
4481
4499
  break;
4482
4500
 
4483
4501
  default:
4484
- parseEnumValue(enm, token);
4502
+ values.push(parseEnumValue(token));
4485
4503
  }
4486
4504
  });
4505
+ for (var i = 0; i < values.length; ++i)
4506
+ enm.add(values[i].name, values[i].id, values[i].comment, values[i].options);
4487
4507
  parent.add(enm);
4488
4508
  if (parent === ptr) {
4489
4509
  topLevelObjects.push(enm);
4490
4510
  }
4491
4511
  }
4492
4512
 
4493
- function parseEnumValue(parent, token) {
4513
+ function parseEnumValue(token) {
4494
4514
 
4495
4515
  /* istanbul ignore if */
4496
4516
  if (!nameRe.test(token))
@@ -4522,7 +4542,12 @@ function parse(source, root, options) {
4522
4542
  }, function parseEnumValue_line() {
4523
4543
  parseInlineOptions(dummy); // skip
4524
4544
  });
4525
- parent.add(token, value, dummy.comment, dummy.parsedOptions || dummy.options);
4545
+ return {
4546
+ name: token,
4547
+ id: value,
4548
+ comment: dummy.comment,
4549
+ options: dummy.parsedOptions || dummy.options
4550
+ };
4526
4551
  }
4527
4552
 
4528
4553
  function parseOption(parent, token) {
@@ -4573,16 +4598,27 @@ function parse(source, root, options) {
4573
4598
  var objectResult = {};
4574
4599
 
4575
4600
  while (!skip("}", true)) {
4576
- /* istanbul ignore if */
4577
- if (!nameRe.test(token = next())) {
4578
- throw illegal(token, "name");
4579
- }
4580
- if (token === null) {
4581
- throw illegal(token, "end of input");
4601
+ token = next();
4602
+ var propName;
4603
+ if (token === null)
4604
+ throw illegal(token, "end of input");
4605
+ if (token === "[") {
4606
+ token = next();
4607
+ // Extension name or, for Any, a type URL: "<prefix>/<fully.qualified.Type>".
4608
+ var slash = token === null ? -1 : token.lastIndexOf("/");
4609
+ if (token === null || !typeRefRe.test(slash < 0 ? token : token.slice(slash + 1)))
4610
+ throw illegal(token, "name");
4611
+ propName = "[" + token + "]";
4612
+ skip("]");
4613
+ } else {
4614
+ /* istanbul ignore if */
4615
+ if (!nameRe.test(token)) {
4616
+ throw illegal(token, "name");
4617
+ }
4618
+ propName = token;
4582
4619
  }
4583
4620
 
4584
4621
  var value;
4585
- var propName = token;
4586
4622
 
4587
4623
  skip(":", true);
4588
4624
 
@@ -4590,25 +4626,30 @@ function parse(source, root, options) {
4590
4626
  // option (my_option) = {
4591
4627
  // repeated_value: [ "foo", "bar" ]
4592
4628
  // };
4593
- value = parseOptionValue(parent, name + "." + token, depth + 1);
4629
+ value = parseOptionValue(parent, name + "." + propName, depth + 1);
4594
4630
  } else if (peek() === "[") {
4595
4631
  value = [];
4596
- var lastValue;
4632
+ var lastValue,
4633
+ lastValueIsAggregate;
4597
4634
  if (skip("[", true)) {
4598
4635
  if (!skip("]", true)) {
4599
4636
  do {
4600
- lastValue = readValue(true);
4637
+ lastValueIsAggregate = peek() === "{";
4638
+ lastValue = lastValueIsAggregate
4639
+ ? parseOptionValue(parent, name + "." + propName, depth + 1)
4640
+ : readValue(true);
4601
4641
  value.push(lastValue);
4602
4642
  } while (skip(",", true));
4603
4643
  skip("]");
4604
4644
  if (typeof lastValue !== "undefined") {
4605
- setOption(parent, name + "." + token, lastValue);
4645
+ if (!lastValueIsAggregate)
4646
+ setOption(parent, name + "." + propName, lastValue);
4606
4647
  }
4607
4648
  }
4608
4649
  }
4609
4650
  } else {
4610
4651
  value = readValue(true);
4611
- setOption(parent, name + "." + token, value);
4652
+ setOption(parent, name + "." + propName, value);
4612
4653
  }
4613
4654
 
4614
4655
  var prevValue = Object.prototype.hasOwnProperty.call(objectResult, propName)
@@ -4643,15 +4684,12 @@ function parse(source, root, options) {
4643
4684
  // lift json_name onto Field
4644
4685
  if (name === "json_name" && parent instanceof Field) {
4645
4686
  parent.jsonName = value;
4646
- return;
4647
4687
  }
4648
4688
  if (parent.setOption)
4649
4689
  parent.setOption(name, value);
4650
4690
  }
4651
4691
 
4652
4692
  function setParsedOption(parent, name, value, propName) {
4653
- if (name === "json_name" && parent instanceof Field)
4654
- return; // lifted onto Field#jsonName above
4655
4693
  if (parent.setParsedOption)
4656
4694
  parent.setParsedOption(name, value, propName);
4657
4695
  }
@@ -4673,7 +4711,7 @@ function parse(source, root, options) {
4673
4711
  throw Error("max depth exceeded");
4674
4712
 
4675
4713
  /* istanbul ignore if */
4676
- if (!nameRe.test(token = next()))
4714
+ if ((token = next()) === null || !nameRe.test(token))
4677
4715
  throw illegal(token, "service name");
4678
4716
 
4679
4717
  var service = new Service(token);
@@ -4751,7 +4789,7 @@ function parse(source, root, options) {
4751
4789
  function parseExtension(parent, token, depth) {
4752
4790
 
4753
4791
  /* istanbul ignore if */
4754
- if (!typeRefRe.test(token = next()))
4792
+ if ((token = next()) === null || !typeRefRe.test(token))
4755
4793
  throw illegal(token, "reference");
4756
4794
 
4757
4795
  var reference = token;
@@ -4879,7 +4917,7 @@ function indexOutOfRange(reader, writeLength) {
4879
4917
 
4880
4918
  /**
4881
4919
  * Constructs a new reader instance using the specified buffer.
4882
- * @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
4920
+ * @classdesc Wire format reader using `Uint8Array`.
4883
4921
  * @constructor
4884
4922
  * @param {Uint8Array} buffer Buffer to read from
4885
4923
  */
@@ -4903,6 +4941,12 @@ function Reader(buffer) {
4903
4941
  */
4904
4942
  this.len = buffer.length;
4905
4943
 
4944
+ /**
4945
+ * Cached DataView for packed reads.
4946
+ * @type {DataView|null}
4947
+ */
4948
+ this.view = null;
4949
+
4906
4950
  /**
4907
4951
  * Whether to discard unknown fields while decoding.
4908
4952
  * @type {boolean}
@@ -4910,18 +4954,14 @@ function Reader(buffer) {
4910
4954
  this.discardUnknown = Reader.discardUnknown;
4911
4955
  }
4912
4956
 
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
- };
4957
+ function create_array(buffer) {
4958
+ // TODO: Remove plain array reader support in the next major release.
4959
+ if (Array.isArray(buffer))
4960
+ buffer = new Uint8Array(buffer);
4961
+ if (buffer instanceof Uint8Array)
4962
+ return new Reader(buffer);
4963
+ throw Error("illegal buffer");
4964
+ }
4925
4965
 
4926
4966
  var create = function create() {
4927
4967
  return util.Buffer
@@ -4946,8 +4986,6 @@ var create = function create() {
4946
4986
  */
4947
4987
  Reader.create = create();
4948
4988
 
4949
- Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore next */ util.Array.prototype.slice;
4950
-
4951
4989
  /**
4952
4990
  * Returns raw bytes from the backing buffer without advancing the reader.
4953
4991
  * @param {number} start Start offset
@@ -4955,12 +4993,7 @@ Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore ne
4955
4993
  * @returns {Uint8Array} Raw bytes
4956
4994
  */
4957
4995
  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);
4996
+ return this.buf.subarray(start, end);
4964
4997
  };
4965
4998
 
4966
4999
  /**
@@ -5257,6 +5290,241 @@ Reader.prototype.double = function read_double() {
5257
5290
  return value;
5258
5291
  };
5259
5292
 
5293
+ /**
5294
+ * Reads a packed repeated field of unsigned 32 bit varints.
5295
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5296
+ * @returns {number[]} Array read into
5297
+ */
5298
+ Reader.prototype.uint32s = function read_uint32s(array) {
5299
+ if (array === undefined) array = [];
5300
+ var end = this.uint32() + this.pos;
5301
+ while (this.pos < end)
5302
+ array.push(this.uint32());
5303
+ return array;
5304
+ };
5305
+
5306
+ /**
5307
+ * Reads a packed repeated field of signed 32 bit varints.
5308
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5309
+ * @returns {number[]} Array read into
5310
+ */
5311
+ Reader.prototype.int32s = function read_int32s(array) {
5312
+ if (array === undefined) array = [];
5313
+ var end = this.uint32() + this.pos;
5314
+ while (this.pos < end)
5315
+ array.push(this.int32());
5316
+ return array;
5317
+ };
5318
+
5319
+ /**
5320
+ * Reads a packed repeated field of zig-zag encoded signed 32 bit varints.
5321
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5322
+ * @returns {number[]} Array read into
5323
+ */
5324
+ Reader.prototype.sint32s = function read_sint32s(array) {
5325
+ if (array === undefined) array = [];
5326
+ var end = this.uint32() + this.pos;
5327
+ while (this.pos < end)
5328
+ array.push(this.sint32());
5329
+ return array;
5330
+ };
5331
+
5332
+ /**
5333
+ * Reads a packed repeated field of booleans.
5334
+ * @param {boolean[]} [array] Array to read into; a new one is created if omitted
5335
+ * @returns {boolean[]} Array read into
5336
+ */
5337
+ Reader.prototype.bools = function read_bools(array) {
5338
+ if (array === undefined) array = [];
5339
+ var end = this.uint32() + this.pos;
5340
+ while (this.pos < end)
5341
+ array.push(this.bool());
5342
+ return array;
5343
+ };
5344
+
5345
+ // The view allocation only pays off when amortized over enough reads
5346
+ var VIEW_THRESHOLD_FLOAT = 8,
5347
+ VIEW_THRESHOLD_INT = 128;
5348
+
5349
+ function getLazyView(reader, count, threshold) {
5350
+ var view = reader.view;
5351
+ if (view || count < threshold)
5352
+ return view;
5353
+ var buf = reader.buf;
5354
+ return reader.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
5355
+ }
5356
+
5357
+ /**
5358
+ * Reads a packed repeated field of unsigned 32 bit fixed values.
5359
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5360
+ * @returns {number[]} Array read into
5361
+ */
5362
+ Reader.prototype.fixed32s = function read_fixed32s(array) {
5363
+ if (array === undefined) array = [];
5364
+ var len = this.uint32(), end = this.pos + len;
5365
+ /* istanbul ignore if */
5366
+ if (end > this.len) throw indexOutOfRange(this, len);
5367
+ var count = len >>> 2, i = array.length, pos = this.pos;
5368
+ array.length = i + count;
5369
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
5370
+ if (dv)
5371
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getUint32(pos, true);
5372
+ else {
5373
+ var buf = this.buf;
5374
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4);
5375
+ }
5376
+ this.pos = pos;
5377
+ if (pos !== end) throw indexOutOfRange(this, 4);
5378
+ return array;
5379
+ };
5380
+
5381
+ /**
5382
+ * Reads a packed repeated field of signed 32 bit fixed values.
5383
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5384
+ * @returns {number[]} Array read into
5385
+ */
5386
+ Reader.prototype.sfixed32s = function read_sfixed32s(array) {
5387
+ if (array === undefined) array = [];
5388
+ var len = this.uint32(), end = this.pos + len;
5389
+ /* istanbul ignore if */
5390
+ if (end > this.len) throw indexOutOfRange(this, len);
5391
+ var count = len >>> 2, i = array.length, pos = this.pos;
5392
+ array.length = i + count;
5393
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_INT);
5394
+ if (dv)
5395
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getInt32(pos, true);
5396
+ else {
5397
+ var buf = this.buf;
5398
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4) | 0;
5399
+ }
5400
+ this.pos = pos;
5401
+ if (pos !== end) throw indexOutOfRange(this, 4);
5402
+ return array;
5403
+ };
5404
+
5405
+ /**
5406
+ * Reads a packed repeated field of floats (32 bit).
5407
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5408
+ * @returns {number[]} Array read into
5409
+ */
5410
+ Reader.prototype.floats = function read_floats(array) {
5411
+ if (array === undefined) array = [];
5412
+ var len = this.uint32(), end = this.pos + len;
5413
+ /* istanbul ignore if */
5414
+ if (end > this.len) throw indexOutOfRange(this, len);
5415
+ var count = len >>> 2, i = array.length, pos = this.pos;
5416
+ array.length = i + count;
5417
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
5418
+ if (dv)
5419
+ for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getFloat32(pos, true);
5420
+ else {
5421
+ var buf = this.buf;
5422
+ for (var j = 0; j < count; ++j, pos += 4) array[i++] = util.float.readFloatLE(buf, pos);
5423
+ }
5424
+ this.pos = pos;
5425
+ if (pos !== end) throw indexOutOfRange(this, 4);
5426
+ return array;
5427
+ };
5428
+
5429
+ /**
5430
+ * Reads a packed repeated field of doubles (64 bit float).
5431
+ * @param {number[]} [array] Array to read into; a new one is created if omitted
5432
+ * @returns {number[]} Array read into
5433
+ */
5434
+ Reader.prototype.doubles = function read_doubles(array) {
5435
+ if (array === undefined) array = [];
5436
+ var len = this.uint32(), end = this.pos + len;
5437
+ /* istanbul ignore if */
5438
+ if (end > this.len) throw indexOutOfRange(this, len);
5439
+ var count = len >>> 3, i = array.length, pos = this.pos;
5440
+ array.length = i + count;
5441
+ var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT);
5442
+ if (dv)
5443
+ for (var k = 0; k < count; ++k, pos += 8) array[i++] = dv.getFloat64(pos, true);
5444
+ else {
5445
+ var buf = this.buf;
5446
+ for (var j = 0; j < count; ++j, pos += 8) array[i++] = util.float.readDoubleLE(buf, pos);
5447
+ }
5448
+ this.pos = pos;
5449
+ if (pos !== end) throw indexOutOfRange(this, 8);
5450
+ return array;
5451
+ };
5452
+
5453
+ /**
5454
+ * Reads a packed repeated field of unsigned 64 bit varints.
5455
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5456
+ * @returns {Array.<Long|number>} Array read into
5457
+ */
5458
+ Reader.prototype.uint64s = function read_uint64s(array) {
5459
+ if (array === undefined) array = [];
5460
+ var end = this.uint32() + this.pos;
5461
+ while (this.pos < end)
5462
+ array.push(this.uint64());
5463
+ return array;
5464
+ };
5465
+
5466
+ /**
5467
+ * Reads a packed repeated field of signed 64 bit varints.
5468
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5469
+ * @returns {Array.<Long|number>} Array read into
5470
+ */
5471
+ Reader.prototype.int64s = function read_int64s(array) {
5472
+ if (array === undefined) array = [];
5473
+ var end = this.uint32() + this.pos;
5474
+ while (this.pos < end)
5475
+ array.push(this.int64());
5476
+ return array;
5477
+ };
5478
+
5479
+ /**
5480
+ * Reads a packed repeated field of zig-zag encoded signed 64 bit varints.
5481
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5482
+ * @returns {Array.<Long|number>} Array read into
5483
+ */
5484
+ Reader.prototype.sint64s = function read_sint64s(array) {
5485
+ if (array === undefined) array = [];
5486
+ var end = this.uint32() + this.pos;
5487
+ while (this.pos < end)
5488
+ array.push(this.sint64());
5489
+ return array;
5490
+ };
5491
+
5492
+ /**
5493
+ * Reads a packed repeated field of unsigned 64 bit fixed values.
5494
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5495
+ * @returns {Array.<Long|number>} Array read into
5496
+ */
5497
+ Reader.prototype.fixed64s = function read_fixed64s(array) {
5498
+ if (array === undefined) array = [];
5499
+ var len = this.uint32(), end = this.pos + len, i = array.length;
5500
+ /* istanbul ignore if */
5501
+ if (end > this.len) throw indexOutOfRange(this, len);
5502
+ var count = len >>> 3;
5503
+ array.length = i + count; // 8 bytes per value, count is known
5504
+ for (var j = 0; j < count; ++j)
5505
+ array[i++] = this.fixed64();
5506
+ if (this.pos !== end) throw indexOutOfRange(this, 8);
5507
+ return array;
5508
+ };
5509
+
5510
+ /**
5511
+ * Reads a packed repeated field of signed 64 bit fixed values.
5512
+ * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted
5513
+ * @returns {Array.<Long|number>} Array read into
5514
+ */
5515
+ Reader.prototype.sfixed64s = function read_sfixed64s(array) {
5516
+ if (array === undefined) array = [];
5517
+ var len = this.uint32(), end = this.pos + len, i = array.length;
5518
+ /* istanbul ignore if */
5519
+ if (end > this.len) throw indexOutOfRange(this, len);
5520
+ var count = len >>> 3;
5521
+ array.length = i + count; // 8 bytes per value, count is known
5522
+ for (var j = 0; j < count; ++j)
5523
+ array[i++] = this.sfixed64();
5524
+ if (this.pos !== end) throw indexOutOfRange(this, 8);
5525
+ return array;
5526
+ };
5527
+
5260
5528
  /**
5261
5529
  * Reads a sequence of bytes preceeded by its length as a varint.
5262
5530
  * @returns {Uint8Array} Value read
@@ -5452,7 +5720,7 @@ function BufferReader(buffer) {
5452
5720
  * Read buffer.
5453
5721
  * @name BufferReader#buf
5454
5722
  * @type {Buffer}
5455
- */
5723
+ */
5456
5724
  }
5457
5725
 
5458
5726
  BufferReader._configure = function () {
@@ -5470,8 +5738,6 @@ BufferReader._configure = function () {
5470
5738
  * @returns {Buffer} Raw bytes
5471
5739
  */
5472
5740
  BufferReader.prototype.raw = function read_raw_buffer(start, end) {
5473
- if (start === end)
5474
- return util.Buffer.alloc(0);
5475
5741
  return this._slice.call(this.buf, start, end);
5476
5742
  };
5477
5743
 
@@ -7271,6 +7537,14 @@ Type.prototype.setup = function setup() {
7271
7537
  // Sets up everything at once so that the prototype chain does not have to be re-evaluated
7272
7538
  // multiple times (V8, soft-deopt prototype-check).
7273
7539
 
7540
+ // Resolve feature defaults incl. field presence before generating codecs
7541
+ var root = this.root;
7542
+ if (root && root._needsRecursiveFeatureResolution) {
7543
+ var edition = root._edition || this._edition;
7544
+ if (edition)
7545
+ root._resolveFeaturesRecursive(edition);
7546
+ }
7547
+
7274
7548
  var fullName = this.fullName,
7275
7549
  types = [];
7276
7550
  for (var i = 0; i < /* initializes */ this.fieldsArray.length; ++i)
@@ -7334,7 +7608,7 @@ Type.prototype.encode = function encode_setup(message, writer) { // eslint-disab
7334
7608
  * @returns {Writer} writer
7335
7609
  */
7336
7610
  Type.prototype.encodeDelimited = function encodeDelimited(message, writer) {
7337
- return this.encode(message, writer && writer.len ? writer.fork() : writer).ldelim();
7611
+ return this.encode(message, (writer || Writer.create()).fork()).ldelim();
7338
7612
  };
7339
7613
 
7340
7614
  /**
@@ -8786,7 +9060,7 @@ module.exports = fs;
8786
9060
  "use strict";
8787
9061
  module.exports = LongBits;
8788
9062
 
8789
- var util = require(37);
9063
+ var Long;
8790
9064
 
8791
9065
  /**
8792
9066
  * Constructs new long bits.
@@ -8865,10 +9139,10 @@ LongBits.fromNumber = function fromNumber(value) {
8865
9139
  LongBits.from = function from(value) {
8866
9140
  if (typeof value === "number")
8867
9141
  return LongBits.fromNumber(value);
8868
- if (util.isString(value)) {
9142
+ if (typeof value === "string" || value instanceof String) {
8869
9143
  /* istanbul ignore else */
8870
- if (util.Long)
8871
- value = util.Long.fromString(value);
9144
+ if (Long)
9145
+ value = Long.fromString(value);
8872
9146
  else
8873
9147
  return LongBits.fromNumber(parseInt(value, 10));
8874
9148
  }
@@ -8897,8 +9171,8 @@ LongBits.prototype.toNumber = function toNumber(unsigned) {
8897
9171
  * @returns {Long} Long
8898
9172
  */
8899
9173
  LongBits.prototype.toLong = function toLong(unsigned) {
8900
- return util.Long
8901
- ? new util.Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
9174
+ return Long
9175
+ ? new Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
8902
9176
  /* istanbul ignore next */
8903
9177
  : { low: this.lo | 0, high: this.hi | 0, unsigned: Boolean(unsigned) };
8904
9178
  };
@@ -8984,7 +9258,11 @@ LongBits.prototype.length = function length() {
8984
9258
  : part2 < 128 ? 9 : 10;
8985
9259
  };
8986
9260
 
8987
- },{"37":37}],37:[function(require,module,exports){
9261
+ LongBits._configure = function(Long_) {
9262
+ Long = Long_;
9263
+ };
9264
+
9265
+ },{}],37:[function(require,module,exports){
8988
9266
  "use strict";
8989
9267
  /* global globalThis */
8990
9268
  var util = exports;
@@ -9124,36 +9402,29 @@ util.isSet = function isSet(obj, prop) {
9124
9402
  util.Buffer = (function() {
9125
9403
  try {
9126
9404
  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;
9405
+ // refuse to use non-node buffers if not explicitly assigned (perf reasons)
9406
+ return Buffer.prototype.utf8Write || util.isNode ? Buffer : /* istanbul ignore next */ null;
9129
9407
  } catch (e) {
9130
9408
  /* istanbul ignore next */
9131
9409
  return null;
9132
9410
  }
9133
9411
  })();
9134
9412
 
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
9413
  /**
9142
9414
  * Creates a new buffer of whatever type supported by the environment.
9143
9415
  * @param {number|number[]} [sizeOrArray=0] Buffer size or number array
9144
9416
  * @returns {Uint8Array|Buffer} Buffer
9145
9417
  */
9146
9418
  util.newBuffer = function newBuffer(sizeOrArray) {
9419
+ var Buffer = util.Buffer;
9147
9420
  /* istanbul ignore next */
9148
9421
  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);
9422
+ ? Buffer
9423
+ ? Buffer.allocUnsafe(sizeOrArray)
9424
+ : new Uint8Array(sizeOrArray)
9425
+ : Buffer
9426
+ ? Buffer.from(sizeOrArray)
9427
+ : new Uint8Array(sizeOrArray);
9157
9428
  };
9158
9429
 
9159
9430
  /**
@@ -9179,10 +9450,11 @@ util.rawField = function rawField(id, wireType, data) {
9179
9450
  };
9180
9451
 
9181
9452
  /**
9182
- * Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
9453
+ * Array implementation used in the browser.
9183
9454
  * @type {Constructor<Uint8Array>}
9455
+ * @deprecated Use `Uint8Array` instead.
9184
9456
  */
9185
- util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : Array;
9457
+ util.Array = Uint8Array;
9186
9458
 
9187
9459
  /**
9188
9460
  * Any compatible Long instance.
@@ -9503,28 +9775,6 @@ util.toJSONOptions = {
9503
9775
  json: true
9504
9776
  };
9505
9777
 
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
9778
  },{"29":29,"30":30,"32":32,"34":34,"36":36,"40":40,"41":41,"long":"long"}],38:[function(require,module,exports){
9529
9779
  "use strict";
9530
9780
 
@@ -9690,19 +9940,21 @@ function pool(alloc, slice, size) {
9690
9940
  "use strict";
9691
9941
 
9692
9942
  /**
9693
- * A minimal UTF8 implementation for number arrays.
9943
+ * A minimal UTF8 implementation.
9694
9944
  * @memberof util
9695
9945
  * @namespace
9696
9946
  */
9697
9947
  var utf8 = exports,
9698
9948
  replacementChar = "\ufffd",
9949
+ looseDecoder = new TextDecoder("utf-8", { ignoreBOM: true }),
9699
9950
  strictDecoder;
9951
+ var TEXT_DECODER_MIN_LENGTH = 64;
9700
9952
 
9701
9953
  try {
9702
9954
  strictDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
9703
9955
  } catch (err) {
9704
9956
  // "fatal" option is not supported on Node.js compiled without ICU
9705
- strictDecoder = new TextDecoder("utf-8", { ignoreBOM: true });
9957
+ strictDecoder = looseDecoder;
9706
9958
  }
9707
9959
 
9708
9960
  /**
@@ -9753,6 +10005,13 @@ function utf8_read_js(buffer, start, end, str) {
9753
10005
  return str;
9754
10006
  }
9755
10007
 
10008
+ function utf8_read_decoder(decoder, buffer, start, end) {
10009
+ var source = start === 0 && end === buffer.length
10010
+ ? buffer
10011
+ : buffer.subarray(start, end);
10012
+ return decoder.decode(source);
10013
+ }
10014
+
9756
10015
  /**
9757
10016
  * Reads UTF8 bytes as a string.
9758
10017
  * @param {Uint8Array} buffer Source buffer
@@ -9760,9 +10019,11 @@ function utf8_read_js(buffer, start, end, str) {
9760
10019
  * @param {number} end Source end
9761
10020
  * @returns {string} String read
9762
10021
  */
9763
- utf8.read = function utf8_read_ascii(buffer, start, end) {
10022
+ utf8.read = function utf8_read_loose(buffer, start, end) {
9764
10023
  if (end - start < 1)
9765
10024
  return "";
10025
+ if (end - start >= TEXT_DECODER_MIN_LENGTH)
10026
+ return utf8_read_decoder(looseDecoder, buffer, start, end);
9766
10027
 
9767
10028
  var str = "",
9768
10029
  i = start,
@@ -9792,17 +10053,6 @@ utf8.read = function utf8_read_ascii(buffer, start, end) {
9792
10053
  return str;
9793
10054
  };
9794
10055
 
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
10056
  /**
9807
10057
  * Reads UTF8 bytes as a string, rejecting invalid UTF8.
9808
10058
  * @param {Uint8Array} buffer Source buffer
@@ -9810,9 +10060,11 @@ function utf8_read_strict(buffer, start, end) {
9810
10060
  * @param {number} end Source end
9811
10061
  * @returns {string} String read
9812
10062
  */
9813
- utf8.readStrict = function utf8_read_strict_ascii(buffer, start, end) {
10063
+ utf8.readStrict = function utf8_read_strict(buffer, start, end) {
9814
10064
  if (end - start < 1)
9815
10065
  return "";
10066
+ if (end - start >= TEXT_DECODER_MIN_LENGTH)
10067
+ return utf8_read_decoder(strictDecoder, buffer, start, end);
9816
10068
 
9817
10069
  var str = "",
9818
10070
  i = start,
@@ -9828,14 +10080,14 @@ utf8.readStrict = function utf8_read_strict_ascii(buffer, start, end) {
9828
10080
  c7 = buffer[i + 6];
9829
10081
  c8 = buffer[i + 7];
9830
10082
  if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80)
9831
- return str + utf8_read_strict(buffer, i, end);
10083
+ return str + utf8_read_decoder(strictDecoder, buffer, i, end);
9832
10084
  str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
9833
10085
  }
9834
10086
 
9835
10087
  for (; i < end; ++i) {
9836
10088
  c1 = buffer[i];
9837
10089
  if (c1 & 0x80)
9838
- return str + utf8_read_strict(buffer, i, end);
10090
+ return str + utf8_read_decoder(strictDecoder, buffer, i, end);
9839
10091
  str += String.fromCharCode(c1);
9840
10092
  }
9841
10093
 
@@ -10183,118 +10435,52 @@ var LongBits = util.LongBits,
10183
10435
  base64 = util.base64,
10184
10436
  utf8 = util.utf8;
10185
10437
 
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
10438
  /**
10261
10439
  * Constructs a new writer instance.
10262
- * @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
10440
+ * @classdesc Wire format writer using `Uint8Array`.
10263
10441
  * @constructor
10264
10442
  */
10265
10443
  function Writer() {
10266
10444
 
10267
10445
  /**
10268
- * Current length.
10446
+ * Write cursor into {@link Writer#buf}.
10269
10447
  * @type {number}
10270
10448
  */
10271
- this.len = 0;
10449
+ this.pos = 0;
10272
10450
 
10273
10451
  /**
10274
- * Operations head.
10275
- * @type {Object}
10452
+ * Backing buffer.
10453
+ * @type {Uint8Array}
10276
10454
  */
10277
- this.head = new Op(noop, 0, 0);
10455
+ this.buf = this.constructor.alloc(64);
10278
10456
 
10279
10457
  /**
10280
- * Operations tail
10281
- * @type {Object}
10458
+ * Cached DataView over {@link Writer#buf}.
10459
+ * @type {DataView|null}
10282
10460
  */
10283
- this.tail = this.head;
10461
+ this.view = null;
10284
10462
 
10285
10463
  /**
10286
- * Linked forked states.
10287
- * @type {Object|null}
10464
+ * Stack of forked length-prefix positions.
10465
+ * @type {Array<number>|null}
10288
10466
  */
10289
10467
  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
10468
  }
10297
10469
 
10470
+ /**
10471
+ * Current write position.
10472
+ * @name Writer#len
10473
+ * @type {number}
10474
+ * @deprecated Use {@link Writer#pos} instead.
10475
+ */
10476
+ Object.defineProperty(Writer.prototype, "len", {
10477
+ configurable: true,
10478
+ enumerable: true,
10479
+ get: function get_len() {
10480
+ return this.pos;
10481
+ }
10482
+ });
10483
+
10298
10484
  var create = function create() {
10299
10485
  return util.Buffer
10300
10486
  ? function create_buffer_setup() {
@@ -10321,32 +10507,45 @@ Writer.create = create();
10321
10507
  * @returns {Uint8Array} Buffer
10322
10508
  */
10323
10509
  Writer.alloc = function alloc(size) {
10324
- return new util.Array(size);
10510
+ return new Uint8Array(size);
10325
10511
  };
10326
10512
 
10327
10513
  // 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);
10514
+ Writer.alloc = util.pool(Writer.alloc, Uint8Array.prototype.subarray);
10331
10515
 
10332
10516
  /**
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`
10517
+ * Calculates the number of bytes a value occupies as a varint.
10518
+ * @param {number} value Value to size (unsigned)
10519
+ * @returns {number} Byte length (1..5)
10520
+ * @ignore
10521
+ */
10522
+ function sizeVarint32(value) {
10523
+ return value < 128 ? 1
10524
+ : value < 16384 ? 2
10525
+ : value < 2097152 ? 3
10526
+ : value < 268435456 ? 4
10527
+ : 5;
10528
+ }
10529
+
10530
+ /**
10531
+ * Ensures that at least `n` more bytes fit into the backing buffer, doubling it if not.
10532
+ * @param {number} n Number of additional bytes required
10533
+ * @returns {undefined}
10338
10534
  * @private
10339
10535
  */
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;
10536
+ Writer.prototype._reserve = function _reserve(n) {
10537
+ var need = this.pos + n;
10538
+ if (need > this.buf.length) {
10539
+ var size = this.buf.length << 1;
10540
+ if (size < need)
10541
+ size = need;
10542
+ var buf = this.constructor.alloc(size);
10543
+ buf.set(this.buf.subarray(0, this.pos), 0);
10544
+ this.buf = buf;
10545
+ this.view = null; // invalidate
10546
+ }
10344
10547
  };
10345
10548
 
10346
- function writeByte(val, buf, pos) {
10347
- buf[pos] = val & 255;
10348
- }
10349
-
10350
10549
  function writeStringAscii(val, buf, pos) {
10351
10550
  for (var i = 0; i < val.length;)
10352
10551
  buf[pos++] = val.charCodeAt(i++);
@@ -10358,42 +10557,19 @@ function writeVarint32(val, buf, pos) {
10358
10557
  val >>>= 7;
10359
10558
  }
10360
10559
  buf[pos] = val;
10560
+ return pos + 1;
10361
10561
  }
10362
10562
 
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
10563
  /**
10382
10564
  * Writes an unsigned 32 bit value as a varint.
10383
10565
  * @param {number} value Value to write
10384
10566
  * @returns {Writer} `this`
10385
10567
  */
10386
10568
  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;
10569
+ value = value >>> 0;
10570
+ this._reserve(5);
10571
+ var pos = this.pos;
10572
+ this.pos = writeVarint32(value, this.buf, pos);
10397
10573
  return this;
10398
10574
  };
10399
10575
 
@@ -10404,9 +10580,13 @@ Writer.prototype.uint32 = function write_uint32(value) {
10404
10580
  * @returns {Writer} `this`
10405
10581
  */
10406
10582
  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);
10583
+ if ((value |= 0) < 0) { // 10 bytes per spec
10584
+ this._reserve(10);
10585
+ writeVarint64(LongBits.fromNumber(value), this.buf, this.pos);
10586
+ this.pos += 10;
10587
+ return this;
10588
+ }
10589
+ return this.uint32(value);
10410
10590
  };
10411
10591
 
10412
10592
  /**
@@ -10430,7 +10610,8 @@ function writeVarint64(val, buf, pos) {
10430
10610
  buf[pos++] = lo & 127 | 128;
10431
10611
  lo = lo >>> 7;
10432
10612
  }
10433
- buf[pos++] = lo;
10613
+ buf[pos] = lo;
10614
+ return pos + 1;
10434
10615
  }
10435
10616
 
10436
10617
  /**
@@ -10441,7 +10622,10 @@ function writeVarint64(val, buf, pos) {
10441
10622
  */
10442
10623
  Writer.prototype.uint64 = function write_uint64(value) {
10443
10624
  var bits = LongBits.from(value);
10444
- return this._push(writeVarint64, bits.length(), bits);
10625
+ this._reserve(10);
10626
+ var pos = this.pos;
10627
+ this.pos = writeVarint64(bits, this.buf, pos);
10628
+ return this;
10445
10629
  };
10446
10630
 
10447
10631
  /**
@@ -10461,7 +10645,10 @@ Writer.prototype.int64 = Writer.prototype.uint64;
10461
10645
  */
10462
10646
  Writer.prototype.sint64 = function write_sint64(value) {
10463
10647
  var bits = LongBits.from(value).zzEncode();
10464
- return this._push(writeVarint64, bits.length(), bits);
10648
+ this._reserve(10);
10649
+ var pos = this.pos;
10650
+ this.pos = writeVarint64(bits, this.buf, pos);
10651
+ return this;
10465
10652
  };
10466
10653
 
10467
10654
  /**
@@ -10470,7 +10657,9 @@ Writer.prototype.sint64 = function write_sint64(value) {
10470
10657
  * @returns {Writer} `this`
10471
10658
  */
10472
10659
  Writer.prototype.bool = function write_bool(value) {
10473
- return this._push(writeByte, 1, value ? 1 : 0);
10660
+ this._reserve(1);
10661
+ this.buf[this.pos++] = value ? 1 : 0;
10662
+ return this;
10474
10663
  };
10475
10664
 
10476
10665
  function writeFixed32(val, buf, pos) {
@@ -10486,7 +10675,10 @@ function writeFixed32(val, buf, pos) {
10486
10675
  * @returns {Writer} `this`
10487
10676
  */
10488
10677
  Writer.prototype.fixed32 = function write_fixed32(value) {
10489
- return this._push(writeFixed32, 4, value >>> 0);
10678
+ this._reserve(4);
10679
+ writeFixed32(value >>> 0, this.buf, this.pos);
10680
+ this.pos += 4;
10681
+ return this;
10490
10682
  };
10491
10683
 
10492
10684
  /**
@@ -10505,7 +10697,11 @@ Writer.prototype.sfixed32 = Writer.prototype.fixed32;
10505
10697
  */
10506
10698
  Writer.prototype.fixed64 = function write_fixed64(value) {
10507
10699
  var bits = LongBits.from(value);
10508
- return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
10700
+ this._reserve(8);
10701
+ writeFixed32(bits.lo, this.buf, this.pos);
10702
+ writeFixed32(bits.hi, this.buf, this.pos + 4);
10703
+ this.pos += 8;
10704
+ return this;
10509
10705
  };
10510
10706
 
10511
10707
  /**
@@ -10524,7 +10720,10 @@ Writer.prototype.sfixed64 = Writer.prototype.fixed64;
10524
10720
  * @returns {Writer} `this`
10525
10721
  */
10526
10722
  Writer.prototype.float = function write_float(value) {
10527
- return this._push(util.float.writeFloatLE, 4, value);
10723
+ this._reserve(4);
10724
+ util.float.writeFloatLE(value, this.buf, this.pos);
10725
+ this.pos += 4;
10726
+ return this;
10528
10727
  };
10529
10728
 
10530
10729
  /**
@@ -10534,19 +10733,12 @@ Writer.prototype.float = function write_float(value) {
10534
10733
  * @returns {Writer} `this`
10535
10734
  */
10536
10735
  Writer.prototype.double = function write_double(value) {
10537
- return this._push(util.float.writeDoubleLE, 8, value);
10736
+ this._reserve(8);
10737
+ util.float.writeDoubleLE(value, this.buf, this.pos);
10738
+ this.pos += 8;
10739
+ return this;
10538
10740
  };
10539
10741
 
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
10742
  /**
10551
10743
  * Writes a sequence of bytes.
10552
10744
  * @param {Uint8Array|string} value Buffer or base64 encoded string to write
@@ -10554,14 +10746,21 @@ var writeBytes = util.Array.prototype.set
10554
10746
  */
10555
10747
  Writer.prototype.bytes = function write_bytes(value) {
10556
10748
  var len = value.length >>> 0;
10557
- if (!len)
10558
- return this._push(writeByte, 1, 0);
10749
+ if (!len) {
10750
+ this._reserve(1);
10751
+ this.buf[this.pos++] = 0;
10752
+ return this;
10753
+ }
10559
10754
  if (util.isString(value)) {
10560
10755
  var buf = Writer.alloc(len = base64.length(value));
10561
10756
  base64.decode(value, buf, 0);
10562
10757
  value = buf;
10563
10758
  }
10564
- return this.uint32(len)._push(writeBytes, len, value);
10759
+ this.uint32(len);
10760
+ this._reserve(len);
10761
+ this.buf.set(value, this.pos);
10762
+ this.pos += len;
10763
+ return this;
10565
10764
  };
10566
10765
 
10567
10766
  /**
@@ -10571,7 +10770,28 @@ Writer.prototype.bytes = function write_bytes(value) {
10571
10770
  */
10572
10771
  Writer.prototype.raw = function write_raw(value) {
10573
10772
  var len = value.length >>> 0;
10574
- return len ? this._push(writeBytes, len, value) : this;
10773
+ if (!len)
10774
+ return this;
10775
+ this._reserve(len);
10776
+ this.buf.set(value, this.pos);
10777
+ this.pos += len;
10778
+ return this;
10779
+ };
10780
+
10781
+ /**
10782
+ * Backfills the length varint.
10783
+ * @param {number} pos Position of reserved length byte
10784
+ * @param {number} len Length of content after length varint
10785
+ * @returns {Writer} `this`
10786
+ * @private
10787
+ */
10788
+ Writer.prototype._delim = function _delim(pos, len) {
10789
+ var n = sizeVarint32(len);
10790
+ if (n > 1)
10791
+ this.buf.copyWithin(pos + n, pos + 1, pos + 1 + len);
10792
+ writeVarint32(len, this.buf, pos);
10793
+ this.pos = pos + n + len;
10794
+ return this;
10575
10795
  };
10576
10796
 
10577
10797
  /**
@@ -10580,21 +10800,245 @@ Writer.prototype.raw = function write_raw(value) {
10580
10800
  * @returns {Writer} `this`
10581
10801
  */
10582
10802
  Writer.prototype.string = function write_string(value) {
10803
+ var n = value.length;
10804
+ if (!n) {
10805
+ this._reserve(1);
10806
+ this.buf[this.pos++] = 0;
10807
+ return this;
10808
+ }
10809
+ if (n < 0x80) {
10810
+ this._reserve(n * 3 + 5); // worst case
10811
+ var lenPos = this.pos;
10812
+ return this._delim(lenPos, utf8.write(value, this.buf, lenPos + 1));
10813
+ }
10583
10814
  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);
10815
+ this.uint32(len);
10816
+ this._reserve(len);
10817
+ if (len === value.length)
10818
+ writeStringAscii(value, this.buf, this.pos);
10819
+ else
10820
+ utf8.write(value, this.buf, this.pos);
10821
+ this.pos += len;
10822
+ return this;
10823
+ };
10824
+
10825
+ /**
10826
+ * Writes an array of unsigned 32 bit values as a packed repeated field.
10827
+ * @param {number[]} value Values to write
10828
+ * @returns {Writer} `this`
10829
+ */
10830
+ Writer.prototype.uint32s = function write_uint32s(value) {
10831
+ var n = value.length;
10832
+ this._reserve(n * 5 + 5); // worst case: 5 bytes per value + length varint
10833
+ var buf = this.buf, lenPos = this.pos, p = lenPos + 1;
10834
+ for (var i = 0; i < n; ++i)
10835
+ p = writeVarint32(value[i] >>> 0, buf, p);
10836
+ return this._delim(lenPos, p - lenPos - 1);
10837
+ };
10838
+
10839
+ /**
10840
+ * Writes an array of signed 32 bit values as a packed repeated field.
10841
+ * @param {number[]} value Values to write
10842
+ * @returns {Writer} `this`
10843
+ */
10844
+ Writer.prototype.int32s = function write_int32s(value) {
10845
+ var n = value.length;
10846
+ this._reserve(n * 10 + 5); // worst case: 10 bytes per negative value + length varint
10847
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1, val;
10848
+ for (var i = 0; i < n; ++i) {
10849
+ if ((val = value[i] | 0) < 0) { // negatives are 10 bytes per spec
10850
+ pos = writeVarint64(LongBits.fromNumber(val), buf, pos);
10851
+ } else {
10852
+ pos = writeVarint32(val, buf, pos);
10853
+ }
10854
+ }
10855
+ return this._delim(lenPos, pos - lenPos - 1);
10856
+ };
10857
+
10858
+ /**
10859
+ * Writes an array of 32 bit values as packed, zig-zag encoded varints.
10860
+ * @param {number[]} value Values to write
10861
+ * @returns {Writer} `this`
10862
+ */
10863
+ Writer.prototype.sint32s = function write_sint32s(value) {
10864
+ var n = value.length;
10865
+ this._reserve(n * 5 + 5);
10866
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
10867
+ for (var i = 0; i < n; ++i)
10868
+ pos = writeVarint32((value[i] << 1 ^ value[i] >> 31) >>> 0, buf, pos);
10869
+ return this._delim(lenPos, pos - lenPos - 1);
10870
+ };
10871
+
10872
+ /**
10873
+ * Writes an array of unsigned 64 bit values as a packed repeated field.
10874
+ * @param {Array.<Long|number|string>} value Values to write
10875
+ * @returns {Writer} `this`
10876
+ */
10877
+ Writer.prototype.uint64s = function write_uint64s(value) {
10878
+ var n = value.length;
10879
+ this._reserve(n * 10 + 5);
10880
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
10881
+ for (var i = 0; i < n; ++i) {
10882
+ pos = writeVarint64(LongBits.from(value[i]), buf, pos);
10883
+ }
10884
+ return this._delim(lenPos, pos - lenPos - 1);
10885
+ };
10886
+
10887
+ /**
10888
+ * Writes an array of signed 64 bit values as a packed repeated field.
10889
+ * @function
10890
+ * @param {Array.<Long|number|string>} value Values to write
10891
+ * @returns {Writer} `this`
10892
+ */
10893
+ Writer.prototype.int64s = Writer.prototype.uint64s;
10894
+
10895
+ /**
10896
+ * Writes an array of 64 bit values as packed, zig-zag encoded varints.
10897
+ * @param {Array.<Long|number|string>} value Values to write
10898
+ * @returns {Writer} `this`
10899
+ */
10900
+ Writer.prototype.sint64s = function write_sint64s(value) {
10901
+ var n = value.length;
10902
+ this._reserve(n * 10 + 5);
10903
+ var buf = this.buf, lenPos = this.pos, pos = lenPos + 1;
10904
+ for (var i = 0; i < n; ++i) {
10905
+ pos = writeVarint64(LongBits.from(value[i]).zzEncode(), buf, pos);
10906
+ }
10907
+ return this._delim(lenPos, pos - lenPos - 1);
10908
+ };
10909
+
10910
+ /**
10911
+ * Writes an array of boolish values as a packed repeated field.
10912
+ * @param {boolean[]} value Values to write
10913
+ * @returns {Writer} `this`
10914
+ */
10915
+ Writer.prototype.bools = function write_bools(value) {
10916
+ var n = value.length;
10917
+ this.uint32(n); // one byte per value
10918
+ this._reserve(n);
10919
+ var buf = this.buf, p = this.pos;
10920
+ for (var i = 0; i < n; ++i)
10921
+ buf[p++] = value[i] ? 1 : 0;
10922
+ this.pos += n;
10923
+ return this;
10924
+ };
10925
+
10926
+ // The view allocation only pays off when amortized over enough writes
10927
+ var VIEW_THRESHOLD_FLOAT = 16,
10928
+ VIEW_THRESHOLD_INT = 128;
10929
+
10930
+ function getLazyView(writer, count, threshold) {
10931
+ var view = writer.view;
10932
+ if (view || count < threshold)
10933
+ return view;
10934
+ var buf = writer.buf;
10935
+ return writer.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
10936
+ }
10937
+
10938
+ /**
10939
+ * Writes an array of unsigned 32 bit values as packed, fixed 32 bits.
10940
+ * @param {number[]} value Values to write
10941
+ * @returns {Writer} `this`
10942
+ */
10943
+ Writer.prototype.fixed32s = function write_fixed32s(value) {
10944
+ var n = value.length, bytes = n * 4;
10945
+ this.uint32(bytes); // length is known exactly
10946
+ this._reserve(bytes);
10947
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_INT);
10948
+ if (dv)
10949
+ for (i = 0; i < n; ++i) { dv.setUint32(p, value[i] >>> 0, true); p += 4; }
10950
+ else {
10951
+ var buf = this.buf;
10952
+ for (i = 0; i < n; ++i) { writeFixed32(value[i] >>> 0, buf, p); p += 4; }
10953
+ }
10954
+ this.pos += bytes;
10955
+ return this;
10956
+ };
10957
+
10958
+ /**
10959
+ * Writes an array of signed 32 bit values as packed, fixed 32 bits.
10960
+ * @function
10961
+ * @param {number[]} value Values to write
10962
+ * @returns {Writer} `this`
10963
+ */
10964
+ Writer.prototype.sfixed32s = Writer.prototype.fixed32s;
10965
+
10966
+ /**
10967
+ * Writes an array of unsigned 64 bit values as packed, fixed 64 bits.
10968
+ * @param {Array.<Long|number|string>} value Values to write
10969
+ * @returns {Writer} `this`
10970
+ */
10971
+ Writer.prototype.fixed64s = function write_fixed64s(value) {
10972
+ var n = value.length, bytes = n * 8;
10973
+ this.uint32(bytes);
10974
+ this._reserve(bytes);
10975
+ var p = this.pos, i, bits, dv = getLazyView(this, n, VIEW_THRESHOLD_INT);
10976
+ if (dv)
10977
+ 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; }
10978
+ else {
10979
+ var buf = this.buf;
10980
+ for (i = 0; i < n; ++i) { bits = LongBits.from(value[i]); writeFixed32(bits.lo, buf, p); writeFixed32(bits.hi, buf, p + 4); p += 8; }
10981
+ }
10982
+ this.pos += bytes;
10983
+ return this;
10984
+ };
10985
+
10986
+ /**
10987
+ * Writes an array of signed 64 bit values as packed, fixed 64 bits.
10988
+ * @function
10989
+ * @param {Array.<Long|number|string>} value Values to write
10990
+ * @returns {Writer} `this`
10991
+ */
10992
+ Writer.prototype.sfixed64s = Writer.prototype.fixed64s;
10993
+
10994
+ /**
10995
+ * Writes an array of floats (32 bit) as a packed repeated field.
10996
+ * @param {number[]} value Values to write
10997
+ * @returns {Writer} `this`
10998
+ */
10999
+ Writer.prototype.floats = function write_floats(value) {
11000
+ var n = value.length, bytes = n * 4;
11001
+ this.uint32(bytes);
11002
+ this._reserve(bytes);
11003
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_FLOAT);
11004
+ if (dv)
11005
+ for (i = 0; i < n; ++i) { dv.setFloat32(p, value[i], true); p += 4; }
11006
+ else {
11007
+ var buf = this.buf;
11008
+ for (i = 0; i < n; ++i) { util.float.writeFloatLE(value[i], buf, p); p += 4; }
11009
+ }
11010
+ this.pos += bytes;
11011
+ return this;
11012
+ };
11013
+
11014
+ /**
11015
+ * Writes an array of doubles (64 bit float) as a packed repeated field.
11016
+ * @param {number[]} value Values to write
11017
+ * @returns {Writer} `this`
11018
+ */
11019
+ Writer.prototype.doubles = function write_doubles(value) {
11020
+ var n = value.length, bytes = n * 8;
11021
+ this.uint32(bytes);
11022
+ this._reserve(bytes);
11023
+ var p = this.pos, i, dv = getLazyView(this, n, VIEW_THRESHOLD_FLOAT);
11024
+ if (dv)
11025
+ for (i = 0; i < n; ++i) { dv.setFloat64(p, value[i], true); p += 8; }
11026
+ else {
11027
+ var buf = this.buf;
11028
+ for (i = 0; i < n; ++i) { util.float.writeDoubleLE(value[i], buf, p); p += 8; }
11029
+ }
11030
+ this.pos += bytes;
11031
+ return this;
10587
11032
  };
10588
11033
 
10589
11034
  /**
10590
11035
  * 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
11036
  * @returns {Writer} `this`
10593
11037
  */
10594
11038
  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;
11039
+ this._reserve(1);
11040
+ (this.states || (this.states = [])).push(this.pos);
11041
+ this.pos += 1;
10598
11042
  return this;
10599
11043
  };
10600
11044
 
@@ -10603,47 +11047,66 @@ Writer.prototype.fork = function fork() {
10603
11047
  * @returns {Writer} `this`
10604
11048
  */
10605
11049
  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;
11050
+ var states = this.states;
11051
+ if (states && states.length) {
11052
+ this.pos = states.pop();
10611
11053
  } else {
10612
- this.head = this.tail = new Op(noop, 0, 0);
10613
- this.len = 0;
11054
+ this.pos = 0;
10614
11055
  }
10615
11056
  return this;
10616
11057
  };
10617
11058
 
10618
11059
  /**
10619
- * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
11060
+ * Resets to the last state and prepends the fork state's current write length as a varint.
10620
11061
  * @returns {Writer} `this`
10621
11062
  */
10622
11063
  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;
11064
+ var states = this.states,
11065
+ len,
11066
+ vlen;
11067
+ if (states && states.length) {
11068
+ var lenPos = states.pop();
11069
+ len = this.pos - lenPos - 1;
11070
+ vlen = sizeVarint32(len);
11071
+ if (vlen > 1) { // grow the reserved single byte and shift content forward
11072
+ this._reserve(vlen - 1);
11073
+ this.buf.copyWithin(lenPos + vlen, lenPos + 1, lenPos + 1 + len);
11074
+ this.pos += vlen - 1;
11075
+ writeVarint32(len, this.buf, lenPos);
11076
+ } else {
11077
+ this.buf[lenPos] = len;
11078
+ }
11079
+ } else { // not forked: prefix the entire buffer with its length
11080
+ // TODO: Compatibility with older generated code.
11081
+ // Remove this branch in the next major release.
11082
+ len = this.pos;
11083
+ vlen = sizeVarint32(len);
11084
+ this._reserve(vlen);
11085
+ this.buf.copyWithin(vlen, 0, len);
11086
+ writeVarint32(len, this.buf, 0);
11087
+ this.pos += vlen;
10631
11088
  }
10632
11089
  return this;
10633
11090
  };
10634
11091
 
10635
11092
  /**
10636
11093
  * Finishes the write operation.
11094
+ * Returns a buffer sized to the written data by default.
11095
+ * @param {boolean} [shared=false] Whether to return a shared view instead of a unique copy
10637
11096
  * @returns {Uint8Array} Finished buffer
10638
11097
  */
10639
- Writer.prototype.finish = function finish() {
10640
- return this.finishInto(this.constructor.alloc(this.len), 0);
11098
+ Writer.prototype.finish = function finish(shared) {
11099
+ if (shared)
11100
+ return this.buf.subarray(0, this.pos);
11101
+ var buf = this.constructor.alloc(this.pos);
11102
+ buf.set(this.buf.subarray(0, this.pos), 0);
11103
+ return buf;
10641
11104
  };
10642
11105
 
10643
11106
  /**
10644
11107
  * Finishes the write operation, writing into the provided buffer.
10645
11108
  * The caller must ensure that `buf` has enough space starting at `offset`
10646
- * to hold {@link Writer#len} bytes.
11109
+ * to hold {@link Writer#pos} bytes.
10647
11110
  * @param {T} buf Target buffer
10648
11111
  * @param {number} [offset=0] Offset to start writing at
10649
11112
  * @returns {T} The provided buffer
@@ -10652,13 +11115,7 @@ Writer.prototype.finish = function finish() {
10652
11115
  Writer.prototype.finishInto = function finishInto(buf, offset) {
10653
11116
  if (offset === undefined)
10654
11117
  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
- }
11118
+ buf.set(this.buf.subarray(0, this.pos), offset);
10662
11119
  return buf;
10663
11120
  };
10664
11121
 
@@ -10695,6 +11152,8 @@ function BufferWriter() {
10695
11152
  Writer.call(this);
10696
11153
  }
10697
11154
 
11155
+ var writeStringBuffer;
11156
+
10698
11157
  BufferWriter._configure = function () {
10699
11158
  /**
10700
11159
  * Allocates a buffer of the specified size.
@@ -10702,19 +11161,14 @@ BufferWriter._configure = function () {
10702
11161
  * @param {number} size Buffer size
10703
11162
  * @returns {Buffer} Buffer
10704
11163
  */
10705
- BufferWriter.alloc = util._Buffer_allocUnsafe;
11164
+ BufferWriter.alloc = util.Buffer && util.Buffer.allocUnsafe;
10706
11165
 
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
11166
+ writeStringBuffer = util.Buffer && util.Buffer.prototype.utf8Write
11167
+ ? function writeStringBuffer_utf8Write(val, buf, pos) {
11168
+ return buf.utf8Write(val, pos);
10711
11169
  }
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++];
11170
+ : function writeStringBuffer_write(val, buf, pos) {
11171
+ return buf.write(val, pos);
10718
11172
  };
10719
11173
  };
10720
11174
 
@@ -10724,48 +11178,42 @@ BufferWriter._configure = function () {
10724
11178
  */
10725
11179
  BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
10726
11180
  if (util.isString(value))
10727
- value = util._Buffer_from(value, "base64");
11181
+ value = util.Buffer.from(value, "base64");
10728
11182
  var len = value.length >>> 0;
10729
11183
  this.uint32(len);
10730
- if (len)
10731
- this._push(BufferWriter.writeBytesBuffer, len, value);
11184
+ if (len) {
11185
+ this._reserve(len);
11186
+ this.buf.set(value, this.pos);
11187
+ this.pos += len;
11188
+ }
10732
11189
  return this;
10733
11190
  };
10734
11191
 
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
11192
  /**
10762
11193
  * @override
10763
11194
  */
10764
11195
  BufferWriter.prototype.string = function write_string_buffer(value) {
11196
+ var n = value.length;
11197
+ if (!n) {
11198
+ this._reserve(1);
11199
+ this.buf[this.pos++] = 0;
11200
+ return this;
11201
+ }
11202
+ if (n < 0x80) {
11203
+ this._reserve(n * 3 + 5); // worst case
11204
+ var pos = this.pos,
11205
+ buf = this.buf;
11206
+ return this._delim(pos,
11207
+ n < 40
11208
+ ? util.utf8.write(value, buf, pos + 1)
11209
+ : writeStringBuffer(value, buf, pos + 1)
11210
+ );
11211
+ }
10765
11212
  var len = util.Buffer.byteLength(value);
10766
11213
  this.uint32(len);
10767
- if (len)
10768
- this._push(len === value.length && len < 40 ? writeStringBufferAscii : writeStringBuffer, len, value);
11214
+ this._reserve(len);
11215
+ writeStringBuffer(value, this.buf, this.pos);
11216
+ this.pos += len;
10769
11217
  return this;
10770
11218
  };
10771
11219