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