Refactor loadFileDescriptorSet

This change refactors the loadFileDescriptorSetFile function to take
a plain Buffer or Javascript object as input.
This commit is contained in:
A. Tate Barber 2020-11-23 21:24:39 -06:00
parent 08254e4d2e
commit bf98c167cd
3 changed files with 75 additions and 11 deletions

View File

@ -379,14 +379,30 @@ export function loadSync(
return createPackageDefinition(root, options!);
}
export async function loadFileDescriptorSet(
descriptorSet: Protobuf.Message<descriptor.IFileDescriptorSet> &
descriptor.IFileDescriptorSet,
export function loadFileDescriptorSet(
descriptorSet:
| Buffer
| ReturnType<typeof descriptor.FileDescriptorSet.toObject>,
options?: Options
): Promise<PackageDefinition> {
): PackageDefinition {
type DecodedDescriptorSet = Protobuf.Message<descriptor.IFileDescriptorSet> &
descriptor.IFileDescriptorSet;
options = options || {};
let decodedDescriptorSet: DecodedDescriptorSet;
if (typeof descriptorSet === 'object') {
decodedDescriptorSet = descriptor.FileDescriptorSet.fromObject(
descriptorSet
) as DecodedDescriptorSet;
} else {
decodedDescriptorSet = descriptor.FileDescriptorSet.decode(
descriptorSet
) as DecodedDescriptorSet;
}
const root = (Protobuf.Root as Protobuf.RootConstructor).fromDescriptor(
descriptorSet
decodedDescriptorSet
);
root.resolveAll();
return createPackageDefinition(root, options);
@ -402,11 +418,7 @@ export function loadFileDescriptorSetFile(
return reject(err);
}
const descriptorSet = descriptor.FileDescriptorSet.decode(
data
) as Protobuf.Message<descriptor.IFileDescriptorSet> &
descriptor.IFileDescriptorSet;
return resolve(loadFileDescriptorSet(descriptorSet, options));
return resolve(loadFileDescriptorSet(data, options));
});
});
}

View File

@ -16,6 +16,7 @@
*/
import * as assert from 'assert';
import { rpcFileDescriptorSet } from '../test_protos/rpc.desc';
import * as proto_loader from '../src/index';
@ -101,7 +102,12 @@ describe('Descriptor types', () => {
});
it('Can load binary-encoded proto file descriptor sets', () => {
// This will throw if the well known protos are not available.
// This will throw if the rpc descriptor cannot be decoded
proto_loader.loadFileDescriptorSetFile(`${TEST_PROTO_DIR}/rpc.desc`);
});
it('Can parse plain file descriptor set objects', () => {
// This will throw if the file descriptor object cannot be parsed
proto_loader.loadFileDescriptorSet(rpcFileDescriptorSet);
});
});

View File

@ -0,0 +1,46 @@
export const rpcFileDescriptorSet = {
"file": [
{
"name": "test_protos/rpc.proto",
"messageType": [
{
"name": "MyRequest",
"field": [
{
"name": "path",
"number": 1,
"label": "LABEL_OPTIONAL",
"type": "TYPE_STRING",
"jsonName": "path"
}
]
},
{
"name": "MyResponse",
"field": [
{
"name": "status",
"number": 2,
"label": "LABEL_OPTIONAL",
"type": "TYPE_INT32",
"jsonName": "status"
}
]
}
],
"service": [
{
"name": "MyService",
"method": [
{
"name": "MyMethod",
"inputType": ".MyRequest",
"outputType": ".MyResponse"
}
]
}
],
"syntax": "proto3"
}
]
}