src_protobuf_js_5_common.js.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. *
  21. * Copyright 2017, Google Inc.
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions are
  26. * met:
  27. *
  28. * * Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * * Redistributions in binary form must reproduce the above
  31. * copyright notice, this list of conditions and the following disclaimer
  32. * in the documentation and/or other materials provided with the
  33. * distribution.
  34. * * Neither the name of Google Inc. nor the names of its
  35. * contributors may be used to endorse or promote products derived from
  36. * this software without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  42. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  45. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  46. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. */
  51. 'use strict';
  52. var _ = require('lodash');
  53. var client = require('./client');
  54. /**
  55. * Get a function that deserializes a specific type of protobuf.
  56. * @param {function()} cls The constructor of the message type to deserialize
  57. * @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
  58. * instead of Buffers. Defaults to false
  59. * @param {bool=} longsAsStrings Deserialize long values as strings instead of
  60. * objects. Defaults to true
  61. * @return {function(Buffer):cls} The deserialization function
  62. */
  63. exports.deserializeCls = function deserializeCls(cls, binaryAsBase64,
  64. longsAsStrings) {
  65. /**
  66. * Deserialize a buffer to a message object
  67. * @param {Buffer} arg_buf The buffer to deserialize
  68. * @return {cls} The resulting object
  69. */
  70. return function deserialize(arg_buf) {
  71. // Convert to a native object with binary fields as Buffers (first argument)
  72. // and longs as strings (second argument)
  73. return cls.decode(arg_buf).toRaw(binaryAsBase64, longsAsStrings);
  74. };
  75. };
  76. var deserializeCls = exports.deserializeCls;
  77. /**
  78. * Get a function that serializes objects to a buffer by protobuf class.
  79. * @param {function()} Cls The constructor of the message type to serialize
  80. * @return {function(Cls):Buffer} The serialization function
  81. */
  82. exports.serializeCls = function serializeCls(Cls) {
  83. /**
  84. * Serialize an object to a Buffer
  85. * @param {Object} arg The object to serialize
  86. * @return {Buffer} The serialized object
  87. */
  88. return function serialize(arg) {
  89. return new Buffer(new Cls(arg).encode().toBuffer());
  90. };
  91. };
  92. var serializeCls = exports.serializeCls;
  93. /**
  94. * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
  95. * @param {ProtoBuf.Reflect.Namespace} value The value to get the name of
  96. * @return {string} The fully qualified name of the value
  97. */
  98. exports.fullyQualifiedName = function fullyQualifiedName(value) {
  99. if (value === null || value === undefined) {
  100. return '';
  101. }
  102. var name = value.name;
  103. var parent_name = fullyQualifiedName(value.parent);
  104. if (parent_name !== '') {
  105. name = parent_name + '.' + name;
  106. }
  107. return name;
  108. };
  109. var fullyQualifiedName = exports.fullyQualifiedName;
  110. /**
  111. * Return a map from method names to method attributes for the service.
  112. * @param {ProtoBuf.Reflect.Service} service The service to get attributes for
  113. * @param {Object=} options Options to apply to these attributes
  114. * @return {Object} The attributes map
  115. */
  116. exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
  117. options) {
  118. var prefix = '/' + fullyQualifiedName(service) + '/';
  119. var binaryAsBase64, longsAsStrings;
  120. if (options) {
  121. binaryAsBase64 = options.binaryAsBase64;
  122. longsAsStrings = options.longsAsStrings;
  123. }
  124. /* This slightly awkward construction is used to make sure we only use
  125. lodash@3.10.1-compatible functions. A previous version used
  126. _.fromPairs, which would be cleaner, but was introduced in lodash
  127. version 4 */
  128. return _.zipObject(_.map(service.children, function(method) {
  129. return _.camelCase(method.name);
  130. }), _.map(service.children, function(method) {
  131. return {
  132. originalName: method.name,
  133. path: prefix + method.name,
  134. requestStream: method.requestStream,
  135. responseStream: method.responseStream,
  136. requestType: method.resolvedRequestType,
  137. responseType: method.resolvedResponseType,
  138. requestSerialize: serializeCls(method.resolvedRequestType.build()),
  139. requestDeserialize: deserializeCls(method.resolvedRequestType.build(),
  140. binaryAsBase64, longsAsStrings),
  141. responseSerialize: serializeCls(method.resolvedResponseType.build()),
  142. responseDeserialize: deserializeCls(method.resolvedResponseType.build(),
  143. binaryAsBase64, longsAsStrings)
  144. };
  145. }));
  146. };
  147. var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
  148. /**
  149. * Load a gRPC object from an existing ProtoBuf.Reflect object.
  150. * @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
  151. * @param {Object=} options Options to apply to the loaded object
  152. * @return {Object&lt;string, *>} The resulting gRPC object
  153. */
  154. exports.loadObject = function loadObject(value, options) {
  155. var result = {};
  156. if (!value) {
  157. return value;
  158. }
  159. if (value.hasOwnProperty('ns')) {
  160. return loadObject(value.ns, options);
  161. }
  162. if (value.className === 'Namespace') {
  163. _.each(value.children, function(child) {
  164. result[child.name] = loadObject(child, options);
  165. });
  166. return result;
  167. } else if (value.className === 'Service') {
  168. return client.makeClientConstructor(getProtobufServiceAttrs(value, options),
  169. options);
  170. } else if (value.className === 'Message' || value.className === 'Enum') {
  171. return value.build();
  172. } else {
  173. return value;
  174. }
  175. };
  176. /**
  177. * The primary purpose of this method is to distinguish between reflection
  178. * objects from different versions of ProtoBuf.js. This is just a heuristic,
  179. * checking for properties that are (currently) specific to this version of
  180. * ProtoBuf.js
  181. * @param {Object} obj The object to check
  182. * @return {boolean} Whether the object appears to be a Protobuf.js 5
  183. * ReflectionObject
  184. */
  185. exports.isProbablyProtobufJs5 = function isProbablyProtobufJs5(obj) {
  186. return _.isArray(obj.children) &amp;&amp; (typeof obj.build === 'function');
  187. };
  188. </code></pre>
  189. </article>
  190. </section>
  191. </div>
  192. <nav>
  193. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-src_client.html">src/client</a></li><li><a href="module-src_common.html">src/common</a></li><li><a href="module-src_credentials.html">src/credentials</a></li><li><a href="module-src_metadata.html">src/metadata</a></li><li><a href="module-src_server.html">src/server</a></li></ul><h3>Classes</h3><ul><li><a href="module-src_client.makeClientConstructor-Client.html">Client</a></li><li><a href="module-src_client-ClientDuplexStream.html">ClientDuplexStream</a></li><li><a href="module-src_client-ClientReadableStream.html">ClientReadableStream</a></li><li><a href="module-src_client-ClientWritableStream.html">ClientWritableStream</a></li><li><a href="module-src_metadata-Metadata.html">Metadata</a></li><li><a href="module-src_server-Server.html">Server</a></li><li><a href="module-src_server-ServerDuplexStream.html">ServerDuplexStream</a></li><li><a href="module-src_server-ServerReadableStream.html">ServerReadableStream</a></li><li><a href="module-src_server-ServerWritableStream.html">ServerWritableStream</a></li></ul><h3>Global</h3><ul><li><a href="global.html#callError">callError</a></li><li><a href="global.html#credentials">credentials</a></li><li><a href="global.html#deserializeCls">deserializeCls</a></li><li><a href="global.html#fullyQualifiedName">fullyQualifiedName</a></li><li><a href="global.html#getClientChannel">getClientChannel</a></li><li><a href="global.html#getProtobufServiceAttrs">getProtobufServiceAttrs</a></li><li><a href="global.html#isProbablyProtobufJs5">isProbablyProtobufJs5</a></li><li><a href="global.html#isProbablyProtobufJs6">isProbablyProtobufJs6</a></li><li><a href="global.html#load">load</a></li><li><a href="global.html#loadObject">loadObject</a></li><li><a href="global.html#logVerbosity">logVerbosity</a></li><li><a href="global.html#makeGenericClientConstructor">makeGenericClientConstructor</a></li><li><a href="global.html#Metadata">Metadata</a></li><li><a href="global.html#propagate">propagate</a></li><li><a href="global.html#serializeCls">serializeCls</a></li><li><a href="global.html#Server">Server</a></li><li><a href="global.html#ServerCredentials">ServerCredentials</a></li><li><a href="global.html#setLogger">setLogger</a></li><li><a href="global.html#setLogVerbosity">setLogVerbosity</a></li><li><a href="global.html#status">status</a></li><li><a href="global.html#waitForClientReady">waitForClientReady</a></li><li><a href="global.html#writeFlags">writeFlags</a></li></ul>
  194. </nav>
  195. <br class="clear">
  196. <footer>
  197. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Thu Apr 27 2017 17:35:39 GMT-0700 (PDT)
  198. </footer>
  199. <script> prettyPrint(); </script>
  200. <script src="scripts/linenumber.js"> </script>
  201. </body>
  202. </html>