protobufjs 8.2.0 → 8.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * protobuf.js v8.2.0 (c) 2016, daniel wirtz
3
- * compiled sat, 09 may 2026 19:37:43 utc
2
+ * protobuf.js v8.3.0 (c) 2016, daniel wirtz
3
+ * compiled wed, 13 may 2026 21:15:34 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -149,15 +149,15 @@ converter.fromObject = function fromObject(mtype) {
149
149
  /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
150
150
  var fields = mtype.fieldsArray;
151
151
  var gen = util.codegen(["d", "q"], mtype.name + "$fromObject")
152
- ("if(d instanceof this.ctor)")
152
+ ("if(d instanceof C)")
153
153
  ("return d")
154
154
  ("if(q===undefined)q=0")
155
155
  ("if(q>util.recursionLimit)")
156
156
  ("throw Error(\"max depth exceeded\")");
157
157
  if (!fields.length) return gen
158
- ("return new this.ctor");
158
+ ("return new C");
159
159
  gen
160
- ("var m=new this.ctor");
160
+ ("var m=new C");
161
161
  for (var i = 0; i < fields.length; ++i) {
162
162
  var field = fields[i].resolve(),
163
163
  prop = util.safeProp(field.name),
@@ -413,7 +413,7 @@ function decoder(mtype) {
413
413
  ("if(q===undefined)q=0")
414
414
  ("if(q>Reader.recursionLimit)")
415
415
  ("throw Error(\"max depth exceeded\")")
416
- ("var c=l===undefined?r.len:r.pos+l,m=g||new this.ctor" + (hasMapField ? ",k,v" : hasImplicitPresenceField ? ",v" : ""))
416
+ ("var c=l===undefined?r.len:r.pos+l,m=g||new C" + (hasMapField ? ",k,v" : hasImplicitPresenceField ? ",v" : ""))
417
417
  ("while(r.pos<c){")
418
418
  ("var s=r.pos")
419
419
  ("var t=r.tag()")
@@ -537,7 +537,11 @@ function decoder(mtype) {
537
537
  ("case %i:{", field.id)
538
538
  ("if(u!==%i)", types.basic[type])
539
539
  ("break");
540
- if (type === "string" || type === "bytes") gen
540
+ if (field.resolvedType instanceof Enum && field.typeDefault !== 0) gen
541
+ // TODO: Protoc rejects open enums whose first value is not zero.
542
+ // We should do the same, but for v8 this would be a regression.
543
+ ("if((v=r.%s())!==%j)", type, field.typeDefault);
544
+ else if (type === "string" || type === "bytes") gen
541
545
  ("if((v=r.%s()).length)", type);
542
546
  else if (types.long[type] !== undefined) gen
543
547
  ("if(typeof(v=r.%s())===\"object\"?v.low||v.high:v!==0)", type);
@@ -710,7 +714,7 @@ var Namespace = require(12),
710
714
  * @param {Object.<string,number>} [values] Enum values as an object, by name
711
715
  * @param {Object.<string,*>} [options] Declared options
712
716
  * @param {string} [comment] The comment for this enum
713
- * @param {Object.<string,string>} [comments] The value comments for this enum
717
+ * @param {Object.<string,string|null>} [comments] The value comments for this enum
714
718
  * @param {Object.<string,Object<string,*>>|undefined} [valuesOptions] The value options for this enum
715
719
  */
716
720
  function Enum(name, values, options, comment, comments, valuesOptions) {
@@ -739,7 +743,7 @@ function Enum(name, values, options, comment, comments, valuesOptions) {
739
743
 
740
744
  /**
741
745
  * Value comment texts, if any.
742
- * @type {Object.<string,string>}
746
+ * @type {Object.<string,string|null>}
743
747
  */
744
748
  this.comments = comments || {};
745
749
 
@@ -789,8 +793,13 @@ Enum.prototype._resolveFeatures = function _resolveFeatures(edition) {
789
793
  /**
790
794
  * Enum descriptor.
791
795
  * @interface IEnum
796
+ * @property {string} [edition] Edition
792
797
  * @property {Object.<string,number>} values Enum values
793
798
  * @property {Object.<string,*>} [options] Enum options
799
+ * @property {Object.<string,Object.<string,*>>} [valuesOptions] Enum value options
800
+ * @property {Array.<number[]|string>} [reserved] Reserved ranges
801
+ * @property {string|null} [comment] Enum comment
802
+ * @property {Object.<string,string|null>} [comments] Value comments
794
803
  */
795
804
 
796
805
  /**
@@ -801,7 +810,7 @@ Enum.prototype._resolveFeatures = function _resolveFeatures(edition) {
801
810
  * @throws {TypeError} If arguments are invalid
802
811
  */
803
812
  Enum.fromJSON = function fromJSON(name, json) {
804
- var enm = new Enum(name, json.values, json.options, json.comment, json.comments);
813
+ var enm = new Enum(name, json.values, json.options, json.comment, json.comments, json.valuesOptions);
805
814
  enm.reserved = json.reserved;
806
815
  if (json.edition)
807
816
  enm._edition = json.edition;
@@ -1180,10 +1189,12 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
1180
1189
  /**
1181
1190
  * Field descriptor.
1182
1191
  * @interface IField
1192
+ * @property {string} [edition] Edition
1183
1193
  * @property {string} [rule="optional"] Field rule
1184
1194
  * @property {string} type Field type
1185
1195
  * @property {number} id Field id
1186
1196
  * @property {Object.<string,*>} [options] Field options
1197
+ * @property {string|null} [comment] Field comment
1187
1198
  */
1188
1199
 
1189
1200
  /**
@@ -1666,12 +1677,9 @@ var util = require(34);
1666
1677
  function Message(properties) {
1667
1678
  // not used internally
1668
1679
  if (properties)
1669
- for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i) {
1670
- var key = keys[i];
1671
- if (key === "__proto__")
1672
- continue;
1673
- this[key] = properties[key];
1674
- }
1680
+ for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
1681
+ if (properties[keys[i]] != null && keys[i] !== "__proto__")
1682
+ this[keys[i]] = properties[keys[i]];
1675
1683
  }
1676
1684
 
1677
1685
  /**
@@ -1905,7 +1913,7 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
1905
1913
  * @property {boolean} [requestStream=false] Whether requests are streamed
1906
1914
  * @property {boolean} [responseStream=false] Whether responses are streamed
1907
1915
  * @property {Object.<string,*>} [options] Method options
1908
- * @property {string} comment Method comments
1916
+ * @property {string|null} [comment] Method comment
1909
1917
  * @property {Array.<Object.<string,*>>} [parsedOptions] Method options properly parsed into objects
1910
1918
  */
1911
1919
 
@@ -1991,7 +1999,10 @@ var Type, // cyclic
1991
1999
  * @throws {TypeError} If arguments are invalid
1992
2000
  */
1993
2001
  Namespace.fromJSON = function fromJSON(name, json, depth) {
1994
- depth = util.checkDepth(depth);
2002
+ if (depth === undefined)
2003
+ depth = 0;
2004
+ if (depth > util.recursionLimit)
2005
+ throw Error("max depth exceeded");
1995
2006
  return new Namespace(name, json.options).addJSON(json.nested, depth);
1996
2007
  };
1997
2008
 
@@ -2154,7 +2165,10 @@ Namespace.prototype.toJSON = function toJSON(toJSONOptions) {
2154
2165
  * @returns {Namespace} `this`
2155
2166
  */
2156
2167
  Namespace.prototype.addJSON = function addJSON(nestedJson, depth) {
2157
- depth = util.checkDepth(depth);
2168
+ if (depth === undefined)
2169
+ depth = 0;
2170
+ if (depth > util.recursionLimit)
2171
+ throw Error("max depth exceeded");
2158
2172
  var ns = this;
2159
2173
  /* istanbul ignore else */
2160
2174
  if (nestedJson) {
@@ -2294,6 +2308,8 @@ Namespace.prototype.define = function define(path, json) {
2294
2308
  throw TypeError("illegal path");
2295
2309
  if (path && path.length && path[0] === "")
2296
2310
  throw Error("path must be relative");
2311
+ if (path.length > util.recursionLimit)
2312
+ throw Error("max depth exceeded");
2297
2313
 
2298
2314
  var ptr = this;
2299
2315
  while (path.length > 0) {
@@ -2658,7 +2674,6 @@ Object.defineProperties(ReflectionObject.prototype, {
2658
2674
  /**
2659
2675
  * Converts this reflection object to its descriptor representation.
2660
2676
  * @returns {Object.<string,*>} Descriptor
2661
- * @abstract
2662
2677
  */
2663
2678
  ReflectionObject.prototype.toJSON = /* istanbul ignore next */ function toJSON() {
2664
2679
  throw Error(); // not implemented, shouldn't happen
@@ -2955,6 +2970,7 @@ function OneOf(name, fieldNames, options, comment) {
2955
2970
  * @interface IOneOf
2956
2971
  * @property {Array.<string>} oneof Oneof field names
2957
2972
  * @property {Object.<string,*>} [options] Oneof options
2973
+ * @property {string|null} [comment] Oneof comment
2958
2974
  */
2959
2975
 
2960
2976
  /**
@@ -3793,7 +3809,10 @@ function Root(options) {
3793
3809
  * @returns {Root} Root namespace
3794
3810
  */
3795
3811
  Root.fromJSON = function fromJSON(json, root, depth) {
3796
- depth = util.checkDepth(depth);
3812
+ if (depth === undefined)
3813
+ depth = 0;
3814
+ if (depth > util.recursionLimit)
3815
+ throw Error("max depth exceeded");
3797
3816
  if (!root)
3798
3817
  root = new Root();
3799
3818
  if (json.options)
@@ -4386,7 +4405,9 @@ function Service(name, options) {
4386
4405
  * Service descriptor.
4387
4406
  * @interface IService
4388
4407
  * @extends INamespace
4408
+ * @property {string} [edition] Edition
4389
4409
  * @property {Object.<string,IMethod>} methods Method descriptors
4410
+ * @property {string|null} [comment] Service comment
4390
4411
  */
4391
4412
 
4392
4413
  /**
@@ -4398,7 +4419,10 @@ function Service(name, options) {
4398
4419
  * @throws {TypeError} If arguments are invalid
4399
4420
  */
4400
4421
  Service.fromJSON = function fromJSON(name, json, depth) {
4401
- depth = util.checkDepth(depth);
4422
+ if (depth === undefined)
4423
+ depth = 0;
4424
+ if (depth > util.recursionLimit)
4425
+ throw Error("max depth exceeded");
4402
4426
  var service = new Service(name, json.options);
4403
4427
  /* istanbul ignore else */
4404
4428
  if (json.methods)
@@ -4713,6 +4737,8 @@ Object.defineProperties(Type.prototype, {
4713
4737
  util.merge(ctor, Message, true);
4714
4738
 
4715
4739
  this._ctor = ctor;
4740
+ delete this.decode;
4741
+ delete this.fromObject;
4716
4742
 
4717
4743
  // Messages have non-enumerable default values on their prototype
4718
4744
  var i = 0;
@@ -4766,11 +4792,13 @@ function clearCache(type) {
4766
4792
  * Message type descriptor.
4767
4793
  * @interface IType
4768
4794
  * @extends INamespace
4795
+ * @property {string} [edition] Edition
4769
4796
  * @property {Object.<string,IOneOf>} [oneofs] Oneof descriptors
4770
4797
  * @property {Object.<string,IField>} fields Field descriptors
4771
4798
  * @property {number[][]} [extensions] Extension ranges
4772
4799
  * @property {Array.<number[]|string>} [reserved] Reserved ranges
4773
4800
  * @property {boolean} [group=false] Whether a legacy group or not
4801
+ * @property {string|null} [comment] Message type comment
4774
4802
  */
4775
4803
 
4776
4804
  /**
@@ -4781,7 +4809,10 @@ function clearCache(type) {
4781
4809
  * @returns {Type} Created message type
4782
4810
  */
4783
4811
  Type.fromJSON = function fromJSON(name, json, depth) {
4784
- depth = util.checkDepth(depth);
4812
+ if (depth === undefined)
4813
+ depth = 0;
4814
+ if (depth > util.nestingLimit)
4815
+ throw Error("max depth exceeded");
4785
4816
  var type = new Type(name, json.options);
4786
4817
  type.extensions = json.extensions;
4787
4818
  type.reserved = json.reserved;
@@ -5021,7 +5052,8 @@ Type.prototype.setup = function setup() {
5021
5052
  this.decode = decoder(this)({
5022
5053
  Reader : Reader,
5023
5054
  types : types,
5024
- util : util
5055
+ util : util,
5056
+ C : this.ctor
5025
5057
  });
5026
5058
  this.verify = verifier(this)({
5027
5059
  types : types,
@@ -5029,7 +5061,8 @@ Type.prototype.setup = function setup() {
5029
5061
  });
5030
5062
  this.fromObject = converter.fromObject(this)({
5031
5063
  types : types,
5032
- util : util
5064
+ util : util,
5065
+ C : this.ctor
5033
5066
  });
5034
5067
  this.toObject = converter.toObject(this)({
5035
5068
  types : types,
@@ -5404,20 +5437,6 @@ var reservedRe = util.patterns.reservedRe,
5404
5437
  */
5405
5438
  util.fs = require(31);
5406
5439
 
5407
- /**
5408
- * Checks a recursion depth.
5409
- * @param {number|undefined} depth Depth of recursion
5410
- * @returns {number} Depth of recursion
5411
- * @throws {Error} If depth exceeds util.recursionLimit
5412
- */
5413
- util.checkDepth = function checkDepth(depth) {
5414
- if (depth === undefined)
5415
- depth = 0;
5416
- if (depth > util.recursionLimit)
5417
- throw Error("max depth exceeded");
5418
- return depth;
5419
- };
5420
-
5421
5440
  /**
5422
5441
  * Converts an object's values to an array.
5423
5442
  * @param {Object.<string,*>} object Object to convert
@@ -5594,6 +5613,8 @@ util.setProperty = function setProperty(dst, path, value, ifNotSet) {
5594
5613
  throw TypeError("path must be specified");
5595
5614
 
5596
5615
  path = path.split(".");
5616
+ if (path.length > util.recursionLimit)
5617
+ throw Error("max depth exceeded");
5597
5618
  return setProp(dst, path, value);
5598
5619
  };
5599
5620
 
@@ -6995,12 +7016,19 @@ function merge(dst, src, ifNotSet) { // used by converters
6995
7016
 
6996
7017
  util.merge = merge;
6997
7018
 
7019
+ /**
7020
+ * Schema declaration nesting limit.
7021
+ * @memberof util
7022
+ * @type {number}
7023
+ */
7024
+ util.nestingLimit = 32; // protoc: MaxMessageDeclarationNestingDepth
7025
+
6998
7026
  /**
6999
7027
  * Recursion limit.
7000
7028
  * @memberof util
7001
7029
  * @type {number}
7002
7030
  */
7003
- util.recursionLimit = 100;
7031
+ util.recursionLimit = 100; // protoc: CodedInputStream::default_recursion_limit_
7004
7032
 
7005
7033
  /**
7006
7034
  * Makes a property safe for assignment as an own property.
@@ -7382,19 +7410,7 @@ utf8.length = function utf8_length(string) {
7382
7410
  return len;
7383
7411
  };
7384
7412
 
7385
- /**
7386
- * Reads UTF8 bytes as a string.
7387
- * @param {Uint8Array} buffer Source buffer
7388
- * @param {number} start Source start
7389
- * @param {number} end Source end
7390
- * @returns {string} String read
7391
- */
7392
- utf8.read = function utf8_read(buffer, start, end) {
7393
- if (end - start < 1) {
7394
- return "";
7395
- }
7396
-
7397
- var str = "";
7413
+ function utf8_read_js(buffer, start, end, str) {
7398
7414
  for (var i = start; i < end;) {
7399
7415
  var t = buffer[i++];
7400
7416
  if (t <= 0x7F) {
@@ -7416,6 +7432,44 @@ utf8.read = function utf8_read(buffer, start, end) {
7416
7432
  }
7417
7433
  }
7418
7434
  }
7435
+ return str;
7436
+ }
7437
+
7438
+ /**
7439
+ * Reads UTF8 bytes as a string.
7440
+ * @param {Uint8Array} buffer Source buffer
7441
+ * @param {number} start Source start
7442
+ * @param {number} end Source end
7443
+ * @returns {string} String read
7444
+ */
7445
+ utf8.read = function utf8_read_ascii(buffer, start, end) {
7446
+ if (end - start < 1)
7447
+ return "";
7448
+
7449
+ var str = "",
7450
+ i = start,
7451
+ c1, c2, c3, c4, c5, c6, c7, c8;
7452
+
7453
+ for (; i + 7 < end; i += 8) {
7454
+ c1 = buffer[i];
7455
+ c2 = buffer[i + 1];
7456
+ c3 = buffer[i + 2];
7457
+ c4 = buffer[i + 3];
7458
+ c5 = buffer[i + 4];
7459
+ c6 = buffer[i + 5];
7460
+ c7 = buffer[i + 6];
7461
+ c8 = buffer[i + 7];
7462
+ if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80)
7463
+ return utf8_read_js(buffer, i, end, str);
7464
+ str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
7465
+ }
7466
+
7467
+ for (; i < end; ++i) {
7468
+ c1 = buffer[i];
7469
+ if (c1 & 0x80)
7470
+ return utf8_read_js(buffer, i, end, str);
7471
+ str += String.fromCharCode(c1);
7472
+ }
7419
7473
 
7420
7474
  return str;
7421
7475
  };
@@ -7917,6 +7971,11 @@ function writeByte(val, buf, pos) {
7917
7971
  buf[pos] = val & 255;
7918
7972
  }
7919
7973
 
7974
+ function writeStringAscii(val, buf, pos) {
7975
+ for (var i = 0; i < val.length;)
7976
+ buf[pos++] = val.charCodeAt(i++);
7977
+ }
7978
+
7920
7979
  function writeVarint32(val, buf, pos) {
7921
7980
  while (val > 127) {
7922
7981
  buf[pos++] = val & 127 | 128;
@@ -8145,7 +8204,7 @@ Writer.prototype.raw = function write_raw(value) {
8145
8204
  Writer.prototype.string = function write_string(value) {
8146
8205
  var len = utf8.length(value);
8147
8206
  return len
8148
- ? this.uint32(len)._push(utf8.write, len, value)
8207
+ ? this.uint32(len)._push(len === value.length ? writeStringAscii : utf8.write, len, value)
8149
8208
  : this._push(writeByte, 1, 0);
8150
8209
  };
8151
8210
 
@@ -8300,6 +8359,11 @@ BufferWriter.prototype.raw = function write_raw_buffer(value) {
8300
8359
  return len ? this._push(BufferWriter.writeBytesBuffer, len, value) : this;
8301
8360
  };
8302
8361
 
8362
+ function writeStringBufferAscii(val, buf, pos) {
8363
+ for (var i = 0; i < val.length;)
8364
+ buf[pos++] = val.charCodeAt(i++);
8365
+ }
8366
+
8303
8367
  function writeStringBuffer(val, buf, pos) {
8304
8368
  if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
8305
8369
  util.utf8.write(val, buf, pos);
@@ -8316,7 +8380,7 @@ BufferWriter.prototype.string = function write_string_buffer(value) {
8316
8380
  var len = util.Buffer.byteLength(value);
8317
8381
  this.uint32(len);
8318
8382
  if (len)
8319
- this._push(writeStringBuffer, len, value);
8383
+ this._push(len === value.length && len < 40 ? writeStringBufferAscii : writeStringBuffer, len, value);
8320
8384
  return this;
8321
8385
  };
8322
8386