src_common.js.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: src/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/common.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/*
  20. *
  21. * Copyright 2015, 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. /**
  52. * This module contains functions that are common to client and server
  53. * code. None of them should be used directly by gRPC users.
  54. * @module
  55. */
  56. 'use strict';
  57. var _ = require('lodash');
  58. /**
  59. * Get a function that deserializes a specific type of protobuf.
  60. * @param {function()} cls The constructor of the message type to deserialize
  61. * @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
  62. * instead of Buffers. Defaults to false
  63. * @param {bool=} longsAsStrings Deserialize long values as strings instead of
  64. * objects. Defaults to true
  65. * @return {function(Buffer):cls} The deserialization function
  66. */
  67. exports.deserializeCls = function deserializeCls(cls, binaryAsBase64,
  68. longsAsStrings) {
  69. if (binaryAsBase64 === undefined || binaryAsBase64 === null) {
  70. binaryAsBase64 = false;
  71. }
  72. if (longsAsStrings === undefined || longsAsStrings === null) {
  73. longsAsStrings = true;
  74. }
  75. /**
  76. * Deserialize a buffer to a message object
  77. * @param {Buffer} arg_buf The buffer to deserialize
  78. * @return {cls} The resulting object
  79. */
  80. return function deserialize(arg_buf) {
  81. // Convert to a native object with binary fields as Buffers (first argument)
  82. // and longs as strings (second argument)
  83. return cls.decode(arg_buf).toRaw(binaryAsBase64, longsAsStrings);
  84. };
  85. };
  86. var deserializeCls = exports.deserializeCls;
  87. /**
  88. * Get a function that serializes objects to a buffer by protobuf class.
  89. * @param {function()} Cls The constructor of the message type to serialize
  90. * @return {function(Cls):Buffer} The serialization function
  91. */
  92. exports.serializeCls = function serializeCls(Cls) {
  93. /**
  94. * Serialize an object to a Buffer
  95. * @param {Object} arg The object to serialize
  96. * @return {Buffer} The serialized object
  97. */
  98. return function serialize(arg) {
  99. return new Buffer(new Cls(arg).encode().toBuffer());
  100. };
  101. };
  102. var serializeCls = exports.serializeCls;
  103. /**
  104. * Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
  105. * @param {ProtoBuf.Reflect.Namespace} value The value to get the name of
  106. * @return {string} The fully qualified name of the value
  107. */
  108. exports.fullyQualifiedName = function fullyQualifiedName(value) {
  109. if (value === null || value === undefined) {
  110. return '';
  111. }
  112. var name = value.name;
  113. var parent_name = fullyQualifiedName(value.parent);
  114. if (parent_name !== '') {
  115. name = parent_name + '.' + name;
  116. }
  117. return name;
  118. };
  119. var fullyQualifiedName = exports.fullyQualifiedName;
  120. /**
  121. * Wrap a function to pass null-like values through without calling it. If no
  122. * function is given, just uses the identity;
  123. * @param {?function} func The function to wrap
  124. * @return {function} The wrapped function
  125. */
  126. exports.wrapIgnoreNull = function wrapIgnoreNull(func) {
  127. if (!func) {
  128. return _.identity;
  129. }
  130. return function(arg) {
  131. if (arg === null || arg === undefined) {
  132. return null;
  133. }
  134. return func(arg);
  135. };
  136. };
  137. /**
  138. * Return a map from method names to method attributes for the service.
  139. * @param {ProtoBuf.Reflect.Service} service The service to get attributes for
  140. * @param {Object=} options Options to apply to these attributes
  141. * @return {Object} The attributes map
  142. */
  143. exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
  144. options) {
  145. var prefix = '/' + fullyQualifiedName(service) + '/';
  146. var binaryAsBase64, longsAsStrings;
  147. if (options) {
  148. binaryAsBase64 = options.binaryAsBase64;
  149. longsAsStrings = options.longsAsStrings;
  150. }
  151. return _.object(_.map(service.children, function(method) {
  152. return [_.camelCase(method.name), {
  153. path: prefix + method.name,
  154. requestStream: method.requestStream,
  155. responseStream: method.responseStream,
  156. requestType: method.resolvedRequestType,
  157. responseType: method.resolvedResponseType,
  158. requestSerialize: serializeCls(method.resolvedRequestType.build()),
  159. requestDeserialize: deserializeCls(method.resolvedRequestType.build(),
  160. binaryAsBase64, longsAsStrings),
  161. responseSerialize: serializeCls(method.resolvedResponseType.build()),
  162. responseDeserialize: deserializeCls(method.resolvedResponseType.build(),
  163. binaryAsBase64, longsAsStrings)
  164. }];
  165. }));
  166. };
  167. </code></pre>
  168. </article>
  169. </section>
  170. </div>
  171. <nav>
  172. <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#getClientChannel">getClientChannel</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#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#Server">Server</a></li><li><a href="global.html#ServerCredentials">ServerCredentials</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>
  173. </nav>
  174. <br class="clear">
  175. <footer>
  176. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed May 11 2016 18:17:25 GMT-0700 (PDT)
  177. </footer>
  178. <script> prettyPrint(); </script>
  179. <script src="scripts/linenumber.js"> </script>
  180. </body>
  181. </html>