Skip to content

Commit 234f7f0

Browse files
committed
refactor(grpc-reflection): switch to using protobufjs library for message encoding/decoding
1 parent bc8f2ea commit 234f7f0

File tree

4 files changed

+120
-137
lines changed

4 files changed

+120
-137
lines changed

packages/grpc-reflection/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@
3131
"generate-types": "proto-loader-gen-types --longs String --enums String --bytes Array --defaults --oneofs --includeComments --includeDirs proto/ -O src/generated grpc/reflection/v1/reflection.proto grpc/reflection/v1alpha/reflection.proto"
3232
},
3333
"dependencies": {
34-
"google-protobuf": "^3.21.2",
35-
"@grpc/proto-loader": "^0.7.10"
34+
"@grpc/proto-loader": "^0.7.10",
35+
"protobufjs": "^7.2.5"
3636
},
3737
"peerDependencies": {
3838
"@grpc/grpc-js": "^1.8.21"
3939
},
4040
"devDependencies": {
4141
"@grpc/grpc-js": "file:../grpc-js",
42-
"@types/google-protobuf": "^3.15.7",
4342
"copyfiles": "^2.4.1",
4443
"typescript": "^5.2.2"
4544
}
Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import {
2-
DescriptorProto,
3-
EnumDescriptorProto,
4-
EnumValueDescriptorProto,
5-
FieldDescriptorProto,
6-
FileDescriptorProto,
7-
MethodDescriptorProto,
8-
OneofDescriptorProto,
9-
ServiceDescriptorProto,
10-
} from 'google-protobuf/google/protobuf/descriptor_pb';
2+
IDescriptorProto,
3+
IEnumDescriptorProto,
4+
IEnumValueDescriptorProto,
5+
IFieldDescriptorProto,
6+
IFileDescriptorProto,
7+
IMethodDescriptorProto,
8+
IOneofDescriptorProto,
9+
IServiceDescriptorProto,
10+
} from 'protobufjs/ext/descriptor';
1111

1212
/** A set of functions for operating on protobuf objects as we visit them in a traversal */
1313
interface Visitor {
14-
field?: (fqn: string, file: FileDescriptorProto, field: FieldDescriptorProto) => void;
15-
extension?: (fqn: string, file: FileDescriptorProto, extension: FieldDescriptorProto) => void;
16-
oneOf?: (fqn: string, file: FileDescriptorProto, decl: OneofDescriptorProto) => void;
17-
message?: (fqn: string, file: FileDescriptorProto, msg: DescriptorProto) => void;
18-
enum?: (fqn: string, file: FileDescriptorProto, msg: EnumDescriptorProto) => void;
19-
enumValue?: (fqn: string, file: FileDescriptorProto, msg: EnumValueDescriptorProto) => void;
20-
service?: (fqn: string, file: FileDescriptorProto, msg: ServiceDescriptorProto) => void;
21-
method?: (fqn: string, file: FileDescriptorProto, method: MethodDescriptorProto) => void;
14+
field?: (fqn: string, file: IFileDescriptorProto, field: IFieldDescriptorProto) => void;
15+
extension?: (fqn: string, file: IFileDescriptorProto, extension: IFieldDescriptorProto) => void;
16+
oneOf?: (fqn: string, file: IFileDescriptorProto, decl: IOneofDescriptorProto) => void;
17+
message?: (fqn: string, file: IFileDescriptorProto, msg: IDescriptorProto) => void;
18+
enum?: (fqn: string, file: IFileDescriptorProto, msg: IEnumDescriptorProto) => void;
19+
enumValue?: (fqn: string, file: IFileDescriptorProto, msg: IEnumValueDescriptorProto) => void;
20+
service?: (fqn: string, file: IFileDescriptorProto, msg: IServiceDescriptorProto) => void;
21+
method?: (fqn: string, file: IFileDescriptorProto, method: IMethodDescriptorProto) => void;
2222
}
2323

2424
/** Visit each node in a protobuf file and perform an operation on it
@@ -29,82 +29,81 @@ interface Visitor {
2929
*
3030
* @see Visitor for the interface to interact with the nodes
3131
*/
32-
export const visit = (file: FileDescriptorProto, visitor: Visitor): void => {
33-
const processField = (prefix: string, file: FileDescriptorProto, field: FieldDescriptorProto) => {
34-
const fqn = `${prefix}.${field.getName()}`;
32+
export const visit = (file: IFileDescriptorProto, visitor: Visitor): void => {
33+
const processField = (prefix: string, file: IFileDescriptorProto, field: IFieldDescriptorProto) => {
34+
const fqn = `${prefix}.${field.name}`;
3535
if (visitor.field) {
3636
visitor.field(fqn, file, field);
3737
}
3838
};
3939

4040
const processExtension = (
4141
prefix: string,
42-
file: FileDescriptorProto,
43-
ext: FieldDescriptorProto,
42+
file: IFileDescriptorProto,
43+
ext: IFieldDescriptorProto,
4444
) => {
45-
const fqn = `${prefix}.${ext.getName()}`;
45+
const fqn = `${prefix}.${ext.name}`;
4646
if (visitor.extension) {
4747
visitor.extension(fqn, file, ext);
4848
}
4949
};
5050

51-
const processOneOf = (prefix: string, file: FileDescriptorProto, decl: OneofDescriptorProto) => {
52-
const fqn = `${prefix}.${decl.getName()}`;
51+
const processOneOf = (prefix: string, file: IFileDescriptorProto, decl: IOneofDescriptorProto) => {
52+
const fqn = `${prefix}.${decl.name}`;
5353
if (visitor.oneOf) {
5454
visitor.oneOf(fqn, file, decl);
5555
}
5656
};
5757

58-
const processEnum = (prefix: string, file: FileDescriptorProto, decl: EnumDescriptorProto) => {
59-
const fqn = `${prefix}.${decl.getName()}`;
58+
const processEnum = (prefix: string, file: IFileDescriptorProto, decl: IEnumDescriptorProto) => {
59+
const fqn = `${prefix}.${decl.name}`;
6060

6161
if (visitor.enum) {
6262
visitor.enum(fqn, file, decl);
6363
}
6464

65-
decl.getValueList().forEach((value) => {
66-
const valueFqn = `${fqn}.${value.getName()}`;
65+
decl.value?.forEach((value) => {
66+
const valueFqn = `${fqn}.${value.name}`;
6767
if (visitor.enumValue) {
6868
visitor.enumValue(valueFqn, file, value);
6969
}
7070
});
7171
};
7272

73-
const processMessage = (prefix: string, file: FileDescriptorProto, msg: DescriptorProto) => {
74-
const fqn = `${prefix}.${msg.getName()}`;
73+
const processMessage = (prefix: string, file: IFileDescriptorProto, msg: IDescriptorProto) => {
74+
const fqn = `${prefix}.${msg.name}`;
7575
if (visitor.message) {
7676
visitor.message(fqn, file, msg);
7777
}
7878

79-
msg.getNestedTypeList().forEach((type) => processMessage(fqn, file, type));
80-
msg.getEnumTypeList().forEach((type) => processEnum(fqn, file, type));
81-
msg.getFieldList().forEach((field) => processField(fqn, file, field));
82-
msg.getOneofDeclList().forEach((decl) => processOneOf(fqn, file, decl));
83-
msg.getExtensionList().forEach((ext) => processExtension(fqn, file, ext));
79+
msg.nestedType?.forEach((type) => processMessage(fqn, file, type));
80+
msg.enumType?.forEach((type) => processEnum(fqn, file, type));
81+
msg.field?.forEach((field) => processField(fqn, file, field));
82+
msg.oneofDecl?.forEach((decl) => processOneOf(fqn, file, decl));
83+
msg.extension?.forEach((ext) => processExtension(fqn, file, ext));
8484
};
8585

8686
const processService = (
8787
prefix: string,
88-
file: FileDescriptorProto,
89-
service: ServiceDescriptorProto,
88+
file: IFileDescriptorProto,
89+
service: IServiceDescriptorProto,
9090
) => {
91-
const fqn = `${prefix}.${service.getName()}`;
91+
const fqn = `${prefix}.${service.name}`;
9292
if (visitor.service) {
9393
visitor.service(fqn, file, service);
9494
}
9595

96-
service.getMethodList().forEach((method) => {
97-
const methodFqn = `${fqn}.${method.getName()}`;
96+
service.method?.forEach((method) => {
97+
const methodFqn = `${fqn}.${method.name}`;
9898
if (visitor.method) {
9999
visitor.method(methodFqn, file, method);
100100
}
101101
});
102102
};
103103

104-
const packageName = file.getPackage() || '';
105-
file.getEnumTypeList().forEach((type) => processEnum(packageName, file, type));
106-
file.getMessageTypeList().forEach((type) => processMessage(packageName, file, type));
107-
file.getServiceList().forEach((service) => processService(packageName, file, service));
108-
109-
file.getExtensionList().forEach((ext) => processExtension(packageName, file, ext));
104+
const packageName = file.package || '';
105+
file.enumType?.forEach((type) => processEnum(packageName, file, type));
106+
file.messageType?.forEach((type) => processMessage(packageName, file, type));
107+
file.service?.forEach((service) => processService(packageName, file, service));
108+
file.extension?.forEach((ext) => processExtension(packageName, file, ext));
110109
};

0 commit comments

Comments
 (0)