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