@bufbuild/protobuf 2.0.0-alpha.1 → 2.0.0-alpha.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 (113) hide show
  1. package/dist/cjs/codegenv1/embed.d.ts +2 -2
  2. package/dist/cjs/codegenv1/embed.js +25 -25
  3. package/dist/cjs/codegenv1/enum.js +2 -4
  4. package/dist/cjs/codegenv1/restore-json-names.d.ts +3 -0
  5. package/dist/cjs/codegenv1/restore-json-names.js +3 -0
  6. package/dist/cjs/codegenv1/symbols.d.ts +9 -0
  7. package/dist/cjs/codegenv1/symbols.js +9 -0
  8. package/dist/cjs/codegenv1/types.d.ts +55 -2
  9. package/dist/cjs/create.js +7 -18
  10. package/dist/cjs/desc-types.d.ts +27 -10
  11. package/dist/cjs/extensions.d.ts +21 -1
  12. package/dist/cjs/extensions.js +38 -7
  13. package/dist/cjs/fields.d.ts +17 -16
  14. package/dist/cjs/fields.js +17 -10
  15. package/dist/cjs/from-binary.js +9 -19
  16. package/dist/cjs/from-json.js +9 -13
  17. package/dist/cjs/index.d.ts +1 -1
  18. package/dist/cjs/index.js +3 -1
  19. package/dist/cjs/reflect/names.d.ts +0 -12
  20. package/dist/cjs/reflect/names.js +1 -39
  21. package/dist/cjs/reflect/nested-types.d.ts +11 -1
  22. package/dist/cjs/reflect/nested-types.js +34 -1
  23. package/dist/cjs/reflect/reflect-types.d.ts +158 -10
  24. package/dist/cjs/reflect/reflect.d.ts +11 -2
  25. package/dist/cjs/reflect/reflect.js +306 -281
  26. package/dist/cjs/reflect/registry.js +50 -41
  27. package/dist/cjs/reflect/unsafe.js +38 -43
  28. package/dist/cjs/to-binary.js +1 -3
  29. package/dist/cjs/to-json.js +7 -27
  30. package/dist/cjs/wire/base64-encoding.js +2 -2
  31. package/dist/cjs/wire/binary-encoding.d.ts +5 -3
  32. package/dist/cjs/wire/binary-encoding.js +16 -10
  33. package/dist/cjs/wkt/gen/google/protobuf/any_pb.d.ts +7 -0
  34. package/dist/cjs/wkt/gen/google/protobuf/any_pb.js +7 -2
  35. package/dist/cjs/wkt/gen/google/protobuf/api_pb.d.ts +15 -0
  36. package/dist/cjs/wkt/gen/google/protobuf/api_pb.js +15 -6
  37. package/dist/cjs/wkt/gen/google/protobuf/compiler/plugin_pb.d.ts +22 -0
  38. package/dist/cjs/wkt/gen/google/protobuf/compiler/plugin_pb.js +22 -9
  39. package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.d.ts +186 -0
  40. package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.js +186 -83
  41. package/dist/cjs/wkt/gen/google/protobuf/duration_pb.d.ts +7 -0
  42. package/dist/cjs/wkt/gen/google/protobuf/duration_pb.js +7 -2
  43. package/dist/cjs/wkt/gen/google/protobuf/empty_pb.d.ts +7 -0
  44. package/dist/cjs/wkt/gen/google/protobuf/empty_pb.js +7 -2
  45. package/dist/cjs/wkt/gen/google/protobuf/field_mask_pb.d.ts +7 -0
  46. package/dist/cjs/wkt/gen/google/protobuf/field_mask_pb.js +7 -2
  47. package/dist/cjs/wkt/gen/google/protobuf/source_context_pb.d.ts +7 -0
  48. package/dist/cjs/wkt/gen/google/protobuf/source_context_pb.js +7 -2
  49. package/dist/cjs/wkt/gen/google/protobuf/struct_pb.d.ts +18 -0
  50. package/dist/cjs/wkt/gen/google/protobuf/struct_pb.js +18 -7
  51. package/dist/cjs/wkt/gen/google/protobuf/timestamp_pb.d.ts +7 -0
  52. package/dist/cjs/wkt/gen/google/protobuf/timestamp_pb.js +7 -2
  53. package/dist/cjs/wkt/gen/google/protobuf/type_pb.d.ts +32 -0
  54. package/dist/cjs/wkt/gen/google/protobuf/type_pb.js +32 -13
  55. package/dist/cjs/wkt/gen/google/protobuf/wrappers_pb.d.ts +39 -0
  56. package/dist/cjs/wkt/gen/google/protobuf/wrappers_pb.js +39 -18
  57. package/dist/esm/codegenv1/embed.d.ts +2 -2
  58. package/dist/esm/codegenv1/embed.js +25 -25
  59. package/dist/esm/codegenv1/enum.js +2 -4
  60. package/dist/esm/codegenv1/restore-json-names.d.ts +3 -0
  61. package/dist/esm/codegenv1/restore-json-names.js +3 -0
  62. package/dist/esm/codegenv1/symbols.d.ts +9 -0
  63. package/dist/esm/codegenv1/symbols.js +9 -0
  64. package/dist/esm/codegenv1/types.d.ts +55 -2
  65. package/dist/esm/create.js +7 -18
  66. package/dist/esm/desc-types.d.ts +27 -10
  67. package/dist/esm/extensions.d.ts +21 -1
  68. package/dist/esm/extensions.js +35 -6
  69. package/dist/esm/fields.d.ts +17 -16
  70. package/dist/esm/fields.js +17 -10
  71. package/dist/esm/from-binary.js +9 -19
  72. package/dist/esm/from-json.js +11 -15
  73. package/dist/esm/index.d.ts +1 -1
  74. package/dist/esm/index.js +1 -1
  75. package/dist/esm/reflect/names.d.ts +0 -12
  76. package/dist/esm/reflect/names.js +0 -37
  77. package/dist/esm/reflect/nested-types.d.ts +11 -1
  78. package/dist/esm/reflect/nested-types.js +32 -0
  79. package/dist/esm/reflect/reflect-types.d.ts +158 -10
  80. package/dist/esm/reflect/reflect.d.ts +11 -2
  81. package/dist/esm/reflect/reflect.js +304 -279
  82. package/dist/esm/reflect/registry.js +50 -41
  83. package/dist/esm/reflect/unsafe.js +38 -43
  84. package/dist/esm/to-binary.js +1 -3
  85. package/dist/esm/to-json.js +7 -27
  86. package/dist/esm/wire/base64-encoding.js +2 -2
  87. package/dist/esm/wire/binary-encoding.d.ts +5 -3
  88. package/dist/esm/wire/binary-encoding.js +16 -10
  89. package/dist/esm/wkt/gen/google/protobuf/any_pb.d.ts +7 -0
  90. package/dist/esm/wkt/gen/google/protobuf/any_pb.js +7 -2
  91. package/dist/esm/wkt/gen/google/protobuf/api_pb.d.ts +15 -0
  92. package/dist/esm/wkt/gen/google/protobuf/api_pb.js +15 -6
  93. package/dist/esm/wkt/gen/google/protobuf/compiler/plugin_pb.d.ts +22 -0
  94. package/dist/esm/wkt/gen/google/protobuf/compiler/plugin_pb.js +22 -9
  95. package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.d.ts +186 -0
  96. package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.js +186 -83
  97. package/dist/esm/wkt/gen/google/protobuf/duration_pb.d.ts +7 -0
  98. package/dist/esm/wkt/gen/google/protobuf/duration_pb.js +7 -2
  99. package/dist/esm/wkt/gen/google/protobuf/empty_pb.d.ts +7 -0
  100. package/dist/esm/wkt/gen/google/protobuf/empty_pb.js +7 -2
  101. package/dist/esm/wkt/gen/google/protobuf/field_mask_pb.d.ts +7 -0
  102. package/dist/esm/wkt/gen/google/protobuf/field_mask_pb.js +7 -2
  103. package/dist/esm/wkt/gen/google/protobuf/source_context_pb.d.ts +7 -0
  104. package/dist/esm/wkt/gen/google/protobuf/source_context_pb.js +7 -2
  105. package/dist/esm/wkt/gen/google/protobuf/struct_pb.d.ts +18 -0
  106. package/dist/esm/wkt/gen/google/protobuf/struct_pb.js +18 -7
  107. package/dist/esm/wkt/gen/google/protobuf/timestamp_pb.d.ts +7 -0
  108. package/dist/esm/wkt/gen/google/protobuf/timestamp_pb.js +7 -2
  109. package/dist/esm/wkt/gen/google/protobuf/type_pb.d.ts +32 -0
  110. package/dist/esm/wkt/gen/google/protobuf/type_pb.js +32 -13
  111. package/dist/esm/wkt/gen/google/protobuf/wrappers_pb.d.ts +39 -0
  112. package/dist/esm/wkt/gen/google/protobuf/wrappers_pb.js +39 -18
  113. package/package.json +2 -2
@@ -19,6 +19,7 @@ const text_format_js_1 = require("../wire/text-format.js");
19
19
  const scalar_js_1 = require("./scalar.js");
20
20
  const nested_types_js_1 = require("./nested-types.js");
21
21
  const unsafe_js_1 = require("./unsafe.js");
22
+ const names_js_1 = require("./names.js");
22
23
  /**
23
24
  * Create a registry from the given inputs.
24
25
  *
@@ -205,8 +206,6 @@ const TYPE_BYTES = 12;
205
206
  const TYPE_ENUM = 14;
206
207
  // bootstrap-inject google.protobuf.FieldDescriptorProto.Label.LABEL_REPEATED: const $name: FieldDescriptorProto_Label.$localName = $number;
207
208
  const LABEL_REPEATED = 3;
208
- // bootstrap-inject google.protobuf.FieldDescriptorProto.Label.LABEL_OPTIONAL: const $name: FieldDescriptorProto_Label.$localName = $number;
209
- const LABEL_OPTIONAL = 1;
210
209
  // bootstrap-inject google.protobuf.FieldDescriptorProto.Label.LABEL_REQUIRED: const $name: FieldDescriptorProto_Label.$localName = $number;
211
210
  const LABEL_REQUIRED = 2;
212
211
  // bootstrap-inject google.protobuf.FieldOptions.JSType.JS_STRING: const $name: FieldOptions_JSType.$localName = $number;
@@ -215,6 +214,8 @@ const JS_STRING = 1;
215
214
  const IDEMPOTENCY_UNKNOWN = 0;
216
215
  // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.EXPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
217
216
  const EXPLICIT = 1;
217
+ // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
218
+ const IMPLICIT = 2;
218
219
  // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.LEGACY_REQUIRED: const $name: FeatureSet_FieldPresence.$localName = $number;
219
220
  const LEGACY_REQUIRED = 3;
220
221
  // bootstrap-inject google.protobuf.FeatureSet.RepeatedFieldEncoding.PACKED: const $name: FeatureSet_RepeatedFieldEncoding.$localName = $number;
@@ -225,7 +226,7 @@ const DELIMITED = 2;
225
226
  const OPEN = 1;
226
227
  // prettier-ignore
227
228
  // bootstrap-inject defaults: EDITION_PROTO2 to EDITION_2023: export const minimumEdition: SupportedEdition = $minimumEdition, maximumEdition: SupportedEdition = $maximumEdition;
228
- // generated from protoc v27.0-rc1
229
+ // generated from protoc v27.0-rc2
229
230
  exports.minimumEdition = 998, exports.maximumEdition = 1000;
230
231
  const featureDefaults = {
231
232
  // EDITION_PROTO2
@@ -266,7 +267,7 @@ function addFile(proto, reg) {
266
267
  proto,
267
268
  deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
268
269
  edition: getFileEdition(proto),
269
- name: proto.name.replace(/\.proto/, ""),
270
+ name: proto.name.replace(/\.proto$/, ""),
270
271
  dependencies: findFileDependencies(proto, reg),
271
272
  enums: [],
272
273
  messages: [],
@@ -274,7 +275,7 @@ function addFile(proto, reg) {
274
275
  services: [],
275
276
  toString() {
276
277
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
277
- return `file ${this.proto.name}`;
278
+ return `file ${proto.name}`;
278
279
  },
279
280
  };
280
281
  const mapEntriesStore = new Map();
@@ -345,6 +346,7 @@ function addFields(message, reg, mapEntries) {
345
346
  const oneof = findOneof(proto, allOneofs);
346
347
  const field = newField(proto, message, reg, oneof, mapEntries);
347
348
  message.fields.push(field);
349
+ message.field[field.localName] = field;
348
350
  if (oneof === undefined) {
349
351
  message.members.push(field);
350
352
  }
@@ -369,6 +371,7 @@ function addFields(message, reg, mapEntries) {
369
371
  */
370
372
  function addEnum(proto, file, parent, reg) {
371
373
  var _a, _b, _c;
374
+ const sharedPrefix = findEnumSharedPrefix(proto.name, proto.value);
372
375
  const desc = {
373
376
  kind: "enum",
374
377
  proto,
@@ -379,7 +382,7 @@ function addEnum(proto, file, parent, reg) {
379
382
  name: proto.name,
380
383
  typeName: makeTypeName(proto, parent, file),
381
384
  values: [],
382
- sharedPrefix: findEnumSharedPrefix(proto.name, proto.value.map((v) => v.name)),
385
+ sharedPrefix,
383
386
  toString() {
384
387
  return `enum ${this.typeName}`;
385
388
  },
@@ -388,12 +391,14 @@ function addEnum(proto, file, parent, reg) {
388
391
  reg.add(desc);
389
392
  proto.value.forEach((proto) => {
390
393
  var _a, _b;
394
+ const name = proto.name;
391
395
  desc.values.push({
392
396
  kind: "enum_value",
393
397
  proto,
394
398
  deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
395
399
  parent: desc,
396
400
  name: proto.name,
401
+ localName: (0, names_js_1.safeObjectProperty)(sharedPrefix == undefined ? name : name.substring(sharedPrefix.length)),
397
402
  number: proto.number,
398
403
  toString() {
399
404
  return `enum value ${desc.typeName}.${this.name}`;
@@ -417,6 +422,7 @@ function addMessage(proto, file, parent, reg, mapEntries) {
417
422
  name: proto.name,
418
423
  typeName: makeTypeName(proto, parent, file),
419
424
  fields: [],
425
+ field: {},
420
426
  oneofs: [],
421
427
  members: [],
422
428
  nestedEnums: [],
@@ -493,6 +499,9 @@ function newMethod(proto, parent, reg) {
493
499
  deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
494
500
  parent,
495
501
  name,
502
+ localName: (0, names_js_1.safeObjectProperty)(name.length
503
+ ? (0, names_js_1.safeObjectProperty)(name[0].toLowerCase() + name.substring(1))
504
+ : name),
496
505
  methodKind,
497
506
  input,
498
507
  output,
@@ -513,6 +522,7 @@ function newOneof(proto, parent) {
513
522
  parent,
514
523
  fields: [],
515
524
  name: proto.name,
525
+ localName: (0, names_js_1.safeObjectProperty)((0, names_js_1.protoCamelCase)(proto.name)),
516
526
  toString() {
517
527
  return `oneof ${parent.typeName}.${this.name}`;
518
528
  },
@@ -520,7 +530,9 @@ function newOneof(proto, parent) {
520
530
  }
521
531
  function newField(proto, parentOrFile, reg, oneof, mapEntries) {
522
532
  var _a, _b, _c;
533
+ const isExtension = mapEntries === undefined;
523
534
  const field = {
535
+ kind: "field",
524
536
  proto,
525
537
  deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
526
538
  name: proto.name,
@@ -528,8 +540,16 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
528
540
  scalar: undefined,
529
541
  message: undefined,
530
542
  enum: undefined,
543
+ presence: getFieldPresence(proto, oneof, isExtension, parentOrFile),
544
+ listKind: undefined,
545
+ mapKind: undefined,
546
+ mapKey: undefined,
547
+ delimitedEncoding: undefined,
548
+ packed: undefined,
549
+ longType: undefined,
550
+ getDefaultValue: undefined,
531
551
  };
532
- if (mapEntries === undefined) {
552
+ if (isExtension) {
533
553
  // extension field
534
554
  const file = parentOrFile.kind == "file" ? parentOrFile : parentOrFile.file;
535
555
  const parent = parentOrFile.kind == "file" ? undefined : parentOrFile;
@@ -549,9 +569,11 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
549
569
  // regular field
550
570
  const parent = parentOrFile;
551
571
  (0, assert_js_1.assert)(parent.kind == "message");
552
- field.kind = "field";
553
572
  field.parent = parent;
554
573
  field.oneof = oneof;
574
+ field.localName = oneof
575
+ ? (0, names_js_1.protoCamelCase)(proto.name)
576
+ : (0, names_js_1.safeObjectProperty)((0, names_js_1.protoCamelCase)(proto.name));
555
577
  field.jsonName = proto.jsonName;
556
578
  field.toString = () => `field ${parent.typeName}.${proto.name}`;
557
579
  }
@@ -572,7 +594,7 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
572
594
  (0, assert_js_1.assert)(keyField.scalar != scalar_js_1.ScalarType.BYTES &&
573
595
  keyField.scalar != scalar_js_1.ScalarType.FLOAT &&
574
596
  keyField.scalar != scalar_js_1.ScalarType.DOUBLE);
575
- const valueField = mapEntry.fields.find((f) => f.proto.number === 2);
597
+ const valueField = mapEntry.fields.find((f) => f.number === 2);
576
598
  (0, assert_js_1.assert)(valueField);
577
599
  (0, assert_js_1.assert)(valueField.fieldKind != "list" && valueField.fieldKind != "map");
578
600
  field.mapKey = keyField.scalar;
@@ -642,8 +664,6 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
642
664
  break;
643
665
  }
644
666
  }
645
- field.presence = getFieldPresence(proto, oneof, parentOrFile);
646
- field.optional = isOptionalField(field);
647
667
  return field;
648
668
  }
649
669
  /**
@@ -682,13 +702,13 @@ function findFileDependencies(proto, reg) {
682
702
  * Finds a prefix shared by enum values, for example `my_enum_` for
683
703
  * `enum MyEnum {MY_ENUM_A=0; MY_ENUM_B=1;}`.
684
704
  */
685
- function findEnumSharedPrefix(enumName, valueNames) {
705
+ function findEnumSharedPrefix(enumName, values) {
686
706
  const prefix = camelToSnakeCase(enumName) + "_";
687
- for (const name of valueNames) {
688
- if (!name.toLowerCase().startsWith(prefix)) {
707
+ for (const value of values) {
708
+ if (!value.name.toLowerCase().startsWith(prefix)) {
689
709
  return undefined;
690
710
  }
691
- const shortName = name.substring(prefix.length);
711
+ const shortName = value.name.substring(prefix.length);
692
712
  if (shortName.length == 0) {
693
713
  return undefined;
694
714
  }
@@ -760,39 +780,28 @@ function findOneof(proto, allOneofs) {
760
780
  * Presence of the field.
761
781
  * See https://protobuf.dev/programming-guides/field_presence/
762
782
  */
763
- function getFieldPresence(proto, oneof, parent) {
783
+ function getFieldPresence(proto, oneof, isExtension, parent) {
764
784
  if (proto.label == LABEL_REQUIRED) {
765
785
  // proto2 required is LEGACY_REQUIRED
766
786
  return LEGACY_REQUIRED;
767
787
  }
768
- const { edition } = parent.kind == "message" ? parent.file : parent;
769
- if (edition == EDITION_PROTO3) {
770
- // proto3 oneof and optional are explicit
771
- if (oneof != undefined || proto.proto3Optional) {
772
- return EXPLICIT;
773
- }
774
- // proto3 singular message is explicit
775
- const singularMessage = proto.label != LABEL_REPEATED && proto.type == TYPE_MESSAGE;
776
- if (singularMessage) {
777
- return EXPLICIT;
778
- }
788
+ if (proto.label == LABEL_REPEATED) {
789
+ // repeated fields (including maps) do not track presence
790
+ return IMPLICIT;
779
791
  }
780
- // also resolves proto2/proto3 defaults
781
- return resolveFeature("fieldPresence", { proto, parent });
782
- }
783
- /**
784
- * Did the user use the `optional` keyword?
785
- */
786
- function isOptionalField(field) {
787
- const edition = (field.kind == "extension" ? field.file : field.parent.file)
788
- .edition;
789
- if (edition == EDITION_PROTO2) {
790
- return !field.oneof && field.proto.label == LABEL_OPTIONAL;
792
+ if (!!oneof || proto.proto3Optional) {
793
+ // oneof is always explicit
794
+ return EXPLICIT;
791
795
  }
792
- if (edition == EDITION_PROTO3) {
793
- return field.proto.proto3Optional;
796
+ if (proto.type == TYPE_MESSAGE) {
797
+ // singular message field cannot be implicit
798
+ return EXPLICIT;
794
799
  }
795
- return false;
800
+ if (isExtension) {
801
+ // extensions always track presence
802
+ return EXPLICIT;
803
+ }
804
+ return resolveFeature("fieldPresence", { proto, parent });
796
805
  }
797
806
  /**
798
807
  * Pack this repeated field?
@@ -14,7 +14,6 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.unsafeSetMapEntry = exports.unsafeAddListItem = exports.unsafeClear = exports.unsafeSet = exports.unsafeGet = exports.unsafeIsSetExplicit = exports.unsafeIsSet = exports.unsafeOneofCase = exports.unsafeLocal = void 0;
17
- const names_js_1 = require("./names.js");
18
17
  const scalar_js_1 = require("./scalar.js");
19
18
  // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
20
19
  const IMPLICIT = 2;
@@ -26,11 +25,11 @@ exports.unsafeLocal = Symbol.for("reflect unsafe local");
26
25
  */
27
26
  function unsafeOneofCase(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
28
27
  oneof) {
29
- const c = target[(0, names_js_1.localName)(oneof)].case;
28
+ const c = target[oneof.localName].case;
30
29
  if (c === undefined) {
31
30
  return c;
32
31
  }
33
- return oneof.fields.find((f) => (0, names_js_1.localName)(f) === c);
32
+ return oneof.fields.find((f) => f.localName === c);
34
33
  }
35
34
  exports.unsafeOneofCase = unsafeOneofCase;
36
35
  /**
@@ -40,28 +39,28 @@ exports.unsafeOneofCase = unsafeOneofCase;
40
39
  */
41
40
  function unsafeIsSet(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
42
41
  field) {
43
- const name = (0, names_js_1.localName)(field);
42
+ const name = field.localName;
44
43
  if (field.oneof) {
45
- return target[(0, names_js_1.localName)(field.oneof)].case === name; // eslint-disable-line @typescript-eslint/no-unsafe-member-access
44
+ return target[field.oneof.localName].case === name; // eslint-disable-line @typescript-eslint/no-unsafe-member-access
46
45
  }
46
+ if (field.presence != IMPLICIT) {
47
+ // Fields with explicit presence have properties on the prototype chain
48
+ // for default / zero values (except for proto3).
49
+ return (target[name] !== undefined &&
50
+ Object.prototype.hasOwnProperty.call(target, name));
51
+ }
52
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
47
53
  switch (field.fieldKind) {
48
- case "message":
49
- return target[name] !== undefined;
50
54
  case "list":
51
55
  return target[name].length > 0;
52
56
  case "map":
53
57
  return Object.keys(target[name]).length > 0; // eslint-disable-line @typescript-eslint/no-unsafe-argument
54
- default:
55
- if (field.presence == IMPLICIT) {
56
- if (field.fieldKind == "enum") {
57
- return target[name] !== field.enum.values[0].number;
58
- }
59
- return !(0, scalar_js_1.isScalarZeroValue)(field.scalar, target[name]);
60
- }
61
- // EXPLICIT and LEGACY_REQUIRED
62
- return (target[name] !== undefined &&
63
- Object.prototype.hasOwnProperty.call(target, name));
58
+ case "scalar":
59
+ return !(0, scalar_js_1.isScalarZeroValue)(field.scalar, target[name]);
60
+ case "enum":
61
+ return target[name] !== field.enum.values[0].number;
64
62
  }
63
+ throw new Error("message field with implicit presence");
65
64
  }
66
65
  exports.unsafeIsSet = unsafeIsSet;
67
66
  /**
@@ -81,15 +80,14 @@ exports.unsafeIsSetExplicit = unsafeIsSetExplicit;
81
80
  * @private
82
81
  */
83
82
  function unsafeGet(target, field) {
84
- const name = (0, names_js_1.localName)(field);
85
83
  if (field.oneof) {
86
- const oneof = target[(0, names_js_1.localName)(field.oneof)];
87
- if (oneof.case === name) {
84
+ const oneof = target[field.oneof.localName];
85
+ if (oneof.case === field.localName) {
88
86
  return oneof.value;
89
87
  }
90
88
  return undefined;
91
89
  }
92
- return target[name];
90
+ return target[field.localName];
93
91
  }
94
92
  exports.unsafeGet = unsafeGet;
95
93
  /**
@@ -98,15 +96,14 @@ exports.unsafeGet = unsafeGet;
98
96
  * @private
99
97
  */
100
98
  function unsafeSet(target, field, value) {
101
- const name = (0, names_js_1.localName)(field);
102
99
  if (field.oneof) {
103
- target[(0, names_js_1.localName)(field.oneof)] = {
104
- case: name,
100
+ target[field.oneof.localName] = {
101
+ case: field.localName,
105
102
  value: value,
106
103
  };
107
104
  }
108
105
  else {
109
- target[name] = value;
106
+ target[field.localName] = value;
110
107
  }
111
108
  }
112
109
  exports.unsafeSet = unsafeSet;
@@ -117,14 +114,21 @@ exports.unsafeSet = unsafeSet;
117
114
  */
118
115
  function unsafeClear(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
119
116
  field) {
120
- const name = (0, names_js_1.localName)(field);
117
+ const name = field.localName;
121
118
  if (field.oneof) {
122
- const oneofLocalName = (0, names_js_1.localName)(field.oneof);
119
+ const oneofLocalName = field.oneof.localName;
123
120
  if (target[oneofLocalName].case === name) {
124
121
  target[oneofLocalName] = { case: undefined };
125
122
  }
126
123
  }
124
+ else if (field.presence != IMPLICIT) {
125
+ // Fields with explicit presence have properties on the prototype chain
126
+ // for default / zero values (except for proto3). By deleting their own
127
+ // property, the field is reset.
128
+ delete target[name];
129
+ }
127
130
  else {
131
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
128
132
  switch (field.fieldKind) {
129
133
  case "map":
130
134
  target[name] = {};
@@ -132,19 +136,12 @@ field) {
132
136
  case "list":
133
137
  target[name] = [];
134
138
  break;
135
- case "message":
136
- delete target[name];
139
+ case "enum":
140
+ target[name] = field.enum.values[0].number;
141
+ break;
142
+ case "scalar":
143
+ target[name] = (0, scalar_js_1.scalarZeroValue)(field.scalar, field.longType);
137
144
  break;
138
- default:
139
- if (field.presence == IMPLICIT) {
140
- target[name] =
141
- field.fieldKind == "enum"
142
- ? field.enum.values[0].number
143
- : (0, scalar_js_1.scalarZeroValue)(field.scalar, field.longType);
144
- }
145
- else {
146
- delete target[name];
147
- }
148
145
  }
149
146
  }
150
147
  }
@@ -155,8 +152,7 @@ exports.unsafeClear = unsafeClear;
155
152
  * @private
156
153
  */
157
154
  function unsafeAddListItem(target, field, value) {
158
- const name = (0, names_js_1.localName)(field);
159
- target[name].push(value);
155
+ target[field.localName].push(value);
160
156
  }
161
157
  exports.unsafeAddListItem = unsafeAddListItem;
162
158
  /**
@@ -165,7 +161,6 @@ exports.unsafeAddListItem = unsafeAddListItem;
165
161
  * @private
166
162
  */
167
163
  function unsafeSetMapEntry(target, field, key, value) {
168
- const name = (0, names_js_1.localName)(field);
169
- target[name][key] = value;
164
+ target[field.localName][key] = value;
170
165
  }
171
166
  exports.unsafeSetMapEntry = unsafeSetMapEntry;
@@ -34,9 +34,7 @@ function writeFields(writer, opts, msg) {
34
34
  var _a;
35
35
  for (const f of msg.sortedFields) {
36
36
  if (!msg.isSet(f)) {
37
- if (f.fieldKind != "map" &&
38
- f.fieldKind != "list" &&
39
- f.presence == LEGACY_REQUIRED) {
37
+ if (f.presence == LEGACY_REQUIRED) {
40
38
  throw new Error(`cannot encode field ${msg.desc.typeName}.${f.name} to binary: required field not set`);
41
39
  }
42
40
  continue;
@@ -24,8 +24,8 @@ const index_js_2 = require("./wire/index.js");
24
24
  const extensions_js_1 = require("./extensions.js");
25
25
  // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.LEGACY_REQUIRED: const $name: FeatureSet_FieldPresence.$localName = $number;
26
26
  const LEGACY_REQUIRED = 3;
27
- // bootstrap-inject google.protobuf.FieldDescriptorProto.Label.LABEL_OPTIONAL: const $name: FieldDescriptorProto_Label.$localName = $number;
28
- const LABEL_OPTIONAL = 1;
27
+ // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
28
+ const IMPLICIT = 2;
29
29
  // Default options for serializing to JSON.
30
30
  const jsonWriteDefaults = {
31
31
  emitDefaultValues: false,
@@ -60,15 +60,11 @@ function reflectToJson(msg, opts) {
60
60
  const json = {};
61
61
  for (const f of msg.sortedFields) {
62
62
  if (!msg.isSet(f)) {
63
- if (f.fieldKind != "map" &&
64
- f.fieldKind != "list" &&
65
- f.presence == LEGACY_REQUIRED) {
63
+ if (f.presence == LEGACY_REQUIRED) {
66
64
  throw new Error(`cannot encode field ${msg.desc.typeName}.${f.name} to binary: required field not set`);
67
65
  }
68
- if (!opts.emitDefaultValues) {
69
- continue;
70
- }
71
- if (!canEmitFieldDefaultValue(f)) {
66
+ if (!opts.emitDefaultValues || f.presence !== IMPLICIT) {
67
+ // Fields with implicit presence omit zero values (e.g. empty string) by default
72
68
  continue;
73
69
  }
74
70
  }
@@ -90,8 +86,8 @@ function reflectToJson(msg, opts) {
90
86
  continue;
91
87
  }
92
88
  const value = (0, extensions_js_1.getExtension)(msg.message, extension);
93
- const [, field] = (0, extensions_js_1.createExtensionContainer)(extension);
94
- const jsonValue = fieldToJson(field, value, opts);
89
+ const [container, field] = (0, extensions_js_1.createExtensionContainer)(extension, value);
90
+ const jsonValue = fieldToJson(field, container.get(field), opts);
95
91
  if (jsonValue !== undefined) {
96
92
  json[extension.jsonName] = jsonValue;
97
93
  }
@@ -219,22 +215,6 @@ function scalarToJson(type, value) {
219
215
  return (0, index_js_2.base64Encode)(value);
220
216
  }
221
217
  }
222
- // Decide whether an unset field should be emitted with JSON write option `emitDefaultValues`
223
- function canEmitFieldDefaultValue(field) {
224
- switch (true) {
225
- // oneof fields are never emitted
226
- case field.oneof !== undefined:
227
- // singular message field are allowed to emit JSON null, but we do not
228
- // eslint-disable-next-line no-fallthrough
229
- case field.fieldKind === "message":
230
- // the field uses explicit presence, so we cannot emit a zero value
231
- // eslint-disable-next-line no-fallthrough
232
- case field.proto.label === LABEL_OPTIONAL:
233
- return false;
234
- default:
235
- return true;
236
- }
237
- }
238
218
  function jsonName(f, opts) {
239
219
  return opts.useProtoFieldName ? f.name : f.jsonName;
240
220
  }
@@ -42,10 +42,10 @@ function base64Decode(base64Str) {
42
42
  b = table[base64Str.charCodeAt(i)];
43
43
  if (b === undefined) {
44
44
  switch (base64Str[i]) {
45
- // @ts-ignore TS7029: Fallthrough case in switch
45
+ // @ts-expect-error TS7029: Fallthrough case in switch
46
46
  case "=":
47
47
  groupPos = 0; // reset state when padding found
48
- // @ts-ignore TS7029: Fallthrough case in switch
48
+ // eslint-disable-next-line no-fallthrough
49
49
  case "\n":
50
50
  case "\r":
51
51
  case "\t":
@@ -170,10 +170,12 @@ export declare class BinaryReader {
170
170
  */
171
171
  tag(): [number, WireType];
172
172
  /**
173
- * Skip one element on the wire and return the skipped data.
174
- * Supports WireType.StartGroup since v2.0.0-alpha.23.
173
+ * Skip one element and return the skipped data.
174
+ *
175
+ * When skipping StartGroup, provide the tags field number to check for
176
+ * matching field number in the EndGroup tag.
175
177
  */
176
- skip(wireType: WireType): Uint8Array;
178
+ skip(wireType: WireType, fieldNo?: number): Uint8Array;
177
179
  protected varint64: () => [number, number];
178
180
  /**
179
181
  * Throws error if position in byte array is out of range.
@@ -295,10 +295,12 @@ class BinaryReader {
295
295
  return [fieldNo, wireType];
296
296
  }
297
297
  /**
298
- * Skip one element on the wire and return the skipped data.
299
- * Supports WireType.StartGroup since v2.0.0-alpha.23.
298
+ * Skip one element and return the skipped data.
299
+ *
300
+ * When skipping StartGroup, provide the tags field number to check for
301
+ * matching field number in the EndGroup tag.
300
302
  */
301
- skip(wireType) {
303
+ skip(wireType, fieldNo) {
302
304
  let start = this.pos;
303
305
  switch (wireType) {
304
306
  case WireType.Varint:
@@ -307,11 +309,10 @@ class BinaryReader {
307
309
  }
308
310
  break;
309
311
  // eslint-disable-next-line
310
- // @ts-ignore TS7029: Fallthrough case in switch
312
+ // @ts-expect-error TS7029: Fallthrough case in switch
311
313
  case WireType.Bit64:
312
314
  this.pos += 4;
313
- // eslint-disable-next-line
314
- // @ts-ignore TS7029: Fallthrough case in switch
315
+ // eslint-disable-next-line no-fallthrough
315
316
  case WireType.Bit32:
316
317
  this.pos += 4;
317
318
  break;
@@ -320,10 +321,15 @@ class BinaryReader {
320
321
  this.pos += len;
321
322
  break;
322
323
  case WireType.StartGroup:
323
- // TODO check for matching field numbers in StartGroup / EndGroup tags
324
- let t;
325
- while ((t = this.tag()[1]) !== WireType.EndGroup) {
326
- this.skip(t);
324
+ for (;;) {
325
+ const [fn, wt] = this.tag();
326
+ if (wt === WireType.EndGroup) {
327
+ if (fieldNo !== undefined && fn !== fieldNo) {
328
+ throw new Error("invalid end group tag");
329
+ }
330
+ break;
331
+ }
332
+ this.skip(wt, fn);
327
333
  }
328
334
  break;
329
335
  default:
@@ -1,5 +1,8 @@
1
1
  import type { GenDescFile, GenDescMessage } from "../../../../codegenv1/types.js";
2
2
  import type { Message } from "../../../../types.js";
3
+ /**
4
+ * Describes the file google/protobuf/any.proto.
5
+ */
3
6
  export declare const fileDesc_google_protobuf_any: GenDescFile;
4
7
  /**
5
8
  * `Any` contains an arbitrary serialized protocol buffer message along with a
@@ -133,4 +136,8 @@ export type Any = Message<"google.protobuf.Any"> & {
133
136
  */
134
137
  value: Uint8Array;
135
138
  };
139
+ /**
140
+ * Describes the message google.protobuf.Any.
141
+ * Use `create(AnyDesc)` to create a new message.
142
+ */
136
143
  export declare const AnyDesc: GenDescMessage<Any>;
@@ -16,7 +16,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.AnyDesc = exports.fileDesc_google_protobuf_any = void 0;
17
17
  const file_js_1 = require("../../../../codegenv1/file.js");
18
18
  const message_js_1 = require("../../../../codegenv1/message.js");
19
+ /**
20
+ * Describes the file google/protobuf/any.proto.
21
+ */
19
22
  exports.fileDesc_google_protobuf_any = (0, file_js_1.fileDesc)("Chlnb29nbGUvcHJvdG9idWYvYW55LnByb3RvEg9nb29nbGUucHJvdG9idWYiJgoDQW55EhAKCHR5cGVfdXJsGAEgASgJEg0KBXZhbHVlGAIgASgMQnYKE2NvbS5nb29nbGUucHJvdG9idWZCCEFueVByb3RvUAFaLGdvb2dsZS5nb2xhbmcub3JnL3Byb3RvYnVmL3R5cGVzL2tub3duL2FueXBiogIDR1BCqgIeR29vZ2xlLlByb3RvYnVmLldlbGxLbm93blR5cGVzYgZwcm90bzM");
20
- // Describes the message google.protobuf.Any.
21
- // Use `create(AnyDesc)` to create a new Any.
23
+ /**
24
+ * Describes the message google.protobuf.Any.
25
+ * Use `create(AnyDesc)` to create a new message.
26
+ */
22
27
  exports.AnyDesc = (0, message_js_1.messageDesc)(exports.fileDesc_google_protobuf_any, 0);
@@ -2,6 +2,9 @@ import type { GenDescFile, GenDescMessage } from "../../../../codegenv1/types.js
2
2
  import type { SourceContext } from "./source_context_pb.js";
3
3
  import type { Option, Syntax } from "./type_pb.js";
4
4
  import type { Message } from "../../../../types.js";
5
+ /**
6
+ * Describes the file google/protobuf/api.proto.
7
+ */
5
8
  export declare const fileDesc_google_protobuf_api: GenDescFile;
6
9
  /**
7
10
  * Api is a light-weight descriptor for an API Interface.
@@ -81,6 +84,10 @@ export type Api = Message<"google.protobuf.Api"> & {
81
84
  */
82
85
  syntax: Syntax;
83
86
  };
87
+ /**
88
+ * Describes the message google.protobuf.Api.
89
+ * Use `create(ApiDesc)` to create a new message.
90
+ */
84
91
  export declare const ApiDesc: GenDescMessage<Api>;
85
92
  /**
86
93
  * Method represents a method of an API interface.
@@ -131,6 +138,10 @@ export type Method = Message<"google.protobuf.Method"> & {
131
138
  */
132
139
  syntax: Syntax;
133
140
  };
141
+ /**
142
+ * Describes the message google.protobuf.Method.
143
+ * Use `create(MethodDesc)` to create a new message.
144
+ */
134
145
  export declare const MethodDesc: GenDescMessage<Method>;
135
146
  /**
136
147
  * Declares an API Interface to be included in this interface. The including
@@ -229,4 +240,8 @@ export type Mixin = Message<"google.protobuf.Mixin"> & {
229
240
  */
230
241
  root: string;
231
242
  };
243
+ /**
244
+ * Describes the message google.protobuf.Mixin.
245
+ * Use `create(MixinDesc)` to create a new message.
246
+ */
232
247
  export declare const MixinDesc: GenDescMessage<Mixin>;