protobufjs 4.1.1 → 5.0.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.
Files changed (72) hide show
  1. package/.travis.yml +2 -1
  2. package/README.md +17 -17
  3. package/bower.json +2 -2
  4. package/cli/pbjs/targets/json.js +3 -2
  5. package/cli/pbjs/targets/proto.js +48 -24
  6. package/cli/pbjs.js +8 -1
  7. package/dist/README.md +10 -10
  8. package/dist/{ProtoBuf-light.js → protobuf-light.js} +49 -43
  9. package/dist/protobuf-light.min.js +87 -0
  10. package/dist/protobuf-light.min.js.gz +0 -0
  11. package/dist/protobuf-light.min.map +8 -0
  12. package/dist/{ProtoBuf.js → protobuf.js} +103 -66
  13. package/dist/protobuf.min.js +108 -0
  14. package/dist/protobuf.min.js.gz +0 -0
  15. package/dist/protobuf.min.map +8 -0
  16. package/docs/ProtoBuf.Builder.Message.html +106 -37
  17. package/docs/ProtoBuf.Builder.Service.html +9 -9
  18. package/docs/ProtoBuf.Builder.html +22 -22
  19. package/docs/ProtoBuf.DotProto.Parser.html +7 -7
  20. package/docs/ProtoBuf.DotProto.Tokenizer.html +11 -11
  21. package/docs/ProtoBuf.DotProto.html +2 -2
  22. package/docs/ProtoBuf.Map.html +6 -6
  23. package/docs/ProtoBuf.Reflect.Element.html +7 -7
  24. package/docs/ProtoBuf.Reflect.Enum.Value.html +10 -10
  25. package/docs/ProtoBuf.Reflect.Enum.html +21 -21
  26. package/docs/ProtoBuf.Reflect.Extension.html +3 -3
  27. package/docs/ProtoBuf.Reflect.Message.ExtensionField.html +29 -29
  28. package/docs/ProtoBuf.Reflect.Message.Field.html +28 -28
  29. package/docs/ProtoBuf.Reflect.Message.OneOf.html +10 -10
  30. package/docs/ProtoBuf.Reflect.Message.html +31 -28
  31. package/docs/ProtoBuf.Reflect.Namespace.html +19 -19
  32. package/docs/ProtoBuf.Reflect.Service.Method.html +11 -11
  33. package/docs/ProtoBuf.Reflect.Service.RPCMethod.html +17 -17
  34. package/docs/ProtoBuf.Reflect.Service.html +20 -20
  35. package/docs/ProtoBuf.Reflect.T.html +9 -9
  36. package/docs/ProtoBuf.Reflect.html +2 -2
  37. package/docs/ProtoBuf.Util.html +2 -151
  38. package/docs/ProtoBuf.html +9 -9
  39. package/docs/ProtoBuf.js.html +104 -67
  40. package/docs/index.html +1 -1
  41. package/donate.png +0 -0
  42. package/externs/{ProtoBuf.js → protobuf.js} +933 -922
  43. package/index.js +1 -18
  44. package/package.json +12 -11
  45. package/protobuf.png +0 -0
  46. package/sandbox/issue347/index.js +27 -0
  47. package/sandbox/issue355/main.proto +15 -0
  48. package/scripts/build.js +4 -9
  49. package/src/ProtoBuf/Builder/Message.js +9 -4
  50. package/src/ProtoBuf/Builder/Service.js +3 -0
  51. package/src/ProtoBuf/Builder.js +17 -10
  52. package/src/ProtoBuf/DotProto/Parser.js +54 -23
  53. package/src/ProtoBuf/Reflect/Element.js +4 -3
  54. package/src/ProtoBuf/Reflect/Message/Field.js +4 -3
  55. package/src/ProtoBuf/Reflect/Message.js +3 -3
  56. package/src/ProtoBuf/Util.js +3 -14
  57. package/src/bower.json +1 -1
  58. package/src/google/protobuf/descriptor.json +28 -14
  59. package/src/{ProtoBuf.js → protobuf.js} +0 -0
  60. package/src/wrap.js +6 -6
  61. package/tests/imports-weak.proto +7 -0
  62. package/tests/suite.js +64 -81
  63. package/ProtoBuf.png +0 -0
  64. package/dist/ProtoBuf-light.min.js +0 -88
  65. package/dist/ProtoBuf-light.min.js.gz +0 -0
  66. package/dist/ProtoBuf-light.min.map +0 -8
  67. package/dist/ProtoBuf.min.js +0 -108
  68. package/dist/ProtoBuf.min.js.gz +0 -0
  69. package/dist/ProtoBuf.min.map +0 -8
  70. package/externs/ByteBuffer.js +0 -767
  71. package/externs/Long.js +0 -328
  72. package/tests/gtfs-realtime.proto +0 -552
@@ -42,20 +42,20 @@
42
42
  */
43
43
 
44
44
  /**
45
- * @license ProtoBuf.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
45
+ * @license protobuf.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
46
46
  * Released under the Apache License, Version 2.0
47
- * see: https://github.com/dcodeIO/ProtoBuf.js for details
47
+ * see: https://github.com/dcodeIO/protobuf.js for details
48
48
  */
49
49
  (function(global, factory) {
50
50
 
51
51
  /* AMD */ if (typeof define === 'function' && define["amd"])
52
- define(["ByteBuffer"], factory);
52
+ define(["bytebuffer"], factory);
53
53
  /* CommonJS */ else if (typeof require === "function" && typeof module === "object" && module && module["exports"])
54
- module["exports"] = factory(require("bytebuffer"), require);
54
+ module["exports"] = factory(require("bytebuffer"), true);
55
55
  /* Global */ else
56
56
  (global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]);
57
57
 
58
- })(this, function(ByteBuffer, nodeRequire) {
58
+ })(this, function(ByteBuffer, isCommonJS) {
59
59
  "use strict";
60
60
 
61
61
  /**
@@ -84,7 +84,7 @@
84
84
  * @const
85
85
  * @expose
86
86
  */
87
- ProtoBuf.VERSION = "4.1.1";
87
+ ProtoBuf.VERSION = "5.0.1";
88
88
 
89
89
  /**
90
90
  * Wire types.
@@ -378,8 +378,9 @@
378
378
  if (callback && typeof callback != 'function')
379
379
  callback = null;
380
380
  if (Util.IS_NODE) {
381
+ var fs = require("fs");
381
382
  if (callback) {
382
- Util.require("fs").readFile(path, function(err, data) {
383
+ fs.readFile(path, function(err, data) {
383
384
  if (err)
384
385
  callback(null);
385
386
  else
@@ -387,7 +388,7 @@
387
388
  });
388
389
  } else
389
390
  try {
390
- return Util.require("fs").readFileSync(path);
391
+ return fs.readFileSync(path);
391
392
  } catch (e) {
392
393
  return null;
393
394
  }
@@ -417,18 +418,6 @@
417
418
  }
418
419
  };
419
420
 
420
- /**
421
- * Requires a node module.
422
- * @function
423
- * @param {string} path
424
- * @returns {*}
425
- * @throws Error If node require is not supported
426
- * @expose
427
- */
428
- Util.require = Util.IS_NODE
429
- ? function(path) { return nodeRequire(path); }
430
- : function(path) { throw Error("node require is not supported by this platform")};
431
-
432
421
  /**
433
422
  * Converts a string to camel case.
434
423
  * @param {string} str
@@ -763,7 +752,8 @@
763
752
  // "syntax": undefined
764
753
  };
765
754
  var token,
766
- head = true;
755
+ head = true,
756
+ weak;
767
757
  try {
768
758
  while (token = this.tn.next()) {
769
759
  switch (token) {
@@ -780,11 +770,12 @@
780
770
  if (!head)
781
771
  throw Error("unexpected 'import'");
782
772
  token = this.tn.peek();
783
- if (token === "public") // ignored
773
+ if (token === "public" || (weak = token === "weak")) // token ignored
784
774
  this.tn.next();
785
775
  token = this._readString();
786
776
  this.tn.skip(";");
787
- topLevel["imports"].push(token);
777
+ if (!weak) // import ignored
778
+ topLevel["imports"].push(token);
788
779
  break;
789
780
  case 'syntax':
790
781
  if (!head)
@@ -1109,6 +1100,7 @@
1109
1100
  "enums": [],
1110
1101
  "messages": [],
1111
1102
  "options": {},
1103
+ "services": [],
1112
1104
  "oneofs": {}
1113
1105
  // "extensions": undefined
1114
1106
  };
@@ -1135,8 +1127,12 @@
1135
1127
  this._parseMessage(msg);
1136
1128
  else if (token === "option")
1137
1129
  this._parseOption(msg);
1130
+ else if (token === "service")
1131
+ this._parseService(msg);
1138
1132
  else if (token === "extensions")
1139
- this._parseExtensions(msg);
1133
+ msg["extensions"] = this._parseExtensionRanges();
1134
+ else if (token === "reserved")
1135
+ this._parseIgnored(); // TODO
1140
1136
  else if (token === "extend")
1141
1137
  this._parseExtend(msg);
1142
1138
  else if (Lang.TYPEREF.test(token)) {
@@ -1151,6 +1147,16 @@
1151
1147
  return msg;
1152
1148
  };
1153
1149
 
1150
+ /**
1151
+ * Parses an ignored statement.
1152
+ * @private
1153
+ */
1154
+ ParserPrototype._parseIgnored = function() {
1155
+ while (this.tn.peek() !== ';')
1156
+ this.tn.next();
1157
+ this.tn.skip(";");
1158
+ };
1159
+
1154
1160
  /**
1155
1161
  * Parses a message field.
1156
1162
  * @param {!Object} msg Message definition
@@ -1313,29 +1319,43 @@
1313
1319
  };
1314
1320
 
1315
1321
  /**
1316
- * Parses an extensions statement.
1317
- * @param {!Object} msg Message object
1322
+ * Parses extension / reserved ranges.
1323
+ * @returns {!Array.<!Array.<number>>}
1318
1324
  * @private
1319
1325
  */
1320
- ParserPrototype._parseExtensions = function(msg) {
1321
- var token = this.tn.next(),
1326
+ ParserPrototype._parseExtensionRanges = function() {
1327
+ var ranges = [];
1328
+ var token,
1329
+ range,
1330
+ value;
1331
+ do {
1322
1332
  range = [];
1323
- if (token === "min")
1324
- range.push(ProtoBuf.ID_MIN);
1325
- else if (token === "max")
1326
- range.push(ProtoBuf.ID_MAX);
1327
- else
1328
- range.push(mkNumber(token));
1329
- this.tn.skip("to");
1330
- token = this.tn.next();
1331
- if (token === "min")
1332
- range.push(ProtoBuf.ID_MIN);
1333
- else if (token === "max")
1334
- range.push(ProtoBuf.ID_MAX);
1335
- else
1336
- range.push(mkNumber(token));
1333
+ while (true) {
1334
+ token = this.tn.next();
1335
+ switch (token) {
1336
+ case "min":
1337
+ value = ProtoBuf.ID_MIN;
1338
+ break;
1339
+ case "max":
1340
+ value = ProtoBuf.ID_MAX;
1341
+ break;
1342
+ default:
1343
+ value = mkNumber(token);
1344
+ break;
1345
+ }
1346
+ range.push(value);
1347
+ if (range.length === 2)
1348
+ break;
1349
+ if (this.tn.peek() !== "to") {
1350
+ range.push(value);
1351
+ break;
1352
+ }
1353
+ this.tn.next();
1354
+ }
1355
+ ranges.push(range);
1356
+ } while (this.tn.omit(","));
1337
1357
  this.tn.skip(";");
1338
- msg["extensions"] = range;
1358
+ return ranges;
1339
1359
  };
1340
1360
 
1341
1361
  /**
@@ -1803,9 +1823,10 @@
1803
1823
  * @expose
1804
1824
  */
1805
1825
  ElementPrototype.verifyValue = function(value) {
1806
- var fail = function(val, msg) {
1807
- throw Error("Illegal value for "+this.toString(true)+" of type "+this.type.name+": "+val+" ("+msg+")");
1808
- }.bind(this);
1826
+ var self = this;
1827
+ function fail(val, msg) {
1828
+ throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
1829
+ }
1809
1830
  switch (this.type) {
1810
1831
  // Signed 32bit
1811
1832
  case ProtoBuf.TYPES["int32"]:
@@ -2295,10 +2316,10 @@
2295
2316
 
2296
2317
  /**
2297
2318
  * Extensions range.
2298
- * @type {!Array.<number>}
2319
+ * @type {!Array.<number>|undefined}
2299
2320
  * @expose
2300
2321
  */
2301
- this.extensions = [ProtoBuf.ID_MIN, ProtoBuf.ID_MAX];
2322
+ this.extensions = undefined;
2302
2323
 
2303
2324
  /**
2304
2325
  * Runtime message class.
@@ -2697,18 +2718,19 @@
2697
2718
  * @name ProtoBuf.Builder.Message#encodeDelimited
2698
2719
  * @function
2699
2720
  * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
2721
+ * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
2700
2722
  * @return {!ByteBuffer} Encoded message as a ByteBuffer
2701
2723
  * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
2702
2724
  * returns the encoded ByteBuffer in the `encoded` property on the error.
2703
2725
  * @expose
2704
2726
  */
2705
- MessagePrototype.encodeDelimited = function(buffer) {
2727
+ MessagePrototype.encodeDelimited = function(buffer, noVerify) {
2706
2728
  var isNew = false;
2707
2729
  if (!buffer)
2708
2730
  buffer = new ByteBuffer(),
2709
2731
  isNew = true;
2710
2732
  var enc = new ByteBuffer().LE();
2711
- T.encode(this, enc).flip();
2733
+ T.encode(this, enc, noVerify).flip();
2712
2734
  buffer.writeVarint32(enc.remaining());
2713
2735
  buffer.append(enc);
2714
2736
  return isNew ? buffer.flip() : buffer;
@@ -2855,7 +2877,7 @@
2855
2877
  return binaryAsBase64 ? obj.toBase64() : obj.toBuffer();
2856
2878
  // Convert Longs to proper objects or strings
2857
2879
  if (ProtoBuf.Long.isLong(obj))
2858
- return longsAsStrings ? obj.toString() : new ProtoBuf.Long(obj);
2880
+ return longsAsStrings ? obj.toString() : ProtoBuf.Long.fromValue(obj);
2859
2881
  var clone;
2860
2882
  // Clone arrays
2861
2883
  if (Array.isArray(obj)) {
@@ -2917,6 +2939,7 @@
2917
2939
  * @name ProtoBuf.Builder.Message.decode
2918
2940
  * @function
2919
2941
  * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
2942
+ * @param {(number|string)=} length Message length. Defaults to decode all the remainig data.
2920
2943
  * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
2921
2944
  * @return {!ProtoBuf.Builder.Message} Decoded message
2922
2945
  * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
@@ -2925,7 +2948,10 @@
2925
2948
  * @see ProtoBuf.Builder.Message.decode64
2926
2949
  * @see ProtoBuf.Builder.Message.decodeHex
2927
2950
  */
2928
- Message.decode = function(buffer, enc) {
2951
+ Message.decode = function(buffer, length, enc) {
2952
+ if (typeof length === 'string')
2953
+ enc = length,
2954
+ length = -1;
2929
2955
  if (typeof buffer === 'string')
2930
2956
  buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
2931
2957
  buffer = ByteBuffer.isByteBuffer(buffer) ? buffer : ByteBuffer.wrap(buffer); // May throw
@@ -3186,7 +3212,7 @@
3186
3212
  /**
3187
3213
  * Decodes an encoded message and returns the decoded message.
3188
3214
  * @param {ByteBuffer} buffer ByteBuffer to decode from
3189
- * @param {number=} length Message length. Defaults to decode all the available data.
3215
+ * @param {number=} length Message length. Defaults to decode all remaining data.
3190
3216
  * @param {number=} expectedGroupEndId Expected GROUPEND id if this is a legacy group
3191
3217
  * @return {ProtoBuf.Builder.Message} Decoded message
3192
3218
  * @throws {Error} If the message cannot be decoded
@@ -3436,9 +3462,10 @@
3436
3462
  */
3437
3463
  FieldPrototype.verifyValue = function(value, skipRepeated) {
3438
3464
  skipRepeated = skipRepeated || false;
3439
- var fail = function(val, msg) {
3440
- throw Error("Illegal value for "+this.toString(true)+" of type "+this.type.name+": "+val+" ("+msg+")");
3441
- }.bind(this);
3465
+ var self = this;
3466
+ function fail(val, msg) {
3467
+ throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
3468
+ }
3442
3469
  if (value === null) { // NULL values for optional fields
3443
3470
  if (this.required)
3444
3471
  fail(typeof value, "required");
@@ -4052,6 +4079,9 @@
4052
4079
  callback(err);
4053
4080
  return;
4054
4081
  }
4082
+ // Coalesce to empty string when service response has empty content
4083
+ if (res === null)
4084
+ res = ''
4055
4085
  try { res = method.resolvedResponseType.clazz.decode(res); } catch (notABuffer) {}
4056
4086
  if (!res || !(res instanceof method.resolvedResponseType.clazz)) {
4057
4087
  callback(Error("Illegal response type received in service method "+ T.name+"#"+method.name));
@@ -4495,13 +4525,12 @@
4495
4525
  subObj.push(svc);
4496
4526
  });
4497
4527
 
4498
- // Set extension range
4528
+ // Set extension ranges
4499
4529
  if (def["extensions"]) {
4500
- obj.extensions = def["extensions"];
4501
- if (obj.extensions[0] < ProtoBuf.ID_MIN)
4502
- obj.extensions[0] = ProtoBuf.ID_MIN;
4503
- if (obj.extensions[1] > ProtoBuf.ID_MAX)
4504
- obj.extensions[1] = ProtoBuf.ID_MAX;
4530
+ if (typeof def["extensions"][0] === 'number') // pre 5.0.1
4531
+ obj.extensions = [ def["extensions"] ];
4532
+ else
4533
+ obj.extensions = def["extensions"];
4505
4534
  }
4506
4535
 
4507
4536
  // Create on top of current namespace
@@ -4540,8 +4569,16 @@
4540
4569
  def["fields"].forEach(function(fld) {
4541
4570
  if (obj.getChild(fld['id']|0) !== null)
4542
4571
  throw Error("duplicate extended field id in "+obj.name+": "+fld['id']);
4543
- if (fld['id'] < obj.extensions[0] || fld['id'] > obj.extensions[1])
4544
- throw Error("illegal extended field id in "+obj.name+": "+fld['id']+" ("+obj.extensions.join(' to ')+" expected)");
4572
+ // Check if field id is allowed to be extended
4573
+ if (obj.extensions) {
4574
+ var valid = false;
4575
+ obj.extensions.forEach(function(range) {
4576
+ if (fld["id"] >= range[0] && fld["id"] <= range[1])
4577
+ valid = true;
4578
+ });
4579
+ if (!valid)
4580
+ throw Error("illegal extended field id in "+obj.name+": "+fld['id']+" (not within valid ranges)");
4581
+ }
4545
4582
  // Convert extension field names to camel case notation if the override is set
4546
4583
  var name = fld["name"];
4547
4584
  if (this.options['convertFieldsToCamelCase'])
@@ -4610,7 +4647,7 @@
4610
4647
  if (typeof filename === 'string') {
4611
4648
 
4612
4649
  if (ProtoBuf.Util.IS_NODE)
4613
- filename = ProtoBuf.Util.require("path")['resolve'](filename);
4650
+ filename = require("path")['resolve'](filename);
4614
4651
  if (this.files[filename] === true)
4615
4652
  return this.reset();
4616
4653
  this.files[filename] = true;
@@ -4619,7 +4656,7 @@
4619
4656
 
4620
4657
  var root = filename.root;
4621
4658
  if (ProtoBuf.Util.IS_NODE)
4622
- root = ProtoBuf.Util.require("path")['resolve'](root);
4659
+ root = require("path")['resolve'](root);
4623
4660
  if (root.indexOf("\\") >= 0 || filename.file.indexOf("\\") >= 0)
4624
4661
  delim = '\\';
4625
4662
  var fname = root + delim + filename.file;
@@ -5227,7 +5264,7 @@
5227
5264
  <br clear="both">
5228
5265
 
5229
5266
  <footer>
5230
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a> on Mon Sep 28 2015 15:24:51 GMT+0200 (Mitteleuropäische Sommerzeit)
5267
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a> on Wed Jan 06 2016 01:34:58 GMT+0100 (Mitteleuropäische Zeit)
5231
5268
  </footer>
5232
5269
 
5233
5270
  <script> prettyPrint(); </script>
package/docs/index.html CHANGED
@@ -54,7 +54,7 @@
54
54
  <br clear="both">
55
55
 
56
56
  <footer>
57
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a> on Mon Sep 28 2015 15:24:51 GMT+0200 (Mitteleuropäische Sommerzeit)
57
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a> on Wed Jan 06 2016 01:34:58 GMT+0100 (Mitteleuropäische Zeit)
58
58
  </footer>
59
59
 
60
60
  <script> prettyPrint(); </script>
package/donate.png ADDED
Binary file