From d86994dc6ab3c581d9c5baff42936f0bed9df09a Mon Sep 17 00:00:00 2001 From: "A. Tate Barber" Date: Mon, 30 Nov 2020 20:45:18 -0600 Subject: [PATCH] Split file descriptor set logic into two utility functions This change exposes loadFileDescriptorSetFromBuffer and loadFileDescriptorSetFromObject functions. --- packages/proto-loader/src/index.ts | 72 +++++++++---------- .../proto-loader/test/descriptor_type_test.ts | 10 ++- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/packages/proto-loader/src/index.ts b/packages/proto-loader/src/index.ts index 6b30e36e..c51bc011 100644 --- a/packages/proto-loader/src/index.ts +++ b/packages/proto-loader/src/index.ts @@ -109,6 +109,9 @@ export type Options = Protobuf.IParseOptions & includeDirs?: string[]; }; +type DecodedDescriptorSet = Protobuf.Message & + descriptor.IFileDescriptorSet; + const descriptorOptions: Protobuf.IConversionOptions = { longs: String, enums: String, @@ -318,6 +321,19 @@ function addIncludePathResolver(root: Protobuf.Root, includePaths: string[]) { }; } +function createPackageDefinitionFromDescriptorSet( + decodedDescriptorSet: DecodedDescriptorSet, + options?: Options +) { + options = options || {}; + + const root = (Protobuf.Root as Protobuf.RootConstructor).fromDescriptor( + decodedDescriptorSet + ); + root.resolveAll(); + return createPackageDefinition(root, options); +} + /** * Load a .proto file with the specified options. * @param filename One or multiple file paths to load. Can be an absolute path @@ -379,52 +395,32 @@ export function loadSync( return createPackageDefinition(root, options!); } -export function loadFileDescriptorSet( - descriptorSet: - | Buffer - | ReturnType, +export function loadFileDescriptorSetFromBuffer( + descriptorSet: Buffer, options?: Options ): PackageDefinition { - type DecodedDescriptorSet = Protobuf.Message & - descriptor.IFileDescriptorSet; + const decodedDescriptorSet = descriptor.FileDescriptorSet.decode( + descriptorSet + ) as DecodedDescriptorSet; - options = options || {}; - - let decodedDescriptorSet: DecodedDescriptorSet; - if (Buffer.isBuffer(descriptorSet)) { - decodedDescriptorSet = descriptor.FileDescriptorSet.decode( - descriptorSet - ) as DecodedDescriptorSet; - } else { - decodedDescriptorSet = descriptor.FileDescriptorSet.fromObject( - descriptorSet - ) as DecodedDescriptorSet; - } - - const root = (Protobuf.Root as Protobuf.RootConstructor).fromDescriptor( - decodedDescriptorSet + return createPackageDefinitionFromDescriptorSet( + decodedDescriptorSet, + options ); - root.resolveAll(); - return createPackageDefinition(root, options); } -export function loadFileDescriptorSetFile( - filename: string, +export function loadFileDescriptorSetFromObject( + descriptorSet: Parameters[0], options?: Options -): Promise { - return new Promise((resolve, reject) => { - fs.readFile(filename, (err, data) => { - if (err) { - return reject(err); - } +): PackageDefinition { + const decodedDescriptorSet = descriptor.FileDescriptorSet.fromObject( + descriptorSet + ) as DecodedDescriptorSet; - try { - data = JSON.parse(data.toString()); - } catch (e) {} - - return resolve(loadFileDescriptorSet(data, options)); - }); - }); + return createPackageDefinitionFromDescriptorSet( + decodedDescriptorSet, + options + ); } // Load Google's well-known proto files that aren't exposed by Protobuf.js. diff --git a/packages/proto-loader/test/descriptor_type_test.ts b/packages/proto-loader/test/descriptor_type_test.ts index 7571da0a..32aca9fc 100644 --- a/packages/proto-loader/test/descriptor_type_test.ts +++ b/packages/proto-loader/test/descriptor_type_test.ts @@ -17,6 +17,7 @@ import * as assert from 'assert'; import { rpcFileDescriptorSet } from '../test_protos/rpc.desc'; +import { readFileSync } from 'fs'; import * as proto_loader from '../src/index'; @@ -102,17 +103,20 @@ describe('Descriptor types', () => { }); it('Can load binary-encoded proto file descriptor sets', () => { + const buffer = readFileSync(`${TEST_PROTO_DIR}/rpc.desc.bin`); // This will throw if the rpc descriptor cannot be decoded - proto_loader.loadFileDescriptorSetFile(`${TEST_PROTO_DIR}/rpc.desc.bin`); + proto_loader.loadFileDescriptorSetFromBuffer(buffer); }); it('Can load json file descriptor sets', () => { + const buffer = readFileSync(`${TEST_PROTO_DIR}/rpc.desc.json`); + const json = JSON.parse(buffer.toString()); // This will throw if the rpc descriptor JSON cannot be decoded - proto_loader.loadFileDescriptorSetFile(`${TEST_PROTO_DIR}/rpc.desc.json`); + proto_loader.loadFileDescriptorSetFromObject(json); }); it('Can parse plain file descriptor set objects', () => { // This will throw if the file descriptor object cannot be parsed - proto_loader.loadFileDescriptorSet(rpcFileDescriptorSet); + proto_loader.loadFileDescriptorSetFromObject(rpcFileDescriptorSet); }); });