protobufjs 8.8.0 → 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/ext/descriptor.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  var $protobuf = require("../light");
3
3
  module.exports = exports = $protobuf.descriptor = $protobuf.Root.fromJSON(require("../google/protobuf/descriptor.json")).lookup(".google.protobuf");
4
+ exports.root.define("pb").addJSON(require("../google/protobuf/json_enumvalue_options.json").nested.pb.nested);
4
5
 
5
6
  var Namespace = $protobuf.Namespace,
6
7
  Root = $protobuf.Root,
@@ -1091,6 +1092,10 @@ function fromDescriptorOptionsRecursive(obj, type) {
1091
1092
  if ((key = (field = type._fieldsArray[i]).name) === "uninterpretedOption") continue;
1092
1093
  if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
1093
1094
 
1095
+ if (key === ".pb.enumvalue.json") {
1096
+ val["(pb.enumvalue.json).string"] = obj[key].string === undefined ? "" : obj[key].string;
1097
+ continue;
1098
+ }
1094
1099
  var newKey = underScore(key);
1095
1100
  if (field.resolvedType instanceof Type) {
1096
1101
  val[newKey] = fromDescriptorOptionsRecursive(obj[key], field.resolvedType);
@@ -1115,6 +1120,10 @@ function toDescriptorOptionsRecursive(obj, type) {
1115
1120
  var keys = Object.keys(obj);
1116
1121
  for (var i = 0; i < keys.length; ++i) {
1117
1122
  var key = keys[i];
1123
+ if (type === exports.EnumValueOptions && key === "(pb.enumvalue.json).string") {
1124
+ val[".pb.enumvalue.json"] = { string: obj[key] };
1125
+ continue;
1126
+ }
1118
1127
  var newKey = $protobuf.util.camelCase(key);
1119
1128
  if (!Object.prototype.hasOwnProperty.call(type.fields, newKey)) continue;
1120
1129
  var field = type.fields[newKey];
package/ext/protojson.js CHANGED
@@ -241,11 +241,19 @@ function readScalar(type, value, name) {
241
241
  }
242
242
  }
243
243
 
244
+ function enumJsonName(enm, name) {
245
+ var options = hasOwn(enm.valuesOptions, name) && enm.valuesOptions[name];
246
+ return hasOwn(options, "(pb.enumvalue.json).string") ? options["(pb.enumvalue.json).string"] : undefined;
247
+ }
248
+
244
249
  function readEnum(enm, value, name, options) {
245
250
  if (typeof value === "string") {
246
- var num = enm.values[value];
247
- if (num !== undefined)
248
- return num;
251
+ if (hasOwn(enm.values, value))
252
+ return enm.values[value];
253
+ if (enm.valuesOptions)
254
+ for (var names = Object.keys(enm.valuesOptions), i = 0; i < names.length; ++i)
255
+ if (hasOwn(enm.values, names[i]) && enumJsonName(enm, names[i]) === value)
256
+ return enm.values[names[i]];
249
257
  if (options.ignoreUnknownFields)
250
258
  return SKIP;
251
259
  throw invalid(name, value, "unknown enum value");
@@ -448,7 +456,10 @@ function writeSingular(field, value, options, depth) {
448
456
  if (field.resolvedType.fullName === ".google.protobuf.NullValue")
449
457
  return null;
450
458
  var name = field.resolvedType.valuesById[value];
451
- return name === undefined ? value : name;
459
+ if (name === undefined)
460
+ return value;
461
+ var jsonName = enumJsonName(field.resolvedType, name);
462
+ return jsonName === undefined ? name : jsonName;
452
463
  }
453
464
  return writeScalar(field.type, value);
454
465
  }
@@ -971,6 +971,10 @@
971
971
  }
972
972
  },
973
973
  "extensions": [
974
+ [
975
+ 990,
976
+ 998
977
+ ],
974
978
  [
975
979
  1000,
976
980
  536870911
@@ -341,6 +341,7 @@ message EnumValueOptions {
341
341
  optional FieldOptions.FeatureSupport feature_support = 4;
342
342
  repeated UninterpretedOption uninterpreted_option = 999;
343
343
 
344
+ extensions 990 to 998;
344
345
  extensions 1000 to max;
345
346
  }
346
347
 
@@ -0,0 +1,56 @@
1
+ {
2
+ "nested": {
3
+ "pb": {
4
+ "nested": {
5
+ "enumvalue": {
6
+ "options": {
7
+ "java_package": "com.google.protobuf.util"
8
+ },
9
+ "nested": {
10
+ "JsonEnumValueOptions": {
11
+ "edition": "2024",
12
+ "fields": {
13
+ "string": {
14
+ "type": "string",
15
+ "id": 1
16
+ }
17
+ },
18
+ "visibility": "local"
19
+ },
20
+ "json": {
21
+ "edition": "2024",
22
+ "type": "JsonEnumValueOptions",
23
+ "id": 998,
24
+ "extend": "google.protobuf.EnumValueOptions",
25
+ "options": {
26
+ "feature_support.edition_introduced": "EDITION_2026"
27
+ }
28
+ }
29
+ }
30
+ }
31
+ }
32
+ },
33
+ "google": {
34
+ "nested": {
35
+ "protobuf": {
36
+ "nested": {
37
+ "EnumValueOptions": {
38
+ "edition": "proto2",
39
+ "fields": {},
40
+ "extensions": [
41
+ [
42
+ 990,
43
+ 998
44
+ ],
45
+ [
46
+ 1000,
47
+ 536870911
48
+ ]
49
+ ]
50
+ }
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
@@ -0,0 +1,31 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2026 Google LLC. All rights reserved.
3
+ //
4
+ // Use of this source code is governed by a BSD-style
5
+ // license that can be found in the LICENSE file or at
6
+ // https://developers.google.com/open-source/licenses/bsd
7
+
8
+ edition = "2024";
9
+
10
+ package pb.enumvalue;
11
+
12
+ import "google/protobuf/descriptor.proto";
13
+
14
+ option java_package = "com.google.protobuf.util";
15
+
16
+ local message JsonEnumValueOptions {
17
+ // Allows user to specify a custom string to use when serializing an enum
18
+ // value to JSON.
19
+ //
20
+ // Sample usage:
21
+ // enum Foo {
22
+ // ...
23
+ // FOO_BAR = 42 [(pb.enumvalue.json).string = "my_custom_name"];
24
+ // }
25
+ string string = 1;
26
+ }
27
+
28
+ extend google.protobuf.EnumValueOptions {
29
+ JsonEnumValueOptions json = 998
30
+ [feature_support = { edition_introduced: EDITION_2026 }];
31
+ }
package/index.d.ts CHANGED
@@ -1270,6 +1270,12 @@ export class Reader {
1270
1270
  */
1271
1271
  tag(): number;
1272
1272
 
1273
+ /**
1274
+ * Reads a varint length prefix within signed 32 bit range.
1275
+ * @returns Length read
1276
+ */
1277
+ size(): number;
1278
+
1273
1279
  /**
1274
1280
  * Reads a varint as a signed 32 bit value.
1275
1281
  * @returns Value read
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "protobufjs",
3
- "version": "8.8.0",
3
+ "version": "8.8.1",
4
4
  "description": "Protocol Buffers for JavaScript & TypeScript.",
5
5
  "author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
6
6
  "license": "BSD-3-Clause",
package/src/decoder.js CHANGED
@@ -83,7 +83,7 @@ function decoder(mtype) {
83
83
  ("if(%s===util.emptyObject)", ref)
84
84
  ("%s={}", ref);
85
85
  gen
86
- ("var c2=r.uint32()+r.pos")
86
+ ("var c2=r.size()+r.pos")
87
87
  ("if(c2>r.len)")
88
88
  ("throw RangeError(\"index out of range\")")
89
89
  ("r.len=c2");
@@ -115,7 +115,7 @@ function decoder(mtype) {
115
115
  ("break");
116
116
 
117
117
  if (types.basic[type] === undefined) gen
118
- ("v=types[%i].decode(r,r.uint32(),undefined,q+1,v)", i); // can't be groups
118
+ ("v=types[%i].decode(r,r.size(),undefined,q+1,v)", i); // can't be groups
119
119
  else gen
120
120
  ("v=r.%s()", type === "string" ? stringMethod(field) : type);
121
121
 
@@ -160,7 +160,7 @@ function decoder(mtype) {
160
160
  ("if(u===2){");
161
161
  if (closed) {
162
162
  gen
163
- ("var c2=r.uint32()+r.pos")
163
+ ("var c2=r.size()+r.pos")
164
164
  ("if(c2>r.len)")
165
165
  ("throw RangeError(\"index out of range\")")
166
166
  ("r.len=c2")
@@ -198,7 +198,7 @@ function decoder(mtype) {
198
198
  if (field.delimited) gen
199
199
  ("%s.push(types[%i].decode(r,undefined,%i,q+1))", ref, i, field.id * 8 + 4);
200
200
  else gen
201
- ("%s.push(types[%i].decode(r,r.uint32(),undefined,q+1))", ref, i);
201
+ ("%s.push(types[%i].decode(r,r.size(),undefined,q+1))", ref, i);
202
202
  } else if (closed) { gen
203
203
  ("v=r.%s()", type)
204
204
  ("if(types[%i].valuesById[v]!==undefined){", i)
@@ -219,7 +219,7 @@ function decoder(mtype) {
219
219
  if (field.delimited) gen
220
220
  ("%s=types[%i].decode(r,undefined,%i,q+1,%s)", ref, i, field.id * 8 + 4, ref);
221
221
  else gen
222
- ("%s=types[%i].decode(r,r.uint32(),undefined,q+1,%s)", ref, i, ref);
222
+ ("%s=types[%i].decode(r,r.size(),undefined,q+1,%s)", ref, i, ref);
223
223
  }
224
224
  else if (field.hasPresence) {
225
225
  gen
package/src/reader.js CHANGED
@@ -208,6 +208,45 @@ Reader.prototype.tag = function read_tag() {
208
208
  throw Error("invalid tag encoding");
209
209
  };
210
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
+
211
250
  /**
212
251
  * Reads a varint as a signed 32 bit value.
213
252
  * @returns {number} Value read
@@ -421,7 +460,7 @@ Reader.prototype.double = function read_double() {
421
460
  */
422
461
  Reader.prototype.uint32s = function read_uint32s(array) {
423
462
  if (array === undefined) array = [];
424
- var end = this.uint32() + this.pos, len = this.len, buf = this.buf, pos = this.pos, value;
463
+ var end = this.size() + this.pos, len = this.len, buf = this.buf, pos = this.pos, value;
425
464
  if (end > len) throw indexOutOfRange(this, end - this.pos);
426
465
  this.len = end;
427
466
  while (pos < end) {
@@ -447,7 +486,7 @@ Reader.prototype.uint32s = function read_uint32s(array) {
447
486
  */
448
487
  Reader.prototype.int32s = function read_int32s(array) {
449
488
  if (array === undefined) array = [];
450
- var end = this.uint32() + this.pos, len = this.len, buf = this.buf, pos = this.pos, value;
489
+ var end = this.size() + this.pos, len = this.len, buf = this.buf, pos = this.pos, value;
451
490
  if (end > len) throw indexOutOfRange(this, end - this.pos);
452
491
  this.len = end;
453
492
  while (pos < end) {
@@ -473,7 +512,7 @@ Reader.prototype.int32s = function read_int32s(array) {
473
512
  */
474
513
  Reader.prototype.sint32s = function read_sint32s(array) {
475
514
  if (array === undefined) array = [];
476
- var end = this.uint32() + this.pos, len = this.len;
515
+ var end = this.size() + this.pos, len = this.len;
477
516
  if (end > len) throw indexOutOfRange(this, end - this.pos);
478
517
  this.len = end;
479
518
  while (this.pos < end)
@@ -490,7 +529,7 @@ Reader.prototype.sint32s = function read_sint32s(array) {
490
529
  */
491
530
  Reader.prototype.bools = function read_bools(array) {
492
531
  if (array === undefined) array = [];
493
- var end = this.uint32() + this.pos, len = this.len, buf = this.buf, pos = this.pos, value;
532
+ var end = this.size() + this.pos, len = this.len, buf = this.buf, pos = this.pos, value;
494
533
  if (end > len) throw indexOutOfRange(this, end - this.pos);
495
534
  this.len = end;
496
535
  while (pos < end) {
@@ -528,7 +567,7 @@ function getLazyView(reader, count, threshold) {
528
567
  */
529
568
  Reader.prototype.fixed32s = function read_fixed32s(array) {
530
569
  if (array === undefined) array = [];
531
- var len = this.uint32(), end = this.pos + len;
570
+ var len = this.size(), end = this.pos + len;
532
571
  /* istanbul ignore if */
533
572
  if (end > this.len) throw indexOutOfRange(this, len);
534
573
  var count = len >>> 2, i = array.length, pos = this.pos;
@@ -552,7 +591,7 @@ Reader.prototype.fixed32s = function read_fixed32s(array) {
552
591
  */
553
592
  Reader.prototype.sfixed32s = function read_sfixed32s(array) {
554
593
  if (array === undefined) array = [];
555
- var len = this.uint32(), end = this.pos + len;
594
+ var len = this.size(), end = this.pos + len;
556
595
  /* istanbul ignore if */
557
596
  if (end > this.len) throw indexOutOfRange(this, len);
558
597
  var count = len >>> 2, i = array.length, pos = this.pos;
@@ -576,7 +615,7 @@ Reader.prototype.sfixed32s = function read_sfixed32s(array) {
576
615
  */
577
616
  Reader.prototype.floats = function read_floats(array) {
578
617
  if (array === undefined) array = [];
579
- var len = this.uint32(), end = this.pos + len;
618
+ var len = this.size(), end = this.pos + len;
580
619
  /* istanbul ignore if */
581
620
  if (end > this.len) throw indexOutOfRange(this, len);
582
621
  var count = len >>> 2, i = array.length, pos = this.pos;
@@ -600,7 +639,7 @@ Reader.prototype.floats = function read_floats(array) {
600
639
  */
601
640
  Reader.prototype.doubles = function read_doubles(array) {
602
641
  if (array === undefined) array = [];
603
- var len = this.uint32(), end = this.pos + len;
642
+ var len = this.size(), end = this.pos + len;
604
643
  /* istanbul ignore if */
605
644
  if (end > this.len) throw indexOutOfRange(this, len);
606
645
  var count = len >>> 3, i = array.length, pos = this.pos;
@@ -624,7 +663,7 @@ Reader.prototype.doubles = function read_doubles(array) {
624
663
  */
625
664
  Reader.prototype.uint64s = function read_uint64s(array) {
626
665
  if (array === undefined) array = [];
627
- var end = this.uint32() + this.pos, len = this.len;
666
+ var end = this.size() + this.pos, len = this.len;
628
667
  if (end > len) throw indexOutOfRange(this, end - this.pos);
629
668
  this.len = end;
630
669
  while (this.pos < end)
@@ -641,7 +680,7 @@ Reader.prototype.uint64s = function read_uint64s(array) {
641
680
  */
642
681
  Reader.prototype.int64s = function read_int64s(array) {
643
682
  if (array === undefined) array = [];
644
- var end = this.uint32() + this.pos, len = this.len;
683
+ var end = this.size() + this.pos, len = this.len;
645
684
  if (end > len) throw indexOutOfRange(this, end - this.pos);
646
685
  this.len = end;
647
686
  while (this.pos < end)
@@ -658,7 +697,7 @@ Reader.prototype.int64s = function read_int64s(array) {
658
697
  */
659
698
  Reader.prototype.sint64s = function read_sint64s(array) {
660
699
  if (array === undefined) array = [];
661
- var end = this.uint32() + this.pos, len = this.len;
700
+ var end = this.size() + this.pos, len = this.len;
662
701
  if (end > len) throw indexOutOfRange(this, end - this.pos);
663
702
  this.len = end;
664
703
  while (this.pos < end)
@@ -675,7 +714,7 @@ Reader.prototype.sint64s = function read_sint64s(array) {
675
714
  */
676
715
  Reader.prototype.fixed64s = function read_fixed64s(array) {
677
716
  if (array === undefined) array = [];
678
- var len = this.uint32(), end = this.pos + len, i = array.length;
717
+ var len = this.size(), end = this.pos + len, i = array.length;
679
718
  /* istanbul ignore if */
680
719
  if (end > this.len) throw indexOutOfRange(this, len);
681
720
  var count = len >>> 3;
@@ -693,7 +732,7 @@ Reader.prototype.fixed64s = function read_fixed64s(array) {
693
732
  */
694
733
  Reader.prototype.sfixed64s = function read_sfixed64s(array) {
695
734
  if (array === undefined) array = [];
696
- var len = this.uint32(), end = this.pos + len, i = array.length;
735
+ var len = this.size(), end = this.pos + len, i = array.length;
697
736
  /* istanbul ignore if */
698
737
  if (end > this.len) throw indexOutOfRange(this, len);
699
738
  var count = len >>> 3;
@@ -709,7 +748,7 @@ Reader.prototype.sfixed64s = function read_sfixed64s(array) {
709
748
  * @returns {Uint8Array} Value read
710
749
  */
711
750
  Reader.prototype.bytes = function read_bytes() {
712
- var length = this.uint32(),
751
+ var length = this.size(),
713
752
  start = this.pos,
714
753
  end = this.pos + length;
715
754
 
@@ -726,7 +765,7 @@ Reader.prototype.bytes = function read_bytes() {
726
765
  * @returns {string} Value read
727
766
  */
728
767
  Reader.prototype.string = function read_string() {
729
- var length = this.uint32(),
768
+ var length = this.size(),
730
769
  start = this.pos,
731
770
  end = this.pos + length;
732
771
 
@@ -743,7 +782,7 @@ Reader.prototype.string = function read_string() {
743
782
  * @returns {string} Value read
744
783
  */
745
784
  Reader.prototype.stringVerify = function read_string_verify() {
746
- var length = this.uint32(),
785
+ var length = this.size(),
747
786
  start = this.pos,
748
787
  end = this.pos + length;
749
788
 
@@ -809,7 +848,7 @@ Reader.prototype.skipType = function(wireType, depth, fieldNumber) {
809
848
  this.skip(8);
810
849
  break;
811
850
  case 2:
812
- this.skip(this.uint32());
851
+ this.skip(this.size());
813
852
  break;
814
853
  case 3:
815
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
@@ -600,7 +600,7 @@ Type.prototype.decode = function decode_setup(reader, length) { // eslint-disabl
600
600
  Type.prototype.decodeDelimited = function decodeDelimited(reader) {
601
601
  if (!(reader instanceof Reader))
602
602
  reader = Reader.create(reader);
603
- return this.decode(reader, reader.uint32());
603
+ return this.decode(reader, reader.size());
604
604
  };
605
605
 
606
606
  /**