src_protobuf_js_5_common.js.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: src/protobuf_js_5_common.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: src/protobuf_js_5_common.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @license
  21. * Copyright 2017 gRPC authors.
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. *
  35. */
  36. /**
  37. * @module
  38. * @private
  39. */
  40. 'use strict';
  41. var _ = require('lodash');
  42. var client = require('./client');
  43. /**
  44. * Get a function that deserializes a specific type of protobuf.
  45. * @param {function()} cls The constructor of the message type to deserialize
  46. * @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
  47. * instead of Buffers. Defaults to false
  48. * @param {bool=} longsAsStrings Deserialize long values as strings instead of
  49. * objects. Defaults to true
  50. * @return {function(Buffer):cls} The deserialization function
  51. */
  52. exports.deserializeCls = function deserializeCls(cls, options) {
  53. /**
  54. * Deserialize a buffer to a message object
  55. * @param {Buffer} arg_buf The buffer to deserialize
  56. * @return {cls} The resulting object
  57. */
  58. return function deserialize(arg_buf) {
  59. // Convert to a native object with binary fields as Buffers (first argument)
  60. // and longs as strings (second argument)
  61. return cls.decode(arg_buf).toRaw(options.binaryAsBase64,
  62. options.longsAsStrings);
  63. };
  64. };
  65. var deserializeCls = exports.deserializeCls;
  66. /**
  67. * Get a function that serializes objects to a buffer by protobuf class.
  68. * @param {function()} Cls The constructor of the message type to serialize
  69. * @return {function(Cls):Buffer} The serialization function
  70. */
  71. exports.serializeCls = function serializeCls(Cls) {
  72. /**
  73. * Serialize an object to a Buffer
  74. * @param {Object} arg The object to serialize
  75. * @return {Buffer} The serialized object
  76. */
  77. return function serialize(arg) {
  78. return new Buffer(new Cls(arg).encode().toBuffer());
  79. };
  80. };
  81. var serializeCls = exports.serializeCls;
  82. /**
  83. * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
  84. * @param {ProtoBuf.Reflect.Namespace} value The value to get the name of
  85. * @return {string} The fully qualified name of the value
  86. */
  87. exports.fullyQualifiedName = function fullyQualifiedName(value) {
  88. if (value === null || value === undefined) {
  89. return '';
  90. }
  91. var name = value.name;
  92. var parent_name = fullyQualifiedName(value.parent);
  93. if (parent_name !== '') {
  94. name = parent_name + '.' + name;
  95. }
  96. return name;
  97. };
  98. var fullyQualifiedName = exports.fullyQualifiedName;
  99. /**
  100. * Return a map from method names to method attributes for the service.
  101. * @param {ProtoBuf.Reflect.Service} service The service to get attributes for
  102. * @param {Object=} options Options to apply to these attributes
  103. * @return {Object} The attributes map
  104. */
  105. exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
  106. options) {
  107. var prefix = '/' + fullyQualifiedName(service) + '/';
  108. var binaryAsBase64, longsAsStrings;
  109. if (options) {
  110. binaryAsBase64 = options.binaryAsBase64;
  111. longsAsStrings = options.longsAsStrings;
  112. }
  113. /* This slightly awkward construction is used to make sure we only use
  114. lodash@3.10.1-compatible functions. A previous version used
  115. _.fromPairs, which would be cleaner, but was introduced in lodash
  116. version 4 */
  117. return _.zipObject(_.map(service.children, function(method) {
  118. return _.camelCase(method.name);
  119. }), _.map(service.children, function(method) {
  120. return {
  121. originalName: method.name,
  122. path: prefix + method.name,
  123. requestStream: method.requestStream,
  124. responseStream: method.responseStream,
  125. requestType: method.resolvedRequestType,
  126. responseType: method.resolvedResponseType,
  127. requestSerialize: serializeCls(method.resolvedRequestType.build()),
  128. requestDeserialize: deserializeCls(method.resolvedRequestType.build(),
  129. options),
  130. responseSerialize: serializeCls(method.resolvedResponseType.build()),
  131. responseDeserialize: deserializeCls(method.resolvedResponseType.build(),
  132. options)
  133. };
  134. }));
  135. };
  136. var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
  137. /**
  138. * Load a gRPC object from an existing ProtoBuf.Reflect object.
  139. * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
  140. * @param {Object=} options Options to apply to the loaded object
  141. * @return {Object&lt;string, *>} The resulting gRPC object
  142. */
  143. exports.loadObject = function loadObject(value, options) {
  144. var result = {};
  145. if (!value) {
  146. return value;
  147. }
  148. if (value.hasOwnProperty('ns')) {
  149. return loadObject(value.ns, options);
  150. }
  151. if (value.className === 'Namespace') {
  152. _.each(value.children, function(child) {
  153. result[child.name] = loadObject(child, options);
  154. });
  155. return result;
  156. } else if (value.className === 'Service') {
  157. return client.makeClientConstructor(getProtobufServiceAttrs(value, options),
  158. options);
  159. } else if (value.className === 'Message' || value.className === 'Enum') {
  160. return value.build();
  161. } else {
  162. return value;
  163. }
  164. };
  165. /**
  166. * The primary purpose of this method is to distinguish between reflection
  167. * objects from different versions of ProtoBuf.js. This is just a heuristic,
  168. * checking for properties that are (currently) specific to this version of
  169. * ProtoBuf.js
  170. * @param {Object} obj The object to check
  171. * @return {boolean} Whether the object appears to be a Protobuf.js 5
  172. * ReflectionObject
  173. */
  174. exports.isProbablyProtobufJs5 = function isProbablyProtobufJs5(obj) {
  175. return _.isArray(obj.children) &amp;&amp; (typeof obj.build === 'function');
  176. };
  177. </code></pre>
  178. </article>
  179. </section>
  180. </div>
  181. <nav>
  182. <h2><a href="index.html">Home</a></h2><h3>Externals</h3><ul><li><a href="external-Duplex.html">Duplex</a></li><li><a href="external-EventEmitter.html">EventEmitter</a></li><li><a href="external-GoogleCredential.html">GoogleCredential</a></li><li><a href="external-Readable.html">Readable</a></li><li><a href="external-Writable.html">Writable</a></li></ul><h3>Classes</h3><ul><li><a href="grpc.Client.html">Client</a></li><li><a href="grpc.credentials-CallCredentials.html">CallCredentials</a></li><li><a href="grpc.credentials-ChannelCredentials.html">ChannelCredentials</a></li><li><a href="grpc.Metadata.html">Metadata</a></li><li><a href="grpc.Server.html">Server</a></li><li><a href="grpc.ServerCredentials.html">ServerCredentials</a></li><li><a href="grpc-ClientDuplexStream.html">ClientDuplexStream</a></li><li><a href="grpc-ClientReadableStream.html">ClientReadableStream</a></li><li><a href="grpc-ClientUnaryCall.html">ClientUnaryCall</a></li><li><a href="grpc-ClientWritableStream.html">ClientWritableStream</a></li><li><a href="grpc-ServerDuplexStream.html">ServerDuplexStream</a></li><li><a href="grpc-ServerReadableStream.html">ServerReadableStream</a></li><li><a href="grpc-ServerUnaryCall.html">ServerUnaryCall</a></li><li><a href="grpc-ServerWritableStream.html">ServerWritableStream</a></li></ul><h3>Events</h3><ul><li><a href="grpc-ClientDuplexStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientDuplexStream.html#event:status">status</a></li><li><a href="grpc-ClientReadableStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientReadableStream.html#event:status">status</a></li><li><a href="grpc-ClientUnaryCall.html#event:metadata">metadata</a></li><li><a href="grpc-ClientUnaryCall.html#event:status">status</a></li><li><a href="grpc-ClientWritableStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientWritableStream.html#event:status">status</a></li><li><a href="grpc-ServerDuplexStream.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerReadableStream.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerUnaryCall.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerWritableStream.html#~event:cancelled">cancelled</a></li></ul><h3>Namespaces</h3><ul><li><a href="grpc.html">grpc</a></li><li><a href="grpc.credentials.html">credentials</a></li></ul>
  183. </nav>
  184. <br class="clear">
  185. <footer>
  186. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.3</a> on Fri Sep 08 2017 11:10:31 GMT-0700 (PDT)
  187. </footer>
  188. <script> prettyPrint(); </script>
  189. <script src="scripts/linenumber.js"> </script>
  190. </body>
  191. </html>