Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / src / protobuf_js_6_common.js
1 /**
2  * @license
3  * Copyright 2017 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 /**
20  * @module
21  * @private
22  */
23
24 'use strict';
25
26 var camelCase = require('lodash.camelcase');
27 var client = require('./client');
28 var common = require('./common');
29
30 /**
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
38  */
39 exports.deserializeCls = function deserializeCls(cls, options) {
40   var conversion_options = {
41     defaults: true,
42     bytes: options.binaryAsBase64 ? String : Buffer,
43     longs: options.longsAsStrings ? String : null,
44     enums: options.enumsAsStrings ? String : null,
45     oneofs: true
46   };
47   /**
48    * Deserialize a buffer to a message object
49    * @param {Buffer} arg_buf The buffer to deserialize
50    * @return {cls} The resulting object
51    */
52   return function deserialize(arg_buf) {
53     return cls.toObject(cls.decode(arg_buf), conversion_options);
54   };
55 };
56
57 var deserializeCls = exports.deserializeCls;
58
59 /**
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
63  */
64 exports.serializeCls = function serializeCls(cls) {
65   /**
66    * Serialize an object to a Buffer
67    * @param {Object} arg The object to serialize
68    * @return {Buffer} The serialized object
69    */
70   return function serialize(arg) {
71     var message = cls.fromObject(arg);
72     return cls.encode(message).finish();
73   };
74 };
75
76 var serializeCls = exports.serializeCls;
77
78 /**
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
82  */
83 exports.fullyQualifiedName = function fullyQualifiedName(value) {
84   if (value === null || value === undefined) {
85     return '';
86   }
87   var name = value.name;
88   var parent_fqn = fullyQualifiedName(value.parent);
89   if (parent_fqn !== '') {
90     name = parent_fqn + '.' + name;
91   }
92   return name;
93 };
94
95 var fullyQualifiedName = exports.fullyQualifiedName;
96
97 /**
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
102  */
103 exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
104                                                                    options) {
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) {
110     return {
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)
121     };
122   }));
123 };
124
125 var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
126
127 exports.loadObject = function loadObject(value, options) {
128   var result = {};
129   if (!value) {
130     return value;
131   }
132   if (value.hasOwnProperty('methods')) {
133     // It's a service object
134     var service_attrs = getProtobufServiceAttrs(value, options);
135     return client.makeClientConstructor(service_attrs);
136   }
137
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);
144       });
145     }
146     return result;
147   }
148
149   // Otherwise, it's not something we need to change
150   return value;
151 };
152
153 /**
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
157  * ProtoBuf.js
158  * @param {Object} obj The object to check
159  * @return {boolean} Whether the object appears to be a Protobuf.js 6
160  *   ReflectionObject
161  */
162 exports.isProbablyProtobufJs6 = function isProbablyProtobufJs6(obj) {
163   return (typeof obj.root === 'object') && (typeof obj.resolve === 'function');
164 };