protobufjs 8.5.0 → 8.6.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.5.0 (c) 2016, daniel wirtz
3
- * compiled fri, 29 may 2026 22:57:25 utc
2
+ * protobuf.js v8.6.0 (c) 2016, daniel wirtz
3
+ * compiled thu, 04 jun 2026 21:47:13 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -549,7 +549,7 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
549
549
  converter.fromObject = function fromObject(mtype) {
550
550
  /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
551
551
  var fields = mtype.fieldsArray;
552
- var gen = util.codegen(["d", "q"], mtype.name + "$fromObject")
552
+ var gen = util.codegen(["d", "q"])
553
553
  ("if(d instanceof C)")
554
554
  ("return d")
555
555
  ("if(!util.isObject(d))")
@@ -683,7 +683,7 @@ converter.toObject = function toObject(mtype) {
683
683
  var fields = mtype.fieldsArray.slice().sort(util.compareFieldsById);
684
684
  if (!fields.length)
685
685
  return util.codegen()("return {}");
686
- var gen = util.codegen(["m", "o", "q"], mtype.name + "$toObject")
686
+ var gen = util.codegen(["m", "o", "q"])
687
687
  ("if(!o)")
688
688
  ("o={}")
689
689
  ("if(q===undefined)q=0")
@@ -772,7 +772,7 @@ converter.toObject = function toObject(mtype) {
772
772
  genValuePartial_toObject(gen, field, /* sorted */ index, prop + "[j]")
773
773
  ("}");
774
774
  } else { gen
775
- ("if(m%s!=null&&m.hasOwnProperty(%j)){", prop, field.name); // !== undefined && !== null
775
+ ("if(m%s!=null&&Object.hasOwnProperty.call(m,%j)){", prop, field.name); // !== undefined && !== null
776
776
  genValuePartial_toObject(gen, field, /* sorted */ index, prop);
777
777
  if (field.partOf && !field.partOf.isProto3Optional) gen
778
778
  ("if(o.oneofs)")
@@ -815,7 +815,7 @@ function decoder(mtype) {
815
815
  if (!pfield.repeated && !pfield.map && !pfield.hasPresence)
816
816
  hasImplicitPresenceField = true;
817
817
  }
818
- var gen = util.codegen(["r", "l", "z", "q", "g"], mtype.name + "$decode")
818
+ var gen = util.codegen(["r", "l", "z", "q", "g"])
819
819
  ("if(!(r instanceof Reader))")
820
820
  ("r=Reader.create(r)")
821
821
  ("if(q===undefined)q=0")
@@ -987,7 +987,7 @@ function decoder(mtype) {
987
987
  for (i = 0; i < mtype._fieldsArray.length; ++i) {
988
988
  var rfield = mtype._fieldsArray[i];
989
989
  if (rfield.required) gen
990
- ("if(!m.hasOwnProperty(%j))", rfield.name)
990
+ ("if(!Object.hasOwnProperty.call(m,%j))", rfield.name)
991
991
  ("throw util.ProtocolError(%j,{instance:m})", missing(rfield));
992
992
  }
993
993
 
@@ -1026,7 +1026,7 @@ function genTypePartial(gen, field, fieldIndex, ref) {
1026
1026
  */
1027
1027
  function encoder(mtype) {
1028
1028
  /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
1029
- var gen = util.codegen(["m", "w", "q"], mtype.name + "$encode")
1029
+ var gen = util.codegen(["m", "w", "q"])
1030
1030
  ("if(!w)")
1031
1031
  ("w=Writer.create()")
1032
1032
  ("if(q===undefined)q=0")
@@ -1383,6 +1383,12 @@ Field.fromJSON = function fromJSON(name, json) {
1383
1383
  var field = new Field(name, json.id, json.type, json.rule, json.extend, json.options, json.comment);
1384
1384
  if (json.edition)
1385
1385
  field._edition = json.edition;
1386
+ if (json.protoName)
1387
+ field.protoName = json.protoName;
1388
+ if (json.jsonName !== undefined)
1389
+ field.jsonName = json.jsonName;
1390
+ else if (json.options && json.options.json_name !== undefined)
1391
+ field.jsonName = json.options.json_name;
1386
1392
  field._defaultEdition = "proto3"; // For backwards-compatibility.
1387
1393
  return field;
1388
1394
  };
@@ -1522,6 +1528,18 @@ function Field(name, id, type, rule, extend, options, comment) {
1522
1528
  * @type {string|null}
1523
1529
  */
1524
1530
  this.comment = comment;
1531
+
1532
+ /**
1533
+ * Field name as declared in the .proto source, if different from `name`.
1534
+ * @type {string|undefined}
1535
+ */
1536
+ this.protoName = undefined;
1537
+
1538
+ /**
1539
+ * JSON name, if different from the derived default.
1540
+ * @type {string|undefined}
1541
+ */
1542
+ this.jsonName = undefined;
1525
1543
  }
1526
1544
 
1527
1545
  /**
@@ -1591,6 +1609,22 @@ Object.defineProperty(Field.prototype, "hasPresence", {
1591
1609
  }
1592
1610
  });
1593
1611
 
1612
+ /**
1613
+ * The field name as declared in the .proto source (snake_case). Populated on resolve,
1614
+ * falling back to `name`. Mirrors `FieldDescriptorProto.name`.
1615
+ * @name Field#protoName
1616
+ * @type {string}
1617
+ * @readonly
1618
+ */
1619
+
1620
+ /**
1621
+ * The JSON name of this field (lowerCamelCase per protoc's `ToJsonName`, or an
1622
+ * explicit `[json_name]`). Populated on resolve. This is the key used on ProtoJSON output.
1623
+ * @name Field#jsonName
1624
+ * @type {string}
1625
+ * @readonly
1626
+ */
1627
+
1594
1628
  /**
1595
1629
  * @override
1596
1630
  */
@@ -1624,13 +1658,15 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
1624
1658
  Field.prototype.toJSON = function toJSON(toJSONOptions) {
1625
1659
  var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
1626
1660
  return util.toObject([
1627
- "edition" , this._editionToJSON(),
1628
- "rule" , this.rule !== "optional" && this.rule || undefined,
1629
- "type" , this.type,
1630
- "id" , this.id,
1631
- "extend" , this.extend,
1632
- "options" , this.options,
1633
- "comment" , keepComments ? this.comment : undefined
1661
+ "edition" , this._editionToJSON(),
1662
+ "rule" , this.rule !== "optional" && this.rule || undefined,
1663
+ "type" , this.type,
1664
+ "id" , this.id,
1665
+ "extend" , this.extend,
1666
+ "protoName" , this.protoName !== this.name ? this.protoName : undefined,
1667
+ "jsonName" , this.jsonName !== util.jsonName(this.protoName || this.name) ? this.jsonName : undefined,
1668
+ "options" , this.options,
1669
+ "comment" , keepComments ? this.comment : undefined
1634
1670
  ]);
1635
1671
  };
1636
1672
 
@@ -1699,6 +1735,12 @@ Field.prototype.resolve = function resolve() {
1699
1735
  if (this.parent instanceof Type && this.parent._ctor)
1700
1736
  this.parent._ctor.prototype[this.name] = this.defaultValue;
1701
1737
 
1738
+ // derive the proto/JSON names
1739
+ if (this.protoName === undefined)
1740
+ this.protoName = this.name;
1741
+ if (this.jsonName === undefined)
1742
+ this.jsonName = util.jsonName(this.protoName);
1743
+
1702
1744
  return ReflectionObject.prototype.resolve.call(this);
1703
1745
  };
1704
1746
 
@@ -2025,7 +2067,14 @@ function MapField(name, id, keyType, type, options, comment) {
2025
2067
  * @throws {TypeError} If arguments are invalid
2026
2068
  */
2027
2069
  MapField.fromJSON = function fromJSON(name, json) {
2028
- return new MapField(name, json.id, json.keyType, json.type, json.options, json.comment);
2070
+ var field = new MapField(name, json.id, json.keyType, json.type, json.options, json.comment);
2071
+ if (json.protoName)
2072
+ field.protoName = json.protoName;
2073
+ if (json.jsonName !== undefined)
2074
+ field.jsonName = json.jsonName;
2075
+ else if (json.options && json.options.json_name !== undefined)
2076
+ field.jsonName = json.options.json_name;
2077
+ return field;
2029
2078
  };
2030
2079
 
2031
2080
  /**
@@ -2036,12 +2085,14 @@ MapField.fromJSON = function fromJSON(name, json) {
2036
2085
  MapField.prototype.toJSON = function toJSON(toJSONOptions) {
2037
2086
  var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
2038
2087
  return util.toObject([
2039
- "keyType" , this.keyType,
2040
- "type" , this.type,
2041
- "id" , this.id,
2042
- "extend" , this.extend,
2043
- "options" , this.options,
2044
- "comment" , keepComments ? this.comment : undefined
2088
+ "keyType" , this.keyType,
2089
+ "type" , this.type,
2090
+ "id" , this.id,
2091
+ "extend" , this.extend,
2092
+ "protoName" , this.protoName !== this.name ? this.protoName : undefined,
2093
+ "jsonName" , this.jsonName !== util.jsonName(this.protoName || this.name) ? this.jsonName : undefined,
2094
+ "options" , this.options,
2095
+ "comment" , keepComments ? this.comment : undefined
2045
2096
  ]);
2046
2097
  };
2047
2098
 
@@ -4065,10 +4116,13 @@ function parse(source, root, options) {
4065
4116
  if (!nameRe.test(name))
4066
4117
  throw illegal(name, "name");
4067
4118
 
4119
+ var protoName = name;
4068
4120
  name = applyCase(name);
4069
4121
  skip("=");
4070
4122
 
4071
4123
  var field = new Field(name, parseId(next()), type, rule === "proto3_optional" ? "optional" : rule, extend);
4124
+ if (protoName !== name)
4125
+ field.protoName = protoName;
4072
4126
 
4073
4127
  ifBlock(field, function parseField_block(token) {
4074
4128
 
@@ -4210,7 +4264,11 @@ function parse(source, root, options) {
4210
4264
  throw illegal(name, "name");
4211
4265
 
4212
4266
  skip("=");
4213
- var field = new MapField(applyCase(name), parseId(next()), keyType, valueType);
4267
+ var protoName = name;
4268
+ name = applyCase(name);
4269
+ var field = new MapField(name, parseId(next()), keyType, valueType);
4270
+ if (protoName !== name)
4271
+ field.protoName = protoName;
4214
4272
  ifBlock(field, function parseMapField_block(token) {
4215
4273
 
4216
4274
  /* istanbul ignore else */
@@ -4422,11 +4480,18 @@ function parse(source, root, options) {
4422
4480
  topLevelOptions[name] = value;
4423
4481
  return;
4424
4482
  }
4483
+ // lift json_name onto Field
4484
+ if (name === "json_name" && parent instanceof Field) {
4485
+ parent.jsonName = value;
4486
+ return;
4487
+ }
4425
4488
  if (parent.setOption)
4426
4489
  parent.setOption(name, value);
4427
4490
  }
4428
4491
 
4429
4492
  function setParsedOption(parent, name, value, propName) {
4493
+ if (name === "json_name" && parent instanceof Field)
4494
+ return; // lifted onto Field#jsonName above
4430
4495
  if (parent.setParsedOption)
4431
4496
  parent.setParsedOption(name, value, propName);
4432
4497
  }
@@ -5893,8 +5958,6 @@ var Method = require(13),
5893
5958
  util = require(28),
5894
5959
  rpc = require(22);
5895
5960
 
5896
- var reservedRe = util.patterns.reservedRe;
5897
-
5898
5961
  /**
5899
5962
  * Constructs a new service instance.
5900
5963
  * @classdesc Reflected service.
@@ -6074,11 +6137,11 @@ Service.prototype.create = function create(rpcImpl, requestDelimited, responseDe
6074
6137
  var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);
6075
6138
  for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {
6076
6139
  var methodName = util.lcFirst((method = this._methodsArray[i]).resolve().name).replace(/[^$\w_]/g, "");
6077
- rpcService[methodName] = util.codegen(["r","c"], reservedRe.test(methodName) ? methodName + "_" : methodName)("return this.rpcCall(m,q,s,r,c)")({
6078
- m: method,
6079
- q: method.resolvedRequestType.ctor,
6080
- s: method.resolvedResponseType.ctor
6081
- });
6140
+ rpcService[methodName] = (function(method, requestType, responseType) {
6141
+ return function rpcMethod(request, callback) {
6142
+ return rpc.Service.prototype.rpcCall.call(this, method, requestType, responseType, request, callback);
6143
+ };
6144
+ })(method, method.resolvedRequestType.ctor, method.resolvedResponseType.ctor);
6082
6145
  }
6083
6146
  return rpcService;
6084
6147
  };
@@ -6598,6 +6661,13 @@ function Type(name, options) {
6598
6661
  * @private
6599
6662
  */
6600
6663
  this._ctor = null;
6664
+
6665
+ /**
6666
+ * Cached fields by JSON name.
6667
+ * @type {Object.<string,Field>|null}
6668
+ * @private
6669
+ */
6670
+ this._fieldsByJsonName = null; // used by ext/protojson
6601
6671
  }
6602
6672
 
6603
6673
  Object.defineProperties(Type.prototype, {
@@ -6710,7 +6780,7 @@ Object.defineProperties(Type.prototype, {
6710
6780
  */
6711
6781
  Type.generateConstructor = function generateConstructor(mtype) {
6712
6782
  /* eslint-disable no-unexpected-multiline */
6713
- var gen = util.codegen(["p"], mtype.name);
6783
+ var gen = util.codegen(["p"]);
6714
6784
  // explicitly initialize mutable object/array fields so that these aren't just inherited from the prototype
6715
6785
  for (var i = 0, field; i < mtype.fieldsArray.length; ++i)
6716
6786
  if ((field = mtype._fieldsArray[i]).map) gen
@@ -6724,7 +6794,7 @@ Type.generateConstructor = function generateConstructor(mtype) {
6724
6794
  };
6725
6795
 
6726
6796
  function clearCache(type) {
6727
- type._fieldsById = type._fieldsArray = type._oneofsArray = null;
6797
+ type._fieldsById = type._fieldsArray = type._oneofsArray = type._fieldsByJsonName = null;
6728
6798
  delete type.encode;
6729
6799
  delete type.decode;
6730
6800
  delete type.verify;
@@ -6888,7 +6958,7 @@ Type.prototype.add = function add(object) {
6888
6958
  throw Error("duplicate id " + object.id + " in " + this);
6889
6959
  if (this.isReservedId(object.id))
6890
6960
  throw Error("id " + object.id + " is reserved in " + this);
6891
- if (this.isReservedName(object.name))
6961
+ if (this.isReservedName(object.name) || object.name.charAt(0) === "$")
6892
6962
  throw Error("name '" + object.name + "' is reserved in " + this);
6893
6963
  if (object.name === "__proto__")
6894
6964
  return this;
@@ -6901,6 +6971,8 @@ Type.prototype.add = function add(object) {
6901
6971
  return clearCache(this);
6902
6972
  }
6903
6973
  if (object instanceof OneOf) {
6974
+ if (object.name.charAt(0) === "$")
6975
+ throw Error("name '" + object.name + "' is reserved in " + this);
6904
6976
  if (object.name === "__proto__")
6905
6977
  return this;
6906
6978
  if (!this.oneofs)
@@ -7448,6 +7520,7 @@ var camelCaseRe = /_([a-z])/g;
7448
7520
  * Converts a string to camel case.
7449
7521
  * @param {string} str String to convert
7450
7522
  * @returns {string} Converted string
7523
+ * @deprecated Use {@link util.jsonName} for protobuf field JSON names.
7451
7524
  */
7452
7525
  util.camelCase = function camelCase(str) {
7453
7526
  return str.substring(0, 1)
@@ -7455,6 +7528,28 @@ util.camelCase = function camelCase(str) {
7455
7528
  .replace(camelCaseRe, function($0, $1) { return $1.toUpperCase(); });
7456
7529
  };
7457
7530
 
7531
+ /**
7532
+ * Converts a proto field name to its protoc-compatible JSON name.
7533
+ * @param {string} str Proto field name
7534
+ * @returns {string} JSON name
7535
+ */
7536
+ util.jsonName = function jsonName(str) {
7537
+ var result = "",
7538
+ upperNext = false,
7539
+ i = 0;
7540
+ for (; i < str.length; ++i) {
7541
+ var ch = str.charAt(i);
7542
+ if (ch === "_")
7543
+ upperNext = true;
7544
+ else if (upperNext) {
7545
+ result += ch.toUpperCase();
7546
+ upperNext = false;
7547
+ } else
7548
+ result += ch;
7549
+ }
7550
+ return result;
7551
+ };
7552
+
7458
7553
  /**
7459
7554
  * Compares reflected fields by id.
7460
7555
  * @param {Field} a First field
@@ -8806,7 +8901,7 @@ util.isset =
8806
8901
  */
8807
8902
  util.isSet = function isSet(obj, prop) {
8808
8903
  var value = obj[prop];
8809
- if (value != null && obj.hasOwnProperty(prop)) // eslint-disable-line eqeqeq, no-prototype-builtins
8904
+ if (value != null && Object.hasOwnProperty.call(obj, prop)) // eslint-disable-line eqeqeq
8810
8905
  return typeof value !== "object" || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
8811
8906
  return false;
8812
8907
  };
@@ -9594,7 +9689,7 @@ function genVerifyKey(gen, field, ref) {
9594
9689
  function verifier(mtype) {
9595
9690
  /* eslint-disable no-unexpected-multiline */
9596
9691
 
9597
- var gen = util.codegen(["m", "q"], mtype.name + "$verify")
9692
+ var gen = util.codegen(["m", "q"])
9598
9693
  ("if(typeof m!==\"object\"||m===null)")
9599
9694
  ("return%j", "object expected")
9600
9695
  ("if(q===undefined)q=0")
@@ -9610,7 +9705,7 @@ function verifier(mtype) {
9610
9705
  ref = "m" + util.safeProp(field.name);
9611
9706
 
9612
9707
  if (field.optional) gen
9613
- ("if(%s!=null&&m.hasOwnProperty(%j)){", ref, field.name); // !== undefined && !== null
9708
+ ("if(%s!=null&&Object.hasOwnProperty.call(m,%j)){", ref, field.name); // !== undefined && !== null
9614
9709
 
9615
9710
  // map fields
9616
9711
  if (field.map) { gen