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