3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 var camelCase = require('lodash.camelcase');
27 var client = require('./client');
28 var common = require('./common');
31 * Get a function that deserializes a specific type of protobuf.
32 * @param {function()} cls The constructor of the message type to deserialize
33 * @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
34 * instead of Buffers. Defaults to false
35 * @param {bool=} longsAsStrings Deserialize long values as strings instead of
36 * objects. Defaults to true
37 * @return {function(Buffer):cls} The deserialization function
39 exports.deserializeCls = function deserializeCls(cls, options) {
40 var conversion_options = {
42 bytes: options.binaryAsBase64 ? String : Buffer,
43 longs: options.longsAsStrings ? String : null,
44 enums: options.enumsAsStrings ? String : null,
48 * Deserialize a buffer to a message object
49 * @param {Buffer} arg_buf The buffer to deserialize
50 * @return {cls} The resulting object
52 return function deserialize(arg_buf) {
53 return cls.toObject(cls.decode(arg_buf), conversion_options);
57 var deserializeCls = exports.deserializeCls;
60 * Get a function that serializes objects to a buffer by protobuf class.
61 * @param {function()} Cls The constructor of the message type to serialize
62 * @return {function(Cls):Buffer} The serialization function
64 exports.serializeCls = function serializeCls(cls) {
66 * Serialize an object to a Buffer
67 * @param {Object} arg The object to serialize
68 * @return {Buffer} The serialized object
70 return function serialize(arg) {
71 var message = cls.fromObject(arg);
72 return cls.encode(message).finish();
76 var serializeCls = exports.serializeCls;
79 * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
80 * @param {ProtoBuf.ReflectionObject} value The value to get the name of
81 * @return {string} The fully qualified name of the value
83 exports.fullyQualifiedName = function fullyQualifiedName(value) {
84 if (value === null || value === undefined) {
87 var name = value.name;
88 var parent_fqn = fullyQualifiedName(value.parent);
89 if (parent_fqn !== '') {
90 name = parent_fqn + '.' + name;
95 var fullyQualifiedName = exports.fullyQualifiedName;
98 * Return a map from method names to method attributes for the service.
99 * @param {ProtoBuf.Service} service The service to get attributes for
100 * @param {Object=} options Options to apply to these attributes
101 * @return {Object} The attributes map
103 exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
105 var prefix = '/' + fullyQualifiedName(service) + '/';
106 service.resolveAll();
107 return common.zipObject(service.methodsArray.map(function(method) {
108 return camelCase(method.name);
109 }), service.methodsArray.map(function(method) {
111 originalName: method.name,
112 path: prefix + method.name,
113 requestStream: !!method.requestStream,
114 responseStream: !!method.responseStream,
115 requestType: method.resolvedRequestType,
116 responseType: method.resolvedResponseType,
117 requestSerialize: serializeCls(method.resolvedRequestType),
118 requestDeserialize: deserializeCls(method.resolvedRequestType, options),
119 responseSerialize: serializeCls(method.resolvedResponseType),
120 responseDeserialize: deserializeCls(method.resolvedResponseType, options)
125 var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
127 exports.loadObject = function loadObject(value, options) {
132 if (value.hasOwnProperty('methods')) {
133 // It's a service object
134 var service_attrs = getProtobufServiceAttrs(value, options);
135 return client.makeClientConstructor(service_attrs);
138 if (value.hasOwnProperty('nested')) {
139 // It's a namespace or root object
140 if (value.nested !== null && value.nested !== undefined) {
141 var values = Object.keys(value.nested).map(key => value.nested[key]);
142 values.forEach(nested => {
143 result[nested.name] = loadObject(nested, options);
149 // Otherwise, it's not something we need to change
154 * The primary purpose of this method is to distinguish between reflection
155 * objects from different versions of ProtoBuf.js. This is just a heuristic,
156 * checking for properties that are (currently) specific to this version of
158 * @param {Object} obj The object to check
159 * @return {boolean} Whether the object appears to be a Protobuf.js 6
162 exports.isProbablyProtobufJs6 = function isProbablyProtobufJs6(obj) {
163 return (typeof obj.root === 'object') && (typeof obj.resolve === 'function');