protobufjs 3.6.0 → 3.8.2

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 (99) hide show
  1. package/bin/proto2js +16 -4
  2. package/bower.json +1 -1
  3. package/dist/ProtoBuf.js +645 -441
  4. package/dist/ProtoBuf.min.js +96 -96
  5. package/dist/ProtoBuf.min.js.gz +0 -0
  6. package/dist/ProtoBuf.min.map +2 -2
  7. package/dist/ProtoBuf.noparse.js +520 -357
  8. package/dist/ProtoBuf.noparse.min.js +68 -66
  9. package/dist/ProtoBuf.noparse.min.js.gz +0 -0
  10. package/dist/ProtoBuf.noparse.min.map +3 -3
  11. package/docs/ProtoBuf.Builder.Message.html +673 -135
  12. package/docs/ProtoBuf.Builder.Service.html +58 -19
  13. package/docs/ProtoBuf.Builder.html +374 -46
  14. package/docs/ProtoBuf.DotProto.Parser.html +58 -19
  15. package/docs/ProtoBuf.DotProto.Tokenizer.html +104 -19
  16. package/docs/ProtoBuf.DotProto.html +16 -10
  17. package/docs/ProtoBuf.Reflect.Enum.Value.html +207 -33
  18. package/docs/ProtoBuf.Reflect.Enum.html +318 -56
  19. package/docs/ProtoBuf.Reflect.Extension.html +53 -13
  20. package/docs/ProtoBuf.Reflect.Message.ExtensionField.html +450 -68
  21. package/docs/ProtoBuf.Reflect.Message.Field.html +454 -51
  22. package/docs/ProtoBuf.Reflect.Message.OneOf.html +1044 -0
  23. package/docs/ProtoBuf.Reflect.Message.html +378 -64
  24. package/docs/ProtoBuf.Reflect.Namespace.html +333 -51
  25. package/docs/ProtoBuf.Reflect.Service.Method.html +228 -35
  26. package/docs/ProtoBuf.Reflect.Service.RPCMethod.html +262 -41
  27. package/docs/ProtoBuf.Reflect.Service.html +318 -56
  28. package/docs/ProtoBuf.Reflect.T.html +210 -25
  29. package/docs/ProtoBuf.Reflect.html +17 -11
  30. package/docs/ProtoBuf.Util.html +59 -13
  31. package/docs/ProtoBuf.html +235 -67
  32. package/docs/ProtoBuf.js.html +648 -443
  33. package/docs/index.html +4 -2
  34. package/docs/styles/jsdoc-default.css +2 -2
  35. package/examples/protoify/.npmignore +2 -0
  36. package/examples/protoify/README.md +28 -0
  37. package/examples/protoify/index.js +147 -0
  38. package/examples/protoify/json.js +123 -0
  39. package/examples/protoify/json.json +123 -0
  40. package/examples/protoify/json.proto +30 -0
  41. package/examples/protoify/package.json +15 -0
  42. package/examples/protoify/test.js +56 -0
  43. package/examples/websocket/README.md +1 -0
  44. package/examples/websocket/package.json +1 -1
  45. package/externs/ProtoBuf.js +922 -922
  46. package/package.json +3 -3
  47. package/scripts/build.js +58 -58
  48. package/src/ProtoBuf/Builder/Message.js +107 -77
  49. package/src/ProtoBuf/Builder/Service.js +8 -5
  50. package/src/ProtoBuf/Builder.js +71 -37
  51. package/src/ProtoBuf/DotProto/Parser.js +108 -72
  52. package/src/ProtoBuf/DotProto/Tokenizer.js +17 -11
  53. package/src/ProtoBuf/Lang.js +1 -1
  54. package/src/ProtoBuf/Reflect/Enum/Value.js +3 -2
  55. package/src/ProtoBuf/Reflect/Enum.js +9 -5
  56. package/src/ProtoBuf/Reflect/Extension.js +4 -3
  57. package/src/ProtoBuf/Reflect/Message/ExtensionField.js +4 -3
  58. package/src/ProtoBuf/Reflect/Message/Field.js +45 -26
  59. package/src/ProtoBuf/Reflect/Message/OneOf.js +19 -0
  60. package/src/ProtoBuf/Reflect/Message.js +36 -19
  61. package/src/ProtoBuf/Reflect/Namespace.js +19 -15
  62. package/src/ProtoBuf/Reflect/Service/Method.js +9 -5
  63. package/src/ProtoBuf/Reflect/Service/RPCMethod.js +3 -2
  64. package/src/ProtoBuf/Reflect/Service.js +9 -5
  65. package/src/ProtoBuf/Reflect/T.js +20 -6
  66. package/src/ProtoBuf/Reflect.js +9 -0
  67. package/src/ProtoBuf/Util.js +132 -132
  68. package/src/ProtoBuf.js +15 -17
  69. package/src/google/protobuf/descriptor.json +33 -13
  70. package/tests/bench.txt +373 -373
  71. package/tests/complex.json +8 -4
  72. package/tests/custom-options.json +169 -169
  73. package/tests/extend.json +71 -71
  74. package/tests/nodeunit-browser/nodeunit.css +70 -70
  75. package/tests/nodeunit-browser/nodeunit.js +2108 -2108
  76. package/tests/oneof.proto +6 -0
  77. package/tests/options.json +32 -32
  78. package/tests/options.proto +2 -0
  79. package/tests/proto2js/Bar.json +46 -46
  80. package/tests/suite.js +83 -12
  81. package/.idea/.name +0 -1
  82. package/.idea/ProtoBuf.iml +0 -9
  83. package/.idea/dictionaries/Daniel.xml +0 -7
  84. package/.idea/encodings.xml +0 -5
  85. package/.idea/misc.xml +0 -5
  86. package/.idea/modules.xml +0 -9
  87. package/.idea/scopes/scope_settings.xml +0 -5
  88. package/.idea/vcs.xml +0 -7
  89. package/.idea/workspace.xml +0 -551
  90. package/NOTICE +0 -2
  91. package/sandbox/issue146/MyOptions.proto +0 -28
  92. package/sandbox/issue146/Sample.proto +0 -21
  93. package/sandbox/issue146/main.js +0 -3
  94. package/sandbox/issue147/enum.proto +0 -8
  95. package/sandbox/issue147/main.js +0 -3
  96. package/sandbox/issue42/innerextend.proto +0 -18
  97. package/sandbox/issue42/main.js +0 -8
  98. package/sandbox/issue42/outerextend.proto +0 -17
  99. package/v8.log +0 -3828
@@ -9,16 +9,17 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
9
9
  * Constructs a new Builder.
10
10
  * @exports ProtoBuf.Builder
11
11
  * @class Provides the functionality to build protocol messages.
12
+ * @param {Object.<string,*>=} options Options
12
13
  * @constructor
13
14
  */
14
- var Builder = function() {
15
+ var Builder = function(options) {
15
16
 
16
17
  /**
17
18
  * Namespace.
18
19
  * @type {ProtoBuf.Reflect.Namespace}
19
20
  * @expose
20
21
  */
21
- this.ns = new Reflect.Namespace(null, ""); // Global namespace
22
+ this.ns = new Reflect.Namespace(this, null, ""); // Global namespace
22
23
 
23
24
  /**
24
25
  * Namespace pointer.
@@ -54,13 +55,26 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
54
55
  * @expose
55
56
  */
56
57
  this.importRoot = null;
58
+
59
+ /**
60
+ * Options.
61
+ * @type {!Object.<string, *>}
62
+ * @expose
63
+ */
64
+ this.options = options || {};
57
65
  };
58
66
 
67
+ /**
68
+ * @alias ProtoBuf.Builder.prototype
69
+ * @inner
70
+ */
71
+ var BuilderPrototype = Builder.prototype;
72
+
59
73
  /**
60
74
  * Resets the pointer to the root namespace.
61
75
  * @expose
62
76
  */
63
- Builder.prototype.reset = function() {
77
+ BuilderPrototype.reset = function() {
64
78
  this.ptr = this.ns;
65
79
  };
66
80
 
@@ -72,7 +86,7 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
72
86
  * @throws {Error} If the package name is invalid
73
87
  * @expose
74
88
  */
75
- Builder.prototype.define = function(pkg, options) {
89
+ BuilderPrototype.define = function(pkg, options) {
76
90
  if (typeof pkg !== 'string' || !Lang.TYPEREF.test(pkg))
77
91
  throw Error("Illegal package: "+pkg);
78
92
  var part = pkg.split("."), i;
@@ -81,7 +95,7 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
81
95
  throw Error("Illegal package: "+part[i]);
82
96
  for (i=0; i<part.length; i++) {
83
97
  if (this.ptr.getChild(part[i]) === null) // Keep existing namespace
84
- this.ptr.addChild(new Reflect.Namespace(this.ptr, part[i], options));
98
+ this.ptr.addChild(new Reflect.Namespace(this, this.ptr, part[i], options));
85
99
  this.ptr = this.ptr.getChild(part[i]);
86
100
  }
87
101
  return this;
@@ -195,7 +209,7 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
195
209
  * @throws {Error} If a message definition is invalid
196
210
  * @expose
197
211
  */
198
- Builder.prototype.create = function(defs) {
212
+ BuilderPrototype.create = function(defs) {
199
213
  if (!defs)
200
214
  return this; // Nothing to create
201
215
  if (!ProtoBuf.Util.isArray(defs))
@@ -204,35 +218,51 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
204
218
  return this;
205
219
 
206
220
  // It's quite hard to keep track of scopes and memory here, so let's do this iteratively.
207
- var stack = [], def, obj, subObj, i, j;
221
+ var stack = [];
208
222
  stack.push(defs); // One level [a, b, c]
209
223
  while (stack.length > 0) {
210
224
  defs = stack.pop();
211
225
  if (ProtoBuf.Util.isArray(defs)) { // Stack always contains entire namespaces
212
226
  while (defs.length > 0) {
213
- def = defs.shift(); // Namespace always contains an array of messages, enums and services
227
+ var def = defs.shift(); // Namespace always contains an array of messages, enums and services
214
228
  if (Builder.isValidMessage(def)) {
215
- obj = new Reflect.Message(this.ptr, def["name"], def["options"], def["isGroup"]);
229
+ var obj = new Reflect.Message(this, this.ptr, def["name"], def["options"], def["isGroup"]);
230
+ // Create OneOfs
231
+ var oneofs = {};
232
+ if (def["oneofs"]) {
233
+ var keys = Object.keys(def["oneofs"]);
234
+ for (var i=0, k=keys.length; i<k; ++i)
235
+ obj.addChild(oneofs[keys[i]] = new Reflect.Message.OneOf(this, obj, keys[i]));
236
+ }
216
237
  // Create fields
217
238
  if (def["fields"] && def["fields"].length > 0) {
218
- for (i=0; i<def["fields"].length; i++) { // i=Fields
219
- if (obj.getChild(def['fields'][i]['id']) !== null)
220
- throw Error("Duplicate field id in message "+obj.name+": "+def['fields'][i]['id']);
221
- if (def["fields"][i]["options"]) {
222
- subObj = Object.keys(def["fields"][i]["options"]);
223
- for (j=0; j<subObj.length; j++) { // j=Option names
224
- if (typeof subObj[j] !== 'string')
225
- throw Error("Illegal field option name in message "+obj.name+"#"+def["fields"][i]["name"]+": "+subObj[j]);
226
- if (typeof def["fields"][i]["options"][subObj[j]] !== 'string' && typeof def["fields"][i]["options"][subObj[j]] !== 'number' && typeof def["fields"][i]["options"][subObj[j]] !== 'boolean')
227
- throw Error("Illegal field option value in message "+obj.name+"#"+def["fields"][i]["name"]+"#"+subObj[j]+": "+def["fields"][i]["options"][subObj[j]]);
239
+ for (i=0, k=def["fields"].length; i<k; ++i) { // i:k=Fields
240
+ var fld = def['fields'][i];
241
+ if (obj.getChild(fld['id']) !== null)
242
+ throw Error("Duplicate field id in message "+obj.name+": "+fld['id']);
243
+ if (fld["options"]) {
244
+ var opts = Object.keys(fld["options"]);
245
+ for (var j= 0,l=opts.length; j<l; ++j) { // j:l=Option names
246
+ if (typeof opts[j] !== 'string')
247
+ throw Error("Illegal field option name in message "+obj.name+"#"+fld["name"]+": "+opts[j]);
248
+ if (typeof fld["options"][opts[j]] !== 'string' && typeof fld["options"][opts[j]] !== 'number' && typeof fld["options"][opts[j]] !== 'boolean')
249
+ throw Error("Illegal field option value in message "+obj.name+"#"+fld["name"]+"#"+opts[j]+": "+fld["options"][opts[j]]);
228
250
  }
229
- subObj = null;
230
251
  }
231
- obj.addChild(new Reflect.Message.Field(obj, def["fields"][i]["rule"], def["fields"][i]["type"], def["fields"][i]["name"], def["fields"][i]["id"], def["fields"][i]["options"]));
252
+ var oneof = null;
253
+ if (typeof fld["oneof"] === 'string') {
254
+ oneof = oneofs[fld["oneof"]];
255
+ if (typeof oneof === 'undefined')
256
+ throw Error("Illegal oneof in message "+obj.name+"#"+fld["name"]+": "+fld["oneof"]);
257
+ }
258
+ fld = new Reflect.Message.Field(this, obj, fld["rule"], fld["type"], fld["name"], fld["id"], fld["options"], oneof);
259
+ if (oneof)
260
+ oneof.fields.push(fld);
261
+ obj.addChild(fld);
232
262
  }
233
263
  }
234
264
  // Push enums and messages to stack
235
- subObj = [];
265
+ var subObj = [];
236
266
  if (typeof def["enums"] !== 'undefined' && def['enums'].length > 0)
237
267
  for (i=0; i<def["enums"].length; i++)
238
268
  subObj.push(def["enums"][i]);
@@ -259,16 +289,16 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
259
289
  subObj = null;
260
290
  obj = null;
261
291
  } else if (Builder.isValidEnum(def)) {
262
- obj = new Reflect.Enum(this.ptr, def["name"], def["options"]);
292
+ obj = new Reflect.Enum(this, this.ptr, def["name"], def["options"]);
263
293
  for (i=0; i<def["values"].length; i++)
264
- obj.addChild(new Reflect.Enum.Value(obj, def["values"][i]["name"], def["values"][i]["id"]));
294
+ obj.addChild(new Reflect.Enum.Value(this, obj, def["values"][i]["name"], def["values"][i]["id"]));
265
295
  this.ptr.addChild(obj);
266
296
  obj = null;
267
297
  } else if (Builder.isValidService(def)) {
268
- obj = new Reflect.Service(this.ptr, def["name"], def["options"]);
298
+ obj = new Reflect.Service(this, this.ptr, def["name"], def["options"]);
269
299
  for (i in def["rpc"])
270
300
  if (def["rpc"].hasOwnProperty(i))
271
- obj.addChild(new Reflect.Service.RPCMethod(obj, i, def["rpc"][i]["request"], def["rpc"][i]["response"], def["rpc"][i]["options"]));
301
+ obj.addChild(new Reflect.Service.RPCMethod(this, obj, i, def["rpc"][i]["request"], def["rpc"][i]["response"], def["rpc"][i]["options"]));
272
302
  this.ptr.addChild(obj);
273
303
  obj = null;
274
304
  } else if (Builder.isValidExtend(def)) {
@@ -279,10 +309,16 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
279
309
  throw Error("Duplicate extended field id in message "+obj.name+": "+def['fields'][i]['id']);
280
310
  if (def['fields'][i]['id'] < obj.extensions[0] || def['fields'][i]['id'] > obj.extensions[1])
281
311
  throw Error("Illegal extended field id in message "+obj.name+": "+def['fields'][i]['id']+" ("+obj.extensions.join(' to ')+" expected)");
312
+ // Convert extension field names to camel case notation if the override is set
313
+ var name = def["fields"][i]["name"];
314
+ if (this.options['convertFieldsToCamelCase'])
315
+ name = Reflect.Message.Field._toCamelCase(def["fields"][i]["name"]);
282
316
  // see #161: Extensions use their fully qualified name as their runtime key and...
283
- var fld = new Reflect.Message.ExtensionField(obj, def["fields"][i]["rule"], def["fields"][i]["type"], this.ptr.fqn()+'.'+def["fields"][i]["name"], def["fields"][i]["id"], def["fields"][i]["options"]);
284
- // ...are added on top of the current namespace as an extension which is used for resolving their type later on
285
- var ext = new Reflect.Extension(this.ptr, def["fields"][i]["name"], fld);
317
+ fld = new Reflect.Message.ExtensionField(this, obj, def["fields"][i]["rule"], def["fields"][i]["type"], this.ptr.fqn()+'.'+name, def["fields"][i]["id"], def["fields"][i]["options"]);
318
+ // ...are added on top of the current namespace as an extension which is used for
319
+ // resolving their type later on (the extension always keeps the original name to
320
+ // prevent naming collisions)
321
+ var ext = new Reflect.Extension(this, this.ptr, def["fields"][i]["name"], fld);
286
322
  fld.extension = ext;
287
323
  this.ptr.addChild(ext);
288
324
  obj.addChild(fld);
@@ -312,7 +348,7 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
312
348
  * @throws {Error} If the definition or file cannot be imported
313
349
  * @expose
314
350
  */
315
- Builder.prototype["import"] = function(json, filename) {
351
+ BuilderPrototype["import"] = function(json, filename) {
316
352
  if (typeof filename === 'string') {
317
353
  if (ProtoBuf.Util.IS_NODE)
318
354
  filename = require("path")['resolve'](filename);
@@ -446,7 +482,7 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
446
482
  * @throws {Error} If a type cannot be resolved
447
483
  * @expose
448
484
  */
449
- Builder.prototype.resolveAll = function() {
485
+ BuilderPrototype.resolveAll = function() {
450
486
  // Resolve all reflected objects
451
487
  var res;
452
488
  if (this.ptr == null || typeof this.ptr.type === 'object')
@@ -489,9 +525,7 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
489
525
  // Should not happen as nothing else is implemented
490
526
  throw Error("Illegal service type in "+this.ptr.toString(true));
491
527
  }
492
- } else if (this.ptr instanceof ProtoBuf.Reflect.Extension) {
493
- // There are no runtime counterparts to extensions
494
- } else
528
+ } else if (!(this.ptr instanceof ProtoBuf.Reflect.Message.OneOf) && !(this.ptr instanceof ProtoBuf.Reflect.Extension))
495
529
  throw Error("Illegal object in namespace: "+typeof(this.ptr)+":"+this.ptr);
496
530
  this.reset();
497
531
  };
@@ -504,7 +538,7 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
504
538
  * @throws {Error} If a type could not be resolved
505
539
  * @expose
506
540
  */
507
- Builder.prototype.build = function(path) {
541
+ BuilderPrototype.build = function(path) {
508
542
  this.reset();
509
543
  if (!this.resolved)
510
544
  this.resolveAll(),
@@ -533,7 +567,7 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
533
567
  * @param {string=} path Specifies what to return. If omitted, the entire namespace wiil be returned.
534
568
  * @return {ProtoBuf.Reflect.T} Reflection descriptor or `null` if not found
535
569
  */
536
- Builder.prototype.lookup = function(path) {
570
+ BuilderPrototype.lookup = function(path) {
537
571
  return path ? this.ns.resolve(path) : this.ns;
538
572
  };
539
573
 
@@ -542,7 +576,7 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
542
576
  * @return {string} String representation as of "Builder"
543
577
  * @expose
544
578
  */
545
- Builder.prototype.toString = function() {
579
+ BuilderPrototype.toString = function() {
546
580
  return "Builder";
547
581
  };
548
582