@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.
- package/README.md +34 -4
- package/dist/cjs/binary-encoding.js +2 -2
- package/dist/cjs/codegen-info.js +60 -0
- package/dist/cjs/create-descriptor-set.js +952 -0
- package/dist/cjs/create-registry-from-desc.js +376 -0
- package/dist/cjs/create-registry.js +75 -0
- package/dist/cjs/descriptor-set.js +0 -458
- package/dist/cjs/google/protobuf/any_pb.js +4 -2
- package/dist/cjs/google/protobuf/duration_pb.js +1 -1
- package/dist/cjs/google/protobuf/struct_pb.js +2 -2
- package/dist/cjs/google/protobuf/type_pb.js +1 -1
- package/dist/cjs/index.js +17 -7
- package/dist/cjs/{descriptor-registry.js → legacy-descriptor-registry.js} +35 -69
- package/dist/cjs/legacy-descriptor-set.js +453 -0
- package/dist/cjs/legacy-type-registry.js +104 -0
- package/dist/cjs/message.js +2 -1
- package/dist/cjs/private/binary-format-common.js +4 -3
- package/dist/cjs/private/binary-format-proto2.js +3 -6
- package/dist/cjs/private/binary-format-proto3.js +2 -5
- package/dist/cjs/private/enum.js +22 -31
- package/dist/cjs/private/field-wrapper.js +30 -1
- package/dist/cjs/private/field.js +1 -1
- package/dist/cjs/private/json-format-common.js +6 -5
- package/dist/cjs/private/message-type.js +2 -1
- package/dist/cjs/private/names.js +168 -29
- package/dist/cjs/proto-int64.js +1 -1
- package/dist/cjs/proto2.js +7 -9
- package/dist/cjs/proto3.js +10 -13
- package/dist/cjs/type-registry.js +0 -87
- package/dist/esm/binary-encoding.js +2 -2
- package/dist/esm/codegen-info.js +57 -0
- package/dist/esm/create-descriptor-set.js +947 -0
- package/dist/esm/create-registry-from-desc.js +371 -0
- package/dist/esm/create-registry.js +71 -0
- package/dist/esm/descriptor-set.js +1 -456
- package/dist/esm/google/protobuf/any_pb.js +4 -2
- package/dist/esm/google/protobuf/duration_pb.js +1 -1
- package/dist/esm/google/protobuf/struct_pb.js +2 -2
- package/dist/esm/google/protobuf/type_pb.js +1 -1
- package/dist/esm/index.js +9 -3
- package/dist/esm/{descriptor-registry.js → legacy-descriptor-registry.js} +33 -67
- package/dist/esm/legacy-descriptor-set.js +449 -0
- package/dist/esm/legacy-type-registry.js +100 -0
- package/dist/esm/message.js +2 -1
- package/dist/esm/private/binary-format-common.js +4 -3
- package/dist/esm/private/binary-format-proto2.js +3 -6
- package/dist/esm/private/binary-format-proto3.js +2 -5
- package/dist/esm/private/enum.js +22 -31
- package/dist/esm/private/field-wrapper.js +28 -0
- package/dist/esm/private/field.js +2 -2
- package/dist/esm/private/json-format-common.js +6 -5
- package/dist/esm/private/message-type.js +2 -1
- package/dist/esm/private/names.js +163 -24
- package/dist/esm/proto-int64.js +1 -1
- package/dist/esm/proto2.js +8 -10
- package/dist/esm/proto3.js +11 -14
- package/dist/esm/type-registry.js +1 -85
- package/dist/types/codegen-info.d.ts +19 -0
- package/dist/types/create-descriptor-set.d.ts +16 -0
- package/dist/types/create-registry-from-desc.d.ts +49 -0
- package/dist/types/create-registry.d.ts +8 -0
- package/dist/types/descriptor-set.d.ts +554 -108
- package/dist/types/enum.d.ts +9 -5
- package/dist/types/index.d.ts +9 -3
- package/dist/types/{descriptor-registry.d.ts → legacy-descriptor-registry.d.ts} +8 -6
- package/dist/types/legacy-descriptor-set.d.ts +152 -0
- package/dist/types/legacy-type-registry.d.ts +44 -0
- package/dist/types/private/enum.d.ts +4 -8
- package/dist/types/private/field-wrapper.d.ts +7 -0
- package/dist/types/private/message-type.d.ts +1 -4
- package/dist/types/private/names.d.ts +26 -8
- package/dist/types/private/proto-runtime.d.ts +2 -2
- package/dist/types/private/util.d.ts +1 -6
- package/dist/types/type-registry.d.ts +0 -38
- package/package.json +4 -3
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
// See the License for the specific language governing permissions and
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.
|
|
16
|
+
exports.LegacyDescriptorRegistry = void 0;
|
|
17
|
+
const descriptor_pb_js_1 = require("./google/protobuf/descriptor_pb.js");
|
|
17
18
|
const assert_js_1 = require("./private/assert.js");
|
|
18
19
|
const proto3_js_1 = require("./proto3.js");
|
|
19
20
|
const proto2_js_1 = require("./proto2.js");
|
|
20
21
|
const field_js_1 = require("./field.js");
|
|
21
|
-
const
|
|
22
|
+
const legacy_descriptor_set_js_1 = require("./legacy-descriptor-set.js");
|
|
22
23
|
const proto_int64_js_1 = require("./proto-int64.js");
|
|
23
|
-
const names_js_1 = require("./private/names.js");
|
|
24
24
|
const timestamp_pb_js_1 = require("./google/protobuf/timestamp_pb.js");
|
|
25
25
|
const duration_pb_js_1 = require("./google/protobuf/duration_pb.js");
|
|
26
26
|
const any_pb_js_1 = require("./google/protobuf/any_pb.js");
|
|
@@ -29,7 +29,6 @@ const field_mask_pb_js_1 = require("./google/protobuf/field_mask_pb.js");
|
|
|
29
29
|
const struct_pb_js_1 = require("./google/protobuf/struct_pb.js");
|
|
30
30
|
const enum_js_1 = require("./private/enum.js");
|
|
31
31
|
const wrappers_pb_js_1 = require("./google/protobuf/wrappers_pb.js");
|
|
32
|
-
const descriptor_pb_js_1 = require("./google/protobuf/descriptor_pb.js");
|
|
33
32
|
// well-known message types with specialized JSON representation
|
|
34
33
|
const wkMessages = [
|
|
35
34
|
any_pb_js_1.Any,
|
|
@@ -54,18 +53,20 @@ const wkMessages = [
|
|
|
54
53
|
// well-known enum types with specialized JSON representation
|
|
55
54
|
const wkEnums = [(0, enum_js_1.getEnumType)(struct_pb_js_1.NullValue)];
|
|
56
55
|
/**
|
|
57
|
-
*
|
|
56
|
+
* LegacyDescriptorRegistry is a type registry that dynamically creates types
|
|
58
57
|
* from a set of google.protobuf.FileDescriptorProto.
|
|
59
58
|
*
|
|
60
59
|
* By default, all well-known types with a specialized JSON representation
|
|
61
60
|
* are replaced with their generated counterpart in this package.
|
|
61
|
+
*
|
|
62
|
+
* @deprecated
|
|
62
63
|
*/
|
|
63
|
-
class
|
|
64
|
+
class LegacyDescriptorRegistry {
|
|
64
65
|
constructor(descriptorSet, replaceWkt = true) {
|
|
65
66
|
this.enums = {};
|
|
66
67
|
this.messages = {};
|
|
67
68
|
this.services = {};
|
|
68
|
-
this.ds = descriptorSet
|
|
69
|
+
this.ds = descriptorSet !== null && descriptorSet !== void 0 ? descriptorSet : new legacy_descriptor_set_js_1.LegacyDescriptorSet();
|
|
69
70
|
if (replaceWkt) {
|
|
70
71
|
for (const mt of wkMessages) {
|
|
71
72
|
this.messages["." + mt.typeName] = mt;
|
|
@@ -83,7 +84,7 @@ class DescriptorRegistry {
|
|
|
83
84
|
const set = bytesOrSet instanceof Uint8Array
|
|
84
85
|
? descriptor_pb_js_1.FileDescriptorSet.fromBinary(bytesOrSet)
|
|
85
86
|
: new descriptor_pb_js_1.FileDescriptorSet(bytesOrSet);
|
|
86
|
-
const dr = new
|
|
87
|
+
const dr = new LegacyDescriptorRegistry();
|
|
87
88
|
dr.add(...set.file);
|
|
88
89
|
return dr;
|
|
89
90
|
}
|
|
@@ -110,6 +111,7 @@ class DescriptorRegistry {
|
|
|
110
111
|
const type = runtime.makeEnumType(typeName, raw.values.map((u) => ({
|
|
111
112
|
no: u.number,
|
|
112
113
|
name: u.name,
|
|
114
|
+
localName: u.name,
|
|
113
115
|
})), {});
|
|
114
116
|
this.enums[protoTypeName] = type;
|
|
115
117
|
return type;
|
|
@@ -160,7 +162,7 @@ class DescriptorRegistry {
|
|
|
160
162
|
(0, assert_js_1.assert)(ot, `output message "${u.outputTypeName}" for ${u.toString()} not found`);
|
|
161
163
|
const m = {
|
|
162
164
|
name: u.name,
|
|
163
|
-
localName: (
|
|
165
|
+
localName: localMethodName(u.name),
|
|
164
166
|
I: it,
|
|
165
167
|
O: ot,
|
|
166
168
|
kind: u.kind,
|
|
@@ -175,10 +177,17 @@ class DescriptorRegistry {
|
|
|
175
177
|
});
|
|
176
178
|
}
|
|
177
179
|
}
|
|
178
|
-
exports.
|
|
180
|
+
exports.LegacyDescriptorRegistry = LegacyDescriptorRegistry;
|
|
181
|
+
function localMethodName(protoName) {
|
|
182
|
+
if (protoName.length == 0) {
|
|
183
|
+
return protoName;
|
|
184
|
+
}
|
|
185
|
+
return protoName[0].toLowerCase() + protoName.substring(1);
|
|
186
|
+
}
|
|
179
187
|
function makeTypeLocalName(type) {
|
|
188
|
+
var _a;
|
|
180
189
|
const typeName = type.typeName;
|
|
181
|
-
const packagePrefix = (type.file.proto.package
|
|
190
|
+
const packagePrefix = ((_a = type.file.proto.package) !== null && _a !== void 0 ? _a : "") + ".";
|
|
182
191
|
if (!typeName.startsWith(packagePrefix)) {
|
|
183
192
|
return type.name;
|
|
184
193
|
}
|
|
@@ -207,32 +216,23 @@ function makeMapFieldInfo(field, resolver) {
|
|
|
207
216
|
if (field.map.value.message) {
|
|
208
217
|
const messageType = resolver.findMessage(field.map.value.message.typeName);
|
|
209
218
|
(0, assert_js_1.assert)(messageType, `message "${field.map.value.message.typeName}" for ${field.toString()} not found`);
|
|
210
|
-
return {
|
|
211
|
-
...base,
|
|
212
|
-
V: {
|
|
219
|
+
return Object.assign(Object.assign({}, base), { V: {
|
|
213
220
|
kind: "message",
|
|
214
221
|
T: messageType,
|
|
215
|
-
}
|
|
216
|
-
};
|
|
222
|
+
} });
|
|
217
223
|
}
|
|
218
224
|
if (field.map.value.enum) {
|
|
219
225
|
const enumType = resolver.findEnum(field.map.value.enum.typeName);
|
|
220
226
|
(0, assert_js_1.assert)(enumType, `enum "${field.map.value.enum.typeName}" for ${field.toString()} not found`);
|
|
221
|
-
return {
|
|
222
|
-
...base,
|
|
223
|
-
V: {
|
|
227
|
+
return Object.assign(Object.assign({}, base), { V: {
|
|
224
228
|
kind: "enum",
|
|
225
229
|
T: enumType,
|
|
226
|
-
}
|
|
227
|
-
};
|
|
230
|
+
} });
|
|
228
231
|
}
|
|
229
|
-
return {
|
|
230
|
-
...base,
|
|
231
|
-
V: {
|
|
232
|
+
return Object.assign(Object.assign({}, base), { V: {
|
|
232
233
|
kind: "scalar",
|
|
233
234
|
T: field.map.value.scalarType,
|
|
234
|
-
}
|
|
235
|
-
};
|
|
235
|
+
} });
|
|
236
236
|
}
|
|
237
237
|
function makeScalarFieldInfo(field) {
|
|
238
238
|
const base = {
|
|
@@ -242,25 +242,13 @@ function makeScalarFieldInfo(field) {
|
|
|
242
242
|
T: field.scalarType,
|
|
243
243
|
};
|
|
244
244
|
if (field.repeated) {
|
|
245
|
-
return {
|
|
246
|
-
...base,
|
|
247
|
-
repeated: true,
|
|
248
|
-
packed: field.packed,
|
|
249
|
-
oneof: undefined,
|
|
250
|
-
T: field.scalarType,
|
|
251
|
-
};
|
|
245
|
+
return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined, T: field.scalarType });
|
|
252
246
|
}
|
|
253
247
|
if (field.oneof) {
|
|
254
|
-
return {
|
|
255
|
-
...base,
|
|
256
|
-
oneof: field.oneof.name,
|
|
257
|
-
};
|
|
248
|
+
return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
|
|
258
249
|
}
|
|
259
250
|
if (field.optional) {
|
|
260
|
-
return {
|
|
261
|
-
...base,
|
|
262
|
-
opt: true,
|
|
263
|
-
};
|
|
251
|
+
return Object.assign(Object.assign({}, base), { opt: true });
|
|
264
252
|
}
|
|
265
253
|
return base;
|
|
266
254
|
}
|
|
@@ -274,24 +262,13 @@ function makeMessageFieldInfo(field, resolver) {
|
|
|
274
262
|
T: messageType,
|
|
275
263
|
};
|
|
276
264
|
if (field.repeated) {
|
|
277
|
-
return {
|
|
278
|
-
...base,
|
|
279
|
-
repeated: true,
|
|
280
|
-
packed: field.packed,
|
|
281
|
-
oneof: undefined,
|
|
282
|
-
};
|
|
265
|
+
return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined });
|
|
283
266
|
}
|
|
284
267
|
if (field.oneof) {
|
|
285
|
-
return {
|
|
286
|
-
...base,
|
|
287
|
-
oneof: field.oneof.name,
|
|
288
|
-
};
|
|
268
|
+
return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
|
|
289
269
|
}
|
|
290
270
|
if (field.optional) {
|
|
291
|
-
return {
|
|
292
|
-
...base,
|
|
293
|
-
opt: true,
|
|
294
|
-
};
|
|
271
|
+
return Object.assign(Object.assign({}, base), { opt: true });
|
|
295
272
|
}
|
|
296
273
|
return base;
|
|
297
274
|
}
|
|
@@ -305,24 +282,13 @@ function makeEnumFieldInfo(field, resolver) {
|
|
|
305
282
|
T: enumType,
|
|
306
283
|
};
|
|
307
284
|
if (field.repeated) {
|
|
308
|
-
return {
|
|
309
|
-
...base,
|
|
310
|
-
repeated: true,
|
|
311
|
-
packed: field.packed,
|
|
312
|
-
oneof: undefined,
|
|
313
|
-
};
|
|
285
|
+
return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined });
|
|
314
286
|
}
|
|
315
287
|
if (field.oneof) {
|
|
316
|
-
return {
|
|
317
|
-
...base,
|
|
318
|
-
oneof: field.oneof.name,
|
|
319
|
-
};
|
|
288
|
+
return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
|
|
320
289
|
}
|
|
321
290
|
if (field.optional) {
|
|
322
|
-
return {
|
|
323
|
-
...base,
|
|
324
|
-
opt: true,
|
|
325
|
-
};
|
|
291
|
+
return Object.assign(Object.assign({}, base), { opt: true });
|
|
326
292
|
}
|
|
327
293
|
return base;
|
|
328
294
|
}
|
|
@@ -0,0 +1,453 @@
|
|
|
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
|
+
}
|