From b1a02311b37c35b8813386d2d7fbb02e1e61d616 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Fri, 17 Mar 2017 13:45:15 -0700 Subject: Autodetect ProtoBuf.js version in grpc.loadObject --- src/node/index.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src/node/index.js') diff --git a/src/node/index.js b/src/node/index.js index 43a4a54dd8..e41664ebf3 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -58,12 +58,17 @@ var protobuf_js_6_common = require('./src/protobuf_js_6_common'); grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii')); exports.loadObject = function loadObject(value, options) { - options = _.defaults(options, {'protobufjs_version': 5}); - switch (options.protobufjs_version) { - case 5: return protobuf_js_5_common.loadObject(value, options); - case 6: return protobuf_js_6_common.loadObject(value, options); - default: throw new Error('Unrecognized protobufjs_version:', - options.protobufjs_version); + options = _.defaults(options, common.defaultGrpcOptions); + if (value instanceof ProtoBuf.ReflectionObject) { + return protobuf_js_6_common.loadObject(value, options); + } else { + /* If value is not a ProtoBuf.js 6 reflection object, we assume that it is + a ProtoBuf.js 5 reflection object, for backwards compatibility */ + var deprecation_message = 'Calling grpc.loadObject with an object ' + + 'generated by ProtoBuf.js 5 is deprecated. Please upgrade to ' + + 'ProtoBuf.js 6.'; + common.log(grpc.logVerbosity.INFO, deprecation_message); + return protobuf_js_5_common.loadObject(value, options); } }; @@ -108,7 +113,6 @@ exports.load = function load(filename, format, options) { still the possibility of adding other formats that would be loaded differently */ options = _.defaults(options, common.defaultGrpcOptions); - options.protobufjs_version = 6; var root = new ProtoBuf.Root(); var parse_options = {keepCase: !options.convertFieldsToCamelCase}; return loadObject(root.loadSync(applyProtoRoot(filename, root), -- cgit v1.2.3