@bufbuild/protobuf 0.0.8 → 0.1.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 (70) hide show
  1. package/README.md +34 -4
  2. package/dist/cjs/codegen-info.js +60 -0
  3. package/dist/cjs/create-descriptor-set.js +952 -0
  4. package/dist/cjs/create-registry-from-desc.js +270 -0
  5. package/dist/cjs/create-registry.js +75 -0
  6. package/dist/cjs/descriptor-set.js +0 -436
  7. package/dist/cjs/google/protobuf/descriptor_pb.js +2 -2
  8. package/dist/cjs/google/protobuf/empty_pb.js +0 -1
  9. package/dist/cjs/google/protobuf/struct_pb.js +1 -1
  10. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  11. package/dist/cjs/google/varint.js +85 -37
  12. package/dist/cjs/index.js +11 -7
  13. package/dist/cjs/private/enum.js +22 -31
  14. package/dist/cjs/private/field-wrapper.js +30 -1
  15. package/dist/cjs/private/field.js +1 -1
  16. package/dist/cjs/private/json-format-common.js +6 -6
  17. package/dist/cjs/private/names.js +168 -29
  18. package/dist/cjs/proto-int64.js +23 -40
  19. package/dist/cjs/proto2.js +2 -2
  20. package/dist/cjs/proto3.js +2 -2
  21. package/dist/cjs/type-registry.js +0 -87
  22. package/dist/esm/codegen-info.js +57 -0
  23. package/dist/esm/create-descriptor-set.js +947 -0
  24. package/dist/esm/create-registry-from-desc.js +266 -0
  25. package/dist/esm/create-registry.js +71 -0
  26. package/dist/esm/descriptor-set.js +1 -434
  27. package/dist/esm/google/protobuf/descriptor_pb.js +2 -2
  28. package/dist/esm/google/protobuf/empty_pb.js +0 -1
  29. package/dist/esm/google/protobuf/struct_pb.js +1 -1
  30. package/dist/esm/google/protobuf/type_pb.js +1 -1
  31. package/dist/esm/google/varint.js +81 -34
  32. package/dist/esm/index.js +6 -3
  33. package/dist/esm/private/enum.js +22 -31
  34. package/dist/esm/private/field-wrapper.js +28 -0
  35. package/dist/esm/private/field.js +2 -2
  36. package/dist/esm/private/json-format-common.js +6 -6
  37. package/dist/esm/private/names.js +163 -24
  38. package/dist/esm/proto-int64.js +24 -41
  39. package/dist/esm/proto2.js +3 -3
  40. package/dist/esm/proto3.js +3 -3
  41. package/dist/esm/type-registry.js +1 -85
  42. package/dist/types/codegen-info.d.ts +19 -0
  43. package/dist/types/create-descriptor-set.d.ts +16 -0
  44. package/dist/types/create-registry-from-desc.d.ts +15 -0
  45. package/dist/types/create-registry.d.ts +8 -0
  46. package/dist/types/descriptor-set.d.ts +554 -108
  47. package/dist/types/enum.d.ts +9 -5
  48. package/dist/types/google/protobuf/descriptor_pb.d.ts +6 -6
  49. package/dist/types/google/protobuf/empty_pb.d.ts +0 -1
  50. package/dist/types/google/varint.d.ts +21 -9
  51. package/dist/types/index.d.ts +6 -3
  52. package/dist/types/message.d.ts +3 -1
  53. package/dist/types/private/enum.d.ts +4 -8
  54. package/dist/types/private/field-wrapper.d.ts +7 -0
  55. package/dist/types/private/message-type.d.ts +1 -4
  56. package/dist/types/private/names.d.ts +26 -8
  57. package/dist/types/private/proto-runtime.d.ts +2 -2
  58. package/dist/types/private/util.d.ts +1 -6
  59. package/dist/types/proto-int64.d.ts +14 -13
  60. package/dist/types/type-registry.d.ts +0 -38
  61. package/package.json +6 -10
  62. package/dist/cjs/descriptor-registry.js +0 -460
  63. package/dist/esm/descriptor-registry.js +0 -456
  64. package/dist/protobuf.cjs +0 -2
  65. package/dist/protobuf.cjs.map +0 -1
  66. package/dist/protobuf.esm.js +0 -2
  67. package/dist/protobuf.esm.js.map +0 -1
  68. package/dist/protobuf.modern.js +0 -2
  69. package/dist/protobuf.modern.js.map +0 -1
  70. package/dist/types/descriptor-registry.d.ts +0 -43
@@ -0,0 +1,952 @@
1
+ "use strict";
2
+ // Copyright 2021-2022 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.isPackedFieldByDefault = exports.createDescriptorSet = void 0;
17
+ const descriptor_pb_js_1 = require("./google/protobuf/descriptor_pb.js");
18
+ const assert_js_1 = require("./private/assert.js");
19
+ const field_js_1 = require("./field.js");
20
+ const service_type_js_1 = require("./service-type.js");
21
+ const names_js_1 = require("./private/names.js");
22
+ const proto_int64_js_1 = require("./proto-int64.js");
23
+ /**
24
+ * Create a DescriptorSet, a convenient interface for working with a set of
25
+ * google.protobuf.FileDescriptorProto.
26
+ *
27
+ * Note that files must be given in topological order, so each file appears
28
+ * before any file that imports it. Protocol buffer compilers always produce
29
+ * files in topological order.
30
+ */
31
+ function createDescriptorSet(input) {
32
+ const cart = {
33
+ enums: new Map(),
34
+ messages: new Map(),
35
+ services: new Map(),
36
+ extensions: new Map(),
37
+ mapEntries: new Map(),
38
+ };
39
+ const fileDescriptors = input instanceof descriptor_pb_js_1.FileDescriptorSet
40
+ ? input.file
41
+ : input instanceof Uint8Array
42
+ ? descriptor_pb_js_1.FileDescriptorSet.fromBinary(input).file
43
+ : input;
44
+ const files = fileDescriptors.map((proto) => newFile(proto, cart));
45
+ return Object.assign({ files }, cart);
46
+ }
47
+ exports.createDescriptorSet = createDescriptorSet;
48
+ /**
49
+ * Create a descriptor for a file.
50
+ */
51
+ function newFile(proto, cart) {
52
+ var _a, _b, _c;
53
+ (0, assert_js_1.assert)(proto.name, `invalid FileDescriptorProto: missing name`);
54
+ (0, assert_js_1.assert)(proto.syntax === undefined || proto.syntax === "proto3", `invalid FileDescriptorProto: unsupported syntax: ${(_a = proto.syntax) !== null && _a !== void 0 ? _a : "undefined"}`);
55
+ const file = {
56
+ kind: "file",
57
+ proto,
58
+ deprecated: (_c = (_b = proto.options) === null || _b === void 0 ? void 0 : _b.deprecated) !== null && _c !== void 0 ? _c : false,
59
+ syntax: proto.syntax === "proto3" ? "proto3" : "proto2",
60
+ name: proto.name.replace(/\.proto/, ""),
61
+ enums: [],
62
+ messages: [],
63
+ extensions: [],
64
+ services: [],
65
+ toString() {
66
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
67
+ return `file ${this.proto.name}`;
68
+ },
69
+ getSyntaxComments() {
70
+ return findComments(this.proto.sourceCodeInfo, [
71
+ FieldNumber.FileDescriptorProto_Syntax,
72
+ ]);
73
+ },
74
+ getPackageComments() {
75
+ return findComments(this.proto.sourceCodeInfo, [
76
+ FieldNumber.FileDescriptorProto_Package,
77
+ ]);
78
+ },
79
+ };
80
+ cart.mapEntries.clear(); // map entries are local to the file, we can safely discard
81
+ for (const enumProto of proto.enumType) {
82
+ addEnum(enumProto, file, undefined, cart);
83
+ }
84
+ for (const messageProto of proto.messageType) {
85
+ addMessage(messageProto, file, undefined, cart);
86
+ }
87
+ for (const serviceProto of proto.service) {
88
+ addService(serviceProto, file, cart);
89
+ }
90
+ addExtensions(file, cart);
91
+ for (const mapEntry of cart.mapEntries.values()) {
92
+ addFields(mapEntry, cart);
93
+ }
94
+ for (const message of file.messages) {
95
+ addFields(message, cart);
96
+ addExtensions(message, cart);
97
+ }
98
+ cart.mapEntries.clear(); // map entries are local to the file, we can safely discard
99
+ return file;
100
+ }
101
+ /**
102
+ * Create descriptors for extensions, and add them to the message / file,
103
+ * and to our cart.
104
+ * Recurses into nested types.
105
+ */
106
+ function addExtensions(desc, cart) {
107
+ switch (desc.kind) {
108
+ case "file":
109
+ for (const proto of desc.proto.extension) {
110
+ const ext = newExtension(proto, desc, undefined, cart);
111
+ desc.extensions.push(ext);
112
+ cart.extensions.set(ext.typeName, ext);
113
+ }
114
+ break;
115
+ case "message":
116
+ for (const proto of desc.proto.extension) {
117
+ const ext = newExtension(proto, desc.file, desc, cart);
118
+ desc.nestedExtensions.push(ext);
119
+ cart.extensions.set(ext.typeName, ext);
120
+ }
121
+ for (const message of desc.nestedMessages) {
122
+ addExtensions(message, cart);
123
+ }
124
+ break;
125
+ }
126
+ }
127
+ /**
128
+ * Create descriptors for fields and oneof groups, and add them to the message.
129
+ * Recurses into nested types.
130
+ */
131
+ function addFields(message, cart) {
132
+ const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message));
133
+ const oneofsSeen = new Set();
134
+ for (const proto of message.proto.field) {
135
+ const oneof = findOneof(proto, allOneofs);
136
+ const field = newField(proto, message.file, message, oneof, cart);
137
+ message.fields.push(field);
138
+ if (oneof === undefined) {
139
+ message.members.push(field);
140
+ }
141
+ else {
142
+ oneof.fields.push(field);
143
+ if (!oneofsSeen.has(oneof)) {
144
+ oneofsSeen.add(oneof);
145
+ message.members.push(oneof);
146
+ }
147
+ }
148
+ }
149
+ for (const oneof of allOneofs.filter((o) => oneofsSeen.has(o))) {
150
+ message.oneofs.push(oneof);
151
+ }
152
+ for (const child of message.nestedMessages) {
153
+ addFields(child, cart);
154
+ }
155
+ }
156
+ /**
157
+ * Create a descriptor for an enumeration, and add it our cart and to the
158
+ * parent type, if any.
159
+ */
160
+ function addEnum(proto, file, parent, cart) {
161
+ var _a, _b, _c;
162
+ (0, assert_js_1.assert)(proto.name, `invalid EnumDescriptorProto: missing name`);
163
+ const desc = {
164
+ kind: "enum",
165
+ proto,
166
+ deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
167
+ file,
168
+ parent,
169
+ name: proto.name,
170
+ typeName: makeTypeName(proto, parent, file),
171
+ values: [],
172
+ sharedPrefix: (0, names_js_1.findEnumSharedPrefix)(proto.name, proto.value.map((v) => { var _a; return (_a = v.name) !== null && _a !== void 0 ? _a : ""; })),
173
+ toString() {
174
+ return `enum ${this.typeName}`;
175
+ },
176
+ getComments() {
177
+ const path = this.parent
178
+ ? [
179
+ ...this.parent.getComments().sourcePath,
180
+ FieldNumber.DescriptorProto_EnumType,
181
+ this.parent.proto.enumType.indexOf(this.proto),
182
+ ]
183
+ : [
184
+ FieldNumber.FileDescriptorProto_EnumType,
185
+ this.file.proto.enumType.indexOf(this.proto),
186
+ ];
187
+ return findComments(file.proto.sourceCodeInfo, path);
188
+ },
189
+ };
190
+ cart.enums.set(desc.typeName, desc);
191
+ proto.value.forEach((proto) => {
192
+ var _a, _b;
193
+ (0, assert_js_1.assert)(proto.name, `invalid EnumValueDescriptorProto: missing name`);
194
+ (0, assert_js_1.assert)(proto.number !== undefined, `invalid EnumValueDescriptorProto: missing number`);
195
+ desc.values.push({
196
+ kind: "enum_value",
197
+ proto,
198
+ deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
199
+ parent: desc,
200
+ name: proto.name,
201
+ number: proto.number,
202
+ toString() {
203
+ return `enum value ${desc.typeName}.${this.name}`;
204
+ },
205
+ declarationString() {
206
+ var _a;
207
+ let str = `${this.name} = ${this.number}`;
208
+ if (((_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) === true) {
209
+ str += " [deprecated = true]";
210
+ }
211
+ return str;
212
+ },
213
+ getComments() {
214
+ const path = [
215
+ ...this.parent.getComments().sourcePath,
216
+ FieldNumber.EnumDescriptorProto_Value,
217
+ this.parent.proto.value.indexOf(this.proto),
218
+ ];
219
+ return findComments(file.proto.sourceCodeInfo, path);
220
+ },
221
+ });
222
+ });
223
+ ((_c = parent === null || parent === void 0 ? void 0 : parent.nestedEnums) !== null && _c !== void 0 ? _c : file.enums).push(desc);
224
+ }
225
+ /**
226
+ * Create a descriptor for a message, including nested types, and add it to our
227
+ * cart. Note that this does not create descriptors fields.
228
+ */
229
+ function addMessage(proto, file, parent, cart) {
230
+ var _a, _b, _c, _d;
231
+ (0, assert_js_1.assert)(proto.name, `invalid DescriptorProto: missing name`);
232
+ const desc = {
233
+ kind: "message",
234
+ proto,
235
+ deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
236
+ file,
237
+ parent,
238
+ name: proto.name,
239
+ typeName: makeTypeName(proto, parent, file),
240
+ fields: [],
241
+ oneofs: [],
242
+ members: [],
243
+ nestedEnums: [],
244
+ nestedMessages: [],
245
+ nestedExtensions: [],
246
+ toString() {
247
+ return `message ${this.typeName}`;
248
+ },
249
+ getComments() {
250
+ const path = this.parent
251
+ ? [
252
+ ...this.parent.getComments().sourcePath,
253
+ FieldNumber.DescriptorProto_NestedType,
254
+ this.parent.proto.nestedType.indexOf(this.proto),
255
+ ]
256
+ : [
257
+ FieldNumber.FileDescriptorProto_MessageType,
258
+ this.file.proto.messageType.indexOf(this.proto),
259
+ ];
260
+ return findComments(file.proto.sourceCodeInfo, path);
261
+ },
262
+ };
263
+ if (((_c = proto.options) === null || _c === void 0 ? void 0 : _c.mapEntry) === true) {
264
+ cart.mapEntries.set(desc.typeName, desc);
265
+ }
266
+ else {
267
+ ((_d = parent === null || parent === void 0 ? void 0 : parent.nestedMessages) !== null && _d !== void 0 ? _d : file.messages).push(desc);
268
+ cart.messages.set(desc.typeName, desc);
269
+ }
270
+ for (const enumProto of proto.enumType) {
271
+ addEnum(enumProto, file, desc, cart);
272
+ }
273
+ for (const messageProto of proto.nestedType) {
274
+ addMessage(messageProto, file, desc, cart);
275
+ }
276
+ }
277
+ /**
278
+ * Create a descriptor for a service, including methods, and add it to our
279
+ * cart.
280
+ */
281
+ function addService(proto, file, cart) {
282
+ var _a, _b;
283
+ (0, assert_js_1.assert)(proto.name, `invalid ServiceDescriptorProto: missing name`);
284
+ const desc = {
285
+ kind: "service",
286
+ proto,
287
+ deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
288
+ file,
289
+ name: proto.name,
290
+ typeName: makeTypeName(proto, undefined, file),
291
+ methods: [],
292
+ toString() {
293
+ return `service ${this.typeName}`;
294
+ },
295
+ getComments() {
296
+ const path = [
297
+ FieldNumber.FileDescriptorProto_Service,
298
+ this.file.proto.service.indexOf(this.proto),
299
+ ];
300
+ return findComments(file.proto.sourceCodeInfo, path);
301
+ },
302
+ };
303
+ file.services.push(desc);
304
+ cart.services.set(desc.typeName, desc);
305
+ for (const methodProto of proto.method) {
306
+ desc.methods.push(newMethod(methodProto, desc, cart));
307
+ }
308
+ }
309
+ /**
310
+ * Create a descriptor for a method.
311
+ */
312
+ function newMethod(proto, parent, cart) {
313
+ var _a, _b, _c;
314
+ (0, assert_js_1.assert)(proto.name, `invalid MethodDescriptorProto: missing name`);
315
+ (0, assert_js_1.assert)(proto.inputType, `invalid MethodDescriptorProto: missing input_type`);
316
+ (0, assert_js_1.assert)(proto.outputType, `invalid MethodDescriptorProto: missing output_type`);
317
+ let methodKind;
318
+ if (proto.clientStreaming === true && proto.serverStreaming === true) {
319
+ methodKind = service_type_js_1.MethodKind.BiDiStreaming;
320
+ }
321
+ else if (proto.clientStreaming === true) {
322
+ methodKind = service_type_js_1.MethodKind.ClientStreaming;
323
+ }
324
+ else if (proto.serverStreaming === true) {
325
+ methodKind = service_type_js_1.MethodKind.ServerStreaming;
326
+ }
327
+ else {
328
+ methodKind = service_type_js_1.MethodKind.Unary;
329
+ }
330
+ let idempotency;
331
+ switch ((_a = proto.options) === null || _a === void 0 ? void 0 : _a.idempotencyLevel) {
332
+ case descriptor_pb_js_1.MethodOptions_IdempotencyLevel.IDEMPOTENT:
333
+ idempotency = service_type_js_1.MethodIdempotency.Idempotent;
334
+ break;
335
+ case descriptor_pb_js_1.MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS:
336
+ idempotency = service_type_js_1.MethodIdempotency.NoSideEffects;
337
+ break;
338
+ case descriptor_pb_js_1.MethodOptions_IdempotencyLevel.IDEMPOTENCY_UNKNOWN:
339
+ case undefined:
340
+ idempotency = undefined;
341
+ break;
342
+ }
343
+ const input = cart.messages.get(trimLeadingDot(proto.inputType));
344
+ const output = cart.messages.get(trimLeadingDot(proto.outputType));
345
+ (0, assert_js_1.assert)(input, `invalid MethodDescriptorProto: input_type ${proto.inputType} not found`);
346
+ (0, assert_js_1.assert)(output, `invalid MethodDescriptorProto: output_type ${proto.inputType} not found`);
347
+ const name = proto.name;
348
+ return {
349
+ kind: "rpc",
350
+ proto,
351
+ deprecated: (_c = (_b = proto.options) === null || _b === void 0 ? void 0 : _b.deprecated) !== null && _c !== void 0 ? _c : false,
352
+ parent,
353
+ name,
354
+ methodKind,
355
+ input,
356
+ output,
357
+ idempotency,
358
+ toString() {
359
+ return `rpc ${parent.typeName}.${name}`;
360
+ },
361
+ getComments() {
362
+ const path = [
363
+ ...this.parent.getComments().sourcePath,
364
+ FieldNumber.ServiceDescriptorProto_Method,
365
+ this.parent.proto.method.indexOf(this.proto),
366
+ ];
367
+ return findComments(parent.file.proto.sourceCodeInfo, path);
368
+ },
369
+ };
370
+ }
371
+ /**
372
+ * Create a descriptor for a oneof group.
373
+ */
374
+ function newOneof(proto, parent) {
375
+ (0, assert_js_1.assert)(proto.name, `invalid OneofDescriptorProto: missing name`);
376
+ return {
377
+ kind: "oneof",
378
+ proto,
379
+ deprecated: false,
380
+ parent,
381
+ fields: [],
382
+ name: proto.name,
383
+ toString() {
384
+ return `oneof ${parent.typeName}.${this.name}`;
385
+ },
386
+ getComments() {
387
+ const path = [
388
+ ...this.parent.getComments().sourcePath,
389
+ FieldNumber.DescriptorProto_OneofDecl,
390
+ this.parent.proto.oneofDecl.indexOf(this.proto),
391
+ ];
392
+ return findComments(parent.file.proto.sourceCodeInfo, path);
393
+ },
394
+ };
395
+ }
396
+ /**
397
+ * Create a descriptor for a field.
398
+ */
399
+ function newField(proto, file, parent, oneof, cart) {
400
+ var _a, _b, _c, _d;
401
+ (0, assert_js_1.assert)(proto.name, `invalid FieldDescriptorProto: missing name`);
402
+ (0, assert_js_1.assert)(proto.number, `invalid FieldDescriptorProto: missing number`);
403
+ (0, assert_js_1.assert)(proto.type, `invalid FieldDescriptorProto: missing type`);
404
+ const packedByDefault = isPackedFieldByDefault(proto, file.syntax);
405
+ const common = {
406
+ proto,
407
+ deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
408
+ name: proto.name,
409
+ number: proto.number,
410
+ parent,
411
+ oneof,
412
+ optional: isOptionalField(proto, file.syntax),
413
+ packed: (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.packed) !== null && _d !== void 0 ? _d : packedByDefault,
414
+ packedByDefault,
415
+ jsonName: proto.jsonName === (0, names_js_1.fieldJsonName)(proto.name) ? undefined : proto.jsonName,
416
+ scalar: undefined,
417
+ message: undefined,
418
+ enum: undefined,
419
+ mapKey: undefined,
420
+ mapValue: undefined,
421
+ toString() {
422
+ // note that newExtension() calls us with parent = null
423
+ return `field ${this.parent.typeName}.${this.name}`;
424
+ },
425
+ declarationString,
426
+ getComments() {
427
+ const path = [
428
+ ...this.parent.getComments().sourcePath,
429
+ FieldNumber.DescriptorProto_Field,
430
+ this.parent.proto.field.indexOf(this.proto),
431
+ ];
432
+ return findComments(file.proto.sourceCodeInfo, path);
433
+ },
434
+ };
435
+ const repeated = proto.label === descriptor_pb_js_1.FieldDescriptorProto_Label.REPEATED;
436
+ switch (proto.type) {
437
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.MESSAGE:
438
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.GROUP: {
439
+ (0, assert_js_1.assert)(proto.typeName, `invalid FieldDescriptorProto: missing type_name`);
440
+ const mapEntry = cart.mapEntries.get(trimLeadingDot(proto.typeName));
441
+ if (mapEntry !== undefined) {
442
+ (0, assert_js_1.assert)(repeated, `invalid FieldDescriptorProto: expected map entry to be repeated`);
443
+ return Object.assign(Object.assign(Object.assign({}, common), { kind: "map_field", repeated: false }), getMapFieldTypes(mapEntry));
444
+ }
445
+ const message = cart.messages.get(trimLeadingDot(proto.typeName));
446
+ (0, assert_js_1.assert)(message !== undefined, `invalid FieldDescriptorProto: type_name ${proto.typeName} not found`);
447
+ return Object.assign(Object.assign({}, common), { kind: "message_field", repeated,
448
+ message });
449
+ }
450
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.ENUM: {
451
+ (0, assert_js_1.assert)(proto.typeName, `invalid FieldDescriptorProto: missing type_name`);
452
+ const e = cart.enums.get(trimLeadingDot(proto.typeName));
453
+ (0, assert_js_1.assert)(e !== undefined, `invalid FieldDescriptorProto: type_name ${proto.typeName} not found`);
454
+ return Object.assign(Object.assign({}, common), { kind: "enum_field", getDefaultValue,
455
+ repeated, enum: e });
456
+ }
457
+ default: {
458
+ const scalar = fieldTypeToScalarType[proto.type];
459
+ (0, assert_js_1.assert)(scalar, `invalid FieldDescriptorProto: unknown type ${proto.type}`);
460
+ return Object.assign(Object.assign({}, common), { kind: "scalar_field", getDefaultValue,
461
+ repeated,
462
+ scalar });
463
+ }
464
+ }
465
+ }
466
+ /**
467
+ * Create a descriptor for an extension field.
468
+ */
469
+ function newExtension(proto, file, parent, cart) {
470
+ (0, assert_js_1.assert)(proto.extendee, `invalid FieldDescriptorProto: missing extendee`);
471
+ const field = newField(proto, file, null, // to safe us many lines of duplicated code, we trick the type system
472
+ undefined, cart);
473
+ const extendee = cart.messages.get(trimLeadingDot(proto.extendee));
474
+ (0, assert_js_1.assert)(extendee, `invalid FieldDescriptorProto: extendee ${proto.extendee} not found`);
475
+ return Object.assign(Object.assign({}, field), { typeName: makeTypeName(proto, parent, file), parent,
476
+ file,
477
+ extendee,
478
+ toString() {
479
+ return `extension ${this.typeName}`;
480
+ },
481
+ getComments() {
482
+ const path = this.parent
483
+ ? [
484
+ ...this.parent.getComments().sourcePath,
485
+ FieldNumber.DescriptorProto_Extension,
486
+ this.parent.proto.extension.indexOf(proto),
487
+ ]
488
+ : [
489
+ FieldNumber.FileDescriptorProto_Extension,
490
+ this.file.proto.extension.indexOf(proto),
491
+ ];
492
+ return findComments(file.proto.sourceCodeInfo, path);
493
+ } });
494
+ }
495
+ /**
496
+ * Create a fully qualified name for a protobuf type or extension field.
497
+ *
498
+ * The fully qualified name for messages, enumerations, and services is
499
+ * constructed by concatenating the package name (if present), parent
500
+ * message names (for nested types), and the type name. We omit the leading
501
+ * dot added by protobuf compilers. Examples:
502
+ * - mypackage.MyMessage
503
+ * - mypackage.MyMessage.NestedMessage
504
+ *
505
+ * The fully qualified name for extension fields is constructed by
506
+ * concatenating the package name (if present), parent message names (for
507
+ * extensions declared within a message), and the field name. Examples:
508
+ * - mypackage.extfield
509
+ * - mypackage.MyMessage.extfield
510
+ */
511
+ function makeTypeName(proto, parent, file) {
512
+ (0, assert_js_1.assert)(proto.name, `invalid ${proto.getType().typeName}: missing name`);
513
+ let typeName;
514
+ if (parent) {
515
+ typeName = `${parent.typeName}.${proto.name}`;
516
+ }
517
+ else if (file.proto.package !== undefined) {
518
+ typeName = `${file.proto.package}.${proto.name}`;
519
+ }
520
+ else {
521
+ typeName = `${proto.name}`;
522
+ }
523
+ return typeName;
524
+ }
525
+ /**
526
+ * Remove the leading dot from a fully qualified type name.
527
+ */
528
+ function trimLeadingDot(typeName) {
529
+ return typeName.startsWith(".") ? typeName.substring(1) : typeName;
530
+ }
531
+ function getMapFieldTypes(mapEntry) {
532
+ var _a, _b;
533
+ (0, assert_js_1.assert)((_a = mapEntry.proto.options) === null || _a === void 0 ? void 0 : _a.mapEntry, `invalid DescriptorProto: expected ${mapEntry.toString()} to be a map entry`);
534
+ (0, assert_js_1.assert)(mapEntry.fields.length === 2, `invalid DescriptorProto: map entry ${mapEntry.toString()} has ${mapEntry.fields.length} fields`);
535
+ const keyField = mapEntry.fields.find((f) => f.proto.number === 1);
536
+ (0, assert_js_1.assert)(keyField, `invalid DescriptorProto: map entry ${mapEntry.toString()} is missing key field`);
537
+ const mapKey = keyField.scalar;
538
+ (0, assert_js_1.assert)(mapKey !== undefined &&
539
+ mapKey !== field_js_1.ScalarType.BYTES &&
540
+ mapKey !== field_js_1.ScalarType.FLOAT &&
541
+ mapKey !== field_js_1.ScalarType.DOUBLE, `invalid DescriptorProto: map entry ${mapEntry.toString()} has unexpected key type ${(_b = keyField.proto.type) !== null && _b !== void 0 ? _b : -1}`);
542
+ const valueField = mapEntry.fields.find((f) => f.proto.number === 2);
543
+ (0, assert_js_1.assert)(valueField, `invalid DescriptorProto: map entry ${mapEntry.toString()} is missing value field`);
544
+ switch (valueField.kind) {
545
+ case "scalar_field":
546
+ return {
547
+ mapKey,
548
+ mapValue: Object.assign(Object.assign({}, valueField), { kind: "scalar" }),
549
+ };
550
+ case "message_field":
551
+ return {
552
+ mapKey,
553
+ mapValue: Object.assign(Object.assign({}, valueField), { kind: "message" }),
554
+ };
555
+ case "enum_field":
556
+ return {
557
+ mapKey,
558
+ mapValue: Object.assign(Object.assign({}, valueField), { kind: "enum" }),
559
+ };
560
+ default:
561
+ throw new Error("invalid DescriptorProto: unsupported map entry value field");
562
+ }
563
+ }
564
+ /**
565
+ * Did the user put the field in a oneof group?
566
+ * This handles proto3 optionals.
567
+ */
568
+ function findOneof(proto, allOneofs) {
569
+ var _a;
570
+ const oneofIndex = proto.oneofIndex;
571
+ if (oneofIndex === undefined) {
572
+ return undefined;
573
+ }
574
+ let oneof;
575
+ if (proto.proto3Optional !== true) {
576
+ oneof = allOneofs[oneofIndex];
577
+ (0, assert_js_1.assert)(oneof, `invalid FieldDescriptorProto: oneof #${oneofIndex} for field #${(_a = proto.number) !== null && _a !== void 0 ? _a : -1} not found`);
578
+ }
579
+ return oneof;
580
+ }
581
+ /**
582
+ * Did the user use the `optional` keyword?
583
+ * This handles proto3 optionals.
584
+ */
585
+ function isOptionalField(proto, syntax) {
586
+ switch (syntax) {
587
+ case "proto2":
588
+ return (proto.oneofIndex === undefined &&
589
+ proto.label === descriptor_pb_js_1.FieldDescriptorProto_Label.OPTIONAL);
590
+ case "proto3":
591
+ return proto.proto3Optional === true;
592
+ }
593
+ }
594
+ /**
595
+ * Get the default `packed` state of a repeated field.
596
+ */
597
+ function isPackedFieldByDefault(proto, syntax) {
598
+ (0, assert_js_1.assert)(proto.type, `invalid FieldDescriptorProto: missing type`);
599
+ if (syntax === "proto3") {
600
+ switch (proto.type) {
601
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.DOUBLE:
602
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.FLOAT:
603
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.INT64:
604
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.UINT64:
605
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.INT32:
606
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.FIXED64:
607
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.FIXED32:
608
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.UINT32:
609
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.SFIXED32:
610
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.SFIXED64:
611
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.SINT32:
612
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.SINT64:
613
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.BOOL:
614
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.ENUM:
615
+ // From the proto3 language guide:
616
+ // > In proto3, repeated fields of scalar numeric types are packed by default.
617
+ // This information is incomplete - according to the conformance tests, BOOL
618
+ // and ENUM are packed by default as well. This means only STRING and BYTES
619
+ // are not packed by default, which makes sense because they are length-delimited.
620
+ return true;
621
+ default:
622
+ return false;
623
+ }
624
+ }
625
+ return false;
626
+ }
627
+ exports.isPackedFieldByDefault = isPackedFieldByDefault;
628
+ /**
629
+ * Map from a compiler-generated field type to our ScalarType, which is a
630
+ * subset of field types declared by protobuf enum google.protobuf.FieldDescriptorProto.
631
+ */
632
+ const fieldTypeToScalarType = {
633
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.DOUBLE]: field_js_1.ScalarType.DOUBLE,
634
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.FLOAT]: field_js_1.ScalarType.FLOAT,
635
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.INT64]: field_js_1.ScalarType.INT64,
636
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.UINT64]: field_js_1.ScalarType.UINT64,
637
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.INT32]: field_js_1.ScalarType.INT32,
638
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.FIXED64]: field_js_1.ScalarType.FIXED64,
639
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.FIXED32]: field_js_1.ScalarType.FIXED32,
640
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.BOOL]: field_js_1.ScalarType.BOOL,
641
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.STRING]: field_js_1.ScalarType.STRING,
642
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.GROUP]: undefined,
643
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.MESSAGE]: undefined,
644
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.BYTES]: field_js_1.ScalarType.BYTES,
645
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.UINT32]: field_js_1.ScalarType.UINT32,
646
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.ENUM]: undefined,
647
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.SFIXED32]: field_js_1.ScalarType.SFIXED32,
648
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.SFIXED64]: field_js_1.ScalarType.SFIXED64,
649
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.SINT32]: field_js_1.ScalarType.SINT32,
650
+ [descriptor_pb_js_1.FieldDescriptorProto_Type.SINT64]: field_js_1.ScalarType.SINT64,
651
+ };
652
+ /**
653
+ * Find comments.
654
+ */
655
+ function findComments(sourceCodeInfo, sourcePath) {
656
+ if (!sourceCodeInfo) {
657
+ return {
658
+ leadingDetached: [],
659
+ sourcePath,
660
+ };
661
+ }
662
+ for (const location of sourceCodeInfo.location) {
663
+ if (location.path.length !== sourcePath.length) {
664
+ continue;
665
+ }
666
+ if (location.path.some((value, index) => sourcePath[index] !== value)) {
667
+ continue;
668
+ }
669
+ return {
670
+ leadingDetached: location.leadingDetachedComments,
671
+ leading: location.leadingComments,
672
+ trailing: location.trailingComments,
673
+ sourcePath,
674
+ };
675
+ }
676
+ return {
677
+ leadingDetached: [],
678
+ sourcePath,
679
+ };
680
+ }
681
+ /**
682
+ * The following field numbers are used to find comments in
683
+ * google.protobuf.SourceCodeInfo.
684
+ */
685
+ var FieldNumber;
686
+ (function (FieldNumber) {
687
+ FieldNumber[FieldNumber["FileDescriptorProto_Package"] = 2] = "FileDescriptorProto_Package";
688
+ FieldNumber[FieldNumber["FileDescriptorProto_MessageType"] = 4] = "FileDescriptorProto_MessageType";
689
+ FieldNumber[FieldNumber["FileDescriptorProto_EnumType"] = 5] = "FileDescriptorProto_EnumType";
690
+ FieldNumber[FieldNumber["FileDescriptorProto_Service"] = 6] = "FileDescriptorProto_Service";
691
+ FieldNumber[FieldNumber["FileDescriptorProto_Extension"] = 7] = "FileDescriptorProto_Extension";
692
+ FieldNumber[FieldNumber["FileDescriptorProto_Syntax"] = 12] = "FileDescriptorProto_Syntax";
693
+ FieldNumber[FieldNumber["DescriptorProto_Field"] = 2] = "DescriptorProto_Field";
694
+ FieldNumber[FieldNumber["DescriptorProto_NestedType"] = 3] = "DescriptorProto_NestedType";
695
+ FieldNumber[FieldNumber["DescriptorProto_EnumType"] = 4] = "DescriptorProto_EnumType";
696
+ FieldNumber[FieldNumber["DescriptorProto_Extension"] = 6] = "DescriptorProto_Extension";
697
+ FieldNumber[FieldNumber["DescriptorProto_OneofDecl"] = 8] = "DescriptorProto_OneofDecl";
698
+ FieldNumber[FieldNumber["EnumDescriptorProto_Value"] = 2] = "EnumDescriptorProto_Value";
699
+ FieldNumber[FieldNumber["ServiceDescriptorProto_Method"] = 2] = "ServiceDescriptorProto_Method";
700
+ })(FieldNumber || (FieldNumber = {}));
701
+ /**
702
+ * Return a string that matches the definition of a field in the protobuf
703
+ * source. Does not take custom options into account.
704
+ */
705
+ function declarationString() {
706
+ var _a, _b;
707
+ const parts = [];
708
+ if (this.repeated) {
709
+ parts.push("repeated");
710
+ }
711
+ if (this.optional) {
712
+ parts.push("optional");
713
+ }
714
+ const file = "extendee" in this ? this.file : this.parent.file;
715
+ if (file.syntax == "proto2" &&
716
+ this.proto.label === descriptor_pb_js_1.FieldDescriptorProto_Label.REQUIRED) {
717
+ parts.push("required");
718
+ }
719
+ let type;
720
+ switch (this.kind) {
721
+ case "scalar_field":
722
+ type = field_js_1.ScalarType[this.scalar].toLowerCase();
723
+ break;
724
+ case "enum_field":
725
+ type = this.enum.typeName;
726
+ break;
727
+ case "message_field":
728
+ type = this.message.typeName;
729
+ break;
730
+ case "map_field": {
731
+ const k = field_js_1.ScalarType[this.mapKey].toLowerCase();
732
+ let v;
733
+ switch (this.mapValue.kind) {
734
+ case "scalar":
735
+ v = field_js_1.ScalarType[this.mapValue.scalar].toLowerCase();
736
+ break;
737
+ case "enum":
738
+ v = this.mapValue.enum.typeName;
739
+ break;
740
+ case "message":
741
+ v = this.mapValue.message.typeName;
742
+ break;
743
+ }
744
+ type = `map<${k}, ${v}>`;
745
+ break;
746
+ }
747
+ }
748
+ parts.push(`${type} ${this.name} = ${this.number}`);
749
+ const options = [];
750
+ if (((_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.packed) !== undefined) {
751
+ options.push(`packed = ${this.proto.options.packed.toString()}`);
752
+ }
753
+ let defaultValue = this.proto.defaultValue;
754
+ if (defaultValue !== undefined) {
755
+ if (this.proto.type == descriptor_pb_js_1.FieldDescriptorProto_Type.BYTES ||
756
+ this.proto.type == descriptor_pb_js_1.FieldDescriptorProto_Type.STRING) {
757
+ defaultValue = '"' + defaultValue.replace('"', '\\"') + '"';
758
+ }
759
+ options.push(`default = ${defaultValue}`);
760
+ }
761
+ if (this.jsonName !== undefined) {
762
+ options.push(`json_name = "${this.jsonName}"`);
763
+ }
764
+ if (((_b = this.proto.options) === null || _b === void 0 ? void 0 : _b.deprecated) === true) {
765
+ options.push(`deprecated = true`);
766
+ }
767
+ if (options.length > 0) {
768
+ parts.push("[" + options.join(", ") + "]");
769
+ }
770
+ return parts.join(" ");
771
+ }
772
+ /**
773
+ * Parses a text-encoded default value (proto2) of a scalar or enum field.
774
+ */
775
+ function getDefaultValue() {
776
+ const d = this.proto.defaultValue;
777
+ if (d === undefined) {
778
+ return undefined;
779
+ }
780
+ switch (this.kind) {
781
+ case "enum_field": {
782
+ const enumValue = this.enum.values.find((v) => v.name === d);
783
+ (0, assert_js_1.assert)(enumValue, `cannot parse ${this.toString()} default value: ${d}`);
784
+ return enumValue.number;
785
+ }
786
+ case "scalar_field":
787
+ switch (this.scalar) {
788
+ case field_js_1.ScalarType.STRING:
789
+ return d;
790
+ case field_js_1.ScalarType.BYTES: {
791
+ const u = unescapeBytesDefaultValue(d);
792
+ if (u === false) {
793
+ throw new Error(`cannot parse ${this.toString()} default value: ${d}`);
794
+ }
795
+ return u;
796
+ }
797
+ case field_js_1.ScalarType.INT64:
798
+ case field_js_1.ScalarType.SFIXED64:
799
+ case field_js_1.ScalarType.SINT64:
800
+ return proto_int64_js_1.protoInt64.parse(d);
801
+ case field_js_1.ScalarType.UINT64:
802
+ case field_js_1.ScalarType.FIXED64:
803
+ return proto_int64_js_1.protoInt64.uParse(d);
804
+ case field_js_1.ScalarType.DOUBLE:
805
+ case field_js_1.ScalarType.FLOAT:
806
+ switch (d) {
807
+ case "inf":
808
+ return Number.POSITIVE_INFINITY;
809
+ case "-inf":
810
+ return Number.NEGATIVE_INFINITY;
811
+ case "nan":
812
+ return Number.NaN;
813
+ default:
814
+ return parseFloat(d);
815
+ }
816
+ case field_js_1.ScalarType.BOOL:
817
+ return d === "true";
818
+ case field_js_1.ScalarType.INT32:
819
+ case field_js_1.ScalarType.UINT32:
820
+ case field_js_1.ScalarType.SINT32:
821
+ case field_js_1.ScalarType.FIXED32:
822
+ case field_js_1.ScalarType.SFIXED32:
823
+ return parseInt(d, 10);
824
+ }
825
+ break;
826
+ default:
827
+ return undefined;
828
+ }
829
+ }
830
+ /**
831
+ * Parses a text-encoded default value (proto2) of a BYTES field.
832
+ */
833
+ function unescapeBytesDefaultValue(str) {
834
+ const b = [];
835
+ const input = {
836
+ tail: str,
837
+ c: "",
838
+ next() {
839
+ if (this.tail.length == 0) {
840
+ return false;
841
+ }
842
+ this.c = this.tail[0];
843
+ this.tail = this.tail.substring(1);
844
+ return true;
845
+ },
846
+ take(n) {
847
+ if (this.tail.length >= n) {
848
+ const r = this.tail.substring(0, n);
849
+ this.tail = this.tail.substring(n);
850
+ return r;
851
+ }
852
+ return false;
853
+ },
854
+ };
855
+ while (input.next()) {
856
+ switch (input.c) {
857
+ case "\\":
858
+ if (input.next()) {
859
+ switch (input.c) {
860
+ case "\\":
861
+ b.push(input.c.charCodeAt(0));
862
+ break;
863
+ case "b":
864
+ b.push(0x08);
865
+ break;
866
+ case "f":
867
+ b.push(0x0c);
868
+ break;
869
+ case "n":
870
+ b.push(0x0a);
871
+ break;
872
+ case "r":
873
+ b.push(0x0d);
874
+ break;
875
+ case "t":
876
+ b.push(0x09);
877
+ break;
878
+ case "v":
879
+ b.push(0x0b);
880
+ break;
881
+ case "0":
882
+ case "1":
883
+ case "2":
884
+ case "3":
885
+ case "4":
886
+ case "5":
887
+ case "6":
888
+ case "7": {
889
+ const s = input.c;
890
+ const t = input.take(2);
891
+ if (t === false) {
892
+ return false;
893
+ }
894
+ const n = parseInt(s + t, 8);
895
+ if (isNaN(n)) {
896
+ return false;
897
+ }
898
+ b.push(n);
899
+ break;
900
+ }
901
+ case "x": {
902
+ const s = input.c;
903
+ const t = input.take(2);
904
+ if (t === false) {
905
+ return false;
906
+ }
907
+ const n = parseInt(s + t, 16);
908
+ if (isNaN(n)) {
909
+ return false;
910
+ }
911
+ b.push(n);
912
+ break;
913
+ }
914
+ case "u": {
915
+ const s = input.c;
916
+ const t = input.take(4);
917
+ if (t === false) {
918
+ return false;
919
+ }
920
+ const n = parseInt(s + t, 16);
921
+ if (isNaN(n)) {
922
+ return false;
923
+ }
924
+ const chunk = new Uint8Array(4);
925
+ const view = new DataView(chunk.buffer);
926
+ view.setInt32(0, n, true);
927
+ b.push(chunk[0], chunk[1], chunk[2], chunk[3]);
928
+ break;
929
+ }
930
+ case "U": {
931
+ const s = input.c;
932
+ const t = input.take(8);
933
+ if (t === false) {
934
+ return false;
935
+ }
936
+ const tc = proto_int64_js_1.protoInt64.uEnc(s + t);
937
+ const chunk = new Uint8Array(8);
938
+ const view = new DataView(chunk.buffer);
939
+ view.setInt32(0, tc.lo, true);
940
+ view.setInt32(4, tc.hi, true);
941
+ b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]);
942
+ break;
943
+ }
944
+ }
945
+ }
946
+ break;
947
+ default:
948
+ b.push(input.c.charCodeAt(0));
949
+ }
950
+ }
951
+ return new Uint8Array(b);
952
+ }