@bufbuild/protobuf 1.4.2 → 1.5.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.
Files changed (47) hide show
  1. package/dist/cjs/binary-encoding.d.ts +3 -4
  2. package/dist/cjs/binary-encoding.js +3 -4
  3. package/dist/cjs/binary-format.d.ts +10 -1
  4. package/dist/cjs/create-descriptor-set.d.ts +19 -5
  5. package/dist/cjs/create-descriptor-set.js +157 -223
  6. package/dist/cjs/create-registry-from-desc.js +1 -0
  7. package/dist/cjs/descriptor-set.d.ts +47 -5
  8. package/dist/cjs/field.d.ts +36 -2
  9. package/dist/cjs/google/protobuf/any_pb.js +1 -1
  10. package/dist/cjs/google/protobuf/descriptor_pb.js +1 -1
  11. package/dist/cjs/google/protobuf/struct_pb.js +1 -1
  12. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  13. package/dist/cjs/private/binary-format-common.d.ts +3 -2
  14. package/dist/cjs/private/binary-format-common.js +36 -15
  15. package/dist/cjs/private/binary-format-proto2.js +2 -2
  16. package/dist/cjs/private/binary-format-proto3.js +2 -2
  17. package/dist/cjs/private/feature-set.d.ts +21 -0
  18. package/dist/cjs/private/feature-set.js +107 -0
  19. package/dist/cjs/private/text-format.d.ts +4 -0
  20. package/dist/cjs/private/text-format.js +189 -0
  21. package/dist/cjs/proto-delimited.js +3 -2
  22. package/dist/cjs/proto2.js +7 -3
  23. package/dist/cjs/proto3.js +14 -10
  24. package/dist/esm/binary-encoding.d.ts +3 -4
  25. package/dist/esm/binary-encoding.js +3 -4
  26. package/dist/esm/binary-format.d.ts +10 -1
  27. package/dist/esm/create-descriptor-set.d.ts +19 -5
  28. package/dist/esm/create-descriptor-set.js +157 -222
  29. package/dist/esm/create-registry-from-desc.js +2 -1
  30. package/dist/esm/descriptor-set.d.ts +47 -5
  31. package/dist/esm/field.d.ts +36 -2
  32. package/dist/esm/google/protobuf/any_pb.js +1 -1
  33. package/dist/esm/google/protobuf/descriptor_pb.js +1 -1
  34. package/dist/esm/google/protobuf/struct_pb.js +1 -1
  35. package/dist/esm/google/protobuf/type_pb.js +1 -1
  36. package/dist/esm/private/binary-format-common.d.ts +3 -2
  37. package/dist/esm/private/binary-format-common.js +36 -15
  38. package/dist/esm/private/binary-format-proto2.js +2 -2
  39. package/dist/esm/private/binary-format-proto3.js +2 -2
  40. package/dist/esm/private/feature-set.d.ts +21 -0
  41. package/dist/esm/private/feature-set.js +103 -0
  42. package/dist/esm/private/text-format.d.ts +4 -0
  43. package/dist/esm/private/text-format.js +184 -0
  44. package/dist/esm/proto-delimited.js +3 -2
  45. package/dist/esm/proto2.js +7 -3
  46. package/dist/esm/proto3.js +14 -10
  47. package/package.json +11 -7
@@ -1,6 +1,7 @@
1
- import type { DescriptorProto, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto, FileDescriptorProto, MethodDescriptorProto, OneofDescriptorProto, ServiceDescriptorProto } from "./google/protobuf/descriptor_pb.js";
2
- import type { ScalarType, LongType } from "./field.js";
1
+ import type { DescriptorProto, Edition, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto, FileDescriptorProto, MethodDescriptorProto, OneofDescriptorProto, ServiceDescriptorProto } from "./google/protobuf/descriptor_pb.js";
2
+ import type { LongType, ScalarType } from "./field.js";
3
3
  import type { MethodIdempotency, MethodKind } from "./service-type.js";
4
+ import type { MergedFeatureSet } from "./private/feature-set.js";
4
5
  /**
5
6
  * DescriptorSet provides a convenient interface for working with a set
6
7
  * of google.protobuf.FileDescriptorProto.
@@ -50,7 +51,12 @@ export interface DescFile {
50
51
  /**
51
52
  * The syntax specified in the protobuf source.
52
53
  */
53
- readonly syntax: "proto3" | "proto2";
54
+ readonly syntax: "proto3" | "proto2" | "editions";
55
+ /**
56
+ * The edition of the protobuf file. Will be EDITION_PROTO2 for syntax="proto2",
57
+ * EDITION_PROTO3 for syntax="proto3";
58
+ */
59
+ readonly edition: Exclude<Edition, Edition.EDITION_1_TEST_ONLY | Edition.EDITION_2_TEST_ONLY | Edition.EDITION_99997_TEST_ONLY | Edition.EDITION_99998_TEST_ONLY | Edition.EDITION_99999_TEST_ONLY>;
54
60
  /**
55
61
  * The name of the file, excluding the .proto suffix.
56
62
  * For a protobuf file `foo/bar.proto`, this is `foo/bar`.
@@ -91,6 +97,10 @@ export interface DescFile {
91
97
  * Get comments on the package element in the protobuf source.
92
98
  */
93
99
  getPackageComments(): DescComments;
100
+ /**
101
+ * Get the edition features for this protobuf element.
102
+ */
103
+ getFeatures(): MergedFeatureSet;
94
104
  toString(): string;
95
105
  }
96
106
  /**
@@ -135,6 +145,10 @@ export interface DescEnum {
135
145
  * Get comments on the element in the protobuf source.
136
146
  */
137
147
  getComments(): DescComments;
148
+ /**
149
+ * Get the edition features for this protobuf element.
150
+ */
151
+ getFeatures(): MergedFeatureSet;
138
152
  toString(): string;
139
153
  }
140
154
  /**
@@ -171,6 +185,10 @@ export interface DescEnumValue {
171
185
  * Get comments on the element in the protobuf source.
172
186
  */
173
187
  getComments(): DescComments;
188
+ /**
189
+ * Get the edition features for this protobuf element.
190
+ */
191
+ getFeatures(): MergedFeatureSet;
174
192
  toString(): string;
175
193
  }
176
194
  /**
@@ -234,6 +252,10 @@ export interface DescMessage {
234
252
  * Get comments on the element in the protobuf source.
235
253
  */
236
254
  getComments(): DescComments;
255
+ /**
256
+ * Get the edition features for this protobuf element.
257
+ */
258
+ getFeatures(): MergedFeatureSet;
237
259
  toString(): string;
238
260
  }
239
261
  /**
@@ -290,10 +312,14 @@ interface DescFieldCommon {
290
312
  */
291
313
  readonly packed: boolean;
292
314
  /**
293
- * Is this field packed by default? Only valid for enum fields, and for
294
- * scalar fields except BYTES and STRING.
315
+ * Is this field packed by default? Only valid for repeated enum fields, and
316
+ * for repeated scalar fields except BYTES and STRING.
317
+ *
295
318
  * In proto3 syntax, fields are packed by default. In proto2 syntax, fields
296
319
  * are unpacked by default.
320
+ *
321
+ * With editions, the default is whatever the edition specifies as a default.
322
+ * In edition 2023, fields are packed by default.
297
323
  */
298
324
  readonly packedByDefault: boolean;
299
325
  /**
@@ -318,6 +344,10 @@ interface DescFieldCommon {
318
344
  * protobuf source.
319
345
  */
320
346
  declarationString(): string;
347
+ /**
348
+ * Get the edition features for this protobuf element.
349
+ */
350
+ getFeatures(): MergedFeatureSet;
321
351
  toString(): string;
322
352
  }
323
353
  interface DescFieldScalar {
@@ -534,6 +564,10 @@ export interface DescOneof {
534
564
  * Get comments on the element in the protobuf source.
535
565
  */
536
566
  getComments(): DescComments;
567
+ /**
568
+ * Get the edition features for this protobuf element.
569
+ */
570
+ getFeatures(): MergedFeatureSet;
537
571
  toString(): string;
538
572
  }
539
573
  /**
@@ -569,6 +603,10 @@ export interface DescService {
569
603
  * Get comments on the element in the protobuf source.
570
604
  */
571
605
  getComments(): DescComments;
606
+ /**
607
+ * Get the edition features for this protobuf element.
608
+ */
609
+ getFeatures(): MergedFeatureSet;
572
610
  toString(): string;
573
611
  }
574
612
  /**
@@ -612,6 +650,10 @@ export interface DescMethod {
612
650
  * Get comments on the element in the protobuf source.
613
651
  */
614
652
  getComments(): DescComments;
653
+ /**
654
+ * Get the edition features for this protobuf element.
655
+ */
656
+ getFeatures(): MergedFeatureSet;
615
657
  toString(): string;
616
658
  }
617
659
  /**
@@ -23,6 +23,7 @@ import type { MessageType } from "./message-type.js";
23
23
  *
24
24
  * - "oneof": If the field is member of a oneof group.
25
25
  * - "default": Only proto2: An explicit default value.
26
+ * - "delimited": Only proto2: Use the tag-delimited group encoding.
26
27
  */
27
28
  export type FieldInfo = fiRules<fiScalar> | fiRules<fiEnum> | fiRules<fiMessage> | fiRules<fiMap>;
28
29
  /**
@@ -46,6 +47,7 @@ export interface OneofInfo {
46
47
  readonly packed: false;
47
48
  readonly opt: false;
48
49
  readonly default: undefined;
50
+ readonly delimited?: undefined;
49
51
  readonly fields: readonly FieldInfo[];
50
52
  /**
51
53
  * Return field information by local name.
@@ -119,6 +121,13 @@ interface fiScalar extends fiShared {
119
121
  * Only proto2: An explicit default value.
120
122
  */
121
123
  readonly default: number | boolean | string | bigint | Uint8Array | undefined;
124
+ /**
125
+ * Serialize this message with the delimited format, also known as group
126
+ * encoding, as opposed to the standard length prefix.
127
+ *
128
+ * Only valid for message fields.
129
+ */
130
+ readonly delimited?: undefined;
122
131
  }
123
132
  interface fiMessage extends fiShared {
124
133
  readonly kind: "message";
@@ -138,6 +147,13 @@ interface fiMessage extends fiShared {
138
147
  * An explicit default value (only proto2). Never set for messages.
139
148
  */
140
149
  readonly default: undefined;
150
+ /**
151
+ * Serialize this message with the delimited format, also known as group
152
+ * encoding, as opposed to the standard length prefix.
153
+ *
154
+ * Only valid for message fields.
155
+ */
156
+ readonly delimited?: boolean;
141
157
  }
142
158
  interface fiEnum extends fiShared {
143
159
  readonly kind: "enum";
@@ -163,6 +179,13 @@ interface fiEnum extends fiShared {
163
179
  * Only proto2: An explicit default value.
164
180
  */
165
181
  readonly default: number | undefined;
182
+ /**
183
+ * Serialize this message with the delimited format, also known as group
184
+ * encoding, as opposed to the standard length prefix.
185
+ *
186
+ * Only valid for message fields.
187
+ */
188
+ readonly delimited?: undefined;
166
189
  }
167
190
  interface fiMap extends fiShared {
168
191
  readonly kind: "map";
@@ -199,6 +222,13 @@ interface fiMap extends fiShared {
199
222
  * An explicit default value (only proto2). Never set for maps.
200
223
  */
201
224
  readonly default: undefined;
225
+ /**
226
+ * Serialize this message with the delimited format, also known as group
227
+ * encoding, as opposed to the standard length prefix.
228
+ *
229
+ * Only valid for message fields.
230
+ */
231
+ readonly delimited?: undefined;
202
232
  }
203
233
  type fiRules<T> = Omit<T, "oneof" | "repeat" | "repeated" | "packed" | "opt"> & ({
204
234
  readonly repeated: false;
@@ -221,7 +251,7 @@ type fiRules<T> = Omit<T, "oneof" | "repeat" | "repeated" | "packed" | "opt"> &
221
251
  readonly opt: false;
222
252
  readonly oneof: OneofInfo;
223
253
  });
224
- type fiPartialRules<T extends fiScalar | fiMap | fiEnum | fiMessage> = Omit<T, "jsonName" | "localName" | "oneof" | "repeat" | "repeated" | "packed" | "opt" | "default" | "L"> & ({
254
+ type fiPartialRules<T extends fiScalar | fiMap | fiEnum | fiMessage> = Omit<T, "jsonName" | "localName" | "oneof" | "repeat" | "repeated" | "packed" | "opt" | "default" | "L" | "delimited"> & ({
225
255
  readonly jsonName?: string;
226
256
  readonly repeated?: false;
227
257
  readonly packed?: false;
@@ -229,6 +259,7 @@ type fiPartialRules<T extends fiScalar | fiMap | fiEnum | fiMessage> = Omit<T, "
229
259
  readonly oneof?: undefined;
230
260
  default?: T["default"];
231
261
  L?: LongType;
262
+ delimited?: boolean;
232
263
  } | {
233
264
  readonly jsonName?: string;
234
265
  readonly repeated?: false;
@@ -237,6 +268,7 @@ type fiPartialRules<T extends fiScalar | fiMap | fiEnum | fiMessage> = Omit<T, "
237
268
  readonly oneof?: undefined;
238
269
  default?: T["default"];
239
270
  L?: LongType;
271
+ delimited?: boolean;
240
272
  } | {
241
273
  readonly jsonName?: string;
242
274
  readonly repeated?: boolean;
@@ -245,6 +277,7 @@ type fiPartialRules<T extends fiScalar | fiMap | fiEnum | fiMessage> = Omit<T, "
245
277
  readonly oneof?: undefined;
246
278
  default?: T["default"];
247
279
  L?: LongType;
280
+ delimited?: boolean;
248
281
  } | {
249
282
  readonly jsonName?: string;
250
283
  readonly repeated?: false;
@@ -253,6 +286,7 @@ type fiPartialRules<T extends fiScalar | fiMap | fiEnum | fiMessage> = Omit<T, "
253
286
  readonly oneof: string;
254
287
  default?: T["default"];
255
288
  L?: LongType;
289
+ delimited?: boolean;
256
290
  });
257
291
  /**
258
292
  * Scalar value types. This is a subset of field types declared by protobuf
@@ -273,7 +307,7 @@ export declare enum ScalarType {
273
307
  UINT32 = 13,
274
308
  SFIXED32 = 15,
275
309
  SFIXED64 = 16,
276
- SINT32 = 17,
310
+ SINT32 = 17,// Uses ZigZag encoding.
277
311
  SINT64 = 18
278
312
  }
279
313
  /**
@@ -237,7 +237,7 @@ export class Any extends Message {
237
237
  throw new Error(`invalid type url: ${url}`);
238
238
  }
239
239
  const slash = url.lastIndexOf("/");
240
- const name = slash > 0 ? url.substring(slash + 1) : url;
240
+ const name = slash >= 0 ? url.substring(slash + 1) : url;
241
241
  if (!name.length) {
242
242
  throw new Error(`invalid type url: ${url}`);
243
243
  }
@@ -18,7 +18,7 @@
18
18
  // The messages in this file describe the definitions found in .proto files.
19
19
  // A valid .proto file can be translated directly to a FileDescriptorProto
20
20
  // without any other information (e.g. without reading its imports).
21
- // @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
21
+ // @generated by protoc-gen-es v1.5.1 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
22
22
  // @generated from file google/protobuf/descriptor.proto (package google.protobuf, syntax proto2)
23
23
  /* eslint-disable */
24
24
  import { proto2 } from "../../proto2.js";
@@ -11,7 +11,7 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- // @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
14
+ // @generated by protoc-gen-es v1.5.1 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
15
15
  // @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3)
16
16
  /* eslint-disable */
17
17
  import { proto3 } from "../../proto3.js";
@@ -11,7 +11,7 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- // @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
14
+ // @generated by protoc-gen-es v1.5.1 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
15
15
  // @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3)
16
16
  /* eslint-disable */
17
17
  import { proto3 } from "../../proto3.js";
@@ -2,12 +2,13 @@ import type { IBinaryReader, IBinaryWriter } from "../binary-encoding.js";
2
2
  import type { BinaryFormat, BinaryWriteOptions } from "../binary-format.js";
3
3
  import type { FieldInfo } from "../field.js";
4
4
  import { ScalarType } from "../field.js";
5
- import type { MessageType } from "../message-type.js";
6
5
  export declare function makeBinaryFormatCommon(): Omit<BinaryFormat, "writeMessage">;
7
6
  export declare function readScalarLTString(reader: IBinaryReader, type: ScalarType): any;
8
7
  export declare function writeMapEntry(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
9
8
  kind: "map";
10
9
  }, key: any, value: any): void;
11
- export declare function writeMessageField(writer: IBinaryWriter, options: BinaryWriteOptions, type: MessageType, fieldNo: number, value: any): void;
10
+ export declare function writeMessageField(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
11
+ kind: "message";
12
+ }, value: any): void;
12
13
  export declare function writeScalar(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: any, emitIntrinsicDefault: boolean): void;
13
14
  export declare function writePacked(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: any[]): void;
@@ -62,11 +62,19 @@ export function makeBinaryFormatCommon() {
62
62
  }
63
63
  m[unknownFieldsSymbol].push({ no, wireType, data });
64
64
  },
65
- readMessage(message, reader, length, options) {
65
+ readMessage(message, reader, lengthOrEndTagFieldNo, options, delimitedMessageEncoding) {
66
66
  const type = message.getType();
67
- const end = length === undefined ? reader.len : reader.pos + length;
67
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
68
+ const end = delimitedMessageEncoding
69
+ ? reader.len
70
+ : reader.pos + lengthOrEndTagFieldNo;
71
+ let fieldNo, wireType;
68
72
  while (reader.pos < end) {
69
- const [fieldNo, wireType] = reader.tag(), field = type.fields.find(fieldNo);
73
+ [fieldNo, wireType] = reader.tag();
74
+ if (wireType == WireType.EndGroup) {
75
+ break;
76
+ }
77
+ const field = type.fields.find(fieldNo);
70
78
  if (!field) {
71
79
  const data = reader.skip(wireType);
72
80
  if (options.readUnknownFields) {
@@ -114,14 +122,14 @@ export function makeBinaryFormatCommon() {
114
122
  const messageType = field.T;
115
123
  if (repeated) {
116
124
  // safe to assume presence of array, oneof cannot contain repeated values
117
- target[localName].push(readMessageField(reader, new messageType(), options));
125
+ target[localName].push(readMessageField(reader, new messageType(), options, field));
118
126
  }
119
127
  else {
120
128
  if (target[localName] instanceof Message) {
121
- readMessageField(reader, target[localName], options);
129
+ readMessageField(reader, target[localName], options, field);
122
130
  }
123
131
  else {
124
- target[localName] = readMessageField(reader, new messageType(), options);
132
+ target[localName] = readMessageField(reader, new messageType(), options, field);
125
133
  if (messageType.fieldWrapper &&
126
134
  !field.oneof &&
127
135
  !field.repeated) {
@@ -137,14 +145,20 @@ export function makeBinaryFormatCommon() {
137
145
  break;
138
146
  }
139
147
  }
148
+ if (delimitedMessageEncoding && // eslint-disable-line @typescript-eslint/strict-boolean-expressions
149
+ (wireType != WireType.EndGroup || fieldNo !== lengthOrEndTagFieldNo)) {
150
+ throw new Error(`invalid end group tag`);
151
+ }
140
152
  },
141
153
  };
142
154
  }
143
155
  // Read a message, avoiding MessageType.fromBinary() to re-use the
144
156
  // BinaryReadOptions and the IBinaryReader.
145
- function readMessageField(reader, message, options) {
157
+ function readMessageField(reader, message, options, field) {
146
158
  const format = message.getType().runtime.bin;
147
- format.readMessage(message, reader, reader.uint32(), options);
159
+ const delimited = field === null || field === void 0 ? void 0 : field.delimited;
160
+ format.readMessage(message, reader, delimited ? field === null || field === void 0 ? void 0 : field.no : reader.uint32(), // eslint-disable-line @typescript-eslint/strict-boolean-expressions
161
+ options, delimited);
148
162
  return message;
149
163
  }
150
164
  // Read a map field, expecting key field = 1, value field = 2
@@ -166,7 +180,7 @@ function readMapEntry(field, reader, options) {
166
180
  val = reader.int32();
167
181
  break;
168
182
  case "message":
169
- val = readMessageField(reader, new field.V.T(), options);
183
+ val = readMessageField(reader, new field.V.T(), options, undefined);
170
184
  break;
171
185
  }
172
186
  break;
@@ -269,17 +283,24 @@ export function writeMapEntry(writer, options, field, key, value) {
269
283
  writeScalar(writer, ScalarType.INT32, 2, value, true);
270
284
  break;
271
285
  case "message":
272
- writeMessageField(writer, options, field.V.T, 2, value);
286
+ writer.tag(2, WireType.LengthDelimited).bytes(value.toBinary(options));
273
287
  break;
274
288
  }
275
289
  writer.join();
276
290
  }
277
- export function writeMessageField(writer, options, type, fieldNo, value) {
291
+ export function writeMessageField(writer, options, field, value) {
278
292
  if (value !== undefined) {
279
- const message = wrapField(type, value);
280
- writer
281
- .tag(fieldNo, WireType.LengthDelimited)
282
- .bytes(message.toBinary(options));
293
+ const message = wrapField(field.T, value);
294
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
295
+ if (field === null || field === void 0 ? void 0 : field.delimited)
296
+ writer
297
+ .tag(field.no, WireType.StartGroup)
298
+ .raw(message.toBinary(options))
299
+ .tag(field.no, WireType.EndGroup);
300
+ else
301
+ writer
302
+ .tag(field.no, WireType.LengthDelimited)
303
+ .bytes(message.toBinary(options));
283
304
  }
284
305
  }
285
306
  export function writeScalar(writer, type, fieldNo, value, emitIntrinsicDefault) {
@@ -62,11 +62,11 @@ export function makeBinaryFormatProto2() {
62
62
  case "message":
63
63
  if (repeated) {
64
64
  for (const item of value) {
65
- writeMessageField(writer, options, field.T, field.no, item);
65
+ writeMessageField(writer, options, field, item);
66
66
  }
67
67
  }
68
68
  else {
69
- writeMessageField(writer, options, field.T, field.no, value);
69
+ writeMessageField(writer, options, field, value);
70
70
  }
71
71
  break;
72
72
  case "map":
@@ -53,11 +53,11 @@ export function makeBinaryFormatProto3() {
53
53
  case "message":
54
54
  if (repeated) {
55
55
  for (const item of value) {
56
- writeMessageField(writer, options, field.T, field.no, item);
56
+ writeMessageField(writer, options, field, item);
57
57
  }
58
58
  }
59
59
  else {
60
- writeMessageField(writer, options, field.T, field.no, value);
60
+ writeMessageField(writer, options, field, value);
61
61
  }
62
62
  break;
63
63
  case "map":
@@ -0,0 +1,21 @@
1
+ import { Edition, FeatureSet, FeatureSetDefaults } from "../google/protobuf/descriptor_pb.js";
2
+ /**
3
+ * Static edition feature defaults supported by @bufbuild/protobuf.
4
+ */
5
+ export declare const featureSetDefaults: FeatureSetDefaults;
6
+ /**
7
+ * A merged google.protobuf.FeaturesSet, with all fields guaranteed to be set.
8
+ */
9
+ export type MergedFeatureSet = FeatureSet & Required<FeatureSet>;
10
+ /**
11
+ * A function that resolves features for the given edition.
12
+ *
13
+ * If no feature set is provided, the default feature set for the edition is
14
+ * returned. If features are provided, they are merged into the edition default
15
+ * features.
16
+ */
17
+ export type FeatureResolverFn = (edition: Edition, a?: FeatureSet, b?: FeatureSet) => MergedFeatureSet;
18
+ /**
19
+ * Create an edition feature resolver with the given feature set defaults.
20
+ */
21
+ export declare function createFeatureResolver(compiledFeatureSetDefaults: FeatureSetDefaults): FeatureResolverFn;
@@ -0,0 +1,103 @@
1
+ // Copyright 2021-2023 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { Edition, FeatureSet, FeatureSetDefaults, } from "../google/protobuf/descriptor_pb.js";
15
+ import { protoBase64 } from "../proto-base64.js";
16
+ /**
17
+ * Static edition feature defaults supported by @bufbuild/protobuf.
18
+ */
19
+ export const featureSetDefaults = FeatureSetDefaults.fromBinary(protoBase64.dec(
20
+ /*upstream-inject-feature-defaults-start*/ "ChESDAgBEAIYAiABKAEwAhjmBwoREgwIAhABGAEgAigBMAEY5wcKERIMCAEQARgBIAIoATABGOgHIOYHKOgH" /*upstream-inject-feature-defaults-end*/));
21
+ /**
22
+ * Create an edition feature resolver with the given feature set defaults.
23
+ */
24
+ export function createFeatureResolver(compiledFeatureSetDefaults) {
25
+ const min = compiledFeatureSetDefaults.minimumEdition;
26
+ const max = compiledFeatureSetDefaults.maximumEdition;
27
+ if (min === undefined ||
28
+ max === undefined ||
29
+ compiledFeatureSetDefaults.defaults.some((d) => d.edition === undefined)) {
30
+ throw new Error("Invalid FeatureSetDefaults");
31
+ }
32
+ const defaultsBinByEdition = new Map();
33
+ return (edition, ...rest) => {
34
+ var _a, _b;
35
+ let defaultsBin = defaultsBinByEdition.get(edition);
36
+ if (defaultsBin === undefined) {
37
+ if (edition < min) {
38
+ throw new Error(`Edition ${Edition[edition]} is earlier than the minimum supported edition ${Edition[min]}`);
39
+ }
40
+ if (max < edition) {
41
+ throw new Error(`Edition ${Edition[edition]} is later than the maximum supported edition ${Edition[max]}`);
42
+ }
43
+ let highestMatch = undefined;
44
+ for (const c of compiledFeatureSetDefaults.defaults) {
45
+ const e = (_a = c.edition) !== null && _a !== void 0 ? _a : 0;
46
+ if (e > edition) {
47
+ continue;
48
+ }
49
+ if (highestMatch !== undefined && highestMatch.e > e) {
50
+ continue;
51
+ }
52
+ highestMatch = {
53
+ e,
54
+ f: (_b = c.features) !== null && _b !== void 0 ? _b : new FeatureSet(),
55
+ };
56
+ }
57
+ if (highestMatch === undefined) {
58
+ throw new Error(`No valid default found for edition ${Edition[edition]}`);
59
+ }
60
+ defaultsBin = highestMatch.f.toBinary();
61
+ defaultsBinByEdition.set(edition, defaultsBin);
62
+ }
63
+ const f = FeatureSet.fromBinary(defaultsBin);
64
+ for (const c of rest) {
65
+ if (c !== undefined) {
66
+ f.fromBinary(c.toBinary());
67
+ }
68
+ }
69
+ if (!validateMergedFeatures(f)) {
70
+ throw new Error(`Invalid FeatureSet for edition ${Edition[edition]}`);
71
+ }
72
+ return f;
73
+ };
74
+ }
75
+ // When protoc generates google.protobuf.FeatureSetDefaults, it ensures that
76
+ // fields are not repeated or required, do not use oneof, and have a default
77
+ // value.
78
+ //
79
+ // When features for an element are resolved, features of the element and its
80
+ // parents are merged into the default FeatureSet for the edition. Because unset
81
+ // fields in the FeatureSet of an element do not unset the default FeatureSet
82
+ // values, a resolved FeatureSet is guaranteed to have all fields set. This is
83
+ // also the case for extensions to FeatureSet that a user might provide, and for
84
+ // features from the future.
85
+ //
86
+ // We cannot exhaustively validate correctness of FeatureSetDefaults at runtime
87
+ // without knowing the schema: If no value for a feature is provided, we do not
88
+ // know that it exists at all.
89
+ //
90
+ // As a sanity check, we validate that all fields known to our version of
91
+ // FeatureSet are set.
92
+ function validateMergedFeatures(featureSet) {
93
+ for (const fi of FeatureSet.fields.list()) {
94
+ const v = featureSet[fi.localName];
95
+ if (v === undefined) {
96
+ return false;
97
+ }
98
+ if (fi.kind == "enum" && v === 0) {
99
+ return false;
100
+ }
101
+ }
102
+ return true;
103
+ }
@@ -0,0 +1,4 @@
1
+ import type { DescEnum } from "../descriptor-set.js";
2
+ import { ScalarType } from "../field.js";
3
+ export declare function parseTextFormatEnumValue(descEnum: DescEnum, value: string): number;
4
+ export declare function parseTextFormatScalarValue(type: ScalarType, value: string): number | boolean | string | bigint | Uint8Array;