Skip to content

Commit d86994d

Browse files
committed
Split file descriptor set logic into two utility functions
This change exposes loadFileDescriptorSetFromBuffer and loadFileDescriptorSetFromObject functions.
1 parent 8c50e2d commit d86994d

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

packages/proto-loader/src/index.ts

+34-38
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export type Options = Protobuf.IParseOptions &
109109
includeDirs?: string[];
110110
};
111111

112+
type DecodedDescriptorSet = Protobuf.Message<descriptor.IFileDescriptorSet> &
113+
descriptor.IFileDescriptorSet;
114+
112115
const descriptorOptions: Protobuf.IConversionOptions = {
113116
longs: String,
114117
enums: String,
@@ -318,6 +321,19 @@ function addIncludePathResolver(root: Protobuf.Root, includePaths: string[]) {
318321
};
319322
}
320323

324+
function createPackageDefinitionFromDescriptorSet(
325+
decodedDescriptorSet: DecodedDescriptorSet,
326+
options?: Options
327+
) {
328+
options = options || {};
329+
330+
const root = (Protobuf.Root as Protobuf.RootConstructor).fromDescriptor(
331+
decodedDescriptorSet
332+
);
333+
root.resolveAll();
334+
return createPackageDefinition(root, options);
335+
}
336+
321337
/**
322338
* Load a .proto file with the specified options.
323339
* @param filename One or multiple file paths to load. Can be an absolute path
@@ -379,52 +395,32 @@ export function loadSync(
379395
return createPackageDefinition(root, options!);
380396
}
381397

382-
export function loadFileDescriptorSet(
383-
descriptorSet:
384-
| Buffer
385-
| ReturnType<typeof descriptor.FileDescriptorSet.toObject>,
398+
export function loadFileDescriptorSetFromBuffer(
399+
descriptorSet: Buffer,
386400
options?: Options
387401
): PackageDefinition {
388-
type DecodedDescriptorSet = Protobuf.Message<descriptor.IFileDescriptorSet> &
389-
descriptor.IFileDescriptorSet;
402+
const decodedDescriptorSet = descriptor.FileDescriptorSet.decode(
403+
descriptorSet
404+
) as DecodedDescriptorSet;
390405

391-
options = options || {};
392-
393-
let decodedDescriptorSet: DecodedDescriptorSet;
394-
if (Buffer.isBuffer(descriptorSet)) {
395-
decodedDescriptorSet = descriptor.FileDescriptorSet.decode(
396-
descriptorSet
397-
) as DecodedDescriptorSet;
398-
} else {
399-
decodedDescriptorSet = descriptor.FileDescriptorSet.fromObject(
400-
descriptorSet
401-
) as DecodedDescriptorSet;
402-
}
403-
404-
const root = (Protobuf.Root as Protobuf.RootConstructor).fromDescriptor(
405-
decodedDescriptorSet
406+
return createPackageDefinitionFromDescriptorSet(
407+
decodedDescriptorSet,
408+
options
406409
);
407-
root.resolveAll();
408-
return createPackageDefinition(root, options);
409410
}
410411

411-
export function loadFileDescriptorSetFile(
412-
filename: string,
412+
export function loadFileDescriptorSetFromObject(
413+
descriptorSet: Parameters<typeof descriptor.FileDescriptorSet.fromObject>[0],
413414
options?: Options
414-
): Promise<PackageDefinition> {
415-
return new Promise((resolve, reject) => {
416-
fs.readFile(filename, (err, data) => {
417-
if (err) {
418-
return reject(err);
419-
}
420-
421-
try {
422-
data = JSON.parse(data.toString());
423-
} catch (e) {}
415+
): PackageDefinition {
416+
const decodedDescriptorSet = descriptor.FileDescriptorSet.fromObject(
417+
descriptorSet
418+
) as DecodedDescriptorSet;
424419

425-
return resolve(loadFileDescriptorSet(data, options));
426-
});
427-
});
420+
return createPackageDefinitionFromDescriptorSet(
421+
decodedDescriptorSet,
422+
options
423+
);
428424
}
429425

430426
// Load Google's well-known proto files that aren't exposed by Protobuf.js.

packages/proto-loader/test/descriptor_type_test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import * as assert from 'assert';
1919
import { rpcFileDescriptorSet } from '../test_protos/rpc.desc';
20+
import { readFileSync } from 'fs';
2021

2122
import * as proto_loader from '../src/index';
2223

@@ -102,17 +103,20 @@ describe('Descriptor types', () => {
102103
});
103104

104105
it('Can load binary-encoded proto file descriptor sets', () => {
106+
const buffer = readFileSync(`${TEST_PROTO_DIR}/rpc.desc.bin`);
105107
// This will throw if the rpc descriptor cannot be decoded
106-
proto_loader.loadFileDescriptorSetFile(`${TEST_PROTO_DIR}/rpc.desc.bin`);
108+
proto_loader.loadFileDescriptorSetFromBuffer(buffer);
107109
});
108110

109111
it('Can load json file descriptor sets', () => {
112+
const buffer = readFileSync(`${TEST_PROTO_DIR}/rpc.desc.json`);
113+
const json = JSON.parse(buffer.toString());
110114
// This will throw if the rpc descriptor JSON cannot be decoded
111-
proto_loader.loadFileDescriptorSetFile(`${TEST_PROTO_DIR}/rpc.desc.json`);
115+
proto_loader.loadFileDescriptorSetFromObject(json);
112116
});
113117

114118
it('Can parse plain file descriptor set objects', () => {
115119
// This will throw if the file descriptor object cannot be parsed
116-
proto_loader.loadFileDescriptorSet(rpcFileDescriptorSet);
120+
proto_loader.loadFileDescriptorSetFromObject(rpcFileDescriptorSet);
117121
});
118122
});

0 commit comments

Comments
 (0)