@bufbuild/protobuf 1.6.0 → 1.7.0

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 (151) hide show
  1. package/dist/cjs/binary-encoding.js +2 -3
  2. package/dist/cjs/binary-format.d.ts +17 -0
  3. package/dist/cjs/binary-format.js +1 -1
  4. package/dist/cjs/codegen-info.d.ts +4 -4
  5. package/dist/cjs/codegen-info.js +2 -1
  6. package/dist/cjs/create-descriptor-set.d.ts +1 -1
  7. package/dist/cjs/create-descriptor-set.js +56 -50
  8. package/dist/cjs/create-registry-from-desc.d.ts +2 -2
  9. package/dist/cjs/create-registry-from-desc.js +53 -9
  10. package/dist/cjs/create-registry.d.ts +3 -2
  11. package/dist/cjs/create-registry.js +56 -37
  12. package/dist/cjs/descriptor-set.js +1 -1
  13. package/dist/cjs/enum.js +1 -1
  14. package/dist/cjs/extension-accessor.d.ts +35 -0
  15. package/dist/cjs/extension-accessor.js +108 -0
  16. package/dist/cjs/extension.d.ts +24 -0
  17. package/dist/cjs/extension.js +15 -0
  18. package/dist/cjs/field-list.js +1 -1
  19. package/dist/cjs/field.js +1 -1
  20. package/dist/cjs/google/protobuf/any_pb.js +1 -1
  21. package/dist/cjs/google/protobuf/api_pb.js +1 -1
  22. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +1 -1
  23. package/dist/cjs/google/protobuf/descriptor_pb.js +2 -2
  24. package/dist/cjs/google/protobuf/duration_pb.js +1 -1
  25. package/dist/cjs/google/protobuf/empty_pb.js +1 -1
  26. package/dist/cjs/google/protobuf/field_mask_pb.js +1 -1
  27. package/dist/cjs/google/protobuf/source_context_pb.js +1 -1
  28. package/dist/cjs/google/protobuf/struct_pb.js +2 -2
  29. package/dist/cjs/google/protobuf/timestamp_pb.js +1 -1
  30. package/dist/cjs/google/protobuf/type_pb.js +2 -2
  31. package/dist/cjs/google/protobuf/wrappers_pb.js +1 -1
  32. package/dist/cjs/index.d.ts +3 -1
  33. package/dist/cjs/index.js +7 -2
  34. package/dist/cjs/json-format.d.ts +5 -5
  35. package/dist/cjs/json-format.js +1 -1
  36. package/dist/cjs/message-type.js +1 -1
  37. package/dist/cjs/message.js +1 -1
  38. package/dist/cjs/private/assert.js +1 -1
  39. package/dist/cjs/private/binary-format-common.d.ts +2 -3
  40. package/dist/cjs/private/binary-format-common.js +80 -78
  41. package/dist/cjs/private/binary-format-proto2.js +59 -64
  42. package/dist/cjs/private/binary-format-proto3.js +44 -40
  43. package/dist/cjs/private/enum.js +1 -1
  44. package/dist/cjs/private/extensions.d.ts +34 -0
  45. package/dist/cjs/private/extensions.js +86 -0
  46. package/dist/cjs/private/feature-set.d.ts +3 -3
  47. package/dist/cjs/private/feature-set.js +27 -32
  48. package/dist/cjs/private/field-list.js +1 -1
  49. package/dist/cjs/private/field-wrapper.d.ts +2 -2
  50. package/dist/cjs/private/field-wrapper.js +1 -1
  51. package/dist/cjs/private/field.js +1 -1
  52. package/dist/cjs/private/json-format-common.js +202 -156
  53. package/dist/cjs/private/json-format-proto2.js +2 -1
  54. package/dist/cjs/private/json-format-proto3.js +7 -5
  55. package/dist/cjs/private/message-type.js +1 -1
  56. package/dist/cjs/private/names.d.ts +2 -2
  57. package/dist/cjs/private/names.js +3 -2
  58. package/dist/cjs/private/options-map.js +1 -1
  59. package/dist/cjs/private/proto-runtime.d.ts +6 -0
  60. package/dist/cjs/private/proto-runtime.js +5 -1
  61. package/dist/cjs/private/reify-wkt.js +1 -1
  62. package/dist/cjs/private/scalars.js +1 -1
  63. package/dist/cjs/private/text-format.js +1 -1
  64. package/dist/cjs/private/util-common.js +1 -1
  65. package/dist/cjs/private/util.js +1 -1
  66. package/dist/cjs/proto-base64.js +1 -1
  67. package/dist/cjs/proto-delimited.js +1 -1
  68. package/dist/cjs/proto-double.js +1 -1
  69. package/dist/cjs/proto-int64.js +1 -1
  70. package/dist/cjs/proto2.js +1 -1
  71. package/dist/cjs/proto3.js +1 -1
  72. package/dist/cjs/service-type.js +1 -1
  73. package/dist/cjs/to-plain-message.js +1 -1
  74. package/dist/cjs/type-registry.d.ts +14 -0
  75. package/dist/cjs/type-registry.js +1 -1
  76. package/dist/esm/binary-encoding.js +2 -3
  77. package/dist/esm/binary-format.d.ts +17 -0
  78. package/dist/esm/binary-format.js +1 -1
  79. package/dist/esm/codegen-info.d.ts +4 -4
  80. package/dist/esm/codegen-info.js +2 -1
  81. package/dist/esm/create-descriptor-set.d.ts +1 -1
  82. package/dist/esm/create-descriptor-set.js +56 -50
  83. package/dist/esm/create-registry-from-desc.d.ts +2 -2
  84. package/dist/esm/create-registry-from-desc.js +53 -9
  85. package/dist/esm/create-registry.d.ts +3 -2
  86. package/dist/esm/create-registry.js +56 -37
  87. package/dist/esm/descriptor-set.js +1 -1
  88. package/dist/esm/enum.js +1 -1
  89. package/dist/esm/extension-accessor.d.ts +35 -0
  90. package/dist/esm/extension-accessor.js +101 -0
  91. package/dist/esm/extension.d.ts +24 -0
  92. package/dist/esm/extension.js +14 -0
  93. package/dist/esm/field-list.js +1 -1
  94. package/dist/esm/field.js +1 -1
  95. package/dist/esm/google/protobuf/any_pb.js +1 -1
  96. package/dist/esm/google/protobuf/api_pb.js +1 -1
  97. package/dist/esm/google/protobuf/compiler/plugin_pb.js +1 -1
  98. package/dist/esm/google/protobuf/descriptor_pb.js +2 -2
  99. package/dist/esm/google/protobuf/duration_pb.js +1 -1
  100. package/dist/esm/google/protobuf/empty_pb.js +1 -1
  101. package/dist/esm/google/protobuf/field_mask_pb.js +1 -1
  102. package/dist/esm/google/protobuf/source_context_pb.js +1 -1
  103. package/dist/esm/google/protobuf/struct_pb.js +2 -2
  104. package/dist/esm/google/protobuf/timestamp_pb.js +1 -1
  105. package/dist/esm/google/protobuf/type_pb.js +2 -2
  106. package/dist/esm/google/protobuf/wrappers_pb.js +1 -1
  107. package/dist/esm/index.d.ts +3 -1
  108. package/dist/esm/index.js +2 -1
  109. package/dist/esm/json-format.d.ts +5 -5
  110. package/dist/esm/json-format.js +1 -1
  111. package/dist/esm/message-type.js +1 -1
  112. package/dist/esm/message.js +1 -1
  113. package/dist/esm/private/assert.js +1 -1
  114. package/dist/esm/private/binary-format-common.d.ts +2 -3
  115. package/dist/esm/private/binary-format-common.js +80 -77
  116. package/dist/esm/private/binary-format-proto2.js +59 -64
  117. package/dist/esm/private/binary-format-proto3.js +44 -40
  118. package/dist/esm/private/enum.js +1 -1
  119. package/dist/esm/private/extensions.d.ts +34 -0
  120. package/dist/esm/private/extensions.js +81 -0
  121. package/dist/esm/private/feature-set.d.ts +3 -3
  122. package/dist/esm/private/feature-set.js +27 -32
  123. package/dist/esm/private/field-list.js +1 -1
  124. package/dist/esm/private/field-wrapper.d.ts +2 -2
  125. package/dist/esm/private/field-wrapper.js +1 -1
  126. package/dist/esm/private/field.js +1 -1
  127. package/dist/esm/private/json-format-common.js +202 -156
  128. package/dist/esm/private/json-format-proto2.js +2 -1
  129. package/dist/esm/private/json-format-proto3.js +7 -5
  130. package/dist/esm/private/message-type.js +1 -1
  131. package/dist/esm/private/names.d.ts +2 -2
  132. package/dist/esm/private/names.js +3 -2
  133. package/dist/esm/private/options-map.js +1 -1
  134. package/dist/esm/private/proto-runtime.d.ts +6 -0
  135. package/dist/esm/private/proto-runtime.js +5 -1
  136. package/dist/esm/private/reify-wkt.js +1 -1
  137. package/dist/esm/private/scalars.js +1 -1
  138. package/dist/esm/private/text-format.js +1 -1
  139. package/dist/esm/private/util-common.js +1 -1
  140. package/dist/esm/private/util.js +1 -1
  141. package/dist/esm/proto-base64.js +1 -1
  142. package/dist/esm/proto-delimited.js +1 -1
  143. package/dist/esm/proto-double.js +1 -1
  144. package/dist/esm/proto-int64.js +1 -1
  145. package/dist/esm/proto2.js +1 -1
  146. package/dist/esm/proto3.js +1 -1
  147. package/dist/esm/service-type.js +1 -1
  148. package/dist/esm/to-plain-message.js +1 -1
  149. package/dist/esm/type-registry.d.ts +14 -0
  150. package/dist/esm/type-registry.js +1 -1
  151. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -319,8 +319,7 @@ class BinaryReader {
319
319
  this.pos += len;
320
320
  break;
321
321
  case WireType.StartGroup:
322
- // From descriptor.proto: Group type is deprecated, not supported in proto3.
323
- // But we must still be able to parse and treat as unknown.
322
+ // TODO check for matching field numbers in StartGroup / EndGroup tags
324
323
  let t;
325
324
  while ((t = this.tag()[1]) !== WireType.EndGroup) {
326
325
  this.skip(t);
@@ -1,5 +1,6 @@
1
1
  import type { Message } from "./message.js";
2
2
  import type { IBinaryReader, IBinaryWriter, WireType } from "./binary-encoding.js";
3
+ import type { FieldInfo } from "./field.js";
3
4
  /**
4
5
  * BinaryFormat is the contract for serializing messages to and from binary
5
6
  * data. Implementations may be specific to a proto syntax, and can be
@@ -27,10 +28,26 @@ export interface BinaryFormat {
27
28
  * delimitedMessageEncoding is optional for backwards compatibility.
28
29
  */
29
30
  readMessage(message: Message, reader: IBinaryReader, lengthOrEndTagFieldNo: number, options: BinaryReadOptions, delimitedMessageEncoding?: boolean): void;
31
+ /**
32
+ * Parse a field from binary data, and store it in the given target.
33
+ *
34
+ * The target must be an initialized message object, with oneof groups,
35
+ * repeated fields and maps already present.
36
+ */
37
+ readField(target: Record<string, any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
38
+ reader: IBinaryReader, field: FieldInfo, wireType: WireType, options: BinaryReadOptions): void;
30
39
  /**
31
40
  * Serialize a message to binary data.
32
41
  */
33
42
  writeMessage(message: Message, writer: IBinaryWriter, options: BinaryWriteOptions): void;
43
+ /**
44
+ * Serialize a field value to binary data.
45
+ *
46
+ * The value must be an array for repeated fields, a record object for map
47
+ * fields. Only selected oneof fields should be passed to this method.
48
+ */
49
+ writeField(field: FieldInfo, value: any, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
50
+ writer: IBinaryWriter, options: BinaryWriteOptions): void;
34
51
  /**
35
52
  * Retrieve the unknown fields for the given message.
36
53
  *
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -1,14 +1,14 @@
1
1
  import { reifyWkt } from "./private/reify-wkt.js";
2
- import type { DescEnum, DescEnumValue, DescField, DescMessage, DescMethod, DescOneof, DescService } from "./descriptor-set";
2
+ import type { DescEnum, DescEnumValue, DescField, DescExtension, DescMessage, DescMethod, DescOneof, DescService } from "./descriptor-set";
3
3
  import { LongType, ScalarType } from "./field.js";
4
4
  interface CodegenInfo {
5
5
  /**
6
6
  * Name of the runtime library NPM package.
7
7
  */
8
8
  readonly packageName: string;
9
- readonly localName: (desc: DescEnum | DescEnumValue | DescMessage | DescOneof | DescField | DescService | DescMethod) => string;
9
+ readonly localName: (desc: DescEnum | DescEnumValue | DescMessage | DescExtension | DescOneof | DescField | DescService | DescMethod) => string;
10
10
  readonly symbols: Record<RuntimeSymbolName, RuntimeSymbolInfo>;
11
- readonly getUnwrappedFieldType: (field: DescField) => ScalarType | undefined;
11
+ readonly getUnwrappedFieldType: (field: DescField | DescExtension) => ScalarType | undefined;
12
12
  readonly wktSourceFiles: readonly string[];
13
13
  readonly scalarDefaultValue: (type: ScalarType, longType: LongType) => any;
14
14
  /**
@@ -18,7 +18,7 @@ interface CodegenInfo {
18
18
  readonly safeIdentifier: (name: string) => string;
19
19
  readonly safeObjectProperty: (name: string) => string;
20
20
  }
21
- type RuntimeSymbolName = "proto2" | "proto3" | "Message" | "PartialMessage" | "PlainMessage" | "FieldList" | "MessageType" | "BinaryReadOptions" | "BinaryWriteOptions" | "JsonReadOptions" | "JsonWriteOptions" | "JsonValue" | "JsonObject" | "protoDouble" | "protoInt64" | "ScalarType" | "LongType" | "MethodKind" | "MethodIdempotency" | "IMessageTypeRegistry";
21
+ type RuntimeSymbolName = "proto2" | "proto3" | "Message" | "PartialMessage" | "PlainMessage" | "FieldList" | "MessageType" | "Extension" | "BinaryReadOptions" | "BinaryWriteOptions" | "JsonReadOptions" | "JsonWriteOptions" | "JsonValue" | "JsonObject" | "protoDouble" | "protoInt64" | "ScalarType" | "LongType" | "MethodKind" | "MethodIdempotency" | "IMessageTypeRegistry";
22
22
  type RuntimeSymbolInfo = {
23
23
  typeOnly: boolean;
24
24
  publicImportPath: string;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@ exports.codegenInfo = {
36
36
  PlainMessage: { typeOnly: true, privateImportPath: "./message.js", publicImportPath: packageName },
37
37
  FieldList: { typeOnly: true, privateImportPath: "./field-list.js", publicImportPath: packageName },
38
38
  MessageType: { typeOnly: true, privateImportPath: "./message-type.js", publicImportPath: packageName },
39
+ Extension: { typeOnly: true, privateImportPath: "./extension.js", publicImportPath: packageName },
39
40
  BinaryReadOptions: { typeOnly: true, privateImportPath: "./binary-format.js", publicImportPath: packageName },
40
41
  BinaryWriteOptions: { typeOnly: true, privateImportPath: "./binary-format.js", publicImportPath: packageName },
41
42
  JsonReadOptions: { typeOnly: true, privateImportPath: "./json-format.js", publicImportPath: packageName },
@@ -20,7 +20,7 @@ interface CreateDescriptorSetOptions {
20
20
  *
21
21
  * To create a DescriptorSet that provides your language-specific features,
22
22
  * you have to provide a google.protobuf.FeatureSetDefaults message in this
23
- * option.
23
+ * option. It can also specify the minimum and maximum supported edition.
24
24
  *
25
25
  * The defaults can be generated with `protoc` - see the flag
26
26
  * `--experimental_edition_defaults_out`.
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -30,28 +30,36 @@ const feature_set_js_1 = require("./private/feature-set.js");
30
30
  * files in topological order.
31
31
  */
32
32
  function createDescriptorSet(input, options) {
33
- var _a;
34
33
  const cart = {
35
34
  enums: new Map(),
36
35
  messages: new Map(),
37
36
  services: new Map(),
38
37
  extensions: new Map(),
39
38
  mapEntries: new Map(),
40
- resolveFeatures: (0, feature_set_js_1.createFeatureResolver)((_a = options === null || options === void 0 ? void 0 : options.featureSetDefaults) !== null && _a !== void 0 ? _a : feature_set_js_1.featureSetDefaults),
41
39
  };
42
40
  const fileDescriptors = input instanceof descriptor_pb_js_1.FileDescriptorSet
43
41
  ? input.file
44
42
  : input instanceof Uint8Array
45
43
  ? descriptor_pb_js_1.FileDescriptorSet.fromBinary(input).file
46
44
  : input;
47
- const files = fileDescriptors.map((proto) => newFile(proto, cart));
45
+ const resolverByEdition = new Map();
46
+ const files = fileDescriptors.map((proto) => {
47
+ var _a, _b;
48
+ const edition = (_a = proto.edition) !== null && _a !== void 0 ? _a : parseFileSyntax(proto.syntax, proto.edition).edition;
49
+ let resolveFeatures = resolverByEdition.get(edition);
50
+ if (resolveFeatures === undefined) {
51
+ resolveFeatures = (0, feature_set_js_1.createFeatureResolver)((_b = options === null || options === void 0 ? void 0 : options.featureSetDefaults) !== null && _b !== void 0 ? _b : feature_set_js_1.featureSetDefaults, edition);
52
+ resolverByEdition.set(edition, resolveFeatures);
53
+ }
54
+ return newFile(proto, cart, resolveFeatures);
55
+ });
48
56
  return Object.assign({ files }, cart);
49
57
  }
50
58
  exports.createDescriptorSet = createDescriptorSet;
51
59
  /**
52
60
  * Create a descriptor for a file.
53
61
  */
54
- function newFile(proto, cart) {
62
+ function newFile(proto, cart, resolveFeatures) {
55
63
  var _a, _b;
56
64
  (0, assert_js_1.assert)(proto.name, `invalid FileDescriptorProto: missing name`);
57
65
  const file = Object.assign(Object.assign({ kind: "file", proto, deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false }, parseFileSyntax(proto.syntax, proto.edition)), { name: proto.name.replace(/\.proto/, ""), enums: [], messages: [], extensions: [], services: [], toString() {
@@ -70,25 +78,25 @@ function newFile(proto, cart) {
70
78
  },
71
79
  getFeatures() {
72
80
  var _a;
73
- return cart.resolveFeatures(this.edition, (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
81
+ return resolveFeatures((_a = proto.options) === null || _a === void 0 ? void 0 : _a.features);
74
82
  } });
75
83
  cart.mapEntries.clear(); // map entries are local to the file, we can safely discard
76
84
  for (const enumProto of proto.enumType) {
77
- addEnum(enumProto, file, undefined, cart);
85
+ addEnum(enumProto, file, undefined, cart, resolveFeatures);
78
86
  }
79
87
  for (const messageProto of proto.messageType) {
80
- addMessage(messageProto, file, undefined, cart);
88
+ addMessage(messageProto, file, undefined, cart, resolveFeatures);
81
89
  }
82
90
  for (const serviceProto of proto.service) {
83
- addService(serviceProto, file, cart);
91
+ addService(serviceProto, file, cart, resolveFeatures);
84
92
  }
85
- addExtensions(file, cart);
93
+ addExtensions(file, cart, resolveFeatures);
86
94
  for (const mapEntry of cart.mapEntries.values()) {
87
- addFields(mapEntry, cart);
95
+ addFields(mapEntry, cart, resolveFeatures);
88
96
  }
89
97
  for (const message of file.messages) {
90
- addFields(message, cart);
91
- addExtensions(message, cart);
98
+ addFields(message, cart, resolveFeatures);
99
+ addExtensions(message, cart, resolveFeatures);
92
100
  }
93
101
  cart.mapEntries.clear(); // map entries are local to the file, we can safely discard
94
102
  return file;
@@ -98,23 +106,23 @@ function newFile(proto, cart) {
98
106
  * and to our cart.
99
107
  * Recurses into nested types.
100
108
  */
101
- function addExtensions(desc, cart) {
109
+ function addExtensions(desc, cart, resolveFeatures) {
102
110
  switch (desc.kind) {
103
111
  case "file":
104
112
  for (const proto of desc.proto.extension) {
105
- const ext = newExtension(proto, desc, undefined, cart);
113
+ const ext = newExtension(proto, desc, undefined, cart, resolveFeatures);
106
114
  desc.extensions.push(ext);
107
115
  cart.extensions.set(ext.typeName, ext);
108
116
  }
109
117
  break;
110
118
  case "message":
111
119
  for (const proto of desc.proto.extension) {
112
- const ext = newExtension(proto, desc.file, desc, cart);
120
+ const ext = newExtension(proto, desc.file, desc, cart, resolveFeatures);
113
121
  desc.nestedExtensions.push(ext);
114
122
  cart.extensions.set(ext.typeName, ext);
115
123
  }
116
124
  for (const message of desc.nestedMessages) {
117
- addExtensions(message, cart);
125
+ addExtensions(message, cart, resolveFeatures);
118
126
  }
119
127
  break;
120
128
  }
@@ -123,12 +131,12 @@ function addExtensions(desc, cart) {
123
131
  * Create descriptors for fields and oneof groups, and add them to the message.
124
132
  * Recurses into nested types.
125
133
  */
126
- function addFields(message, cart) {
127
- const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message, cart));
134
+ function addFields(message, cart, resolveFeatures) {
135
+ const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message, resolveFeatures));
128
136
  const oneofsSeen = new Set();
129
137
  for (const proto of message.proto.field) {
130
138
  const oneof = findOneof(proto, allOneofs);
131
- const field = newField(proto, message.file, message, oneof, cart);
139
+ const field = newField(proto, message.file, message, oneof, cart, resolveFeatures);
132
140
  message.fields.push(field);
133
141
  if (oneof === undefined) {
134
142
  message.members.push(field);
@@ -145,14 +153,14 @@ function addFields(message, cart) {
145
153
  message.oneofs.push(oneof);
146
154
  }
147
155
  for (const child of message.nestedMessages) {
148
- addFields(child, cart);
156
+ addFields(child, cart, resolveFeatures);
149
157
  }
150
158
  }
151
159
  /**
152
160
  * Create a descriptor for an enumeration, and add it our cart and to the
153
161
  * parent type, if any.
154
162
  */
155
- function addEnum(proto, file, parent, cart) {
163
+ function addEnum(proto, file, parent, cart, resolveFeatures) {
156
164
  var _a, _b, _c;
157
165
  (0, assert_js_1.assert)(proto.name, `invalid EnumDescriptorProto: missing name`);
158
166
  const desc = {
@@ -182,9 +190,8 @@ function addEnum(proto, file, parent, cart) {
182
190
  return findComments(file.proto.sourceCodeInfo, path);
183
191
  },
184
192
  getFeatures() {
185
- var _a, _b, _c;
186
- const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures();
187
- return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features);
193
+ var _a, _b;
194
+ return resolveFeatures((_a = parent === null || parent === void 0 ? void 0 : parent.getFeatures()) !== null && _a !== void 0 ? _a : file.getFeatures(), (_b = proto.options) === null || _b === void 0 ? void 0 : _b.features);
188
195
  },
189
196
  };
190
197
  cart.enums.set(desc.typeName, desc);
@@ -220,7 +227,7 @@ function addEnum(proto, file, parent, cart) {
220
227
  },
221
228
  getFeatures() {
222
229
  var _a;
223
- return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
230
+ return resolveFeatures(desc.getFeatures(), (_a = proto.options) === null || _a === void 0 ? void 0 : _a.features);
224
231
  },
225
232
  });
226
233
  });
@@ -230,7 +237,7 @@ function addEnum(proto, file, parent, cart) {
230
237
  * Create a descriptor for a message, including nested types, and add it to our
231
238
  * cart. Note that this does not create descriptors fields.
232
239
  */
233
- function addMessage(proto, file, parent, cart) {
240
+ function addMessage(proto, file, parent, cart, resolveFeatures) {
234
241
  var _a, _b, _c, _d;
235
242
  (0, assert_js_1.assert)(proto.name, `invalid DescriptorProto: missing name`);
236
243
  const desc = {
@@ -264,9 +271,8 @@ function addMessage(proto, file, parent, cart) {
264
271
  return findComments(file.proto.sourceCodeInfo, path);
265
272
  },
266
273
  getFeatures() {
267
- var _a, _b, _c;
268
- const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures();
269
- return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features);
274
+ var _a, _b;
275
+ return resolveFeatures((_a = parent === null || parent === void 0 ? void 0 : parent.getFeatures()) !== null && _a !== void 0 ? _a : file.getFeatures(), (_b = proto.options) === null || _b === void 0 ? void 0 : _b.features);
270
276
  },
271
277
  };
272
278
  if (((_c = proto.options) === null || _c === void 0 ? void 0 : _c.mapEntry) === true) {
@@ -277,17 +283,17 @@ function addMessage(proto, file, parent, cart) {
277
283
  cart.messages.set(desc.typeName, desc);
278
284
  }
279
285
  for (const enumProto of proto.enumType) {
280
- addEnum(enumProto, file, desc, cart);
286
+ addEnum(enumProto, file, desc, cart, resolveFeatures);
281
287
  }
282
288
  for (const messageProto of proto.nestedType) {
283
- addMessage(messageProto, file, desc, cart);
289
+ addMessage(messageProto, file, desc, cart, resolveFeatures);
284
290
  }
285
291
  }
286
292
  /**
287
293
  * Create a descriptor for a service, including methods, and add it to our
288
294
  * cart.
289
295
  */
290
- function addService(proto, file, cart) {
296
+ function addService(proto, file, cart, resolveFeatures) {
291
297
  var _a, _b;
292
298
  (0, assert_js_1.assert)(proto.name, `invalid ServiceDescriptorProto: missing name`);
293
299
  const desc = {
@@ -310,19 +316,19 @@ function addService(proto, file, cart) {
310
316
  },
311
317
  getFeatures() {
312
318
  var _a;
313
- return cart.resolveFeatures(this.file.edition, this.file.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
319
+ return resolveFeatures(file.getFeatures(), (_a = proto.options) === null || _a === void 0 ? void 0 : _a.features);
314
320
  },
315
321
  };
316
322
  file.services.push(desc);
317
323
  cart.services.set(desc.typeName, desc);
318
324
  for (const methodProto of proto.method) {
319
- desc.methods.push(newMethod(methodProto, desc, cart));
325
+ desc.methods.push(newMethod(methodProto, desc, cart, resolveFeatures));
320
326
  }
321
327
  }
322
328
  /**
323
329
  * Create a descriptor for a method.
324
330
  */
325
- function newMethod(proto, parent, cart) {
331
+ function newMethod(proto, parent, cart, resolveFeatures) {
326
332
  var _a, _b, _c;
327
333
  (0, assert_js_1.assert)(proto.name, `invalid MethodDescriptorProto: missing name`);
328
334
  (0, assert_js_1.assert)(proto.inputType, `invalid MethodDescriptorProto: missing input_type`);
@@ -381,14 +387,14 @@ function newMethod(proto, parent, cart) {
381
387
  },
382
388
  getFeatures() {
383
389
  var _a;
384
- return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
390
+ return resolveFeatures(parent.getFeatures(), (_a = proto.options) === null || _a === void 0 ? void 0 : _a.features);
385
391
  },
386
392
  };
387
393
  }
388
394
  /**
389
395
  * Create a descriptor for a oneof group.
390
396
  */
391
- function newOneof(proto, parent, cart) {
397
+ function newOneof(proto, parent, resolveFeatures) {
392
398
  (0, assert_js_1.assert)(proto.name, `invalid OneofDescriptorProto: missing name`);
393
399
  return {
394
400
  kind: "oneof",
@@ -410,14 +416,14 @@ function newOneof(proto, parent, cart) {
410
416
  },
411
417
  getFeatures() {
412
418
  var _a;
413
- return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
419
+ return resolveFeatures(parent.getFeatures(), (_a = proto.options) === null || _a === void 0 ? void 0 : _a.features);
414
420
  },
415
421
  };
416
422
  }
417
423
  /**
418
424
  * Create a descriptor for a field.
419
425
  */
420
- function newField(proto, file, parent, oneof, cart) {
426
+ function newField(proto, file, parent, oneof, cart, resolveFeatures) {
421
427
  var _a, _b, _c;
422
428
  (0, assert_js_1.assert)(proto.name, `invalid FieldDescriptorProto: missing name`);
423
429
  (0, assert_js_1.assert)(proto.number, `invalid FieldDescriptorProto: missing number`);
@@ -430,8 +436,8 @@ function newField(proto, file, parent, oneof, cart) {
430
436
  parent,
431
437
  oneof,
432
438
  optional: isOptionalField(proto, file.syntax),
433
- packedByDefault: isPackedFieldByDefault(file, proto, cart.resolveFeatures),
434
- packed: isPackedField(file, parent, proto, cart.resolveFeatures),
439
+ packedByDefault: isPackedFieldByDefault(proto, resolveFeatures),
440
+ packed: isPackedField(file, parent, proto, resolveFeatures),
435
441
  jsonName: proto.jsonName === (0, names_js_1.fieldJsonName)(proto.name) ? undefined : proto.jsonName,
436
442
  scalar: undefined,
437
443
  longType: undefined,
@@ -454,7 +460,7 @@ function newField(proto, file, parent, oneof, cart) {
454
460
  },
455
461
  getFeatures() {
456
462
  var _a;
457
- return cart.resolveFeatures(file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
463
+ return resolveFeatures(parent.getFeatures(), (_a = proto.options) === null || _a === void 0 ? void 0 : _a.features);
458
464
  },
459
465
  };
460
466
  const repeated = proto.label === descriptor_pb_js_1.FieldDescriptorProto_Label.REPEATED;
@@ -493,10 +499,10 @@ function newField(proto, file, parent, oneof, cart) {
493
499
  /**
494
500
  * Create a descriptor for an extension field.
495
501
  */
496
- function newExtension(proto, file, parent, cart) {
502
+ function newExtension(proto, file, parent, cart, resolveFeatures) {
497
503
  (0, assert_js_1.assert)(proto.extendee, `invalid FieldDescriptorProto: missing extendee`);
498
504
  const field = newField(proto, file, null, // to safe us many lines of duplicated code, we trick the type system
499
- undefined, cart);
505
+ undefined, cart, resolveFeatures);
500
506
  const extendee = cart.messages.get(trimLeadingDot(proto.extendee));
501
507
  (0, assert_js_1.assert)(extendee, `invalid FieldDescriptorProto: extendee ${proto.extendee} not found`);
502
508
  return Object.assign(Object.assign({}, field), { kind: "extension", typeName: makeTypeName(proto, parent, file), parent,
@@ -521,8 +527,8 @@ function newExtension(proto, file, parent, cart) {
521
527
  return findComments(file.proto.sourceCodeInfo, path);
522
528
  },
523
529
  getFeatures() {
524
- var _a, _b;
525
- return cart.resolveFeatures(this.file.edition, ((_a = this.parent) !== null && _a !== void 0 ? _a : this.file).getFeatures(), (_b = this.proto.options) === null || _b === void 0 ? void 0 : _b.features);
530
+ var _a;
531
+ return resolveFeatures((parent !== null && parent !== void 0 ? parent : file).getFeatures(), (_a = proto.options) === null || _a === void 0 ? void 0 : _a.features);
526
532
  } });
527
533
  }
528
534
  /**
@@ -678,8 +684,8 @@ function isOptionalField(proto, syntax) {
678
684
  * are unpacked by default. With editions, the default is whatever the edition
679
685
  * specifies as a default. In edition 2023, fields are packed by default.
680
686
  */
681
- function isPackedFieldByDefault(file, proto, resolveFeatures) {
682
- const { repeatedFieldEncoding } = resolveFeatures(file.edition);
687
+ function isPackedFieldByDefault(proto, resolveFeatures) {
688
+ const { repeatedFieldEncoding } = resolveFeatures();
683
689
  if (repeatedFieldEncoding != descriptor_pb_js_1.FeatureSet_RepeatedFieldEncoding.PACKED) {
684
690
  return false;
685
691
  }
@@ -720,7 +726,7 @@ function isPackedField(file, parent, proto, resolveFeatures) {
720
726
  case descriptor_pb_js_1.Edition.EDITION_PROTO3:
721
727
  return (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.packed) !== null && _d !== void 0 ? _d : true;
722
728
  default: {
723
- const { repeatedFieldEncoding } = resolveFeatures(file.edition, (_e = parent === null || parent === void 0 ? void 0 : parent.getFeatures()) !== null && _e !== void 0 ? _e : file.getFeatures(), (_f = proto.options) === null || _f === void 0 ? void 0 : _f.features);
729
+ const { repeatedFieldEncoding } = resolveFeatures((_e = parent === null || parent === void 0 ? void 0 : parent.getFeatures()) !== null && _e !== void 0 ? _e : file.getFeatures(), (_f = proto.options) === null || _f === void 0 ? void 0 : _f.features);
724
730
  return (repeatedFieldEncoding == descriptor_pb_js_1.FeatureSet_RepeatedFieldEncoding.PACKED);
725
731
  }
726
732
  }
@@ -1,4 +1,4 @@
1
- import type { IEnumTypeRegistry, IMessageTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
1
+ import type { IEnumTypeRegistry, IExtensionRegistry, IMessageTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
2
2
  import { FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
3
3
  import type { DescriptorSet } from "./descriptor-set.js";
4
4
  /**
@@ -12,4 +12,4 @@ import type { DescriptorSet } from "./descriptor-set.js";
12
12
  * By default, all well-known types with a specialized JSON representation
13
13
  * are replaced with their generated counterpart in this package.
14
14
  */
15
- export declare function createRegistryFromDescriptors(input: DescriptorSet | FileDescriptorSet | Uint8Array, replaceWkt?: boolean): IMessageTypeRegistry & IEnumTypeRegistry & IServiceTypeRegistry;
15
+ export declare function createRegistryFromDescriptors(input: DescriptorSet | FileDescriptorSet | Uint8Array, replaceWkt?: boolean): IMessageTypeRegistry & IEnumTypeRegistry & IExtensionRegistry & IServiceTypeRegistry;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -67,15 +67,17 @@ function createRegistryFromDescriptors(input, replaceWkt = true) {
67
67
  const set = input instanceof Uint8Array || input instanceof descriptor_pb_js_1.FileDescriptorSet
68
68
  ? (0, create_descriptor_set_js_1.createDescriptorSet)(input)
69
69
  : input;
70
- const enums = {};
71
- const messages = {};
70
+ const enums = new Map();
71
+ const messages = new Map();
72
+ const extensions = new Map();
73
+ const extensionsByExtendee = new Map();
72
74
  const services = {};
73
75
  if (replaceWkt) {
74
76
  for (const mt of wkMessages) {
75
- messages[mt.typeName] = mt;
77
+ messages.set(mt.typeName, mt);
76
78
  }
77
79
  for (const et of wkEnums) {
78
- enums[et.typeName] = et;
80
+ enums.set(et.typeName, et);
79
81
  }
80
82
  }
81
83
  return {
@@ -83,7 +85,7 @@ function createRegistryFromDescriptors(input, replaceWkt = true) {
83
85
  * May raise an error on invalid descriptors.
84
86
  */
85
87
  findEnum(typeName) {
86
- const existing = enums[typeName];
88
+ const existing = enums.get(typeName);
87
89
  if (existing) {
88
90
  return existing;
89
91
  }
@@ -97,14 +99,14 @@ function createRegistryFromDescriptors(input, replaceWkt = true) {
97
99
  name: u.name,
98
100
  localName: (0, names_js_1.localName)(u),
99
101
  })), {});
100
- enums[typeName] = type;
102
+ enums.set(typeName, type);
101
103
  return type;
102
104
  },
103
105
  /**
104
106
  * May raise an error on invalid descriptors.
105
107
  */
106
108
  findMessage(typeName) {
107
- const existing = messages[typeName];
109
+ const existing = messages.get(typeName);
108
110
  if (existing) {
109
111
  return existing;
110
112
  }
@@ -117,7 +119,7 @@ function createRegistryFromDescriptors(input, replaceWkt = true) {
117
119
  const type = runtime.makeMessageType(typeName, () => fields, {
118
120
  localName: (0, names_js_1.localName)(desc),
119
121
  });
120
- messages[typeName] = type;
122
+ messages.set(typeName, type);
121
123
  for (const field of desc.fields) {
122
124
  const fieldInfo = makeFieldInfo(field, this);
123
125
  fields.push(fieldInfo);
@@ -157,12 +159,54 @@ function createRegistryFromDescriptors(input, replaceWkt = true) {
157
159
  methods,
158
160
  });
159
161
  },
162
+ /**
163
+ * May raise an error on invalid descriptors.
164
+ */
165
+ findExtensionFor(typeName, no) {
166
+ var _a;
167
+ if (!set.messages.has(typeName)) {
168
+ return undefined;
169
+ }
170
+ let extensionsByNo = extensionsByExtendee.get(typeName);
171
+ if (!extensionsByNo) {
172
+ // maintain a lookup for extension desc by number
173
+ extensionsByNo = new Map();
174
+ extensionsByExtendee.set(typeName, extensionsByNo);
175
+ for (const desc of set.extensions.values()) {
176
+ if (desc.extendee.typeName == typeName) {
177
+ extensionsByNo.set(desc.number, desc);
178
+ }
179
+ }
180
+ }
181
+ const desc = (_a = extensionsByExtendee.get(typeName)) === null || _a === void 0 ? void 0 : _a.get(no);
182
+ return desc ? this.findExtension(desc.typeName) : undefined;
183
+ },
184
+ /**
185
+ * May raise an error on invalid descriptors.
186
+ */
187
+ findExtension(typeName) {
188
+ const existing = extensions.get(typeName);
189
+ if (existing) {
190
+ return existing;
191
+ }
192
+ const desc = set.extensions.get(typeName);
193
+ if (!desc) {
194
+ return undefined;
195
+ }
196
+ const extendee = this.findMessage(desc.extendee.typeName);
197
+ (0, assert_js_1.assert)(extendee, `message "${desc.extendee.typeName}" for ${desc.toString()} not found`);
198
+ const runtime = desc.file.syntax == "proto3" ? proto3_js_1.proto3 : proto2_js_1.proto2;
199
+ const ext = runtime.makeExtension(typeName, extendee, makeFieldInfo(desc, this));
200
+ extensions.set(typeName, ext);
201
+ return ext;
202
+ },
160
203
  };
161
204
  }
162
205
  exports.createRegistryFromDescriptors = createRegistryFromDescriptors;
163
206
  function makeFieldInfo(desc, resolver) {
164
207
  switch (desc.fieldKind) {
165
208
  case "map":
209
+ (0, assert_js_1.assert)(desc.kind == "field"); // maps are not allowed for extensions
166
210
  return makeMapFieldInfo(desc, resolver);
167
211
  case "message":
168
212
  return makeMessageFieldInfo(desc, resolver);
@@ -1,8 +1,9 @@
1
1
  import type { MessageType } from "./message-type.js";
2
2
  import type { EnumType } from "./enum.js";
3
3
  import type { ServiceType } from "./service-type.js";
4
- import type { IMessageTypeRegistry, IEnumTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
4
+ import type { IEnumTypeRegistry, IExtensionRegistry, IMessageTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
5
+ import type { Extension } from "./extension.js";
5
6
  /**
6
7
  * Create a new registry from the given types.
7
8
  */
8
- export declare function createRegistry(...types: Array<MessageType | EnumType | ServiceType>): IMessageTypeRegistry & IEnumTypeRegistry & IServiceTypeRegistry;
9
+ export declare function createRegistry(...types: Array<MessageType | EnumType | ServiceType | Extension>): IMessageTypeRegistry & IEnumTypeRegistry & IExtensionRegistry & IServiceTypeRegistry;