protobufjs 8.4.2 → 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.
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * protobuf.js v8.4.2 (c) 2016, daniel wirtz
3
- * compiled fri, 22 may 2026 02:44:15 utc
2
+ * protobuf.js v8.6.0 (c) 2016, daniel wirtz
3
+ * compiled thu, 04 jun 2026 21:47:14 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -84,7 +84,7 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
84
84
  } gen
85
85
  ("}");
86
86
  } else gen
87
- ("if(typeof d%s!==\"object\")", prop)
87
+ ("if(!util.isObject(d%s))", prop)
88
88
  ("throw TypeError(%j)", field.fullName + ": object expected")
89
89
  ("m%s=types[%i].fromObject(d%s,q+1)", prop, fieldIndex, prop);
90
90
  } else {
@@ -148,9 +148,11 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
148
148
  converter.fromObject = function fromObject(mtype) {
149
149
  /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
150
150
  var fields = mtype.fieldsArray;
151
- var gen = util.codegen(["d", "q"], mtype.name + "$fromObject")
151
+ var gen = util.codegen(["d", "q"])
152
152
  ("if(d instanceof C)")
153
153
  ("return d")
154
+ ("if(!util.isObject(d))")
155
+ ("throw TypeError(%j)", mtype.fullName + ": object expected")
154
156
  ("if(q===undefined)q=0")
155
157
  ("if(q>util.recursionLimit)")
156
158
  ("throw Error(\"max depth exceeded\")");
@@ -167,7 +169,7 @@ converter.fromObject = function fromObject(mtype) {
167
169
  // Map fields
168
170
  if (field.map) { gen
169
171
  ("if(d%s){", prop)
170
- ("if(typeof d%s!==\"object\")", prop)
172
+ ("if(!util.isObject(d%s))", prop)
171
173
  ("throw TypeError(%j)", field.fullName + ": object expected")
172
174
  ("m%s={}", prop)
173
175
  ("for(var ks=Object.keys(d%s),i=0;i<ks.length;++i){", prop);
@@ -280,7 +282,7 @@ converter.toObject = function toObject(mtype) {
280
282
  var fields = mtype.fieldsArray.slice().sort(util.compareFieldsById);
281
283
  if (!fields.length)
282
284
  return util.codegen()("return {}");
283
- var gen = util.codegen(["m", "o", "q"], mtype.name + "$toObject")
285
+ var gen = util.codegen(["m", "o", "q"])
284
286
  ("if(!o)")
285
287
  ("o={}")
286
288
  ("if(q===undefined)q=0")
@@ -369,7 +371,7 @@ converter.toObject = function toObject(mtype) {
369
371
  genValuePartial_toObject(gen, field, /* sorted */ index, prop + "[j]")
370
372
  ("}");
371
373
  } else { gen
372
- ("if(m%s!=null&&m.hasOwnProperty(%j)){", prop, field.name); // !== undefined && !== null
374
+ ("if(m%s!=null&&Object.hasOwnProperty.call(m,%j)){", prop, field.name); // !== undefined && !== null
373
375
  genValuePartial_toObject(gen, field, /* sorted */ index, prop);
374
376
  if (field.partOf && !field.partOf.isProto3Optional) gen
375
377
  ("if(o.oneofs)")
@@ -412,7 +414,7 @@ function decoder(mtype) {
412
414
  if (!pfield.repeated && !pfield.map && !pfield.hasPresence)
413
415
  hasImplicitPresenceField = true;
414
416
  }
415
- var gen = util.codegen(["r", "l", "z", "q", "g"], mtype.name + "$decode")
417
+ var gen = util.codegen(["r", "l", "z", "q", "g"])
416
418
  ("if(!(r instanceof Reader))")
417
419
  ("r=Reader.create(r)")
418
420
  ("if(q===undefined)q=0")
@@ -572,8 +574,10 @@ function decoder(mtype) {
572
574
  // Unknown fields
573
575
  gen
574
576
  ("r.skipType(%s,q,t)", i ? "u" : "t&7")
575
- ("util.makeProp(m,\"$unknowns\",false);")
576
- ("(m.$unknowns||(m.$unknowns=[])).push(r.raw(s,r.pos))")
577
+ ("if(!r.discardUnknown){")
578
+ ("util.makeProp(m,\"$unknowns\",false);")
579
+ ("(m.$unknowns||(m.$unknowns=[])).push(r.raw(s,r.pos))")
580
+ ("}")
577
581
  ("}")
578
582
  ("if(z!==undefined)")
579
583
  ("throw Error(\"missing end group\")");
@@ -582,7 +586,7 @@ function decoder(mtype) {
582
586
  for (i = 0; i < mtype._fieldsArray.length; ++i) {
583
587
  var rfield = mtype._fieldsArray[i];
584
588
  if (rfield.required) gen
585
- ("if(!m.hasOwnProperty(%j))", rfield.name)
589
+ ("if(!Object.hasOwnProperty.call(m,%j))", rfield.name)
586
590
  ("throw util.ProtocolError(%j,{instance:m})", missing(rfield));
587
591
  }
588
592
 
@@ -621,7 +625,7 @@ function genTypePartial(gen, field, fieldIndex, ref) {
621
625
  */
622
626
  function encoder(mtype) {
623
627
  /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
624
- var gen = util.codegen(["m", "w", "q"], mtype.name + "$encode")
628
+ var gen = util.codegen(["m", "w", "q"])
625
629
  ("if(!w)")
626
630
  ("w=Writer.create()")
627
631
  ("if(q===undefined)q=0")
@@ -978,6 +982,12 @@ Field.fromJSON = function fromJSON(name, json) {
978
982
  var field = new Field(name, json.id, json.type, json.rule, json.extend, json.options, json.comment);
979
983
  if (json.edition)
980
984
  field._edition = json.edition;
985
+ if (json.protoName)
986
+ field.protoName = json.protoName;
987
+ if (json.jsonName !== undefined)
988
+ field.jsonName = json.jsonName;
989
+ else if (json.options && json.options.json_name !== undefined)
990
+ field.jsonName = json.options.json_name;
981
991
  field._defaultEdition = "proto3"; // For backwards-compatibility.
982
992
  return field;
983
993
  };
@@ -1117,6 +1127,18 @@ function Field(name, id, type, rule, extend, options, comment) {
1117
1127
  * @type {string|null}
1118
1128
  */
1119
1129
  this.comment = comment;
1130
+
1131
+ /**
1132
+ * Field name as declared in the .proto source, if different from `name`.
1133
+ * @type {string|undefined}
1134
+ */
1135
+ this.protoName = undefined;
1136
+
1137
+ /**
1138
+ * JSON name, if different from the derived default.
1139
+ * @type {string|undefined}
1140
+ */
1141
+ this.jsonName = undefined;
1120
1142
  }
1121
1143
 
1122
1144
  /**
@@ -1186,6 +1208,22 @@ Object.defineProperty(Field.prototype, "hasPresence", {
1186
1208
  }
1187
1209
  });
1188
1210
 
1211
+ /**
1212
+ * The field name as declared in the .proto source (snake_case). Populated on resolve,
1213
+ * falling back to `name`. Mirrors `FieldDescriptorProto.name`.
1214
+ * @name Field#protoName
1215
+ * @type {string}
1216
+ * @readonly
1217
+ */
1218
+
1219
+ /**
1220
+ * The JSON name of this field (lowerCamelCase per protoc's `ToJsonName`, or an
1221
+ * explicit `[json_name]`). Populated on resolve. This is the key used on ProtoJSON output.
1222
+ * @name Field#jsonName
1223
+ * @type {string}
1224
+ * @readonly
1225
+ */
1226
+
1189
1227
  /**
1190
1228
  * @override
1191
1229
  */
@@ -1219,13 +1257,15 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
1219
1257
  Field.prototype.toJSON = function toJSON(toJSONOptions) {
1220
1258
  var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
1221
1259
  return util.toObject([
1222
- "edition" , this._editionToJSON(),
1223
- "rule" , this.rule !== "optional" && this.rule || undefined,
1224
- "type" , this.type,
1225
- "id" , this.id,
1226
- "extend" , this.extend,
1227
- "options" , this.options,
1228
- "comment" , keepComments ? this.comment : undefined
1260
+ "edition" , this._editionToJSON(),
1261
+ "rule" , this.rule !== "optional" && this.rule || undefined,
1262
+ "type" , this.type,
1263
+ "id" , this.id,
1264
+ "extend" , this.extend,
1265
+ "protoName" , this.protoName !== this.name ? this.protoName : undefined,
1266
+ "jsonName" , this.jsonName !== util.jsonName(this.protoName || this.name) ? this.jsonName : undefined,
1267
+ "options" , this.options,
1268
+ "comment" , keepComments ? this.comment : undefined
1229
1269
  ]);
1230
1270
  };
1231
1271
 
@@ -1294,6 +1334,12 @@ Field.prototype.resolve = function resolve() {
1294
1334
  if (this.parent instanceof Type && this.parent._ctor)
1295
1335
  this.parent._ctor.prototype[this.name] = this.defaultValue;
1296
1336
 
1337
+ // derive the proto/JSON names
1338
+ if (this.protoName === undefined)
1339
+ this.protoName = this.name;
1340
+ if (this.jsonName === undefined)
1341
+ this.jsonName = util.jsonName(this.protoName);
1342
+
1297
1343
  return ReflectionObject.prototype.resolve.call(this);
1298
1344
  };
1299
1345
 
@@ -1606,7 +1652,14 @@ function MapField(name, id, keyType, type, options, comment) {
1606
1652
  * @throws {TypeError} If arguments are invalid
1607
1653
  */
1608
1654
  MapField.fromJSON = function fromJSON(name, json) {
1609
- return new MapField(name, json.id, json.keyType, json.type, json.options, json.comment);
1655
+ var field = new MapField(name, json.id, json.keyType, json.type, json.options, json.comment);
1656
+ if (json.protoName)
1657
+ field.protoName = json.protoName;
1658
+ if (json.jsonName !== undefined)
1659
+ field.jsonName = json.jsonName;
1660
+ else if (json.options && json.options.json_name !== undefined)
1661
+ field.jsonName = json.options.json_name;
1662
+ return field;
1610
1663
  };
1611
1664
 
1612
1665
  /**
@@ -1617,12 +1670,14 @@ MapField.fromJSON = function fromJSON(name, json) {
1617
1670
  MapField.prototype.toJSON = function toJSON(toJSONOptions) {
1618
1671
  var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
1619
1672
  return util.toObject([
1620
- "keyType" , this.keyType,
1621
- "type" , this.type,
1622
- "id" , this.id,
1623
- "extend" , this.extend,
1624
- "options" , this.options,
1625
- "comment" , keepComments ? this.comment : undefined
1673
+ "keyType" , this.keyType,
1674
+ "type" , this.type,
1675
+ "id" , this.id,
1676
+ "extend" , this.extend,
1677
+ "protoName" , this.protoName !== this.name ? this.protoName : undefined,
1678
+ "jsonName" , this.jsonName !== util.jsonName(this.protoName || this.name) ? this.jsonName : undefined,
1679
+ "options" , this.options,
1680
+ "comment" , keepComments ? this.comment : undefined
1626
1681
  ]);
1627
1682
  };
1628
1683
 
@@ -1870,7 +1925,7 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
1870
1925
 
1871
1926
  /**
1872
1927
  * Whether requests are streamed or not.
1873
- * @type {boolean|undefined}
1928
+ * @type {true|undefined}
1874
1929
  */
1875
1930
  this.requestStream = requestStream ? true : undefined; // toJSON
1876
1931
 
@@ -1882,10 +1937,16 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
1882
1937
 
1883
1938
  /**
1884
1939
  * Whether responses are streamed or not.
1885
- * @type {boolean|undefined}
1940
+ * @type {true|undefined}
1886
1941
  */
1887
1942
  this.responseStream = responseStream ? true : undefined; // toJSON
1888
1943
 
1944
+ /**
1945
+ * gRPC-style method path.
1946
+ * @type {string}
1947
+ */
1948
+ this.path = "/" + this.name;
1949
+
1889
1950
  /**
1890
1951
  * Resolved request type.
1891
1952
  * @type {Type|null}
@@ -1963,6 +2024,14 @@ Method.prototype.resolve = function resolve() {
1963
2024
  if (this.resolved)
1964
2025
  return this;
1965
2026
 
2027
+ if (this.parent) {
2028
+ var serviceName = this.parent.fullName;
2029
+ if (serviceName.charAt(0) === ".")
2030
+ serviceName = serviceName.substring(1);
2031
+ this.path = "/" + serviceName + "/" + this.name;
2032
+ } else
2033
+ this.path = "/" + this.name;
2034
+
1966
2035
  this.resolvedRequestType = this.parent.lookupType(this.requestType);
1967
2036
  this.resolvedResponseType = this.parent.lookupType(this.responseType);
1968
2037
 
@@ -2398,14 +2467,14 @@ Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChe
2398
2467
  if (path[0] === "")
2399
2468
  return this.root.lookup(path.slice(1), filterTypes);
2400
2469
 
2401
- // Early bailout for objects with matching absolute paths
2402
- var found = this.root._fullyQualifiedObjects && this.root._fullyQualifiedObjects["." + flatPath];
2470
+ // Lookup at this namespace and below
2471
+ var found = this._lookupImpl(path, flatPath);
2403
2472
  if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
2404
2473
  return found;
2405
2474
  }
2406
2475
 
2407
- // Do a regular lookup at this namespace and below
2408
- found = this._lookupImpl(path, flatPath);
2476
+ // Fall back to respective absolute path once relative scope has been checked (non-standard)
2477
+ found = this.root._fullyQualifiedObjects && this.root._fullyQualifiedObjects["." + flatPath];
2409
2478
  if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
2410
2479
  return found;
2411
2480
  }
@@ -3188,6 +3257,12 @@ function Reader(buffer) {
3188
3257
  * @type {number}
3189
3258
  */
3190
3259
  this.len = buffer.length;
3260
+
3261
+ /**
3262
+ * Whether to discard unknown fields while decoding.
3263
+ * @type {boolean}
3264
+ */
3265
+ this.discardUnknown = Reader.discardUnknown;
3191
3266
  }
3192
3267
 
3193
3268
  var create_array = typeof Uint8Array !== "undefined"
@@ -3600,6 +3675,12 @@ Reader.prototype.skip = function skip(length) {
3600
3675
  */
3601
3676
  Reader.recursionLimit = util.recursionLimit;
3602
3677
 
3678
+ /**
3679
+ * Whether readers discard unknown fields while decoding.
3680
+ * @type {boolean}
3681
+ */
3682
+ Reader.discardUnknown = false;
3683
+
3603
3684
  /**
3604
3685
  * Skips the next element of the specified wire type.
3605
3686
  * @param {number} wireType Wire type received
@@ -4174,7 +4255,7 @@ Root._configure = function(Type_, parse_, common_) {
4174
4255
 
4175
4256
  },{"12":12,"14":14,"24":24,"5":5,"6":6}],18:[function(require,module,exports){
4176
4257
  "use strict";
4177
- module.exports = {};
4258
+ module.exports = Object.create(null);
4178
4259
 
4179
4260
  /**
4180
4261
  * Named roots.
@@ -4256,10 +4337,16 @@ var util = require(34);
4256
4337
  * @typedef rpc.ServiceMethod
4257
4338
  * @template TReq extends Message<TReq>
4258
4339
  * @template TRes extends Message<TRes>
4259
- * @type {function}
4260
- * @param {TReq|Properties<TReq>} request Request message or plain object
4261
- * @param {rpc.ServiceMethodCallback<TRes>} [callback] Node-style callback called with the error, if any, and the response message
4262
- * @returns {Promise<Message<TRes>>} Promise if `callback` has been omitted, otherwise `undefined`
4340
+ * @type {{
4341
+ * (request: TReq|Properties<TReq>, callback: rpc.ServiceMethodCallback<TRes>): void;
4342
+ * (request: TReq|Properties<TReq>): Promise<TRes>;
4343
+ * readonly name: string;
4344
+ * readonly path: string;
4345
+ * readonly requestType: string;
4346
+ * readonly responseType: string;
4347
+ * readonly requestStream: true|undefined;
4348
+ * readonly responseStream: true|undefined;
4349
+ * }}
4263
4350
  */
4264
4351
 
4265
4352
  /**
@@ -4386,8 +4473,6 @@ var Method = require(11),
4386
4473
  util = require(24),
4387
4474
  rpc = require(19);
4388
4475
 
4389
- var reservedRe = util.patterns.reservedRe;
4390
-
4391
4476
  /**
4392
4477
  * Constructs a new service instance.
4393
4478
  * @classdesc Reflected service.
@@ -4567,11 +4652,11 @@ Service.prototype.create = function create(rpcImpl, requestDelimited, responseDe
4567
4652
  var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);
4568
4653
  for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {
4569
4654
  var methodName = util.lcFirst((method = this._methodsArray[i]).resolve().name).replace(/[^$\w_]/g, "");
4570
- rpcService[methodName] = util.codegen(["r","c"], reservedRe.test(methodName) ? methodName + "_" : methodName)("return this.rpcCall(m,q,s,r,c)")({
4571
- m: method,
4572
- q: method.resolvedRequestType.ctor,
4573
- s: method.resolvedResponseType.ctor
4574
- });
4655
+ rpcService[methodName] = (function(method, requestType, responseType) {
4656
+ return function rpcMethod(request, callback) {
4657
+ return rpc.Service.prototype.rpcCall.call(this, method, requestType, responseType, request, callback);
4658
+ };
4659
+ })(method, method.resolvedRequestType.ctor, method.resolvedResponseType.ctor);
4575
4660
  }
4576
4661
  return rpcService;
4577
4662
  };
@@ -4668,6 +4753,13 @@ function Type(name, options) {
4668
4753
  * @private
4669
4754
  */
4670
4755
  this._ctor = null;
4756
+
4757
+ /**
4758
+ * Cached fields by JSON name.
4759
+ * @type {Object.<string,Field>|null}
4760
+ * @private
4761
+ */
4762
+ this._fieldsByJsonName = null; // used by ext/protojson
4671
4763
  }
4672
4764
 
4673
4765
  Object.defineProperties(Type.prototype, {
@@ -4780,7 +4872,7 @@ Object.defineProperties(Type.prototype, {
4780
4872
  */
4781
4873
  Type.generateConstructor = function generateConstructor(mtype) {
4782
4874
  /* eslint-disable no-unexpected-multiline */
4783
- var gen = util.codegen(["p"], mtype.name);
4875
+ var gen = util.codegen(["p"]);
4784
4876
  // explicitly initialize mutable object/array fields so that these aren't just inherited from the prototype
4785
4877
  for (var i = 0, field; i < mtype.fieldsArray.length; ++i)
4786
4878
  if ((field = mtype._fieldsArray[i]).map) gen
@@ -4794,7 +4886,7 @@ Type.generateConstructor = function generateConstructor(mtype) {
4794
4886
  };
4795
4887
 
4796
4888
  function clearCache(type) {
4797
- type._fieldsById = type._fieldsArray = type._oneofsArray = null;
4889
+ type._fieldsById = type._fieldsArray = type._oneofsArray = type._fieldsByJsonName = null;
4798
4890
  delete type.encode;
4799
4891
  delete type.decode;
4800
4892
  delete type.verify;
@@ -4958,7 +5050,7 @@ Type.prototype.add = function add(object) {
4958
5050
  throw Error("duplicate id " + object.id + " in " + this);
4959
5051
  if (this.isReservedId(object.id))
4960
5052
  throw Error("id " + object.id + " is reserved in " + this);
4961
- if (this.isReservedName(object.name))
5053
+ if (this.isReservedName(object.name) || object.name.charAt(0) === "$")
4962
5054
  throw Error("name '" + object.name + "' is reserved in " + this);
4963
5055
  if (object.name === "__proto__")
4964
5056
  return this;
@@ -4971,6 +5063,8 @@ Type.prototype.add = function add(object) {
4971
5063
  return clearCache(this);
4972
5064
  }
4973
5065
  if (object instanceof OneOf) {
5066
+ if (object.name.charAt(0) === "$")
5067
+ throw Error("name '" + object.name + "' is reserved in " + this);
4974
5068
  if (object.name === "__proto__")
4975
5069
  return this;
4976
5070
  if (!this.oneofs)
@@ -5518,6 +5612,7 @@ var camelCaseRe = /_([a-z])/g;
5518
5612
  * Converts a string to camel case.
5519
5613
  * @param {string} str String to convert
5520
5614
  * @returns {string} Converted string
5615
+ * @deprecated Use {@link util.jsonName} for protobuf field JSON names.
5521
5616
  */
5522
5617
  util.camelCase = function camelCase(str) {
5523
5618
  return str.substring(0, 1)
@@ -5525,6 +5620,28 @@ util.camelCase = function camelCase(str) {
5525
5620
  .replace(camelCaseRe, function($0, $1) { return $1.toUpperCase(); });
5526
5621
  };
5527
5622
 
5623
+ /**
5624
+ * Converts a proto field name to its protoc-compatible JSON name.
5625
+ * @param {string} str Proto field name
5626
+ * @returns {string} JSON name
5627
+ */
5628
+ util.jsonName = function jsonName(str) {
5629
+ var result = "",
5630
+ upperNext = false,
5631
+ i = 0;
5632
+ for (; i < str.length; ++i) {
5633
+ var ch = str.charAt(i);
5634
+ if (ch === "_")
5635
+ upperNext = true;
5636
+ else if (upperNext) {
5637
+ result += ch.toUpperCase();
5638
+ upperNext = false;
5639
+ } else
5640
+ result += ch;
5641
+ }
5642
+ return result;
5643
+ };
5644
+
5528
5645
  /**
5529
5646
  * Compares reflected fields by id.
5530
5647
  * @param {Field} a First field
@@ -6876,7 +6993,7 @@ util.isset =
6876
6993
  */
6877
6994
  util.isSet = function isSet(obj, prop) {
6878
6995
  var value = obj[prop];
6879
- if (value != null && obj.hasOwnProperty(prop)) // eslint-disable-line eqeqeq, no-prototype-builtins
6996
+ if (value != null && Object.hasOwnProperty.call(obj, prop)) // eslint-disable-line eqeqeq
6880
6997
  return typeof value !== "object" || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
6881
6998
  return false;
6882
6999
  };
@@ -7664,7 +7781,7 @@ function genVerifyKey(gen, field, ref) {
7664
7781
  function verifier(mtype) {
7665
7782
  /* eslint-disable no-unexpected-multiline */
7666
7783
 
7667
- var gen = util.codegen(["m", "q"], mtype.name + "$verify")
7784
+ var gen = util.codegen(["m", "q"])
7668
7785
  ("if(typeof m!==\"object\"||m===null)")
7669
7786
  ("return%j", "object expected")
7670
7787
  ("if(q===undefined)q=0")
@@ -7680,7 +7797,7 @@ function verifier(mtype) {
7680
7797
  ref = "m" + util.safeProp(field.name);
7681
7798
 
7682
7799
  if (field.optional) gen
7683
- ("if(%s!=null&&m.hasOwnProperty(%j)){", ref, field.name); // !== undefined && !== null
7800
+ ("if(%s!=null&&Object.hasOwnProperty.call(m,%j)){", ref, field.name); // !== undefined && !== null
7684
7801
 
7685
7802
  // map fields
7686
7803
  if (field.map) { gen