protobufjs 6.9.0 → 6.10.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 v6.8.9 (c) 2016, daniel wirtz
3
- * compiled fri, 17 apr 2020 21:58:51 utc
2
+ * protobuf.js v6.9.0 (c) 2016, daniel wirtz
3
+ * compiled mon, 13 jul 2020 22:57:44 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -1826,7 +1826,7 @@ function decoder(mtype) {
1826
1826
  var gen = util.codegen(["r", "l"], mtype.name + "$decode")
1827
1827
  ("if(!(r instanceof Reader))")
1828
1828
  ("r=Reader.create(r)")
1829
- ("var c=l===undefined?r.len:r.pos+l,m=new this.ctor" + (mtype.fieldsArray.filter(function(field) { return field.map; }).length ? ",k" : ""))
1829
+ ("var c=l===undefined?r.len:r.pos+l,m=new this.ctor" + (mtype.fieldsArray.filter(function(field) { return field.map; }).length ? ",k,value" : ""))
1830
1830
  ("while(r.pos<c){")
1831
1831
  ("var t=r.uint32()");
1832
1832
  if (mtype.group) gen
@@ -1844,22 +1844,44 @@ function decoder(mtype) {
1844
1844
 
1845
1845
  // Map fields
1846
1846
  if (field.map) { gen
1847
- ("r.skip().pos++") // assumes id 1 + key wireType
1848
1847
  ("if(%s===util.emptyObject)", ref)
1849
1848
  ("%s={}", ref)
1850
- ("k=r.%s()", field.keyType)
1851
- ("r.pos++"); // assumes id 2 + value wireType
1852
- if (types.long[field.keyType] !== undefined) {
1853
- if (types.basic[type] === undefined) gen
1854
- ("%s[typeof k===\"object\"?util.longToHash(k):k]=types[%i].decode(r,r.uint32())", ref, i); // can't be groups
1855
- else gen
1856
- ("%s[typeof k===\"object\"?util.longToHash(k):k]=r.%s()", ref, type);
1857
- } else {
1858
- if (types.basic[type] === undefined) gen
1859
- ("%s[k]=types[%i].decode(r,r.uint32())", ref, i); // can't be groups
1860
- else gen
1861
- ("%s[k]=r.%s()", ref, type);
1862
- }
1849
+ ("var c2 = r.uint32()+r.pos");
1850
+
1851
+ if (types.defaults[field.keyType] !== undefined) gen
1852
+ ("k=%j", types.defaults[field.keyType]);
1853
+ else gen
1854
+ ("k=null");
1855
+
1856
+ if (types.defaults[type] !== undefined) gen
1857
+ ("value=%j", types.defaults[type]);
1858
+ else gen
1859
+ ("value=null");
1860
+
1861
+ gen
1862
+ ("while(r.pos<c2){")
1863
+ ("var tag2=r.uint32()")
1864
+ ("switch(tag2>>>3){")
1865
+ ("case 1: k=r.%s(); break", field.keyType)
1866
+ ("case 2:");
1867
+
1868
+ if (types.basic[type] === undefined) gen
1869
+ ("value=types[%i].decode(r,r.uint32())", i); // can't be groups
1870
+ else gen
1871
+ ("value=r.%s()", type);
1872
+
1873
+ gen
1874
+ ("break")
1875
+ ("default:")
1876
+ ("r.skipType(tag2&7)")
1877
+ ("break")
1878
+ ("}")
1879
+ ("}");
1880
+
1881
+ if (types.long[field.keyType] !== undefined) gen
1882
+ ("%s[typeof k===\"object\"?util.longToHash(k):k]=value", ref);
1883
+ else gen
1884
+ ("%s[k]=value", ref);
1863
1885
 
1864
1886
  // Repeated fields
1865
1887
  } else if (field.repeated) { gen
@@ -3616,6 +3638,12 @@ function ReflectionObject(name, options) {
3616
3638
  */
3617
3639
  this.options = options; // toJSON
3618
3640
 
3641
+ /**
3642
+ * Parsed Options.
3643
+ * @type {Array.<Object.<string,*>>|undefined}
3644
+ */
3645
+ this.parsedOptions = null;
3646
+
3619
3647
  /**
3620
3648
  * Unique name within its namespace.
3621
3649
  * @type {string}
@@ -3756,6 +3784,43 @@ ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet)
3756
3784
  return this;
3757
3785
  };
3758
3786
 
3787
+ /**
3788
+ * Sets a parsed option.
3789
+ * @param {string} name parsed Option name
3790
+ * @param {*} value Option value
3791
+ * @param {string} propName dot '.' delimited full path of property within the option to set. if undefined\empty, will add a new option with that value
3792
+ * @returns {ReflectionObject} `this`
3793
+ */
3794
+ ReflectionObject.prototype.setParsedOption = function setParsedOption(name, value, propName) {
3795
+ if (!this.parsedOptions) {
3796
+ this.parsedOptions = [];
3797
+ }
3798
+ var parsedOptions = this.parsedOptions;
3799
+ if (propName) {
3800
+ // If setting a sub property of an option then try to merge it
3801
+ // with an existing option
3802
+ var opt = parsedOptions.find(function (opt) {
3803
+ return Object.prototype.hasOwnProperty.call(opt, name);
3804
+ });
3805
+ if (opt) {
3806
+ // If we found an existing option - just merge the property value
3807
+ var newValue = opt[name];
3808
+ util.setProperty(newValue, propName, value);
3809
+ } else {
3810
+ // otherwise, create a new option, set it's property and add it to the list
3811
+ opt = {};
3812
+ opt[name] = util.setProperty({}, propName, value);
3813
+ parsedOptions.push(opt);
3814
+ }
3815
+ } else {
3816
+ // Always create a new option when setting the value of the option itself
3817
+ var newOpt = {};
3818
+ newOpt[name] = value;
3819
+ parsedOptions.push(newOpt);
3820
+ }
3821
+ return this;
3822
+ };
3823
+
3759
3824
  /**
3760
3825
  * Sets multiple options.
3761
3826
  * @param {Object.<string,*>} options Options to set
@@ -4036,6 +4101,7 @@ var base10Re = /^[1-9][0-9]*$/,
4036
4101
  * @interface IParseOptions
4037
4102
  * @property {boolean} [keepCase=false] Keeps field casing instead of converting to camel case
4038
4103
  * @property {boolean} [alternateCommentMode=false] Recognize double-slash comments in addition to doc-block comments.
4104
+ * @property {boolean} [preferTrailingComment=false] Use trailing comment when both leading comment and trailing comment exist.
4039
4105
  */
4040
4106
 
4041
4107
  /**
@@ -4062,6 +4128,7 @@ function parse(source, root, options) {
4062
4128
  if (!options)
4063
4129
  options = parse.defaults;
4064
4130
 
4131
+ var preferTrailingComment = options.preferTrailingComment || false;
4065
4132
  var tn = tokenize(source, options.alternateCommentMode || false),
4066
4133
  next = tn.next,
4067
4134
  push = tn.push,
@@ -4285,8 +4352,8 @@ function parse(source, root, options) {
4285
4352
  if (fnElse)
4286
4353
  fnElse();
4287
4354
  skip(";");
4288
- if (obj && typeof obj.comment !== "string")
4289
- obj.comment = cmnt(trailingLine); // try line-type comment if no block
4355
+ if (obj && (typeof obj.comment !== "string" || preferTrailingComment))
4356
+ obj.comment = cmnt(trailingLine) || obj.comment; // try line-type comment
4290
4357
  }
4291
4358
  }
4292
4359
 
@@ -4534,39 +4601,58 @@ function parse(source, root, options) {
4534
4601
  throw illegal(token, "name");
4535
4602
 
4536
4603
  var name = token;
4604
+ var option = name;
4605
+ var propName;
4606
+
4537
4607
  if (isCustom) {
4538
4608
  skip(")");
4539
4609
  name = "(" + name + ")";
4610
+ option = name;
4540
4611
  token = peek();
4541
4612
  if (fqTypeRefRe.test(token)) {
4613
+ propName = token.substr(1); //remove '.' before property name
4542
4614
  name += token;
4543
4615
  next();
4544
4616
  }
4545
4617
  }
4546
4618
  skip("=");
4547
- parseOptionValue(parent, name);
4619
+ var optionValue = parseOptionValue(parent, name);
4620
+ setParsedOption(parent, option, optionValue, propName);
4548
4621
  }
4549
4622
 
4550
4623
  function parseOptionValue(parent, name) {
4551
4624
  if (skip("{", true)) { // { a: "foo" b { c: "bar" } }
4552
- do {
4625
+ var result = {};
4626
+ while (!skip("}", true)) {
4553
4627
  /* istanbul ignore if */
4554
4628
  if (!nameRe.test(token = next()))
4555
4629
  throw illegal(token, "name");
4556
4630
 
4631
+ var value;
4632
+ var propName = token;
4557
4633
  if (peek() === "{")
4558
- parseOptionValue(parent, name + "." + token);
4634
+ value = parseOptionValue(parent, name + "." + token);
4559
4635
  else {
4560
4636
  skip(":");
4561
4637
  if (peek() === "{")
4562
- parseOptionValue(parent, name + "." + token);
4563
- else
4564
- setOption(parent, name + "." + token, readValue(true));
4638
+ value = parseOptionValue(parent, name + "." + token);
4639
+ else {
4640
+ value = readValue(true);
4641
+ setOption(parent, name + "." + token, value);
4642
+ }
4565
4643
  }
4644
+ var prevValue = result[propName];
4645
+ if (prevValue)
4646
+ value = [].concat(prevValue).concat(value);
4647
+ result[propName] = value;
4566
4648
  skip(",", true);
4567
- } while (!skip("}", true));
4568
- } else
4569
- setOption(parent, name, readValue(true));
4649
+ }
4650
+ return result;
4651
+ }
4652
+
4653
+ var simpleValue = readValue(true);
4654
+ setOption(parent, name, simpleValue);
4655
+ return simpleValue;
4570
4656
  // Does not enforce a delimiter to be universal
4571
4657
  }
4572
4658
 
@@ -4575,6 +4661,11 @@ function parse(source, root, options) {
4575
4661
  parent.setOption(name, value);
4576
4662
  }
4577
4663
 
4664
+ function setParsedOption(parent, name, value, propName) {
4665
+ if (parent.setParsedOption)
4666
+ parent.setParsedOption(name, value, propName);
4667
+ }
4668
+
4578
4669
  function parseInlineOptions(parent) {
4579
4670
  if (skip("[", true)) {
4580
4671
  do {
@@ -5284,6 +5375,16 @@ Root.fromJSON = function fromJSON(json, root) {
5284
5375
  */
5285
5376
  Root.prototype.resolvePath = util.path.resolve;
5286
5377
 
5378
+ /**
5379
+ * Fetch content from file path or url
5380
+ * This method exists so you can override it with your own logic.
5381
+ * @function
5382
+ * @param {string} path File path or url
5383
+ * @param {FetchCallback} callback Callback function
5384
+ * @returns {undefined}
5385
+ */
5386
+ Root.prototype.fetch = util.fetch;
5387
+
5287
5388
  // A symbol-like function to safely signal synchronous loading
5288
5389
  /* istanbul ignore next */
5289
5390
  function SYNC() {} // eslint-disable-line no-empty-function
@@ -5391,7 +5492,7 @@ Root.prototype.load = function load(filename, options, callback) {
5391
5492
  process(filename, source);
5392
5493
  } else {
5393
5494
  ++queued;
5394
- util.fetch(filename, function(err, source) {
5495
+ self.fetch(filename, function(err, source) {
5395
5496
  --queued;
5396
5497
  /* istanbul ignore if */
5397
5498
  if (!callback)
@@ -6055,7 +6156,8 @@ function tokenize(source, alternateCommentMode) {
6055
6156
  commentType = null,
6056
6157
  commentText = null,
6057
6158
  commentLine = 0,
6058
- commentLineEmpty = false;
6159
+ commentLineEmpty = false,
6160
+ commentIsLeading = false;
6059
6161
 
6060
6162
  var stack = [];
6061
6163
 
@@ -6103,13 +6205,15 @@ function tokenize(source, alternateCommentMode) {
6103
6205
  * Sets the current comment text.
6104
6206
  * @param {number} start Start offset
6105
6207
  * @param {number} end End offset
6208
+ * @param {boolean} isLeading set if a leading comment
6106
6209
  * @returns {undefined}
6107
6210
  * @inner
6108
6211
  */
6109
- function setComment(start, end) {
6212
+ function setComment(start, end, isLeading) {
6110
6213
  commentType = source.charAt(start++);
6111
6214
  commentLine = line;
6112
6215
  commentLineEmpty = false;
6216
+ commentIsLeading = isLeading;
6113
6217
  var lookback;
6114
6218
  if (alternateCommentMode) {
6115
6219
  lookback = 2; // alternate comment parsing: "//" or "/*"
@@ -6171,14 +6275,17 @@ function tokenize(source, alternateCommentMode) {
6171
6275
  prev,
6172
6276
  curr,
6173
6277
  start,
6174
- isDoc;
6278
+ isDoc,
6279
+ isLeadingComment = offset === 0;
6175
6280
  do {
6176
6281
  if (offset === length)
6177
6282
  return null;
6178
6283
  repeat = false;
6179
6284
  while (whitespaceRe.test(curr = charAt(offset))) {
6180
- if (curr === "\n")
6285
+ if (curr === "\n") {
6286
+ isLeadingComment = true;
6181
6287
  ++line;
6288
+ }
6182
6289
  if (++offset === length)
6183
6290
  return null;
6184
6291
  }
@@ -6199,7 +6306,7 @@ function tokenize(source, alternateCommentMode) {
6199
6306
  }
6200
6307
  ++offset;
6201
6308
  if (isDoc) {
6202
- setComment(start, offset - 1);
6309
+ setComment(start, offset - 1, isLeadingComment);
6203
6310
  }
6204
6311
  ++line;
6205
6312
  repeat = true;
@@ -6220,7 +6327,7 @@ function tokenize(source, alternateCommentMode) {
6220
6327
  offset = Math.min(length, findEndOfLine(offset) + 1);
6221
6328
  }
6222
6329
  if (isDoc) {
6223
- setComment(start, offset);
6330
+ setComment(start, offset, isLeadingComment);
6224
6331
  }
6225
6332
  line++;
6226
6333
  repeat = true;
@@ -6241,7 +6348,7 @@ function tokenize(source, alternateCommentMode) {
6241
6348
  } while (prev !== "*" || curr !== "/");
6242
6349
  ++offset;
6243
6350
  if (isDoc) {
6244
- setComment(start, offset - 2);
6351
+ setComment(start, offset - 2, isLeadingComment);
6245
6352
  }
6246
6353
  repeat = true;
6247
6354
  } else {
@@ -6319,7 +6426,7 @@ function tokenize(source, alternateCommentMode) {
6319
6426
  var ret = null;
6320
6427
  if (trailingLine === undefined) {
6321
6428
  if (commentLine === line - 1 && (alternateCommentMode || commentType === "*" || commentLineEmpty)) {
6322
- ret = commentText;
6429
+ ret = commentIsLeading ? commentText : null;
6323
6430
  }
6324
6431
  } else {
6325
6432
  /* istanbul ignore else */
@@ -6327,7 +6434,7 @@ function tokenize(source, alternateCommentMode) {
6327
6434
  peek();
6328
6435
  }
6329
6436
  if (commentLine === trailingLine && !commentLineEmpty && (alternateCommentMode || commentType === "/")) {
6330
- ret = commentText;
6437
+ ret = commentIsLeading ? null : commentText;
6331
6438
  }
6332
6439
  }
6333
6440
  return ret;
@@ -7302,6 +7409,37 @@ util.decorateEnum = function decorateEnum(object) {
7302
7409
  return enm;
7303
7410
  };
7304
7411
 
7412
+
7413
+ /**
7414
+ * Sets the value of a property by property path. If a value already exists, it is turned to an array
7415
+ * @param {Object.<string,*>} dst Destination object
7416
+ * @param {string} path dot '.' delimited path of the property to set
7417
+ * @param {Object} value the value to set
7418
+ * @returns {Object.<string,*>} Destination object
7419
+ */
7420
+ util.setProperty = function setProperty(dst, path, value) {
7421
+ function setProp(dst, path, value) {
7422
+ var part = path.shift();
7423
+ if (path.length > 0) {
7424
+ dst[part] = setProp(dst[part] || {}, path, value);
7425
+ } else {
7426
+ var prevValue = dst[part];
7427
+ if (prevValue)
7428
+ value = [].concat(prevValue).concat(value);
7429
+ dst[part] = value;
7430
+ }
7431
+ return dst;
7432
+ }
7433
+
7434
+ if (typeof dst !== "object")
7435
+ throw TypeError("dst must be an object");
7436
+ if (!path)
7437
+ throw TypeError("path must be specified");
7438
+
7439
+ path = path.split(".");
7440
+ return setProp(dst, path, value);
7441
+ };
7442
+
7305
7443
  /**
7306
7444
  * Decorator root (TypeScript).
7307
7445
  * @name util.decorateRoot
@@ -7545,8 +7683,8 @@ util.pool = require(9);
7545
7683
  util.LongBits = require(38);
7546
7684
 
7547
7685
  // global object reference
7548
- util.global = typeof window !== "undefined" && window
7549
- || typeof global !== "undefined" && global
7686
+ util.global = typeof global !== "undefined" && Object.prototype.toString.call(global) === "[object global]" && global
7687
+ || typeof window !== "undefined" && window
7550
7688
  || typeof self !== "undefined" && self
7551
7689
  || this; // eslint-disable-line no-invalid-this
7552
7690
 
@@ -8155,15 +8293,20 @@ wrappers[".google.protobuf.Any"] = {
8155
8293
 
8156
8294
  // unwrap value type if mapped
8157
8295
  if (object && object["@type"]) {
8158
- var type = this.lookup(object["@type"]);
8296
+ // Only use fully qualified type name after the last '/'
8297
+ var name = object["@type"].substring(object["@type"].lastIndexOf("/") + 1);
8298
+ var type = this.lookup(name);
8159
8299
  /* istanbul ignore else */
8160
8300
  if (type) {
8161
8301
  // type_url does not accept leading "."
8162
8302
  var type_url = object["@type"].charAt(0) === "." ?
8163
8303
  object["@type"].substr(1) : object["@type"];
8164
8304
  // type_url prefix is optional, but path seperator is required
8305
+ if (type_url.indexOf("/") === -1) {
8306
+ type_url = "/" + type_url;
8307
+ }
8165
8308
  return this.create({
8166
- type_url: "/" + type_url,
8309
+ type_url: type_url,
8167
8310
  value: type.encode(type.fromObject(object)).finish()
8168
8311
  });
8169
8312
  }
@@ -8174,10 +8317,17 @@ wrappers[".google.protobuf.Any"] = {
8174
8317
 
8175
8318
  toObject: function(message, options) {
8176
8319
 
8320
+ // Default prefix
8321
+ var googleApi = "type.googleapis.com/";
8322
+ var prefix = "";
8323
+ var name = "";
8324
+
8177
8325
  // decode value if requested and unmapped
8178
8326
  if (options && options.json && message.type_url && message.value) {
8179
8327
  // Only use fully qualified type name after the last '/'
8180
- var name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
8328
+ name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
8329
+ // Separate the prefix used
8330
+ prefix = message.type_url.substring(0, message.type_url.lastIndexOf("/") + 1);
8181
8331
  var type = this.lookup(name);
8182
8332
  /* istanbul ignore else */
8183
8333
  if (type)
@@ -8187,7 +8337,14 @@ wrappers[".google.protobuf.Any"] = {
8187
8337
  // wrap value if unmapped
8188
8338
  if (!(message instanceof this.ctor) && message instanceof Message) {
8189
8339
  var object = message.$type.toObject(message, options);
8190
- object["@type"] = message.$type.fullName;
8340
+ var messageName = message.$type.fullName[0] === "." ?
8341
+ message.$type.fullName.substr(1) : message.$type.fullName;
8342
+ // Default to type.googleapis.com prefix if no prefix is used
8343
+ if (prefix === "") {
8344
+ prefix = googleApi;
8345
+ }
8346
+ name = prefix + messageName;
8347
+ object["@type"] = name;
8191
8348
  return object;
8192
8349
  }
8193
8350