protobufjs 8.7.2 → 8.8.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.
package/src/object.js CHANGED
@@ -10,10 +10,11 @@ var Root; // cyclic
10
10
 
11
11
  /* eslint-disable no-warning-comments */
12
12
  // TODO: Replace with embedded proto.
13
- var editions2024Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY", enforce_naming_style: "STYLE2024", default_symbol_visibility: "EXPORT_TOP_LEVEL" };
14
- var editions2023Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY", enforce_naming_style: "STYLE_LEGACY", default_symbol_visibility: "EXPORT_ALL" };
15
13
  var proto2Defaults = {enum_type: "CLOSED", field_presence: "EXPLICIT", json_format: "LEGACY_BEST_EFFORT", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "EXPANDED", utf8_validation: "NONE", enforce_naming_style: "STYLE_LEGACY", default_symbol_visibility: "EXPORT_ALL" };
16
14
  var proto3Defaults = {enum_type: "OPEN", field_presence: "IMPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY", enforce_naming_style: "STYLE_LEGACY", default_symbol_visibility: "EXPORT_ALL" };
15
+ var editions2023Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY", enforce_naming_style: "STYLE_LEGACY", default_symbol_visibility: "EXPORT_ALL" };
16
+ var editions2024Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY", enforce_naming_style: "STYLE2024", default_symbol_visibility: "EXPORT_TOP_LEVEL" };
17
+ var editions2026Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY", enforce_naming_style: "STYLE2026", default_symbol_visibility: "STRICT", enforce_proto_limits: "PROTO_LIMITS2026" };
17
18
 
18
19
  /**
19
20
  * Constructs a new reflection object instance.
@@ -227,6 +228,8 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition)
227
228
  defaults = Object.assign({}, editions2023Defaults);
228
229
  } else if (edition === "2024") {
229
230
  defaults = Object.assign({}, editions2024Defaults);
231
+ } else if (edition === "2026") {
232
+ defaults = Object.assign({}, editions2026Defaults);
230
233
  } else {
231
234
  throw new Error("Unknown edition: " + edition);
232
235
  }
package/src/parse.js CHANGED
@@ -308,7 +308,7 @@ function parse(source, root, options) {
308
308
  function parseEdition() {
309
309
  skip("=");
310
310
  edition = readString();
311
- const supportedEditions = ["2023", "2024"];
311
+ const supportedEditions = ["2023", "2024", "2026"];
312
312
 
313
313
  /* istanbul ignore if */
314
314
  if (!supportedEditions.includes(edition))
@@ -342,6 +342,7 @@ function parse(source, root, options) {
342
342
  if (edition < "2024") {
343
343
  return false;
344
344
  }
345
+ var visibility = token;
345
346
  token = next();
346
347
  if (token === "export" || token === "local") {
347
348
  return false;
@@ -349,9 +350,11 @@ function parse(source, root, options) {
349
350
  if (token !== "message" && token !== "enum") {
350
351
  return false;
351
352
  }
352
- /* eslint-disable no-warning-comments */
353
- // TODO: actually enforce visiblity modifiers like protoc does.
354
- return parseCommon(parent, token, depth);
353
+ (token === "message"
354
+ ? parseType(parent, token, depth + 1)
355
+ : parseEnum(parent, token)
356
+ ).visibility = visibility;
357
+ return true;
355
358
 
356
359
  case "service":
357
360
  parseService(parent, token, depth + 1);
@@ -456,6 +459,7 @@ function parse(source, root, options) {
456
459
  if (parent === ptr) {
457
460
  topLevelObjects.push(type);
458
461
  }
462
+ return type;
459
463
  }
460
464
 
461
465
  function parseField(parent, rule, extend, depth) {
@@ -720,6 +724,7 @@ function parse(source, root, options) {
720
724
  if (parent === ptr) {
721
725
  topLevelObjects.push(enm);
722
726
  }
727
+ return enm;
723
728
  }
724
729
 
725
730
  function parseEnumValue(token) {
package/src/reader.js CHANGED
@@ -94,12 +94,32 @@ Reader.prototype.raw = function read_raw(start, end) {
94
94
  return this.buf.subarray(start, end);
95
95
  };
96
96
 
97
+ function readVarint32NearEnd(reader) {
98
+ // Safely read up to four bytes of a varint32 near the reader limit
99
+ var value = 0;
100
+ for (var i = 0; i < 4; ++i) {
101
+ if (reader.pos >= reader.len)
102
+ throw indexOutOfRange(reader);
103
+ var b = reader.buf[reader.pos++];
104
+ value = (value | (b & 127) << i * 7) >>> 0;
105
+ if (b < 128)
106
+ return value;
107
+ }
108
+ throw indexOutOfRange(reader);
109
+ }
110
+
97
111
  /**
98
112
  * Reads a varint as an unsigned 32 bit value.
99
113
  * @function
100
114
  * @returns {number} Value read
101
115
  */
102
116
  Reader.prototype.uint32 = function read_uint32() {
117
+ if (this.len - this.pos < 5) {
118
+ if (this.pos >= this.len)
119
+ throw indexOutOfRange(this);
120
+ if (this.buf[this.pos] >= 128)
121
+ return readVarint32NearEnd(this);
122
+ }
103
123
  var buf = this.buf,
104
124
  pos = this.pos,
105
125
  value = (buf[pos] & 127) >>> 0;
@@ -150,6 +170,12 @@ Reader.prototype.uint32 = function read_uint32() {
150
170
  * @returns {number} Tag read
151
171
  */
152
172
  Reader.prototype.tag = function read_tag() {
173
+ if (this.len - this.pos < 5) {
174
+ if (this.pos >= this.len)
175
+ throw indexOutOfRange(this);
176
+ if (this.buf[this.pos] >= 128)
177
+ return readVarint32NearEnd(this);
178
+ }
153
179
  var buf = this.buf,
154
180
  pos = this.pos,
155
181
  value = (buf[pos] & 127) >>> 0;
@@ -182,6 +208,45 @@ Reader.prototype.tag = function read_tag() {
182
208
  throw Error("invalid tag encoding");
183
209
  };
184
210
 
211
+ /**
212
+ * Reads a varint length prefix within signed 32 bit range.
213
+ * @returns {number} Length read
214
+ */
215
+ Reader.prototype.size = function read_size() {
216
+ if (this.len - this.pos < 5) {
217
+ if (this.pos >= this.len)
218
+ throw indexOutOfRange(this);
219
+ if (this.buf[this.pos] >= 128)
220
+ return readVarint32NearEnd(this);
221
+ }
222
+ var buf = this.buf,
223
+ pos = this.pos,
224
+ value = (buf[pos] & 127) >>> 0;
225
+ if (buf[pos++] < 128) {
226
+ this.pos = pos;
227
+ return value;
228
+ }
229
+ value = (value | (buf[pos] & 127) << 7) >>> 0;
230
+ if (buf[pos++] < 128) {
231
+ this.pos = pos;
232
+ return value;
233
+ }
234
+ value = (value | (buf[pos] & 127) << 14) >>> 0;
235
+ if (buf[pos++] < 128) {
236
+ this.pos = pos;
237
+ return value;
238
+ }
239
+ value = (value | (buf[pos] & 127) << 21) >>> 0;
240
+ if (buf[pos++] < 128) {
241
+ this.pos = pos;
242
+ return value;
243
+ }
244
+ this.pos = pos + 1;
245
+ if (buf[pos] > 7)
246
+ throw Error("invalid length encoding");
247
+ return (value | buf[pos] << 28) >>> 0;
248
+ };
249
+
185
250
  /**
186
251
  * Reads a varint as a signed 32 bit value.
187
252
  * @returns {number} Value read
@@ -395,7 +460,9 @@ Reader.prototype.double = function read_double() {
395
460
  */
396
461
  Reader.prototype.uint32s = function read_uint32s(array) {
397
462
  if (array === undefined) array = [];
398
- var end = this.uint32() + this.pos, buf = this.buf, pos = this.pos, value;
463
+ var end = this.size() + this.pos, len = this.len, buf = this.buf, pos = this.pos, value;
464
+ if (end > len) throw indexOutOfRange(this, end - this.pos);
465
+ this.len = end;
399
466
  while (pos < end) {
400
467
  value = buf[pos++];
401
468
  if (value < 128)
@@ -407,6 +474,8 @@ Reader.prototype.uint32s = function read_uint32s(array) {
407
474
  }
408
475
  }
409
476
  this.pos = pos;
477
+ if (pos !== end) throw RangeError("index out of range");
478
+ this.len = len;
410
479
  return array;
411
480
  };
412
481
 
@@ -417,7 +486,9 @@ Reader.prototype.uint32s = function read_uint32s(array) {
417
486
  */
418
487
  Reader.prototype.int32s = function read_int32s(array) {
419
488
  if (array === undefined) array = [];
420
- var end = this.uint32() + this.pos, buf = this.buf, pos = this.pos, value;
489
+ var end = this.size() + this.pos, len = this.len, buf = this.buf, pos = this.pos, value;
490
+ if (end > len) throw indexOutOfRange(this, end - this.pos);
491
+ this.len = end;
421
492
  while (pos < end) {
422
493
  value = buf[pos++];
423
494
  if (value < 128)
@@ -429,6 +500,8 @@ Reader.prototype.int32s = function read_int32s(array) {
429
500
  }
430
501
  }
431
502
  this.pos = pos;
503
+ if (pos !== end) throw RangeError("index out of range");
504
+ this.len = len;
432
505
  return array;
433
506
  };
434
507
 
@@ -439,9 +512,13 @@ Reader.prototype.int32s = function read_int32s(array) {
439
512
  */
440
513
  Reader.prototype.sint32s = function read_sint32s(array) {
441
514
  if (array === undefined) array = [];
442
- var end = this.uint32() + this.pos;
515
+ var end = this.size() + this.pos, len = this.len;
516
+ if (end > len) throw indexOutOfRange(this, end - this.pos);
517
+ this.len = end;
443
518
  while (this.pos < end)
444
519
  array.push(this.sint32());
520
+ if (this.pos !== end) throw RangeError("index out of range");
521
+ this.len = len;
445
522
  return array;
446
523
  };
447
524
 
@@ -452,7 +529,9 @@ Reader.prototype.sint32s = function read_sint32s(array) {
452
529
  */
453
530
  Reader.prototype.bools = function read_bools(array) {
454
531
  if (array === undefined) array = [];
455
- var end = this.uint32() + this.pos, buf = this.buf, pos = this.pos, value;
532
+ var end = this.size() + this.pos, len = this.len, buf = this.buf, pos = this.pos, value;
533
+ if (end > len) throw indexOutOfRange(this, end - this.pos);
534
+ this.len = end;
456
535
  while (pos < end) {
457
536
  value = buf[pos++];
458
537
  if (value < 128)
@@ -464,6 +543,8 @@ Reader.prototype.bools = function read_bools(array) {
464
543
  }
465
544
  }
466
545
  this.pos = pos;
546
+ if (pos !== end) throw RangeError("index out of range");
547
+ this.len = len;
467
548
  return array;
468
549
  };
469
550
 
@@ -486,7 +567,7 @@ function getLazyView(reader, count, threshold) {
486
567
  */
487
568
  Reader.prototype.fixed32s = function read_fixed32s(array) {
488
569
  if (array === undefined) array = [];
489
- var len = this.uint32(), end = this.pos + len;
570
+ var len = this.size(), end = this.pos + len;
490
571
  /* istanbul ignore if */
491
572
  if (end > this.len) throw indexOutOfRange(this, len);
492
573
  var count = len >>> 2, i = array.length, pos = this.pos;
@@ -510,7 +591,7 @@ Reader.prototype.fixed32s = function read_fixed32s(array) {
510
591
  */
511
592
  Reader.prototype.sfixed32s = function read_sfixed32s(array) {
512
593
  if (array === undefined) array = [];
513
- var len = this.uint32(), end = this.pos + len;
594
+ var len = this.size(), end = this.pos + len;
514
595
  /* istanbul ignore if */
515
596
  if (end > this.len) throw indexOutOfRange(this, len);
516
597
  var count = len >>> 2, i = array.length, pos = this.pos;
@@ -534,7 +615,7 @@ Reader.prototype.sfixed32s = function read_sfixed32s(array) {
534
615
  */
535
616
  Reader.prototype.floats = function read_floats(array) {
536
617
  if (array === undefined) array = [];
537
- var len = this.uint32(), end = this.pos + len;
618
+ var len = this.size(), end = this.pos + len;
538
619
  /* istanbul ignore if */
539
620
  if (end > this.len) throw indexOutOfRange(this, len);
540
621
  var count = len >>> 2, i = array.length, pos = this.pos;
@@ -558,7 +639,7 @@ Reader.prototype.floats = function read_floats(array) {
558
639
  */
559
640
  Reader.prototype.doubles = function read_doubles(array) {
560
641
  if (array === undefined) array = [];
561
- var len = this.uint32(), end = this.pos + len;
642
+ var len = this.size(), end = this.pos + len;
562
643
  /* istanbul ignore if */
563
644
  if (end > this.len) throw indexOutOfRange(this, len);
564
645
  var count = len >>> 3, i = array.length, pos = this.pos;
@@ -582,9 +663,13 @@ Reader.prototype.doubles = function read_doubles(array) {
582
663
  */
583
664
  Reader.prototype.uint64s = function read_uint64s(array) {
584
665
  if (array === undefined) array = [];
585
- var end = this.uint32() + this.pos;
666
+ var end = this.size() + this.pos, len = this.len;
667
+ if (end > len) throw indexOutOfRange(this, end - this.pos);
668
+ this.len = end;
586
669
  while (this.pos < end)
587
670
  array.push(this.uint64());
671
+ if (this.pos !== end) throw RangeError("index out of range");
672
+ this.len = len;
588
673
  return array;
589
674
  };
590
675
 
@@ -595,9 +680,13 @@ Reader.prototype.uint64s = function read_uint64s(array) {
595
680
  */
596
681
  Reader.prototype.int64s = function read_int64s(array) {
597
682
  if (array === undefined) array = [];
598
- var end = this.uint32() + this.pos;
683
+ var end = this.size() + this.pos, len = this.len;
684
+ if (end > len) throw indexOutOfRange(this, end - this.pos);
685
+ this.len = end;
599
686
  while (this.pos < end)
600
687
  array.push(this.int64());
688
+ if (this.pos !== end) throw RangeError("index out of range");
689
+ this.len = len;
601
690
  return array;
602
691
  };
603
692
 
@@ -608,9 +697,13 @@ Reader.prototype.int64s = function read_int64s(array) {
608
697
  */
609
698
  Reader.prototype.sint64s = function read_sint64s(array) {
610
699
  if (array === undefined) array = [];
611
- var end = this.uint32() + this.pos;
700
+ var end = this.size() + this.pos, len = this.len;
701
+ if (end > len) throw indexOutOfRange(this, end - this.pos);
702
+ this.len = end;
612
703
  while (this.pos < end)
613
704
  array.push(this.sint64());
705
+ if (this.pos !== end) throw RangeError("index out of range");
706
+ this.len = len;
614
707
  return array;
615
708
  };
616
709
 
@@ -621,7 +714,7 @@ Reader.prototype.sint64s = function read_sint64s(array) {
621
714
  */
622
715
  Reader.prototype.fixed64s = function read_fixed64s(array) {
623
716
  if (array === undefined) array = [];
624
- var len = this.uint32(), end = this.pos + len, i = array.length;
717
+ var len = this.size(), end = this.pos + len, i = array.length;
625
718
  /* istanbul ignore if */
626
719
  if (end > this.len) throw indexOutOfRange(this, len);
627
720
  var count = len >>> 3;
@@ -639,7 +732,7 @@ Reader.prototype.fixed64s = function read_fixed64s(array) {
639
732
  */
640
733
  Reader.prototype.sfixed64s = function read_sfixed64s(array) {
641
734
  if (array === undefined) array = [];
642
- var len = this.uint32(), end = this.pos + len, i = array.length;
735
+ var len = this.size(), end = this.pos + len, i = array.length;
643
736
  /* istanbul ignore if */
644
737
  if (end > this.len) throw indexOutOfRange(this, len);
645
738
  var count = len >>> 3;
@@ -655,7 +748,7 @@ Reader.prototype.sfixed64s = function read_sfixed64s(array) {
655
748
  * @returns {Uint8Array} Value read
656
749
  */
657
750
  Reader.prototype.bytes = function read_bytes() {
658
- var length = this.uint32(),
751
+ var length = this.size(),
659
752
  start = this.pos,
660
753
  end = this.pos + length;
661
754
 
@@ -672,7 +765,7 @@ Reader.prototype.bytes = function read_bytes() {
672
765
  * @returns {string} Value read
673
766
  */
674
767
  Reader.prototype.string = function read_string() {
675
- var length = this.uint32(),
768
+ var length = this.size(),
676
769
  start = this.pos,
677
770
  end = this.pos + length;
678
771
 
@@ -689,7 +782,7 @@ Reader.prototype.string = function read_string() {
689
782
  * @returns {string} Value read
690
783
  */
691
784
  Reader.prototype.stringVerify = function read_string_verify() {
692
- var length = this.uint32(),
785
+ var length = this.size(),
693
786
  start = this.pos,
694
787
  end = this.pos + length;
695
788
 
@@ -755,7 +848,7 @@ Reader.prototype.skipType = function(wireType, depth, fieldNumber) {
755
848
  this.skip(8);
756
849
  break;
757
850
  case 2:
758
- this.skip(this.uint32());
851
+ this.skip(this.size());
759
852
  break;
760
853
  case 3:
761
854
  while (true) {
@@ -53,7 +53,7 @@ BufferReader.prototype.raw = function read_raw_buffer(start, end) {
53
53
  * @override
54
54
  */
55
55
  BufferReader.prototype.string = function read_string_buffer() {
56
- var len = this.uint32(), // modifies pos
56
+ var len = this.size(), // modifies pos
57
57
  start = this.pos,
58
58
  end = this.pos + len;
59
59
 
package/src/tokenize.js CHANGED
@@ -9,13 +9,19 @@ var setCommentRe = /^ *[*/]+ */,
9
9
  setCommentAltRe = /^\s*\*?\/*/,
10
10
  setCommentSplitRe = /\n/g,
11
11
  whitespaceRe = /\s/,
12
- unescapeRe = /\\(.?)/g;
12
+ unescapeRe = /\\([0-7]{1,3}|[xX][0-9a-fA-F]{1,2}|.?)/g;
13
13
 
14
14
  var unescapeMap = {
15
- "0": "\0",
15
+ "a": "\x07",
16
+ "b": "\b",
17
+ "f": "\f",
16
18
  "r": "\r",
17
19
  "n": "\n",
18
- "t": "\t"
20
+ "t": "\t",
21
+ "v": "\v",
22
+ "?": "?",
23
+ "'": "'",
24
+ "\"": "\""
19
25
  };
20
26
 
21
27
  /**
@@ -27,6 +33,11 @@ var unescapeMap = {
27
33
  */
28
34
  function unescape(str) {
29
35
  return str.replace(unescapeRe, function($0, $1) {
36
+ var first = $1.charAt(0);
37
+ if (first >= "0" && first <= "7")
38
+ return String.fromCharCode(parseInt($1, 8) & 255);
39
+ if (first === "x" || first === "X")
40
+ return String.fromCharCode(parseInt($1.substring(1), 16));
30
41
  switch ($1) {
31
42
  case "\\":
32
43
  case "":
package/src/type.js CHANGED
@@ -70,6 +70,12 @@ function Type(name, options) {
70
70
  */
71
71
  this.group = undefined; // toJSON
72
72
 
73
+ /**
74
+ * Declared symbol visibility, if any.
75
+ * @type {"local"|"export"|undefined}
76
+ */
77
+ this.visibility = undefined; // toJSON
78
+
73
79
  /**
74
80
  * Cached fields by id.
75
81
  * @type {Object.<number,Field>|null}
@@ -254,6 +260,7 @@ function clearCache(type) {
254
260
  * @property {number[][]} [extensions] Extension ranges
255
261
  * @property {Array.<number[]|string>} [reserved] Reserved ranges
256
262
  * @property {boolean} [group=false] Whether a legacy group or not
263
+ * @property {"local"|"export"} [visibility] Declared symbol visibility
257
264
  * @property {string|null} [comment] Message type comment
258
265
  */
259
266
 
@@ -304,6 +311,8 @@ Type.fromJSON = function fromJSON(name, json, depth) {
304
311
  type.reserved = json.reserved;
305
312
  if (json.group)
306
313
  type.group = true;
314
+ if (json.visibility)
315
+ type.visibility = json.visibility;
307
316
  if (json.comment)
308
317
  type.comment = json.comment;
309
318
  if (json.edition)
@@ -328,6 +337,7 @@ Type.prototype.toJSON = function toJSON(toJSONOptions) {
328
337
  "extensions" , this.extensions && this.extensions.length ? this.extensions : undefined,
329
338
  "reserved" , this.reserved && this.reserved.length ? this.reserved : undefined,
330
339
  "group" , this.group || undefined,
340
+ "visibility" , this.visibility,
331
341
  "nested" , inherited && inherited.nested || undefined,
332
342
  "comment" , keepComments ? this.comment : undefined
333
343
  ]);
@@ -590,7 +600,7 @@ Type.prototype.decode = function decode_setup(reader, length) { // eslint-disabl
590
600
  Type.prototype.decodeDelimited = function decodeDelimited(reader) {
591
601
  if (!(reader instanceof Reader))
592
602
  reader = Reader.create(reader);
593
- return this.decode(reader, reader.uint32());
603
+ return this.decode(reader, reader.size());
594
604
  };
595
605
 
596
606
  /**
package/src/wrappers.js CHANGED
@@ -40,6 +40,10 @@ var Message = require("./message"),
40
40
  wrappers[".google.protobuf.Any"] = {
41
41
 
42
42
  fromObject: function(object, depth) {
43
+ if (depth === undefined)
44
+ depth = 0;
45
+ if (depth > util.recursionLimit)
46
+ throw Error("max depth exceeded");
43
47
 
44
48
  // unwrap value type if mapped
45
49
  if (object && object["@type"]) {
@@ -57,7 +61,7 @@ wrappers[".google.protobuf.Any"] = {
57
61
  }
58
62
  return this.create({
59
63
  type_url: type_url,
60
- value: type.encode(type.fromObject(object, depth === undefined ? 1 : depth + 1)).finish()
64
+ value: type.encode(type.fromObject(object, depth + 1)).finish()
61
65
  });
62
66
  }
63
67
  }