protobufjs 4.1.3 → 5.0.3

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 (80) hide show
  1. package/.travis.yml +3 -1
  2. package/README.md +147 -25
  3. package/bin/pbjs +2 -2
  4. package/bower.json +3 -3
  5. package/cli/pbjs/targets/amd.js +1 -1
  6. package/cli/pbjs/targets/json.js +9 -1
  7. package/cli/pbjs/targets/proto.js +48 -24
  8. package/cli/pbjs/util.js +1 -1
  9. package/cli/pbjs.js +9 -2
  10. package/dist/README.md +10 -10
  11. package/dist/{ProtoBuf-light.js → protobuf-light.js} +92 -37
  12. package/dist/protobuf-light.min.js +94 -0
  13. package/dist/protobuf-light.min.js.gz +0 -0
  14. package/dist/protobuf-light.min.map +8 -0
  15. package/dist/{ProtoBuf.js → protobuf.js} +150 -60
  16. package/dist/protobuf.min.js +116 -0
  17. package/dist/protobuf.min.js.gz +0 -0
  18. package/dist/protobuf.min.map +8 -0
  19. package/docs/ProtoBuf.Builder.Message.html +106 -37
  20. package/docs/ProtoBuf.Builder.Service.html +9 -9
  21. package/docs/ProtoBuf.Builder.html +22 -22
  22. package/docs/ProtoBuf.DotProto.Parser.html +7 -7
  23. package/docs/ProtoBuf.DotProto.Tokenizer.html +11 -11
  24. package/docs/ProtoBuf.DotProto.html +2 -2
  25. package/docs/ProtoBuf.Map.html +6 -6
  26. package/docs/ProtoBuf.Reflect.Element.html +104 -8
  27. package/docs/ProtoBuf.Reflect.Enum.Value.html +10 -10
  28. package/docs/ProtoBuf.Reflect.Enum.html +21 -21
  29. package/docs/ProtoBuf.Reflect.Extension.html +3 -3
  30. package/docs/ProtoBuf.Reflect.Message.ExtensionField.html +29 -29
  31. package/docs/ProtoBuf.Reflect.Message.Field.html +28 -28
  32. package/docs/ProtoBuf.Reflect.Message.OneOf.html +10 -10
  33. package/docs/ProtoBuf.Reflect.Message.html +31 -28
  34. package/docs/ProtoBuf.Reflect.Namespace.html +19 -19
  35. package/docs/ProtoBuf.Reflect.Service.Method.html +11 -11
  36. package/docs/ProtoBuf.Reflect.Service.RPCMethod.html +17 -17
  37. package/docs/ProtoBuf.Reflect.Service.html +20 -20
  38. package/docs/ProtoBuf.Reflect.T.html +9 -9
  39. package/docs/ProtoBuf.Reflect.html +2 -2
  40. package/docs/ProtoBuf.Util.html +6 -6
  41. package/docs/ProtoBuf.html +28 -28
  42. package/docs/ProtoBuf.js.html +151 -61
  43. package/docs/index.html +1 -1
  44. package/donate.png +0 -0
  45. package/examples/websocket/server.js +4 -4
  46. package/externs/{ProtoBuf.js → protobuf.js} +25 -14
  47. package/index.js +1 -18
  48. package/jsdoc.json +1 -1
  49. package/package.json +11 -11
  50. package/protobuf.png +0 -0
  51. package/scripts/build.js +5 -10
  52. package/src/ProtoBuf/Builder/Message.js +17 -8
  53. package/src/ProtoBuf/Builder/Service.js +3 -0
  54. package/src/ProtoBuf/Builder.js +34 -10
  55. package/src/ProtoBuf/DotProto/Parser.js +58 -23
  56. package/src/ProtoBuf/Lang.js +2 -2
  57. package/src/ProtoBuf/Reflect/Element.js +17 -4
  58. package/src/ProtoBuf/Reflect/Message/Field.js +6 -5
  59. package/src/ProtoBuf/Reflect/Message.js +9 -4
  60. package/src/{bower.json → bower.json.in} +2 -2
  61. package/src/google/protobuf/descriptor.json +55 -15
  62. package/src/{ProtoBuf.js → protobuf.js} +0 -0
  63. package/src/wrap.js +4 -4
  64. package/tests/complex.json +8 -1
  65. package/tests/imports-weak.proto +7 -0
  66. package/tests/suite.js +107 -82
  67. package/.npmignore +0 -3
  68. package/ProtoBuf.png +0 -0
  69. package/dist/ProtoBuf-light.min.js +0 -87
  70. package/dist/ProtoBuf-light.min.js.gz +0 -0
  71. package/dist/ProtoBuf-light.min.map +0 -8
  72. package/dist/ProtoBuf.min.js +0 -108
  73. package/dist/ProtoBuf.min.js.gz +0 -0
  74. package/dist/ProtoBuf.min.map +0 -8
  75. package/examples/protoify/.npmignore +0 -2
  76. package/examples/websocket/.npmignore +0 -3
  77. package/externs/ByteBuffer.js +0 -767
  78. package/externs/Long.js +0 -328
  79. package/tests/gtfs-realtime.proto +0 -552
  80. package/webpack.config.js +0 -9
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "name": "protobufjs",
3
- "version": "4.1.3",
3
+ "version": "5.0.3",
4
4
  "description": "Protocol Buffers for JavaScript. Finally.",
5
5
  "author": "Daniel Wirtz <dcode@dcode.io>",
6
6
  "contributors": [
7
7
  "Frank Xu <yyfrankyy@gmail.com>"
8
8
  ],
9
- "main": "dist/ProtoBuf.js",
9
+ "main": "./dist/protobuf.js",
10
10
  "bin": {
11
11
  "pbjs": "./bin/pbjs"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "https://github.com/dcodeIO/ProtoBuf.js.git"
15
+ "url": "https://github.com/dcodeIO/protobuf.js.git"
16
16
  },
17
17
  "bugs": {
18
- "url": "https://github.com/dcodeIO/ProtoBuf.js/issues"
18
+ "url": "https://github.com/dcodeIO/protobuf.js/issues"
19
19
  },
20
- "homepage": "https://github.com/dcodeIO/ProtoBuf.js",
20
+ "homepage": "https://github.com/dcodeIO/protobuf.js",
21
21
  "keywords": [
22
22
  "net",
23
23
  "buffer",
@@ -29,8 +29,8 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "ascli": "~1",
32
- "bytebuffer": "~4 >=4.1",
33
- "glob": "^5.0.10",
32
+ "bytebuffer": "~5",
33
+ "glob": "^7.0.5",
34
34
  "yargs": "^3.10.0"
35
35
  },
36
36
  "devDependencies": {
@@ -55,11 +55,11 @@
55
55
  "build": "node scripts/build.js",
56
56
  "descriptor2json": "node bin/pbjs src/google/protobuf/descriptor.proto --target=json > src/google/protobuf/descriptor.json",
57
57
  "compile": "npm run-script compile-full && npm run-script compile-light",
58
- "compile-full": "ccjs dist/ProtoBuf.js --create_source_map=dist/ProtoBuf.min.map --compilation_level=SIMPLE_OPTIMIZATIONS > dist/ProtoBuf.min.js",
59
- "compile-light": "ccjs dist/ProtoBuf-light.js --create_source_map=dist/ProtoBuf-light.min.map --compilation_level=SIMPLE_OPTIMIZATIONS > dist/ProtoBuf-light.min.js",
58
+ "compile-full": "ccjs dist/protobuf.js --create_source_map=dist/protobuf.min.map --compilation_level=SIMPLE_OPTIMIZATIONS > dist/protobuf.min.js",
59
+ "compile-light": "ccjs dist/protobuf-light.js --create_source_map=dist/protobuf-light.min.map --compilation_level=SIMPLE_OPTIMIZATIONS > dist/protobuf-light.min.js",
60
60
  "compress": "npm run-script compress-full && npm run-script compress-light",
61
- "compress-full": "gzip -c -9 dist/ProtoBuf.min.js > dist/ProtoBuf.min.js.gz",
62
- "compress-light": "gzip -c -9 dist/ProtoBuf-light.min.js > dist/ProtoBuf-light.min.js.gz",
61
+ "compress-full": "gzip -c -9 dist/protobuf.min.js > dist/protobuf.min.js.gz",
62
+ "compress-light": "gzip -c -9 dist/protobuf-light.min.js > dist/protobuf-light.min.js.gz",
63
63
  "jsdoc": "node node_modules/jsdoc/jsdoc.js -c jsdoc.json"
64
64
  }
65
65
  }
package/protobuf.png ADDED
Binary file
package/scripts/build.js CHANGED
@@ -14,11 +14,6 @@
14
14
  limitations under the License.
15
15
  */
16
16
 
17
- /**
18
- * @overview ProtoBuf.js Build Script (c) 2014 Daniel Wirtz <dcode@dcode.io>
19
- * Released under the Apache License, Version 2.0
20
- * see: https://github.com/dcodeIO/ProtoBuf.js for details
21
- */
22
17
  var MetaScript = require("metascript"),
23
18
  path = require("path"),
24
19
  fs = require("fs");
@@ -35,17 +30,17 @@ var scope = {
35
30
  };
36
31
 
37
32
  // Make full build
38
- console.log("Building ProtoBuf.js with scope", JSON.stringify(scope, null, 2));
33
+ console.log("Building protobuf.js with scope", JSON.stringify(scope, null, 2));
39
34
  fs.writeFileSync(
40
- path.join(distDir, "ProtoBuf.js"),
35
+ path.join(distDir, "protobuf.js"),
41
36
  MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "wrap.js")), filename, scope, srcDir)
42
37
  );
43
38
 
44
39
  // Make light build
45
40
  scope.DOTPROTO = false;
46
- console.log("Building ProtoBuf-light.js with scope", JSON.stringify(scope, null, 2));
41
+ console.log("Building protobuf-light.js with scope", JSON.stringify(scope, null, 2));
47
42
  fs.writeFileSync(
48
- path.join(distDir, "ProtoBuf-light.js"),
43
+ path.join(distDir, "protobuf-light.js"),
49
44
  MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "wrap.js")), filename, scope, srcDir)
50
45
  );
51
46
 
@@ -54,5 +49,5 @@ scope = { VERSION: pkg.version };
54
49
  console.log("Updating bower.json with scope", JSON.stringify(scope, null, 2));
55
50
  fs.writeFileSync(
56
51
  path.join(rootDir, "bower.json"),
57
- MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "bower.json")), filename, scope, srcDir)
52
+ MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "bower.json.in")), filename, scope, srcDir)
58
53
  );
@@ -110,9 +110,11 @@ MessagePrototype.$add = MessagePrototype.add;
110
110
  MessagePrototype.set = function(keyOrObj, value, noAssert) {
111
111
  if (keyOrObj && typeof keyOrObj === 'object') {
112
112
  noAssert = value;
113
- for (var ikey in keyOrObj)
114
- if (keyOrObj.hasOwnProperty(ikey) && typeof (value = keyOrObj[ikey]) !== 'undefined')
113
+ for (var ikey in keyOrObj) {
114
+ // Check if virtual oneof field - don't set these
115
+ if (keyOrObj.hasOwnProperty(ikey) && typeof (value = keyOrObj[ikey]) !== 'undefined' && T._oneofsByName[ikey] === undefined)
115
116
  this.$set(ikey, value, noAssert);
117
+ }
116
118
  return this;
117
119
  }
118
120
  var field = T._fieldsByName[keyOrObj];
@@ -340,18 +342,19 @@ MessagePrototype.calculate = function() {
340
342
  * @name ProtoBuf.Builder.Message#encodeDelimited
341
343
  * @function
342
344
  * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
345
+ * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
343
346
  * @return {!ByteBuffer} Encoded message as a ByteBuffer
344
347
  * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
345
348
  * returns the encoded ByteBuffer in the `encoded` property on the error.
346
349
  * @expose
347
350
  */
348
- MessagePrototype.encodeDelimited = function(buffer) {
351
+ MessagePrototype.encodeDelimited = function(buffer, noVerify) {
349
352
  var isNew = false;
350
353
  if (!buffer)
351
354
  buffer = new ByteBuffer(),
352
355
  isNew = true;
353
356
  var enc = new ByteBuffer().LE();
354
- T.encode(this, enc).flip();
357
+ T.encode(this, enc, noVerify).flip();
355
358
  buffer.writeVarint32(enc.remaining());
356
359
  buffer.append(enc);
357
360
  return isNew ? buffer.flip() : buffer;
@@ -560,6 +563,7 @@ MessagePrototype.encodeJSON = function() {
560
563
  * @name ProtoBuf.Builder.Message.decode
561
564
  * @function
562
565
  * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
566
+ * @param {(number|string)=} length Message length. Defaults to decode all the remainig data.
563
567
  * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
564
568
  * @return {!ProtoBuf.Builder.Message} Decoded message
565
569
  * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
@@ -568,13 +572,17 @@ MessagePrototype.encodeJSON = function() {
568
572
  * @see ProtoBuf.Builder.Message.decode64
569
573
  * @see ProtoBuf.Builder.Message.decodeHex
570
574
  */
571
- Message.decode = function(buffer, enc) {
575
+ Message.decode = function(buffer, length, enc) {
576
+ if (typeof length === 'string')
577
+ enc = length,
578
+ length = -1;
572
579
  if (typeof buffer === 'string')
573
580
  buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
574
- buffer = ByteBuffer.isByteBuffer(buffer) ? buffer : ByteBuffer.wrap(buffer); // May throw
581
+ else if (!ByteBuffer.isByteBuffer(buffer))
582
+ buffer = ByteBuffer.wrap(buffer); // May throw
575
583
  var le = buffer.littleEndian;
576
584
  try {
577
- var msg = T.decode(buffer.LE());
585
+ var msg = T.decode(buffer.LE(), length);
578
586
  buffer.LE(le);
579
587
  return msg;
580
588
  } catch (e) {
@@ -597,7 +605,8 @@ Message.decode = function(buffer, enc) {
597
605
  Message.decodeDelimited = function(buffer, enc) {
598
606
  if (typeof buffer === 'string')
599
607
  buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
600
- buffer = ByteBuffer.isByteBuffer(buffer) ? buffer : ByteBuffer.wrap(buffer); // May throw
608
+ else if (!ByteBuffer.isByteBuffer(buffer))
609
+ buffer = ByteBuffer.wrap(buffer); // May throw
601
610
  if (buffer.remaining() < 1)
602
611
  return null;
603
612
  var off = buffer.offset,
@@ -77,6 +77,9 @@ for (var i=0; i<rpc.length; i++) {
77
77
  callback(err);
78
78
  return;
79
79
  }
80
+ // Coalesce to empty string when service response has empty content
81
+ if (res === null)
82
+ res = ''
80
83
  try { res = method.resolvedResponseType.clazz.decode(res); } catch (notABuffer) {}
81
84
  if (!res || !(res instanceof method.resolvedResponseType.clazz)) {
82
85
  callback(Error("Illegal response type received in service method "+ T.name+"#"+method.name));
@@ -243,13 +243,12 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
243
243
  subObj.push(svc);
244
244
  });
245
245
 
246
- // Set extension range
246
+ // Set extension ranges
247
247
  if (def["extensions"]) {
248
- obj.extensions = def["extensions"];
249
- if (obj.extensions[0] < ProtoBuf.ID_MIN)
250
- obj.extensions[0] = ProtoBuf.ID_MIN;
251
- if (obj.extensions[1] > ProtoBuf.ID_MAX)
252
- obj.extensions[1] = ProtoBuf.ID_MAX;
248
+ if (typeof def["extensions"][0] === 'number') // pre 5.0.1
249
+ obj.extensions = [ def["extensions"] ];
250
+ else
251
+ obj.extensions = def["extensions"];
253
252
  }
254
253
 
255
254
  // Create on top of current namespace
@@ -288,8 +287,16 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
288
287
  def["fields"].forEach(function(fld) {
289
288
  if (obj.getChild(fld['id']|0) !== null)
290
289
  throw Error("duplicate extended field id in "+obj.name+": "+fld['id']);
291
- if (fld['id'] < obj.extensions[0] || fld['id'] > obj.extensions[1])
292
- throw Error("illegal extended field id in "+obj.name+": "+fld['id']+" ("+obj.extensions.join(' to ')+" expected)");
290
+ // Check if field id is allowed to be extended
291
+ if (obj.extensions) {
292
+ var valid = false;
293
+ obj.extensions.forEach(function(range) {
294
+ if (fld["id"] >= range[0] && fld["id"] <= range[1])
295
+ valid = true;
296
+ });
297
+ if (!valid)
298
+ throw Error("illegal extended field id in "+obj.name+": "+fld['id']+" (not within valid ranges)");
299
+ }
293
300
  // Convert extension field names to camel case notation if the override is set
294
301
  var name = fld["name"];
295
302
  if (this.options['convertFieldsToCamelCase'])
@@ -370,7 +377,11 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
370
377
  root = require("path")['resolve'](root);
371
378
  if (root.indexOf("\\") >= 0 || filename.file.indexOf("\\") >= 0)
372
379
  delim = '\\';
373
- var fname = root + delim + filename.file;
380
+ var fname;
381
+ if (ProtoBuf.Util.IS_NODE)
382
+ fname = require("path")['join'](root, filename.file);
383
+ else
384
+ fname = root + delim + filename.file;
374
385
  if (this.files[fname] === true)
375
386
  return this.reset();
376
387
  this.files[fname] = true;
@@ -416,7 +427,10 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
416
427
  var importFilename = json['imports'][i];
417
428
  if (importFilename === "google/protobuf/descriptor.proto")
418
429
  continue; // Not needed and therefore not used
419
- importFilename = importRoot + delim + importFilename;
430
+ if (ProtoBuf.Util.IS_NODE)
431
+ importFilename = require("path")['join'](importRoot, importFilename);
432
+ else
433
+ importFilename = importRoot + delim + importFilename;
420
434
  if (this.files[importFilename] === true)
421
435
  continue; // Already imported
422
436
  if (/\.proto$/i.test(importFilename) && !ProtoBuf.DotProto) // If this is a light build
@@ -514,6 +528,16 @@ ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
514
528
  this.ptr.keyType = ProtoBuf.TYPES[this.ptr.keyType];
515
529
  }
516
530
 
531
+ // If it's a repeated and packable field then proto3 mandates it should be packed by
532
+ // default
533
+ if (
534
+ this.ptr.syntax === 'proto3' &&
535
+ this.ptr.repeated && this.ptr.options.packed === undefined &&
536
+ ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.ptr.type.wireType) !== -1
537
+ ) {
538
+ this.ptr.options.packed = true;
539
+ }
540
+
517
541
  } else if (this.ptr instanceof ProtoBuf.Reflect.Service.Method) {
518
542
 
519
543
  if (this.ptr instanceof ProtoBuf.Reflect.Service.RPCMethod) {
@@ -50,7 +50,8 @@ ParserPrototype.parse = function() {
50
50
  // "syntax": undefined
51
51
  };
52
52
  var token,
53
- head = true;
53
+ head = true,
54
+ weak;
54
55
  try {
55
56
  while (token = this.tn.next()) {
56
57
  switch (token) {
@@ -67,11 +68,12 @@ ParserPrototype.parse = function() {
67
68
  if (!head)
68
69
  throw Error("unexpected 'import'");
69
70
  token = this.tn.peek();
70
- if (token === "public") // ignored
71
+ if (token === "public" || (weak = token === "weak")) // token ignored
71
72
  this.tn.next();
72
73
  token = this._readString();
73
74
  this.tn.skip(";");
74
- topLevel["imports"].push(token);
75
+ if (!weak) // import ignored
76
+ topLevel["imports"].push(token);
75
77
  break;
76
78
  case 'syntax':
77
79
  if (!head)
@@ -396,6 +398,7 @@ ParserPrototype._parseMessage = function(parent, fld) {
396
398
  "enums": [],
397
399
  "messages": [],
398
400
  "options": {},
401
+ "services": [],
399
402
  "oneofs": {}
400
403
  // "extensions": undefined
401
404
  };
@@ -422,8 +425,16 @@ ParserPrototype._parseMessage = function(parent, fld) {
422
425
  this._parseMessage(msg);
423
426
  else if (token === "option")
424
427
  this._parseOption(msg);
428
+ else if (token === "service")
429
+ this._parseService(msg);
425
430
  else if (token === "extensions")
426
- this._parseExtensions(msg);
431
+ if (msg.hasOwnProperty("extensions")) {
432
+ msg["extensions"] = msg["extensions"].concat(this._parseExtensionRanges())
433
+ } else {
434
+ msg["extensions"] = this._parseExtensionRanges();
435
+ }
436
+ else if (token === "reserved")
437
+ this._parseIgnored(); // TODO
427
438
  else if (token === "extend")
428
439
  this._parseExtend(msg);
429
440
  else if (Lang.TYPEREF.test(token)) {
@@ -438,6 +449,16 @@ ParserPrototype._parseMessage = function(parent, fld) {
438
449
  return msg;
439
450
  };
440
451
 
452
+ /**
453
+ * Parses an ignored statement.
454
+ * @private
455
+ */
456
+ ParserPrototype._parseIgnored = function() {
457
+ while (this.tn.peek() !== ';')
458
+ this.tn.next();
459
+ this.tn.skip(";");
460
+ };
461
+
441
462
  /**
442
463
  * Parses a message field.
443
464
  * @param {!Object} msg Message definition
@@ -600,29 +621,43 @@ ParserPrototype._parseEnum = function(msg) {
600
621
  };
601
622
 
602
623
  /**
603
- * Parses an extensions statement.
604
- * @param {!Object} msg Message object
624
+ * Parses extension / reserved ranges.
625
+ * @returns {!Array.<!Array.<number>>}
605
626
  * @private
606
627
  */
607
- ParserPrototype._parseExtensions = function(msg) {
608
- var token = this.tn.next(),
628
+ ParserPrototype._parseExtensionRanges = function() {
629
+ var ranges = [];
630
+ var token,
631
+ range,
632
+ value;
633
+ do {
609
634
  range = [];
610
- if (token === "min")
611
- range.push(ProtoBuf.ID_MIN);
612
- else if (token === "max")
613
- range.push(ProtoBuf.ID_MAX);
614
- else
615
- range.push(mkNumber(token));
616
- this.tn.skip("to");
617
- token = this.tn.next();
618
- if (token === "min")
619
- range.push(ProtoBuf.ID_MIN);
620
- else if (token === "max")
621
- range.push(ProtoBuf.ID_MAX);
622
- else
623
- range.push(mkNumber(token));
635
+ while (true) {
636
+ token = this.tn.next();
637
+ switch (token) {
638
+ case "min":
639
+ value = ProtoBuf.ID_MIN;
640
+ break;
641
+ case "max":
642
+ value = ProtoBuf.ID_MAX;
643
+ break;
644
+ default:
645
+ value = mkNumber(token);
646
+ break;
647
+ }
648
+ range.push(value);
649
+ if (range.length === 2)
650
+ break;
651
+ if (this.tn.peek() !== "to") {
652
+ range.push(value);
653
+ break;
654
+ }
655
+ this.tn.next();
656
+ }
657
+ ranges.push(range);
658
+ } while (this.tn.omit(","));
624
659
  this.tn.skip(";");
625
- msg["extensions"] = range;
660
+ return ranges;
626
661
  };
627
662
 
628
663
  /**
@@ -21,10 +21,10 @@ ProtoBuf.Lang = {
21
21
  TYPEDEF: /^[a-zA-Z][a-zA-Z_0-9]*$/,
22
22
 
23
23
  // Type references
24
- TYPEREF: /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)+$/,
24
+ TYPEREF: /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*$/,
25
25
 
26
26
  // Fully qualified type references
27
- FQTYPEREF: /^(?:\.[a-zA-Z][a-zA-Z_0-9]*)+$/,
27
+ FQTYPEREF: /^(?:\.[a-zA-Z_][a-zA-Z_0-9]*)+$/,
28
28
 
29
29
  // All numbers
30
30
  NUMBER: /^-?(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+|([0-9]*(\.[0-9]*)?([Ee][+-]?[0-9]+)?)|inf|nan)$/,
@@ -15,9 +15,11 @@
15
15
  * converted to string form if so.
16
16
  * @param {string} syntax Syntax level of defining message type, e.g.,
17
17
  * proto2 or proto3.
18
+ * @param {string} name Name of the field containing this element (for error
19
+ * messages)
18
20
  * @constructor
19
21
  */
20
- var Element = function(type, resolvedType, isMapKey, syntax) {
22
+ var Element = function(type, resolvedType, isMapKey, syntax, name) {
21
23
 
22
24
  /**
23
25
  * Element type, as a string (e.g., int32).
@@ -43,6 +45,12 @@ var Element = function(type, resolvedType, isMapKey, syntax) {
43
45
  */
44
46
  this.syntax = syntax;
45
47
 
48
+ /**
49
+ * Name of the field containing this element (for error messages)
50
+ * @type {string}
51
+ */
52
+ this.name = name;
53
+
46
54
  if (isMapKey && ProtoBuf.MAP_KEY_TYPES.indexOf(type) < 0)
47
55
  throw Error("Invalid map key type: " + type.name);
48
56
  };
@@ -93,6 +101,10 @@ function mkLong(value, unsigned) {
93
101
  throw Error("not convertible to Long");
94
102
  }
95
103
 
104
+ ElementPrototype.toString = function() {
105
+ return (this.name || '') + (this.isMapKey ? 'map' : 'value') + ' element';
106
+ }
107
+
96
108
  /**
97
109
  * Checks if the given value can be set for an element of this type (singular
98
110
  * field or one element of a repeated field or map).
@@ -102,9 +114,10 @@ function mkLong(value, unsigned) {
102
114
  * @expose
103
115
  */
104
116
  ElementPrototype.verifyValue = function(value) {
105
- var fail = function(val, msg) {
106
- throw Error("Illegal value for "+this.toString(true)+" of type "+this.type.name+": "+val+" ("+msg+")");
107
- }.bind(this);
117
+ var self = this;
118
+ function fail(val, msg) {
119
+ throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
120
+ }
108
121
  switch (this.type) {
109
122
  // Signed 32bit
110
123
  case ProtoBuf.TYPES["int32"]:
@@ -141,9 +141,9 @@ var FieldPrototype = Field.prototype = Object.create(T.prototype);
141
141
  * @expose
142
142
  */
143
143
  FieldPrototype.build = function() {
144
- this.element = new Element(this.type, this.resolvedType, false, this.syntax);
144
+ this.element = new Element(this.type, this.resolvedType, false, this.syntax, this.name);
145
145
  if (this.map)
146
- this.keyElement = new Element(this.keyType, undefined, true, this.syntax);
146
+ this.keyElement = new Element(this.keyType, undefined, true, this.syntax, this.name);
147
147
 
148
148
  // In proto3, fields do not have field presence, and every field is set to
149
149
  // its type's default value ("", 0, 0.0, or false).
@@ -165,9 +165,10 @@ FieldPrototype.build = function() {
165
165
  */
166
166
  FieldPrototype.verifyValue = function(value, skipRepeated) {
167
167
  skipRepeated = skipRepeated || false;
168
- var fail = function(val, msg) {
169
- throw Error("Illegal value for "+this.toString(true)+" of type "+this.type.name+": "+val+" ("+msg+")");
170
- }.bind(this);
168
+ var self = this;
169
+ function fail(val, msg) {
170
+ throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
171
+ }
171
172
  if (value === null) { // NULL values for optional fields
172
173
  if (this.required)
173
174
  fail(typeof value, "required");
@@ -20,10 +20,10 @@ var Message = function(builder, parent, name, options, isGroup, syntax) {
20
20
 
21
21
  /**
22
22
  * Extensions range.
23
- * @type {!Array.<number>}
23
+ * @type {!Array.<number>|undefined}
24
24
  * @expose
25
25
  */
26
- this.extensions = [ProtoBuf.ID_MIN, ProtoBuf.ID_MAX];
26
+ this.extensions = undefined;
27
27
 
28
28
  /**
29
29
  * Runtime message class.
@@ -94,6 +94,7 @@ MessagePrototype.build = function(rebuild) {
94
94
  this._fields = [];
95
95
  this._fieldsById = {};
96
96
  this._fieldsByName = {};
97
+ this._oneofsByName = {};
97
98
  for (var i=0, k=this.children.length, child; i<k; i++) {
98
99
  child = this.children[i];
99
100
  if (child instanceof Enum || child instanceof Message || child instanceof Service) {
@@ -105,6 +106,9 @@ MessagePrototype.build = function(rebuild) {
105
106
  this._fields.push(child),
106
107
  this._fieldsById[child.id] = child,
107
108
  this._fieldsByName[child.name] = child;
109
+ else if (child instanceof Message.OneOf) {
110
+ this._oneofsByName[child.name] = child;
111
+ }
108
112
  else if (!(child instanceof Message.OneOf) && !(child instanceof Extension)) // Not built
109
113
  throw Error("Illegal reflect child of "+this.toString(true)+": "+this.children[i].toString(true));
110
114
  }
@@ -204,14 +208,15 @@ function skipTillGroupEnd(expectedId, buf) {
204
208
  /**
205
209
  * Decodes an encoded message and returns the decoded message.
206
210
  * @param {ByteBuffer} buffer ByteBuffer to decode from
207
- * @param {number=} length Message length. Defaults to decode all the available data.
211
+ * @param {number=} length Message length. Defaults to decode all remaining data.
208
212
  * @param {number=} expectedGroupEndId Expected GROUPEND id if this is a legacy group
209
213
  * @return {ProtoBuf.Builder.Message} Decoded message
210
214
  * @throws {Error} If the message cannot be decoded
211
215
  * @expose
212
216
  */
213
217
  MessagePrototype.decode = function(buffer, length, expectedGroupEndId) {
214
- length = typeof length === 'number' ? length : -1;
218
+ if (typeof length !== 'number')
219
+ length = -1;
215
220
  var start = buffer.offset,
216
221
  msg = new (this.clazz)(),
217
222
  tag, wireType, id, field;
@@ -2,11 +2,11 @@
2
2
  "name": "protobuf",
3
3
  "description": "Protocol Buffers for JavaScript. Finally.",
4
4
  "version": /*?== VERSION */,
5
- "main": "dist/ProtoBuf.js",
5
+ "main": "dist/protobuf.js",
6
6
  "license": "Apache-2.0",
7
7
  "homepage": "http://dcode.io/",
8
8
  "dependencies": {
9
- "bytebuffer": "~3"
9
+ "bytebuffer": "~5"
10
10
  },
11
11
  "keywords": ["net", "buffer", "protobuf", "serialization", "bytebuffer", "websocket", "webrtc"],
12
12
  "ignore": [