@bufbuild/protobuf 0.0.6 → 0.0.9

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 (75) hide show
  1. package/README.md +34 -4
  2. package/dist/cjs/binary-encoding.js +2 -2
  3. package/dist/cjs/codegen-info.js +60 -0
  4. package/dist/cjs/create-descriptor-set.js +952 -0
  5. package/dist/cjs/create-registry-from-desc.js +376 -0
  6. package/dist/cjs/create-registry.js +75 -0
  7. package/dist/cjs/descriptor-set.js +0 -458
  8. package/dist/cjs/google/protobuf/any_pb.js +4 -2
  9. package/dist/cjs/google/protobuf/duration_pb.js +1 -1
  10. package/dist/cjs/google/protobuf/struct_pb.js +2 -2
  11. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  12. package/dist/cjs/index.js +17 -7
  13. package/dist/cjs/{descriptor-registry.js → legacy-descriptor-registry.js} +35 -69
  14. package/dist/cjs/legacy-descriptor-set.js +453 -0
  15. package/dist/cjs/legacy-type-registry.js +104 -0
  16. package/dist/cjs/message.js +2 -1
  17. package/dist/cjs/private/binary-format-common.js +4 -3
  18. package/dist/cjs/private/binary-format-proto2.js +3 -6
  19. package/dist/cjs/private/binary-format-proto3.js +2 -5
  20. package/dist/cjs/private/enum.js +22 -31
  21. package/dist/cjs/private/field-wrapper.js +30 -1
  22. package/dist/cjs/private/field.js +1 -1
  23. package/dist/cjs/private/json-format-common.js +6 -5
  24. package/dist/cjs/private/message-type.js +2 -1
  25. package/dist/cjs/private/names.js +168 -29
  26. package/dist/cjs/proto-int64.js +1 -1
  27. package/dist/cjs/proto2.js +7 -9
  28. package/dist/cjs/proto3.js +10 -13
  29. package/dist/cjs/type-registry.js +0 -87
  30. package/dist/esm/binary-encoding.js +2 -2
  31. package/dist/esm/codegen-info.js +57 -0
  32. package/dist/esm/create-descriptor-set.js +947 -0
  33. package/dist/esm/create-registry-from-desc.js +371 -0
  34. package/dist/esm/create-registry.js +71 -0
  35. package/dist/esm/descriptor-set.js +1 -456
  36. package/dist/esm/google/protobuf/any_pb.js +4 -2
  37. package/dist/esm/google/protobuf/duration_pb.js +1 -1
  38. package/dist/esm/google/protobuf/struct_pb.js +2 -2
  39. package/dist/esm/google/protobuf/type_pb.js +1 -1
  40. package/dist/esm/index.js +9 -3
  41. package/dist/esm/{descriptor-registry.js → legacy-descriptor-registry.js} +33 -67
  42. package/dist/esm/legacy-descriptor-set.js +449 -0
  43. package/dist/esm/legacy-type-registry.js +100 -0
  44. package/dist/esm/message.js +2 -1
  45. package/dist/esm/private/binary-format-common.js +4 -3
  46. package/dist/esm/private/binary-format-proto2.js +3 -6
  47. package/dist/esm/private/binary-format-proto3.js +2 -5
  48. package/dist/esm/private/enum.js +22 -31
  49. package/dist/esm/private/field-wrapper.js +28 -0
  50. package/dist/esm/private/field.js +2 -2
  51. package/dist/esm/private/json-format-common.js +6 -5
  52. package/dist/esm/private/message-type.js +2 -1
  53. package/dist/esm/private/names.js +163 -24
  54. package/dist/esm/proto-int64.js +1 -1
  55. package/dist/esm/proto2.js +8 -10
  56. package/dist/esm/proto3.js +11 -14
  57. package/dist/esm/type-registry.js +1 -85
  58. package/dist/types/codegen-info.d.ts +19 -0
  59. package/dist/types/create-descriptor-set.d.ts +16 -0
  60. package/dist/types/create-registry-from-desc.d.ts +49 -0
  61. package/dist/types/create-registry.d.ts +8 -0
  62. package/dist/types/descriptor-set.d.ts +554 -108
  63. package/dist/types/enum.d.ts +9 -5
  64. package/dist/types/index.d.ts +9 -3
  65. package/dist/types/{descriptor-registry.d.ts → legacy-descriptor-registry.d.ts} +8 -6
  66. package/dist/types/legacy-descriptor-set.d.ts +152 -0
  67. package/dist/types/legacy-type-registry.d.ts +44 -0
  68. package/dist/types/private/enum.d.ts +4 -8
  69. package/dist/types/private/field-wrapper.d.ts +7 -0
  70. package/dist/types/private/message-type.d.ts +1 -4
  71. package/dist/types/private/names.d.ts +26 -8
  72. package/dist/types/private/proto-runtime.d.ts +2 -2
  73. package/dist/types/private/util.d.ts +1 -6
  74. package/dist/types/type-registry.d.ts +0 -38
  75. package/package.json +4 -3
@@ -11,13 +11,13 @@
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
+ import { FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
14
15
  import { assert } from "./private/assert.js";
15
16
  import { proto3 } from "./proto3.js";
16
17
  import { proto2 } from "./proto2.js";
17
18
  import { ScalarType } from "./field.js";
18
- import { DescriptorSet } from "./descriptor-set.js";
19
+ import { LegacyDescriptorSet } from "./legacy-descriptor-set.js";
19
20
  import { protoInt64 } from "./proto-int64.js";
20
- import { makeMethodName } from "./private/names.js";
21
21
  import { Timestamp } from "./google/protobuf/timestamp_pb.js";
22
22
  import { Duration } from "./google/protobuf/duration_pb.js";
23
23
  import { Any } from "./google/protobuf/any_pb.js";
@@ -26,7 +26,6 @@ import { FieldMask } from "./google/protobuf/field_mask_pb.js";
26
26
  import { ListValue, NullValue, Struct, Value, } from "./google/protobuf/struct_pb.js";
27
27
  import { getEnumType } from "./private/enum.js";
28
28
  import { BoolValue, BytesValue, DoubleValue, FloatValue, Int32Value, Int64Value, StringValue, UInt32Value, UInt64Value, } from "./google/protobuf/wrappers_pb.js";
29
- import { FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
30
29
  // well-known message types with specialized JSON representation
31
30
  const wkMessages = [
32
31
  Any,
@@ -51,18 +50,20 @@ const wkMessages = [
51
50
  // well-known enum types with specialized JSON representation
52
51
  const wkEnums = [getEnumType(NullValue)];
53
52
  /**
54
- * DescriptorRegistry is a type registry that dynamically creates types
53
+ * LegacyDescriptorRegistry is a type registry that dynamically creates types
55
54
  * from a set of google.protobuf.FileDescriptorProto.
56
55
  *
57
56
  * By default, all well-known types with a specialized JSON representation
58
57
  * are replaced with their generated counterpart in this package.
58
+ *
59
+ * @deprecated
59
60
  */
60
- export class DescriptorRegistry {
61
+ export class LegacyDescriptorRegistry {
61
62
  constructor(descriptorSet, replaceWkt = true) {
62
63
  this.enums = {};
63
64
  this.messages = {};
64
65
  this.services = {};
65
- this.ds = descriptorSet ?? new DescriptorSet();
66
+ this.ds = descriptorSet !== null && descriptorSet !== void 0 ? descriptorSet : new LegacyDescriptorSet();
66
67
  if (replaceWkt) {
67
68
  for (const mt of wkMessages) {
68
69
  this.messages["." + mt.typeName] = mt;
@@ -80,7 +81,7 @@ export class DescriptorRegistry {
80
81
  const set = bytesOrSet instanceof Uint8Array
81
82
  ? FileDescriptorSet.fromBinary(bytesOrSet)
82
83
  : new FileDescriptorSet(bytesOrSet);
83
- const dr = new DescriptorRegistry();
84
+ const dr = new LegacyDescriptorRegistry();
84
85
  dr.add(...set.file);
85
86
  return dr;
86
87
  }
@@ -107,6 +108,7 @@ export class DescriptorRegistry {
107
108
  const type = runtime.makeEnumType(typeName, raw.values.map((u) => ({
108
109
  no: u.number,
109
110
  name: u.name,
111
+ localName: u.name,
110
112
  })), {});
111
113
  this.enums[protoTypeName] = type;
112
114
  return type;
@@ -157,7 +159,7 @@ export class DescriptorRegistry {
157
159
  assert(ot, `output message "${u.outputTypeName}" for ${u.toString()} not found`);
158
160
  const m = {
159
161
  name: u.name,
160
- localName: makeMethodName(u.name),
162
+ localName: localMethodName(u.name),
161
163
  I: it,
162
164
  O: ot,
163
165
  kind: u.kind,
@@ -172,9 +174,16 @@ export class DescriptorRegistry {
172
174
  });
173
175
  }
174
176
  }
177
+ function localMethodName(protoName) {
178
+ if (protoName.length == 0) {
179
+ return protoName;
180
+ }
181
+ return protoName[0].toLowerCase() + protoName.substring(1);
182
+ }
175
183
  function makeTypeLocalName(type) {
184
+ var _a;
176
185
  const typeName = type.typeName;
177
- const packagePrefix = (type.file.proto.package ?? "") + ".";
186
+ const packagePrefix = ((_a = type.file.proto.package) !== null && _a !== void 0 ? _a : "") + ".";
178
187
  if (!typeName.startsWith(packagePrefix)) {
179
188
  return type.name;
180
189
  }
@@ -203,32 +212,23 @@ function makeMapFieldInfo(field, resolver) {
203
212
  if (field.map.value.message) {
204
213
  const messageType = resolver.findMessage(field.map.value.message.typeName);
205
214
  assert(messageType, `message "${field.map.value.message.typeName}" for ${field.toString()} not found`);
206
- return {
207
- ...base,
208
- V: {
215
+ return Object.assign(Object.assign({}, base), { V: {
209
216
  kind: "message",
210
217
  T: messageType,
211
- },
212
- };
218
+ } });
213
219
  }
214
220
  if (field.map.value.enum) {
215
221
  const enumType = resolver.findEnum(field.map.value.enum.typeName);
216
222
  assert(enumType, `enum "${field.map.value.enum.typeName}" for ${field.toString()} not found`);
217
- return {
218
- ...base,
219
- V: {
223
+ return Object.assign(Object.assign({}, base), { V: {
220
224
  kind: "enum",
221
225
  T: enumType,
222
- },
223
- };
226
+ } });
224
227
  }
225
- return {
226
- ...base,
227
- V: {
228
+ return Object.assign(Object.assign({}, base), { V: {
228
229
  kind: "scalar",
229
230
  T: field.map.value.scalarType,
230
- },
231
- };
231
+ } });
232
232
  }
233
233
  function makeScalarFieldInfo(field) {
234
234
  const base = {
@@ -238,25 +238,13 @@ function makeScalarFieldInfo(field) {
238
238
  T: field.scalarType,
239
239
  };
240
240
  if (field.repeated) {
241
- return {
242
- ...base,
243
- repeated: true,
244
- packed: field.packed,
245
- oneof: undefined,
246
- T: field.scalarType,
247
- };
241
+ return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined, T: field.scalarType });
248
242
  }
249
243
  if (field.oneof) {
250
- return {
251
- ...base,
252
- oneof: field.oneof.name,
253
- };
244
+ return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
254
245
  }
255
246
  if (field.optional) {
256
- return {
257
- ...base,
258
- opt: true,
259
- };
247
+ return Object.assign(Object.assign({}, base), { opt: true });
260
248
  }
261
249
  return base;
262
250
  }
@@ -270,24 +258,13 @@ function makeMessageFieldInfo(field, resolver) {
270
258
  T: messageType,
271
259
  };
272
260
  if (field.repeated) {
273
- return {
274
- ...base,
275
- repeated: true,
276
- packed: field.packed,
277
- oneof: undefined,
278
- };
261
+ return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined });
279
262
  }
280
263
  if (field.oneof) {
281
- return {
282
- ...base,
283
- oneof: field.oneof.name,
284
- };
264
+ return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
285
265
  }
286
266
  if (field.optional) {
287
- return {
288
- ...base,
289
- opt: true,
290
- };
267
+ return Object.assign(Object.assign({}, base), { opt: true });
291
268
  }
292
269
  return base;
293
270
  }
@@ -301,24 +278,13 @@ function makeEnumFieldInfo(field, resolver) {
301
278
  T: enumType,
302
279
  };
303
280
  if (field.repeated) {
304
- return {
305
- ...base,
306
- repeated: true,
307
- packed: field.packed,
308
- oneof: undefined,
309
- };
281
+ return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined });
310
282
  }
311
283
  if (field.oneof) {
312
- return {
313
- ...base,
314
- oneof: field.oneof.name,
315
- };
284
+ return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
316
285
  }
317
286
  if (field.optional) {
318
- return {
319
- ...base,
320
- opt: true,
321
- };
287
+ return Object.assign(Object.assign({}, base), { opt: true });
322
288
  }
323
289
  return base;
324
290
  }
@@ -0,0 +1,449 @@
1
+ // Copyright 2021-2022 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 { FieldDescriptorProto_Label, FieldDescriptorProto_Type, MethodOptions_IdempotencyLevel, } from "./google/protobuf/descriptor_pb.js";
15
+ import { assert } from "./private/assert.js";
16
+ import { ScalarType } from "./field.js";
17
+ import { MethodIdempotency, MethodKind } from "./service-type.js";
18
+ /**
19
+ * LegacyDescriptorSet wraps a set of google.protobuf.FileDescriptorProto,
20
+ * asserting basic expectations and providing hierarchical information.
21
+ *
22
+ * Note that all types are kept by their fully qualified type name
23
+ * with a leading dot.
24
+ *
25
+ * @deprecated
26
+ */
27
+ export class LegacyDescriptorSet {
28
+ constructor() {
29
+ this.enums = {};
30
+ this.messages = {};
31
+ this.services = {};
32
+ }
33
+ /**
34
+ * May raise an error on invalid descriptors.
35
+ */
36
+ add(...files) {
37
+ for (const file of files) {
38
+ newFile(file, this);
39
+ }
40
+ }
41
+ listEnums() {
42
+ return Object.values(this.enums).filter(isDefined);
43
+ }
44
+ listMessages() {
45
+ return Object.values(this.messages).filter(isDefined);
46
+ }
47
+ listServices() {
48
+ return Object.values(this.services).filter(isDefined);
49
+ }
50
+ }
51
+ function isDefined(v) {
52
+ return v !== undefined;
53
+ }
54
+ const fieldTypeToScalarType = {
55
+ [FieldDescriptorProto_Type.DOUBLE]: ScalarType.DOUBLE,
56
+ [FieldDescriptorProto_Type.FLOAT]: ScalarType.FLOAT,
57
+ [FieldDescriptorProto_Type.INT64]: ScalarType.INT64,
58
+ [FieldDescriptorProto_Type.UINT64]: ScalarType.UINT64,
59
+ [FieldDescriptorProto_Type.INT32]: ScalarType.INT32,
60
+ [FieldDescriptorProto_Type.FIXED64]: ScalarType.FIXED64,
61
+ [FieldDescriptorProto_Type.FIXED32]: ScalarType.FIXED32,
62
+ [FieldDescriptorProto_Type.BOOL]: ScalarType.BOOL,
63
+ [FieldDescriptorProto_Type.STRING]: ScalarType.STRING,
64
+ [FieldDescriptorProto_Type.GROUP]: undefined,
65
+ [FieldDescriptorProto_Type.MESSAGE]: undefined,
66
+ [FieldDescriptorProto_Type.BYTES]: ScalarType.BYTES,
67
+ [FieldDescriptorProto_Type.UINT32]: ScalarType.UINT32,
68
+ [FieldDescriptorProto_Type.ENUM]: undefined,
69
+ [FieldDescriptorProto_Type.SFIXED32]: ScalarType.SFIXED32,
70
+ [FieldDescriptorProto_Type.SFIXED64]: ScalarType.SFIXED64,
71
+ [FieldDescriptorProto_Type.SINT32]: ScalarType.SINT32,
72
+ [FieldDescriptorProto_Type.SINT64]: ScalarType.SINT64,
73
+ };
74
+ function newFile(proto, ds) {
75
+ assert(proto.name, `missing file descriptor name`);
76
+ if (proto.syntax !== undefined && proto.syntax !== "proto3") {
77
+ throw new Error(`invalid descriptor: unsupported syntax: ${proto.syntax}`);
78
+ }
79
+ const file = {
80
+ proto,
81
+ syntax: proto.syntax === "proto3" ? "proto3" : "proto2",
82
+ name: proto.name.endsWith(".proto")
83
+ ? proto.name.substring(-".proto".length)
84
+ : proto.name,
85
+ toString() {
86
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
87
+ return `file ${this.proto.name}`;
88
+ },
89
+ };
90
+ for (const messageType of proto.messageType) {
91
+ newMessage(messageType, undefined, file, ds);
92
+ }
93
+ for (const enumType of proto.enumType) {
94
+ newEnum(enumType, undefined, file, ds);
95
+ }
96
+ for (const extension of proto.extension) {
97
+ newExtension(extension, undefined, file, ds);
98
+ }
99
+ for (const service of proto.service) {
100
+ newService(service, file, ds);
101
+ }
102
+ return file;
103
+ }
104
+ function newEnum(proto, parent, file, us) {
105
+ assert(proto.name, `invalid descriptor: missing enum descriptor name`);
106
+ let protoTypeName;
107
+ if (parent) {
108
+ protoTypeName = `${parent.protoTypeName}.${proto.name}`;
109
+ }
110
+ else if (file.proto.package !== undefined) {
111
+ protoTypeName = `.${file.proto.package}.${proto.name}`;
112
+ }
113
+ else {
114
+ protoTypeName = `.${proto.name}`;
115
+ }
116
+ const values = [];
117
+ const enumT = {
118
+ proto,
119
+ file,
120
+ parent,
121
+ name: proto.name,
122
+ protoTypeName,
123
+ typeName: protoTypeName.startsWith(".")
124
+ ? protoTypeName.substring(1)
125
+ : protoTypeName,
126
+ values,
127
+ toString() {
128
+ return `enum ${this.typeName}`;
129
+ },
130
+ };
131
+ us.enums[enumT.protoTypeName] = enumT;
132
+ for (const value of proto.value) {
133
+ values.push(newEnumValue(value, enumT));
134
+ }
135
+ return enumT;
136
+ }
137
+ function newEnumValue(proto, parent) {
138
+ assert(proto.name, `invalid descriptor: missing enum value descriptor name`);
139
+ assert(proto.number !== undefined, `invalid descriptor: missing enum value descriptor number`);
140
+ return {
141
+ proto,
142
+ name: proto.name,
143
+ number: proto.number,
144
+ parent,
145
+ toString() {
146
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
147
+ return `enum value ${this.parent.typeName}.${this.proto.name}`;
148
+ },
149
+ };
150
+ }
151
+ function newMessage(proto, parent, file, us) {
152
+ var _a;
153
+ assert(proto.name, `invalid descriptor: missing name`);
154
+ const nestedMessages = [];
155
+ const fields = [];
156
+ const oneofs = [];
157
+ const nestedEnums = [];
158
+ const nestedExtensions = [];
159
+ let protoTypeName;
160
+ if (parent) {
161
+ protoTypeName = `${parent.protoTypeName}.${proto.name}`;
162
+ }
163
+ else if (file.proto.package !== undefined) {
164
+ protoTypeName = `.${file.proto.package}.${proto.name}`;
165
+ }
166
+ else {
167
+ protoTypeName = `.${proto.name}`;
168
+ }
169
+ const message = {
170
+ proto,
171
+ file,
172
+ parent,
173
+ name: proto.name,
174
+ typeName: protoTypeName.startsWith(".")
175
+ ? protoTypeName.substring(1)
176
+ : protoTypeName,
177
+ protoTypeName,
178
+ fields,
179
+ oneofs,
180
+ nestedEnums,
181
+ nestedMessages,
182
+ nestedExtensions,
183
+ toString() {
184
+ return `message ${this.typeName}`;
185
+ },
186
+ };
187
+ us.messages[message.protoTypeName] = message;
188
+ for (const nestedType of proto.nestedType) {
189
+ nestedMessages.push(newMessage(nestedType, message, file, us));
190
+ }
191
+ for (const oneofDecl of proto.oneofDecl) {
192
+ oneofs.push(newOneof(oneofDecl, message, file));
193
+ }
194
+ for (const field of proto.field) {
195
+ let oneof = undefined;
196
+ if (field.oneofIndex !== undefined) {
197
+ oneof = oneofs[field.oneofIndex];
198
+ assert(oneof, `invalid descriptor: oneof declaration index ${field.oneofIndex} specified by field #${(_a = field.number) !== null && _a !== void 0 ? _a : -1} not found`);
199
+ }
200
+ fields.push(newField(field, message, oneof));
201
+ }
202
+ for (const enumType of proto.enumType) {
203
+ nestedEnums.push(newEnum(enumType, message, file, us));
204
+ }
205
+ for (const extension of proto.extension) {
206
+ nestedExtensions.push(newExtension(extension, message, file, us));
207
+ }
208
+ return message;
209
+ }
210
+ function newField(proto, parent, oneof) {
211
+ var _a;
212
+ assert(proto.name, `invalid descriptor: missing name`);
213
+ assert(proto.number, `invalid descriptor: missing number`);
214
+ assert(proto.type, `invalid descriptor: missing type`);
215
+ let optional = false;
216
+ let packed = ((_a = proto.options) === null || _a === void 0 ? void 0 : _a.packed) === true;
217
+ switch (parent.file.syntax) {
218
+ case "proto2":
219
+ optional =
220
+ oneof === undefined &&
221
+ proto.label === FieldDescriptorProto_Label.OPTIONAL;
222
+ break;
223
+ case "proto3":
224
+ optional = proto.proto3Optional === true;
225
+ switch (proto.type) {
226
+ case FieldDescriptorProto_Type.DOUBLE:
227
+ case FieldDescriptorProto_Type.FLOAT:
228
+ case FieldDescriptorProto_Type.INT64:
229
+ case FieldDescriptorProto_Type.UINT64:
230
+ case FieldDescriptorProto_Type.INT32:
231
+ case FieldDescriptorProto_Type.FIXED64:
232
+ case FieldDescriptorProto_Type.FIXED32:
233
+ case FieldDescriptorProto_Type.UINT32:
234
+ case FieldDescriptorProto_Type.SFIXED32:
235
+ case FieldDescriptorProto_Type.SFIXED64:
236
+ case FieldDescriptorProto_Type.SINT32:
237
+ case FieldDescriptorProto_Type.SINT64:
238
+ case FieldDescriptorProto_Type.BOOL:
239
+ case FieldDescriptorProto_Type.ENUM:
240
+ // From the proto3 language guide:
241
+ // > In proto3, repeated fields of scalar numeric types are packed by default.
242
+ // This information is incomplete - according to the conformance tests, BOOL
243
+ // and ENUM are packed by default as well. This means only STRING and BYTES
244
+ // are not packed by default, which makes sense because they are length-delimited.
245
+ packed = true;
246
+ break;
247
+ case FieldDescriptorProto_Type.STRING:
248
+ case FieldDescriptorProto_Type.GROUP:
249
+ case FieldDescriptorProto_Type.MESSAGE:
250
+ case FieldDescriptorProto_Type.BYTES:
251
+ assert(!packed, `invalid descriptor: ${FieldDescriptorProto_Type[proto.type]} cannot be packed`);
252
+ break;
253
+ }
254
+ break;
255
+ }
256
+ const field = {
257
+ proto,
258
+ name: proto.name,
259
+ number: proto.number,
260
+ type: proto.type,
261
+ parent,
262
+ oneof: proto.proto3Optional === true ? undefined : oneof,
263
+ optional,
264
+ packed,
265
+ toString() {
266
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
267
+ return `field ${parent.typeName}.${proto.name}`;
268
+ },
269
+ resolve(us) {
270
+ return resolveField(this, us);
271
+ },
272
+ };
273
+ if (oneof) {
274
+ oneof.fields.push(field);
275
+ }
276
+ return field;
277
+ }
278
+ function resolveField(u, us) {
279
+ var _a;
280
+ const repeated = u.proto.label === FieldDescriptorProto_Label.REPEATED;
281
+ switch (u.type) {
282
+ case FieldDescriptorProto_Type.MESSAGE:
283
+ case FieldDescriptorProto_Type.GROUP: {
284
+ assert(u.proto.typeName, `invalid descriptor: ${u.toString()} has type ${FieldDescriptorProto_Type[u.type]}, but type_name is unset`);
285
+ const refMessage = us.messages[u.proto.typeName];
286
+ assert(refMessage, `invalid descriptor: cannot find type_name "${u.proto.typeName}" specified by ${u.toString()}`);
287
+ if (((_a = refMessage.proto.options) === null || _a === void 0 ? void 0 : _a.mapEntry) !== undefined) {
288
+ return Object.assign(Object.assign({}, u), { repeated: false, scalarType: undefined, message: undefined, enum: undefined, map: resolveMap(refMessage, us) });
289
+ }
290
+ return Object.assign(Object.assign({}, u), { repeated, scalarType: undefined, message: refMessage, enum: undefined, map: undefined });
291
+ }
292
+ case FieldDescriptorProto_Type.ENUM: {
293
+ assert(u.proto.typeName, `invalid descriptor: ${u.toString()} has type ${FieldDescriptorProto_Type[u.type]}, but type_name is unset`);
294
+ const refEnum = us.enums[u.proto.typeName];
295
+ assert(refEnum, `invalid descriptor: cannot find type_name "${u.proto.typeName}" specified by ${u.toString()}`);
296
+ return Object.assign(Object.assign({}, u), { repeated, scalarType: undefined, message: undefined, enum: refEnum, map: undefined });
297
+ }
298
+ default: {
299
+ const scalarType = fieldTypeToScalarType[u.type];
300
+ assert(scalarType, `invalid descriptor: unable to convert google.protobuf.FieldDescriptorProto.Type ${FieldDescriptorProto_Type[u.type]} to ScalarType`);
301
+ return Object.assign(Object.assign({}, u), { repeated,
302
+ scalarType, message: undefined, enum: undefined, map: undefined });
303
+ }
304
+ }
305
+ }
306
+ function resolveMap(mapEntry, us) {
307
+ var _a;
308
+ assert((_a = mapEntry.proto.options) === null || _a === void 0 ? void 0 : _a.mapEntry, `invalid descriptor: expected ${mapEntry.toString()} to be a map entry`);
309
+ assert(mapEntry.fields.length === 2, `invalid descriptor: map entry ${mapEntry.toString()} has ${mapEntry.fields.length} fields`);
310
+ const uKeyField = mapEntry.fields.find((f) => f.proto.number === 1);
311
+ assert(uKeyField, `invalid descriptor: map entry ${mapEntry.toString()} is missing key field`);
312
+ const keyField = resolveField(uKeyField, us);
313
+ assert(keyField.scalarType !== undefined &&
314
+ keyField.scalarType !== ScalarType.BYTES &&
315
+ keyField.scalarType !== ScalarType.FLOAT &&
316
+ keyField.scalarType !== ScalarType.DOUBLE, `invalid descriptor: map entry ${mapEntry.toString()} has unexpected key type ${FieldDescriptorProto_Type[keyField.type]}`);
317
+ const uValueField = mapEntry.fields.find((f) => f.proto.number === 2);
318
+ assert(uValueField, `invalid descriptor: map entry ${mapEntry.toString()} is missing value field`);
319
+ const valueField = resolveField(uValueField, us);
320
+ if (valueField.scalarType !== undefined) {
321
+ return {
322
+ key: keyField.scalarType,
323
+ value: {
324
+ scalarType: valueField.scalarType,
325
+ enum: undefined,
326
+ message: undefined,
327
+ },
328
+ };
329
+ }
330
+ if (valueField.enum !== undefined) {
331
+ return {
332
+ key: keyField.scalarType,
333
+ value: { scalar: undefined, enum: valueField.enum, message: undefined },
334
+ };
335
+ }
336
+ if (valueField.message !== undefined) {
337
+ return {
338
+ key: keyField.scalarType,
339
+ value: {
340
+ scalar: undefined,
341
+ enum: undefined,
342
+ message: valueField.message,
343
+ },
344
+ };
345
+ }
346
+ throw new Error(`invalid descriptor: map entry ${mapEntry.toString()} has unexpected value type ${FieldDescriptorProto_Type[keyField.type]}`);
347
+ }
348
+ function newOneof(proto, parent, file) {
349
+ assert(proto.name, `invalid descriptor: missing oneof descriptor name`);
350
+ return {
351
+ proto,
352
+ parent,
353
+ file,
354
+ fields: [],
355
+ name: proto.name,
356
+ toString() {
357
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
358
+ return `oneof ${parent.typeName}.${proto.name}`;
359
+ },
360
+ };
361
+ }
362
+ function newExtension(proto, parent, file, us // eslint-disable-line @typescript-eslint/no-unused-vars
363
+ ) {
364
+ assert(proto.name, `invalid descriptor: missing field descriptor name`);
365
+ return {
366
+ proto,
367
+ file,
368
+ parent,
369
+ };
370
+ }
371
+ function newService(proto, file, us) {
372
+ assert(proto.name, `invalid descriptor: missing service descriptor name`);
373
+ let protoTypeName;
374
+ if (file.proto.package !== undefined) {
375
+ protoTypeName = `.${file.proto.package}.${proto.name}`;
376
+ }
377
+ else {
378
+ protoTypeName = `.${proto.name}`;
379
+ }
380
+ const methods = [];
381
+ const service = {
382
+ proto,
383
+ file,
384
+ typeName: protoTypeName.startsWith(".")
385
+ ? protoTypeName.substring(1)
386
+ : protoTypeName,
387
+ protoTypeName,
388
+ methods,
389
+ toString() {
390
+ return `service ${this.typeName}`;
391
+ },
392
+ };
393
+ for (const method of proto.method) {
394
+ methods.push(newMethod(method, service));
395
+ }
396
+ us.services[service.protoTypeName] = service;
397
+ return service;
398
+ }
399
+ function newMethod(proto, parent) {
400
+ var _a;
401
+ assert(proto.name, `invalid descriptor: missing method descriptor name`);
402
+ assert(proto.inputType, `invalid descriptor: missing method input type`);
403
+ assert(proto.outputType, `invalid descriptor: missing method output type`);
404
+ let kind;
405
+ if (proto.clientStreaming === true && proto.serverStreaming === true) {
406
+ kind = MethodKind.BiDiStreaming;
407
+ }
408
+ else if (proto.clientStreaming === true) {
409
+ kind = MethodKind.ClientStreaming;
410
+ }
411
+ else if (proto.serverStreaming === true) {
412
+ kind = MethodKind.ServerStreaming;
413
+ }
414
+ else {
415
+ kind = MethodKind.Unary;
416
+ }
417
+ let idempotency;
418
+ switch ((_a = proto.options) === null || _a === void 0 ? void 0 : _a.idempotencyLevel) {
419
+ case MethodOptions_IdempotencyLevel.IDEMPOTENT:
420
+ idempotency = MethodIdempotency.Idempotent;
421
+ break;
422
+ case MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS:
423
+ idempotency = MethodIdempotency.NoSideEffects;
424
+ break;
425
+ case MethodOptions_IdempotencyLevel.IDEMPOTENCY_UNKNOWN:
426
+ case undefined:
427
+ idempotency = undefined;
428
+ break;
429
+ }
430
+ const inputTypeName = proto.inputType.startsWith(".")
431
+ ? proto.inputType.substring(1)
432
+ : proto.inputType;
433
+ const outputTypeName = proto.outputType.startsWith(".")
434
+ ? proto.outputType.substring(1)
435
+ : proto.outputType;
436
+ return {
437
+ proto,
438
+ parent,
439
+ name: proto.name,
440
+ kind,
441
+ idempotency,
442
+ inputTypeName,
443
+ outputTypeName,
444
+ toString() {
445
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
446
+ return `rpc ${this.parent.typeName}.${proto.name}`;
447
+ },
448
+ };
449
+ }